Skip to content

Commit 34fddbb

Browse files
[hw] KMAC/ROM patches for scrambled ROM checking
Rather than vendoring dependencies of KMAC not already in Mocha (keymgr, edn, csrng), patch kmac_pkg.sv to include the minor bits required from the keymgr and edn packages. Mocha doesn't need these modules in full as we are only using KMAC to check the contents of the scrambled ROM. Also, fix the ROM image scrambling script import paths for Mocha.
1 parent 8981e03 commit 34fddbb

3 files changed

Lines changed: 188 additions & 1 deletion

File tree

hw/vendor/lowrisc_ip.vendor.hjson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
// Hardware IP blocks.
3131
{from: "hw/ip/entropy_src", to: "ip/entropy_src", patch_dir: "entropy_src"}, // Entropy source.
3232
{from: "hw/ip/i2c", to: "ip/i2c", patch_dir: "i2c"}, // I^2C controller/target
33-
{from: "hw/ip/kmac", to: "ip/kmac"}, // kmac.
33+
{from: "hw/ip/kmac", to: "ip/kmac", patch_dir: "kmac"}, // KMAC.
3434
{from: "hw/ip/lc_ctrl", to: "ip/lc_ctrl"}, // Dependency of crossbar and power manager.
3535
{from: "hw/ip/rom_ctrl", to: "ip/rom_ctrl", patch_dir: "rom_ctrl"}, // Read-only memory.
3636
{from: "hw/ip/rv_core_ibex", to: "ip/rv_core_pkg", patch_dir: "rv_core_pkg"}, // RISC-V core package.
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
diff --git a/kmac.core b/kmac.core
2+
index c4472c4e..6551e023 100644
3+
--- a/kmac.core
4+
+++ b/kmac.core
5+
@@ -17,10 +17,7 @@ filesets:
6+
- lowrisc:prim:trivium
7+
- lowrisc:prim:mubi
8+
- lowrisc:ip:tlul
9+
- - lowrisc:ip:keymgr_pkg
10+
- lowrisc:ip:sha3
11+
- - lowrisc:ip:edn_pkg
12+
- - lowrisc:prim:edn_req
13+
- lowrisc:ip:kmac_pkg
14+
- lowrisc:ip:lc_ctrl_pkg
15+
- lowrisc:prim:lc_sync
16+
diff --git a/rtl/kmac.sv b/rtl/kmac.sv
17+
index eb719248..17526815 100644
18+
--- a/rtl/kmac.sv
19+
+++ b/rtl/kmac.sv
20+
@@ -57,15 +57,15 @@ module kmac
21+
output prim_alert_pkg::alert_tx_t [NumAlerts-1:0] alert_tx_o,
22+
23+
// KeyMgr sideload (secret key) interface
24+
- input keymgr_pkg::hw_key_req_t keymgr_key_i,
25+
+ input hw_key_req_t keymgr_key_i,
26+
27+
// KeyMgr KDF data path
28+
input app_req_t [NumAppIntf-1:0] app_i,
29+
output app_rsp_t [NumAppIntf-1:0] app_o,
30+
31+
// EDN interface
32+
- output edn_pkg::edn_req_t entropy_o,
33+
- input edn_pkg::edn_rsp_t entropy_i,
34+
+ output edn_req_t entropy_o,
35+
+ input edn_rsp_t entropy_i,
36+
37+
// Life cycle
38+
input lc_ctrl_pkg::lc_tx_t lc_escalate_en_i,
39+
@@ -1224,12 +1224,12 @@ module kmac
40+
if (EnMasking == 1) begin : gen_entropy
41+
42+
logic entropy_req, entropy_ack;
43+
- logic [edn_pkg::ENDPOINT_BUS_WIDTH-1:0] entropy_data;
44+
+ logic [ENDPOINT_BUS_WIDTH-1:0] entropy_data;
45+
logic unused_entropy_fips;
46+
47+
// Synchronize EDN interface
48+
prim_sync_reqack_data #(
49+
- .Width(edn_pkg::ENDPOINT_BUS_WIDTH),
50+
+ .Width(ENDPOINT_BUS_WIDTH),
51+
.DataSrc2Dst(1'b0),
52+
.DataReg(1'b0)
53+
) u_prim_sync_reqack_data (
54+
@@ -1310,7 +1310,7 @@ module kmac
55+
);
56+
end else begin : gen_empty_entropy
57+
// If Masking is not used, no need of entropy. Ignore inputs and config; tie output to 0.
58+
- edn_pkg::edn_rsp_t unused_entropy_input;
59+
+ edn_rsp_t unused_entropy_input;
60+
entropy_mode_e unused_entropy_mode;
61+
logic unused_entropy_fast_process;
62+
63+
diff --git a/rtl/kmac_app.sv b/rtl/kmac_app.sv
64+
index 54303868..3be7586b 100644
65+
--- a/rtl/kmac_app.sv
66+
+++ b/rtl/kmac_app.sv
67+
@@ -38,7 +38,7 @@ module kmac_app
68+
output logic sw_ready_o,
69+
70+
// KeyMgr Sideload Key interface
71+
- input keymgr_pkg::hw_key_req_t keymgr_key_i,
72+
+ input hw_key_req_t keymgr_key_i,
73+
74+
// Application Message in/ Digest out interface + control signals
75+
input app_req_t [NumAppIntf-1:0] app_i,
76+
@@ -774,7 +774,7 @@ module kmac_app
77+
end else begin : g_unmasked_key
78+
always_comb begin
79+
keymgr_key[0] = '0;
80+
- for (int i = 0; i < keymgr_pkg::Shares; i++) begin
81+
+ for (int i = 0; i < Shares; i++) begin
82+
keymgr_key[0][KeyMgrKeyW-1:0] ^= keymgr_key_i.key[i];
83+
end
84+
end
85+
diff --git a/rtl/kmac_entropy.sv b/rtl/kmac_entropy.sv
86+
index 07d1cbcd..7caa7345 100644
87+
--- a/rtl/kmac_entropy.sv
88+
+++ b/rtl/kmac_entropy.sv
89+
@@ -20,7 +20,7 @@ module kmac_entropy
90+
// EDN interface
91+
output logic entropy_req_o,
92+
input entropy_ack_i,
93+
- input [edn_pkg::ENDPOINT_BUS_WIDTH-1:0] entropy_data_i,
94+
+ input [ENDPOINT_BUS_WIDTH-1:0] entropy_data_i,
95+
96+
// Entropy to internal
97+
output logic rand_valid_o,
98+
@@ -194,7 +194,7 @@ module kmac_entropy
99+
// SW configures to use EDN or ENTROPY_SEED register as PRNG seed
100+
logic seed_en, seed_done;
101+
logic seed_req, seed_ack;
102+
- logic [edn_pkg::ENDPOINT_BUS_WIDTH-1:0] seed;
103+
+ logic [ENDPOINT_BUS_WIDTH-1:0] seed;
104+
logic prng_en;
105+
logic [EntropyOutputW-1:0] prng_data, prng_data_permuted;
106+
107+
@@ -359,7 +359,7 @@ module kmac_entropy
108+
.OutputWidth (EntropyOutputW),
109+
.StrictLockupProtection(1),
110+
.SeedType (prim_trivium_pkg::SeedTypeStatePartial),
111+
- .PartialSeedWidth (edn_pkg::ENDPOINT_BUS_WIDTH),
112+
+ .PartialSeedWidth (ENDPOINT_BUS_WIDTH),
113+
.RndCnstTriviumLfsrSeed(RndCnstLfsrSeed)
114+
) u_prim_trivium (
115+
.clk_i (clk_i),
116+
@@ -758,7 +758,7 @@ module kmac_entropy
117+
118+
// The EDN bus width needs to be equal to the width of the ENTROPY_SEED
119+
// register as this module doesn't perform width adaption.
120+
- `ASSERT_INIT(EdnBusWidth_A, edn_pkg::ENDPOINT_BUS_WIDTH == 32)
121+
+ `ASSERT_INIT(EdnBusWidth_A, ENDPOINT_BUS_WIDTH == 32)
122+
123+
// the code below is not meant to be synthesized,
124+
// but it is intended to be used in simulation and FPV
125+
diff --git a/rtl/kmac_pkg.sv b/rtl/kmac_pkg.sv
126+
index 10a03442..208be1c4 100644
127+
--- a/rtl/kmac_pkg.sv
128+
+++ b/rtl/kmac_pkg.sv
129+
@@ -474,4 +474,38 @@ package kmac_pkg;
130+
conv_endian32 = (swap) ? conv_data : v ;
131+
endfunction : conv_endian32
132+
133+
+
134+
+ ///////////////////////
135+
+ // KEYMGR Copy-Paste //
136+
+ ///////////////////////
137+
+
138+
+ parameter int KeyWidth = 256;
139+
+ parameter int Shares = 2;
140+
+ typedef struct packed {
141+
+ logic valid;
142+
+ logic [Shares-1:0][KeyWidth-1:0] key;
143+
+ } hw_key_req_t;
144+
+
145+
+ parameter hw_key_req_t HW_KEY_REQ_DEFAULT = '{
146+
+ valid: 1'b0,
147+
+ key: {Shares{KeyWidth'(32'hDEADBEEF)}}
148+
+ };
149+
+
150+
+ ////////////////////
151+
+ // EDN Copy-Paste //
152+
+ ////////////////////
153+
+
154+
+ parameter int unsigned ENDPOINT_BUS_WIDTH = 32;
155+
+
156+
+ typedef struct packed {
157+
+ logic edn_req;
158+
+ } edn_req_t;
159+
+ typedef struct packed {
160+
+ logic edn_ack;
161+
+ logic edn_fips;
162+
+ logic [ENDPOINT_BUS_WIDTH-1:0] edn_bus;
163+
+ } edn_rsp_t;
164+
+
165+
+ parameter edn_rsp_t EDN_RSP_DEFAULT = '0;
166+
+
167+
endpackage : kmac_pkg

