forked from tropicsquare/ts-spect-fw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sha512.py
executable file
·71 lines (52 loc) · 1.98 KB
/
test_sha512.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
#!/usr/bin/env python3
import sys
import binascii
import hashlib
import random as rn
import os
import test_common as tc
def sha512(s):
return hashlib.sha512(s).digest()
if __name__ == "__main__":
args = tc.parser.parse_args()
seed = tc.set_seed(args)
rn.seed(seed)
print("seed:", seed)
ops_cfg = tc.get_ops_config()
test_name = "sha512"
test_dir = tc.make_test_dir(test_name)
cmd_file = tc.get_cmd_file(test_dir)
msg_bitlen = rn.randint(2*128, 5*128)*8
message = int.to_bytes(rn.getrandbits(msg_bitlen), msg_bitlen//8, 'big')
digest_ref = sha512(message)
digest_ref_int = int.from_bytes(digest_ref, 'little')
message_padded = message + b'\x80'
while len(message_padded) % 128 != (128 - 16):
message_padded += b'\x00'
message_padded += int.to_bytes(len(message)*8, 16, 'big')
m_blocks = []
for i in range(0, len(message_padded), 128):
m_blocks.append(message_padded[i:i+128])
tc.print_run_name("sha512_init")
tc.start(cmd_file)
ctx = tc.run_op(cmd_file, "sha512_init", 0x0, 0x1, 0, ops_cfg, test_dir)
for i in range(len(m_blocks)-1):
tc.print_run_name(f"sha512_update_{i}")
cmd_file = tc.get_cmd_file(test_dir)
tc.start(cmd_file)
tc.write_bytes(cmd_file, m_blocks[i], 0x0010)
run_name = "sha512_update" + f"_{i}"
ctx = tc.run_op(cmd_file, "sha512_update", 0x0, 0x1, 128, ops_cfg, test_dir, run_name=run_name, old_context=ctx)
tc.print_run_name("sha512_final")
cmd_file = tc.get_cmd_file(test_dir)
tc.start(cmd_file)
tc.write_bytes(cmd_file, m_blocks[-1], 0x0010)
ctx = tc.run_op(cmd_file, "sha512_final", 0x0, 0x1, 128, ops_cfg, test_dir, old_context=ctx)
digest = tc.read_output(test_dir, "sha512_final", 0x1010, 16)
if digest != digest_ref_int:
tc.print_failed()
sys.exit(0)
tc.print_passed()
if "TS_SPECT_FW_TEST_DONT_DUMP" in os.environ.keys():
os.system(f"rm -r {test_dir}")
sys.exit(0)