1 /*
2  * Copyright (c) 2023, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef __CC3XX_HMAC_H__
9 #define __CC3XX_HMAC_H__
10 
11 #include <stdint.h>
12 #include "cc3xx_error.h"
13 #include "cc3xx_hash.h"
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /**
20  * @brief The size in Bytes, i.e. B, associated to the HMAC block size
21  *
22  */
23 #define CC3XX_HMAC_BLOCK_SIZE (64)
24 
25 /**
26  * @brief Contains the state of the HMAC operation
27  *
28  */
29 struct cc3xx_hmac_state_t {
30     uint8_t key[CC3XX_HMAC_BLOCK_SIZE];
31     struct cc3xx_hash_state_t hash; /* Allows to restart low-level hash */
32     cc3xx_hash_alg_t alg; /* Based on the hashing algorithm, sizes change */
33 } __attribute__((aligned(4)));
34 
35 /**
36  * @brief Sets the key for the HMAC operation on the state
37  *
38  * @param[out] state   A pointer to a state structure
39  * @param[in] key      Buffer containing the key
40  * @param[in] key_size Size in bytes of the buffer \param key
41  * @param[in] alg      Underlying hashing algorithm
42  * @return cc3xx_err_t
43  */
44 cc3xx_err_t cc3xx_hmac_set_key(
45     struct cc3xx_hmac_state_t *state,
46     const uint8_t *key,
47     size_t key_size,
48     cc3xx_hash_alg_t alg);
49 
50 /**
51  * @brief Update the HMAC operation with a new chunk of data to authenticate
52  *
53  * @param[in,out] state   A pointer to a state structure
54  * @param[in] data        Buffer containing the data to use for the update
55  * @param[in] data_length Size in bytes of the buffer \param data
56  * @return cc3xx_err_t
57  */
58 cc3xx_err_t cc3xx_hmac_update(
59     struct cc3xx_hmac_state_t *state,
60     const uint8_t *data,
61     size_t data_length);
62 
63 /**
64  * @brief Finalize the HMAC operation by producing the authentication tag
65  *
66  * @param[in,out] state A pointer to a state structure
67  * @param[out] tag      Output buffer
68  * @param[in] tag_size  Size in bytes of the buffer \param tag
69  * @return cc3xx_err_t
70  */
71 cc3xx_err_t cc3xx_hmac_finish(
72     struct cc3xx_hmac_state_t *state,
73     uint32_t *tag,
74     size_t tag_size);
75 
76 #ifdef __cplusplus
77 }
78 #endif
79 
80 #endif /* __CC3XX_HMAC_H__ */
81