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();
104 switch (security_strength) {
106 *mode = kOtcryptoKmacModeKmac128;
109 *mode = kOtcryptoKmacModeKmac256;
112 LOG_INFO(
"Invalid size for KMAC: %d bits", security_strength);
113 return INVALID_ARGUMENT();
121 static status_t run_test_vector(
void) {
122 size_t digest_num_words = current_test_vector->digest.len /
sizeof(uint32_t);
123 if (current_test_vector->digest.len %
sizeof(uint32_t) != 0) {
126 uint32_t digest[digest_num_words];
129 .len = digest_num_words,
132 switch (current_test_vector->test_operation) {
133 case kKmacTestOperationShake: {
134 TRY(get_shake_mode(current_test_vector->security_strength,
139 case kKmacTestOperationCshake: {
140 TRY(get_cshake_mode(current_test_vector->security_strength,
143 current_test_vector->func_name,
144 current_test_vector->cust_str, digest_buf));
147 case kKmacTestOperationSha3: {
148 TRY(get_sha3_mode(current_test_vector->security_strength,
150 TRY(
otcrypto_hash(current_test_vector->input_msg, digest_buf));
153 case kKmacTestOperationKmac: {
154 current_test_vector->key.checksum =
155 integrity_blinded_checksum(¤t_test_vector->key);
157 TRY(get_kmac_mode(current_test_vector->security_strength, &mode));
159 .data = digest_buf.data,
160 .len = digest_buf.len,
163 current_test_vector->input_msg, mode,
164 current_test_vector->cust_str,
165 current_test_vector->digest.len, tag_buf));
169 LOG_INFO(
"Unrecognized `operation` field: 0x%04x",
170 current_test_vector->test_operation);
171 return INVALID_ARGUMENT();
175 TRY_CHECK_ARRAYS_EQ((
unsigned char *)digest_buf.data,
176 current_test_vector->digest.data,
177 current_test_vector->digest.len);
181 OTTF_DEFINE_TEST_CONFIG();
183 LOG_INFO(
"Testing cryptolib KMAC driver.");
186 CHECK_STATUS_OK(entropy_complex_init());
187 CHECK_STATUS_OK(kmac_hwip_default_configure());
190 for (
size_t i = 0; i <
ARRAYSIZE(kKmacTestVectors); i++) {
191 current_test_vector = &kKmacTestVectors[i];
192 LOG_INFO(
"Running test %d of %d, test vector identifier: %s", i + 1,
194 current_test_vector->vector_identifier);
197 return status_ok(test_result);