8 #include "sw/device/lib/crypto/drivers/hmac.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('h', 'm', 'c')
22 "`otcrypto_hash_context_t` must be big enough to hold `hmac_ctx_t`.");
27 static_assert(
sizeof(
hmac_ctx_t) %
sizeof(uint32_t) == 0,
28 "Size of `hmac_ctx_t` must be a multiple of the word size for "
29 "`hardened_memcpy()`");
76 hmac_mode_t *hmac_mode,
size_t *block_size) {
78 case kOtcryptoKeyModeHmacSha256:
79 *hmac_mode = kHmacModeHmac256;
80 *block_size = kHmacSha256BlockWords;
82 case kOtcryptoKeyModeHmacSha384:
83 *hmac_mode = kHmacModeHmac384;
85 *block_size = kHmacSha512BlockWords;
87 case kOtcryptoKeyModeHmacSha512:
88 *hmac_mode = kHmacModeHmac512;
89 *block_size = kHmacSha512BlockWords;
92 return OTCRYPTO_BAD_ARGS;
112 uint32_t *processed_key) {
116 switch (key->config.key_mode) {
117 case kOtcryptoKeyModeHmacSha256:
118 hash_mode = kOtcryptoHashModeSha256;
119 block_size = kHmacSha256BlockWords;
120 digest_size = kHmacSha256DigestWords;
122 case kOtcryptoKeyModeHmacSha384:
123 hash_mode = kOtcryptoHashModeSha384;
125 block_size = kHmacSha512BlockWords;
126 digest_size = kHmacSha384DigestWords;
128 case kOtcryptoKeyModeHmacSha512:
129 hash_mode = kOtcryptoHashModeSha512;
130 block_size = kHmacSha512BlockWords;
131 digest_size = kHmacSha512DigestWords;
134 return OTCRYPTO_BAD_ARGS;
138 size_t unmasked_key_len = keyblob_share_num_words(key->config);
139 uint32_t unmasked_key[unmasked_key_len];
140 HARDENED_TRY(keyblob_key_unmask(key, unmasked_key_len, unmasked_key));
144 memset(processed_key, 0, block_size *
sizeof(uint32_t));
147 if (key->config.key_length > block_size *
sizeof(uint32_t)) {
150 .data = processed_key,
154 .len = key->config.key_length,
155 .data = (
unsigned char *)unmasked_key,
162 size_t offset = key->config.key_length %
sizeof(uint32_t);
164 unsigned char *key_end_ptr =
165 (
unsigned char *)&processed_key[unmasked_key_len];
166 size_t num_zero_bytes =
sizeof(uint32_t) - offset;
167 memset(key_end_ptr - num_zero_bytes, 0, num_zero_bytes);
178 if (key == NULL || key->keyblob == NULL || tag.data == NULL) {
179 return OTCRYPTO_BAD_ARGS;
185 return OTCRYPTO_NOT_IMPLEMENTED;
187 if (key->config.security_level != kOtcryptoKeySecurityLevelLow) {
188 return OTCRYPTO_NOT_IMPLEMENTED;
192 if (input_message.data == NULL && input_message.len != 0) {
193 return OTCRYPTO_BAD_ARGS;
196 hmac_mode_t hmac_mode;
198 HARDENED_TRY(get_hmac_mode(key->config.key_mode, &hmac_mode, &block_size));
200 uint32_t processed_key[block_size];
201 HARDENED_TRY(hmac_key_process(key, processed_key));
203 return hmac(hmac_mode, processed_key, block_size, input_message.data,
204 input_message.len, tag.data, tag.len);
209 if (ctx == NULL || key == NULL || key->keyblob == NULL) {
210 return OTCRYPTO_BAD_ARGS;
214 return OTCRYPTO_NOT_IMPLEMENTED;
216 if (key->config.security_level != kOtcryptoKeySecurityLevelLow) {
218 return OTCRYPTO_NOT_IMPLEMENTED;
223 hmac_mode_t hmac_mode;
225 HARDENED_TRY(get_hmac_mode(key->config.key_mode, &hmac_mode, &block_size));
227 uint32_t processed_key[block_size];
228 HARDENED_TRY(hmac_key_process(key, processed_key));
231 HARDENED_TRY(hmac_init(&
hmac_ctx, hmac_mode, processed_key, block_size));
240 return OTCRYPTO_BAD_ARGS;
244 if (input_message.data == NULL && input_message.len != 0) {
245 return OTCRYPTO_BAD_ARGS;
250 HARDENED_TRY(hmac_update(&
hmac_ctx, input_message.data, input_message.len));
257 if (ctx == NULL || tag.data == NULL) {
258 return OTCRYPTO_BAD_ARGS;
263 HARDENED_TRY(hmac_final(&
hmac_ctx, tag.data, tag.len));