Software APIs
kmac_mode_cshake_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 
5 #include "dt/dt_kmac.h" // Generated
11 #include "sw/device/lib/testing/test_framework/check.h"
13 
14 OTTF_DEFINE_TEST_CONFIG();
15 
16 #define DIGEST_LEN_CSHAKE_MAX 4
17 
18 /**
19  * cSHAKE test description.
20  */
21 typedef struct cshake_test {
23 
24  const char *message;
25  size_t message_len;
26 
27  const char *function_name;
28  size_t function_name_len;
29 
30  const char *customization_string;
31  size_t customization_string_len;
32 
33  const uint32_t digest[DIGEST_LEN_CSHAKE_MAX];
34  size_t digest_len;
36 
37 /**
38  * cSHAKE tests.
39  */
40 const cshake_test_t cshake_tests[] = {
41  {
43  .message = "OpenTitan",
44  .message_len = 9,
45  .function_name = "",
46  .function_name_len = 0,
47  .customization_string = "",
48  .customization_string_len = 0,
49  .digest = {0x235a6522, 0x3bd735ac, 0x77832247, 0xc6b12919},
50  .digest_len = 4, // Rate (r) is 42 words.
51  },
52  {
54  .message = "OpenTitan",
55  .message_len = 9,
56  .function_name = "A",
57  .function_name_len = 1,
58  .customization_string = "",
59  .customization_string_len = 0,
60  .digest = {0xf2f20928, 0xa2a59a0, 0xfc1e5d5d, 0x1cee38d0},
61  .digest_len = 4, // Rate (r) is 42 words.
62  },
63  {
65  .message = "OpenTitan",
66  .message_len = 9,
67  .function_name = "",
68  .function_name_len = 0,
69  .customization_string = "Ibex",
70  .customization_string_len = 4,
71  .digest = {0xcd582d56, 0x59e88860, 0xa4344c29, 0x5576778f},
72  .digest_len = 4, // Rate (r) is 34 words.
73  },
74  {
76  .message = "OpenTitan",
77  .message_len = 9,
78  .function_name = "Ibex",
79  .function_name_len = 4,
80  .customization_string =
81  "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
82  "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
83  .customization_string_len = 32,
84  .digest = {0xda353307, 0xdf18e570, 0x6211cee0, 0x716e816c},
85  .digest_len = 4, // Rate (r) is 34 words.
86  },
87 };
88 
89 bool test_main(void) {
90  LOG_INFO("Running KMAC DIF cSHAKE test...");
91 
92  // Intialize KMAC hardware.
93  dif_kmac_t kmac;
94  dt_kmac_t kKmacDt = (dt_kmac_t)0;
95  static_assert(kDtKmacCount >= 1,
96  "This test requires at least one KMAC instance");
97  dif_kmac_operation_state_t kmac_operation_state;
98  CHECK_DIF_OK(dif_kmac_init_from_dt(kKmacDt, &kmac));
99 
100  // Configure KMAC hardware using software entropy.
102  .entropy_mode = kDifKmacEntropyModeSoftware,
103  .entropy_seed = {0xb153e3fe, 0x09596819, 0x3e85a6e8, 0xb6dcdaba,
104  0x50dc409c, 0x11e1ebd1},
105  .entropy_fast_process = kDifToggleEnabled,
106  };
107  CHECK_DIF_OK(dif_kmac_configure(&kmac, config));
108 
109  // Run cSHAKE test cases using single blocking absorb/squeeze operations.
110  for (int i = 0; i < ARRAYSIZE(cshake_tests); ++i) {
111  cshake_test_t test = cshake_tests[i];
112 
114  CHECK_DIF_OK(dif_kmac_function_name_init(test.function_name,
115  test.function_name_len, &n));
116 
119  test.customization_string, test.customization_string_len, &s));
120 
121  // Use NULL for empty strings to exercise that code path.
122  dif_kmac_function_name_t *np = test.function_name_len == 0 ? NULL : &n;
124  test.customization_string_len == 0 ? NULL : &s;
125 
126  CHECK_DIF_OK(dif_kmac_mode_cshake_start(&kmac, &kmac_operation_state,
127  test.mode, np, sp));
128  CHECK_DIF_OK(dif_kmac_absorb(&kmac, &kmac_operation_state, test.message,
129  test.message_len, NULL));
130  uint32_t out[DIGEST_LEN_CSHAKE_MAX];
131  CHECK(DIGEST_LEN_CSHAKE_MAX >= test.digest_len);
132  CHECK_DIF_OK(dif_kmac_squeeze(&kmac, &kmac_operation_state, out,
133  test.digest_len, /*processed=*/NULL,
134  /*capacity=*/NULL));
135  CHECK_DIF_OK(dif_kmac_end(&kmac, &kmac_operation_state));
136 
137  for (int j = 0; j < test.digest_len; ++j) {
138  CHECK(out[j] == test.digest[j],
139  "test %d: mismatch at %d got=0x%x want=0x%x", i, j, out[j],
140  test.digest[j]);
141  }
142  }
143 
144  return true;
145 }