Software APIs
rnd.c
1// Copyright lowRISC contributors (OpenTitan project).
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5#include "sw/device/silicon_creator/lib/drivers/rnd.h"
6
7#include "hw/top/dt/entropy_src.h"
8#include "hw/top/dt/rv_core_ibex.h"
11#include "sw/device/lib/base/crc32.h"
15#include "sw/device/silicon_creator/lib/drivers/otp.h"
16
17#include "hw/top/entropy_src_regs.h"
18#include "hw/top/otp_ctrl_regs.h"
19#include "hw/top/rv_core_ibex_regs.h"
20
21enum {
22 // This covers the health threshold registers which are contiguous. The alert
23 // threshold register is not included here.
24 kNumHealthRegisters = 9,
25};
26
27static inline uint32_t entropy_src_base(void) {
28 return dt_entropy_src_primary_reg_block(kDtEntropySrc);
29}
30
31static inline uint32_t ibex_base(void) {
32 return dt_rv_core_ibex_primary_reg_block(kDtRvCoreIbex);
33}
34
35// Check the number of health registers covered by this driver.
36static_assert(kNumHealthRegisters ==
37 (ENTROPY_SRC_EXTHT_LO_THRESHOLD_REG_OFFSET -
38 ENTROPY_SRC_REPCNT_THRESHOLD_REG_OFFSET) /
39 sizeof(uint32_t) +
40 1,
41 "Unexpected entropy_src health register count.");
42
43// Ensure the relative offsets of OTP versus entropy_src registers are
44// equivalent. This is imporant as rom_start.S uses a copy function to
45// copy the values from OTP into the entropy_src.
46#define ASSERT_REG_OFFSET(otp_offset_, entropy_src_offset_) \
47 static_assert( \
48 ((otp_offset_)-OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_REPCNT_THRESHOLDS_OFFSET) == \
49 ((entropy_src_offset_)-ENTROPY_SRC_REPCNT_THRESHOLD_REG_OFFSET), \
50 "OTP configuration offset does not match the expected entropy_src " \
51 "register offset")
52
53ASSERT_REG_OFFSET(OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_REPCNT_THRESHOLDS_OFFSET,
54 ENTROPY_SRC_REPCNT_THRESHOLD_REG_OFFSET);
55ASSERT_REG_OFFSET(OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_REPCNTS_THRESHOLDS_OFFSET,
56 ENTROPY_SRC_REPCNTS_THRESHOLD_REG_OFFSET);
57ASSERT_REG_OFFSET(OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_ADAPTP_HI_THRESHOLDS_OFFSET,
58 ENTROPY_SRC_ADAPTP_HI_THRESHOLD_REG_OFFSET);
59ASSERT_REG_OFFSET(OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_ADAPTP_LO_THRESHOLDS_OFFSET,
60 ENTROPY_SRC_ADAPTP_LO_THRESHOLD_REG_OFFSET);
61ASSERT_REG_OFFSET(OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_BUCKET_THRESHOLDS_OFFSET,
62 ENTROPY_SRC_BUCKET_THRESHOLD_REG_OFFSET);
63ASSERT_REG_OFFSET(OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_MARKOV_HI_THRESHOLDS_OFFSET,
64 ENTROPY_SRC_MARKOV_HI_THRESHOLD_REG_OFFSET);
65ASSERT_REG_OFFSET(OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_MARKOV_LO_THRESHOLDS_OFFSET,
66 ENTROPY_SRC_MARKOV_LO_THRESHOLD_REG_OFFSET);
67ASSERT_REG_OFFSET(OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_EXTHT_HI_THRESHOLDS_OFFSET,
68 ENTROPY_SRC_EXTHT_HI_THRESHOLD_REG_OFFSET);
69ASSERT_REG_OFFSET(OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_EXTHT_LO_THRESHOLDS_OFFSET,
70 ENTROPY_SRC_EXTHT_LO_THRESHOLD_REG_OFFSET);
71
72/**
73 * Calculates CRC32 over the entropy_src health test and alert thresholds.
74 */
75static uint32_t health_config_crc32(void) {
76 uint32_t ctx;
77 crc32_init(&ctx);
78
79 // Health test thresholds, whose offsets are statically checked.
80 uint32_t offset = ENTROPY_SRC_REPCNT_THRESHOLD_REG_OFFSET;
81 for (size_t i = 0; i < kNumHealthRegisters; ++i, offset += sizeof(uint32_t)) {
82 crc32_add32(&ctx, abs_mmio_read32(entropy_src_base() + offset));
83 }
84 crc32_add32(&ctx, abs_mmio_read32(entropy_src_base() +
85 ENTROPY_SRC_ALERT_THRESHOLD_REG_OFFSET));
86 return crc32_finish(&ctx);
87}
88
89rom_error_t rnd_health_config_check(lifecycle_state_t lc_state,
90 hardened_bool_t boot_mode) {
91 if (otp_read32(OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_EN_OFFSET) !=
93 return kErrorOk;
94 }
95
96 uint32_t crc32 = health_config_crc32();
97 rom_error_t res = crc32;
98
99 if (launder32(lc_state) == kLcStateTest) {
100 res ^= crc32 ^ kErrorOk;
101 HARDENED_CHECK_EQ(res, kErrorOk);
102 HARDENED_CHECK_EQ(lc_state, kLcStateTest);
103 return res;
104 }
105
106 uint32_t config_digest_offset =
107 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_FIPS_CONFIG_DIGEST_OFFSET;
108 if (boot_mode == kHardenedBoolTrue) {
109 config_digest_offset =
110 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_BOOT_CONFIG_DIGEST_OFFSET;
111 }
112 res ^= otp_read32(config_digest_offset);
113 if (launder32(res) != kErrorOk) {
114 return kErrorRndBadCrc32;
115 }
116
117 HARDENED_CHECK_EQ(res, kErrorOk);
118 return res;
119}
120
121uint32_t rnd_uint32(void) {
123 otp_read32(OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_EN_OFFSET) ==
125 // When bit-0 is clear an EDN request for new data for RND_DATA is
126 // pending.
127 while (!(abs_mmio_read32(ibex_base() + RV_CORE_IBEX_RND_STATUS_REG_OFFSET) &
128 1)) {
129 }
130 }
131 uint32_t mcycle;
132 CSR_READ(CSR_REG_MCYCLE, &mcycle);
133 return mcycle +
134 abs_mmio_read32(ibex_base() + RV_CORE_IBEX_RND_DATA_REG_OFFSET);
135}
136
137// Provides the source of randomness for `hardened_memshred` (see
138// `hardened_memory.h`). Declare as weak in case the cryptolib driver is also
139// included.
141uint32_t hardened_memshred_random_word(void) { return rnd_uint32(); }
142
143// Provides the source of randomness for `random_order` (see `random_order.h`).
144// Declare as weak in case the cryptolib driver is also included.
146uint32_t random_order_random_word(void) { return rnd_uint32(); }