Software APIs
csrng_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 
5 #include "dt/dt_csrng.h"
10 #include "sw/device/lib/testing/csrng_testutils.h"
11 #include "sw/device/lib/testing/test_framework/check.h"
13 
14 static_assert(kDtCsrngCount >= 1,
15  "This test requires at least one CSRNG instance");
16 
17 static const dt_csrng_t kTestCsrng = (dt_csrng_t)0;
18 
19 OTTF_DEFINE_TEST_CONFIG();
20 
21 enum {
22  kExpectedOutputLen = 16,
23 };
24 
25 /**
26  * Run CTR DRBG Known-Answer-Tests (KATs).
27  *
28  * This is a simplified version of csrng_kat_test. It skips CSRNG internal
29  * state checks to optimize runtime.
30  */
31 status_t test_ctr_drbg_ctr0_smoke(const dif_csrng_t *csrng) {
32  CHECK_DIF_OK(dif_csrng_uninstantiate(csrng));
33 
34  const dif_csrng_seed_material_t kEntropyInput = {
35  .seed_material = {0x73bec010, 0x9262474c, 0x16a30f76, 0x531b51de,
36  0x2ee494e5, 0xdfec9db3, 0xcb7a879d, 0x5600419c,
37  0xca79b0b0, 0xdda33b5c, 0xa468649e, 0xdf5d73fa},
38  .seed_material_len = 12,
39  };
40  TRY(csrng_testutils_cmd_ready_wait(csrng));
42  &kEntropyInput));
43 
44  uint32_t got[kExpectedOutputLen];
45  TRY(csrng_testutils_cmd_generate_run(csrng, got, kExpectedOutputLen));
46  TRY(csrng_testutils_cmd_generate_run(csrng, got, kExpectedOutputLen));
47 
48  const uint32_t kExpectedOutput[kExpectedOutputLen] = {
49  0xe48bb8cb, 0x1012c84c, 0x5af8a7f1, 0xd1c07cd9, 0xdf82ab22, 0x771c619b,
50  0xd40fccb1, 0x87189e99, 0x510494b3, 0x64f7ac0c, 0x2581f391, 0x80b1dc2f,
51  0x793e01c5, 0x87b107ae, 0xdb17514c, 0xa43c41b7,
52  };
53  CHECK_ARRAYS_EQ(got, kExpectedOutput, kExpectedOutputLen,
54  "Generate command KAT output mismatch");
55  return OK_STATUS();
56 }
57 
58 bool test_main(void) {
59  dif_csrng_t csrng;
60  CHECK_DIF_OK(dif_csrng_init_from_dt(kTestCsrng, &csrng));
61  CHECK_DIF_OK(dif_csrng_configure(&csrng));
62  CHECK_STATUS_OK(test_ctr_drbg_ctr0_smoke(&csrng));
63  return true;
64 }