summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorKévin Le Gouguec <kevin.legouguec@airbus.com>2019-03-14 12:38:36 +0100
committerKévin Le Gouguec <kevin.legouguec@airbus.com>2019-03-21 14:49:15 +0100
commit02eb0c9f257435595889d15577e4641b2242d0a1 (patch)
treed919592bd4632d23a8d13ec291a0812d4d0b93d7 /python
parentd8eeb99d9106b93c0a30e3ab8849d7687d2a6f29 (diff)
downloadlilliput-ae-implem-02eb0c9f257435595889d15577e4641b2242d0a1.tar.xz
[implem-python] Suppression de paramètres redondants
Création d'un nouveau module "helpers" qui contiendra les fonctions utilisées par les deux modes.
Diffstat (limited to 'python')
-rw-r--r--python/constants.py (renamed from python/constant.py)67
-rw-r--r--python/helpers.py25
-rw-r--r--python/lilliput.py52
-rw-r--r--python/lilliput_ae_1.py28
-rw-r--r--python/lilliput_ae_2.py29
-rw-r--r--python/lilliput_tbc.py4
6 files changed, 105 insertions, 100 deletions
diff --git a/python/constant.py b/python/constants.py
index 1bc208c..acedfa7 100644
--- a/python/constant.py
+++ b/python/constants.py
@@ -1,32 +1,35 @@
-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,
- 232, 243, 162, 194, 218, 81, 42, 16,
- 33, 1, 35, 120, 92, 36, 39, 181,
- 55, 199, 43, 31, 174, 10, 119, 95,
- 111, 9, 157, 129, 4, 90, 41, 220,
- 57, 156, 5, 87, 151, 116, 121, 23,
- 68, 198, 230, 233, 221, 65, 242, 138,
- 84, 202, 110, 74, 225, 173, 182, 136,
- 28, 152, 126, 206, 99, 73, 58, 93,
- 12, 239, 246, 52, 86, 37, 46, 214,
- 103, 117, 85, 118, 184, 210, 97, 217,
- 113, 139, 205, 11, 114, 108, 49, 75,
- 105, 253, 123, 109, 96, 60, 47, 98,
- 63, 34, 115, 19, 201, 130, 127, 83,
- 50, 18, 160, 124, 2, 135, 132, 134,
- 147, 78, 104, 70, 141, 195, 219, 236,
- 155, 183, 137, 146, 167, 190, 61, 216,
- 234, 80, 145, 241, 51, 56, 224, 169,
- 163, 131, 161, 27, 207, 6, 149, 7,
- 158, 237, 185, 245, 76, 192, 244, 45,
- 22, 250, 180, 3, 38, 179, 144, 79,
- 171, 101, 252, 254, 20, 247, 227, 148,
- 238, 172, 140, 26, 222, 203, 40, 64,
- 125, 200, 196, 72, 107, 223, 165, 82,
- 229, 251, 215, 100, 249, 240, 211, 94,
- 102, 150, 143, 29, 69, 54, 204, 197,
- 77, 159, 191, 15, 209, 8, 235, 67,
- 66, 25, 231, 153, 168, 142, 88, 193,
- 154, 212, 24, 71, 170, 175, 188, 91,
- 213, 17, 208, 176, 112, 187, 13, 186]
+BLOCK_BYTES = 16
+
+
+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,
+ 232, 243, 162, 194, 218, 81, 42, 16,
+ 33, 1, 35, 120, 92, 36, 39, 181,
+ 55, 199, 43, 31, 174, 10, 119, 95,
+ 111, 9, 157, 129, 4, 90, 41, 220,
+ 57, 156, 5, 87, 151, 116, 121, 23,
+ 68, 198, 230, 233, 221, 65, 242, 138,
+ 84, 202, 110, 74, 225, 173, 182, 136,
+ 28, 152, 126, 206, 99, 73, 58, 93,
+ 12, 239, 246, 52, 86, 37, 46, 214,
+ 103, 117, 85, 118, 184, 210, 97, 217,
+ 113, 139, 205, 11, 114, 108, 49, 75,
+ 105, 253, 123, 109, 96, 60, 47, 98,
+ 63, 34, 115, 19, 201, 130, 127, 83,
+ 50, 18, 160, 124, 2, 135, 132, 134,
+ 147, 78, 104, 70, 141, 195, 219, 236,
+ 155, 183, 137, 146, 167, 190, 61, 216,
+ 234, 80, 145, 241, 51, 56, 224, 169,
+ 163, 131, 161, 27, 207, 6, 149, 7,
+ 158, 237, 185, 245, 76, 192, 244, 45,
+ 22, 250, 180, 3, 38, 179, 144, 79,
+ 171, 101, 252, 254, 20, 247, 227, 148,
+ 238, 172, 140, 26, 222, 203, 40, 64,
+ 125, 200, 196, 72, 107, 223, 165, 82,
+ 229, 251, 215, 100, 249, 240, 211, 94,
+ 102, 150, 143, 29, 69, 54, 204, 197,
+ 77, 159, 191, 15, 209, 8, 235, 67,
+ 66, 25, 231, 153, 168, 142, 88, 193,
+ 154, 212, 24, 71, 170, 175, 188, 91,
+ 213, 17, 208, 176, 112, 187, 13, 186]
diff --git a/python/helpers.py b/python/helpers.py
new file mode 100644
index 0000000..34949a4
--- /dev/null
+++ b/python/helpers.py
@@ -0,0 +1,25 @@
+from constants import BLOCK_BYTES
+
+
+def ArrayToBlockbytesMatrix(array) :
+ length = len(array)
+ pad = 0
+ if(length % BLOCK_BYTES == 0) :
+ number_blocks = int(length / BLOCK_BYTES)
+ else :
+ number_blocks = int((length + (BLOCK_BYTES - (length % BLOCK_BYTES))) / BLOCK_BYTES)
+ pad = 1
+
+ matrix = [[0] * BLOCK_BYTES for block in range(0, number_blocks - pad)]
+ if(pad == 1) :
+ matrix.append([0] * (length % BLOCK_BYTES))
+
+ for byte in range(0, length) :
+ matrix[int(byte / BLOCK_BYTES)][byte % BLOCK_BYTES] = array[byte]
+
+ return matrix
+
+
+def BlockbytesMatrixToBytes(matrix):
+ return bytes(byte for block in matrix for byte in block)
+
diff --git a/python/lilliput.py b/python/lilliput.py
index 3fe41bf..21feb60 100644
--- a/python/lilliput.py
+++ b/python/lilliput.py
@@ -1,7 +1,6 @@
import lilliput_ae_1
import lilliput_ae_2
-BLOCK_BYTES = 16
N_BYTES = 15
def _getParameters(mode=1, key_length=128) :
@@ -19,27 +18,6 @@ def _getParameters(mode=1, key_length=128) :
return tweak_lengths[mode], rounds[key_length]
-def ArrayToBlockbytesMatrix(array) :
- length = len(array)
- pad = 0
- if(length % BLOCK_BYTES == 0) :
- number_blocks = int(length / BLOCK_BYTES)
- else :
- number_blocks = int((length + (BLOCK_BYTES - (length % BLOCK_BYTES))) / BLOCK_BYTES)
- pad = 1
-
- matrix = [[0] * BLOCK_BYTES for block in range(0, number_blocks - pad)]
- if(pad == 1) :
- matrix.append([0] * (length % BLOCK_BYTES))
-
- for byte in range(0, length) :
- matrix[int(byte / BLOCK_BYTES)][byte % BLOCK_BYTES] = array[byte]
-
- return matrix
-
-def BlockbytesMatrixToBytes(matrix):
- return bytes(byte for block in matrix for byte in block)
-
############################################
def _checkInputs(key, nonce):
@@ -55,47 +33,29 @@ def _checkInputs(key, nonce):
def mainEnc(plaintext, adata, key, nonce, mode):
_checkInputs(key, nonce)
- key_bits = len(key)*8
- tweak_bits, rounds = _getParameters(mode, key_bits)
+ tweak_bits, rounds = _getParameters(mode, len(key)*8)
A = adata
M = plaintext
N = nonce
- A_BITS = 8 * len(A)
- M_BITS = 8 * len(M)
-
- A = ArrayToBlockbytesMatrix(A)
- M = ArrayToBlockbytesMatrix(M)
-
if(mode == 1) :
- (C, tag) = lilliput_ae_1.OCB3Enc(A, M, N, A_BITS, M_BITS, key, key_bits, tweak_bits, rounds)
+ return lilliput_ae_1.OCB3Enc(A, M, N, key, tweak_bits, rounds)
if(mode == 2) :
- (C, tag) = lilliput_ae_2.SCT2Enc(A, M, N, A_BITS, M_BITS, key, key_bits, tweak_bits, rounds)
-
- return BlockbytesMatrixToBytes(C), bytes(tag)
+ return lilliput_ae_2.SCT2Enc(A, M, N, key, tweak_bits, rounds)
def mainDec(ciphertext, tag, adata, key, nonce, mode):
_checkInputs(key, nonce)
- key_bits = len(key)*8
- tweak_bits, rounds = _getParameters(mode, key_bits)
+ tweak_bits, rounds = _getParameters(mode, len(key)*8)
A = adata
C = ciphertext
N = nonce
tag = list(tag)
- M_BITS = 8 * len(C)
- A_BITS = 8 * len(A)
-
- A = ArrayToBlockbytesMatrix(A)
- C = ArrayToBlockbytesMatrix(C)
-
if(mode == 1) :
- M = lilliput_ae_1.OCB3Dec(A, C, N, tag, A_BITS, M_BITS, key, key_bits, tweak_bits, rounds)
+ return lilliput_ae_1.OCB3Dec(A, C, N, tag, key, tweak_bits, rounds)
if(mode == 2) :
- M = lilliput_ae_2.SCT2Dec(A, C, N, tag, A_BITS, M_BITS, key, key_bits, tweak_bits, rounds)
-
- return BlockbytesMatrixToBytes(M)
+ return lilliput_ae_2.SCT2Dec(A, C, N, tag, key, tweak_bits, rounds)
diff --git a/python/lilliput_ae_1.py b/python/lilliput_ae_1.py
index cd7fc82..3629fec 100644
--- a/python/lilliput_ae_1.py
+++ b/python/lilliput_ae_1.py
@@ -3,6 +3,8 @@
"""
import lilliput_tbc as ltbc
+from helpers import ArrayToBlockbytesMatrix, BlockbytesMatrixToBytes
+
BLOCK_BITS = 128
KEY_BITS = 128
@@ -219,14 +221,17 @@ def TreatMessageDec(C, N, key) :
################################################################################
-def OCB3Enc(A, M, N, associated_data_length_bit, message_length_bit, key, key_bits, tweak_bits, rounds) :
- InitParameters(key_bits, tweak_bits, rounds)
+def OCB3Enc(A, M, N, key, tweak_bits, rounds) :
+ InitParameters(len(key)*8, tweak_bits, rounds)
global A_BITS
global M_BITS
- A_BITS = associated_data_length_bit
- M_BITS = message_length_bit
+ A_BITS = len(A)*8
+ M_BITS = len(M)*8
+
+ A = ArrayToBlockbytesMatrix(A)
+ M = ArrayToBlockbytesMatrix(M)
ltbc.KEY_BITS = KEY_BITS
ltbc.ROUNDS = ROUNDS
@@ -247,17 +252,20 @@ def OCB3Enc(A, M, N, associated_data_length_bit, message_length_bit, key, key_bi
(Final, C) = TreatMessageEnc(M, N, key)
tag = XorState(Auth, Final)
- return (C, tag)
+ return BlockbytesMatrixToBytes(C), bytes(tag)
-def OCB3Dec(A, C, N, tag, associated_data_length_bit, message_length_bit, key, key_bits, tweak_bits, rounds) :
- InitParameters(key_bits, tweak_bits, rounds)
+def OCB3Dec(A, C, N, tag, key, tweak_bits, rounds) :
+ InitParameters(len(key)*8, tweak_bits, rounds)
global A_BITS
global M_BITS
- A_BITS = associated_data_length_bit
- M_BITS = message_length_bit
+ A_BITS = len(A)*8
+ M_BITS = len(C)*8
+
+ A = ArrayToBlockbytesMatrix(A)
+ C = ArrayToBlockbytesMatrix(C)
ltbc.KEY_BITS = KEY_BITS
ltbc.ROUNDS = ROUNDS
@@ -278,4 +286,4 @@ def OCB3Dec(A, C, N, tag, associated_data_length_bit, message_length_bit, key, k
tag2 = XorState(Auth, Final)
if(tag == tag2) :
- return M
+ return BlockbytesMatrixToBytes(M)
diff --git a/python/lilliput_ae_2.py b/python/lilliput_ae_2.py
index 40ee485..757088d 100644
--- a/python/lilliput_ae_2.py
+++ b/python/lilliput_ae_2.py
@@ -3,6 +3,8 @@
"""
import lilliput_tbc as ltbc
+from helpers import ArrayToBlockbytesMatrix, BlockbytesMatrixToBytes
+
BLOCK_BITS = 128
KEY_BITS = 128
@@ -215,14 +217,17 @@ def MessageEncryption(M, N, tag, key) :
return C
################################################################################
-def SCT2Enc(A, M, N, associated_data_length_bit, message_length_bit, key, key_bits, tweak_bits, rounds) :
- InitParameters(key_bits, tweak_bits, rounds)
+def SCT2Enc(A, M, N, key, tweak_bits, rounds) :
+ InitParameters(len(key)*8, tweak_bits, rounds)
global A_BITS
global M_BITS
- A_BITS = associated_data_length_bit
- M_BITS = message_length_bit
+ A_BITS = len(A)*8
+ M_BITS = len(M)*8
+
+ A = ArrayToBlockbytesMatrix(A)
+ M = ArrayToBlockbytesMatrix(M)
ltbc.KEY_BITS = KEY_BITS
ltbc.ROUNDS = ROUNDS
@@ -243,16 +248,20 @@ def SCT2Enc(A, M, N, associated_data_length_bit, message_length_bit, key, key_bi
tag = MesssageAuthTag(M, N, Auth, key)
C = MessageEncryption(M, N, tag, key)
- return (C, tag)
+ return BlockbytesMatrixToBytes(C), bytes(tag)
-def SCT2Dec(A, C, N, tag, associated_data_length_bit, message_length_bit, key, key_bits, tweak_bits, rounds) :
- InitParameters(key_bits, tweak_bits, rounds)
+
+def SCT2Dec(A, C, N, tag, key, tweak_bits, rounds) :
+ InitParameters(len(key)*8, tweak_bits, rounds)
global A_BITS
global M_BITS
- A_BITS = associated_data_length_bit
- M_BITS = message_length_bit
+ A_BITS = len(A)*8
+ M_BITS = len(C)*8
+
+ A = ArrayToBlockbytesMatrix(A)
+ C = ArrayToBlockbytesMatrix(C)
ltbc.KEY_BITS = KEY_BITS
ltbc.ROUNDS = ROUNDS
@@ -274,4 +283,4 @@ def SCT2Dec(A, C, N, tag, associated_data_length_bit, message_length_bit, key, k
tag2 = MesssageAuthTag(M, N, Auth, key)
if(tag == tag2) :
- return M
+ return BlockbytesMatrixToBytes(M)
diff --git a/python/lilliput_tbc.py b/python/lilliput_tbc.py
index 540d956..1ed4d8d 100644
--- a/python/lilliput_tbc.py
+++ b/python/lilliput_tbc.py
@@ -2,7 +2,7 @@
Lilliput TBC
"""
import random
-import constant
+import constants
import multiplications
BLOCK_BITS = 128
@@ -19,7 +19,7 @@ KEY_BYTES = int(KEY_BITS / 8)
TWEAK_BYTES = int(TWEAK_BITS / 8)
TWEAKEY_BYTES = int(TWEAKEY_BITS / 8)
-Sbox = constant.Sbox
+Sbox = constants.Sbox
MultiplyM = multiplications.MultiplyM
MultiplyM2 = multiplications.MultiplyM2
MultiplyM3 = multiplications.MultiplyM3