10 #include "sw/device/lib/crypto/drivers/hmac.h"
11 #include "sw/device/lib/crypto/drivers/kmac.h"
12 #include "sw/device/lib/crypto/impl/status.h"
15 #define MODULE_ID MAKE_MODULE_ID('h', 'a', 's')
18 static_assert(kSha256DigestBits == kHmacSha256DigestBits &&
19 kSha256DigestBytes == kHmacSha256DigestBytes &&
20 kSha256DigestWords == kHmacSha256DigestWords,
21 "Exposed and driver-level SHA-256 digest size mismatch.");
22 static_assert(kSha384DigestBits == kHmacSha384DigestBits &&
23 kSha384DigestBytes == kHmacSha384DigestBytes &&
24 kSha384DigestWords == kHmacSha384DigestWords,
25 "Exposed and driver-level SHA-384 digest size mismatch.");
26 static_assert(kSha512DigestBits == kHmacSha512DigestBits &&
27 kSha512DigestBytes == kHmacSha512DigestBytes &&
28 kSha512DigestWords == kHmacSha512DigestWords,
29 "Exposed and driver-level SHA-512 digest size mismatch.");
34 "`otcrypto_hash_context_t` must be big enough to hold `hmac_ctx_t`.");
37 static_assert(
sizeof(
hmac_ctx_t) %
sizeof(uint32_t) == 0,
38 "Size of `hmac_ctx_t` must be a multiple of the word size for "
39 "`hardened_memcpy()`");
81 switch (launder32(digest.mode)) {
82 case kOtcryptoHashModeSha3_224:
83 if (launder32(digest.len) == (224 / 32)) {
87 return OTCRYPTO_BAD_ARGS;
88 case kOtcryptoHashModeSha256:
90 case kOtcryptoHashModeSha3_256:
91 if (launder32(digest.len) == (256 / 32)) {
95 return OTCRYPTO_BAD_ARGS;
96 case kOtcryptoHashModeSha384:
98 case kOtcryptoHashModeSha3_384:
99 if (launder32(digest.len) == (384 / 32)) {
103 return OTCRYPTO_BAD_ARGS;
104 case kOtcryptoHashModeSha512:
106 case kOtcryptoHashModeSha3_512:
107 if (launder32(digest.len) == (512 / 32)) {
111 return OTCRYPTO_BAD_ARGS;
113 return OTCRYPTO_BAD_ARGS;
117 return OTCRYPTO_FATAL_ERR;
122 if (input_message.data == NULL && input_message.len != 0) {
123 return OTCRYPTO_BAD_ARGS;
126 if (digest.data == NULL) {
127 return OTCRYPTO_BAD_ARGS;
131 HARDENED_TRY(check_digest_len(digest));
133 switch (digest.mode) {
134 case kOtcryptoHashModeSha3_224:
135 return kmac_sha3_224(input_message.data, input_message.len, digest.data);
136 case kOtcryptoHashModeSha3_256:
137 return kmac_sha3_256(input_message.data, input_message.len, digest.data);
138 case kOtcryptoHashModeSha3_384:
139 return kmac_sha3_384(input_message.data, input_message.len, digest.data);
140 case kOtcryptoHashModeSha3_512:
141 return kmac_sha3_512(input_message.data, input_message.len, digest.data);
142 case kOtcryptoHashModeSha256:
143 return hmac(kHmacModeSha256, NULL, 0,
144 input_message.data, input_message.len, digest.data,
147 case kOtcryptoHashModeSha384:
148 return hmac(kHmacModeSha384, NULL, 0,
149 input_message.data, input_message.len, digest.data,
152 case kOtcryptoHashModeSha512:
153 return hmac(kHmacModeSha512, NULL, 0,
154 input_message.data, input_message.len, digest.data,
159 return OTCRYPTO_BAD_ARGS;
166 switch (digest.mode) {
167 case kOtcryptoHashXofModeShake128:
168 return kmac_shake_128(input_message.data, input_message.len, digest.data,
170 case kOtcryptoHashXofModeShake256:
171 return kmac_shake_256(input_message.data, input_message.len, digest.data,
174 return OTCRYPTO_BAD_ARGS;
179 return OTCRYPTO_FATAL_ERR;
189 if (customization_string.len == 0 && function_name_string.len == 0) {
190 switch (digest.mode) {
191 case kOtcryptoHashXofModeCshake128:
192 return kmac_shake_128(input_message.data, input_message.len,
193 digest.data, digest.len);
194 case kOtcryptoHashXofModeCshake256:
195 return kmac_shake_256(input_message.data, input_message.len,
196 digest.data, digest.len);
198 return OTCRYPTO_BAD_ARGS;
202 switch (digest.mode) {
203 case kOtcryptoHashXofModeCshake128:
204 return kmac_cshake_128(
205 input_message.data, input_message.len, function_name_string.data,
206 function_name_string.len, customization_string.data,
207 customization_string.len, digest.data, digest.len);
209 case kOtcryptoHashXofModeCshake256:
210 return kmac_cshake_256(
211 input_message.data, input_message.len, function_name_string.data,
212 function_name_string.len, customization_string.data,
213 customization_string.len, digest.data, digest.len);
215 return OTCRYPTO_BAD_ARGS;
220 return OTCRYPTO_FATAL_ERR;
226 return OTCRYPTO_BAD_ARGS;
231 case kOtcryptoHashModeSha256: {
232 HARDENED_TRY(hmac_init(&
hmac_ctx, kHmacModeSha256, NULL,
236 case kOtcryptoHashModeSha384: {
237 HARDENED_TRY(hmac_init(&
hmac_ctx, kHmacModeSha384, NULL,
241 case kOtcryptoHashModeSha512: {
242 HARDENED_TRY(hmac_init(&
hmac_ctx, kHmacModeSha512, NULL,
248 return OTCRYPTO_BAD_ARGS;
258 if (ctx == NULL || (input_message.data == NULL && input_message.len != 0)) {
259 return OTCRYPTO_BAD_ARGS;
263 HARDENED_TRY(hmac_update(&
hmac_ctx, input_message.data, input_message.len));
270 if (ctx == NULL || digest.data == NULL) {
271 return OTCRYPTO_BAD_ARGS;
275 HARDENED_TRY(check_digest_len(digest));
279 HARDENED_TRY(hmac_final(&
hmac_ctx, digest.data, digest.len));