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"
8 #include "sw/device/lib/crypto/impl/keyblob.h"
14 #include "sw/device/lib/testing/keymgr_testutils.h"
15 #include "sw/device/lib/testing/test_framework/check.h"
18 #define MODULE_ID MAKE_MODULE_ID('t', 's', 't')
25 typedef enum kdf_test_operation_t {
26 kKdfTestOperationKmac128,
27 kKdfTestOperationKmac256,
28 } kdf_test_operation_t;
31 char *vector_identifier;
32 kdf_test_operation_t test_operation;
44 typedef enum kmac_test_operation_t {
45 kKmacTestOperationCshake,
46 kKmacTestOperationShake,
47 kKmacTestOperationSha3,
48 kKmacTestOperationKmac,
49 } kmac_test_operation_t;
52 char *vector_identifier;
53 kmac_test_operation_t test_operation;
54 size_t security_strength;
66 .vector_identifier =
"Manually edited KDF-KMAC sample #1",
67 .test_operation = kKdfTestOperationKmac128,
72 .key_mode = kOtcryptoKeyModeKdfKmac128,
75 .security_level = kOtcryptoKeySecurityLevelLow,
105 0xdc, 0xd5, 0x70, 0x1f, 0x22, 0x83, 0x53,
106 0xa9, 0xc7, 0x3a, 0xe6, 0x72, 0x65, 0xb4,
107 0xa5, 0x55, 0x84, 0xa0, 0x4f, 0x3d, 0x62,
108 0x2b, 0xac, 0x32, 0x69, 0x8f, 0xb0, 0xf3,
124 .vector_identifier =
"Manually edited KDF-KMAC sample #2",
125 .test_operation = kKdfTestOperationKmac128,
126 .key_derivation_key =
130 .key_mode = kOtcryptoKeyModeKdfKmac128,
133 .security_level = kOtcryptoKeySecurityLevelHigh,
136 .keyblob_length = 32,
206 .vector_identifier =
"Manually edited KDF-KMAC sample #3",
207 .test_operation = kKdfTestOperationKmac256,
208 .key_derivation_key =
212 .key_mode = kOtcryptoKeyModeKdfKmac256,
215 .security_level = kOtcryptoKeySecurityLevelHigh,
218 .keyblob_length = 32,
235 0x6e, 0xa6, 0x87, 0x76, 0x6e, 0xac, 0xfb,
236 0x9c, 0xf0, 0x5e, 0x91, 0x5f, 0xfc, 0xeb,
237 0x62, 0x44, 0x51, 0x77, 0x23,
245 0x07, 0x32, 0xa7, 0x2f, 0x55, 0xff, 0xd6, 0xdd,
246 0x5e, 0x83, 0x38, 0xad, 0x50, 0xba, 0xa5, 0x01,
247 0x36, 0x80, 0x9c, 0x56, 0x53, 0xb6, 0x80,
268 "NIST CAVP, byte-oriented, SHA3_224ShortMsg.rsp, Len = 8",
269 .test_operation = kKmacTestOperationSha3,
270 .security_strength = 224,
293 0x48, 0x82, 0x86, 0xd9, 0xd3, 0x27, 0x16, 0xe5, 0x88, 0x1e,
294 0xa1, 0xee, 0x51, 0xf3, 0x6d, 0x36, 0x60, 0xd7, 0x0f, 0x0d,
295 0xb0, 0x3b, 0x3f, 0x61, 0x2c, 0xe9, 0xed, 0xa4,
311 switch (security_strength) {
313 *mode = kOtcryptoHashModeSha3_224;
316 *mode = kOtcryptoHashModeSha3_256;
319 *mode = kOtcryptoHashModeSha3_384;
322 *mode = kOtcryptoHashModeSha3_512;
325 LOG_INFO(
"Invalid size for SHA3: %d bits", security_strength);
326 return INVALID_ARGUMENT();
337 status_t get_kmac_mode(kdf_test_operation_t test_operation,
339 switch (test_operation) {
340 case kKdfTestOperationKmac128:
341 *mode = kOtcryptoKmacModeKmac128;
343 case kKdfTestOperationKmac256:
344 *mode = kOtcryptoKmacModeKmac256;
347 LOG_INFO(
"Invalid test operation is given for KDF-KMAC");
348 return INVALID_ARGUMENT();
356 static status_t run_test_vector(
void) {
359 size_t km_key_len = current_test_vector->
keying_material.config.key_length;
360 size_t km_keyblob_len =
362 uint32_t km_buffer1[km_keyblob_len];
363 uint32_t km_buffer2[km_keyblob_len];
369 TRY(get_kmac_mode(current_test_vector->test_operation, &mode));
374 .key_mode = kOtcryptoKeyModeKdfKmac128,
375 .key_length = km_key_len,
377 .security_level = kOtcryptoKeySecurityLevelHigh,
383 .keyblob = km_buffer1,
384 .keyblob_length = km_keyblob_len *
sizeof(uint32_t),
389 .keyblob = km_buffer2,
390 .keyblob_length = km_keyblob_len *
sizeof(uint32_t),
393 size_t digest_num_words = sha3_test_vector.digest.len /
sizeof(uint32_t);
394 if (sha3_test_vector.digest.len %
sizeof(uint32_t) != 0) {
397 uint32_t digest[digest_num_words];
404 LOG_INFO(
"Running the first KDF-KMAC sideload operation.");
407 current_test_vector->context, km_key_len, &keying_material1));
410 LOG_INFO(
"Running the intermediate SHA3 operation.");
411 TRY(get_sha3_mode(sha3_test_vector.security_strength, &digest_buf.mode));
414 LOG_INFO(
"Running the second KDF-KMAC sideload operation for comparison.");
417 current_test_vector->context, km_key_len, &keying_material2));
419 TRY_CHECK_ARRAYS_EQ((
unsigned char *)keying_material1.keyblob,
420 (
unsigned char *)keying_material2.keyblob, km_key_len);
424 OTTF_DEFINE_TEST_CONFIG();
429 CHECK_STATUS_OK(keymgr_testutils_initialize(&keymgr, &kmac));
431 const char *state_name;
432 CHECK_STATUS_OK(keymgr_testutils_state_string_get(&keymgr, &state_name));
434 LOG_INFO(
"Keymgr entered %s State", state_name);
435 LOG_INFO(
"Testing cryptolib KDF-KMAC driver with sideloaded key.");
438 CHECK_STATUS_OK(entropy_complex_init());
439 CHECK_STATUS_OK(kmac_hwip_default_configure());
442 for (
size_t i = 0; i <
ARRAYSIZE(kKdfTestVectors); i++) {
444 current_test_vector = &kKdfTestVectors[i];
446 LOG_INFO(
"Running test %d of %d, test vector identifier: %s", i + 1,
448 current_test_vector->vector_identifier);
451 return status_ok(test_result);