Skip to content

Commit f673bd3

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

8 files changed

Lines changed: 397 additions & 12 deletions

File tree

communication/sca_kmac_commands.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def absorb_batch(self, num_segments):
122122
self.target.write(json.dumps("Batch").encode("ascii"))
123123
# Num_segments payload.
124124
time.sleep(0.01)
125-
num_segments_data = {"data": [x for x in num_segments]}
125+
num_segments_data = {"num_enc": num_segments}
126126
self.target.write(json.dumps(num_segments_data).encode("ascii"))
127127

128128
def absorb_daisy_chain(self, text, key, num_segments):
@@ -137,15 +137,16 @@ def absorb_daisy_chain(self, text, key, num_segments):
137137
else:
138138
# KmacSca command.
139139
self._ujson_kmac_sca_cmd()
140-
# Batch command.
140+
# BatchDaisy command.
141141
self.target.write(json.dumps("BatchDaisy").encode("ascii"))
142142
# Num_segments payload.
143143
time.sleep(0.01)
144144
num_it_data = {"num_enc": num_segments}
145145
self.target.write(json.dumps(num_it_data).encode("ascii"))
146-
message_data = {"msg": text, "msg_length": 16}
146+
message_data = {"msg": text, "msg_length": len(text)}
147147
self.target.write(json.dumps(message_data).encode("ascii"))
148-
key_data = {"key": key, "key_length": 16}
148+
key_data = {"key": key, "key_length": len(key)}
149+
self.target.write(json.dumps(key_data).encode("ascii"))
149150

