Skip to content

Commit b28ec5e

Browse files
committed
[fi] Add OTFI base class
Shared implementation logic in OTFI Ibex, Otbn, and Crypto is moved in a base class named `OTFI`.
1 parent 34a0227 commit b28ec5e

File tree

4 files changed

+121
-182
lines changed

4 files changed

+121
-182
lines changed

target/communication/fi_crypto_commands.py

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,18 @@
1010
from typing import Optional
1111

1212

13-
class OTFICrypto:
14-
def __init__(self, target) -> None:
15-
self.target = target
13+
from target.communication.otfi import OTFI
1614

17-
def _ujson_crypto_cmd(self) -> None:
18-
time.sleep(0.01)
19-
self.target.write(json.dumps("CryptoFi").encode("ascii"))
20-
time.sleep(0.01)
2115

22-
def init(self) -> None:
23-
""" Initialize the Crypto FI code on the chip.
24-
Returns:
25-
The device ID of the device.
26-
"""
27-
# CryptoFi command.
28-
self._ujson_crypto_cmd()
29-
# Init command.
30-
time.sleep(0.01)
31-
self.target.write(json.dumps("Init").encode("ascii"))
32-
# Read back device ID from device.
33-
return self.read_response(max_tries=30)
16+
class OTFICrypto(OTFI):
17+
def __init__(self, target) -> None:
18+
super().__init__(target, "Crypto")
3419

3520
def crypto_shadow_reg_access(self) -> None:
3621
""" Starts the crypto.fi.shadow_reg_access test.
3722
"""
3823
# CryptoFi command.
39-
self._ujson_crypto_cmd()
24+
self._ujson_fi_cmd()
4025
# ShadowRegAccess command.
4126
time.sleep(0.01)
4227
self.target.write(json.dumps("ShadowRegAccess").encode("ascii"))
@@ -45,7 +30,7 @@ def crypto_aes_key(self) -> None:
4530
""" Starts the crypto.fi.aes_key test.
4631
"""
4732
# CryptoFi command.
48-
self._ujson_crypto_cmd()
33+
self._ujson_fi_cmd()
4934
# Aes command.
5035
time.sleep(0.01)
5136
self.target.write(json.dumps("Aes").encode("ascii"))
@@ -59,7 +44,7 @@ def crypto_aes_plaintext(self) -> None:
5944
""" Starts the crypto.fi.aes_plaintext test.
6045
"""
6146
# CryptoFi command.
62-
self._ujson_crypto_cmd()
47+
self._ujson_fi_cmd()
6348
# Aes command.
6449
time.sleep(0.01)
6550
self.target.write(json.dumps("Aes").encode("ascii"))
@@ -73,7 +58,7 @@ def crypto_aes_encrypt(self) -> None:
7358
""" Starts the crypto.fi.aes_encrypt test.
7459
"""
7560
# CryptoFi command.
76-
self._ujson_crypto_cmd()
61+
self._ujson_fi_cmd()
7762
# Aes command.
7863
time.sleep(0.01)
7964
self.target.write(json.dumps("Aes").encode("ascii"))
@@ -87,7 +72,7 @@ def crypto_aes_ciphertext(self) -> None:
8772
""" Starts the crypto.fi.aes_ciphertext test.
8873
"""
8974
# CryptoFi command.
90-
self._ujson_crypto_cmd()
75+
self._ujson_fi_cmd()
9176
# Aes command.
9277
time.sleep(0.01)
9378
self.target.write(json.dumps("Aes").encode("ascii"))
@@ -101,7 +86,7 @@ def crypto_kmac_key(self) -> None:
10186
""" Starts the crypto.fi.kmac_key test.
10287
"""
10388
# CryptoFi command.
104-
self._ujson_crypto_cmd()
89+
self._ujson_fi_cmd()
10590
# Kmac command.
10691
time.sleep(0.01)
10792
self.target.write(json.dumps("Kmac").encode("ascii"))
@@ -115,7 +100,7 @@ def crypto_kmac_absorb(self) -> None:
115100
""" Starts the crypto.fi.kmac_absorb test.
116101
"""
117102
# CryptoFi command.
118-
self._ujson_crypto_cmd()
103+
self._ujson_fi_cmd()
119104
# Kmac command.
120105
time.sleep(0.01)
121106
self.target.write(json.dumps("Kmac").encode("ascii"))
@@ -129,7 +114,7 @@ def crypto_kmac_squeeze(self) -> None:
129114
""" Starts the crypto.fi.kmac_squeeze test.
130115
"""
131116
# CryptoFi command.
132-
self._ujson_crypto_cmd()
117+
self._ujson_fi_cmd()
133118
# Kmac command.
134119
time.sleep(0.01)
135120
self.target.write(json.dumps("Kmac").encode("ascii"))
@@ -143,7 +128,7 @@ def crypto_kmac_static(self) -> None:
143128
""" Starts the crypto.fi.kmac_static test.
144129
"""
145130
# CryptoFi command.
146-
self._ujson_crypto_cmd()
131+
self._ujson_fi_cmd()
147132
# Kmac command.
148133
time.sleep(0.01)
149134
self.target.write(json.dumps("Kmac").encode("ascii"))
@@ -152,31 +137,3 @@ def crypto_kmac_static(self) -> None:
152137
mode = {"key_trigger": False, "absorb_trigger": False,
153138
"static_trigger": True, "squeeze_trigger": False}
154139
self.target.write(json.dumps(mode).encode("ascii"))
155-
156-
def start_test(self, cfg: dict) -> None:
157-
""" Start the selected test.
158-
159-
Call the function selected in the config file. Uses the getattr()
160-
construct to call the function.
161-
162-
Args:
163-
cfg: Config dict containing the selected test.
164-
"""
165-
test_function = getattr(self, cfg["test"]["which_test"])
166-
test_function()
167-
168-
def read_response(self, max_tries: Optional[int] = 1) -> str:
169-
""" Read response from Crypto FI framework.
170-
Args:
171-
max_tries: Maximum number of attempts to read from UART.
172-
173-
Returns:
174-
The JSON response of OpenTitan.
175-
"""
176-
it = 0
177-
while it != max_tries:
178-
read_line = str(self.target.readline())
179-
if "RESP_OK" in read_line:
180-
return read_line.split("RESP_OK:")[1].split(" CRC:")[0]
181-
it += 1
182-
return ""

0 commit comments

Comments
 (0)