8 #include "sw/device/lib/crypto/drivers/kmac.h"
9 #include "sw/device/lib/crypto/impl/integrity.h"
10 #include "sw/device/lib/crypto/impl/keyblob.h"
11 #include "sw/device/lib/crypto/impl/status.h"
15 #define MODULE_ID MAKE_MODULE_ID('k', 'k', 'd')
23 if (key_derivation_key.keyblob == NULL || output_key_material == NULL ||
24 output_key_material->keyblob == NULL) {
25 return OTCRYPTO_BAD_ARGS;
29 if (label.data == NULL && label.len != 0) {
30 return OTCRYPTO_BAD_ARGS;
34 if (label.len > kKmacCustStrMaxSize) {
35 return OTCRYPTO_BAD_ARGS;
39 if (context.data == NULL && context.len != 0) {
40 return OTCRYPTO_BAD_ARGS;
45 return OTCRYPTO_BAD_ARGS;
50 HARDENED_TRY(kmac_key_length_check(key_derivation_key.config.key_length));
55 .hw_backed = key_derivation_key.config.hw_backed,
56 .len = key_derivation_key.config.key_length,
63 if (keyblob_share_num_words(key_derivation_key.config) *
sizeof(uint32_t) !=
64 kKmacSideloadKeyLength / 8) {
65 return OTCRYPTO_BAD_ARGS;
72 HARDENED_TRY(keyblob_to_keymgr_diversification(&key_derivation_key,
74 HARDENED_TRY(keymgr_generate_key_kmac(diversification));
76 if (key_derivation_key.keyblob_length !=
77 keyblob_num_words(key_derivation_key.config) *
sizeof(uint32_t)) {
78 return OTCRYPTO_BAD_ARGS;
80 HARDENED_TRY(keyblob_to_shares(&key_derivation_key, &kmac_key.share0,
83 return OTCRYPTO_BAD_ARGS;
87 if (output_key_material->config.key_length %
sizeof(uint32_t) != 0) {
88 return OTCRYPTO_NOT_IMPLEMENTED;
93 return OTCRYPTO_BAD_ARGS;
98 if (output_key_material->keyblob_length !=
99 keyblob_num_words(output_key_material->config) *
sizeof(uint32_t)) {
100 return OTCRYPTO_BAD_ARGS;
103 switch (launder32(key_derivation_key.config.key_mode)) {
104 case kOtcryptoKeyModeKdfKmac128: {
106 kOtcryptoKeyModeKdfKmac128);
109 HARDENED_TRY(kmac_kmac_128(
111 context.len, label.data, label.len, output_key_material->keyblob,
112 output_key_material->config.key_length /
sizeof(uint32_t)));
115 case kOtcryptoKeyModeKdfKmac256: {
117 kOtcryptoKeyModeKdfKmac256);
120 if (key_derivation_key.config.key_length < 256 / 8) {
121 return OTCRYPTO_BAD_ARGS;
123 HARDENED_TRY(kmac_kmac_256(
125 context.len, label.data, label.len, output_key_material->keyblob,
126 output_key_material->config.key_length /
sizeof(uint32_t)));
130 return OTCRYPTO_BAD_ARGS;
133 output_key_material->checksum =
134 integrity_blinded_checksum(output_key_material);
137 return keymgr_sideload_clear_kmac();