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/keymgr_testutils.h"
13 #include "sw/device/lib/testing/test_framework/check.h"
16 #define MODULE_ID MAKE_MODULE_ID('t', 's', 't')
23 typedef enum kmac_test_operation_t {
24 kKmacTestOperationCshake,
25 kKmacTestOperationShake,
26 kKmacTestOperationSha3,
27 kKmacTestOperationKmac,
28 } kmac_test_operation_t;
31 char *vector_identifier;
32 kmac_test_operation_t test_operation;
33 size_t security_strength;
45 .vector_identifier =
"Manually edited sample #1",
46 .security_strength = 128,
51 .key_mode = kOtcryptoKeyModeKmac128,
52 .key_length = kKmacSideloadKeyLength / 8,
55 .security_level = kOtcryptoKeySecurityLevelHigh,
74 0xf5, 0xb1, 0x65, 0x22, 0x4a, 0x58, 0xb7, 0x91,
75 0xdf, 0x6a, 0xf1, 0xd8, 0x30, 0x3e, 0x61, 0xcd,
76 0xc4, 0xbb, 0x86, 0xc3, 0xd1, 0xc4, 0x27, 0x10,
77 0x3c, 0x34, 0x4c, 0x41, 0x89, 0xeb, 0x2f, 0x1e,
85 0x7b, 0xd5, 0xd4, 0x7e, 0x44, 0x6f, 0xce, 0xc2,
86 0xa3, 0xd8, 0x11, 0x73, 0x61, 0x10, 0xe5, 0x78,
87 0x1b, 0xcc, 0xce, 0xa6, 0x96, 0x76, 0x2e, 0x61,
88 0x16, 0xc6, 0xe9, 0xc9, 0x2d, 0x99, 0xbf, 0x35,
100 .vector_identifier =
"Manually edited sample #2",
101 .security_strength = 256,
106 .key_mode = kOtcryptoKeyModeKmac256,
107 .key_length = kKmacSideloadKeyLength / 8,
110 .security_level = kOtcryptoKeySecurityLevelHigh,
112 .keyblob_length = 32,
179 .vector_identifier =
"Manually edited sample #3",
180 .security_strength = 128,
185 .key_mode = kOtcryptoKeyModeKmac128,
186 .key_length = kKmacSideloadKeyLength / 8,
189 .security_level = kOtcryptoKeySecurityLevelHigh,
191 .keyblob_length = 32,
206 .data = (uint8_t[]){0xf5, 0xb1, 0x65, 0x22},
211 .data = (uint8_t[]){0x7b, 0xd5, 0xd4, 0x7e},
227 "NIST CAVP, byte-oriented, SHA3_224ShortMsg.rsp, Len = 8",
228 .test_operation = kKmacTestOperationSha3,
229 .security_strength = 224,
252 0x48, 0x82, 0x86, 0xd9, 0xd3, 0x27, 0x16, 0xe5, 0x88, 0x1e,
253 0xa1, 0xee, 0x51, 0xf3, 0x6d, 0x36, 0x60, 0xd7, 0x0f, 0x0d,
254 0xb0, 0x3b, 0x3f, 0x61, 0x2c, 0xe9, 0xed, 0xa4,
270 switch (security_strength) {
272 *mode = kOtcryptoHashModeSha3_224;
275 *mode = kOtcryptoHashModeSha3_256;
278 *mode = kOtcryptoHashModeSha3_384;
281 *mode = kOtcryptoHashModeSha3_512;
284 LOG_INFO(
"Invalid size for SHA3: %d bits", security_strength);
285 return INVALID_ARGUMENT();
297 switch (security_strength) {
299 *mode = kOtcryptoKmacModeKmac128;
302 *mode = kOtcryptoKmacModeKmac256;
305 LOG_INFO(
"Invalid size for KMAC: %d bits", security_strength);
306 return INVALID_ARGUMENT();
314 static status_t run_test_vector(
void) {
315 size_t digest_num_words = current_test_vector->digest.len /
sizeof(uint32_t);
316 if (current_test_vector->digest.len %
sizeof(uint32_t) != 0) {
319 uint32_t digest1[digest_num_words];
320 uint32_t digest2[digest_num_words];
322 current_test_vector->key.checksum =
323 integrity_blinded_checksum(¤t_test_vector->key);
326 TRY(get_kmac_mode(current_test_vector->security_strength, &mode));
337 digest_num_words = sha3_test_vector.digest.len /
sizeof(uint32_t);
338 if (sha3_test_vector.digest.len %
sizeof(uint32_t) != 0) {
341 uint32_t digest3[digest_num_words];
348 LOG_INFO(
"Running the first KMAC sideload operation.");
349 TRY(
otcrypto_kmac(¤t_test_vector->key, current_test_vector->input_msg,
350 mode, current_test_vector->cust_str,
351 current_test_vector->digest.len, tag_buf1));
354 LOG_INFO(
"Running the intermediate SHA3 operation.");
355 TRY(get_sha3_mode(sha3_test_vector.security_strength, &digest_buf.mode));
358 LOG_INFO(
"Running the second KMAC sideload operation for comparison.");
359 TRY(
otcrypto_kmac(¤t_test_vector->key, current_test_vector->input_msg,
360 mode, current_test_vector->cust_str,
361 current_test_vector->digest.len, tag_buf2));
363 TRY_CHECK_ARRAYS_EQ((
unsigned char *)tag_buf1.data,
364 (
unsigned char *)tag_buf2.data,
365 current_test_vector->digest.len);
369 OTTF_DEFINE_TEST_CONFIG();
374 CHECK_STATUS_OK(keymgr_testutils_initialize(&keymgr, &kmac));
376 const char *state_name;
377 CHECK_STATUS_OK(keymgr_testutils_state_string_get(&keymgr, &state_name));
379 LOG_INFO(
"Keymgr entered %s State", state_name);
380 LOG_INFO(
"Testing cryptolib KMAC driver with sideloaded key.");
383 CHECK_STATUS_OK(entropy_complex_init());
384 CHECK_STATUS_OK(kmac_hwip_default_configure());
387 for (
size_t i = 0; i <
ARRAYSIZE(kKmacTestVectors); i++) {
389 current_test_vector = &kKmacTestVectors[i];
391 LOG_INFO(
"Running test %d of %d, test vector identifier: %s", i + 1,
393 current_test_vector->vector_identifier);
396 return status_ok(test_result);