diff options
Diffstat (limited to 'python/lilliput_tbc.py')
| -rw-r--r-- | python/lilliput_tbc.py | 73 |
1 files changed, 33 insertions, 40 deletions
diff --git a/python/lilliput_tbc.py b/python/lilliput_tbc.py index 78389f9..12df2dc 100644 --- a/python/lilliput_tbc.py +++ b/python/lilliput_tbc.py @@ -10,7 +10,6 @@ TWEAK_BITS = 192 LANE_BITS = 64 LANE_BYTES = LANE_BITS / 8 LANES = int((TWEAK_BITS+KEY_BITS) / LANE_BITS) -ROUNDS = 32 BLOCK_BYTES = int(BLOCK_BITS / 8) TWEAKEY_BITS = KEY_BITS + TWEAK_BITS @@ -29,12 +28,6 @@ MultiplyMR3 = multiplications.MultiplyMR3 permutation = [14, 11, 12, 10, 8, 9, 13, 15, 3, 1, 4, 5, 6, 0, 2, 7] permutationInv = [13, 9, 14, 8, 10, 11, 12, 15, 4, 5, 3, 1, 2, 6 ,0 ,7] -# Personnal global variables to check better -TKs = [[0 for byte in range(0, TWEAKEY_BYTES)] for round in range(0, ROUNDS)] -RTKs = [[0 for byte in range(0, 8)] for round in range(0, ROUNDS)] -States = [[0 for byte in range(0, BLOCK_BYTES)] for round in range(0, ROUNDS)] - - ################################################################################ def BuildTweakey(tweak, key) : @@ -74,26 +67,29 @@ def RoundTweakeySchedule(tweakey) : return tweakey_multiplied ; -def SubTweakeyExtract(tweakey, round) : +def SubTweakeyExtract(tweakey, Ci): subtweakey = [0 for byte in range(0, 8)] for byte in range(0, TWEAKEY_BYTES) : subtweakey[byte % 8] ^= tweakey[byte] - subtweakey[0] ^= round + subtweakey[0] ^= Ci return subtweakey -def TweakeyScheduleWhole(tweakey) : +def TweakeyScheduleWhole(tweakey, r): # store main tweakey in TKs[0] # and corresponding RTKs[0] - TKs[0] = tweakey - RTKs[0] = SubTweakeyExtract(TKs[0], 0) + TKs = [tweakey] + RTKs = [SubTweakeyExtract(TKs[0], 0)] + + for i in range(1, r) : + TKs.append(RoundTweakeySchedule(TKs[i-1])) + RTKs.append(SubTweakeyExtract(TKs[i], i)) + + return RTKs - for round in range(1, ROUNDS) : - TKs[round] = RoundTweakeySchedule(TKs[round - 1]) - RTKs[round] = SubTweakeyExtract(TKs[round], round) ################################################################################ @@ -145,21 +141,21 @@ def PermutationLayerDec(state) : return state_output -def OneRoundEGFNEnc(state, subtweakey, round) : +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, round) : +def LastRoundEGFN(state, subtweakey) : state_non_linear = NonLinearLayer(state, subtweakey) state_linear = LinearLayer(state_non_linear) return state_linear -def OneRoundEGFNDec(state, subtweakey, round) : +def OneRoundEGFNDec(state, subtweakey) : state_non_linear = NonLinearLayer(state, subtweakey) state_linear = LinearLayer(state_non_linear) state_permutation = PermutationLayerDec(state_linear) @@ -167,58 +163,55 @@ def OneRoundEGFNDec(state, subtweakey, round) : return state_permutation +def _rounds(key_bytes): + rounds = { + 128: 32, + 192: 36, + 256: 42 + } + return rounds[key_bytes*8] + + ################################################################################ # Lilliput TBC def LilliputTBCEnc(tweak, key, message) : - - global TKs - global RTKs - global states - TKs = [[0 for byte in range(0, TWEAKEY_BYTES)] for round in range(0, ROUNDS)] - RTKs = [[0 for byte in range(0, 8)] for round in range(0, ROUNDS)] - States = [[0 for byte in range(0, BLOCK_BYTES)] for round in range(0, ROUNDS)] + r = _rounds(len(key)) tweakey = BuildTweakey(tweak, key) - TweakeyScheduleWhole(tweakey) + RTKs = TweakeyScheduleWhole(tweakey, r) state = [0 for byte in range(0, BLOCK_BYTES)] for byte in range(0, BLOCK_BYTES) : state[byte] = message[byte] - for round in range(0, ROUNDS - 1) : - state_output = OneRoundEGFNEnc(state, RTKs[round], round = round) + for i in range(0, r-1) : + state_output = OneRoundEGFNEnc(state, RTKs[i]) for byte in range(0, BLOCK_BYTES) : state[byte] = state_output[byte] - state_output = LastRoundEGFN(state, RTKs[ROUNDS - 1], round = ROUNDS - 1) + state_output = LastRoundEGFN(state, RTKs[r-1]) return state_output def LilliputTBCDec(tweak, key, cipher) : - - global TKs - global RTKs - global states - TKs = [[0 for byte in range(0, TWEAKEY_BYTES)] for round in range(0, ROUNDS)] - RTKs = [[0 for byte in range(0, 8)] for round in range(0, ROUNDS)] - States = [[0 for byte in range(0, BLOCK_BYTES)] for round in range(0, ROUNDS)] + r = _rounds(len(key)) tweakey = BuildTweakey(tweak, key) - TweakeyScheduleWhole(tweakey) + RTKs = TweakeyScheduleWhole(tweakey, r) state = [0 for byte in range(0, BLOCK_BYTES)] for byte in range(0, BLOCK_BYTES) : state[byte] = cipher[byte] - for round in range(0, ROUNDS - 1) : - state_output = OneRoundEGFNDec(state, RTKs[ROUNDS - round - 1], round = round) + for i in range(0, r-1) : + state_output = OneRoundEGFNDec(state, RTKs[r-i-1]) for byte in range(0, BLOCK_BYTES) : state[byte] = state_output[byte] - state_output = LastRoundEGFN(state, RTKs[0], round = ROUNDS - 1) + state_output = LastRoundEGFN(state, RTKs[0]) return state_output |
