diff options
| author | Kévin Le Gouguec <kevin.legouguec@airbus.com> | 2019-03-25 11:01:42 +0100 |
|---|---|---|
| committer | Kévin Le Gouguec <kevin.legouguec@airbus.com> | 2019-03-25 11:01:42 +0100 |
| commit | 24c5f5d817085bd875fa6b86ef261d87b9d5fef4 (patch) | |
| tree | b70624d4c77e1635c9f8f4953a1b46fce8a1631a /test | |
| parent | 7d08844da485016ce87432a36b397d9919d91f38 (diff) | |
| parent | fc64da017336c553a345fdb690a2e496a4aefff3 (diff) | |
| download | lilliput-ae-implem-24c5f5d817085bd875fa6b86ef261d87b9d5fef4.tar.xz | |
Merge branch 'refactor-python-implem'
Diffstat (limited to 'test')
| -rwxr-xr-x | test/python.sh | 8 | ||||
| -rwxr-xr-x | test/python/compare.sh | 17 | ||||
| -rw-r--r-- | test/python/crypto_aead.py | 40 | ||||
| -rwxr-xr-x | test/python/generate-vectors.sh | 41 | ||||
| -rwxr-xr-x | test/python/genkat_aead.py | 72 |
5 files changed, 178 insertions, 0 deletions
diff --git a/test/python.sh b/test/python.sh new file mode 100755 index 0000000..df4ce01 --- /dev/null +++ b/test/python.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +set -eu + +TEST_DIR=$(dirname $0) + +${TEST_DIR}/python/generate-vectors.sh +${TEST_DIR}/python/compare.sh diff --git a/test/python/compare.sh b/test/python/compare.sh new file mode 100755 index 0000000..414eb1e --- /dev/null +++ b/test/python/compare.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +PYTHON_DIR=$(dirname $0) +ROOT_DIR=${PYTHON_DIR}/../../ +RESULTS_DIR=${ROOT_DIR}/results +CRYPTO_AEAD=${ROOT_DIR}/crypto_aead + +set -eu + +mkdir -p ${RESULTS_DIR}/crypto_aead_ref +for d in ${CRYPTO_AEAD}/lilliputaei* +do + mkdir -p ${RESULTS_DIR}/crypto_aead_ref/$(basename $d) + cp $d/LWC*.txt ${RESULTS_DIR}/crypto_aead_ref/$(basename $d)/ +done + +diff -ru ${RESULTS_DIR}/crypto_aead_ref ${RESULTS_DIR}/crypto_aead_py diff --git a/test/python/crypto_aead.py b/test/python/crypto_aead.py new file mode 100644 index 0000000..d2f1896 --- /dev/null +++ b/test/python/crypto_aead.py @@ -0,0 +1,40 @@ +# Implementation of the Lilliput-AE tweakable block cipher. +# +# Authors, hereby denoted as "the implementer": +# Kévin Le Gouguec, +# 2019. +# +# For more information, feedback or questions, refer to our website: +# https://paclido.fr/lilliput-ae +# +# To the extent possible under law, the implementer has waived all copyright +# and related or neighboring rights to the source code in this file. +# http://creativecommons.org/publicdomain/zero/1.0/ + +"""Python port of the crypto_aead API for Lilliput-AE.""" + +import lilliput + +from lilliput.constants import ( + NONCE_BITS, + TAG_BYTES +) + +from parameters import ( + KEYBYTES, # Expose to genkat_aead. + MODE +) + + +NPUBBYTES = NONCE_BITS//8 + + +def encrypt(m, ad, npub, k): + c, tag = lilliput.encrypt(m, ad, k, npub, MODE) + return c+tag + + +def decrypt(c, ad, npub, k): + ctext = c[:-TAG_BYTES] + tag = c[-TAG_BYTES:] + return lilliput.decrypt(ctext, tag, ad, k, npub, MODE) diff --git a/test/python/generate-vectors.sh b/test/python/generate-vectors.sh new file mode 100755 index 0000000..d37f15f --- /dev/null +++ b/test/python/generate-vectors.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +set -eu + +PYTHON_DIR=$(dirname $0) +ROOT_DIR=${PYTHON_DIR}/../../ +SRC_DIR=${ROOT_DIR}/src/add_python +RESULTS_DIR=${ROOT_DIR}/results +CRYPTO_AEAD=${RESULTS_DIR}/crypto_aead_py + +mkdir -p ${CRYPTO_AEAD}/lilliputae{i,ii}{128,192,256}v1 + +declare -A NAMES=([1]=lilliputaei [2]=lilliputaeii) + + +generate () +{ + local mode=$1 + local keylen=$2 + + echo generating for ${mode} ${keylen} + + cat <<EOF > ${RESULTS_DIR}/parameters.py +MODE = ${mode} +KEYBYTES = $((keylen/8)) +EOF + + PYTHONPATH=${RESULTS_DIR}:${SRC_DIR} ${PYTHON_DIR}/genkat_aead.py + + dest=${CRYPTO_AEAD}/${NAMES[${mode}]}${keylen}v1 + mv LWC_AEAD_KAT_${keylen}_120.txt ${dest} +} + + +for mode in 1 2 +do + for keylen in 128 192 256 + do + generate ${mode} ${keylen} + done +done diff --git a/test/python/genkat_aead.py b/test/python/genkat_aead.py new file mode 100755 index 0000000..db3a89c --- /dev/null +++ b/test/python/genkat_aead.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 + +# Python port of genkat_aead.c. +# +# Authors, hereby denoted as "the implementer": +# Kévin Le Gouguec, +# 2019. +# +# For more information, feedback or questions, refer to our website: +# https://paclido.fr/lilliput-ae +# +# To the extent possible under law, the implementer has waived all copyright +# and related or neighboring rights to the source code in this file. +# http://creativecommons.org/publicdomain/zero/1.0/ + +"""Python port of the genkat_aead.c program.""" + +import crypto_aead + + +class DecryptionError(Exception): + def __init__(self): + super().__init__('crypto_aead.decrypt did not recover the plaintext') + + +MAX_MESSAGE_LENGTH = 32 +MAX_ASSOCIATED_DATA_LENGTH = 32 + + +def print_bstr(output, label, buf): + print('{l} = {b}'.format(l=label, b=buf.hex().upper()), file=output) + + +def generate_test_vectors(): + count = 1 + filename = 'LWC_AEAD_KAT_{key}_{npub}.txt'.format( + key=crypto_aead.KEYBYTES*8, npub=crypto_aead.NPUBBYTES*8 + ) + + npub = bytes(range(crypto_aead.NPUBBYTES)) + key = bytes(range(crypto_aead.KEYBYTES)) + + with open(filename, 'w') as output: + + for mlen in range(MAX_MESSAGE_LENGTH+1): + for adlen in range(MAX_ASSOCIATED_DATA_LENGTH+1): + + msg = bytes(range(mlen)) + ad = bytes(range(adlen)) + + print('Count = {c}'.format(c=count), file=output) + count += 1 + + print_bstr(output, 'Key', key) + print_bstr(output, 'Nonce', npub) + print_bstr(output, 'PT', msg) + print_bstr(output, 'AD', ad) + + ct = crypto_aead.encrypt(msg, ad, npub, key) + + print_bstr(output, 'CT', ct) + + msg2 = crypto_aead.decrypt(ct, ad, npub, key) + + if msg != msg2: + raise DecryptionError() + + print(file=output) + + +if __name__ == '__main__': + generate_test_vectors() |
