Software APIs
entropy_src_smoketest.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 
10 #include "sw/device/lib/testing/entropy_src_testutils.h"
11 #include "sw/device/lib/testing/entropy_testutils.h"
12 #include "sw/device/lib/testing/test_framework/check.h"
14 
16 
17 OTTF_DEFINE_TEST_CONFIG();
18 
19 const size_t kEntropyDataChecks = 10;
20 
21 bool test_main(void) {
22  dif_entropy_src_t entropy_src;
23  CHECK_DIF_OK(dif_entropy_src_init(
25 
26  // Disable entropy for test purpose, as it has been turned on by ROM
27  CHECK_DIF_OK(dif_entropy_src_set_enabled(&entropy_src, kDifToggleDisabled));
28 
29  // Setup fips grade entropy that can be read by firmware
30  const dif_entropy_src_config_t config = {
31  .fips_enable = true,
32  .fips_flag = true,
33  .rng_fips = true,
34  .route_to_firmware = true,
35  .single_bit_mode = kDifEntropySrcSingleBitModeDisabled,
36  .health_test_threshold_scope = false, /*default*/
37  .health_test_window_size = 0x0200, /*default*/
38  .alert_threshold = 2, /*default*/
39  };
40 
41  // Re-enable entropy src
42  CHECK_DIF_OK(
43  dif_entropy_src_configure(&entropy_src, config, kDifToggleEnabled));
44 
45  // ensure health tests are actually running
46  CHECK_STATUS_OK(entropy_src_testutils_wait_for_state(
47  &entropy_src, kDifEntropySrcMainFsmStateContHTRunning));
48 
49  uint32_t entropy_data;
50  uint32_t last_entropy_data = 0;
51  for (uint32_t i = 0; i < kEntropyDataChecks; ++i) {
52  // wait for entropy to become available and read
53  while (dif_entropy_src_non_blocking_read(&entropy_src, &entropy_data) !=
54  kDifOk)
55  ;
56  if (entropy_data == last_entropy_data) {
57  LOG_FATAL("Received duplicate entropy, this should never happen");
58  } else {
59  last_entropy_data = entropy_data;
60  }
61  }
62 
63  uint32_t errors;
64  CHECK_DIF_OK(dif_entropy_src_get_errors(&entropy_src, &errors));
65  if (errors != 0) {
66  LOG_FATAL("Error is non-zero, 0x%h", errors);
67  } else {
68  return true;
69  }
70 
71  return false;
72 }