Software APIs
csrng_testutils.h
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
5#ifndef OPENTITAN_SW_DEVICE_LIB_TESTING_CSRNG_TESTUTILS_H_
6#define OPENTITAN_SW_DEVICE_LIB_TESTING_CSRNG_TESTUTILS_H_
7
8#include "sw/device/lib/base/status.h"
11
12/**
13 * Generates randomized seed material.
14 *
15 * @param disable_rand If set, the generated seed material has length zero.
16 * @param seed_material The seed material to generate.
17 */
19status_t csrng_testutils_seed_material_build(
20 bool disable_rand, dif_csrng_seed_material_t *seed_material);
21
22/**
23 * Returns a randomized CSRNG application command.
24 *
25 * @param disable_rand If set, a struct containing default values is returned.
26 * @param id CSRNG application command to generate.
27 * @param entropy_src_en If set, Instantiate and Reseed commands use entropy
28 * from the entropy source.
29 * @param glen_val If > 0, the generate length is set to glen_val.
30 * @param seed_material The seed material to generate and use for the generated
31 * application command.
32 * @return The generated CSRNG application command.
33 */
35csrng_app_cmd_t csrng_testutils_app_cmd_build(
36 bool disable_rand, csrng_app_cmd_id_t id,
37 dif_csrng_entropy_src_toggle_t entropy_src_en, unsigned int glen_val,
38 dif_csrng_seed_material_t *seed_material);
39
40/**
41 * Wait for the `csrng` instance command interface to be ready to accept
42 * commands. Aborts test execution if an error is found.
43 */
45status_t csrng_testutils_cmd_ready_wait(const dif_csrng_t *csrng);
46
47/**
48 * Runs CSRNG generate command.
49 *
50 * @param csrng A CSRNG handle.
51 * @param additional_data Additional data, set to NULL if unused.
52 * @param output Output buffer.
53 * @param output_len Number of words of entropy to write to output buffer.
54 */
56status_t csrng_testutils_cmd_generate_run(
57 const dif_csrng_t *csrng, const dif_csrng_seed_material_t *additional_data,
58 uint32_t *output, size_t output_len);
59
60/**
61 * Checks the CSRNG internal state against `expected` values.
62 *
63 * @param csrng A CSRNG handle.
64 * @param expected Expected CSRNG internal state.
65 */
67status_t csrng_testutils_check_internal_state(
68 const dif_csrng_t *csrng, const dif_csrng_internal_state_t *expected);
69
70/**
71 * CTR_DRBG Known-Answer-Test (KAT) for INSTANTIATE command.
72 *
73 * @param csrng A CSRNG handle.
74 * @param fail_expected Expected fail.
75 * @param seed_material Seed material to use for the command.
76 * @param expected_state Expected CSRNG internal state after the command.
77 */
79status_t csrng_testutils_kat_instantiate(
80 const dif_csrng_t *csrng, bool fail_expected,
81 const dif_csrng_seed_material_t *seed_material,
82 const dif_csrng_internal_state_t *expected_state);
83
84/**
85 * CTR_DRBG Known-Answer-Test (KAT) for GENERATE command.
86 *
87 * @param csrng A CSRNG handle.
88 * @param num_generates Number of GENERATE commands to run.
89 * @param output_len Number of output words to read from CSRNG after the last
90 * command.
91 * @param additional_data Additional data, set to NULL if unused.
92 * @param expected_output Expected CSRNG output after the last command.
93 * @param expected_state Expected CSRNG internal state after the last command.
94 */
96status_t csrng_testutils_kat_generate(
97 const dif_csrng_t *csrng, size_t num_generates, size_t output_len,
98 const dif_csrng_seed_material_t *additional_data,
99 const uint32_t *expected_output,
100 const dif_csrng_internal_state_t *expected_state);
101
102/**
103 * CTR_DRBG Known-Answer-Test (KAT) for RESEED command.
104 *
105 * @param csrng A CSRNG handle.
106 * @param seed_material Seed material to use for the command.
107 * @param expected_state Expected CSRNG internal state after the command.
108 */
110status_t csrng_testutils_kat_reseed(
111 const dif_csrng_t *csrng, const dif_csrng_seed_material_t *seed_material,
112 const dif_csrng_internal_state_t *expected_state);
113
114/**
115 * CTR DRBG Known-Answer-Test (KAT) for INSTANTIATE command for a NIST test
116 * vector without additional data.
117 *
118 * @param csrng Handle.
119 * @param fail_expected Expected fail.
120 */
122status_t csrng_testutils_fips_instantiate_kat(const dif_csrng_t *csrng,
123 bool fail_expected);
124
125/**
126 * CTR DRBG Known-Answer-Test (KAT) for INSTANTIATE command for a NIST test
127 * vector using additional data for the GENERATE command.
128 *
129 * @param csrng Handle.
130 * @param fail_expected Expected fail.
131 */
133status_t csrng_testutils_fips_instantiate_kat_adata(const dif_csrng_t *csrng,
134 bool fail_expected);
135
136/**
137 * CTR DRBG Known-Answer-Test (KAT) for GENERATE command for a NIST test
138 * vector without additional data.
139 *
140 * @param csrng Handle.
141 */
143status_t csrng_testutils_fips_generate_kat(const dif_csrng_t *csrng);
144
145/**
146 * CTR DRBG Known-Answer-Test (KAT) for GENERATE command for a NIST test
147 * vector with additional data, first call.
148 *
149 * @param csrng Handle.
150 */
152status_t csrng_testutils_fips_generate_kat_adata1(const dif_csrng_t *csrng);
153
154/**
155 * CTR DRBG Known-Answer-Test (KAT) for GENERATE command for a NIST test
156 * vector with additional data, second call.
157 *
158 * @param csrng Handle.
159 */
161status_t csrng_testutils_fips_generate_kat_adata2(const dif_csrng_t *csrng);
162
163/**
164 * Checks CSRNG command status.
165 *
166 * Asserts error if the command or internal FIFO status contains any errors.
167 *
168 * @param csrng Handle.
169 */
171status_t csrng_testutils_cmd_status_check(const dif_csrng_t *csrng);
172
173/**
174 * Checks CSRNG recoverable alerts.
175 *
176 * Asserts error if there are any CSRNG recoverable alerts set.
177 *
178 * @param csrng Handle.
179 */
181status_t csrng_testutils_recoverable_alerts_check(const dif_csrng_t *csrng);
182
183#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_CSRNG_TESTUTILS_H_