blob: 60e7caabbb3e67af4424ab10a552b79b6586cd38 [file] [log] [blame]
Donald Stufft9e1a48b2013-08-09 00:32:30 -04001import os.path
2
3
4def load_nist_vectors(vector_data, op, fields):
5 section, count, data = None, None, {}
6
7 for line in vector_data:
8 line = line.strip()
9
10 # Blank lines are ignored
11 if not line:
12 continue
13
14 # Lines starting with # are comments
15 if line.startswith("#"):
16 continue
17
18 # Look for section headers
19 if line.startswith("[") and line.endswith("]"):
20 section = line[1:-1]
21 data[section] = {}
22 continue
23
24 # Build our data using a simple Key = Value format
25 name, value = line.split(" = ")
26
27 # COUNT is a special token that indicates a new block of data
28 if name.upper() == "COUNT":
29 count = value
30 data[section][count] = {}
31 # For all other tokens we simply want the name, value stored in
32 # the dictionary
33 else:
34 data[section][count][name.lower()] = value
35
36 # We want to test only for a particular operation
37 return [
Alex Gaynorde9a3ee2013-08-09 10:42:44 -070038 tuple(vector[1][f].encode("ascii") for f in fields)
Donald Stufft9e1a48b2013-08-09 00:32:30 -040039 for vector in sorted(data[op].items(), key=lambda v: v[0])
40 ]
41
42
43def load_nist_vectors_from_file(filename, op, fields):
44 base = os.path.join(
45 os.path.dirname(__file__), "primitives", "vectors", "NIST",
46 )
47 with open(os.path.join(base, filename), "r") as vector_file:
48 return load_nist_vectors(vector_file, op, fields)