diff options
| author | Kévin Le Gouguec <kevin.legouguec@airbus.com> | 2019-03-13 17:19:49 +0100 |
|---|---|---|
| committer | Kévin Le Gouguec <kevin.legouguec@airbus.com> | 2019-03-13 17:46:05 +0100 |
| commit | 6bcce333963bff1bb588abbe20156abf07004928 (patch) | |
| tree | 29b511e8d6bcc9b665481e21a7aabdd56fae117d /python/genkat_aead.py | |
| parent | 1870b1eadf3963f9ec5d52bed21f04e523933612 (diff) | |
| download | lilliput-ae-implem-6bcce333963bff1bb588abbe20156abf07004928.tar.xz | |
Traduction de genkat_aead.c en Python
Et ré-adaptation de l'API de lilliput.py pour simplifier
l'interfaçage ; et retrait des print() pour accélérer la génération
des vecteurs (qui même comme ça prend 2 bonnes minutes).
NB : pour le moment, les vecteurs ne correspondent pas…
Diffstat (limited to 'python/genkat_aead.py')
| -rwxr-xr-x | python/genkat_aead.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/python/genkat_aead.py b/python/genkat_aead.py new file mode 100755 index 0000000..5a50e03 --- /dev/null +++ b/python/genkat_aead.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 + +import lilliput + + +MAX_MESSAGE_LENGTH = 32 +MAX_ADATA_LENGTH = 32 + +CRYPTO_NPUBBYTES = 120//8 + + +def bstr(buf): + return ''.join('{:02X}'.format(b) for b in buf) + + +def print_bstr(output, label, buf): + print('{l} = {b}'.format(l=label, b=bstr(buf)), file=output) + + +class DecryptionError(Exception): + def __init__(self, expected, actual, mode, keylen): + self.expected = expected + self.actual = actual + self.mode = mode + self.keylen = keylen + + def __str__(self): + return '({s.mode} / {s.keylen}) Expected {exp}; got {act}'.format( + s=self, + exp=bstr(self.expected), + act=bstr(self.actual) + ) + + +def generate_test_vectors(mode, keylen): + + print('generating for', mode, keylen) + + output_path = 'LWC_AEAD_KAT_{mode}_{keylen}'.format(mode=mode, keylen=keylen) + with open(output_path, 'w') as output: + + count = 1 + for mlen in range(MAX_MESSAGE_LENGTH+1): + for adlen in range(MAX_ADATA_LENGTH+1): + print('Count = {c}'.format(c=count), file=output) + + 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, 'PT', msg) + print_bstr(output, 'AD', ad) + + ct, tag = lilliput.mainEnc(msg, ad, mode, keylen) + + print_bstr(output, 'CT', ct+tag) + + msg2 = lilliput.mainDec(ct, tag, ad, mode, keylen) + + if msg != msg2: + raise DecryptionError(msg, msg2, mode, keylen) + + count+=1 + + print(file=output) + + +if __name__ == '__main__': + for mode in 1, 2: + for keylen in 128, 192, 256: + generate_test_vectors(mode, keylen) |
