Skip to content

Commit 54afccc

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

5 files changed

Lines changed: 474 additions & 8 deletions

File tree

communication/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,5 @@ py_library(
8686
py_library(
8787
name = "data_generator",
8888
srcs = ["data_generator.py"],
89+
deps = [requirement("pycryptodome"),],
8990
)

communication/sca_aes_commands.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ def start_fvsr_batch_generate(self, command):
121121
self.target.write(json.dumps("FvsrKeyStartBatchGenerate").encode("ascii"))
122122
# Command.
123123
time.sleep(0.01)
124-
cmd = {"data": [command]}
124+
cmd = {"cmd": command}
125125
self.target.write(json.dumps(cmd).encode("ascii"))
126126

127127
def write_fvsr_batch_generate(self, num_segments):
128-
""" Generate random plaintexts for FVSR.
128+
""" Generate random keys for FVSR.
129129
Args:
130130
num_segments: Number of encryptions to perform.
131131
"""
@@ -138,7 +138,7 @@ def write_fvsr_batch_generate(self, num_segments):
138138
self.target.write(json.dumps("FvsrKeyBatchGenerate").encode("ascii"))
139139
# Number of encryptions.
140140
time.sleep(0.01)
141-
num_encryption_data = {"data": [x for x in num_segments]}
141+
num_encryption_data = {"num_enc": num_segments}
142142
self.target.write(json.dumps(num_encryption_data).encode("ascii"))
143143

144144
def batch_alternative_encrypt(self, num_segments):
@@ -152,11 +152,11 @@ def batch_alternative_encrypt(self, num_segments):
152152
else:
153153
# AesSca command.
154154
self._ujson_aes_sca_cmd()
155-
# BatchEncrypt command.
155+
# BatchAlternativeEncrypt command.
156156
self.target.write(json.dumps("BatchAlternativeEncrypt").encode("ascii"))
157157
# Number of encryptions.
158158
time.sleep(0.01)
159-
num_encryption_data = {"data": [x for x in num_segments]}
159+
num_encryption_data = {"num_enc": num_segments}
160160
self.target.write(json.dumps(num_encryption_data).encode("ascii"))
161161

162162
def batch_encrypt(self, num_segments):
@@ -174,7 +174,7 @@ def batch_encrypt(self, num_segments):
174174
self.target.write(json.dumps("BatchEncrypt").encode("ascii"))
175175
# Number of encryptions.
176176
time.sleep(0.01)
177-
num_encryption_data = {"data": [x for x in num_segments]}
177+
num_encryption_data = {"num_enc": num_segments}
178178
self.target.write(json.dumps(num_encryption_data).encode("ascii"))
179179

180180
def fvsr_key_batch_encrypt(self, num_segments):
@@ -192,7 +192,7 @@ def fvsr_key_batch_encrypt(self, num_segments):
192192
self.target.write(json.dumps("FvsrKeyBatchEncrypt").encode("ascii"))
193193
# Number of encryptions.
194194
time.sleep(0.01)
195-
num_encryption_data = {"data": [x for x in num_segments]}
195+
num_encryption_data = {"num_enc": num_segments}
196196
self.target.write(json.dumps(num_encryption_data).encode("ascii"))
197197

198198
def fvsr_data_batch_encrypt(self, num_segments):
@@ -210,7 +210,7 @@ def fvsr_data_batch_encrypt(self, num_segments):
210210
self.target.write(json.dumps("FvsrDataBatchEncrypt").encode("ascii"))
211211
# Number of encryptions.
212212
time.sleep(0.01)
213-
num_encryption_data = {"data": [x for x in num_segments]}
213+
num_encryption_data = {"num_enc": num_segments}
214214
self.target.write(json.dumps(num_encryption_data).encode("ascii"))
215215

216216
def batch_plaintext_set(self, text, text_length = 16):

test/penetrationtests/sca/BUILD

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package(default_visibility = ["//visibility:public"])
22

3+
load("@//third_party/python:requirements.bzl", "requirement")
4+
35
py_binary(
46
name = "sca_ibex_test",
57
srcs = ["test_scripts/sca_ibex_test.py"],
@@ -40,6 +42,46 @@ py_binary(
4042
}),
4143
)
4244

45+
py_binary(
46+
name = "sca_aes_test",
47+
srcs = ["test_scripts/sca_aes_test.py"],
48+
testonly = True,
49+
deps = [
50+
":sca_aes_functions",
51+
"//communication:dut",
52+
"//communication:chip",
53+
"//communication:sca_aes_commands",
54+
"//test/penetrationtests/util:utils",
55+
"@rules_python//python/runfiles",
56+
],
57+
data = [
58+
"@lowrisc_opentitan//sw/host/opentitantool",
59+
] +
60+
select({
61+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_silicon_owner_gb_rom_ext": [
62+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_silicon_owner_gb_rom_ext",
63+
],
64+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_fpga_cw310_rom_with_fake_keys": [
65+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_fpga_cw310_rom_with_fake_keys",
66+
],
67+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_fpga_cw310_sival_rom_ext": [
68+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_fpga_cw310_sival_rom_ext",
69+
],
70+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_fpga_cw310_test_rom": [
71+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_fpga_cw310_test_rom",
72+
],
73+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_silicon_owner_a2_rom_ext": [
74+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_silicon_owner_a2_rom_ext",
75+
],
76+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/config:env_silicon_owner_sival_rom_ext": [
77+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_silicon_owner_sival_rom_ext",
78+
],
79+
"//conditions:default": [
80+
"@lowrisc_opentitan//sw/device/tests/penetrationtests/firmware:pen_test_sca_silicon_owner_gb_rom_ext",
81+
],
82+
}),
83+
)
84+
4385
py_library(
4486
name = "sca_ibex_functions",
4587
srcs = ["host_scripts/sca_ibex_functions.py"],
@@ -49,4 +91,18 @@ py_library(
4991
"//communication:sca_ibex_commands",
5092
"//communication:sca_prng_commands",
5193
],
94+
)
95+
96+
py_library(
97+
name = "sca_aes_functions",
98+
srcs = ["host_scripts/sca_aes_functions.py"],
99+
deps = [
100+
"//communication:dut",
101+
"//communication:chip",
102+
"//communication:sca_aes_commands",
103+
"//communication:sca_prng_commands",
104+
"//communication:sca_trigger_commands",
105+
"//communication:data_generator",
106+
requirement("pycryptodome"),
107+
],
52108
)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
from communication.sca_aes_commands import OTAES
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_aes_single_encrypt(opentitantool, iterations, masking, key, text):
9+
target = DUT()
10+
reset_target(opentitantool)
11+
# Clear the output from the reset
12+
target.dump_all()
13+
14+
aessca = OTAES(target, "ujson")
15+
# Initialize our chip and catch its output
16+
device_id, owner_page, boot_log, boot_measurements, version = aessca.init(0)
17+
18+
# Check whether we enable the masking
19+
if masking:
20+
lfsr_seed = 1
21+
else:
22+
lfsr_seed = 0
23+
aessca.seed_lfsr(lfsr_seed.to_bytes(4, "little"))
24+
25+
# Set the trigger
26+
triggersca = OTTRIGGER(target, "ujson")
27+
triggersca.select_trigger(0)
28+
29+
aessca.key_set(key)
30+
31+
for _ in range(iterations):
32+
aessca.single_encrypt(text)
33+
response = target.read_response()
34+
return response
35+
36+
def char_aes_batch_alternative_encrypt(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+
aessca = OTAES(target, "ujson")
43+
# Initialize our chip and catch its output
44+
device_id, owner_page, boot_log, boot_measurements, version = aessca.init(0)
45+
46+
# Check whether we enable the masking
47+
if masking:
48+
lfsr_seed = 1
49+
else:
50+
lfsr_seed = 0
51+
aessca.seed_lfsr(lfsr_seed.to_bytes(4, "little"))
52+
53+
# Set the trigger
54+
triggersca = OTTRIGGER(target, "ujson")
55+
triggersca.select_trigger(0)
56+
57+
aessca.key_set(key, len(key))
58+
aessca.batch_plaintext_set(text, len(text))
59+
60+
for _ in range(iterations):
61+
aessca.batch_alternative_encrypt(num_segments)
62+
response = target.read_response()
63+
return response
64+
65+
def char_aes_batch_data_fvsr_encrypt(opentitantool, iterations, num_segments, masking):
66+
target = DUT()
67+
reset_target(opentitantool)
68+
# Clear the output from the reset
69+
target.dump_all()
70+
71+
aessca = OTAES(target, "ujson")
72+
# Initialize our chip and catch its output
73+
device_id, owner_page, boot_log, boot_measurements, version = aessca.init(0)
74+
75+
# Check whether we enable the masking
76+
if masking:
77+
lfsr_seed = 1
78+
else:
79+
lfsr_seed = 0
80+
aessca.seed_lfsr(lfsr_seed.to_bytes(4, "little"))
81+
82+
# Set the trigger
83+
triggersca = OTTRIGGER(target, "ujson")
84+
triggersca.select_trigger(0)
85+
86+
# Set the internal prng
87+
ot_prng = OTPRNG(target=target, protocol="ujson")
88+
ot_prng.seed_prng([0,0,0,0])
89+
90+
# Generate plaintexts and keys for first batch.
91+
aessca.start_fvsr_batch_generate(2)
92+
93+
for _ in range(iterations):
94+
aessca.fvsr_data_batch_encrypt(num_segments)
95+
response = target.read_response()
96+
return response
97+
98+
def char_aes_batch_key_fvsr_encrypt(opentitantool, iterations, num_segments, masking):
99+
target = DUT()
100+
reset_target(opentitantool)
101+
# Clear the output from the reset
102+
target.dump_all()
103+
104+
aessca = OTAES(target, "ujson")
105+
# Initialize our chip and catch its output
106+
device_id, owner_page, boot_log, boot_measurements, version = aessca.init(0)
107+
108+
# Check whether we enable the masking
109+
if masking:
110+
lfsr_seed = 1
111+
else:
112+
lfsr_seed = 0
113+
aessca.seed_lfsr(lfsr_seed.to_bytes(4, "little"))
114+
115+
# Set the trigger
116+
triggersca = OTTRIGGER(target, "ujson")
117+
triggersca.select_trigger(0)
118+
119+
# Set the internal prng
120+
ot_prng = OTPRNG(target=target, protocol="ujson")
121+
ot_prng.seed_prng([0,0,0,0])
122+
123+
# Generate plaintexts and keys for first batch.
124+
aessca.start_fvsr_batch_generate(1)
125+
aessca.write_fvsr_batch_generate(num_segments)
126+
127+
for _ in range(iterations):
128+
aessca.fvsr_key_batch_encrypt(num_segments)
129+
response = target.read_response()
130+
return response
131+

0 commit comments

Comments
 (0)