From 52c8c1a2b72e11f8814215034139e8991f1aeb63 Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Thu, 14 Mar 2019 16:11:26 +0100 Subject: [implem-python] Extraction de fonctions communes aux deux modes --- python/helpers.py | 66 ++++++++++++++++++++++++++++++++++++++++++- python/lilliput_ae_1.py | 75 ++++++++----------------------------------------- 2 files changed, 77 insertions(+), 64 deletions(-) (limited to 'python') diff --git a/python/helpers.py b/python/helpers.py index 34949a4..3d741b0 100644 --- a/python/helpers.py +++ b/python/helpers.py @@ -1,4 +1,5 @@ -from constants import BLOCK_BYTES +from constants import BLOCK_BITS, BLOCK_BYTES +from lilliput_tbc import LilliputTBCEnc def ArrayToBlockbytesMatrix(array) : @@ -23,3 +24,66 @@ def ArrayToBlockbytesMatrix(array) : def BlockbytesMatrixToBytes(matrix): return bytes(byte for block in matrix for byte in block) + +def XorState(state1, state2): + return list( + s1^s2 for (s1, s2) in zip(state1, state2) + ) + + +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 _tweakAssociatedData(t, i, padded): + t_bytes = t//8 + tweak = [0]*(t_bytes) + + mask = 0xff + for byte in range(t_bytes-1): + tweak[byte] = (i & mask) >> (byte * 8) + mask = mask << 8 + + mask = (0xf << (8 * t_bytes-1)) + tweak[-1] = (i & mask) >> ((t_bytes-1)*8) + if not padded: + tweak[-1] |= 0x20 + else: + tweak[-1] |= 0x60 + + return tweak + + +def BuildAuth(t, A, key): + Auth = [0 for byte in range(0, BLOCK_BYTES)] + l_a = len(A)//BLOCK_BYTES + + padding_bytes = len(A)%BLOCK_BYTES + + A = ArrayToBlockbytesMatrix(A) + + for i in range(0, l_a): + tweak = _tweakAssociatedData(t, i, padded=False) + enc = LilliputTBCEnc(tweak, key, A[i]) + Auth = XorState(Auth, enc) + + if padding_bytes == 0: + return Auth + + tweak = _tweakAssociatedData(t, l_a, padded=True) + ad_padded = Padding10LSB(A[l_a], padding_bytes*8) + enc = LilliputTBCEnc(tweak, key, ad_padded) + Auth = XorState(Auth, enc) + + return Auth diff --git a/python/lilliput_ae_1.py b/python/lilliput_ae_1.py index c9a731e..8cf55bb 100644 --- a/python/lilliput_ae_1.py +++ b/python/lilliput_ae_1.py @@ -2,10 +2,17 @@ OCB 3 for lilliput ae i """ -import lilliput_tbc as ltbc -from constants import BLOCK_BITS, BLOCK_BYTES, NONCE_BYTES from enum import Enum -from helpers import ArrayToBlockbytesMatrix, BlockbytesMatrixToBytes + +import lilliput_tbc as ltbc +from constants import BLOCK_BYTES, NONCE_BYTES +from helpers import ( + ArrayToBlockbytesMatrix, + BlockbytesMatrixToBytes, + BuildAuth, + Padding10LSB, + XorState +) KEY_BITS = 128 @@ -35,26 +42,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,45 +66,7 @@ def LowPart(array, number_bits) : ############################################################################### -def TweakAssociatedData(i, padded) : - 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 not padded: - 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 = len(A)//BLOCK_BYTES - - padding_bytes = len(A)%BLOCK_BYTES - - A = ArrayToBlockbytesMatrix(A) - - for i in range(0, l_a) : - tweak = TweakAssociatedData(i, padded=False) - enc = ltbc.LilliputTBCEnc(tweak, key, A[i]) - Auth = XorState(Auth, enc) - - if padding_bytes == 0: - return Auth - - tweak = TweakAssociatedData(l_a, padded=True) - ad_padded = Padding10LSB(A[l_a], padding_bytes*8) - enc = ltbc.LilliputTBCEnc(tweak, key, ad_padded) - Auth = XorState(Auth, enc) - return Auth ################################################################################ @@ -214,7 +163,7 @@ def OCB3Enc(A, M, N, key) : K = list(key) - Auth = BuildAuth(A, K) + Auth = BuildAuth(TWEAK_BITS, A, K) (Final, C) = TreatMessageEnc(M, N, K) tag = XorState(Auth, Final) @@ -226,7 +175,7 @@ def OCB3Dec(A, C, N, tag, key) : K = list(key) - Auth = BuildAuth(A, K) + Auth = BuildAuth(TWEAK_BITS, A, K) (Final, M) = TreatMessageDec(C, N, K) tag2 = XorState(Auth, Final) -- cgit v1.2.3