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" 
   13 #include "sw/device/lib/testing/keymgr_testutils.h" 
   14 #include "sw/device/lib/testing/test_framework/check.h" 
   17 #define MODULE_ID MAKE_MODULE_ID('t', 's', 't') 
   24 typedef enum kdf_test_operation_t {
 
   25   kKdfTestOperationKmac128,
 
   26   kKdfTestOperationKmac256,
 
   27 } kdf_test_operation_t;
 
   30   char *vector_identifier;
 
   31   kdf_test_operation_t test_operation;
 
   43 typedef enum kmac_test_operation_t {
 
   44   kKmacTestOperationCshake,
 
   45   kKmacTestOperationShake,
 
   46   kKmacTestOperationSha3,
 
   47   kKmacTestOperationKmac,
 
   48 } kmac_test_operation_t;
 
   51   char *vector_identifier;
 
   52   kmac_test_operation_t test_operation;
 
   53   size_t security_strength;
 
   65         .vector_identifier = 
"Manually edited KDF-KMAC sample #1",
 
   66         .test_operation = kKdfTestOperationKmac128,
 
   71                         .key_mode = kOtcryptoKeyModeKdfKmac128,
 
   74                         .security_level = kOtcryptoKeySecurityLevelLow,
 
  104                         0xdc, 0xd5, 0x70, 0x1f, 0x22, 0x83, 0x53,
 
  105                         0xa9, 0xc7, 0x3a, 0xe6, 0x72, 0x65, 0xb4,
 
  106                         0xa5, 0x55, 0x84, 0xa0, 0x4f, 0x3d, 0x62,
 
  107                         0x2b, 0xac, 0x32, 0x69, 0x8f, 0xb0, 0xf3,
 
  123         .vector_identifier = 
"Manually edited KDF-KMAC sample #2",
 
  124         .test_operation = kKdfTestOperationKmac128,
 
  125         .key_derivation_key =
 
  129                         .key_mode = kOtcryptoKeyModeKdfKmac128,
 
  132                         .security_level = kOtcryptoKeySecurityLevelHigh,
 
  135                 .keyblob_length = 32,
 
  205         .vector_identifier = 
"Manually edited KDF-KMAC sample #3",
 
  206         .test_operation = kKdfTestOperationKmac256,
 
  207         .key_derivation_key =
 
  211                         .key_mode = kOtcryptoKeyModeKdfKmac256,
 
  214                         .security_level = kOtcryptoKeySecurityLevelHigh,
 
  217                 .keyblob_length = 32,
 
  234                         0x6e, 0xa6, 0x87, 0x76, 0x6e, 0xac, 0xfb,
 
  235                         0x9c, 0xf0, 0x5e, 0x91, 0x5f, 0xfc, 0xeb,
 
  236                         0x62, 0x44, 0x51, 0x77, 0x23,
 
  244                         0x07, 0x32, 0xa7, 0x2f, 0x55, 0xff, 0xd6, 0xdd,
 
  245                         0x5e, 0x83, 0x38, 0xad, 0x50, 0xba, 0xa5, 0x01,
 
  246                         0x36, 0x80, 0x9c, 0x56, 0x53, 0xb6, 0x80,
 
  267         "NIST CAVP, byte-oriented, SHA3_224ShortMsg.rsp, Len = 8",
 
  268     .test_operation = kKmacTestOperationSha3,
 
  269     .security_strength = 224,
 
  292                     0x48, 0x82, 0x86, 0xd9, 0xd3, 0x27, 0x16, 0xe5, 0x88, 0x1e,
 
  293                     0xa1, 0xee, 0x51, 0xf3, 0x6d, 0x36, 0x60, 0xd7, 0x0f, 0x0d,
 
  294                     0xb0, 0x3b, 0x3f, 0x61, 0x2c, 0xe9, 0xed, 0xa4,
 
  310   switch (security_strength) {
 
  312       *mode = kOtcryptoHashModeSha3_224;
 
  315       *mode = kOtcryptoHashModeSha3_256;
 
  318       *mode = kOtcryptoHashModeSha3_384;
 
  321       *mode = kOtcryptoHashModeSha3_512;
 
  324       LOG_INFO(
"Invalid size for SHA3: %d bits", security_strength);
 
  325       return INVALID_ARGUMENT();
 
  333 static status_t run_test_vector(
void) {
 
  336   size_t km_key_len = current_test_vector->
keying_material.config.key_length;
 
  337   size_t km_keyblob_len =
 
  339   uint32_t km_buffer1[km_keyblob_len];
 
  340   uint32_t km_buffer2[km_keyblob_len];
 
  348       .key_mode = kOtcryptoKeyModeKdfKmac128,
 
  349       .key_length = km_key_len,
 
  351       .security_level = kOtcryptoKeySecurityLevelHigh,
 
  357       .keyblob = km_buffer1,
 
  358       .keyblob_length = km_keyblob_len * 
sizeof(uint32_t),
 
  363       .keyblob = km_buffer2,
 
  364       .keyblob_length = km_keyblob_len * 
sizeof(uint32_t),
 
  367   size_t digest_num_words = sha3_test_vector.digest.len / 
sizeof(uint32_t);
 
  368   if (sha3_test_vector.digest.len % 
sizeof(uint32_t) != 0) {
 
  371   uint32_t digest[digest_num_words];
 
  378   LOG_INFO(
"Running the first KDF-KMAC sideload operation.");
 
  380                         current_test_vector->label,
 
  381                         current_test_vector->context, &keying_material1));
 
  384   LOG_INFO(
"Running the intermediate SHA3 operation.");
 
  385   TRY(get_sha3_mode(sha3_test_vector.security_strength, &digest_buf.mode));
 
  388   LOG_INFO(
"Running the second KDF-KMAC sideload operation for comparison.");
 
  390                         current_test_vector->label,
 
  391                         current_test_vector->context, &keying_material2));
 
  393   TRY_CHECK_ARRAYS_EQ((
unsigned char *)keying_material1.keyblob,
 
  394                       (
unsigned char *)keying_material2.keyblob, km_key_len);
 
  398 OTTF_DEFINE_TEST_CONFIG();
 
  403   CHECK_STATUS_OK(keymgr_testutils_initialize(&keymgr, &kmac));
 
  405   const char *state_name;
 
  406   CHECK_STATUS_OK(keymgr_testutils_state_string_get(&keymgr, &state_name));
 
  408   LOG_INFO(
"Keymgr entered %s State", state_name);
 
  409   LOG_INFO(
"Testing cryptolib KDF-KMAC driver with sideloaded key.");
 
  412   CHECK_STATUS_OK(entropy_complex_init());
 
  413   CHECK_STATUS_OK(kmac_hwip_default_configure());
 
  416   for (
size_t i = 0; i < 
ARRAYSIZE(kKdfTestVectors); i++) {
 
  418     current_test_vector = &kKdfTestVectors[i];
 
  420     LOG_INFO(
"Running test %d of %d, test vector identifier: %s", i + 1,
 
  422              current_test_vector->vector_identifier);
 
  425   return status_ok(test_result);