diff options
| author | Kévin Le Gouguec <kevin.legouguec@airbus.com> | 2019-03-14 11:15:35 +0100 |
|---|---|---|
| committer | Kévin Le Gouguec <kevin.legouguec@airbus.com> | 2019-03-21 14:49:15 +0100 |
| commit | d8eeb99d9106b93c0a30e3ab8849d7687d2a6f29 (patch) | |
| tree | 6f314d9837839ee30382af44e70d2b0878c9b40f | |
| parent | 95e1596db04fd55d777a1fccf031e86657ab1072 (diff) | |
| download | lilliput-ae-implem-d8eeb99d9106b93c0a30e3ab8849d7687d2a6f29.tar.xz | |
[implem-python] Simplification du contrôle des paramètres
Encore un peu de duplication sur les longueurs de clés valides. On y
travaille.
| -rwxr-xr-x | python/genkat_aead.py | 4 | ||||
| -rw-r--r-- | python/lilliput.py | 55 | ||||
| -rw-r--r-- | python/parameters_i_128.py | 6 | ||||
| -rw-r--r-- | python/parameters_i_192.py | 6 | ||||
| -rw-r--r-- | python/parameters_i_256.py | 6 | ||||
| -rw-r--r-- | python/parameters_ii_128.py | 6 | ||||
| -rw-r--r-- | python/parameters_ii_192.py | 6 | ||||
| -rw-r--r-- | python/parameters_ii_256.py | 6 |
8 files changed, 26 insertions, 69 deletions
diff --git a/python/genkat_aead.py b/python/genkat_aead.py index 3a69d72..6d7ca51 100755 --- a/python/genkat_aead.py +++ b/python/genkat_aead.py @@ -64,11 +64,11 @@ def generate_test_vectors(mode, keylen): print_bstr(output, 'PT', msg) print_bstr(output, 'AD', ad) - ct, tag = lilliput.mainEnc(msg, ad, key, nonce, mode, keylen) + ct, tag = lilliput.mainEnc(msg, ad, key, nonce, mode) print_bstr(output, 'CT', ct+tag) - msg2 = lilliput.mainDec(ct, tag, ad, key, nonce, mode, keylen) + msg2 = lilliput.mainDec(ct, tag, ad, key, nonce, mode) if msg != msg2: raise DecryptionError(msg, msg2, mode, keylen) diff --git a/python/lilliput.py b/python/lilliput.py index 79f14e5..3fe41bf 100644 --- a/python/lilliput.py +++ b/python/lilliput.py @@ -1,34 +1,23 @@ import lilliput_ae_1 import lilliput_ae_2 -import parameters_i_128 as i_128 -import parameters_i_192 as i_192 -import parameters_i_256 as i_256 -import parameters_ii_128 as ii_128 -import parameters_ii_192 as ii_192 -import parameters_ii_256 as ii_256 - BLOCK_BYTES = 16 N_BYTES = 15 -def GetParameters(mode = 1, length = 128) : - if(mode == 1 and length == 128) : - return (i_128.KEY_BITS, i_128.TWEAK_BITS, i_128.ROUNDS) - - if(mode == 1 and length == 192) : - return (i_192.KEY_BITS, i_192.TWEAK_BITS, i_192.ROUNDS) +def _getParameters(mode=1, key_length=128) : + rounds = { + 128: 32, + 192: 36, + 256: 42 + } - if(mode == 1 and length == 256) : - return (i_256.KEY_BITS, i_256.TWEAK_BITS, i_256.ROUNDS) + tweak_lengths = { + 1: 192, + 2: 128 + } - if(mode == 2 and length == 128) : - return (ii_128.KEY_BITS, ii_128.TWEAK_BITS, ii_128.ROUNDS) + return tweak_lengths[mode], rounds[key_length] - if(mode == 2 and length == 192) : - return (ii_192.KEY_BITS, ii_192.TWEAK_BITS, ii_192.ROUNDS) - - if(mode == 2 and length == 256) : - return (ii_256.KEY_BITS, ii_256.TWEAK_BITS, ii_256.ROUNDS) def ArrayToBlockbytesMatrix(array) : length = len(array) @@ -53,18 +42,21 @@ def BlockbytesMatrixToBytes(matrix): ############################################ -def _checkInputs(key, length, nonce): - if len(key) != length//8: - raise ValueError('invalid key size: {} != {}'.format(len(key), length//8)) +def _checkInputs(key, nonce): + valid_key_lengths = (128, 192, 256) + + if len(key)*8 not in valid_key_lengths: + raise ValueError('invalid key size: {} not in {}'.format(len(key)*8, valid_key_lengths)) 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) +def mainEnc(plaintext, adata, key, nonce, mode): + _checkInputs(key, nonce) - (key_bits, tweak_bits, rounds) = GetParameters(mode, length) + key_bits = len(key)*8 + tweak_bits, rounds = _getParameters(mode, key_bits) A = adata M = plaintext @@ -84,10 +76,11 @@ def mainEnc(plaintext, adata, key, nonce, mode=1, length=128): return BlockbytesMatrixToBytes(C), bytes(tag) -def mainDec(ciphertext, tag, adata, key, nonce, mode=1, length=128): - _checkInputs(key, length, nonce) +def mainDec(ciphertext, tag, adata, key, nonce, mode): + _checkInputs(key, nonce) - (key_bits, tweak_bits, rounds) = GetParameters(mode, length) + key_bits = len(key)*8 + tweak_bits, rounds = _getParameters(mode, key_bits) A = adata C = ciphertext diff --git a/python/parameters_i_128.py b/python/parameters_i_128.py deleted file mode 100644 index 7f0675a..0000000 --- a/python/parameters_i_128.py +++ /dev/null @@ -1,6 +0,0 @@ -""" - Lilliput ae i 128 -""" -KEY_BITS = 128 -TWEAK_BITS = 192 -ROUNDS = 32 diff --git a/python/parameters_i_192.py b/python/parameters_i_192.py deleted file mode 100644 index c513331..0000000 --- a/python/parameters_i_192.py +++ /dev/null @@ -1,6 +0,0 @@ -""" - Lilliput ae i 128 -""" -KEY_BITS = 192 -TWEAK_BITS = 192 -ROUNDS = 36 diff --git a/python/parameters_i_256.py b/python/parameters_i_256.py deleted file mode 100644 index ab81130..0000000 --- a/python/parameters_i_256.py +++ /dev/null @@ -1,6 +0,0 @@ -""" - Lilliput ae i 128 -""" -KEY_BITS = 256 -TWEAK_BITS = 192 -ROUNDS = 42 diff --git a/python/parameters_ii_128.py b/python/parameters_ii_128.py deleted file mode 100644 index 8647a66..0000000 --- a/python/parameters_ii_128.py +++ /dev/null @@ -1,6 +0,0 @@ -""" - Lilliput ae i 128 -""" -KEY_BITS = 128 -TWEAK_BITS = 128 -ROUNDS = 32 diff --git a/python/parameters_ii_192.py b/python/parameters_ii_192.py deleted file mode 100644 index d17d6ec..0000000 --- a/python/parameters_ii_192.py +++ /dev/null @@ -1,6 +0,0 @@ -""" - Lilliput ae i 128 -""" -KEY_BITS = 192 -TWEAK_BITS = 128 -ROUNDS = 36 diff --git a/python/parameters_ii_256.py b/python/parameters_ii_256.py deleted file mode 100644 index 159c78f..0000000 --- a/python/parameters_ii_256.py +++ /dev/null @@ -1,6 +0,0 @@ -""" - Lilliput ae i 128 -""" -KEY_BITS = 256 -TWEAK_BITS = 128 -ROUNDS = 42 |
