8 #include "sw/device/lib/crypto/drivers/hmac.h"
9 #include "sw/device/lib/crypto/drivers/kmac.h"
10 #include "sw/device/lib/crypto/impl/integrity.h"
11 #include "sw/device/lib/crypto/impl/keyblob.h"
12 #include "sw/device/lib/crypto/impl/status.h"
16 #define MODULE_ID MAKE_MODULE_ID('m', 'a', 'c')
23 "`otcrypto_hash_context_t` must be big enough to hold `hmac_ctx_t`.");
28 static_assert(
sizeof(
hmac_ctx_t) %
sizeof(uint32_t) == 0,
29 "Size of `hmac_ctx_t` must be a multiple of the word size for "
30 "`hardened_memcpy()`");
77 hmac_mode_t *hmac_mode,
size_t *block_size) {
79 case kOtcryptoKeyModeHmacSha256:
80 *hmac_mode = kHmacModeHmac256;
81 *block_size = kHmacSha256BlockWords;
83 case kOtcryptoKeyModeHmacSha384:
84 *hmac_mode = kHmacModeHmac384;
86 *block_size = kHmacSha512BlockWords;
88 case kOtcryptoKeyModeHmacSha512:
89 *hmac_mode = kHmacModeHmac512;
90 *block_size = kHmacSha512BlockWords;
93 return OTCRYPTO_BAD_ARGS;
113 uint32_t *processed_key) {
117 switch (key->config.key_mode) {
118 case kOtcryptoKeyModeHmacSha256:
119 hash_mode = kOtcryptoHashModeSha256;
120 block_size = kHmacSha256BlockWords;
121 digest_size = kHmacSha256DigestWords;
123 case kOtcryptoKeyModeHmacSha384:
124 hash_mode = kOtcryptoHashModeSha384;
126 block_size = kHmacSha512BlockWords;
127 digest_size = kHmacSha384DigestWords;
129 case kOtcryptoKeyModeHmacSha512:
130 hash_mode = kOtcryptoHashModeSha512;
131 block_size = kHmacSha512BlockWords;
132 digest_size = kHmacSha512DigestWords;
135 return OTCRYPTO_BAD_ARGS;
139 size_t unmasked_key_len = keyblob_share_num_words(key->config);
140 uint32_t unmasked_key[unmasked_key_len];
141 HARDENED_TRY(keyblob_key_unmask(key, unmasked_key_len, unmasked_key));
145 memset(processed_key, 0, block_size *
sizeof(uint32_t));
148 if (key->config.key_length > block_size *
sizeof(uint32_t)) {
151 .data = processed_key,
155 .len = key->config.key_length,
156 .data = (
unsigned char *)unmasked_key,
163 size_t offset = key->config.key_length %
sizeof(uint32_t);
165 unsigned char *key_end_ptr =
166 (
unsigned char *)&processed_key[unmasked_key_len];
167 size_t num_zero_bytes =
sizeof(uint32_t) - offset;
168 memset(key_end_ptr - num_zero_bytes, 0, num_zero_bytes);
179 if (key == NULL || key->keyblob == NULL || tag.data == NULL) {
180 return OTCRYPTO_BAD_ARGS;
186 return OTCRYPTO_NOT_IMPLEMENTED;
188 if (key->config.security_level != kOtcryptoKeySecurityLevelLow) {
189 return OTCRYPTO_NOT_IMPLEMENTED;
193 if (input_message.data == NULL && input_message.len != 0) {
194 return OTCRYPTO_BAD_ARGS;
197 hmac_mode_t hmac_mode;
199 HARDENED_TRY(get_hmac_mode(key->config.key_mode, &hmac_mode, &block_size));
201 uint32_t processed_key[block_size];
202 HARDENED_TRY(hmac_key_process(key, processed_key));
204 return hmac(hmac_mode, processed_key, block_size, input_message.data,
205 input_message.len, tag.data, tag.len);
213 size_t required_output_len,
218 if (key == NULL || key->keyblob == NULL || tag.data == NULL) {
219 return OTCRYPTO_BAD_ARGS;
223 if (input_message.data == NULL && input_message.len != 0) {
224 return OTCRYPTO_BAD_ARGS;
228 if (customization_string.data == NULL && customization_string.len != 0) {
229 return OTCRYPTO_BAD_ARGS;
233 if (required_output_len != tag.len *
sizeof(uint32_t) ||
234 required_output_len == 0) {
235 return OTCRYPTO_BAD_ARGS;
238 size_t key_len = keyblob_share_num_words(key->config) *
sizeof(uint32_t);
241 HARDENED_TRY(kmac_key_length_check(key_len));
245 return OTCRYPTO_BAD_ARGS;
251 .hw_backed = key->config.hw_backed,
256 if (key_len != kKmacSideloadKeyLength / 8) {
257 return OTCRYPTO_BAD_ARGS;
264 HARDENED_TRY(keyblob_to_keymgr_diversification(key, &diversification));
265 HARDENED_TRY(keymgr_generate_key_kmac(diversification));
268 if (key->keyblob_length != 2 * key->config.key_length) {
269 return OTCRYPTO_BAD_ARGS;
271 HARDENED_TRY(keyblob_to_shares(key, &kmac_key.share0, &kmac_key.share1));
273 return OTCRYPTO_BAD_ARGS;
277 case kOtcryptoKmacModeKmac128:
279 if (key->config.key_mode != kOtcryptoKeyModeKmac128) {
280 return OTCRYPTO_BAD_ARGS;
282 HARDENED_TRY(kmac_kmac_128(&kmac_key, input_message.data,
283 input_message.len, customization_string.data,
284 customization_string.len, tag.data, tag.len));
286 case kOtcryptoKmacModeKmac256:
288 if (key->config.key_mode != kOtcryptoKeyModeKmac256) {
289 return OTCRYPTO_BAD_ARGS;
292 HARDENED_TRY(kmac_kmac_256(&kmac_key, input_message.data,
293 input_message.len, customization_string.data,
294 customization_string.len, tag.data, tag.len));
297 return OTCRYPTO_BAD_ARGS;
301 HARDENED_TRY(keymgr_sideload_clear_kmac());
303 return OTCRYPTO_BAD_ARGS;
311 if (ctx == NULL || key == NULL || key->keyblob == NULL) {
312 return OTCRYPTO_BAD_ARGS;
316 return OTCRYPTO_NOT_IMPLEMENTED;
318 if (key->config.security_level != kOtcryptoKeySecurityLevelLow) {
320 return OTCRYPTO_NOT_IMPLEMENTED;
325 hmac_mode_t hmac_mode;
327 HARDENED_TRY(get_hmac_mode(key->config.key_mode, &hmac_mode, &block_size));
329 uint32_t processed_key[block_size];
330 HARDENED_TRY(hmac_key_process(key, processed_key));
333 HARDENED_TRY(hmac_init(&
hmac_ctx, hmac_mode, processed_key, block_size));
342 return OTCRYPTO_BAD_ARGS;
346 if (input_message.data == NULL && input_message.len != 0) {
347 return OTCRYPTO_BAD_ARGS;
352 HARDENED_TRY(hmac_update(&
hmac_ctx, input_message.data, input_message.len));
359 if (ctx == NULL || tag.data == NULL) {
360 return OTCRYPTO_BAD_ARGS;
365 HARDENED_TRY(hmac_final(&
hmac_ctx, tag.data, tag.len));