From 1870b1eadf3963f9ec5d52bed21f04e523933612 Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Wed, 13 Mar 2019 16:47:57 +0100 Subject: Changement de l'API de l'implémentation Python MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pour qu'on puisse plus facilement manipuler les entrées/sorties. Pour le moment le round-trip chiffrement/déchiffrement marche. import lilliput message = 'Hello 🌐!' adata = 'Signed: Kévin' for mode in 1,2: for keylen in 128, 192, 256: ct, tag = lilliput.mainEnc(message, adata, mode, keylen) pt = lilliput.mainDec(ct, tag, adata, mode, keylen) assert message == pt --- python/lilliput.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'python/lilliput.py') diff --git a/python/lilliput.py b/python/lilliput.py index e090a10..6a2aae4 100644 --- a/python/lilliput.py +++ b/python/lilliput.py @@ -48,15 +48,17 @@ def ArrayToBlockbytesMatrix(array) : return matrix +def BlockbytesMatrixToBytes(matrix): + return bytes(byte for block in matrix for byte in block) + ############################################ -def mainEnc(mode = 1, length = 128) : +def mainEnc(plaintext, adata, 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)] + A = adata.encode() + M = plaintext.encode() N = [0 for byte in range(0, N_BYTES)] key = [byte for byte in range(0, int(key_bits/8))] @@ -66,7 +68,6 @@ def mainEnc(mode = 1, length = 128) : 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) : @@ -78,18 +79,18 @@ def mainEnc(mode = 1, length = 128) : print("%02x "%(byte), end="") print() + return BlockbytesMatrixToBytes(C), bytes(tag) -def mainDec(mode = 1, length = 128) : - (key_bits, tweak_bits, rounds) = GetParameters(mode, length) +def mainDec(ciphertext, tag, adata, 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)] + A = adata.encode() + C = ciphertext N = [0 for byte in range(0, N_BYTES)] key = [byte for byte in range(0, int(key_bits/8))] - tag = [] - + tag = list(tag) M_BITS = 8 * len(C) A_BITS = 8 * len(A) @@ -97,7 +98,6 @@ def mainDec(mode = 1, length = 128) : 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) : @@ -106,3 +106,5 @@ def mainDec(mode = 1, length = 128) : for byte in M[block] : print("%02x "%(byte), end="") print() + + return BlockbytesMatrixToBytes(M).decode() -- cgit v1.2.3