-
Notifications
You must be signed in to change notification settings - Fork 4
/
cavs.py
212 lines (164 loc) · 6.31 KB
/
cavs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import itertools
import os, os.path
import re
import unittest
import drbg
def readlines (input_file):
with open(input_file) as fp:
for lineno, line in enumerate(fp):
yield lineno, line.strip('\r\n')
def case_generator (lines, suffix=''):
case_info = None
case = None
call = None
for lineno, line in lines:
if line == '':
continue
matches = re.match(r'^\[([^=]+)(?: =(.*))?\]$', line)
if matches:
key, value = matches.groups()
if value is None:
case_info = {'alg': '{}{}'.format(key.strip(), suffix)}
else:
try:
case_info[key.strip()] = int(value.strip())
except:
case_info[key.strip()] = value.strip()
continue
matches = re.match('^COUNT = (\d+)$', line)
if matches:
if case:
case['calls'] = case['calls'][:-1]
yield case
call = {}
case = dict({
'count': int(matches.group(1)),
'line': lineno,
'calls': [{}]
}, **case_info)
continue
if line.startswith('**'):
case['calls'][-1]['call'] = line[3:-1]
case['calls'].append({})
matches = re.match('^(\s*[^ ]+)\s+=(?: (.*))?$', line)
if matches:
key, value = matches.groups()
idx = -2 if re.match('^\s+', key) else -1
case['calls'][idx][key.strip()] = value.strip()
def find_cases (input_file, suffix):
lines = readlines(input_file)
data = {}
algs = None
# Scan for options string
for lineno, line in lines:
matches = re.match('# ([^ ]+) options: (.*)$', line)
if matches:
mode, algs = matches.groups()
data['mode'] = mode
algs = [_.strip() for _ in algs.split('::')]
data['algs'] = algs
break
if algs is None:
raise RuntimeError('No options string found.')
data['cases'] = case_generator(lines, suffix)
return data
class DRBGTestCase ():
def fromhex (self, b):
return bytes(bytearray.fromhex(b))
def compare_state (self, D, call):
try:
if isinstance(D, (drbg.CTRDRBG, drbg.HMACDRBG)):
return self.compare_ctr_state(D, call)
elif isinstance(D, drbg.HashDRBG):
return self.compare_hash_state(D, call)
except:
import pprint
pprint.pprint(self.case)
pprint.pprint(call)
print('')
raise
def compare_ctr_state (self, D, call):
Key = getattr(D, '_{}__key'.format(type(D).__name__))
V = getattr(D, '_{}__V'.format(type(D).__name__))
self.assertEqual(Key, self.fromhex(call['Key']))
self.assertEqual(V, self.fromhex(call['V']))
def compare_hash_state (self, D, call):
C = getattr(D, '_{}__C'.format(type(D).__name__))
V = getattr(D, '_{}__V'.format(type(D).__name__))
self.assertEqual(C, self.fromhex(call['C']))
self.assertEqual(V, self.fromhex(call['V']))
def test_drbg (self):
alg = {
'3KeyTDEA no df': 'tdea',
'AES-128 no df': 'aes128',
'AES-192 no df': 'aes192',
'AES-256 no df': 'aes256',
'SHA-1': 'sha1',
'SHA-224': 'sha224',
'SHA-256': 'sha256',
'SHA-384': 'sha384',
'SHA-512': 'sha512',
'SHA-1hmac': 'sha1hmac',
'SHA-224hmac': 'sha224hmac',
'SHA-256hmac': 'sha256hmac',
'SHA-384hmac': 'sha384hmac',
'SHA-512hmac': 'sha512hmac',
}[self.case['alg']]
D = None
for call in self.case['calls']:
self.assertTrue(D or call['call'] == 'INSTANTIATE')
if call['call'] == 'INSTANTIATE':
entropy = self.fromhex(call['EntropyInput'])
data = None
if call['PersonalizationString'] != '':
data = self.fromhex(call['PersonalizationString'])
if alg.startswith('aes') or alg == 'tdea':
D = drbg.CTRDRBG(alg, entropy, data)
else:
nonce = self.fromhex(call['Nonce'])
if alg.endswith('hmac'):
D = drbg.HMACDRBG(alg, entropy, nonce, data)
else:
D = drbg.HashDRBG(alg, entropy, nonce, data)
self.compare_state(D, call)
elif call['call'] == 'RESEED':
entropy = self.fromhex(call['EntropyInputReseed'])
data = None
if call['AdditionalInputReseed'] != '':
data = self.fromhex(call['AdditionalInputReseed'])
D.reseed(entropy, data)
self.compare_state(D, call)
elif call['call'].startswith('GENERATE'):
data = None
if call['AdditionalInput'] != '':
data = self.fromhex(call['AdditionalInput'])
out = D.generate(int(self.case['ReturnedBitsLen']) // 8, data)
self.compare_state(D, call)
if call.get('ReturnedBits', '') != '':
rbits = self.fromhex(call['ReturnedBits'])
self.assertEqual(out, rbits)
def generate_test_cases (mechs):
cases = {}
for subtype in ['no_reseed', 'pr_false']:
path = os.path.join('nist', 'drbgvectors_{}'.format(subtype))
iters = []
for mech, suffix in mechs:
filename = os.path.join(path, '{}_DRBG.txt'.format(mech))
it = find_cases(filename, suffix)['cases']
iters.append(it)
for case in itertools.chain(*iters):
if not re.match('^(3KeyTDEA|(AES|SHA)-\d+)( no df|hmac)?$', case['alg']):
continue
alg = re.sub('[-\s]', '_', case['alg'])
name = '{}_{}_{}'.format(alg, subtype, case['line'])
cls = type(name, (unittest.TestCase, DRBGTestCase), {'case': case})
cases[name] = cls
return cases
if __name__ == '__main__':
mechs = [
('Hash', ''),
('CTR', ''),
('HMAC', 'hmac'),
]
globals().update(generate_test_cases(mechs))
unittest.main()