summaryrefslogtreecommitdiff
path: root/src/add_python/lilliput/tbc.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/add_python/lilliput/tbc.py')
-rw-r--r--src/add_python/lilliput/tbc.py91
1 files changed, 43 insertions, 48 deletions
diff --git a/src/add_python/lilliput/tbc.py b/src/add_python/lilliput/tbc.py
index 50f9e2f..c607e45 100644
--- a/src/add_python/lilliput/tbc.py
+++ b/src/add_python/lilliput/tbc.py
@@ -17,35 +17,33 @@
This module provides functions to encrypt and decrypt blocks of 128 bits.
"""
-from .constants import BLOCK_BYTES, Sbox
+from .constants import BLOCK_BYTES, SBOX
from .multiplications import ALPHAS
-_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]
+_PERMUTATION = [14, 11, 12, 10, 8, 9, 13, 15, 3, 1, 4, 5, 6, 0, 2, 7]
+_PERMUTATION_INV = [13, 9, 14, 8, 10, 11, 12, 15, 4, 5, 3, 1, 2, 6 ,0 ,7]
-################################################################################
-def _BuildTweakey(tweak, key):
+def _build_tweakey(tweak, key):
return tweak+key
-#############################
-def _Lane(TK, j):
+def _lane(TK, j):
return TK[j*8:(j+1)*8]
-def _RoundTweakeySchedule(tweakey):
+def _round_tweakey_schedule(tweakey):
p = len(tweakey)//8
multiplied_lanes = (
- ALPHAS[j](_Lane(tweakey, j)) for j in range(p)
+ ALPHAS[j](_lane(tweakey, j)) for j in range(p)
)
return [byte for lane in multiplied_lanes for byte in lane]
-def _SubTweakeyExtract(tweakey, Ci):
+def _subtweakey_extract(tweakey, Ci):
RTKi = [0]*8
for j, byte in enumerate(tweakey):
@@ -56,22 +54,20 @@ def _SubTweakeyExtract(tweakey, Ci):
return RTKi
-def _TweakeyScheduleWhole(tweakey, r):
+def _tweakey_schedule_whole(tweakey, r):
# Store the initial tweakey in TKs[0], and the corresponding round tweakey
# in RTKs[0].
TKs = [tweakey]
- RTKs = [_SubTweakeyExtract(TKs[0], 0)]
+ RTKs = [_subtweakey_extract(TKs[0], 0)]
for i in range(1, r):
- TKs.append(_RoundTweakeySchedule(TKs[i-1]))
- RTKs.append(_SubTweakeyExtract(TKs[i], i))
+ TKs.append(_round_tweakey_schedule(TKs[i-1]))
+ RTKs.append(_subtweakey_extract(TKs[i], i))
return RTKs
-################################################################################
-
-def _NonLinearLayer(state, subtweakey):
+def _non_linear_layer(state, subtweakey):
variables_xored = [0 for byte in range(0, 8)]
for byte in range(0,8):
@@ -79,7 +75,7 @@ def _NonLinearLayer(state, subtweakey):
variables_sboxed = [0 for byte in range(0, 8)]
for byte in range(0, 8):
- variables_sboxed[byte] = Sbox[variables_xored[byte]]
+ variables_sboxed[byte] = SBOX[variables_xored[byte]]
state_output = [0 for byte in range(0, BLOCK_BYTES)]
for byte in range(0,BLOCK_BYTES):
@@ -90,7 +86,7 @@ def _NonLinearLayer(state, subtweakey):
return state_output
-def _LinearLayer(state):
+def _linear_layer(state):
state_output = [0 for byte in range(0, BLOCK_BYTES)]
for byte in range(0, BLOCK_BYTES):
state_output[byte] = state[byte]
@@ -104,44 +100,46 @@ def _LinearLayer(state):
return state_output
-def _PermutationLayerEnc(state):
+def _permutation_layer_enc(state):
state_output = [0 for byte in range(0, BLOCK_BYTES)]
for byte in range(0, BLOCK_BYTES):
- state_output[byte] = state[_permutation[byte]]
+ state_output[byte] = state[_PERMUTATION[byte]]
return state_output
-def _PermutationLayerDec(state):
+
+def _permutation_layer_dec(state):
state_output = [0 for byte in range(0, BLOCK_BYTES)]
for byte in range(0, BLOCK_BYTES):
- state_output[byte] = state[_permutationInv[byte]]
+ state_output[byte] = state[_PERMUTATION_INV[byte]]
return state_output
-def _OneRoundEGFNEnc(state, subtweakey):
- state_non_linear = _NonLinearLayer(state, subtweakey)
- state_linear = _LinearLayer(state_non_linear)
- state_permutation = _PermutationLayerEnc(state_linear)
+def _one_round_egfn_enc(state, subtweakey):
+ state_non_linear = _non_linear_layer(state, subtweakey)
+ state_linear = _linear_layer(state_non_linear)
+ state_permutation = _permutation_layer_enc(state_linear)
return state_permutation
-def _LastRoundEGFN(state, subtweakey):
- state_non_linear = _NonLinearLayer(state, subtweakey)
- state_linear = _LinearLayer(state_non_linear)
+
+def _last_round_egfn(state, subtweakey):
+ state_non_linear = _non_linear_layer(state, subtweakey)
+ state_linear = _linear_layer(state_non_linear)
return state_linear
-def _OneRoundEGFNDec(state, subtweakey):
- state_non_linear = _NonLinearLayer(state, subtweakey)
- state_linear = _LinearLayer(state_non_linear)
- state_permutation = _PermutationLayerDec(state_linear)
+def _one_round_egfn_dec(state, subtweakey):
+ state_non_linear = _non_linear_layer(state, subtweakey)
+ state_linear = _linear_layer(state_non_linear)
+ state_permutation = _permutation_layer_dec(state_linear)
return state_permutation
-def _Rounds(key_bytes):
+def _rounds(key_bytes):
rounds = {
128: 32,
192: 36,
@@ -150,46 +148,43 @@ def _Rounds(key_bytes):
return rounds[key_bytes*8]
-################################################################################
-
-
def encrypt(tweak, key, message):
- r = _Rounds(len(key))
+ r = _rounds(len(key))
- tweakey = _BuildTweakey(tweak, key)
- RTKs = _TweakeyScheduleWhole(tweakey, r)
+ tweakey = _build_tweakey(tweak, key)
+ RTKs = _tweakey_schedule_whole(tweakey, r)
state = [0 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):
- state_output = _OneRoundEGFNEnc(state, RTKs[i])
+ state_output = _one_round_egfn_enc(state, RTKs[i])
for byte in range(0, BLOCK_BYTES):
state[byte] = state_output[byte]
- state_output = _LastRoundEGFN(state, RTKs[r-1])
+ state_output = _last_round_egfn(state, RTKs[r-1])
return state_output
def decrypt(tweak, key, cipher):
- r = _Rounds(len(key))
+ r = _rounds(len(key))
- tweakey = _BuildTweakey(tweak, key)
- RTKs = _TweakeyScheduleWhole(tweakey, r)
+ tweakey = _build_tweakey(tweak, key)
+ RTKs = _tweakey_schedule_whole(tweakey, r)
state = [0 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):
- state_output = _OneRoundEGFNDec(state, RTKs[r-i-1])
+ state_output = _one_round_egfn_dec(state, RTKs[r-i-1])
for byte in range(0, BLOCK_BYTES):
state[byte] = state_output[byte]
- state_output = _LastRoundEGFN(state, RTKs[0])
+ state_output = _last_round_egfn(state, RTKs[0])
return state_output