From d7830279e9545b420e4cc0f4b810675df728e98f Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 13 Mar 2019 15:23:27 +0100 Subject: Ajout de l'implémentation Python MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/lilliput.py | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 python/lilliput.py (limited to 'python/lilliput.py') diff --git a/python/lilliput.py b/python/lilliput.py new file mode 100644 index 0000000..e090a10 --- /dev/null +++ b/python/lilliput.py @@ -0,0 +1,108 @@ +import lilliput_ae_1 +import lilliput_ae_2 + +import parameters_i_128 as i_128 +import parameters_i_192 as i_192 +import parameters_i_256 as i_256 +import parameters_ii_128 as ii_128 +import parameters_ii_192 as ii_192 +import parameters_ii_256 as ii_256 + +BLOCK_BYTES = 16 +N_BYTES = 15 + +def GetParameters(mode = 1, length = 128) : + if(mode == 1 and length == 128) : + return (i_128.KEY_BITS, i_128.TWEAK_BITS, i_128.ROUNDS) + + if(mode == 1 and length == 192) : + return (i_192.KEY_BITS, i_192.TWEAK_BITS, i_192.ROUNDS) + + if(mode == 1 and length == 256) : + return (i_256.KEY_BITS, i_256.TWEAK_BITS, i_256.ROUNDS) + + if(mode == 2 and length == 128) : + return (ii_128.KEY_BITS, ii_128.TWEAK_BITS, ii_128.ROUNDS) + + if(mode == 2 and length == 192) : + return (ii_192.KEY_BITS, ii_192.TWEAK_BITS, ii_192.ROUNDS) + + if(mode == 2 and length == 256) : + return (ii_256.KEY_BITS, ii_256.TWEAK_BITS, ii_256.ROUNDS) + +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 mainEnc(mode = 1, length = 128) : + + (key_bits, tweak_bits, rounds) = GetParameters(mode, length) + + + A = [byte for byte in range(0, 16)] + M = [byte for byte in range(0, 17)] + N = [0 for byte in range(0, N_BYTES)] + key = [byte for byte in range(0, int(key_bits/8))] + + 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) + if(mode == 2) : + (C, tag) = lilliput_ae_2.SCT2Enc(A, M, N, A_BITS, M_BITS, key, key_bits, tweak_bits, rounds) + for block in range(0,len(C)) : + for byte in C[block] : + print("%02x "%(byte), end="") + for byte in tag : + print("%02x "%(byte), end="") + print() + + +def mainDec(mode = 1, length = 128) : + + (key_bits, tweak_bits, rounds) = GetParameters(mode, length) + + + A = [byte for byte in range(0, 16)] + C = [byte for byte in range(0, 16)] + N = [0 for byte in range(0, N_BYTES)] + key = [byte for byte in range(0, int(key_bits/8))] + 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) + if(mode == 2) : + M = lilliput_ae_2.SCT2Dec(A, C, N, tag, A_BITS, M_BITS, key, key_bits, tweak_bits, rounds) + for block in range(0,len(M)) : + for byte in M[block] : + print("%02x "%(byte), end="") + print() -- cgit v1.2.3