aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pypride.py76
1 files changed, 40 insertions, 36 deletions
diff --git a/pypride.py b/pypride.py
index c661168..cff778e 100644
--- a/pypride.py
+++ b/pypride.py
@@ -14,6 +14,9 @@
# Philippe Teuwen's PRESENT implementation:
# http://www.lightweightcrypto.org/downloads/implementations/pypresent.py
#
+# This implementation is slightly (~10%) faster using Python3 as opposed to
+# using Python2. If you're concerned with performance, consider Python3.
+#
# =============================================================================
#
# PRIDE is a modern (2014) lightweight block cipher optimized for 8-bit
@@ -80,19 +83,19 @@ class Pride:
state = string2number(block)
# Initial permutation & pre-whitening
- state = pLayer_dec(state)
+ state = permute_dec(state)
state = addRoundKey(state, self.whitening_key)
# 19 rounds R
for i in range (1,20):
state = addRoundKey(state, self.roundkeys[i])
- state = sBoxLayer(state)
- state = lLayer(state)
+ state = apply_SBox(state)
+ state = linear_layer(state)
# Last round R'
state = addRoundKey(state, self.roundkeys[20])
- state = sBoxLayer(state)
+ state = apply_SBox(state)
# Post-whitening & final permutation
state = addRoundKey(state, self.whitening_key)
- state = pLayer(state)
+ state = permute(state)
return number2string_N(state,8)
@@ -105,19 +108,19 @@ class Pride:
state = string2number(block)
# Final permutation & post-whitening
- state = pLayer_dec(state)
+ state = permute_dec(state)
state = addRoundKey(state, self.whitening_key)
# Last round R'
- state = sBoxLayer_dec(state)
+ state = apply_SBox_dec(state)
state = addRoundKey(state, self.roundkeys[20])
# 19 rounds R
for i in range(19,0,-1):
- state = lLayer_dec(state)
- state = sBoxLayer_dec(state)
+ state = linear_layer_dec(state)
+ state = apply_SBox_dec(state)
state = addRoundKey(state, self.roundkeys[i])
# Pre-whitening & initial permutation
state = addRoundKey(state, self.whitening_key)
- state = pLayer(state)
+ state = permute(state)
return number2string_N(state,8)
@@ -141,42 +144,42 @@ def ror(byte):
"""Rotate right (bit 7 := bit 0)"""
return (byte >> 1) | ((byte & 0x01) << 7)
-def M_L0(state):
+def apply_L0(state):
"""Apply matrix L0 using the method described in appendix H of the paper"""
s0 = state >> 8
s1 = state & 0x00ff
temp = swap(s1 ^ s0)
return ((s1 ^ temp) << 8) | (s0 ^ temp)
-def M_L1(state):
+def apply_L1(state):
"""Apply matrix L1 using the method described in appendix H of the paper"""
s2 = state >> 8
s3 = swap(state & 0x00ff)
temp = s2 ^ ror(s3)
return ((rol(s2) ^ temp) << 8) | (s3 ^ temp)
-def M_L1_inv(state):
+def apply_L1_inv(state):
"""Apply matrix L1^-1 using the method described in appendix H of the paper"""
s2 = ror(state >> 8)
s3 = state & 0x00ff
temp = s2 ^ ror(s3)
return ((ror(temp) ^ s2) << 8) | swap(temp ^ s3)
-def M_L2(state):
+def apply_L2(state):
"""Apply matrix L2 using the method described in appendix H of the paper"""
s4 = swap(state >> 8)
s5 = state & 0x00ff
temp = s4 ^ ror(s5)
return ((temp ^ rol(s4)) << 8) | (temp ^ s5)
-def M_L2_inv(state):
+def apply_L2_inv(state):
"""Apply matrix L2^-1 using the method described in appendix H of the paper"""
s4 = ror(state >> 8)
s5 = state & 0x00ff
temp = s4 ^ ror(s5)
return (swap(s4 ^ ror(temp)) << 8) | (s5 ^ temp)
-def M_L3(state):
+def apply_L3(state):
"""Apply matrix L3 using the method described in appendix H of the paper"""
s6 = state >> 8
s7 = state & 0x00ff
@@ -192,12 +195,12 @@ def roundKey(key, i):
key = string_bytes(key)
key_parts = [key[0], (key[1] + 193 * i) % 256, key[2], (key[3] + 165 * i) % 256, key[4], (key[5] + 81 * i) % 256, key[6], (key[7] + 197 * i) % 256]
- return pLayer_dec(sum([(1 << (8 * (7-i))) * k for i, k in enumerate(key_parts)]))
+ return permute_dec(sum([(1 << (8 * (7-i))) * k for i, k in enumerate(key_parts)]))
def addRoundKey(state,roundkey):
return state ^ roundkey
-def sBoxLayer(state):
+def apply_SBox(state):
"""SBox function for encryption
Input: 64-bit integer
@@ -205,7 +208,7 @@ def sBoxLayer(state):
return sum([Sbox[( state >> (i * 4)) & 0xf] << (i * 4) for i in range(16)])
-def sBoxLayer_dec(state):
+def apply_SBox_dec(state):
"""Inverse SBox function for decryption
Input: 64-bit integer
@@ -213,7 +216,7 @@ def sBoxLayer_dec(state):
return sum([Sbox_inv[( state >> (i * 4)) & 0xf] << (i * 4) for i in range(16)])
-def pLayer(state):
+def permute(state):
"""Permutation layer for encryption
Input: 64-bit integer
@@ -221,7 +224,7 @@ def pLayer(state):
return sum ([((state >> i) & 1) << PBox[i] for i in range(64)])
-def pLayer_dec(state):
+def permute_dec(state):
"""Permutation layer for decryption
Input: 64-bit integer
@@ -229,7 +232,7 @@ def pLayer_dec(state):
return sum ([((state >> i) & 1) << PBox_inv[i] for i in range(64)])
-def lLayer(state):
+def linear_layer(state):
"""Perform the L layer:
* P (permutation)
* L0 .. L3 on all four 16-bit substrings
@@ -238,15 +241,15 @@ def lLayer(state):
Input: the current state, as raw string
Output: the new state, as an raw string"""
- state = pLayer(state)
- state = (M_L0((state >> 48) & 0xffff) << 48) + (
- M_L1((state >> 32) & 0xffff) << 32) + (
- M_L2((state >> 16) & 0xffff) << 16) + (
- M_L3(state & 0xffff))
- return pLayer_dec(state)
+ state = permute(state)
+ state = (apply_L0((state >> 48) & 0xffff) << 48) + (
+ apply_L1((state >> 32) & 0xffff) << 32) + (
+ apply_L2((state >> 16) & 0xffff) << 16) + (
+ apply_L3(state & 0xffff))
+ return permute_dec(state)
-def lLayer_dec(state):
- """L layer for decryption:
+def linear_layer_dec(state):
+ """Linear layer for decryption:
* P (permutation)
* L0_inv .. L3_inv multiplication on the four 16-bit substrings, respectively
* P_inv (permutation inverse)
@@ -254,12 +257,12 @@ def lLayer_dec(state):
Input: the current state, as raw string
Output: the new state, as raw string"""
- state = pLayer(state)
- state = (M_L0((state >> 48) & 0xffff) << 48) + (
- M_L1_inv((state >> 32) & 0xffff) << 32) + (
- M_L2_inv((state >> 16) & 0xffff) << 16) + (
- M_L3(state & 0xffff))
- return pLayer_dec(state)
+ state = permute(state)
+ state = (apply_L0((state >> 48) & 0xffff) << 48) + (
+ apply_L1_inv((state >> 32) & 0xffff) << 32) + (
+ apply_L2_inv((state >> 16) & 0xffff) << 16) + (
+ apply_L3(state & 0xffff))
+ return permute_dec(state)
def string2number(i):
""" Convert a string to a number
@@ -281,3 +284,4 @@ def number2string_N(i, N):
def string_bytes(s):
return [ord(c) for c in s] if sys.version_info.major == 2 else s
+