From f7fb850ecf267b39459c334aabe119b5f177ee8d Mon Sep 17 00:00:00 2001 From: "Matthias J. Kannwischer" Date: Fri, 18 Jan 2019 14:43:05 +0100 Subject: [PATCH] check testvectors --- crypto_kem/kyber768/META.yml | 1 + crypto_sign/dilithium-iii/META.yml | 1 + requirements.txt | 1 + test/check_testvectors.py | 38 ++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 requirements.txt create mode 100755 test/check_testvectors.py diff --git a/crypto_kem/kyber768/META.yml b/crypto_kem/kyber768/META.yml index 6169fd72..4b3cafed 100644 --- a/crypto_kem/kyber768/META.yml +++ b/crypto_kem/kyber768/META.yml @@ -4,6 +4,7 @@ version: https://github.com/pq-crystals/kyber/commit/ab996e7460e5356b0e23aa034e7 claimed-nist-level: 3 length-public-key: 1088 length-ciphertext: 1152 +testvectors-sha3-256: 0xF3D335A7C393083F11F2D9EF4D7312ADB5683139473EA9BDF0091618476BA412 principal-submitter: Peter Schwabe auxiliary-submitters: - Roberto Avanzi diff --git a/crypto_sign/dilithium-iii/META.yml b/crypto_sign/dilithium-iii/META.yml index 498af051..384f54e4 100644 --- a/crypto_sign/dilithium-iii/META.yml +++ b/crypto_sign/dilithium-iii/META.yml @@ -4,6 +4,7 @@ version: https://github.com/pq-crystals/dilithium/commit/ffa89bdbc12a8ee178ccec4 claimed-nist-level: 3 length-public-key: 1472 length-signature: 2701 +testvectors-sha3-256: 0xCE28BBD23C2AC42C7F9A17A4FA463DC9F8ACBF767397AD4ECDE136A92D4D6C3E principal-submitter: Vadim Lyubashevsky auxiliary-submitters: - Léo Ducas diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..4a285555 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +PyYAML==3.13 diff --git a/test/check_testvectors.py b/test/check_testvectors.py new file mode 100755 index 00000000..ee11dcc3 --- /dev/null +++ b/test/check_testvectors.py @@ -0,0 +1,38 @@ +import yaml +import sys +import os +import subprocess +import hashlib + +""" +For a given SCHEME, this script verifies that the hash of the testvectors +produced on this platform matches the one provided in the META file. +""" + +if len(sys.argv) != 2: + print("Provide a scheme name (e.g. crypto_kem/kyber768) as argv[1]") + exit(1) + +SCHEME = sys.argv[1] +SCHEMEFULL = SCHEME.replace('/', '_') # e.g. crypto_kem_kyber768 +SCHEMESHORT = SCHEME.split('/')[1].upper() + +def get_hash(scheme): + with open(f"{scheme}/META.yml", 'r') as stream: + meta = yaml.load(stream) + return hex(meta['testvectors-sha3-256']).replace('0x', '') + + +expectedTestvectorsHash = get_hash(SCHEME) + +subprocess.run(["make", "testvectors", f"SCHEME={SCHEME}"]) +implementations = [x for x in os.listdir('bin') if 'testvectors' in x and SCHEMEFULL in x] + +for impl in implementations: + testvectors = subprocess.run([f"bin/{impl}"],stdout=subprocess.PIPE) + testvectorsHash = hashlib.sha3_256(testvectors.stdout).hexdigest() + if testvectorsHash.lower() != expectedTestvectorsHash.lower(): + print(f"testvectors of {SCHEME} should be {expectedTestvectorsHash}, but is {testvectorsHash}") + sys.exit(1) + +