Skip to content

Instantly share code, notes, and snippets.

@elazarl
Last active Jan 26, 2020
Embed
What would you like to do?
NIST test vector to C format
# This converts test vectors from
# https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/CAVP-TESTING-BLOCK-CIPHER-MODES
# to a C-struct format.
# For example to initialize
# struct test_case {
# int count;
# int data_unit_len;
# std::string key;
# std::string index;
# std::string plaintext;
# std::string ciphertext;
# };
# struct test_case cases[] = {
# <...output of the script on .rst files...>
# };
import sys
field_num = 0
elt = {
'COUNT': 0,
'DataUnitLen': 0,
'Key': "",
"i" : "",
"PT" : "",
"CT" : "",
}
def print_elt(m):
print(''' {{
{COUNT},
{DataUnitLen},
"{Key}",
"{i}",
"{PT}",
"{CT}",
}},'''.format(**elt))
for line in sys.stdin.readlines():
if not line.strip() or line.startswith('#') or line.startswith('['):
field_num = 0
else:
line = line.strip()
if field_num > 5:
raise Exception('illegal input:' + line)
if not ' = ' in line:
raise Exception('weird: ' + line)
prefix, line = line.split(' = ')
if elt.get(prefix) is None:
raise Exception('illegal prefix %s in line: %s' % (repr(prefix), repr(line)))
elt[prefix] = line
if field_num == 5:
print_elt(elt)
field_num += 1
# mark final element as -1
for k in elt.keys():
elt[k] = ""
elt['COUNT'] = -1
elt['DataUnitLen'] = -1
print_elt(elt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment