From 417ac09668ea124245dbac0084608d9c396f5a76 Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Thu, 14 Mar 2019 16:15:08 +0100 Subject: [implem-python] Retrait de A_BITS/M_BITS (1.5/2) --- python/lilliput_ae_2.py | 83 ++++++------------------------------------------- 1 file changed, 10 insertions(+), 73 deletions(-) (limited to 'python/lilliput_ae_2.py') diff --git a/python/lilliput_ae_2.py b/python/lilliput_ae_2.py index 161598e..164c3df 100644 --- a/python/lilliput_ae_2.py +++ b/python/lilliput_ae_2.py @@ -3,7 +3,14 @@ """ import lilliput_tbc as ltbc -from helpers import ArrayToBlockbytesMatrix, BlockbytesMatrixToBytes +from constants import BLOCK_BITS, BLOCK_BYTES +from helpers import ( + ArrayToBlockbytesMatrix, + BlockbytesMatrixToBytes, + BuildAuth, + Padding10LSB, + XorState +) BLOCK_BITS = 128 @@ -18,7 +25,6 @@ KEY_BYTES = int(KEY_BITS / 8) TWEAK_BYTES = int(TWEAK_BITS / 8) TWEAKEY_BYTES = int(TWEAKEY_BITS / 8) -A_BITS = BLOCK_BITS M_BITS = BLOCK_BITS @@ -38,25 +44,6 @@ def InitParameters(key_bits) : ############################################################################### -def XorState(state1, state2) : - state_output = [state1[byte] ^ state2[byte] for byte in range(0, len(state1))] - return state_output - -def Padding10LSB(array, number_bits) : - shifted = 0 - for byte in range(0, len(array)) : - shifted |= (array[byte] << (8 * byte)) - shifted = (shifted << (BLOCK_BITS - number_bits)) & 0xffffffffffffffffffffffffffffffff - - padded = shifted | (0x1 << (BLOCK_BITS - number_bits - 1)) - - array_padded = [0 for byte in range(0, BLOCK_BYTES)] - for byte in range(0, BLOCK_BYTES) : - array_padded[byte] = (padded & (0xff << (8 * byte))) >> (8 * byte) - - return array_padded - - def LowPart(array, number_bits) : shifted = 0 for byte in range(0, len(array)) : @@ -79,48 +66,6 @@ def LowPart(array, number_bits) : return lower_part_byte -############################################################################### - -def TweakAssociatedData(i, padded = 0) : - tweak = [0 for byte in range(0, TWEAK_BYTES)] - - mask = 0xff - for byte in range(0, TWEAK_BYTES - 1) : - tweak[byte] = (i & mask) >> (byte * 8) - mask = mask << 8 - - mask = (0xf << (8 * (TWEAK_BYTES - 1))) - tweak[TWEAK_BYTES - 1] = (i & mask) >> ((TWEAK_BYTES - 1) * 8) - if padded == 0 : - tweak[TWEAK_BYTES - 1] |= 0x20 - else : - tweak[TWEAK_BYTES - 1] |= 0x60 - return tweak - - -def BuildAuth(A, key) : - Auth = [0 for byte in range(0, BLOCK_BYTES)] - l_a = int(A_BITS / BLOCK_BITS) - if int(A_BITS % BLOCK_BITS) > 0 : - will_padd = 1 - else : - will_padd = 0 - - for i in range(0, l_a) : - tweak = TweakAssociatedData(i, padded = 0) - enc = ltbc.LilliputTBCEnc(tweak, key, A[i]) - Auth = XorState(Auth, enc) - - if (A_BITS % BLOCK_BITS) == 0 : - return Auth - - tweak = TweakAssociatedData(l_a, padded = 1) - ad_padded = Padding10LSB(A[l_a], (A_BITS % BLOCK_BITS)) - enc = ltbc.LilliputTBCEnc(tweak, key, ad_padded) - Auth = XorState(Auth, enc) - - return Auth - ################################################################################ def TweakTag(j, padded = 0) : @@ -211,17 +156,13 @@ def MessageEncryption(M, N, tag, key) : def SCT2Enc(A, M, N, key) : InitParameters(len(key)*8) - global A_BITS global M_BITS - - A_BITS = len(A)*8 M_BITS = len(M)*8 - A = ArrayToBlockbytesMatrix(A) M = ArrayToBlockbytesMatrix(M) K = list(key) - Auth = BuildAuth(A, K) + Auth = BuildAuth(TWEAK_BITS, A, K) tag = MesssageAuthTag(M, N, Auth, K) C = MessageEncryption(M, N, tag, K) @@ -231,18 +172,14 @@ def SCT2Enc(A, M, N, key) : def SCT2Dec(A, C, N, tag, key) : InitParameters(len(key)*8) - global A_BITS global M_BITS - - A_BITS = len(A)*8 M_BITS = len(C)*8 - A = ArrayToBlockbytesMatrix(A) C = ArrayToBlockbytesMatrix(C) K = list(key) M = MessageEncryption(C, N, tag, K) - Auth = BuildAuth(A, K) + Auth = BuildAuth(TWEAK_BITS, A, K) tag2 = MesssageAuthTag(M, N, Auth, K) if(tag == tag2) : -- cgit v1.2.3