150151
def absorb(self, text, text_length: Optional[int] = 16):
151152
""" Write plaintext to OpenTitan KMAC & start absorb.

test/penetrationtests/sca/BUILD

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,47 @@ py_binary(
122122
}),
123123
)
124124

125+
py_binary(
126+
name = "sca_kmac_test",
127+
srcs = ["test_scripts/sca_kmac_test.py"],
128+
testonly = True,
129+
deps = [
130+
":sca_kmac_functions",
131+
"//communication:dut",
132+
"//communication:chip",
133+
"//communication:sca_kmac_commands",
134+
"//test/penetrationtests/util:utils",
135+
"@rules_python//python/runfiles",
136+
],
137+
data = [
138+
"@lowrisc_opentitan//sw/host/opentitantool",
139+
] +
140+
select({
141+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_silicon_owner_gb_rom_ext": [
142+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_silicon_owner_gb_rom_ext",
143+
],
144+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_fpga_cw310_rom_with_fake_keys": [
145+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_fpga_cw310_rom_with_fake_keys",
146+
],
147+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_fpga_cw310_sival_rom_ext": [
148+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_fpga_cw310_sival_rom_ext",
149+
],
150+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_fpga_cw310_test_rom": [
151+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_fpga_cw310_test_rom",
152+
],
153+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_silicon_owner_a2_rom_ext": [
154+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_silicon_owner_a2_rom_ext",
155+
],
156+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_silicon_owner_sival_rom_ext": [
157+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_silicon_owner_sival_rom_ext",
158+
],
159+
"//conditions:default": [
160+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_silicon_owner_gb_rom_ext",
161+
],
162+
}),
163+
)
164+
165+
125166
py_library(
126167
name = "sca_ibex_functions",
127168
srcs = ["host_scripts/sca_ibex_functions.py"],
@@ -158,4 +199,17 @@ py_library(
158199
"//communication:sca_trigger_commands",
159200
requirement("pycryptodome"),
160201
],
202+
)
203+
204+
py_library(
205+
name = "sca_kmac_functions",
206+
srcs = ["host_scripts/sca_kmac_functions.py"],
207+
deps = [
208+
"//communication:dut",
209+
"//communication:chip",
210+
"//communication:sca_kmac_commands",
211+
"//communication:sca_prng_commands",
212+
"//communication:sca_trigger_commands",
213+
requirement("pycryptodome"),
214+
],
161215
)

test/penetrationtests/sca/host_scripts/sca_aes_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def char_aes_batch_data_fvsr_encrypt(opentitantool, iterations, num_segments, ma
8585

8686
# Set the internal prng
8787
ot_prng = OTPRNG(target=target, protocol="ujson")
88-
ot_prng.seed_prng([0,0,0,0])
88+
ot_prng.seed_prng([1,0,0,0])
8989

9090
# Generate plaintexts and keys for first batch.
9191
aessca.start_fvsr_batch_generate(2)
@@ -118,7 +118,7 @@ def char_aes_batch_key_fvsr_encrypt(opentitantool, iterations, num_segments, mas
118118

119119
# Set the internal prng
120120
ot_prng = OTPRNG(target=target, protocol="ujson")
121-
ot_prng.seed_prng([0,0,0,0])
121+
ot_prng.seed_prng([1,0,0,0])
122122

123123
# Generate plaintexts and keys for first batch.
124124
aessca.start_fvsr_batch_generate(1)

test/penetrationtests/sca/host_scripts/sca_hmac_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def char_hmac_random_batch(opentitantool, iterations, num_segments, trigger):
5959

6060
# Set the internal prng
6161
ot_prng = OTPRNG(target=target, protocol="ujson")
62-
ot_prng.seed_prng([0,0,0,0])
62+
ot_prng.seed_prng([1,0,0,0])
6363

6464
for _ in range(iterations):
6565
hmacsca.random_batch(num_segments, trigger)
@@ -82,7 +82,7 @@ def char_hmac_fvsr_batch(opentitantool, iterations, num_segments, trigger, key):
8282

8383
# Set the internal prng
8484
ot_prng = OTPRNG(target=target, protocol="ujson")
85-
ot_prng.seed_prng([0,0,0,0])
85+
ot_prng.seed_prng([1,0,0,0])
8686

8787
for _ in range(iterations):
8888
hmacsca.fvsr_batch(key, num_segments, trigger)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from communication.sca_kmac_commands import OTKMAC
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+
import random
8+
9+
def char_kmac_single(opentitantool, iterations, masking, key, text):
10+
target = DUT()
11+
reset_target(opentitantool)
12+
# Clear the output from the reset
13+
target.dump_all()
14+
15+
kmacsca = OTKMAC(target, "ujson")
16+
# Initialize our chip and catch its output
17+
device_id, owner_page, boot_log, boot_measurements, version = kmacsca.init(0)
18+
19+
if masking:
20+
lfsr_seed = [0, 1, 2, 3]
21+
else:
22+
lfsr_seed = [0, 0, 0, 0]
23+
kmacsca.write_lfsr_seed(lfsr_seed)
24+
25+
kmacsca.write_key(key)
26+
27+
# Set the trigger
28+
triggersca = OTTRIGGER(target, "ujson")
29+
triggersca.select_trigger(0)
30+
31+
for _ in range(iterations):
32+
kmacsca.absorb(text, len(text))
33+
response = target.read_response()
34+
return response
35+
36+
def char_kmac_batch_daisy_chain(opentitantool, iterations, num_segments, masking, key, text):
37+
target = DUT()
38+
reset_target(opentitantool)
39+
# Clear the output from the reset
40+
target.dump_all()
41+
42+
kmacsca = OTKMAC(target, "ujson")
43+
# Initialize our chip and catch its output
44+
device_id, owner_page, boot_log, boot_measurements, version = kmacsca.init(0)
45+
46+
if masking:
47+
lfsr_seed = [0, 1, 2, 3]
48+
else:
49+
lfsr_seed = [0, 0, 0, 0]
50+
kmacsca.write_lfsr_seed(lfsr_seed)
51+
52+
# Set the trigger
53+
triggersca = OTTRIGGER(target, "ujson")
54+
triggersca.select_trigger(0)
55+
56+
for _ in range(iterations):
57+
kmacsca.absorb_daisy_chain(text, key, num_segments)
58+
response = target.read_response()
59+
return response
60+
61+
def char_kmac_batch(opentitantool, iterations, num_segments, masking, key, text):
62+
target = DUT()
63+
reset_target(opentitantool)
64+
# Clear the output from the reset
65+
target.dump_all()
66+
67+
kmacsca = OTKMAC(target, "ujson")
68+
# Initialize our chip and catch its output
69+
device_id, owner_page, boot_log, boot_measurements, version = kmacsca.init(0)
70+
71+
if masking:
72+
lfsr_seed = [0, 1, 2, 3]
73+
else:
74+
lfsr_seed = [0, 0, 0, 0]
75+
kmacsca.write_lfsr_seed(lfsr_seed)
76+
77+
# Set the trigger
78+
triggersca = OTTRIGGER(target, "ujson")
79+
triggersca.select_trigger(0)
80+
81+
# Set the internal prng
82+
ot_prng = OTPRNG(target=target, protocol="ujson")
83+
ot_prng.seed_prng([1,0,0,0])
84+
85+
kmacsca.fvsr_key_set(key, len(key))
86+
87+
for _ in range(iterations):
88+
kmacsca.absorb_batch(num_segments)
89+
response = target.read_response()
90+
return response

test/penetrationtests/sca/test_scripts/sca_aes_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def char_aes_batch_data_fvsr_encrypt_test(opentitantool_path, iterations, num_se
175175
return False
176176

177177
# Set the syncrhonized randomness
178-
batch_prng_seed = 0
178+
batch_prng_seed = 1
179179
random.seed(batch_prng_seed)
180180

181181
# Start with a fixed measurement
@@ -216,7 +216,7 @@ def char_aes_batch_key_fvsr_encrypt_test(opentitantool_path, iterations, num_seg
216216
return False
217217

218218
# Set the syncrhonized randomness
219-
batch_prng_seed = 0
219+
batch_prng_seed = 1
220220
random.seed(batch_prng_seed)
221221

222222
# Start with a fixed measurement

test/penetrationtests/sca/test_scripts/sca_hmac_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def char_hmac_random_batch_test(opentitantool_path, iterations, num_segments):
173173
return False
174174

175175
# Set the syncrhonized randomness
176-
batch_prng_seed = 0
176+
batch_prng_seed = 1
177177
random.seed(batch_prng_seed)
178178

179179
# Generate the batch data
@@ -209,7 +209,7 @@ def char_hmac_fvsr_batch_test(opentitantool_path, iterations, num_segments):
209209
return False
210210

211211
# Set the syncrhonized randomness
212-
batch_prng_seed = 0
212+
batch_prng_seed = 1
213213
random.seed(batch_prng_seed)
214214

215215
# Generate the batch data

0 commit comments

Comments
 (0)