diff options
| author | Kévin Le Gouguec <kevin.legouguec@airbus.com> | 2019-03-14 11:06:41 +0100 |
|---|---|---|
| committer | Kévin Le Gouguec <kevin.legouguec@airbus.com> | 2019-03-21 14:49:15 +0100 |
| commit | 95e1596db04fd55d777a1fccf031e86657ab1072 (patch) | |
| tree | 553a11bc2d6251ecbc137bcc6910414208fb1060 /python | |
| parent | d26dfcef1bca5d86ce9042b78605a399b6d74423 (diff) | |
| download | lilliput-ae-implem-95e1596db04fd55d777a1fccf031e86657ab1072.tar.xz | |
[implem-python] Passage des clés et nonces par paramètres
Diffstat (limited to 'python')
| -rwxr-xr-x | python/genkat_aead.py | 11 | ||||
| -rw-r--r-- | python/lilliput.py | 20 |
2 files changed, 21 insertions, 10 deletions
diff --git a/python/genkat_aead.py b/python/genkat_aead.py index e9f9101..3a69d72 100755 --- a/python/genkat_aead.py +++ b/python/genkat_aead.py @@ -46,6 +46,9 @@ def generate_test_vectors(mode, keylen): directory, 'LWC_AEAD_KAT_{keylen}_120.txt'.format(keylen=keylen) ) + nonce = bytes(range(CRYPTO_NPUBBYTES)) + key = bytes(range(keylen//8)) + with open(output_path, 'w') as output: count = 1 @@ -56,16 +59,16 @@ def generate_test_vectors(mode, keylen): msg = bytes(range(mlen)) ad = bytes(range(adlen)) - print_bstr(output, 'Key', bytes(range(keylen//8))) - print_bstr(output, 'Nonce', bytes(range(CRYPTO_NPUBBYTES))) + print_bstr(output, 'Key', key) + print_bstr(output, 'Nonce', nonce) print_bstr(output, 'PT', msg) print_bstr(output, 'AD', ad) - ct, tag = lilliput.mainEnc(msg, ad, mode, keylen) + ct, tag = lilliput.mainEnc(msg, ad, key, nonce, mode, keylen) print_bstr(output, 'CT', ct+tag) - msg2 = lilliput.mainDec(ct, tag, ad, mode, keylen) + msg2 = lilliput.mainDec(ct, tag, ad, key, nonce, mode, keylen) if msg != msg2: raise DecryptionError(msg, msg2, mode, keylen) diff --git a/python/lilliput.py b/python/lilliput.py index 92d8662..79f14e5 100644 --- a/python/lilliput.py +++ b/python/lilliput.py @@ -53,14 +53,22 @@ def BlockbytesMatrixToBytes(matrix): ############################################ -def mainEnc(plaintext, adata, mode=1, length=128): +def _checkInputs(key, length, nonce): + if len(key) != length//8: + raise ValueError('invalid key size: {} != {}'.format(len(key), length//8)) + + if len(nonce) != N_BYTES: + raise ValueError('nonce must be {}-byte long'.format(N_BYTES)) + + +def mainEnc(plaintext, adata, key, nonce, mode=1, length=128): + _checkInputs(key, length, nonce) (key_bits, tweak_bits, rounds) = GetParameters(mode, length) A = adata M = plaintext - N = [byte for byte in range(0, N_BYTES)] - key = [byte for byte in range(0, int(key_bits/8))] + N = nonce A_BITS = 8 * len(A) M_BITS = 8 * len(M) @@ -76,14 +84,14 @@ def mainEnc(plaintext, adata, mode=1, length=128): return BlockbytesMatrixToBytes(C), bytes(tag) -def mainDec(ciphertext, tag, adata, mode=1, length=128): +def mainDec(ciphertext, tag, adata, key, nonce, mode=1, length=128): + _checkInputs(key, length, nonce) (key_bits, tweak_bits, rounds) = GetParameters(mode, length) A = adata C = ciphertext - N = [byte for byte in range(0, N_BYTES)] - key = [byte for byte in range(0, int(key_bits/8))] + N = nonce tag = list(tag) M_BITS = 8 * len(C) |
