Last active
January 26, 2020 21:59
-
-
Save elazarl/41865ca98e5dc560d98566dfe2331b59 to your computer and use it in GitHub Desktop.
NIST test vector to C format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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