Line data Source code
1 : /**
2 : * Copyright Notice:
3 : * Copyright 2021-2022 DMTF. All rights reserved.
4 : * License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
5 : **/
6 :
7 : /** @file
8 : * HMAC-SHA3_256/384/512 Wrapper Implementation.
9 : **/
10 :
11 : #include "internal_crypt_lib.h"
12 : #include <mbedtls/md.h>
13 :
14 : /**
15 : * Allocates and initializes one HMAC_CTX context for subsequent HMAC-MD use.
16 : *
17 : * @return Pointer to the HMAC_CTX context that has been initialized.
18 : * If the allocations fails, hmac_md_new() returns NULL.
19 : *
20 : **/
21 0 : static void *hmac_md_new(void)
22 : {
23 : void *hmac_md_ctx;
24 :
25 0 : hmac_md_ctx = allocate_zero_pool(sizeof(mbedtls_md_context_t));
26 0 : if (hmac_md_ctx == NULL) {
27 0 : return NULL;
28 : }
29 :
30 0 : return hmac_md_ctx;
31 : }
32 :
33 : /**
34 : * Release the specified HMAC_CTX context.
35 : *
36 : * @param[in] hmac_md_ctx Pointer to the HMAC_CTX context to be released.
37 : *
38 : **/
39 0 : static void hmac_md_free(void *hmac_md_ctx)
40 : {
41 0 : mbedtls_md_free(hmac_md_ctx);
42 0 : free_pool (hmac_md_ctx);
43 0 : }
44 :
45 : /**
46 : * Set user-supplied key for subsequent use. It must be done before any
47 : * calling to hmac_md_update().
48 : *
49 : * If hmac_md_ctx is NULL, then return false.
50 : *
51 : * @param[in] md_type message digest Type.
52 : * @param[out] hmac_md_ctx Pointer to HMAC-MD context.
53 : * @param[in] key Pointer to the user-supplied key.
54 : * @param[in] key_size key size in bytes.
55 : *
56 : * @retval true The key is set successfully.
57 : * @retval false The key is set unsuccessfully.
58 : *
59 : **/
60 0 : static bool hmac_md_set_key(const mbedtls_md_type_t md_type, void *hmac_md_ctx,
61 : const uint8_t *key, size_t key_size)
62 : {
63 : const mbedtls_md_info_t *md_info;
64 : int ret;
65 :
66 0 : if (hmac_md_ctx == NULL || key_size > INT_MAX) {
67 0 : return false;
68 : }
69 :
70 0 : libspdm_zero_mem(hmac_md_ctx, sizeof(mbedtls_md_context_t));
71 0 : mbedtls_md_init(hmac_md_ctx);
72 :
73 0 : md_info = mbedtls_md_info_from_type(md_type);
74 0 : LIBSPDM_ASSERT(md_info != NULL);
75 :
76 0 : ret = mbedtls_md_setup(hmac_md_ctx, md_info, 1);
77 0 : if (ret != 0) {
78 0 : return false;
79 : }
80 :
81 0 : ret = mbedtls_md_hmac_starts(hmac_md_ctx, key, key_size);
82 0 : if (ret != 0) {
83 0 : return false;
84 : }
85 0 : return true;
86 : }
87 :
88 : /**
89 : * Return block size in md_type.
90 : * This function is use to enable hmac_duplicate.
91 : *
92 : * @param[in] md_type mbedtls Type.
93 : *
94 : * @retval blocksize in md_type
95 : **/
96 0 : static int hmac_md_get_blocksize( mbedtls_md_type_t md_type )
97 : {
98 0 : switch( md_type )
99 : {
100 0 : case MBEDTLS_MD_SHA3_256:
101 0 : return 64;
102 0 : case MBEDTLS_MD_SHA3_384:
103 0 : return 128;
104 0 : case MBEDTLS_MD_SHA3_512:
105 0 : return 128;
106 0 : default:
107 0 : LIBSPDM_ASSERT(false);
108 0 : return 0;
109 : }
110 : }
111 :
112 : /**
113 : * Makes a copy of an existing HMAC-MD context.
114 : *
115 : * If hmac_md_ctx is NULL, then return false.
116 : * If new_hmac_md_ctx is NULL, then return false.
117 : *
118 : * @param[in] md_type message digest Type.
119 : * @param[in] hmac_md_ctx Pointer to HMAC-MD context being copied.
120 : * @param[out] new_hmac_md_ctx Pointer to new HMAC-MD context.
121 : *
122 : * @retval true HMAC-MD context copy succeeded.
123 : * @retval false HMAC-MD context copy failed.
124 : *
125 : **/
126 0 : static bool hmac_md_duplicate(const mbedtls_md_type_t md_type, const void *hmac_md_ctx,
127 : void *new_hmac_md_ctx)
128 : {
129 : int ret;
130 : const mbedtls_md_info_t *md_info;
131 :
132 0 : if (hmac_md_ctx == NULL || new_hmac_md_ctx == NULL) {
133 0 : return false;
134 : }
135 :
136 0 : libspdm_zero_mem(new_hmac_md_ctx, sizeof(mbedtls_md_context_t));
137 0 : mbedtls_md_init(new_hmac_md_ctx);
138 :
139 0 : md_info = mbedtls_md_info_from_type(md_type);
140 0 : LIBSPDM_ASSERT(md_info != NULL);
141 :
142 0 : ret = mbedtls_md_setup(new_hmac_md_ctx, md_info, 1);
143 0 : if (ret != 0) {
144 0 : return false;
145 : }
146 0 : ret = mbedtls_md_clone(new_hmac_md_ctx, hmac_md_ctx);
147 0 : if (ret != 0) {
148 0 : return false;
149 : }
150 : /*Temporary solution to the problem of context clone.
151 : * There are not any standard function in mbedtls to clone a complete hmac context.*/
152 0 : libspdm_copy_mem(((mbedtls_md_context_t *)new_hmac_md_ctx)->MBEDTLS_PRIVATE(hmac_ctx),
153 0 : hmac_md_get_blocksize(md_type) * 2,
154 0 : ((const mbedtls_md_context_t *)hmac_md_ctx)->MBEDTLS_PRIVATE(hmac_ctx),
155 0 : hmac_md_get_blocksize(md_type) * 2);
156 0 : return true;
157 : }
158 :
159 : /**
160 : * Digests the input data and updates HMAC-MD context.
161 : *
162 : * This function performs HMAC-MD digest on a data buffer of the specified size.
163 : * It can be called multiple times to compute the digest of long or discontinuous data streams.
164 : * HMAC-MD context should be initialized by hmac_md_new(), and should not be finalized
165 : * by hmac_md_final(). Behavior with invalid context is undefined.
166 : *
167 : * If hmac_md_ctx is NULL, then return false.
168 : *
169 : * @param[in, out] hmac_md_ctx Pointer to the HMAC-MD context.
170 : * @param[in] data Pointer to the buffer containing the data to be digested.
171 : * @param[in] data_size size of data buffer in bytes.
172 : *
173 : * @retval true HMAC-MD data digest succeeded.
174 : * @retval false HMAC-MD data digest failed.
175 : *
176 : **/
177 0 : static bool hmac_md_update(void *hmac_md_ctx, const void *data,
178 : size_t data_size)
179 : {
180 : int ret;
181 :
182 0 : if (hmac_md_ctx == NULL) {
183 0 : return false;
184 : }
185 :
186 0 : if (data == NULL && data_size != 0) {
187 0 : return false;
188 : }
189 0 : if (data_size > INT_MAX) {
190 0 : return false;
191 : }
192 :
193 0 : ret = mbedtls_md_hmac_update(hmac_md_ctx, data, data_size);
194 0 : if (ret != 0) {
195 0 : return false;
196 : }
197 0 : return true;
198 : }
199 :
200 : /**
201 : * Completes computation of the HMAC-MD digest value.
202 : *
203 : * This function completes HMAC-MD hash computation and retrieves the digest value into
204 : * the specified memory. After this function has been called, the HMAC-MD context cannot
205 : * be used again.
206 : * HMAC-MD context should be initialized by hmac_md_new(), and should not be finalized
207 : * by hmac_md_final(). Behavior with invalid HMAC-MD context is undefined.
208 : *
209 : * If hmac_md_ctx is NULL, then return false.
210 : * If hmac_value is NULL, then return false.
211 : *
212 : * @param[in, out] hmac_md_ctx Pointer to the HMAC-MD context.
213 : * @param[out] hmac_value Pointer to a buffer that receives the HMAC-MD digest
214 : * value.
215 : *
216 : * @retval true HMAC-MD digest computation succeeded.
217 : * @retval false HMAC-MD digest computation failed.
218 : *
219 : **/
220 0 : static bool hmac_md_final(void *hmac_md_ctx, uint8_t *hmac_value)
221 : {
222 : int ret;
223 :
224 0 : if (hmac_md_ctx == NULL || hmac_value == NULL) {
225 0 : return false;
226 : }
227 :
228 0 : ret = mbedtls_md_hmac_finish(hmac_md_ctx, hmac_value);
229 0 : mbedtls_md_free(hmac_md_ctx);
230 0 : if (ret != 0) {
231 0 : return false;
232 : }
233 0 : return true;
234 : }
235 :
236 : /**
237 : * Computes the HMAC-MD digest of a input data buffer.
238 : *
239 : * This function performs the HMAC-MD digest of a given data buffer, and places
240 : * the digest value into the specified memory.
241 : *
242 : * If this interface is not supported, then return false.
243 : *
244 : * @param[in] md_type message digest Type.
245 : * @param[in] data Pointer to the buffer containing the data to be digested.
246 : * @param[in] data_size size of data buffer in bytes.
247 : * @param[in] key Pointer to the user-supplied key.
248 : * @param[in] key_size key size in bytes.
249 : * @param[out] hash_value Pointer to a buffer that receives the HMAC-MD digest
250 : * value.
251 : *
252 : * @retval true HMAC-MD digest computation succeeded.
253 : * @retval false HMAC-MD digest computation failed.
254 : * @retval false This interface is not supported.
255 : *
256 : **/
257 0 : static bool hmac_md_all(const mbedtls_md_type_t md_type, const void *data,
258 : size_t data_size, const uint8_t *key, size_t key_size,
259 : uint8_t *hmac_value)
260 : {
261 : const mbedtls_md_info_t *md_info;
262 : int ret;
263 :
264 0 : md_info = mbedtls_md_info_from_type(md_type);
265 0 : LIBSPDM_ASSERT(md_info != NULL);
266 :
267 0 : ret = mbedtls_md_hmac(md_info, key, key_size, data, data_size,
268 : hmac_value);
269 0 : if (ret != 0) {
270 0 : return false;
271 : }
272 0 : return true;
273 : }
274 :
275 : /**
276 : * Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA3_256 use.
277 : *
278 : * @return Pointer to the HMAC_CTX context that has been initialized.
279 : * If the allocations fails, libspdm_hmac_sha3_256_new() returns NULL.
280 : *
281 : **/
282 0 : void *libspdm_hmac_sha3_256_new(void)
283 : {
284 0 : return hmac_md_new();
285 : }
286 :
287 : /**
288 : * Release the specified HMAC_CTX context.
289 : *
290 : * @param[in] hmac_sha3_256_ctx Pointer to the HMAC_CTX context to be released.
291 : *
292 : **/
293 0 : void libspdm_hmac_sha3_256_free(void *hmac_sha3_256_ctx)
294 : {
295 0 : hmac_md_free(hmac_sha3_256_ctx);
296 0 : }
297 :
298 : /**
299 : * Set user-supplied key for subsequent use. It must be done before any
300 : * calling to libspdm_hmac_sha3_256_update().
301 : *
302 : * If hmac_sha3_256_ctx is NULL, then return false.
303 : *
304 : * @param[out] hmac_sha3_256_ctx Pointer to HMAC-SHA3_256 context.
305 : * @param[in] key Pointer to the user-supplied key.
306 : * @param[in] key_size key size in bytes.
307 : *
308 : * @retval true The key is set successfully.
309 : * @retval false The key is set unsuccessfully.
310 : *
311 : **/
312 0 : bool libspdm_hmac_sha3_256_set_key(void *hmac_sha3_256_ctx, const uint8_t *key,
313 : size_t key_size)
314 : {
315 0 : return hmac_md_set_key(MBEDTLS_MD_SHA3_256, hmac_sha3_256_ctx, key,
316 : key_size);
317 : }
318 :
319 : /**
320 : * Makes a copy of an existing HMAC-SHA3_256 context.
321 : *
322 : * If hmac_sha3_256_ctx is NULL, then return false.
323 : * If new_hmac_sha3_256_ctx is NULL, then return false.
324 : *
325 : * @param[in] hmac_sha3_256_ctx Pointer to HMAC-SHA3_256 context being copied.
326 : * @param[out] new_hmac_sha3_256_ctx Pointer to new HMAC-SHA3_256 context.
327 : *
328 : * @retval true HMAC-SHA3_256 context copy succeeded.
329 : * @retval false HMAC-SHA3_256 context copy failed.
330 : *
331 : **/
332 0 : bool libspdm_hmac_sha3_256_duplicate(const void *hmac_sha3_256_ctx,
333 : void *new_hmac_sha3_256_ctx)
334 : {
335 0 : return hmac_md_duplicate(MBEDTLS_MD_SHA3_256, hmac_sha3_256_ctx, new_hmac_sha3_256_ctx);
336 : }
337 :
338 : /**
339 : * Digests the input data and updates HMAC-SHA3_256 context.
340 : *
341 : * This function performs HMAC-SHA3_256 digest on a data buffer of the specified size.
342 : * It can be called multiple times to compute the digest of long or discontinuous data streams.
343 : * HMAC-SHA3_256 context should be initialized by libspdm_hmac_sha3_256_new(), and should not be finalized
344 : * by libspdm_hmac_sha3_256_final(). Behavior with invalid context is undefined.
345 : *
346 : * If hmac_sha3_256_ctx is NULL, then return false.
347 : *
348 : * @param[in, out] hmac_sha3_256_ctx Pointer to the HMAC-SHA3_256 context.
349 : * @param[in] data Pointer to the buffer containing the data to be digested.
350 : * @param[in] data_size size of data buffer in bytes.
351 : *
352 : * @retval true HMAC-SHA3_256 data digest succeeded.
353 : * @retval false HMAC-SHA3_256 data digest failed.
354 : *
355 : **/
356 0 : bool libspdm_hmac_sha3_256_update(void *hmac_sha3_256_ctx, const void *data,
357 : size_t data_size)
358 : {
359 0 : return hmac_md_update(hmac_sha3_256_ctx, data, data_size);
360 : }
361 :
362 : /**
363 : * Completes computation of the HMAC-SHA3_256 digest value.
364 : *
365 : * This function completes HMAC-SHA3_256 hash computation and retrieves the digest value into
366 : * the specified memory. After this function has been called, the HMAC-SHA3_256 context cannot
367 : * be used again.
368 : * HMAC-SHA3_256 context should be initialized by libspdm_hmac_sha3_256_new(), and should not be finalized
369 : * by libspdm_hmac_sha3_256_final(). Behavior with invalid HMAC-SHA3_256 context is undefined.
370 : *
371 : * If hmac_sha3_256_ctx is NULL, then return false.
372 : * If hmac_value is NULL, then return false.
373 : *
374 : * @param[in, out] hmac_sha3_256_ctx Pointer to the HMAC-SHA3_256 context.
375 : * @param[out] hmac_value Pointer to a buffer that receives the HMAC-SHA3_256 digest
376 : * value (32 bytes).
377 : *
378 : * @retval true HMAC-SHA3_256 digest computation succeeded.
379 : * @retval false HMAC-SHA3_256 digest computation failed.
380 : *
381 : **/
382 0 : bool libspdm_hmac_sha3_256_final(void *hmac_sha3_256_ctx, uint8_t *hmac_value)
383 : {
384 0 : return hmac_md_final(hmac_sha3_256_ctx, hmac_value);
385 : }
386 :
387 : /**
388 : * Computes the HMAC-SHA3_256 digest of a input data buffer.
389 : *
390 : * This function performs the HMAC-SHA3_256 digest of a given data buffer, and places
391 : * the digest value into the specified memory.
392 : *
393 : * If this interface is not supported, then return false.
394 : *
395 : * @param[in] data Pointer to the buffer containing the data to be digested.
396 : * @param[in] data_size size of data buffer in bytes.
397 : * @param[in] key Pointer to the user-supplied key.
398 : * @param[in] key_size key size in bytes.
399 : * @param[out] hash_value Pointer to a buffer that receives the HMAC-SHA3_256 digest
400 : * value (32 bytes).
401 : *
402 : * @retval true HMAC-SHA3_256 digest computation succeeded.
403 : * @retval false HMAC-SHA3_256 digest computation failed.
404 : * @retval false This interface is not supported.
405 : *
406 : **/
407 0 : bool libspdm_hmac_sha3_256_all(const void *data, size_t data_size,
408 : const uint8_t *key, size_t key_size,
409 : uint8_t *hmac_value)
410 : {
411 0 : return hmac_md_all(MBEDTLS_MD_SHA3_256, data, data_size, key, key_size,
412 : hmac_value);
413 : }
414 :
415 : /**
416 : * Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA3_384 use.
417 : *
418 : * @return Pointer to the HMAC_CTX context that has been initialized.
419 : * If the allocations fails, libspdm_hmac_sha3_384_new() returns NULL.
420 : *
421 : **/
422 0 : void *libspdm_hmac_sha3_384_new(void)
423 : {
424 0 : return hmac_md_new();
425 : }
426 :
427 : /**
428 : * Release the specified HMAC_CTX context.
429 : *
430 : * @param[in] hmac_sha3_384_ctx Pointer to the HMAC_CTX context to be released.
431 : *
432 : **/
433 0 : void libspdm_hmac_sha3_384_free(void *hmac_sha3_384_ctx)
434 : {
435 0 : hmac_md_free(hmac_sha3_384_ctx);
436 0 : }
437 :
438 : /**
439 : * Set user-supplied key for subsequent use. It must be done before any
440 : * calling to libspdm_hmac_sha3_384_update().
441 : *
442 : * If hmac_sha3_384_ctx is NULL, then return false.
443 : * If this interface is not supported, then return false.
444 : *
445 : * @param[out] hmac_sha3_384_ctx Pointer to HMAC-SHA3_384 context.
446 : * @param[in] key Pointer to the user-supplied key.
447 : * @param[in] key_size key size in bytes.
448 : *
449 : * @retval true The key is set successfully.
450 : * @retval false The key is set unsuccessfully.
451 : * @retval false This interface is not supported.
452 : *
453 : **/
454 0 : bool libspdm_hmac_sha3_384_set_key(void *hmac_sha3_384_ctx, const uint8_t *key,
455 : size_t key_size)
456 : {
457 0 : return hmac_md_set_key(MBEDTLS_MD_SHA3_384, hmac_sha3_384_ctx, key,
458 : key_size);
459 : }
460 :
461 : /**
462 : * Makes a copy of an existing HMAC-SHA3_384 context.
463 : *
464 : * If hmac_sha3_384_ctx is NULL, then return false.
465 : * If new_hmac_sha3_384_ctx is NULL, then return false.
466 : * If this interface is not supported, then return false.
467 : *
468 : * @param[in] hmac_sha3_384_ctx Pointer to HMAC-SHA3_384 context being copied.
469 : * @param[out] new_hmac_sha3_384_ctx Pointer to new HMAC-SHA3_384 context.
470 : *
471 : * @retval true HMAC-SHA3_384 context copy succeeded.
472 : * @retval false HMAC-SHA3_384 context copy failed.
473 : * @retval false This interface is not supported.
474 : *
475 : **/
476 0 : bool libspdm_hmac_sha3_384_duplicate(const void *hmac_sha3_384_ctx,
477 : void *new_hmac_sha3_384_ctx)
478 : {
479 0 : return hmac_md_duplicate(MBEDTLS_MD_SHA3_384, hmac_sha3_384_ctx, new_hmac_sha3_384_ctx);
480 : }
481 :
482 : /**
483 : * Digests the input data and updates HMAC-SHA3_384 context.
484 : *
485 : * This function performs HMAC-SHA3_384 digest on a data buffer of the specified size.
486 : * It can be called multiple times to compute the digest of long or discontinuous data streams.
487 : * HMAC-SHA3_384 context should be initialized by libspdm_hmac_sha3_384_new(), and should not be finalized
488 : * by libspdm_hmac_sha3_384_final(). Behavior with invalid context is undefined.
489 : *
490 : * If hmac_sha3_384_ctx is NULL, then return false.
491 : * If this interface is not supported, then return false.
492 : *
493 : * @param[in, out] hmac_sha3_384_ctx Pointer to the HMAC-SHA3_384 context.
494 : * @param[in] data Pointer to the buffer containing the data to be digested.
495 : * @param[in] data_size size of data buffer in bytes.
496 : *
497 : * @retval true HMAC-SHA3_384 data digest succeeded.
498 : * @retval false HMAC-SHA3_384 data digest failed.
499 : * @retval false This interface is not supported.
500 : *
501 : **/
502 0 : bool libspdm_hmac_sha3_384_update(void *hmac_sha3_384_ctx, const void *data,
503 : size_t data_size)
504 : {
505 0 : return hmac_md_update(hmac_sha3_384_ctx, data, data_size);
506 : }
507 :
508 : /**
509 : * Completes computation of the HMAC-SHA3_384 digest value.
510 : *
511 : * This function completes HMAC-SHA3_384 hash computation and retrieves the digest value into
512 : * the specified memory. After this function has been called, the HMAC-SHA3_384 context cannot
513 : * be used again.
514 : * HMAC-SHA3_384 context should be initialized by libspdm_hmac_sha3_384_new(), and should not be finalized
515 : * by libspdm_hmac_sha3_384_final(). Behavior with invalid HMAC-SHA3_384 context is undefined.
516 : *
517 : * If hmac_sha3_384_ctx is NULL, then return false.
518 : * If hmac_value is NULL, then return false.
519 : * If this interface is not supported, then return false.
520 : *
521 : * @param[in, out] hmac_sha3_384_ctx Pointer to the HMAC-SHA3_384 context.
522 : * @param[out] hmac_value Pointer to a buffer that receives the HMAC-SHA3_384 digest
523 : * value (48 bytes).
524 : *
525 : * @retval true HMAC-SHA3_384 digest computation succeeded.
526 : * @retval false HMAC-SHA3_384 digest computation failed.
527 : * @retval false This interface is not supported.
528 : *
529 : **/
530 0 : bool libspdm_hmac_sha3_384_final(void *hmac_sha3_384_ctx, uint8_t *hmac_value)
531 : {
532 0 : return hmac_md_final(hmac_sha3_384_ctx, hmac_value);
533 : }
534 :
535 : /**
536 : * Computes the HMAC-SHA3_384 digest of a input data buffer.
537 : *
538 : * This function performs the HMAC-SHA3_384 digest of a given data buffer, and places
539 : * the digest value into the specified memory.
540 : *
541 : * If this interface is not supported, then return false.
542 : *
543 : * @param[in] data Pointer to the buffer containing the data to be digested.
544 : * @param[in] data_size size of data buffer in bytes.
545 : * @param[in] key Pointer to the user-supplied key.
546 : * @param[in] key_size key size in bytes.
547 : * @param[out] hash_value Pointer to a buffer that receives the HMAC-SHA3_384 digest
548 : * value (48 bytes).
549 : *
550 : * @retval true HMAC-SHA3_384 digest computation succeeded.
551 : * @retval false HMAC-SHA3_384 digest computation failed.
552 : * @retval false This interface is not supported.
553 : *
554 : **/
555 0 : bool libspdm_hmac_sha3_384_all(const void *data, size_t data_size,
556 : const uint8_t *key, size_t key_size,
557 : uint8_t *hmac_value)
558 : {
559 0 : return hmac_md_all(MBEDTLS_MD_SHA3_384, data, data_size, key, key_size,
560 : hmac_value);
561 : }
562 :
563 : /**
564 : * Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA3_512 use.
565 : *
566 : * @return Pointer to the HMAC_CTX context that has been initialized.
567 : * If the allocations fails, libspdm_hmac_sha3_512_new() returns NULL.
568 : *
569 : **/
570 0 : void *libspdm_hmac_sha3_512_new(void)
571 : {
572 0 : return hmac_md_new();
573 : }
574 :
575 : /**
576 : * Release the specified HMAC_CTX context.
577 : *
578 : * @param[in] hmac_sha3_512_ctx Pointer to the HMAC_CTX context to be released.
579 : *
580 : **/
581 0 : void libspdm_hmac_sha3_512_free(void *hmac_sha3_512_ctx)
582 : {
583 0 : hmac_md_free(hmac_sha3_512_ctx);
584 0 : }
585 :
586 : /**
587 : * Set user-supplied key for subsequent use. It must be done before any
588 : * calling to libspdm_hmac_sha3_512_update().
589 : *
590 : * If hmac_sha3_512_ctx is NULL, then return false.
591 : * If this interface is not supported, then return false.
592 : *
593 : * @param[out] hmac_sha3_512_ctx Pointer to HMAC-SHA3_512 context.
594 : * @param[in] key Pointer to the user-supplied key.
595 : * @param[in] key_size key size in bytes.
596 : *
597 : * @retval true The key is set successfully.
598 : * @retval false The key is set unsuccessfully.
599 : * @retval false This interface is not supported.
600 : *
601 : **/
602 0 : bool libspdm_hmac_sha3_512_set_key(void *hmac_sha3_512_ctx, const uint8_t *key,
603 : size_t key_size)
604 : {
605 0 : return hmac_md_set_key(MBEDTLS_MD_SHA3_512, hmac_sha3_512_ctx, key,
606 : key_size);
607 : }
608 :
609 : /**
610 : * Makes a copy of an existing HMAC-SHA3_512 context.
611 : *
612 : * If hmac_sha3_512_ctx is NULL, then return false.
613 : * If new_hmac_sha3_512_ctx is NULL, then return false.
614 : * If this interface is not supported, then return false.
615 : *
616 : * @param[in] hmac_sha3_512_ctx Pointer to HMAC-SHA3_512 context being copied.
617 : * @param[out] new_hmac_sha3_512_ctx Pointer to new HMAC-SHA3_512 context.
618 : *
619 : * @retval true HMAC-SHA3_512 context copy succeeded.
620 : * @retval false HMAC-SHA3_512 context copy failed.
621 : * @retval false This interface is not supported.
622 : *
623 : **/
624 0 : bool libspdm_hmac_sha3_512_duplicate(const void *hmac_sha3_512_ctx,
625 : void *new_hmac_sha3_512_ctx)
626 : {
627 0 : return hmac_md_duplicate(MBEDTLS_MD_SHA3_512, hmac_sha3_512_ctx, new_hmac_sha3_512_ctx);
628 : }
629 :
630 : /**
631 : * Digests the input data and updates HMAC-SHA3_512 context.
632 : *
633 : * This function performs HMAC-SHA3_512 digest on a data buffer of the specified size.
634 : * It can be called multiple times to compute the digest of long or discontinuous data streams.
635 : * HMAC-SHA3_512 context should be initialized by libspdm_hmac_sha3_512_new(), and should not be finalized
636 : * by libspdm_hmac_sha3_512_final(). Behavior with invalid context is undefined.
637 : *
638 : * If hmac_sha3_512_ctx is NULL, then return false.
639 : * If this interface is not supported, then return false.
640 : *
641 : * @param[in, out] hmac_sha3_512_ctx Pointer to the HMAC-SHA3_512 context.
642 : * @param[in] data Pointer to the buffer containing the data to be digested.
643 : * @param[in] data_size size of data buffer in bytes.
644 : *
645 : * @retval true HMAC-SHA3_512 data digest succeeded.
646 : * @retval false HMAC-SHA3_512 data digest failed.
647 : * @retval false This interface is not supported.
648 : *
649 : **/
650 0 : bool libspdm_hmac_sha3_512_update(void *hmac_sha3_512_ctx, const void *data,
651 : size_t data_size)
652 : {
653 0 : return hmac_md_update(hmac_sha3_512_ctx, data, data_size);
654 : }
655 :
656 : /**
657 : * Completes computation of the HMAC-SHA3_512 digest value.
658 : *
659 : * This function completes HMAC-SHA3_512 hash computation and retrieves the digest value into
660 : * the specified memory. After this function has been called, the HMAC-SHA3_512 context cannot
661 : * be used again.
662 : * HMAC-SHA3_512 context should be initialized by libspdm_hmac_sha3_512_new(), and should not be finalized
663 : * by libspdm_hmac_sha3_512_final(). Behavior with invalid HMAC-SHA3_512 context is undefined.
664 : *
665 : * If hmac_sha3_512_ctx is NULL, then return false.
666 : * If hmac_value is NULL, then return false.
667 : * If this interface is not supported, then return false.
668 : *
669 : * @param[in, out] hmac_sha3_512_ctx Pointer to the HMAC-SHA3_512 context.
670 : * @param[out] hmac_value Pointer to a buffer that receives the HMAC-SHA3_512 digest
671 : * value (64 bytes).
672 : *
673 : * @retval true HMAC-SHA3_512 digest computation succeeded.
674 : * @retval false HMAC-SHA3_512 digest computation failed.
675 : * @retval false This interface is not supported.
676 : *
677 : **/
678 0 : bool libspdm_hmac_sha3_512_final(void *hmac_sha3_512_ctx, uint8_t *hmac_value)
679 : {
680 0 : return hmac_md_final(hmac_sha3_512_ctx, hmac_value);
681 : }
682 :
683 : /**
684 : * Computes the HMAC-SHA3_512 digest of a input data buffer.
685 : *
686 : * This function performs the HMAC-SHA3_512 digest of a given data buffer, and places
687 : * the digest value into the specified memory.
688 : *
689 : * If this interface is not supported, then return false.
690 : *
691 : * @param[in] data Pointer to the buffer containing the data to be digested.
692 : * @param[in] data_size size of data buffer in bytes.
693 : * @param[in] key Pointer to the user-supplied key.
694 : * @param[in] key_size key size in bytes.
695 : * @param[out] hash_value Pointer to a buffer that receives the HMAC-SHA3_512 digest
696 : * value (64 bytes).
697 : *
698 : * @retval true HMAC-SHA3_512 digest computation succeeded.
699 : * @retval false HMAC-SHA3_512 digest computation failed.
700 : * @retval false This interface is not supported.
701 : *
702 : **/
703 0 : bool libspdm_hmac_sha3_512_all(const void *data, size_t data_size,
704 : const uint8_t *key, size_t key_size,
705 : uint8_t *hmac_value)
706 : {
707 0 : return hmac_md_all(MBEDTLS_MD_SHA3_512, data, data_size, key, key_size,
708 : hmac_value);
709 : }
|