hw/vendor/patches/lowrisc_ip/rom_ctrl/0001_Path_And_Tool_Fixes.patch

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,23 @@ index 9062c0f..228a0ec 100644
115115

116116
// Default UVM test and seq class name.
117117
uvm_test: rom_ctrl_base_test
118+
diff --git a/util/scramble_image.py b/util/scramble_image.py
119+
index 91701d47..26f2f691 100755
120+
--- a/util/scramble_image.py
121+
+++ b/util/scramble_image.py
122+
@@ -12,6 +12,15 @@ from typing import Dict, IO, Optional, Tuple
123+
import hjson # type: ignore
124+
from Crypto.Hash import cSHAKE256
125+
126+
+# Add to the PATH the directory that this script and its "mem" dependency
127+
+# expect to be able to use relative paths to import other dependencies from.
128+
+from pathlib import Path
129+
+PROJECT_ROOT = Path.resolve(Path(__file__)).parent.parent.parent.parent.parent.parent.parent
130+
+LOWRISC_IP_ROOT = PROJECT_ROOT / Path("hw/vendor/lowrisc_ip")
131+
+print("PROJECT_ROOT: ", PROJECT_ROOT)
132+
+print("LOWRISC_IP_ROOT:", LOWRISC_IP_ROOT)
133+
+sys.path.append(str(LOWRISC_IP_ROOT))
134+
+
135+
from mem import MemChunk, MemFile
136+
from util.design.prince import prince, sbox # type: ignore
137+
from util.design.secded_gen import ecc_encode_some # type: ignore

0 commit comments

Comments
 (0)