Skip to content

Commit a62fa3f

Browse files
committed
[test] Add hmac sca scripts
Add the host and test scripts from hmac sca. Signed-off-by: Siemen Dhooghe <sdhooghe@google.com>
1 parent 54afccc commit a62fa3f

4 files changed

Lines changed: 417 additions & 29 deletions

File tree

communication/sca_hmac_commands.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,6 @@ def init(self) -> list:
4848
version = self.target.read_response()
4949
return device_id, owner_page, boot_log, boot_measurements, version
5050

51-
def test(self, key: list[int], num_segments: int):
52-
# HmacSca command.
53-
self._ujson_hmac_sca_cmd()
54-
# Single command.
55-
self.target.write(json.dumps("Test").encode("ascii"))
56-
# Key payload.
57-
time.sleep(0.01)
58-
key_data = {"key": key}
59-
self.target.write(json.dumps(key_data).encode("ascii"))
60-
# Number of iterations payload.
61-
time.sleep(0.05)
62-
num_it_data = {"num_iterations": num_segments}
63-
self.target.write(json.dumps(num_it_data).encode("ascii"))
64-
6551
def single(self, msg: list[int], key: list[int], trigger: int):
6652
""" Start a single HMAC operation using the given message and key.
6753
Args:
@@ -214,21 +200,6 @@ def read_response(self, max_tries = 1) -> str:
214200
return read_line.split("RESP_OK:")[1].split(" CRC:")[0]
215201
it += 1
216202
return ""
217-
218-
def crypto_sha2(self, msg, calculation_trigger) -> None:
219-
# HmacSca command.
220-
self._ujson_hmac_sca_cmd()
221-
# Sha2 command.
222-
time.sleep(0.01)
223-
self.target.write(json.dumps("Sha2").encode("ascii"))
224-
time.sleep(0.01)
225-
if calculation_trigger:
226-
mode = {"message": msg, "update_trigger": False,
227-
"final_trigger": True}
228-
else:
229-
mode = {"message": msg, "update_trigger": True,
230-
"final_trigger": False}
231-
self.target.write(json.dumps(mode).encode("ascii"))
232203

233204
def read_tag(self):
234205
""" Read tag from OpenTitan HMAC.

test/penetrationtests/sca/BUILD

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,46 @@ py_binary(
8282
}),
8383
)
8484

