summaryrefslogtreecommitdiff
path: root/python/lilliput_tbc.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/lilliput_tbc.py')
-rw-r--r--python/lilliput_tbc.py54
1 files changed, 27 insertions, 27 deletions
diff --git a/python/lilliput_tbc.py b/python/lilliput_tbc.py
index 418d083..9dc7df2 100644
--- a/python/lilliput_tbc.py
+++ b/python/lilliput_tbc.py
@@ -10,7 +10,7 @@ permutationInv = [13, 9, 14, 8, 10, 11, 12, 15, 4, 5, 3, 1, 2, 6 ,0 ,7]
################################################################################
-def BuildTweakey(tweak, key) :
+def BuildTweakey(tweak, key):
return tweak+key
#############################
@@ -18,7 +18,7 @@ def BuildTweakey(tweak, key) :
def _lane(TK, j):
return TK[j*8:(j+1)*8]
-def RoundTweakeySchedule(tweakey) :
+def RoundTweakeySchedule(tweakey):
p = len(tweakey)//8
multiplied_lanes = (
@@ -45,7 +45,7 @@ def TweakeyScheduleWhole(tweakey, r):
TKs = [tweakey]
RTKs = [SubTweakeyExtract(TKs[0], 0)]
- for i in range(1, r) :
+ for i in range(1, r):
TKs.append(RoundTweakeySchedule(TKs[i-1]))
RTKs.append(SubTweakeyExtract(TKs[i], i))
@@ -54,69 +54,69 @@ def TweakeyScheduleWhole(tweakey, r):
################################################################################
-def NonLinearLayer(state, subtweakey) :
+def NonLinearLayer(state, subtweakey):
variables_xored = [0 for byte in range(0, 8)]
- for byte in range(0,8) :
+ for byte in range(0,8):
variables_xored[byte] = state[byte] ^ subtweakey[byte]
variables_sboxed = [0 for byte in range(0, 8)]
- for byte in range(0, 8) :
+ for byte in range(0, 8):
variables_sboxed[byte] = Sbox[variables_xored[byte]]
state_output = [0 for byte in range(0, BLOCK_BYTES)]
- for byte in range(0,BLOCK_BYTES) :
+ for byte in range(0,BLOCK_BYTES):
state_output[byte] = state[byte]
- for byte in range(0, 8) :
+ for byte in range(0, 8):
state_output[15 - byte] ^= variables_sboxed[byte]
return state_output
-def LinearLayer(state) :
+def LinearLayer(state):
state_output = [0 for byte in range(0, BLOCK_BYTES)]
- for byte in range(0, BLOCK_BYTES) :
+ for byte in range(0, BLOCK_BYTES):
state_output[byte] = state[byte]
- for byte in range(1, 8) :
+ for byte in range(1, 8):
state_output[15] ^= state[byte]
- for byte in range(9, 15) :
+ for byte in range(9, 15):
state_output[byte] ^= state[7]
return state_output
-def PermutationLayerEnc(state) :
+def PermutationLayerEnc(state):
state_output = [0 for byte in range(0, BLOCK_BYTES)]
- for byte in range(0, BLOCK_BYTES) :
+ for byte in range(0, BLOCK_BYTES):
state_output[byte] = state[permutation[byte]]
return state_output
-def PermutationLayerDec(state) :
+def PermutationLayerDec(state):
state_output = [0 for byte in range(0, BLOCK_BYTES)]
- for byte in range(0, BLOCK_BYTES) :
+ for byte in range(0, BLOCK_BYTES):
state_output[byte] = state[permutationInv[byte]]
return state_output
-def OneRoundEGFNEnc(state, subtweakey) :
+def OneRoundEGFNEnc(state, subtweakey):
state_non_linear = NonLinearLayer(state, subtweakey)
state_linear = LinearLayer(state_non_linear)
state_permutation = PermutationLayerEnc(state_linear)
return state_permutation
-def LastRoundEGFN(state, subtweakey) :
+def LastRoundEGFN(state, subtweakey):
state_non_linear = NonLinearLayer(state, subtweakey)
state_linear = LinearLayer(state_non_linear)
return state_linear
-def OneRoundEGFNDec(state, subtweakey) :
+def OneRoundEGFNDec(state, subtweakey):
state_non_linear = NonLinearLayer(state, subtweakey)
state_linear = LinearLayer(state_non_linear)
state_permutation = PermutationLayerDec(state_linear)
@@ -136,20 +136,20 @@ def _rounds(key_bytes):
################################################################################
# Lilliput TBC
-def LilliputTBCEnc(tweak, key, message) :
+def LilliputTBCEnc(tweak, key, message):
r = _rounds(len(key))
tweakey = BuildTweakey(tweak, key)
RTKs = TweakeyScheduleWhole(tweakey, r)
state = [0 for byte in range(0, BLOCK_BYTES)]
- for byte in range(0, BLOCK_BYTES) :
+ for byte in range(0, BLOCK_BYTES):
state[byte] = message[byte]
- for i in range(0, r-1) :
+ for i in range(0, r-1):
state_output = OneRoundEGFNEnc(state, RTKs[i])
- for byte in range(0, BLOCK_BYTES) :
+ for byte in range(0, BLOCK_BYTES):
state[byte] = state_output[byte]
state_output = LastRoundEGFN(state, RTKs[r-1])
@@ -157,20 +157,20 @@ def LilliputTBCEnc(tweak, key, message) :
return state_output
-def LilliputTBCDec(tweak, key, cipher) :
+def LilliputTBCDec(tweak, key, cipher):
r = _rounds(len(key))
tweakey = BuildTweakey(tweak, key)
RTKs = TweakeyScheduleWhole(tweakey, r)
state = [0 for byte in range(0, BLOCK_BYTES)]
- for byte in range(0, BLOCK_BYTES) :
+ for byte in range(0, BLOCK_BYTES):
state[byte] = cipher[byte]
- for i in range(0, r-1) :
+ for i in range(0, r-1):
state_output = OneRoundEGFNDec(state, RTKs[r-i-1])
- for byte in range(0, BLOCK_BYTES) :
+ for byte in range(0, BLOCK_BYTES):
state[byte] = state_output[byte]
state_output = LastRoundEGFN(state, RTKs[0])