Skip to content

Commit baca7bf

Browse files
siemen11nasahlpa
authored andcommitted
[coverage/drivers] Add comments to unreached lines
Add comments explaining why testing of crypto did not reach lines in drivers. Signed-off-by: Siemen Dhooghe <sdhooghe@google.com>
1 parent d68ec75 commit baca7bf

6 files changed

Lines changed: 58 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ static status_t aes_begin(aes_key_t key, const aes_block_t *iv,
276276
// Check that AES is ready to receive input data.
277277
uint32_t status = abs_mmio_read32(kBase + AES_STATUS_REG_OFFSET);
278278
if (!bitfield_bit32_read(status, AES_STATUS_INPUT_READY_BIT)) {
279+
// COVERAGE (HW ERR) This line checks whether the AES HW is ready to receive
280+
// input but after spin_until(AES_STATUS_IDLE_BIT), hence it will always be
281+
// ready unless there is a HW error.
279282
return OTCRYPTO_RECOV_ERR;
280283
}
281284
HARDENED_CHECK_EQ(launder32(bitfield_bit32_read(

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,8 @@ static status_t csrng_send_app_cmd(uint32_t base_address,
449449
launder32(cmd_type_used) | kEntropyCsrngSendAppCmdTypeEdnRes;
450450
break;
451451
default:
452+
// COVERAGE (SW ERR) This is an internal function, the cmd_type given will
453+
// always be from these cases.
452454
return OTCRYPTO_BAD_ARGS;
453455
}
454456
// Check if we landed in the correct case statement. Use ORs for this to
@@ -465,6 +467,7 @@ static status_t csrng_send_app_cmd(uint32_t base_address,
465467
} while (!ready && --timeout);
466468

467469
if (timeout == 0) {
470+
// COVERAGE (HW ERR) The timeout should only happen with a HW error.
468471
return OTCRYPTO_RECOV_ERR;
469472
}
470473
}
@@ -519,6 +522,7 @@ static status_t csrng_send_app_cmd(uint32_t base_address,
519522
} while (!ready && --timeout);
520523

521524
if (timeout == 0) {
525+
// COVERAGE (HW ERR) The timeout should only happen with a HW error.
522526
return OTCRYPTO_RECOV_ERR;
523527
}
524528
}
@@ -537,6 +541,7 @@ static status_t csrng_send_app_cmd(uint32_t base_address,
537541
--timeout);
538542

539543
if (timeout == 0) {
544+
// COVERAGE (HW ERR) The timeout should only happen with a HW error.
540545
return OTCRYPTO_RECOV_ERR;
541546
}
542547

@@ -552,12 +557,14 @@ static status_t csrng_send_app_cmd(uint32_t base_address,
552557
--timeout);
553558

554559
if (timeout == 0) {
560+
// COVERAGE (HW ERR) The timeout should only happen with a HW error.
555561
return OTCRYPTO_RECOV_ERR;
556562
}
557563

558-
// Check the "status" bit, which will be 0 unless there was an error.
559564
reg = abs_mmio_read32(kBaseCsrng + CSRNG_SW_CMD_STS_REG_OFFSET);
560565
if (bitfield_field32_read(reg, CSRNG_SW_CMD_STS_CMD_STS_FIELD)) {
566+
// COVERAGE (HW ERR) The status bit will be 0 unless there was a HW
567+
// error.
561568
return OTCRYPTO_RECOV_ERR;
562569
}
563570
}
@@ -575,11 +582,13 @@ static status_t csrng_send_app_cmd(uint32_t base_address,
575582
--timeout);
576583

577584
if (timeout == 0) {
585+
// COVERAGE (HW ERR) The timeout should only happen with a HW error.
578586
return OTCRYPTO_RECOV_ERR;
579587
}
580588

581-
// Check the "status" bit, which will be 0 unless there was an error.
582589
if (bitfield_field32_read(reg, CSRNG_SW_CMD_STS_CMD_STS_FIELD)) {
590+
// COVERAGE (HW ERR) The status bit will be 0 unless there was a HW
591+
// error.
583592
return OTCRYPTO_RECOV_ERR;
584593
}
585594
}
@@ -655,10 +664,12 @@ static status_t edn_ready_block(uint32_t edn_address) {
655664
} while (!bitfield_bit32_read(reg, EDN_SW_CMD_STS_CMD_RDY_BIT) && --timeout);
656665

657666
if (timeout == 0) {
667+
// COVERAGE (HW ERR) The timeout should only happen with a HW error.
658668
return OTCRYPTO_RECOV_ERR;
659669
}
660670

