diff options
| author | Kévin Le Gouguec <kevin.legouguec@airbus.com> | 2019-03-14 12:38:36 +0100 |
|---|---|---|
| committer | Kévin Le Gouguec <kevin.legouguec@airbus.com> | 2019-03-21 14:49:15 +0100 |
| commit | 02eb0c9f257435595889d15577e4641b2242d0a1 (patch) | |
| tree | d919592bd4632d23a8d13ec291a0812d4d0b93d7 /python/lilliput.py | |
| parent | d8eeb99d9106b93c0a30e3ab8849d7687d2a6f29 (diff) | |
| download | lilliput-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/lilliput.py')
| -rw-r--r-- | python/lilliput.py | 52 |
1 files changed, 6 insertions, 46 deletions
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) |
