2019-01-18 13:43:05 +00:00
|
|
|
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()
|
|
|
|
|
2019-01-22 14:21:49 +00:00
|
|
|
|
2019-01-18 13:43:05 +00:00
|
|
|
def get_hash(scheme):
|
2019-01-22 14:21:49 +00:00
|
|
|
with open("{}/META.yml".format(scheme), 'r') as stream:
|
2019-01-18 13:45:14 +00:00
|
|
|
meta = yaml.load(stream)
|
|
|
|
return hex(meta['testvectors-sha3-256']).replace('0x', '')
|
2019-01-18 13:43:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
expectedTestvectorsHash = get_hash(SCHEME)
|
|
|
|
|
2019-01-22 14:21:49 +00:00
|
|
|
subprocess.run(["make", "testvectors", "SCHEME={}".format(SCHEME)])
|
|
|
|
implementations = [
|
|
|
|
x for x in os.listdir('bin') if 'testvectors' in x and SCHEMEFULL in x]
|
2019-01-18 13:43:05 +00:00
|
|
|
|
|
|
|
for impl in implementations:
|
2019-01-22 14:21:49 +00:00
|
|
|
testvectors = subprocess.run(["bin/{}".format(impl)],
|
|
|
|
stdout=subprocess.PIPE)
|
2019-01-18 13:45:14 +00:00
|
|
|
testvectorsHash = hashlib.sha3_256(testvectors.stdout).hexdigest()
|
|
|
|
if testvectorsHash.lower() != expectedTestvectorsHash.lower():
|
2019-01-22 14:21:49 +00:00
|
|
|
print("testvectors of {} should be {}, but is {}"
|
|
|
|
.format(SCHEME, expectedTestvectorsHash, testvectorsHash))
|
2019-01-18 13:45:14 +00:00
|
|
|
sys.exit(1)
|
2019-01-21 15:05:46 +00:00
|
|
|
else:
|
2019-01-22 14:21:49 +00:00
|
|
|
print("testvectors of {} matched expected hash".format(SCHEME))
|