661671
if (bitfield_field32_read(reg, CSRNG_SW_CMD_STS_CMD_STS_FIELD)) {
672+
// COVERAGE (HW ERR) The status bit will be 0 unless there was a HW error.
662673
return OTCRYPTO_RECOV_ERR;
663674
}
664675
return OTCRYPTO_OK;
@@ -771,6 +782,7 @@ static status_t entropy_src_configure(const entropy_src_config_t *config) {
771782
if (config->bypass_conditioner != kMultiBitBool4False) {
772783
HARDENED_CHECK_NE(config->bypass_conditioner, kMultiBitBool4False);
773784
// Bypassing the conditioner is not supported.
785+
// COVERAGE (SW ERR) The configs provided in the code do not support bypass.
774786
return OTCRYPTO_BAD_ARGS;
775787
}
776788

@@ -870,6 +882,8 @@ static status_t entropy_src_configure(const entropy_src_config_t *config) {
870882
return OTCRYPTO_RECOV_ERR; \
871883
} \
872884
} while (false);
885+
// COVERAGE (HW ERR) This is only reached if the registers from the RNG were
886+
// faulted.
873887

874888
/**
875889
* Check the entropy_src configuration.
@@ -888,13 +902,16 @@ static status_t entropy_src_check(const entropy_src_config_t *config) {
888902
config->route_to_firmware != kMultiBitBool4False) {
889903
// This check only supports FIPS-compatible configurations which do not
890904
// bypass the conditioner or route to firmware.
905+
// COVERAGE (SW ERR) This code does not support bypass.
891906
return OTCRYPTO_BAD_ARGS;
892907
}
893908

894909
// Check that entropy_src is enabled.
895910
uint32_t reg =
896911
abs_mmio_read32(kBaseEntropySrc + ENTROPY_SRC_MODULE_ENABLE_REG_OFFSET);
897912
if (reg != kMultiBitBool4True) {
913+
// COVERAGE (HW ERR) This is only reached when the HW was not enabled before
914+
// the check.
898915
return OTCRYPTO_RECOV_ERR;
899916
}
900917

@@ -912,6 +929,8 @@ static status_t entropy_src_check(const entropy_src_config_t *config) {
912929
bitfield_field32_read(reg, ENTROPY_SRC_CONF_RNG_BIT_ENABLE_FIELD);
913930
if (conf_fips_enable != kMultiBitBool4True ||
914931
conf_rng_bit_enable != kMultiBitBool4False) {
932+
// COVERAGE (SW ERR) This is only reached when the RNG was not set in FIPS
933+
// mode, but we only support FIPS mode.
915934
return OTCRYPTO_RECOV_ERR;
916935
}
917936
reg =
@@ -922,6 +941,8 @@ static status_t entropy_src_check(const entropy_src_config_t *config) {
922941
bitfield_field32_read(reg, ENTROPY_SRC_ENTROPY_CONTROL_ES_ROUTE_FIELD);
923942
if (control_es_type != kMultiBitBool4False ||
924943
control_es_route != kMultiBitBool4False) {
944+
// COVERAGE (SW ERR) We only support configurations which set ES TYPE and
945+
// ROUTE to true.
925946
return OTCRYPTO_RECOV_ERR;
926947
}
927948

@@ -931,6 +952,7 @@ static status_t entropy_src_check(const entropy_src_config_t *config) {
931952
if (bitfield_field32_read(
932953
reg, ENTROPY_SRC_HEALTH_TEST_WINDOWS_FIPS_WINDOW_FIELD) !=
933954
config->fips_test_window_size) {
955+
// COVERAGE (SW ERR) We only support a single test window size.
934956
return OTCRYPTO_RECOV_ERR;
935957
}
936958

@@ -943,6 +965,7 @@ static status_t entropy_src_check(const entropy_src_config_t *config) {
943965
~(uint32_t)config->alert_threshold);
944966
if (exp_reg != abs_mmio_read32(kBaseEntropySrc +
945967
ENTROPY_SRC_ALERT_THRESHOLD_REG_OFFSET)) {
968+
// COVERAGE (SW ERR) We only support a single alert threshold.
946969
return OTCRYPTO_RECOV_ERR;
947970
}
948971

@@ -1013,6 +1036,7 @@ status_t entropy_complex_init(hardened_bool_t fips) {
10131036
if (launder32(config->id) != ((fips == kHardenedBoolFalse)
10141037
? kEntropyComplexConfigIdContinuous
10151038
: kEntropyComplexConfigIdFipsContinuous)) {
1039+
// COVERAGE (SW ERR) We only support FIPS mode.
10161040
return OTCRYPTO_RECOV_ERR;
10171041
}
10181042

@@ -1034,6 +1058,7 @@ status_t entropy_complex_check(hardened_bool_t fips) {
10341058
if (launder32(config->id) != ((fips == kHardenedBoolFalse)
10351059
? kEntropyComplexConfigIdContinuous
10361060
: kEntropyComplexConfigIdFipsContinuous)) {
1061+
// COVERAGE (SW ERR) We only support FIPS mode.
10371062
return OTCRYPTO_RECOV_ERR;
10381063
}
10391064

@@ -1068,6 +1093,7 @@ status_t entropy_complex_health_test_config_check(hardened_bool_t fips) {
10681093
// Check recoverable alerts
10691094
if (abs_mmio_read32(kBaseEntropySrc +
10701095
ENTROPY_SRC_RECOV_ALERT_STS_REG_OFFSET) != 0) {
1096+
// COVERAGE (HW ERR) This is only reached when we detect a HW alert.
10711097
return OTCRYPTO_RECOV_ERR;
10721098
}
10731099

@@ -1150,13 +1176,15 @@ status_t entropy_csrng_generate_data_get(uint32_t *buf, size_t len,
11501176
--timeout);
11511177

11521178
if (timeout == 0) {
1179+
// COVERAGE (HW ERR) The timeout should only happen with a HW error.
11531180
return OTCRYPTO_RECOV_ERR;
11541181
}
11551182

11561183
if (fips_check != kHardenedBoolFalse &&
11571184
!bitfield_bit32_read(reg, CSRNG_GENBITS_VLD_GENBITS_FIPS_BIT)) {
11581185
// Entropy isn't FIPS-compatible, so we should return an error when
11591186
// done. However, we still need to read the result to clear CSRNG's FIFO.
1187+
// COVERAGE (SW ERR) We only support FIPS mode.
11601188
res = OTCRYPTO_RECOV_ERR;
11611189
}
11621190

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ static status_t hmac_idle_wait(void) {
140140
uint32_t intr_state =
141141
abs_mmio_read32(kHmacBaseAddr + HMAC_INTR_STATE_REG_OFFSET);
142142
if (bitfield_bit32_read(intr_state, HMAC_INTR_STATE_HMAC_DONE_BIT) == 0) {
143+
// COVERAGE (HW ERR) Only reached if the HMAC has a HW error.
143144
return OTCRYPTO_FATAL_ERR;
144145
}
145146

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ static status_t keymgr_wait_until_done(void) {
117117
}
118118

119119
// Should be unreachable.
120+
// COVERAGE (FI CM) Only reached if there is a fault.
120121
HARDENED_TRAP();
121122
return OTCRYPTO_FATAL_ERR;
122123
}
@@ -235,6 +236,7 @@ status_t keymgr_generate_key_otbn(keymgr_diversification_t diversification,
235236
WRITE_CTRL(OTBN, GENERATE_HW, true);
236237
break;
237238
default:
239+
// COVERAGE (FI CM) Only reached if there is a fault.
238240
HARDENED_TRAP();
239241
return OTCRYPTO_FATAL_ERR;
240242
}
@@ -251,6 +253,7 @@ status_t keymgr_generate_key_otbn(keymgr_diversification_t diversification,
251253
VERIFY_CTRL(OTBN, GENERATE_HW, true);
252254
break;
253255
default:
256+
// COVERAGE (FI CM) Only reached if there is a fault.
254257
HARDENED_TRAP();
255258
return OTCRYPTO_FATAL_ERR;
256259
}
@@ -280,6 +283,7 @@ static status_t keymgr_sideload_clear(uint32_t slot) {
280283
abs_mmio_read32(kBaseAddr + KEYMGR_SIDELOAD_CLEAR_REG_OFFSET);
281284
if (bitfield_field32_read(sideload_clear, KEYMGR_SIDELOAD_CLEAR_VAL_FIELD) !=
282285
slot) {
286+
// COVERAGE (FI CM) Only reached if there is a fault.
283287
return OTCRYPTO_FATAL_ERR;
284288
}
285289

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ static status_t kmac_get_keccak_rate_words(kmac_security_str_t security_str,
193193
security_str_set = launder32(security_str_set) | kKmacSecurityStrength512;
194194
break;
195195
default:
196+
// COVERAGE (SW ERR) This is an internal function, we only provide valid
197+
// inputs to it, hence the default case should not be reached.
196198
return OTCRYPTO_BAD_ARGS;
197199
}
198200
// Check if we landed in the correct case statement. Use ORs for this to
@@ -354,15 +356,19 @@ status_t kmac_hwip_default_configure(void) {
354356
OT_WARN_UNUSED_RESULT
355357
static status_t wait_status_bit(uint32_t bit_position, bool bit_value) {
356358
if (bit_position > 31) {
359+
// COVERAGE (SW ERR) This is an internal function, we only provide valid
360+
// inputs to it, hence the if case should not be reached.
357361
return OTCRYPTO_BAD_ARGS;
358362
}
359363

360364
while (true) {
361365
uint32_t reg = abs_mmio_read32(kKmacBaseAddr + KMAC_STATUS_REG_OFFSET);
362366
if (bitfield_bit32_read(reg, KMAC_STATUS_ALERT_FATAL_FAULT_BIT)) {
367+
// COVERAGE (HW ERR) This is only reached if KMAC raises an alert.
363368
return OTCRYPTO_FATAL_ERR;
364369
}
365370
if (bitfield_bit32_read(reg, KMAC_STATUS_ALERT_RECOV_CTRL_UPDATE_ERR_BIT)) {
371+
// COVERAGE (HW ERR) This is only reached if KMAC raises an alert.
366372
return OTCRYPTO_RECOV_ERR;
367373
}
368374
if (bitfield_bit32_read(reg, bit_position) == bit_value) {
@@ -510,6 +516,8 @@ static status_t kmac_init(kmac_operation_t operation,
510516
} else if (hw_backed == kHardenedBoolFalse) {
511517
cfg_reg = bitfield_bit32_write(cfg_reg, KMAC_CFG_SHADOWED_SIDELOAD_BIT, 0);
512518
} else {
519+
// COVERAGE (SW ERR) This internal function is only given valid encodings of
520+
// hw_backed.
513521
return OTCRYPTO_BAD_ARGS;
514522
};
515523

@@ -548,6 +556,8 @@ static status_t kmac_write_key_block(kmac_blinded_key_t *key) {
548556
return OTCRYPTO_OK;
549557
} else if (launder32(key->hw_backed) != kHardenedBoolFalse) {
550558
// Invalid value.
559+
// COVERAGE (SW ERR) This is only reached with a bad encoding of
560+
// key->hw_backed.
551561
return OTCRYPTO_BAD_ARGS;
552562
}
553563
HARDENED_CHECK_EQ(key->hw_backed, kHardenedBoolFalse);
@@ -647,6 +657,8 @@ static status_t kmac_process_msg_blocks(
647657
if (operation == kKmacOperationKmac) {
648658
uint32_t digest_len_bits = 8 * sizeof(uint32_t) * digest_len_words;
649659
if (digest_len_bits / (8 * sizeof(uint32_t)) != digest_len_words) {
660+
// COVERAGE (SW ERR) This is an internal function, we only provide it
661+
// valid inputs.
650662
return OTCRYPTO_BAD_ARGS;
651663
}
652664

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,15 @@ static status_t check_offset_len(uint32_t offset_bytes, size_t num_words,
8080
uint32_t num_bytes = num_words * sizeof(uint32_t);
8181

8282
if (offset_bytes > UINT32_MAX - num_bytes) {
83+
// COVERAGE (SW ERR) This is an internal function, we only provide it valid
84+
// inputs.
8385
return OTCRYPTO_BAD_ARGS;
8486
}
8587
uint32_t adjusted_offset_bytes = offset_bytes + num_bytes;
8688

8789
if (adjusted_offset_bytes > mem_size) {
90+
// COVERAGE (SW ERR) This is an internal function, we only provide it valid
91+
// inputs.
8892
return OTCRYPTO_BAD_ARGS;
8993
}
9094

@@ -228,11 +232,13 @@ status_t otbn_busy_wait_for_done(void) {
228232
// If OTBN is idle (not locked), then return a recoverable error.
229233
if (launder32(status) == kOtbnStatusIdle) {
230234
HARDENED_CHECK_EQ(status, kOtbnStatusIdle);
235+
// COVERAGE (HW ERR) This requires the OTBN to reach an error.
231236
return OTCRYPTO_RECOV_ERR;
232237
}
233238

234239
// OTBN is locked; return a fatal error.
235240
HARDENED_CHECK_EQ(status, kOtbnStatusLocked);
241+
// COVERAGE (HW ERR) This is only reached when the OTBN has a HW error.
236242
return OTCRYPTO_FATAL_ERR;
237243
}
238244

@@ -290,6 +296,8 @@ status_t otbn_set_ctrl_software_errs_fatal(bool enable) {
290296
static status_t check_app_address_ranges(const otbn_app_t *app) {
291297
// IMEM must not be backwards or empty.
292298
if (app->imem_end <= app->imem_start) {
299+
// COVERAGE (SW ERR) This is an internal function, we only provide it valid
300+
// inputs.
293301
return OTCRYPTO_BAD_ARGS;
294302
}
295303
HARDENED_CHECK_LT(app->imem_start, app->imem_end);

0 commit comments

Comments
 (0)