From 33c615feaaf148c099ee4299ad2c8a6f7e1778cf Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Sun, 24 Mar 2019 15:19:15 +0100 Subject: [implem-python] Réécriture de certains range() dans tbc.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IME, itérer sur un range() est rarement la façon la plus expressive de faire les choses ; les alternatives imposent une structure qui rendent l'intention plus claire. E.g. quand on voit une compréhension, on comprend que l'auteur cherche à filtrer et/ou transformer ce sur quoi il itère. Réutilisation de xor_state(), renommé xor() puisqu'il sert dans plusieurs situations. Séparation de ce xor() et des fonctions communes aux modes authentifiés pour éviter un import circulaire. --- src/add_python/lilliput/ae_mode_1.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/add_python/lilliput/ae_mode_1.py') diff --git a/src/add_python/lilliput/ae_mode_1.py b/src/add_python/lilliput/ae_mode_1.py index cc550e8..efa0b6f 100644 --- a/src/add_python/lilliput/ae_mode_1.py +++ b/src/add_python/lilliput/ae_mode_1.py @@ -21,13 +21,13 @@ using Lilliput-AE's nonce-respecting mode based on ΘCB3. from enum import Enum from .constants import BLOCK_BYTES, NONCE_BYTES -from .helpers import ( +from .ae_common import ( bytes_to_block_matrix, block_matrix_to_bytes, build_auth, pad10, TagValidationError, - xor_state + xor ) from . import tbc @@ -92,7 +92,7 @@ def _treat_message_enc(M, N, key): C = [] for j in range(0, l): - checksum = xor_state(checksum, M[j]) + checksum = xor(checksum, M[j]) tweak = _tweak_message(N, j, _MessageTweak.BLOCK) C.append(tbc.encrypt(tweak, key, M[j])) @@ -102,12 +102,12 @@ def _treat_message_enc(M, N, key): else: m_padded = pad10(M[l]) - checksum = xor_state(checksum, m_padded) + checksum = xor(checksum, m_padded) tweak = _tweak_message(N, l, _MessageTweak.PAD) pad = tbc.encrypt(tweak, key, [0 for byte in range(0, BLOCK_BYTES)]) lower_part = _low_part(pad, padding_bytes*8) - C.append(xor_state(M[l], lower_part)) + C.append(xor(M[l], lower_part)) tweak_final = _tweak_message(N, l+1, _MessageTweak.FINAL) Final = tbc.encrypt(tweak_final, key, checksum) @@ -126,7 +126,7 @@ def _treat_message_dec(C, N, key): for j in range(0, l): tweak = _tweak_message(N, j, _MessageTweak.BLOCK) M.append(tbc.decrypt(tweak, key, C[j])) - checksum = xor_state(checksum, M[j]) + checksum = xor(checksum, M[j]) if padding_bytes == 0: tweak = _tweak_message(N, l, _MessageTweak.NO_PADDING) @@ -136,10 +136,10 @@ def _treat_message_dec(C, N, key): tweak = _tweak_message(N, l, _MessageTweak.PAD) pad = tbc.encrypt(tweak, key, [0 for byte in range(0, BLOCK_BYTES)]) lower_part = _low_part(pad, padding_bytes*8) - M.append(xor_state(C[l], lower_part)) + M.append(xor(C[l], lower_part)) m_padded = pad10(M[l]) - checksum = xor_state(checksum, m_padded) + checksum = xor(checksum, m_padded) tweak_final = _tweak_message(N, l+1, _MessageTweak.FINAL) Final = tbc.encrypt(tweak_final, key, checksum) @@ -151,7 +151,7 @@ def encrypt(A, M, N, key): Auth = build_auth(TWEAK_BITS, A, K) (Final, C) = _treat_message_enc(M, N, K) - tag = xor_state(Auth, Final) + tag = xor(Auth, Final) return block_matrix_to_bytes(C), bytes(tag) @@ -162,7 +162,7 @@ def decrypt(A, C, N, tag, key): Auth = build_auth(TWEAK_BITS, A, K) (Final, M) = _treat_message_dec(C, N, K) - tag2 = xor_state(Auth, Final) + tag2 = xor(Auth, Final) if tag != tag2: raise TagValidationError(tag, tag2) -- cgit v1.2.3