85+
py_binary(
86+
name = "sca_hmac_test",
87+
srcs = ["test_scripts/sca_hmac_test.py"],
88+
testonly = True,
89+
deps = [
90+
":sca_hmac_functions",
91+
"//communication:dut",
92+
"//communication:chip",
93+
"//communication:sca_hmac_commands",
94+
"//test/penetrationtests/util:utils",
95+
"@rules_python//python/runfiles",
96+
],
97+
data = [
98+
"@lowrisc_opentitan//sw/host/opentitantool",
99+
] +
100+
select({
101+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_silicon_owner_gb_rom_ext": [
102+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_silicon_owner_gb_rom_ext",
103+
],
104+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_fpga_cw310_rom_with_fake_keys": [
105+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_fpga_cw310_rom_with_fake_keys",
106+
],
107+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_fpga_cw310_sival_rom_ext": [
108+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_fpga_cw310_sival_rom_ext",
109+
],
110+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_fpga_cw310_test_rom": [
111+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_fpga_cw310_test_rom",
112+
],
113+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_silicon_owner_a2_rom_ext": [
114+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_silicon_owner_a2_rom_ext",
115+
],
116+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_silicon_owner_sival_rom_ext": [
117+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_silicon_owner_sival_rom_ext",
118+
],
119+
"//conditions:default": [
120+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_silicon_owner_gb_rom_ext",
121+
],
122+
}),
123+
)
124+
85125
py_library(
86126
name = "sca_ibex_functions",
87127
srcs = ["host_scripts/sca_ibex_functions.py"],
@@ -105,4 +145,17 @@ py_library(
105145
"//communication:data_generator",
106146
requirement("pycryptodome"),
107147
],
148+
)
149+
150+
py_library(
151+
name = "sca_hmac_functions",
152+
srcs = ["host_scripts/sca_hmac_functions.py"],
153+
deps = [
154+
"//communication:dut",
155+
"//communication:chip",
156+
"//communication:sca_hmac_commands",
157+
"//communication:sca_prng_commands",
158+
"//communication:sca_trigger_commands",
159+
requirement("pycryptodome"),
160+
],
108161
)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from communication.sca_hmac_commands import OTHMAC
2+
from communication.sca_prng_commands import OTPRNG
3+
from communication.sca_trigger_commands import OTTRIGGER
4+
from communication.chip import *
5+
from communication.dut import DUT
6+
import time
7+
8+
def char_hmac_single(opentitantool, iterations, trigger, key, text):
9+
target = DUT()
10+
reset_target(opentitantool)
11+
# Clear the output from the reset
12+
target.dump_all()
13+
14+
hmacsca = OTHMAC(target, "ujson")
15+
# Initialize our chip and catch its output
16+
device_id, owner_page, boot_log, boot_measurements, version = hmacsca.init()
17+
18+
# Set the trigger
19+
triggersca = OTTRIGGER(target, "ujson")
20+
triggersca.select_trigger(0)
21+
22+
for _ in range(iterations):
23+
hmacsca.single(text, key, trigger)
24+
response = target.read_response()
25+
return response
26+
27+
def char_hmac_daisy_chain(opentitantool, iterations, num_segments, trigger, key, text):
28+
target = DUT()
29+
reset_target(opentitantool)
30+
# Clear the output from the reset
31+
target.dump_all()
32+
33+
hmacsca = OTHMAC(target, "ujson")
34+
# Initialize our chip and catch its output
35+
device_id, owner_page, boot_log, boot_measurements, version = hmacsca.init()
36+
37+
# Set the trigger
38+
triggersca = OTTRIGGER(target, "ujson")
39+
triggersca.select_trigger(0)
40+
41+
for _ in range(iterations):
42+
hmacsca.daisy_chain(text, key, num_segments, trigger)
43+
response = target.read_response()
44+
return response
45+
46+
def char_hmac_random_batch(opentitantool, iterations, num_segments, trigger):
47+
target = DUT()
48+
reset_target(opentitantool)
49+
# Clear the output from the reset
50+
target.dump_all()
51+
52+
hmacsca = OTHMAC(target, "ujson")
53+
# Initialize our chip and catch its output
54+
device_id, owner_page, boot_log, boot_measurements, version = hmacsca.init()
55+
56+
# Set the trigger
57+
triggersca = OTTRIGGER(target, "ujson")
58+
triggersca.select_trigger(0)
59+
60+
# Set the internal prng
61+
ot_prng = OTPRNG(target=target, protocol="ujson")
62+
ot_prng.seed_prng([0,0,0,0])
63+
64+
for _ in range(iterations):
65+
hmacsca.random_batch(num_segments, trigger)
66+
response = target.read_response()
67+
return response
68+
69+
def char_hmac_fvsr_batch(opentitantool, iterations, num_segments, trigger, key):
70+
target = DUT()
71+
reset_target(opentitantool)
72+
# Clear the output from the reset
73+
target.dump_all()
74+
75+
hmacsca = OTHMAC(target, "ujson")
76+
# Initialize our chip and catch its output
77+
device_id, owner_page, boot_log, boot_measurements, version = hmacsca.init()
78+
79+
# Set the trigger
80+
triggersca = OTTRIGGER(target, "ujson")
81+
triggersca.select_trigger(0)
82+
83+
# Set the internal prng
84+
ot_prng = OTPRNG(target=target, protocol="ujson")
85+
ot_prng.seed_prng([0,0,0,0])
86+
87+
for _ in range(iterations):
88+
hmacsca.fvsr_batch(key, num_segments, trigger)
89+
response = target.read_response()
90+
return response

0 commit comments

Comments
 (0)