summaryrefslogtreecommitdiff
path: root/python/lilliput_ae_1.py
diff options
context:
space:
mode:
authorKévin Le Gouguec <kevin.legouguec@airbus.com>2019-03-21 16:04:16 +0100
committerKévin Le Gouguec <kevin.legouguec@airbus.com>2019-03-21 16:04:16 +0100
commitaaf0d2c49f343e909dc69b487056ca31238ffabf (patch)
tree9e054a390aad0be8f2acec8a77de3a7e97d881ba /python/lilliput_ae_1.py
parent57952a7dd9d9c586dcac84e46987d15d49674774 (diff)
downloadlilliput-ae-implem-aaf0d2c49f343e909dc69b487056ca31238ffabf.tar.xz
[implem-python] Nettoyage PEP8
- espaces avant ':' - espaces en trop après ',' - parenthèses dans les if - levée d'exception plutôt que 'return None' implicite Simplification de genkat_aead.py grâce à l'exception levée par le module.
Diffstat (limited to 'python/lilliput_ae_1.py')
-rw-r--r--python/lilliput_ae_1.py31
1 files changed, 17 insertions, 14 deletions
diff --git a/python/lilliput_ae_1.py b/python/lilliput_ae_1.py
index 8b618d9..9aad3a6 100644
--- a/python/lilliput_ae_1.py
+++ b/python/lilliput_ae_1.py
@@ -11,6 +11,7 @@ from helpers import (
BlockbytesMatrixToBytes,
BuildAuth,
Padding10LSB,
+ TagValidationError,
XorState
)
@@ -19,23 +20,23 @@ TWEAK_BITS = 192
TWEAK_BYTES = TWEAK_BITS//8
-def LowPart(array, number_bits) :
+def LowPart(array, number_bits):
shifted = 0
- for byte in range(0, len(array)) :
+ for byte in range(0, len(array)):
shifted |= (array[byte] << (8 * byte))
mask = 0
- for bit in range(0, number_bits) :
+ for bit in range(0, number_bits):
mask |= (0x1 << bit)
lower_part = shifted & mask
will_padd = 0
- if (number_bits % 8) != 0 :
+ if number_bits % 8 != 0:
will_padd = 1
- lower_part_byte = [0 for byte in range(0, int(number_bits / 8) + will_padd)]
- for byte in range(0, int(number_bits / 8) + will_padd) :
+ lower_part_byte = [0 for byte in range(0, int(number_bits / 8) + will_padd)]
+ for byte in range(0, int(number_bits / 8) + will_padd):
lower_part_byte[byte] = lower_part & 0xff
lower_part = lower_part >> 8
@@ -50,13 +51,13 @@ class _MessageTweak(Enum):
def TweakMessage(N, j, padding):
- tweak = [0 for byte in range(0, TWEAK_BYTES)]
- for byte in range(NONCE_BYTES-1, -1, -1) :
+ tweak = [0 for byte in range(0, TWEAK_BYTES)]
+ for byte in range(NONCE_BYTES-1, -1, -1):
tweak[byte + (TWEAK_BYTES-NONCE_BYTES)] |= (N[byte] & 0xf0) >> 4
tweak[byte + (TWEAK_BYTES-NONCE_BYTES-1)] |= (N[byte] & 0x0f) << 4
tweak[TWEAK_BYTES-NONCE_BYTES-1] |= ((j >> 64) & 0xf)
- for byte in range(TWEAK_BYTES-NONCE_BYTES-2, -1, -1) :
+ for byte in range(TWEAK_BYTES-NONCE_BYTES-2, -1, -1):
tweak[byte] = (j >> (8 * byte)) & 0xff
tweak[-1] |= padding.value<<4
@@ -96,7 +97,7 @@ def TreatMessageEnc(M, N, key):
return (Final, C)
-def TreatMessageDec(C, N, key) :
+def TreatMessageDec(C, N, key):
checksum = [0 for byte in range(0, BLOCK_BYTES)]
l = len(C)//BLOCK_BYTES
@@ -129,7 +130,7 @@ def TreatMessageDec(C, N, key) :
################################################################################
-def encrypt(A, M, N, key) :
+def encrypt(A, M, N, key):
K = list(key)
Auth = BuildAuth(TWEAK_BITS, A, K)
@@ -139,7 +140,7 @@ def encrypt(A, M, N, key) :
return BlockbytesMatrixToBytes(C), bytes(tag)
-def decrypt(A, C, N, tag, key) :
+def decrypt(A, C, N, tag, key):
K = list(key)
tag = list(tag)
@@ -147,5 +148,7 @@ def decrypt(A, C, N, tag, key) :
(Final, M) = TreatMessageDec(C, N, K)
tag2 = XorState(Auth, Final)
- if(tag == tag2) :
- return BlockbytesMatrixToBytes(M)
+ if tag != tag2:
+ raise TagValidationError(tag, tag2)
+
+ return BlockbytesMatrixToBytes(M)