From fc64da017336c553a345fdb690a2e496a4aefff3 Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Mon, 25 Mar 2019 10:59:24 +0100 Subject: [implem-python] Ajustements dans _tweak_message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hopefully, le résultat est plus clair en construisant le tweak par concaténations progressives. --- src/add_python/lilliput/ae_mode_1.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 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 23f4c7b..4a40b78 100644 --- a/src/add_python/lilliput/ae_mode_1.py +++ b/src/add_python/lilliput/ae_mode_1.py @@ -56,21 +56,25 @@ def _byte_from_nibbles(lower, upper): return upper<<4 | lower -def _tweak_message(N, j, padding): - j = integer_to_byte_array(j, (TWEAK_BITS-NONCE_BITS-4)//8+1) - - middle_byte = _byte_from_nibbles( - _lower_nibble(j[-1]), _lower_nibble(N[0]) - ) - - shifted_N = [ +def _tweak_message(N, j, prefix): + # j is encoded on 68 bits; get 72 and clear the upper 4. + j_len = (TWEAK_BITS-NONCE_BITS-4)//8 + 1 + tweak = integer_to_byte_array(j, j_len) + tweak[-1] &= 0b00001111 + + # Add nonce. + tweak[-1] |= _lower_nibble(N[0]) << 4 + tweak.extend( _byte_from_nibbles(_upper_nibble(N[i-1]), _lower_nibble(N[i])) for i in range(1, NONCE_BITS//8) - ] + ) - last_byte = _byte_from_nibbles(_upper_nibble(N[-1]), padding.value) + # Add last nibble from nonce and prefix. + tweak.append( + _byte_from_nibbles(_upper_nibble(N[-1]), prefix.value) + ) - return j[:-1] + [middle_byte] + shifted_N + [last_byte] + return tweak def _treat_message_enc(M, N, key): -- cgit v1.2.3