summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKévin Le Gouguec <kevin.legouguec@airbus.com>2019-03-14 13:47:25 +0100
committerKévin Le Gouguec <kevin.legouguec@airbus.com>2019-03-21 14:49:15 +0100
commit9f999131bc6798e320432e71f3bfbcfbf20dc295 (patch)
tree293d744cd33e952a7bacc2781d08f09592a1fa38
parenta1d3889f48a3fffd38a855fc433a9a3d3f649434 (diff)
downloadlilliput-ae-implem-9f999131bc6798e320432e71f3bfbcfbf20dc295.tar.xz
[implem-python] Confinement de la gestion des tours au module TBC
Retrait de quelques variables globales par la même occasion. Renommage de "round" en "i" pour 1) coller à la spec 2) éviter le conflit avec le builtin "round".
-rw-r--r--python/constants.py9
-rw-r--r--python/lilliput_ae_1.py16
-rw-r--r--python/lilliput_ae_2.py16
-rw-r--r--python/lilliput_tbc.py73
4 files changed, 34 insertions, 80 deletions
diff --git a/python/constants.py b/python/constants.py
index 02bbc1f..e8d8d03 100644
--- a/python/constants.py
+++ b/python/constants.py
@@ -2,15 +2,6 @@ BLOCK_BYTES = 16
NONCE_BYTES = 15
-def rounds(key_bits):
- r = {
- 128: 32,
- 192: 36,
- 256: 42
- }
- return r[key_bits]
-
-
Sbox = [32, 0, 178, 133, 59, 53, 166, 164,
48, 228, 106, 44, 255, 89, 226, 14,
248, 30, 122, 128, 21, 189, 62, 177,
diff --git a/python/lilliput_ae_1.py b/python/lilliput_ae_1.py
index 92cfa6e..9eb5460 100644
--- a/python/lilliput_ae_1.py
+++ b/python/lilliput_ae_1.py
@@ -3,7 +3,7 @@
"""
import lilliput_tbc as ltbc
-from constants import NONCE_BYTES, rounds
+from constants import NONCE_BYTES
from helpers import ArrayToBlockbytesMatrix, BlockbytesMatrixToBytes
@@ -13,7 +13,6 @@ TWEAK_BITS = 192
TWEAKEY_BITS = KEY_BITS + TWEAK_BITS
LANE_BITS = 64
LANES = int((TWEAKEY_BITS) / LANE_BITS)
-ROUNDS = 32
BLOCK_BYTES = int(BLOCK_BITS / 8)
KEY_BYTES = int(KEY_BITS / 8)
@@ -30,12 +29,10 @@ def InitParameters(key_bits) :
global TWEAKEY_BITS
global TWEAKEY_BYTES
global LANES
- global ROUNDS
KEY_BITS = key_bits
TWEAKEY_BITS = KEY_BITS + TWEAK_BITS
LANES = int((TWEAKEY_BITS) / LANE_BITS)
- ROUNDS = rounds(key_bits)
KEY_BYTES = int(KEY_BITS / 8)
TWEAKEY_BYTES = int(TWEAKEY_BITS / 8)
@@ -230,7 +227,6 @@ def OCB3Enc(A, M, N, key) :
M = ArrayToBlockbytesMatrix(M)
ltbc.KEY_BITS = KEY_BITS
- ltbc.ROUNDS = ROUNDS
ltbc.TWEAK_BITS = TWEAK_BITS
ltbc.LANES = LANES
@@ -239,11 +235,6 @@ def OCB3Enc(A, M, N, key) :
ltbc.TWEAK_BYTES = TWEAK_BYTES
ltbc.TWEAKEY_BYTES = TWEAKEY_BYTES
- ltbc.TKs = [[0 for byte in range(0, TWEAKEY_BYTES)] for round in range(0, ROUNDS)]
- ltbc.RTKs = [[0 for byte in range(0, 8)] for round in range(0, ROUNDS)]
- ltbc.States = [[0 for byte in range(0, BLOCK_BYTES)] for round in range(0, ROUNDS)]
-
-
Auth = BuildAuth(A, key)
(Final, C) = TreatMessageEnc(M, N, key)
tag = XorState(Auth, Final)
@@ -264,7 +255,6 @@ def OCB3Dec(A, C, N, tag, key) :
C = ArrayToBlockbytesMatrix(C)
ltbc.KEY_BITS = KEY_BITS
- ltbc.ROUNDS = ROUNDS
ltbc.TWEAK_BITS = TWEAK_BITS
ltbc.LANES = LANES
@@ -273,10 +263,6 @@ def OCB3Dec(A, C, N, tag, key) :
ltbc.TWEAK_BYTES = TWEAK_BYTES
ltbc.TWEAKEY_BYTES = TWEAKEY_BYTES
- ltbc.TKs = [[0 for byte in range(0, TWEAKEY_BYTES)] for round in range(0, ROUNDS)]
- ltbc.RTKs = [[0 for byte in range(0, 8)] for round in range(0, ROUNDS)]
- ltbc.States = [[0 for byte in range(0, BLOCK_BYTES)] for round in range(0, ROUNDS)]
-
Auth = BuildAuth(A, key)
(Final, M) = TreatMessageDec(C, N, key)
tag2 = XorState(Auth, Final)
diff --git a/python/lilliput_ae_2.py b/python/lilliput_ae_2.py
index d072935..ed57cdf 100644
--- a/python/lilliput_ae_2.py
+++ b/python/lilliput_ae_2.py
@@ -3,7 +3,6 @@
"""
import lilliput_tbc as ltbc
-from constants import rounds
from helpers import ArrayToBlockbytesMatrix, BlockbytesMatrixToBytes
@@ -13,7 +12,6 @@ TWEAK_BITS = 128
TWEAKEY_BITS = KEY_BITS + TWEAK_BITS
LANE_BITS = 64
LANES = int((TWEAKEY_BITS) / LANE_BITS)
-ROUNDS = 32
BLOCK_BYTES = int(BLOCK_BITS / 8)
KEY_BYTES = int(KEY_BITS / 8)
@@ -32,12 +30,10 @@ def InitParameters(key_bits) :
global TWEAKEY_BITS
global TWEAKEY_BYTES
global LANES
- global ROUNDS
KEY_BITS = key_bits
TWEAKEY_BITS = KEY_BITS + TWEAK_BITS
LANES = int((TWEAKEY_BITS) / LANE_BITS)
- ROUNDS = rounds(key_bits)
KEY_BYTES = int(KEY_BITS / 8)
TWEAKEY_BYTES = int(TWEAKEY_BITS / 8)
@@ -227,7 +223,6 @@ def SCT2Enc(A, M, N, key) :
M = ArrayToBlockbytesMatrix(M)
ltbc.KEY_BITS = KEY_BITS
- ltbc.ROUNDS = ROUNDS
ltbc.TWEAK_BITS = TWEAK_BITS
ltbc.LANES = LANES
@@ -236,11 +231,6 @@ def SCT2Enc(A, M, N, key) :
ltbc.TWEAK_BYTES = TWEAK_BYTES
ltbc.TWEAKEY_BYTES = TWEAKEY_BYTES
- ltbc.TKs = [[0 for byte in range(0, TWEAKEY_BYTES)] for round in range(0, ROUNDS)]
- ltbc.RTKs = [[0 for byte in range(0, 8)] for round in range(0, ROUNDS)]
- ltbc.States = [[0 for byte in range(0, BLOCK_BYTES)] for round in range(0, ROUNDS)]
-
-
Auth = BuildAuth(A, key)
tag = MesssageAuthTag(M, N, Auth, key)
C = MessageEncryption(M, N, tag, key)
@@ -261,7 +251,6 @@ def SCT2Dec(A, C, N, tag, key) :
C = ArrayToBlockbytesMatrix(C)
ltbc.KEY_BITS = KEY_BITS
- ltbc.ROUNDS = ROUNDS
ltbc.TWEAK_BITS = TWEAK_BITS
ltbc.LANES = LANES
@@ -270,11 +259,6 @@ def SCT2Dec(A, C, N, tag, key) :
ltbc.TWEAK_BYTES = TWEAK_BYTES
ltbc.TWEAKEY_BYTES = TWEAKEY_BYTES
- ltbc.TKs = [[0 for byte in range(0, TWEAKEY_BYTES)] for round in range(0, ROUNDS)]
- ltbc.RTKs = [[0 for byte in range(0, 8)] for round in range(0, ROUNDS)]
- ltbc.States = [[0 for byte in range(0, BLOCK_BYTES)] for round in range(0, ROUNDS)]
-
-
M = MessageEncryption(C, N, tag, key)
Auth = BuildAuth(A, key)
tag2 = MesssageAuthTag(M, N, Auth, key)
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