Skip to content

Commit ebda1d5

Browse files
committed
[cryptolib,silicon_creator] Deduplicate ibex random function
This commit makes two changes: - the ibex_rnd32_read() function is moved from cryptolib to silicon_creator, the users are fixed - the rnd_uint32() function is changed to return either mcycle OR rnd32(), depending on the boot stage and OTP setting. Regarding the second change, it was previoulsy returning `mcycle + rnd32()` but not waiting for random data depending on an OTP setting. There is no good reason for that because if it waits for data from the EDN, all the health checks are already done by the csrng, meaning that adding `mcycle` does not add any value to a random variable. Also if the OTP is *not* set, it causes a read to known-invalid register using the fact that it will be 0 in this cases. Signed-off-by: Amaury Pouly <[email protected]>
1 parent 91bddb1 commit ebda1d5

File tree

13 files changed

+52
-60
lines changed

13 files changed

+52
-60
lines changed

sw/device/lib/crypto/drivers/BUILD

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ cc_library(
121121
],
122122
deps = [
123123
":entropy",
124-
":rv_core_ibex",
125124
"//hw/top:dt_kmac",
126125
"//hw/top:kmac_c_regs",
127126
"//sw/device/lib/base:abs_mmio",
128127
"//sw/device/lib/base:bitfield",
129128
"//sw/device/lib/base:hardened",
130129
"//sw/device/lib/base:macros",
131130
"//sw/device/lib/crypto/impl:status",
131+
"//sw/device/silicon_creator/lib/drivers:ibex",
132132
],
133133
)
134134

@@ -213,8 +213,8 @@ cc_library(
213213
"//sw/device/lib/base:macros",
214214
"//sw/device/lib/base:memory",
215215
"//sw/device/lib/crypto/drivers:entropy",
216-
"//sw/device/lib/crypto/drivers:rv_core_ibex",
217216
"//sw/device/lib/crypto/impl:status",
217+
"//sw/device/silicon_creator/lib/drivers:ibex",
218218
],
219219
)
220220

@@ -238,6 +238,7 @@ dual_cc_library(
238238
shared = [
239239
"//sw/device/lib/base:hardened",
240240
"//sw/device/lib/crypto/impl:status",
241+
"//sw/device/silicon_creator/lib/drivers:ibex",
241242
],
242243
),
243244
)
@@ -250,14 +251,11 @@ opentitan_test(
250251
timeout = "long",
251252
),
252253
deps = [
253-
":rv_core_ibex",
254-
"//hw/top:rv_core_ibex_c_regs",
255-
"//sw/device/lib/base:bitfield",
256-
"//sw/device/lib/base:csr",
257254
"//sw/device/lib/base:status",
258255
"//sw/device/lib/runtime:log",
259256
"//sw/device/lib/testing/test_framework:check",
260257
"//sw/device/lib/testing/test_framework:ottf_main",
258+
"//sw/device/silicon_creator/lib/drivers:ibex",
261259
],
262260
)
263261

sw/device/lib/crypto/drivers/hmac.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include "sw/device/lib/base/hardened_memory.h"
1313
#include "sw/device/lib/base/memory.h"
1414
#include "sw/device/lib/crypto/drivers/entropy.h"
15-
#include "sw/device/lib/crypto/drivers/rv_core_ibex.h"
1615
#include "sw/device/lib/crypto/impl/status.h"
16+
#include "sw/device/silicon_creator/lib/drivers/ibex.h"
1717

1818
#include "hw/top/hmac_regs.h" // Generated.
1919

sw/device/lib/crypto/drivers/kmac.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include "sw/device/lib/base/bitfield.h"
1010
#include "sw/device/lib/base/memory.h"
1111
#include "sw/device/lib/crypto/drivers/entropy.h"
12-
#include "sw/device/lib/crypto/drivers/rv_core_ibex.h"
1312
#include "sw/device/lib/crypto/impl/status.h"
13+
#include "sw/device/silicon_creator/lib/drivers/ibex.h"
1414

1515
#include "hw/top/kmac_regs.h" // Generated.
1616

sw/device/lib/crypto/drivers/rv_core_ibex.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "sw/device/lib/base/hardened.h"
1212
#include "sw/device/lib/base/macros.h"
1313
#include "sw/device/lib/crypto/impl/status.h"
14+
#include "sw/device/silicon_creator/lib/drivers/ibex.h"
1415

1516
#include "hw/top/rv_core_ibex_regs.h"
1617

@@ -56,19 +57,6 @@ hardened_bool_t ibex_check_security_config(void) {
5657
return kHardenedBoolTrue;
5758
}
5859

59-
/**
60-
* Blocks until data is ready in the RND register.
61-
*/
62-
static void wait_rnd_valid(void) {
63-
while (true) {
64-
uint32_t reg = abs_mmio_read32(rv_core_ibex_base() +
65-
RV_CORE_IBEX_RND_STATUS_REG_OFFSET);
66-
if (bitfield_bit32_read(reg, RV_CORE_IBEX_RND_STATUS_RND_DATA_VALID_BIT)) {
67-
return;
68-
}
69-
}
70-
}
71-
7260
status_t ibex_disable_icache(hardened_bool_t *icache_enabled) {
7361
// Check if the instruction cache is already disabled.
7462
uint32_t csr;
@@ -98,12 +86,6 @@ void ibex_restore_icache(hardened_bool_t icache_enabled) {
9886
}
9987
}
10088

