Software APIs
entropy_src_ast_rng_req_test.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/test_framework/check.h"
12 
13 #include "hw/top_earlgrey/sw/autogen/top_earlgrey.h" // Generated.
14 
15 OTTF_DEFINE_TEST_CONFIG();
16 
17 enum {
18  /**
19  * The size of the buffer used in firmware to process the entropy bits in
20  * firmware override mode.
21  */
22  kEntropyFifoBufferSize = 16,
23 };
24 
25 static uint32_t read_fifo_depth(dif_entropy_src_t *entropy) {
26  uint32_t fifo_depth = 0;
27  CHECK_DIF_OK(dif_entropy_src_get_fifo_depth(entropy, &fifo_depth));
28  return fifo_depth;
29 }
30 
31 bool test_main(void) {
32  dif_entropy_src_t entropy_src;
33  CHECK_DIF_OK(dif_entropy_src_init(
35 
36  CHECK_DIF_OK(dif_entropy_src_set_enabled(&entropy_src, kDifToggleDisabled));
37 
38  const dif_entropy_src_fw_override_config_t fw_override_config = {
39  .entropy_insert_enable = true,
40  .buffer_threshold = kEntropyFifoBufferSize,
41  };
43  &entropy_src, fw_override_config, kDifToggleEnabled));
44 
45  // Program the entropy src in normal RNG mode.
46  const dif_entropy_src_config_t config = {
47  .fips_enable = true,
48  .fips_flag = true,
49  .rng_fips = true,
50  // Route the entropy data received from RNG to the FIFO.
51  .route_to_firmware = true,
52  .single_bit_mode = kDifEntropySrcSingleBitModeDisabled,
53  .health_test_threshold_scope = false, /*default*/
54  .health_test_window_size = 0x0200, /*default*/
55  .alert_threshold = 2, /*default*/
56  };
57  CHECK_DIF_OK(
58  dif_entropy_src_configure(&entropy_src, config, kDifToggleEnabled));
59 
60  // Verify that the FIFO depth is non-zero via SW - indicating the reception of
61  // data over the AST RNG interface.
62  IBEX_SPIN_FOR(read_fifo_depth(&entropy_src) > 0, 6000);
63 
64  return true;
65 }