Skip to content

Commit 1415433

Browse files
siemen11nasahlpa
authored andcommitted
[crypto] Move entropy_init and check
Move the entropy_complex_init function to the init from the cryptolib to ensure that the library has a correct state to start. Create a new entropy_complex_health_test_config_check function which verifies the health test thresholds and alerts and move that function the otcrypto_eval_exit function such that now every API call finishes by checking the state of the entropy source. This allows us to remove the entropy_complex_check calls inside the impl and drivers functions themselves. However, this changes one critical aspect: - The crypto functions now assume that the otcrypto_init is called before. Especially the drivers might be called outside of that assumption in which case it is left to the user to call the setup and checking of the entropy source themselves. Signed-off-by: Siemen Dhooghe <sdhooghe@google.com>
1 parent 8e3335d commit 1415433

29 files changed

Lines changed: 79 additions & 278 deletions

sw/device/lib/crypto/drivers/BUILD

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ cc_library(
8787
srcs = ["aes.c"],
8888
hdrs = ["aes.h"],
8989
deps = [
90-
":entropy",
9190
":rv_core_ibex",
9291
"//hw/ip/aes/data:aes_c_regs",
9392
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
@@ -115,6 +114,7 @@ opentitan_test(
115114
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
116115
"//sw/device/lib/base:macros",
117116
"//sw/device/lib/base:memory",
117+
"//sw/device/lib/crypto/drivers:entropy",
118118
"//sw/device/lib/crypto/impl:status",
119119
"//sw/device/lib/testing/test_framework:check",
120120
"//sw/device/lib/testing/test_framework:ottf_alerts",
@@ -129,7 +129,6 @@ cc_library(
129129
"keymgr.h",
130130
],
131131
deps = [
132-
":entropy",
133132
":rv_core_ibex",
134133
"//hw/ip/keymgr/data:keymgr_c_regs",
135134
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
@@ -154,6 +153,7 @@ opentitan_test(
154153
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
155154
"//sw/device/lib/base:macros",
156155
"//sw/device/lib/base:memory",
156+
"//sw/device/lib/crypto/drivers:entropy",
157157
"//sw/device/lib/crypto/impl:status",
158158
"//sw/device/lib/testing:keymgr_testutils",
159159
"//sw/device/lib/testing/test_framework:check",
@@ -170,7 +170,6 @@ cc_library(
170170
"//sw/device/lib/crypto/include:datatypes.h",
171171
],
172172
deps = [
173-
":entropy",
174173
":rv_core_ibex",
175174
"//hw/ip/kmac/data:kmac_c_regs",
176175
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
@@ -198,6 +197,7 @@ opentitan_test(
198197
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
199198
"//sw/device/lib/base:macros",
200199
"//sw/device/lib/base:memory",
200+
"//sw/device/lib/crypto/drivers:entropy",
201201
"//sw/device/lib/crypto/impl:integrity",
202202
"//sw/device/lib/crypto/impl:status",
203203
"//sw/device/lib/testing/test_framework:check",
@@ -278,7 +278,6 @@ cc_library(
278278
srcs = ["hmac.c"],
279279
hdrs = ["hmac.h"],
280280
deps = [
281-
":entropy",
282281
"//hw/ip/hmac/data:hmac_c_regs",
283282
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
284283
"//sw/device/lib/base:abs_mmio",
@@ -308,6 +307,7 @@ opentitan_test(
308307
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
309308
"//sw/device/lib/base:macros",
310309
"//sw/device/lib/base:memory",
310+
"//sw/device/lib/crypto/drivers:entropy",
311311
"//sw/device/lib/crypto/impl:integrity",
312312
"//sw/device/lib/crypto/impl:status",
313313
"//sw/device/lib/testing/test_framework:check",
@@ -364,7 +364,6 @@ cc_library(
364364
srcs = ["otbn.c"],
365365
hdrs = ["otbn.h"],
366366
deps = [
367-
":entropy",
368367
"//hw/ip/otbn/data:otbn_c_regs",
369368
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
370369
"//sw/device/lib/base:abs_mmio",
@@ -390,6 +389,7 @@ opentitan_test(
390389
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
391390
"//sw/device/lib/base:macros",
392391
"//sw/device/lib/base:memory",
392+
"//sw/device/lib/crypto/drivers:entropy",
393393
"//sw/device/lib/crypto/impl:status",
394394
"//sw/device/lib/testing/test_framework:check",
395395
"//sw/device/lib/testing/test_framework:ottf_alerts",

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "sw/device/lib/base/hardened_memory.h"
1212
#include "sw/device/lib/base/macros.h"
1313
#include "sw/device/lib/base/memory.h"
14-
#include "sw/device/lib/crypto/drivers/entropy.h"
1514
#include "sw/device/lib/crypto/drivers/rv_core_ibex.h"
1615
#include "sw/device/lib/crypto/impl/status.h"
1716

@@ -248,10 +247,6 @@ status_t aes_verify_ctrl_aux_reg(void) {
248247
*/
249248
static status_t aes_begin(aes_key_t key, const aes_block_t *iv,
250249
hardened_bool_t encrypt) {
251-
// Ensure the entropy complex is in an appropriate state. The AES block seeds
252-
// its PRNG from EDN for masking every time a new key is provided.
253-
HARDENED_TRY(entropy_complex_check());
254-
255250
// Wait for the AES block to be idle.
256251
HARDENED_TRY(spin_until(AES_STATUS_IDLE_BIT));
257252

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,37 @@ status_t entropy_complex_check(void) {
938938
return edn_check(&config->edn1);
939939
}
940940

941+
OT_WARN_UNUSED_RESULT
942+
status_t entropy_complex_health_test_config_check(void) {
943+
const entropy_src_config_t *entropy_src_config =
944+
&kEntropyComplexConfigs[kEntropyComplexConfigIdContinuous].entropy_src;
945+
946+
uint32_t reg;
947+
// Check health test window
948+
reg = abs_mmio_read32(kBaseEntropySrc +
949+
ENTROPY_SRC_HEALTH_TEST_WINDOWS_REG_OFFSET);
950+
HARDENED_CHECK_EQ(bitfield_field32_read(
951+
reg, ENTROPY_SRC_HEALTH_TEST_WINDOWS_FIPS_WINDOW_FIELD),
952+
entropy_src_config->fips_test_window_size);
953+
954+
// Check recoverable alerts
955+
if (abs_mmio_read32(kBaseEntropySrc +
956+
ENTROPY_SRC_RECOV_ALERT_STS_REG_OFFSET) != 0) {
957+
return OTCRYPTO_RECOV_ERR;
958+
}
959+
960+
// Check health test thresholds
961+
VERIFY_FIPS_THRESH(REPCNT, entropy_src_config->repcnt_threshold);
962+
VERIFY_FIPS_THRESH(REPCNTS, entropy_src_config->repcnts_threshold);
963+
VERIFY_FIPS_THRESH(ADAPTP_HI, entropy_src_config->adaptp_hi_threshold);
964+
VERIFY_FIPS_THRESH(ADAPTP_LO, entropy_src_config->adaptp_lo_threshold);
965+
VERIFY_FIPS_THRESH(BUCKET, entropy_src_config->bucket_threshold);
966+
VERIFY_FIPS_THRESH(MARKOV_HI, entropy_src_config->markov_hi_threshold);
967+
VERIFY_FIPS_THRESH(MARKOV_LO, entropy_src_config->markov_lo_threshold);
968+
969+
return OTCRYPTO_OK;
970+
}
971+
941972
status_t entropy_csrng_instantiate(
942973
hardened_bool_t disable_trng_input,
943974
const entropy_seed_material_t *seed_material) {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ status_t entropy_complex_init(void);
112112
OT_WARN_UNUSED_RESULT
113113
status_t entropy_complex_check(void);
114114

115+
/**
116+
* Check the entropy complex health test and alert configurations.
117+
*
118+
* This function checks the entropy src registers to detect tampering.
119+
* The multi-bit encoded registers are checked by the HW.
120+
*
121+
* This function does not return a status error, and will simply assert the chip
122+
* is in the correct configuration.
123+
*
124+
* @return error on failure.
125+
*/
126+
OT_WARN_UNUSED_RESULT
127+
status_t entropy_complex_health_test_config_check(void);
128+
115129
/**
116130
* Instantiate the SW CSRNG with a new seed value.
117131
*

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ static status_t check_internal_state(
110110
}
111111

112112
status_t entropy_csrng_kat(void) {
113-
// Check that the complex is initialized.
114-
TRY(entropy_complex_check());
115-
116113
TRY(entropy_csrng_uninstantiate());
117114

118115
const entropy_seed_material_t kEntropyInput = {

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "sw/device/lib/base/hardened.h"
1111
#include "sw/device/lib/base/hardened_memory.h"
1212
#include "sw/device/lib/base/memory.h"
13-
#include "sw/device/lib/crypto/drivers/entropy.h"
1413
#include "sw/device/lib/crypto/drivers/rv_core_ibex.h"
1514
#include "sw/device/lib/crypto/impl/status.h"
1615
#include "sw/device/lib/crypto/include/integrity.h"
@@ -292,9 +291,6 @@ static void context_save(hmac_ctx_t *ctx) {
292291
* @return Result of the operation.
293292
*/
294293
static status_t hmac_context_wipe(hmac_ctx_t *ctx) {
295-
// Ensure entropy complex is initialized.
296-
HARDENED_TRY(entropy_complex_check());
297-
298294
// Randomize sensitive data.
299295
HARDENED_TRY(hardened_memshred(ctx->key.key_block, kHmacMaxBlockWords));
300296
HARDENED_TRY(hardened_memshred(ctx->H, kHmacMaxDigestWords));
@@ -439,9 +435,6 @@ static status_t oneshot(const uint32_t cfg, const hmac_key_t *key,
439435
// Check that the block is idle.
440436
HARDENED_TRY(ensure_idle());
441437

442-
// Make sure that the entropy complex is configured correctly.
443-
HARDENED_TRY(entropy_complex_check());
444-
445438
// Configure the HMAC block.
446439
abs_mmio_write32(kHmacBaseAddr + HMAC_CFG_REG_OFFSET, cfg);
447440

@@ -774,9 +767,6 @@ hardened_bool_t hmac_key_integrity_checksum_check(const hmac_key_t *key) {
774767
}
775768

776769
status_t hmac_update(hmac_ctx_t *ctx, const otcrypto_const_byte_buf_t *data) {
777-
// Make sure that the entropy complex is configured correctly.
778-
HARDENED_TRY(entropy_complex_check());
779-
780770
// If we don't have enough new bytes to fill a block, just update the partial
781771
// block and return.
782772
size_t block_bytelen = ctx->msg_block_wordlen * sizeof(uint32_t);
@@ -836,9 +826,6 @@ status_t hmac_update(hmac_ctx_t *ctx, const otcrypto_const_byte_buf_t *data) {
836826
}
837827

838828
status_t hmac_final(hmac_ctx_t *ctx, otcrypto_word32_buf_t *digest) {
839-
// Make sure that the entropy complex is configured correctly.
840-
HARDENED_TRY(entropy_complex_check());
841-
842829
// Retore context will restore the context and also hit start or continue
843830
// button as necessary.
844831
HARDENED_TRY(context_restore(ctx));

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "sw/device/lib/base/bitfield.h"
99
#include "sw/device/lib/base/hardened_memory.h"
1010
#include "sw/device/lib/base/memory.h"
11-
#include "sw/device/lib/crypto/drivers/entropy.h"
1211
#include "sw/device/lib/crypto/impl/status.h"
1312

1413
#include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
@@ -167,8 +166,6 @@ static status_t keymgr_wait_until_done(void) {
167166

168167
status_t keymgr_generate_key_sw(keymgr_diversification_t diversification,
169168
keymgr_output_t *key) {
170-
// Ensure that the entropy complex has been initialized and keymgr is idle.
171-
HARDENED_TRY(entropy_complex_check());
172169
HARDENED_TRY(keymgr_is_idle());
173170

174171
// Set the control register to generate a software-visible key.
@@ -197,8 +194,6 @@ status_t keymgr_generate_key_sw(keymgr_diversification_t diversification,
197194
}
198195

199196
status_t keymgr_generate_key_aes(keymgr_diversification_t diversification) {
200-
// Ensure that the entropy complex has been initialized and keymgr is idle.
201-
HARDENED_TRY(entropy_complex_check());
202197
HARDENED_TRY(keymgr_is_idle());
203198

204199
// Set the control register to generate an AES key.
@@ -214,8 +209,6 @@ status_t keymgr_generate_key_aes(keymgr_diversification_t diversification) {
214209
}
215210

216211
status_t keymgr_generate_key_kmac(keymgr_diversification_t diversification) {
217-
// Ensure that the entropy complex has been initialized and keymgr is idle.
218-
HARDENED_TRY(entropy_complex_check());
219212
HARDENED_TRY(keymgr_is_idle());
220213

221214
// Set the control register to generate a KMAC key.
@@ -231,8 +224,6 @@ status_t keymgr_generate_key_kmac(keymgr_diversification_t diversification) {
231224

232225
status_t keymgr_generate_key_otbn(keymgr_diversification_t diversification,
233226
hardened_bool_t attestation) {
234-
// Ensure that the entropy complex has been initialized and keymgr is idle.
235-
HARDENED_TRY(entropy_complex_check());
236227
HARDENED_TRY(keymgr_is_idle());
237228

238229
// Set the control register to generate an OTBN key.
@@ -277,8 +268,6 @@ status_t keymgr_generate_key_otbn(keymgr_diversification_t diversification,
277268
* @param slot Value to write to the SIDELOAD_CLEAR register.
278269
*/
279270
static status_t keymgr_sideload_clear(uint32_t slot) {
280-
// Ensure that the entropy complex has been initialized and keymgr is idle.
281-
HARDENED_TRY(entropy_complex_check());
282271
HARDENED_TRY(keymgr_is_idle());
283272

284273
// Set SIDELOAD_CLEAR to begin continuously clearing the requested slot.

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "sw/device/lib/base/bitfield.h"
99
#include "sw/device/lib/base/hardened_memory.h"
1010
#include "sw/device/lib/base/memory.h"
11-
#include "sw/device/lib/crypto/drivers/entropy.h"
1211
#include "sw/device/lib/crypto/drivers/rv_core_ibex.h"
1312
#include "sw/device/lib/crypto/impl/status.h"
1413
#include "sw/device/lib/crypto/include/integrity.h"
@@ -253,9 +252,6 @@ status_t kmac_key_length_check(size_t key_len) {
253252
}
254253

255254
status_t kmac_hwip_default_configure(void) {
256-
// Ensure that the entropy complex is initialized.
257-
HARDENED_TRY(entropy_complex_check());
258-
259255
uint32_t status_reg = abs_mmio_read32(kKmacBaseAddr + KMAC_STATUS_REG_OFFSET);
260256

261257
// Check that core is not in fault state
@@ -499,12 +495,6 @@ static status_t kmac_init(kmac_operation_t operation,
499495
hardened_bool_t hw_backed) {
500496
HARDENED_TRY(wait_status_bit(KMAC_STATUS_SHA3_IDLE_BIT, 1));
501497

502-
// If the operation is KMAC, ensure that the entropy complex has been
503-
// initialized for masking.
504-
if (operation == kKmacOperationKmac) {
505-
HARDENED_TRY(entropy_complex_check());
506-
}
507-
508498
// We need to preserve some bits of CFG register, such as:
509499
// entropy_mode, entropy_ready etc. On the other hand, some bits
510500
// need to be reset for each invocation.

sw/device/lib/crypto/drivers/mock_entropy.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ status_t entropy_complex_init(void) { return OTCRYPTO_OK; }
2323

2424
status_t entropy_complex_check(void) { return OTCRYPTO_OK; }
2525

26+
status_t entropy_complex_health_test_config_check(void) { return OTCRYPTO_OK; }
27+
2628
status_t entropy_csrng_instantiate(
2729
hardened_bool_t disable_trng_input,
2830
const entropy_seed_material_t *seed_material) {

sw/device/lib/crypto/impl/BUILD

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ cc_library(
3939
"//sw/device/lib/base:hardened_memory",
4040
"//sw/device/lib/base:memory",
4141
"//sw/device/lib/crypto/drivers:aes",
42-
"//sw/device/lib/crypto/drivers:entropy",
4342
"//sw/device/lib/crypto/drivers:rv_core_ibex",
4443
"//sw/device/lib/crypto/include:datatypes",
4544
],
@@ -56,7 +55,6 @@ cc_library(
5655
"//sw/device/lib/base:hardened_memory",
5756
"//sw/device/lib/base:memory",
5857
"//sw/device/lib/crypto/drivers:aes",
59-
"//sw/device/lib/crypto/drivers:entropy",
6058
"//sw/device/lib/crypto/drivers:rv_core_ibex",
6159
"//sw/device/lib/crypto/impl/aes_gcm",
6260
"//sw/device/lib/crypto/include:datatypes",
@@ -107,7 +105,6 @@ cc_library(
107105
":sha2",
108106
":status",
109107
"//sw/device/lib/base:hardened_memory",
110-
"//sw/device/lib/crypto/drivers:entropy",
111108
"//sw/device/lib/crypto/impl/ecc:curve25519",
112109
"//sw/device/lib/crypto/include:datatypes",
113110
],
@@ -121,7 +118,6 @@ cc_library(
121118
deps = [
122119
":config",
123120
":keyblob",
124-
"//sw/device/lib/crypto/drivers:entropy",
125121
"//sw/device/lib/crypto/drivers:hmac",
126122
"//sw/device/lib/crypto/impl/ecc:p256",
127123
"//sw/device/lib/crypto/include:datatypes",
@@ -136,7 +132,6 @@ cc_library(
136132
deps = [
137133
":config",
138134
":keyblob",
139-
"//sw/device/lib/crypto/drivers:entropy",
140135
"//sw/device/lib/crypto/drivers:hmac",
141136
"//sw/device/lib/crypto/impl/ecc:p384",
142137
"//sw/device/lib/crypto/include:datatypes",
@@ -193,7 +188,6 @@ cc_library(
193188
":status",
194189
"//sw/device/lib/base:hardened_memory",
195190
"//sw/device/lib/base:math",
196-
"//sw/device/lib/crypto/drivers:entropy",
197191
"//sw/device/lib/crypto/drivers:keymgr",
198192
"//sw/device/lib/crypto/drivers:rv_core_ibex",
199193
"//sw/device/lib/crypto/include:datatypes",
@@ -274,6 +268,7 @@ cc_library(
274268
srcs = ["config.c"],
275269
hdrs = ["//sw/device/lib/crypto/include:config.h"],
276270
deps = [
271+
":entropy_src",
277272
":integrity",
278273
":status",
279274
"//hw/top_earlgrey/ip_autogen/clkmgr:clkmgr_c_regs",
@@ -371,7 +366,6 @@ cc_library(
371366
":sha2",
372367
"//sw/device/lib/base:hardened",
373368
"//sw/device/lib/base:hardened_memory",
374-
"//sw/device/lib/crypto/drivers:entropy",
375369
"//sw/device/lib/crypto/drivers:hmac",
376370
"//sw/device/lib/crypto/drivers:rv_core_ibex",
377371
],

0 commit comments

Comments
 (0)