summaryrefslogtreecommitdiff
path: root/python/lilliput_ae_2.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_2.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_2.py')
-rw-r--r--python/lilliput_ae_2.py49
1 files changed, 27 insertions, 22 deletions
diff --git a/python/lilliput_ae_2.py b/python/lilliput_ae_2.py
index 624fd93..3f72020 100644
--- a/python/lilliput_ae_2.py
+++ b/python/lilliput_ae_2.py
@@ -3,12 +3,13 @@
"""
import lilliput_tbc as ltbc
-from constants import BLOCK_BITS, BLOCK_BYTES
+from constants import BLOCK_BYTES
from helpers import (
ArrayToBlockbytesMatrix,
BlockbytesMatrixToBytes,
BuildAuth,
Padding10LSB,
+ TagValidationError,
XorState
)
@@ -18,30 +19,31 @@ TWEAK_BYTES = TWEAK_BITS//8
def TweakTag(j, padded):
- tweak = [0 for byte in range(0, TWEAK_BYTES)]
+ tweak = [0 for byte in range(0, TWEAK_BYTES)]
- tweak[TWEAK_BYTES - 1] |= ((j >> 120) & 0xf)
- for byte in range(TWEAK_BYTES - 2, -1, -1) :
- tweak[byte] = (j >> (8 * byte)) & 0xff
+ tweak[TWEAK_BYTES - 1] |= ((j >> 120) & 0xf)
+ for byte in range(TWEAK_BYTES - 2, -1, -1):
+ tweak[byte] = (j >> (8 * byte)) & 0xff
- if padded:
- tweak[TWEAK_BYTES - 1] |= 0x40
+ if padded:
+ tweak[TWEAK_BYTES - 1] |= 0x40
+
+ return tweak
- return tweak
-def TweakTagEnd(N) :
- tweak = [0 for byte in range(0, TWEAK_BYTES)]
+def TweakTagEnd(N):
+ tweak = [0 for byte in range(0, TWEAK_BYTES)]
- for byte in range(0, TWEAK_BYTES - 1) :
+ for byte in range(0, TWEAK_BYTES - 1):
tweak[byte] = N[byte]
tweak[TWEAK_BYTES - 1] = 0x10
return tweak
-def AddTagJ(tag, j) :
+def AddTagJ(tag, j):
array_j = [0 for byte in range(0, TWEAK_BYTES)]
- for byte in range(0, TWEAK_BYTES) :
+ for byte in range(0, TWEAK_BYTES):
array_j[byte] = (j >> (byte * 8))
xorr = XorState(tag, array_j)
@@ -51,19 +53,19 @@ def AddTagJ(tag, j) :
return xorr
-def MesssageAuthTag(M, N, Auth, key) :
+def MesssageAuthTag(M, N, Auth, key):
l = len(M)//BLOCK_BYTES
padding_bytes = len(M)%BLOCK_BYTES
tag = list(Auth)
M = ArrayToBlockbytesMatrix(M)
- for j in range(0, l) :
+ for j in range(0, l):
tweak = TweakTag(j, False)
encryption = ltbc.LilliputTBCEnc(tweak, key, M[j])
tag = XorState(tag, encryption)
- if padding_bytes > 0 :
+ if padding_bytes > 0:
tweak = TweakTag(l, True)
m_padded = Padding10LSB(M[l], 8*padding_bytes)
encryption = ltbc.LilliputTBCEnc(tweak, key, m_padded)
@@ -76,14 +78,14 @@ def MesssageAuthTag(M, N, Auth, key) :
return tag
-def MessageEncryption(M, N, tag, key) :
+def MessageEncryption(M, N, tag, key):
l = len(M)//BLOCK_BYTES
padding_bytes = len(M)%BLOCK_BYTES
M = ArrayToBlockbytesMatrix(M)
C = []
- for j in range(0, l) :
+ for j in range(0, l):
tweak = AddTagJ(tag, j)
padded_nonce = list(N) + [0x00]
encryption = ltbc.LilliputTBCEnc(tweak, key, padded_nonce)
@@ -97,8 +99,9 @@ def MessageEncryption(M, N, tag, key) :
return C
+
################################################################################
-def encrypt(A, M, N, key) :
+def encrypt(A, M, N, key):
K = list(key)
Auth = BuildAuth(TWEAK_BITS, A, K)
@@ -108,7 +111,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)
@@ -118,5 +121,7 @@ def decrypt(A, C, N, tag, key) :
Auth = BuildAuth(TWEAK_BITS, A, K)
tag2 = MesssageAuthTag(M, N, Auth, K)
- if tag == tag2:
- return M
+ if tag != tag2:
+ raise TagValidationError(tag, tag2)
+
+ return M