summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorKévin Le Gouguec <kevin.legouguec@airbus.com>2019-03-14 12:47:41 +0100
committerKévin Le Gouguec <kevin.legouguec@airbus.com>2019-03-21 14:49:15 +0100
commitf161a41e1bb1b379335bb658877a8859a64c9d10 (patch)
tree763e94039894a154edfcf58602082ca6d3afb4b9 /python
parent02eb0c9f257435595889d15577e4641b2242d0a1 (diff)
downloadlilliput-ae-implem-f161a41e1bb1b379335bb658877a8859a64c9d10.tar.xz
[implem-python] Suppression de paramètres redondants
tweak_bits est constant pour un mode donné ; rounds se déduit de la taille de clé.
Diffstat (limited to 'python')
-rw-r--r--python/constants.py9
-rw-r--r--python/lilliput.py29
-rw-r--r--python/lilliput_ae_1.py17
-rw-r--r--python/lilliput_ae_2.py15
4 files changed, 28 insertions, 42 deletions
diff --git a/python/constants.py b/python/constants.py
index acedfa7..94a3e0e 100644
--- a/python/constants.py
+++ b/python/constants.py
@@ -1,6 +1,15 @@
BLOCK_BYTES = 16
+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.py b/python/lilliput.py
index 21feb60..f6679a8 100644
--- a/python/lilliput.py
+++ b/python/lilliput.py
@@ -1,24 +1,9 @@
import lilliput_ae_1
import lilliput_ae_2
-N_BYTES = 15
-
-def _getParameters(mode=1, key_length=128) :
- rounds = {
- 128: 32,
- 192: 36,
- 256: 42
- }
-
- tweak_lengths = {
- 1: 192,
- 2: 128
- }
-
- return tweak_lengths[mode], rounds[key_length]
+N_BYTES = 15
-############################################
def _checkInputs(key, nonce):
valid_key_lengths = (128, 192, 256)
@@ -33,29 +18,25 @@ def _checkInputs(key, nonce):
def mainEnc(plaintext, adata, key, nonce, mode):
_checkInputs(key, nonce)
- tweak_bits, rounds = _getParameters(mode, len(key)*8)
-
A = adata
M = plaintext
N = nonce
if(mode == 1) :
- return lilliput_ae_1.OCB3Enc(A, M, N, key, tweak_bits, rounds)
+ return lilliput_ae_1.OCB3Enc(A, M, N, key)
if(mode == 2) :
- return lilliput_ae_2.SCT2Enc(A, M, N, key, tweak_bits, rounds)
+ return lilliput_ae_2.SCT2Enc(A, M, N, key)
def mainDec(ciphertext, tag, adata, key, nonce, mode):
_checkInputs(key, nonce)
- tweak_bits, rounds = _getParameters(mode, len(key)*8)
-
A = adata
C = ciphertext
N = nonce
tag = list(tag)
if(mode == 1) :
- return lilliput_ae_1.OCB3Dec(A, C, N, tag, key, tweak_bits, rounds)
+ return lilliput_ae_1.OCB3Dec(A, C, N, tag, key)
if(mode == 2) :
- return lilliput_ae_2.SCT2Dec(A, C, N, tag, key, tweak_bits, rounds)
+ return lilliput_ae_2.SCT2Dec(A, C, N, tag, key)
diff --git a/python/lilliput_ae_1.py b/python/lilliput_ae_1.py
index 3629fec..688148f 100644
--- a/python/lilliput_ae_1.py
+++ b/python/lilliput_ae_1.py
@@ -3,6 +3,7 @@
"""
import lilliput_tbc as ltbc
+from constants import rounds
from helpers import ArrayToBlockbytesMatrix, BlockbytesMatrixToBytes
@@ -24,23 +25,19 @@ M_BITS = BLOCK_BITS
N_BITS = 120
N_BYTES = int(N_BITS / 8)
-def InitParameters(key_bits = 128, tweak_bits = 192, rounds = 32) :
+def InitParameters(key_bits) :
global KEY_BITS
global KEY_BYTES
- global TWEAK_BITS
- global TWEAK_BYTES
global TWEAKEY_BITS
global TWEAKEY_BYTES
global LANES
global ROUNDS
KEY_BITS = key_bits
- TWEAK_BITS = tweak_bits
TWEAKEY_BITS = KEY_BITS + TWEAK_BITS
LANES = int((TWEAKEY_BITS) / LANE_BITS)
- ROUNDS = rounds
+ ROUNDS = rounds(key_bits)
KEY_BYTES = int(KEY_BITS / 8)
- TWEAK_BYTES = int(TWEAK_BITS / 8)
TWEAKEY_BYTES = int(TWEAKEY_BITS / 8)
@@ -221,8 +218,8 @@ def TreatMessageDec(C, N, key) :
################################################################################
-def OCB3Enc(A, M, N, key, tweak_bits, rounds) :
- InitParameters(len(key)*8, tweak_bits, rounds)
+def OCB3Enc(A, M, N, key) :
+ InitParameters(len(key)*8)
global A_BITS
global M_BITS
@@ -255,8 +252,8 @@ def OCB3Enc(A, M, N, key, tweak_bits, rounds) :
return BlockbytesMatrixToBytes(C), bytes(tag)
-def OCB3Dec(A, C, N, tag, key, tweak_bits, rounds) :
- InitParameters(len(key)*8, tweak_bits, rounds)
+def OCB3Dec(A, C, N, tag, key) :
+ InitParameters(len(key)*8)
global A_BITS
global M_BITS
diff --git a/python/lilliput_ae_2.py b/python/lilliput_ae_2.py
index 757088d..d333be2 100644
--- a/python/lilliput_ae_2.py
+++ b/python/lilliput_ae_2.py
@@ -3,6 +3,7 @@
"""
import lilliput_tbc as ltbc
+from constants import rounds
from helpers import ArrayToBlockbytesMatrix, BlockbytesMatrixToBytes
@@ -25,7 +26,7 @@ N_BITS = 120
N_BYTES = int(N_BITS / 8)
-def InitParameters(key_bits = 128, tweak_bits = 128, rounds = 32) :
+def InitParameters(key_bits) :
global KEY_BITS
global KEY_BYTES
global TWEAK_BITS
@@ -36,12 +37,10 @@ def InitParameters(key_bits = 128, tweak_bits = 128, rounds = 32) :
global ROUNDS
KEY_BITS = key_bits
- TWEAK_BITS = tweak_bits
TWEAKEY_BITS = KEY_BITS + TWEAK_BITS
LANES = int((TWEAKEY_BITS) / LANE_BITS)
- ROUNDS = rounds
+ ROUNDS = rounds(key_bits)
KEY_BYTES = int(KEY_BITS / 8)
- TWEAK_BYTES = int(TWEAK_BITS / 8)
TWEAKEY_BYTES = int(TWEAKEY_BITS / 8)
@@ -217,8 +216,8 @@ def MessageEncryption(M, N, tag, key) :
return C
################################################################################
-def SCT2Enc(A, M, N, key, tweak_bits, rounds) :
- InitParameters(len(key)*8, tweak_bits, rounds)
+def SCT2Enc(A, M, N, key) :
+ InitParameters(len(key)*8)
global A_BITS
global M_BITS
@@ -251,8 +250,8 @@ def SCT2Enc(A, M, N, key, tweak_bits, rounds) :
return BlockbytesMatrixToBytes(C), bytes(tag)
-def SCT2Dec(A, C, N, tag, key, tweak_bits, rounds) :
- InitParameters(len(key)*8, tweak_bits, rounds)
+def SCT2Dec(A, C, N, tag, key) :
+ InitParameters(len(key)*8)
global A_BITS
global M_BITS