5 #include "sw/device/lib/crypto/drivers/entropy.h"
6 #include "sw/device/lib/crypto/drivers/kmac.h"
7 #include "sw/device/lib/crypto/impl/integrity.h"
12 #include "sw/device/lib/testing/test_framework/check.h"
19 #include "kmac_testvectors.h"
21 #define MODULE_ID MAKE_MODULE_ID('t', 's', 't')
33 switch (security_strength) {
35 *mode = kOtcryptoHashXofModeShake128;
38 *mode = kOtcryptoHashXofModeShake256;
41 LOG_INFO(
"Invalid security strength for SHAKE: %d bits",
43 return INVALID_ARGUMENT();
55 switch (security_strength) {
57 *mode = kOtcryptoHashXofModeCshake128;
60 *mode = kOtcryptoHashXofModeCshake256;
63 LOG_INFO(
"Invalid security strength for cSHAKE: %d bits",
65 return INVALID_ARGUMENT();
77 switch (security_strength) {
79 *mode = kOtcryptoHashModeSha3_224;
82 *mode = kOtcryptoHashModeSha3_256;
85 *mode = kOtcryptoHashModeSha3_384;
88 *mode = kOtcryptoHashModeSha3_512;
91 LOG_INFO(
"Invalid size for SHA3: %d bits", security_strength);
92 return INVALID_ARGUMENT();
100 static status_t run_test_vector(
void) {
101 size_t digest_num_words = current_test_vector->digest.len /
sizeof(uint32_t);
102 if (current_test_vector->digest.len %
sizeof(uint32_t) != 0) {
105 uint32_t digest[digest_num_words];
108 .len = digest_num_words,
111 switch (current_test_vector->test_operation) {
112 case kKmacTestOperationShake: {
113 TRY(get_shake_mode(current_test_vector->security_strength,
118 case kKmacTestOperationCshake: {
119 TRY(get_cshake_mode(current_test_vector->security_strength,
122 current_test_vector->func_name,
123 current_test_vector->cust_str, digest_buf));
126 case kKmacTestOperationSha3: {
127 TRY(get_sha3_mode(current_test_vector->security_strength,
129 TRY(
otcrypto_hash(current_test_vector->input_msg, digest_buf));
132 case kKmacTestOperationKmac: {
133 current_test_vector->key.checksum =
134 integrity_blinded_checksum(¤t_test_vector->key);
136 .data = digest_buf.data,
137 .len = digest_buf.len,
140 current_test_vector->input_msg,
141 current_test_vector->cust_str,
142 current_test_vector->digest.len, tag_buf));
146 LOG_INFO(
"Unrecognized `operation` field: 0x%04x",
147 current_test_vector->test_operation);
148 return INVALID_ARGUMENT();
152 TRY_CHECK_ARRAYS_EQ((
unsigned char *)digest_buf.data,
153 current_test_vector->digest.data,
154 current_test_vector->digest.len);
158 OTTF_DEFINE_TEST_CONFIG();
160 LOG_INFO(
"Testing cryptolib KMAC driver.");
163 CHECK_STATUS_OK(entropy_complex_init());
164 CHECK_STATUS_OK(kmac_hwip_default_configure());
167 for (
size_t i = 0; i <
ARRAYSIZE(kKmacTestVectors); i++) {
168 current_test_vector = &kKmacTestVectors[i];
169 LOG_INFO(
"Running test %d of %d, test vector identifier: %s", i + 1,
171 current_test_vector->vector_identifier);
174 return status_ok(test_result);