aboutsummaryrefslogtreecommitdiff
path: root/pypride.py
diff options
context:
space:
mode:
Diffstat (limited to 'pypride.py')
-rw-r--r--pypride.py136
1 files changed, 75 insertions, 61 deletions
diff --git a/pypride.py b/pypride.py
index ee9182f..19b7016 100644
--- a/pypride.py
+++ b/pypride.py
@@ -16,13 +16,9 @@ class Pride:
if len(key) * 8 == 128:
self.key_whitening = string2number(key[:8])
self.key_1 = key[8:]
- #self.roundkeys = generateRoundkeys(key[8:])
else:
raise ValueError, "Key must be a 128-bit rawstring"
- # print self.key_whitening
- #print self.roundkeys
-
def encrypt(self,block):
"""Encrypt 1 block (8 bytes)
@@ -64,7 +60,7 @@ L0 = [[0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0],
[0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0],
[0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0],
[0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0]]
-
+L0_inv = L0
L1 = [[1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0],
[0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0],
[0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0],
@@ -81,43 +77,6 @@ L1 = [[1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0],
[0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,0,0,1,1,0,0,0,0]]
-
-L2 = [[0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1],
- [0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0],
- [1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0],
- [1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0],
- [0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0],
- [0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0],
- [0,0,0,1,1,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
+ [0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0],
+ [0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0],
+ [0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1],
+ [1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0],
+ [0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0],
+ [0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0],
+ [0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0],
+ [0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0],
+ [0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0],
+ [0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0],
+ [0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1],
+ [1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0],
+ [0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0],
+ [0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0],
+ [0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1]]
L3_inv = L3
def matrixMultiply(matrix, input):
- r_outs = []
- for r in matrix:
- r_out = 0;
- for c_i, c in reversed(list(enumerate(r))):
- r_out ^= c * ((input >> (15 - c_i)) & 0x1)
- r_outs.append(r_out)
- return sum([(1 << (15-i)) * v for i,v in enumerate(r_outs)])
+ """Multiply a vector with a binary matrix
+
+ Input: matrix as [[Int]];
+ input as Int
+ Output: Int"""
+
+ mult = [ reduce(
+ lambda a,b: a^b,
+ [c * ((input >> (15 - c_i)) & 0x1) for c_i, c in reversed(list(enumerate(r)))],
+ 0
+ ) for r in matrix ]
+ return sum([(1 << (15-i)) * v for i,v in enumerate(mult)])
def roundKey(key, i):
- return string2number(key[0] + chr((ord(key[1]) + 193 * i) % 256) + key[2] + chr((ord(key[3]) + 165 * i) % 256) + key[4] + chr((ord(key[5]) + 81 * i) % 256) + key[6] + chr((ord(key[7]) + 197 * i) % 256))
+ """Calculate a round key
+
+ Input: the base key (second half of it) as a rawstring;
+ the round number
+ Output: the round key as rawstring"""
+
+ return string2number(
+ key[0]
+ + chr((ord(key[1]) + 193 * i) % 256)
+ + key[2]
+ + chr((ord(key[3]) + 165 * i) % 256)
+ + key[4]
+ + chr((ord(key[5]) + 81 * i) % 256)
+ + key[6]
+ + chr((ord(key[7]) + 197 * i) % 256)
+ )
def addRoundKey(state,roundkey):
return state ^ roundkey
@@ -175,10 +184,7 @@ def sBoxLayer(state):
Input: 64-bit integer
Output: 64-bit integer"""
- output = 0
- for i in xrange(16):
- output += Sbox[( state >> (i*4)) & 0xF] << (i*4)
- return output
+ return sum([Sbox[( state >> (i*4)) & 0xF] << (i*4) for i in xrange(16)])
def sBoxLayer_dec(state):
"""Inverse SBox function for decryption
@@ -186,10 +192,7 @@ def sBoxLayer_dec(state):
Input: 64-bit integer
Output: 64-bit integer"""
- output = 0
- for i in xrange(16):
- output += Sbox_inv[( state >> (i*4)) & 0xF] << (i*4)
- return output
+ return sum([Sbox_inv[( state >> (i*4)) & 0xF] << (i*4) for i in xrange(16)])
def pLayer(state):
"""Permutation layer for encryption
@@ -208,8 +211,19 @@ def pLayer_dec(state):
return sum ([((state >> i) & 0x01) << PBox_inv[i] for i in xrange(64)])
def lLayer(state):
+ """Perform the L layer:
+ * P (permutation)
+ * L0 .. L3 on all four 16-bit substrings
+ * P_inv (permutation inverse)
+
+ Input: the current state, as an 8-byte rawstring
+ Output: the new state, as an 8-byte rawstring"""
+
state = pLayer(state)
- state = (matrixMultiply(L0, (state >> 48) & 0xffff) << 48) + (matrixMultiply(L1, (state >> 32) & 0xffff) << 32) + (matrixMultiply(L2, (state >> 16) & 0xffff) << 16) + matrixMultiply(L3, state & 0xffff)
+ state = (matrixMultiply(L0, (state >> 48) & 0xffff) << 48) + (
+ matrixMultiply(L1, (state >> 32) & 0xffff) << 32) + (
+ matrixMultiply(L2, (state >> 16) & 0xffff) << 16) + (
+ matrixMultiply(L3, state & 0xffff))
state = pLayer_dec(state)
return state