From 7350fbb6583236b929235a8be7f17f149901f004 Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Thu, 21 Mar 2019 17:16:21 +0100 Subject: [implem-python] Simplification de pad10* MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dans le cadre d'une croisade contre les range(len(…)). Suppression d'un paramètre inutile dans la foulée. --- python/helpers.py | 23 ++++++----------------- python/lilliput_ae_1.py | 4 ++-- python/lilliput_ae_2.py | 10 +++++----- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/python/helpers.py b/python/helpers.py index 07affa9..be4b406 100644 --- a/python/helpers.py +++ b/python/helpers.py @@ -32,19 +32,9 @@ def XorState(state1, state2): return [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 Padding10LSB(X): + zeroes = [0] * (BLOCK_BYTES-len(X)-1) + return zeroes + [0b10000000] + X def _tweakAssociatedData(t, i, padded): @@ -69,8 +59,7 @@ def _tweakAssociatedData(t, i, padded): 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 + need_padding = len(A)%BLOCK_BYTES > 0 A = ArrayToBlockbytesMatrix(A) @@ -79,11 +68,11 @@ def BuildAuth(t, A, key): enc = LilliputTBCEnc(tweak, key, A[i]) Auth = XorState(Auth, enc) - if padding_bytes == 0: + if not need_padding: return Auth tweak = _tweakAssociatedData(t, l_a, padded=True) - ad_padded = Padding10LSB(A[l_a], padding_bytes*8) + ad_padded = Padding10LSB(A[l_a]) enc = LilliputTBCEnc(tweak, key, ad_padded) Auth = XorState(Auth, enc) diff --git a/python/lilliput_ae_1.py b/python/lilliput_ae_1.py index 0bc4236..5cc263b 100644 --- a/python/lilliput_ae_1.py +++ b/python/lilliput_ae_1.py @@ -85,7 +85,7 @@ def TreatMessageEnc(M, N, key): Final = ltbc.LilliputTBCEnc(tweak, key, checksum) else: - m_padded = Padding10LSB(M[l], padding_bytes*8) + m_padded = Padding10LSB(M[l]) checksum = XorState(checksum, m_padded) tweak = TweakMessage(N, l, _MessageTweak.PAD) pad = ltbc.LilliputTBCEnc(tweak, key, [0 for byte in range(0, BLOCK_BYTES)]) @@ -122,7 +122,7 @@ def TreatMessageDec(C, N, key): lower_part = LowPart(pad, padding_bytes*8) M.append(XorState(C[l], lower_part)) - m_padded = Padding10LSB(M[l], padding_bytes*8) + m_padded = Padding10LSB(M[l]) checksum = XorState(checksum, m_padded) tweak_final = TweakMessage(N, l+1, _MessageTweak.FINAL) Final = ltbc.LilliputTBCEnc(tweak_final, key, checksum) diff --git a/python/lilliput_ae_2.py b/python/lilliput_ae_2.py index 3f72020..2e7843b 100644 --- a/python/lilliput_ae_2.py +++ b/python/lilliput_ae_2.py @@ -55,7 +55,7 @@ def AddTagJ(tag, j): def MesssageAuthTag(M, N, Auth, key): l = len(M)//BLOCK_BYTES - padding_bytes = len(M)%BLOCK_BYTES + need_padding = len(M)%BLOCK_BYTES > 0 tag = list(Auth) M = ArrayToBlockbytesMatrix(M) @@ -65,9 +65,9 @@ def MesssageAuthTag(M, N, Auth, key): encryption = ltbc.LilliputTBCEnc(tweak, key, M[j]) tag = XorState(tag, encryption) - if padding_bytes > 0: + if need_padding: tweak = TweakTag(l, True) - m_padded = Padding10LSB(M[l], 8*padding_bytes) + m_padded = Padding10LSB(M[l]) encryption = ltbc.LilliputTBCEnc(tweak, key, m_padded) tag = XorState(tag, encryption) @@ -80,7 +80,7 @@ def MesssageAuthTag(M, N, Auth, key): def MessageEncryption(M, N, tag, key): l = len(M)//BLOCK_BYTES - padding_bytes = len(M)%BLOCK_BYTES + need_padding = len(M)%BLOCK_BYTES > 0 M = ArrayToBlockbytesMatrix(M) C = [] @@ -91,7 +91,7 @@ def MessageEncryption(M, N, tag, key): encryption = ltbc.LilliputTBCEnc(tweak, key, padded_nonce) C.append(XorState(M[j], encryption)) - if padding_bytes > 0: + if need_padding: tweak = AddTagJ(tag, l) padded_nonce = list(N) + [0x00] encryption = ltbc.LilliputTBCEnc(tweak, key, padded_nonce) -- cgit v1.2.3