Software APIs
entropy_src_testutils.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 #include "sw/device/lib/testing/entropy_src_testutils.h"
5 
7 #include "sw/device/lib/testing/test_framework/check.h"
8 
9 #define MODULE_ID MAKE_MODULE_ID('e', 'n', 's')
10 
11 status_t entropy_src_testutils_fw_override_enable(
12  dif_entropy_src_t *entropy_src, uint8_t buffer_threshold,
13  bool route_to_firmware, bool bypass_conditioner) {
14  const dif_entropy_src_fw_override_config_t fw_override_config = {
15  .entropy_insert_enable = true,
16  .buffer_threshold = buffer_threshold,
17  };
18  TRY(dif_entropy_src_fw_override_configure(entropy_src, fw_override_config,
20 
21  const dif_entropy_src_config_t config = {
22  .fips_enable = true,
23  .fips_flag = true,
24  .rng_fips = true,
25  .route_to_firmware = route_to_firmware,
26  .bypass_conditioner = bypass_conditioner,
27  .single_bit_mode = kDifEntropySrcSingleBitModeDisabled,
28  .health_test_threshold_scope = false,
29  .health_test_window_size = 0x0200,
30  .alert_threshold = 2,
31  };
32  TRY(dif_entropy_src_configure(entropy_src, config, kDifToggleEnabled));
33  return OK_STATUS();
34 }
35 
36 status_t entropy_src_testutils_wait_for_state(
37  const dif_entropy_src_t *entropy_src, dif_entropy_src_main_fsm_t state) {
39 
40  do {
41  TRY(dif_entropy_src_get_main_fsm_state(entropy_src, &cur_state));
42  } while (cur_state != state);
43  return OK_STATUS();
44 }
45 
46 status_t entropy_src_testutils_drain_observe_fifo(
47  dif_entropy_src_t *entropy_src) {
48  // This value is arbitrary, it could be 1 but since there is some
49  // overhead in dif_entropy_src_observe_fifo_nonblocking_read, it's better
50  // to read several words every time to drain the FIFO quickly.
51  const size_t kDrainCount = 32;
52  size_t len;
53  // Read from the FIFO until we get a short read which means that the FIFO was
54  // emptied.
55  do {
56  len = kDrainCount;
57  TRY(dif_entropy_src_observe_fifo_nonblocking_read(entropy_src, NULL, &len));
58  } while (len == kDrainCount);
59 
60  return OK_STATUS();
61 }
62 
63 status_t entropy_src_testutils_disable_health_tests(
64  dif_entropy_src_t *entropy_src) {
65  static dif_entropy_src_test_t kHealthTest[] = {
70  for (size_t i = 0; i < ARRAYSIZE(kHealthTest); i++) {
72  entropy_src,
73  (dif_entropy_src_health_test_config_t){.test_type = kHealthTest[i],
74  .high_threshold = 0xffffffff,
75  .low_threshold = 0}));
76  }
77 
78  return OK_STATUS();
79 }