-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathparse_salsa20_tv.py
More file actions
58 lines (53 loc) · 1.71 KB
/
parse_salsa20_tv.py
File metadata and controls
58 lines (53 loc) · 1.71 KB
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
# Copyright 2018 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
import re
import paths
import parsetv
class ParseSalsa20(parsetv.ParseTv):
def _finish_tvkv(self):
if self._tvvalue:
m = re.fullmatch(r"stream\[(\d+)\.\.(\d+)\]", self._tvkey)
if m:
self._tvstreamlist.append({
"first": int(m.group(1)),
"last": int(m.group(2)),
"value": self._tvvalue,
})
else:
self._tvdict[self._tvkey] = self._tvvalue
self._tvkey = None
self._tvvalue = None
def _handle_line(self, l):
l = l.strip()
if len(l) == 0:
self._finish_tv()
elif l[0] in "*=(":
self._finish_tv()
elif l.startswith("Test vectors -- set"):
self._start_tvset({
"setintro": l,
})
elif l.startswith("End of test vectors"):
self._finish_setlist()
elif l[-1] == ":":
self._start_tv({
"intro": l[:-1],
})
elif ":" in l:
k, v = l.split(":", 1)
self._globalparams[k.strip()] = v.strip()
elif "=" in l:
vkey, vval = l.split("=", 1)
self._start_tvkv(vkey.strip())
self._vappend(vval.strip())
elif re.fullmatch(r"[0-9A-F]+", l):
self._vappend(l)
else:
raise Exception("Can't parse: " + repr(l))
def test_vectors():
p = ParseSalsa20()
p.parse_file(paths.top / "test_vectors" / "other" / "salsa20.txt")
return p.get()