|
5 | 5 |
|
6 | 6 | Communication with OpenTitan happens over the uJSON command interface. |
7 | 7 | """ |
| 8 | +import copy |
8 | 9 | import json |
9 | 10 | import time |
10 | 11 | from typing import Optional |
|
13 | 14 | from target.communication.otfi import OTFI |
14 | 15 |
|
15 | 16 |
|
16 | | -class OTFICrypto(OTFI): |
17 | | - def __init__(self, target) -> None: |
18 | | - super().__init__(target, "Crypto") |
19 | | - |
20 | | - def crypto_shadow_reg_access(self) -> None: |
21 | | - """ Starts the crypto.fi.shadow_reg_access test. |
22 | | - """ |
23 | | - # CryptoFi command. |
24 | | - self._ujson_fi_cmd() |
25 | | - # ShadowRegAccess command. |
26 | | - time.sleep(0.01) |
27 | | - self.target.write(json.dumps("ShadowRegAccess").encode("ascii")) |
28 | | - |
29 | | - def crypto_aes_key(self) -> None: |
30 | | - """ Starts the crypto.fi.aes_key test. |
31 | | - """ |
32 | | - # CryptoFi command. |
33 | | - self._ujson_fi_cmd() |
34 | | - # Aes command. |
35 | | - time.sleep(0.01) |
36 | | - self.target.write(json.dumps("Aes").encode("ascii")) |
37 | | - # Mode payload. |
38 | | - time.sleep(0.01) |
39 | | - mode = {"key_trigger": True, "plaintext_trigger": False, |
40 | | - "encrypt_trigger": False, "ciphertext_trigger": False} |
41 | | - self.target.write(json.dumps(mode).encode("ascii")) |
| 17 | +MODES = { |
| 18 | + "aes": { |
| 19 | + "key_trigger": False, "plaintext_trigger": False, |
| 20 | + "encrypt_trigger": False, "ciphertext_trigger": False |
| 21 | + }, |
| 22 | + "kmac": { |
| 23 | + "key_trigger": False, "absorb_trigger": False, |
| 24 | + "static_trigger": False, "squeeze_trigger": False |
| 25 | + }, |
| 26 | +} |
42 | 27 |
|
43 | | - def crypto_aes_plaintext(self) -> None: |
44 | | - """ Starts the crypto.fi.aes_plaintext test. |
45 | | - """ |
46 | | - # CryptoFi command. |
47 | | - self._ujson_fi_cmd() |
48 | | - # Aes command. |
49 | | - time.sleep(0.01) |
50 | | - self.target.write(json.dumps("Aes").encode("ascii")) |
51 | | - # Mode payload. |
52 | | - time.sleep(0.01) |
53 | | - mode = {"key_trigger": False, "plaintext_trigger": True, |
54 | | - "encrypt_trigger": False, "ciphertext_trigger": False} |
55 | | - self.target.write(json.dumps(mode).encode("ascii")) |
56 | 28 |
|
57 | | - def crypto_aes_encrypt(self) -> None: |
58 | | - """ Starts the crypto.fi.aes_encrypt test. |
59 | | - """ |
60 | | - # CryptoFi command. |
61 | | - self._ujson_fi_cmd() |
62 | | - # Aes command. |
63 | | - time.sleep(0.01) |
64 | | - self.target.write(json.dumps("Aes").encode("ascii")) |
65 | | - # Mode payload. |
66 | | - time.sleep(0.01) |
67 | | - mode = {"key_trigger": False, "plaintext_trigger": False, |
68 | | - "encrypt_trigger": True, "ciphertext_trigger": False} |
69 | | - self.target.write(json.dumps(mode).encode("ascii")) |
| 29 | +def _get_mode(ip, mode_id): |
| 30 | + assert ip in MODES, f"IP {ip} not in MODES ({MODES})" |
| 31 | + mode = copy.deepcopy(MODES[ip]) |
| 32 | + assert mode_id in mode, f"Mode id {mode_id} not in {ip} mode ({mode})" |
| 33 | + mode[mode_id] = True |
| 34 | + return mode |
70 | 35 |
|
71 | | - def crypto_aes_ciphertext(self) -> None: |
72 | | - """ Starts the crypto.fi.aes_ciphertext test. |
73 | | - """ |
74 | | - # CryptoFi command. |
75 | | - self._ujson_fi_cmd() |
76 | | - # Aes command. |
77 | | - time.sleep(0.01) |
78 | | - self.target.write(json.dumps("Aes").encode("ascii")) |
79 | | - # Mode payload. |
80 | | - time.sleep(0.01) |
81 | | - mode = {"key_trigger": False, "plaintext_trigger": False, |
82 | | - "encrypt_trigger": False, "ciphertext_trigger": True} |
83 | | - self.target.write(json.dumps(mode).encode("ascii")) |
84 | 36 |
|
85 | | - def crypto_kmac_key(self) -> None: |
86 | | - """ Starts the crypto.fi.kmac_key test. |
87 | | - """ |
88 | | - # CryptoFi command. |
89 | | - self._ujson_fi_cmd() |
90 | | - # Kmac command. |
91 | | - time.sleep(0.01) |
92 | | - self.target.write(json.dumps("Kmac").encode("ascii")) |
93 | | - # Mode payload. |
94 | | - time.sleep(0.01) |
95 | | - mode = {"key_trigger": True, "absorb_trigger": False, |
96 | | - "static_trigger": False, "squeeze_trigger": False} |
97 | | - self.target.write(json.dumps(mode).encode("ascii")) |
98 | | - |
99 | | - def crypto_kmac_absorb(self) -> None: |
100 | | - """ Starts the crypto.fi.kmac_absorb test. |
101 | | - """ |
102 | | - # CryptoFi command. |
103 | | - self._ujson_fi_cmd() |
104 | | - # Kmac command. |
105 | | - time.sleep(0.01) |
106 | | - self.target.write(json.dumps("Kmac").encode("ascii")) |
107 | | - # Mode payload. |
108 | | - time.sleep(0.01) |
109 | | - mode = {"key_trigger": False, "absorb_trigger": True, |
110 | | - "static_trigger": False, "squeeze_trigger": False} |
111 | | - self.target.write(json.dumps(mode).encode("ascii")) |
112 | | - |
113 | | - def crypto_kmac_squeeze(self) -> None: |
114 | | - """ Starts the crypto.fi.kmac_squeeze test. |
115 | | - """ |
116 | | - # CryptoFi command. |
117 | | - self._ujson_fi_cmd() |
118 | | - # Kmac command. |
119 | | - time.sleep(0.01) |
120 | | - self.target.write(json.dumps("Kmac").encode("ascii")) |
121 | | - # Mode payload. |
122 | | - time.sleep(0.01) |
123 | | - mode = {"key_trigger": False, "absorb_trigger": False, |
124 | | - "static_trigger": False, "squeeze_trigger": True} |
125 | | - self.target.write(json.dumps(mode).encode("ascii")) |
| 37 | +class OTFICrypto(OTFI): |
| 38 | + TESTS = [ |
| 39 | + OTFITest("shadow_reg_access"), |
| 40 | + OTFITest("aes_key", "Aes", _get_mode("aes", "key_trigger")), |
| 41 | + OTFITest("aes_plaintext", "Aes", _get_mode("aes", "plaintext_trigger")), |
| 42 | + OTFITest("aes_encrypt", "Aes", _get_mode("aes", "encrypt_trigger")), |
| 43 | + OTFITest("aes_ciphertext", "Aes", _get_mode("aes", "ciphertext_trigger")), |
| 44 | + OTFITest("kmac_key", "Kmac", _get_mode("kmac", "key_trigger")), |
| 45 | + OTFITest("kmac_absorb", "Kmac", _get_mode("kmac", "absorb_trigger")), |
| 46 | + OTFITest("kmac_static", "Kmac", _get_mode("kmac", "static_trigger")), |
| 47 | + OTFITest("kmac_squeeze", "Kmac", _get_mode("kmac", "squeeze_trigger")), |
| 48 | + ] |
126 | 49 |
|
127 | | - def crypto_kmac_static(self) -> None: |
128 | | - """ Starts the crypto.fi.kmac_static test. |
129 | | - """ |
130 | | - # CryptoFi command. |
131 | | - self._ujson_fi_cmd() |
132 | | - # Kmac command. |
133 | | - time.sleep(0.01) |
134 | | - self.target.write(json.dumps("Kmac").encode("ascii")) |
135 | | - # Mode payload. |
136 | | - time.sleep(0.01) |
137 | | - mode = {"key_trigger": False, "absorb_trigger": False, |
138 | | - "static_trigger": True, "squeeze_trigger": False} |
139 | | - self.target.write(json.dumps(mode).encode("ascii")) |
| 50 | + def __init__(self, target) -> None: |
| 51 | + super().__init__(target, "Crypto") |
0 commit comments