101-
uint32_t ibex_rnd32_read(void) {
102-
wait_rnd_valid();
103-
return abs_mmio_read32(rv_core_ibex_base() +
104-
RV_CORE_IBEX_RND_DATA_REG_OFFSET);
105-
}
106-
10789
OT_ALWAYS_INLINE void ibex_clear_rf(void) {
10890
#ifdef OT_PLATFORM_RV32
10991
// ibex_clear_rf() below cannot clear x8 (frame pointer). To avoid that

sw/device/lib/crypto/drivers/rv_core_ibex.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,6 @@ void ibex_restore_icache(hardened_bool_t icache_enabled);
4444
OT_WARN_UNUSED_RESULT
4545
hardened_bool_t ibex_check_security_config(void);
4646

47-
/**
48-
* Get random data from the EDN0 interface.
49-
*
50-
* Important: this function will hang if the entropy complex is not
51-
* initialized. Callers are responsible for checking first.
52-
*
53-
* @return 32 bits of randomness from EDN0.
54-
*/
55-
uint32_t ibex_rnd32_read(void);
56-
5747
/**
5848
* Write a random value into x5...x7 and x9...x31.
5949
*

sw/device/lib/crypto/drivers/rv_core_ibex_test.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
33
// SPDX-License-Identifier: Apache-2.0
44

5-
#include "sw/device/lib/crypto/drivers/rv_core_ibex.h"
6-
75
#include "sw/device/lib/base/status.h"
86
#include "sw/device/lib/runtime/log.h"
97
#include "sw/device/lib/testing/test_framework/check.h"
108
#include "sw/device/lib/testing/test_framework/ottf_main.h"
9+
#include "sw/device/silicon_creator/lib/drivers/ibex.h"
1110

1211
#include "hw/top/rv_core_ibex_regs.h"
1312

sw/device/lib/crypto/impl/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ cc_library(
347347
"//sw/device/lib/base:hardened_memory",
348348
"//sw/device/lib/crypto/drivers:entropy",
349349
"//sw/device/lib/crypto/drivers:hmac",
350-
"//sw/device/lib/crypto/drivers:rv_core_ibex",
350+
"//sw/device/silicon_creator/lib/drivers:ibex",
351351
],
352352
)
353353

sw/device/lib/crypto/impl/hmac.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
#include "sw/device/lib/base/hardened_memory.h"
88
#include "sw/device/lib/crypto/drivers/entropy.h"
99
#include "sw/device/lib/crypto/drivers/hmac.h"
10-
#include "sw/device/lib/crypto/drivers/rv_core_ibex.h"
1110
#include "sw/device/lib/crypto/impl/integrity.h"
1211
#include "sw/device/lib/crypto/impl/keyblob.h"
1312
#include "sw/device/lib/crypto/impl/security_config.h"
1413
#include "sw/device/lib/crypto/impl/status.h"
14+
#include "sw/device/silicon_creator/lib/drivers/ibex.h"
1515

1616
// Module ID for status codes.
1717
#define MODULE_ID MAKE_MODULE_ID('h', 'm', 'c')

sw/device/silicon_creator/lib/drivers/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,6 @@ dual_cc_library(
612612
"//sw/device/lib/base:hardened",
613613
"//hw/top:entropy_src_c_regs",
614614
"//hw/top:otp_ctrl_c_regs",
615-
"//hw/top:rv_core_ibex_c_regs",
616615
"//sw/device/lib/base:crc32",
617616
],
618617
host = [
@@ -624,6 +623,7 @@ dual_cc_library(
624623
":lifecycle",
625624
"//sw/device/lib/base:macros",
626625
"//sw/device/silicon_creator/lib:error",
626+
"//sw/device/silicon_creator/lib/drivers:ibex",
627627
],
628628
),
629629
)

sw/device/silicon_creator/lib/drivers/ibex.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ static inline uint32_t rv_core_ibex_base(void) {
2222
return dt_rv_core_ibex_reg_block(kRvCoreIbexDt, kDtRvCoreIbexRegBlockCfg);
2323
}
2424

25+
/**
26+
* Blocks until data is ready in the RND register.
27+
*/
28+
static void wait_rnd_valid(void) {
29+
while (true) {
30+
uint32_t reg = abs_mmio_read32(rv_core_ibex_base() +
31+
RV_CORE_IBEX_RND_STATUS_REG_OFFSET);
32+
if (bitfield_bit32_read(reg, RV_CORE_IBEX_RND_STATUS_RND_DATA_VALID_BIT)) {
33+
return;
34+
}
35+
}
36+
}
37+
38+
uint32_t ibex_rnd32_read(void) {
39+
wait_rnd_valid();
40+
return abs_mmio_read32(rv_core_ibex_base() +
41+
RV_CORE_IBEX_RND_DATA_REG_OFFSET);
42+
}
43+
2544
uint32_t ibex_fpga_version(void) {
2645
const uint32_t kBase = rv_core_ibex_base();
2746
return abs_mmio_read32(kBase + RV_CORE_IBEX_FPGA_INFO_REG_OFFSET);

0 commit comments

Comments
 (0)