-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutil.py
109 lines (87 loc) · 2.75 KB
/
util.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
#!/usr/bin/python2
#
# Utilities for BLS signatures Sage reference impl
import binascii
import getopt
import struct
import sys
if sys.version_info[0] == 3:
as_bytes = lambda x: bytes(x, "utf-8") if isinstance(x, str) else x
else:
as_bytes = lambda x: x
DEBUG = False
GENVEC = False
def is_debug():
return DEBUG
def is_genvec():
return GENVEC
def enable_debug():
global DEBUG # pylint: disable=global-statement
DEBUG = True
def enable_genvec():
global GENVEC # pylint: disable=global-statement
GENVEC = True
def print_iv(iv, name, fn, indent=8):
if not DEBUG:
return
sys.stdout.write("[%s() intermediate value] %s =\n" % (fn, name))
if iv is not None:
print_value(iv, indent, False)
def print_value(iv, indent=8, skip_first=False):
max_line_length = 111
if isinstance(iv, str):
cs = struct.unpack("=" + "B" * len(iv), as_bytes(iv))
elif isinstance(iv, (list, bytes)):
cs = iv
else:
cs = [iv]
line_length = indent
indent_string = " " * indent
if not skip_first:
sys.stdout.write(indent_string)
for c in cs:
if isinstance(c, bytes):
c = ord(c)
out_str = "0x%02x" % c
if line_length + len(out_str) > max_line_length:
sys.stdout.write("\n%s" % indent_string)
line_length = indent
sys.stdout.write(out_str + " ")
line_length += len(out_str) + 1
sys.stdout.write("\n")
def get_cmdline_options():
sk = "11223344556677889900112233445566"
msg_dflt = "the message to be signed"
ret = []
sig_type = "NUL"
# go through the commandline arguments
try:
(opts, args) = getopt.gnu_getopt(sys.argv[1:], "k:dgT:BAP")
except getopt.GetoptError as err:
print("Usage: %s [-B | -A | -P] [-g] [-d] [-k key] [-T test_file] [msg ...]" % sys.argv[0])
sys.exit(str(err))
for (opt, arg) in opts:
if opt == "-k":
sk = arg
elif opt == "-d":
enable_debug()
elif opt == "-g":
enable_genvec()
elif opt == "-T":
with open(arg, "r") as test_file:
ret += [ tuple( binascii.unhexlify(val) \
for val in line.strip().split(' ') ) \
for line in test_file.readlines() ]
elif opt == "-B":
sig_type = "NUL"
elif opt == "-A":
sig_type = "AUG"
elif opt == "-P":
sig_type = "POP"
else:
raise RuntimeError("got unexpected option %s from getopt" % opt)
# build up return value: (msg, sk) tuples from cmdline and any test files
ret += [ (arg, sk) for arg in args ]
if not ret:
ret = [ (msg_dflt, sk) ]
return (sig_type, ret)