xref: /aosp_15_r20/external/mbedtls/library/psa_crypto_mac.h (revision 62c56f9862f102b96d72393aff6076c951fb8148)
1*62c56f98SSadaf Ebrahimi /*
2*62c56f98SSadaf Ebrahimi  *  PSA MAC layer on top of Mbed TLS software crypto
3*62c56f98SSadaf Ebrahimi  */
4*62c56f98SSadaf Ebrahimi /*
5*62c56f98SSadaf Ebrahimi  *  Copyright The Mbed TLS Contributors
6*62c56f98SSadaf Ebrahimi  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7*62c56f98SSadaf Ebrahimi  */
8*62c56f98SSadaf Ebrahimi 
9*62c56f98SSadaf Ebrahimi #ifndef PSA_CRYPTO_MAC_H
10*62c56f98SSadaf Ebrahimi #define PSA_CRYPTO_MAC_H
11*62c56f98SSadaf Ebrahimi 
12*62c56f98SSadaf Ebrahimi #include <psa/crypto.h>
13*62c56f98SSadaf Ebrahimi 
14*62c56f98SSadaf Ebrahimi /** Calculate the MAC (message authentication code) of a message using Mbed TLS.
15*62c56f98SSadaf Ebrahimi  *
16*62c56f98SSadaf Ebrahimi  * \note The signature of this function is that of a PSA driver mac_compute
17*62c56f98SSadaf Ebrahimi  *       entry point. This function behaves as a mac_compute entry point as
18*62c56f98SSadaf Ebrahimi  *       defined in the PSA driver interface specification for transparent
19*62c56f98SSadaf Ebrahimi  *       drivers.
20*62c56f98SSadaf Ebrahimi  *
21*62c56f98SSadaf Ebrahimi  * \param[in] attributes        The attributes of the key to use for the
22*62c56f98SSadaf Ebrahimi  *                              operation.
23*62c56f98SSadaf Ebrahimi  * \param[in] key_buffer        The buffer containing the key to use for
24*62c56f98SSadaf Ebrahimi  *                              computing the MAC. This buffer contains the key
25*62c56f98SSadaf Ebrahimi  *                              in export representation as defined by
26*62c56f98SSadaf Ebrahimi  *                              psa_export_key() (i.e. the raw key bytes).
27*62c56f98SSadaf Ebrahimi  * \param key_buffer_size       Size of the \p key_buffer buffer in bytes.
28*62c56f98SSadaf Ebrahimi  * \param alg                   The MAC algorithm to use (\c PSA_ALG_XXX value
29*62c56f98SSadaf Ebrahimi  *                              such that #PSA_ALG_IS_MAC(\p alg) is true).
30*62c56f98SSadaf Ebrahimi  * \param[in] input             Buffer containing the input message.
31*62c56f98SSadaf Ebrahimi  * \param input_length          Size of the \p input buffer in bytes.
32*62c56f98SSadaf Ebrahimi  * \param[out] mac              Buffer where the MAC value is to be written.
33*62c56f98SSadaf Ebrahimi  * \param mac_size              Size of the \p mac buffer in bytes.
34*62c56f98SSadaf Ebrahimi  * \param[out] mac_length       On success, the number of bytes
35*62c56f98SSadaf Ebrahimi  *                              that make up the MAC value.
36*62c56f98SSadaf Ebrahimi  *
37*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
38*62c56f98SSadaf Ebrahimi  *         Success.
39*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
40*62c56f98SSadaf Ebrahimi  *         \p alg is not supported.
41*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
42*62c56f98SSadaf Ebrahimi  *         \p mac_size is too small
43*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
44*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
45*62c56f98SSadaf Ebrahimi  */
46*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_mac_compute(
47*62c56f98SSadaf Ebrahimi     const psa_key_attributes_t *attributes,
48*62c56f98SSadaf Ebrahimi     const uint8_t *key_buffer,
49*62c56f98SSadaf Ebrahimi     size_t key_buffer_size,
50*62c56f98SSadaf Ebrahimi     psa_algorithm_t alg,
51*62c56f98SSadaf Ebrahimi     const uint8_t *input,
52*62c56f98SSadaf Ebrahimi     size_t input_length,
53*62c56f98SSadaf Ebrahimi     uint8_t *mac,
54*62c56f98SSadaf Ebrahimi     size_t mac_size,
55*62c56f98SSadaf Ebrahimi     size_t *mac_length);
56*62c56f98SSadaf Ebrahimi 
57*62c56f98SSadaf Ebrahimi /** Set up a multipart MAC calculation operation using Mbed TLS.
58*62c56f98SSadaf Ebrahimi  *
59*62c56f98SSadaf Ebrahimi  * \note The signature of this function is that of a PSA driver mac_sign_setup
60*62c56f98SSadaf Ebrahimi  *       entry point. This function behaves as a mac_sign_setup entry point as
61*62c56f98SSadaf Ebrahimi  *       defined in the PSA driver interface specification for transparent
62*62c56f98SSadaf Ebrahimi  *       drivers.
63*62c56f98SSadaf Ebrahimi  *
64*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     The operation object to set up. It must have
65*62c56f98SSadaf Ebrahimi  *                              been initialized and not yet in use.
66*62c56f98SSadaf Ebrahimi  * \param[in] attributes        The attributes of the key to use for the
67*62c56f98SSadaf Ebrahimi  *                              operation.
68*62c56f98SSadaf Ebrahimi  * \param[in] key_buffer        The buffer containing the key to use for
69*62c56f98SSadaf Ebrahimi  *                              computing the MAC. This buffer contains the key
70*62c56f98SSadaf Ebrahimi  *                              in export representation as defined by
71*62c56f98SSadaf Ebrahimi  *                              psa_export_key() (i.e. the raw key bytes).
72*62c56f98SSadaf Ebrahimi  * \param key_buffer_size       Size of the \p key_buffer buffer in bytes.
73*62c56f98SSadaf Ebrahimi  * \param alg                   The MAC algorithm to use (\c PSA_ALG_XXX value
74*62c56f98SSadaf Ebrahimi  *                              such that #PSA_ALG_IS_MAC(\p alg) is true).
75*62c56f98SSadaf Ebrahimi  *
76*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
77*62c56f98SSadaf Ebrahimi  *         Success.
78*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
79*62c56f98SSadaf Ebrahimi  *         \p alg is not supported.
80*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
81*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
82*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
83*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be inactive).
84*62c56f98SSadaf Ebrahimi  */
85*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_mac_sign_setup(
86*62c56f98SSadaf Ebrahimi     mbedtls_psa_mac_operation_t *operation,
87*62c56f98SSadaf Ebrahimi     const psa_key_attributes_t *attributes,
88*62c56f98SSadaf Ebrahimi     const uint8_t *key_buffer,
89*62c56f98SSadaf Ebrahimi     size_t key_buffer_size,
90*62c56f98SSadaf Ebrahimi     psa_algorithm_t alg);
91*62c56f98SSadaf Ebrahimi 
92*62c56f98SSadaf Ebrahimi /** Set up a multipart MAC verification operation using Mbed TLS.
93*62c56f98SSadaf Ebrahimi  *
94*62c56f98SSadaf Ebrahimi  * \note The signature of this function is that of a PSA driver mac_verify_setup
95*62c56f98SSadaf Ebrahimi  *       entry point. This function behaves as a mac_verify_setup entry point as
96*62c56f98SSadaf Ebrahimi  *       defined in the PSA driver interface specification for transparent
97*62c56f98SSadaf Ebrahimi  *       drivers.
98*62c56f98SSadaf Ebrahimi  *
99*62c56f98SSadaf Ebrahimi  * \param[in,out] operation     The operation object to set up. It must have
100*62c56f98SSadaf Ebrahimi  *                              been initialized and not yet in use.
101*62c56f98SSadaf Ebrahimi  * \param[in] attributes        The attributes of the key to use for the
102*62c56f98SSadaf Ebrahimi  *                              operation.
103*62c56f98SSadaf Ebrahimi  * \param[in] key_buffer        The buffer containing the key to use for
104*62c56f98SSadaf Ebrahimi  *                              computing the MAC. This buffer contains the key
105*62c56f98SSadaf Ebrahimi  *                              in export representation as defined by
106*62c56f98SSadaf Ebrahimi  *                              psa_export_key() (i.e. the raw key bytes).
107*62c56f98SSadaf Ebrahimi  * \param key_buffer_size       Size of the \p key_buffer buffer in bytes.
108*62c56f98SSadaf Ebrahimi  * \param alg                   The MAC algorithm to use (\c PSA_ALG_XXX value
109*62c56f98SSadaf Ebrahimi  *                              such that #PSA_ALG_IS_MAC(\p alg) is true).
110*62c56f98SSadaf Ebrahimi  *
111*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
112*62c56f98SSadaf Ebrahimi  *         Success.
113*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_SUPPORTED
114*62c56f98SSadaf Ebrahimi  *         \p alg is not supported.
115*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
116*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
117*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
118*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be inactive).
119*62c56f98SSadaf Ebrahimi  */
120*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_mac_verify_setup(
121*62c56f98SSadaf Ebrahimi     mbedtls_psa_mac_operation_t *operation,
122*62c56f98SSadaf Ebrahimi     const psa_key_attributes_t *attributes,
123*62c56f98SSadaf Ebrahimi     const uint8_t *key_buffer,
124*62c56f98SSadaf Ebrahimi     size_t key_buffer_size,
125*62c56f98SSadaf Ebrahimi     psa_algorithm_t alg);
126*62c56f98SSadaf Ebrahimi 
127*62c56f98SSadaf Ebrahimi /** Add a message fragment to a multipart MAC operation using Mbed TLS.
128*62c56f98SSadaf Ebrahimi  *
129*62c56f98SSadaf Ebrahimi  * \note The signature of this function is that of a PSA driver mac_update
130*62c56f98SSadaf Ebrahimi  *       entry point. This function behaves as a mac_update entry point as
131*62c56f98SSadaf Ebrahimi  *       defined in the PSA driver interface specification for transparent
132*62c56f98SSadaf Ebrahimi  *       drivers.
133*62c56f98SSadaf Ebrahimi  *
134*62c56f98SSadaf Ebrahimi  * The PSA core calls mbedtls_psa_mac_sign_setup() or
135*62c56f98SSadaf Ebrahimi  * mbedtls_psa_mac_verify_setup() before calling this function.
136*62c56f98SSadaf Ebrahimi  *
137*62c56f98SSadaf Ebrahimi  * If this function returns an error status, the PSA core aborts the
138*62c56f98SSadaf Ebrahimi  * operation by calling mbedtls_psa_mac_abort().
139*62c56f98SSadaf Ebrahimi  *
140*62c56f98SSadaf Ebrahimi  * \param[in,out] operation Active MAC operation.
141*62c56f98SSadaf Ebrahimi  * \param[in] input         Buffer containing the message fragment to add to
142*62c56f98SSadaf Ebrahimi  *                          the MAC calculation.
143*62c56f98SSadaf Ebrahimi  * \param input_length      Size of the \p input buffer in bytes.
144*62c56f98SSadaf Ebrahimi  *
145*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
146*62c56f98SSadaf Ebrahimi  *         Success.
147*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
148*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be active).
149*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
150*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
151*62c56f98SSadaf Ebrahimi  */
152*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_mac_update(
153*62c56f98SSadaf Ebrahimi     mbedtls_psa_mac_operation_t *operation,
154*62c56f98SSadaf Ebrahimi     const uint8_t *input,
155*62c56f98SSadaf Ebrahimi     size_t input_length);
156*62c56f98SSadaf Ebrahimi 
157*62c56f98SSadaf Ebrahimi /** Finish the calculation of the MAC of a message using Mbed TLS.
158*62c56f98SSadaf Ebrahimi  *
159*62c56f98SSadaf Ebrahimi  * \note The signature of this function is that of a PSA driver mac_sign_finish
160*62c56f98SSadaf Ebrahimi  *       entry point. This function behaves as a mac_sign_finish entry point as
161*62c56f98SSadaf Ebrahimi  *       defined in the PSA driver interface specification for transparent
162*62c56f98SSadaf Ebrahimi  *       drivers.
163*62c56f98SSadaf Ebrahimi  *
164*62c56f98SSadaf Ebrahimi  * The PSA core calls mbedtls_psa_mac_sign_setup() before calling this function.
165*62c56f98SSadaf Ebrahimi  * This function calculates the MAC of the message formed by concatenating
166*62c56f98SSadaf Ebrahimi  * the inputs passed to preceding calls to mbedtls_psa_mac_update().
167*62c56f98SSadaf Ebrahimi  *
168*62c56f98SSadaf Ebrahimi  * Whether this function returns successfully or not, the PSA core subsequently
169*62c56f98SSadaf Ebrahimi  * aborts the operation by calling mbedtls_psa_mac_abort().
170*62c56f98SSadaf Ebrahimi  *
171*62c56f98SSadaf Ebrahimi  * \param[in,out] operation Active MAC operation.
172*62c56f98SSadaf Ebrahimi  * \param[out] mac          Buffer where the MAC value is to be written.
173*62c56f98SSadaf Ebrahimi  * \param mac_size          Output size requested for the MAC algorithm. The PSA
174*62c56f98SSadaf Ebrahimi  *                          core guarantees this is a valid MAC length for the
175*62c56f98SSadaf Ebrahimi  *                          algorithm and key combination passed to
176*62c56f98SSadaf Ebrahimi  *                          mbedtls_psa_mac_sign_setup(). It also guarantees the
177*62c56f98SSadaf Ebrahimi  *                          \p mac buffer is large enough to contain the
178*62c56f98SSadaf Ebrahimi  *                          requested output size.
179*62c56f98SSadaf Ebrahimi  * \param[out] mac_length   On success, the number of bytes output to buffer
180*62c56f98SSadaf Ebrahimi  *                          \p mac, which will be equal to the requested length
181*62c56f98SSadaf Ebrahimi  *                          \p mac_size.
182*62c56f98SSadaf Ebrahimi  *
183*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
184*62c56f98SSadaf Ebrahimi  *         Success.
185*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
186*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be an active mac sign
187*62c56f98SSadaf Ebrahimi  *         operation).
188*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
189*62c56f98SSadaf Ebrahimi  *         The size of the \p mac buffer is too small. A sufficient buffer size
190*62c56f98SSadaf Ebrahimi  *         can be determined by calling PSA_MAC_LENGTH().
191*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
192*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
193*62c56f98SSadaf Ebrahimi  */
194*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_mac_sign_finish(
195*62c56f98SSadaf Ebrahimi     mbedtls_psa_mac_operation_t *operation,
196*62c56f98SSadaf Ebrahimi     uint8_t *mac,
197*62c56f98SSadaf Ebrahimi     size_t mac_size,
198*62c56f98SSadaf Ebrahimi     size_t *mac_length);
199*62c56f98SSadaf Ebrahimi 
200*62c56f98SSadaf Ebrahimi /** Finish the calculation of the MAC of a message and compare it with
201*62c56f98SSadaf Ebrahimi  * an expected value using Mbed TLS.
202*62c56f98SSadaf Ebrahimi  *
203*62c56f98SSadaf Ebrahimi  * \note The signature of this function is that of a PSA driver
204*62c56f98SSadaf Ebrahimi  *       mac_verify_finish entry point. This function behaves as a
205*62c56f98SSadaf Ebrahimi  *       mac_verify_finish entry point as defined in the PSA driver interface
206*62c56f98SSadaf Ebrahimi  *       specification for transparent drivers.
207*62c56f98SSadaf Ebrahimi  *
208*62c56f98SSadaf Ebrahimi  * The PSA core calls mbedtls_psa_mac_verify_setup() before calling this
209*62c56f98SSadaf Ebrahimi  * function. This function calculates the MAC of the message formed by
210*62c56f98SSadaf Ebrahimi  * concatenating the inputs passed to preceding calls to
211*62c56f98SSadaf Ebrahimi  * mbedtls_psa_mac_update(). It then compares the calculated MAC with the
212*62c56f98SSadaf Ebrahimi  * expected MAC passed as a parameter to this function.
213*62c56f98SSadaf Ebrahimi  *
214*62c56f98SSadaf Ebrahimi  * Whether this function returns successfully or not, the PSA core subsequently
215*62c56f98SSadaf Ebrahimi  * aborts the operation by calling mbedtls_psa_mac_abort().
216*62c56f98SSadaf Ebrahimi  *
217*62c56f98SSadaf Ebrahimi  * \param[in,out] operation Active MAC operation.
218*62c56f98SSadaf Ebrahimi  * \param[in] mac           Buffer containing the expected MAC value.
219*62c56f98SSadaf Ebrahimi  * \param mac_length        Length in bytes of the expected MAC value. The PSA
220*62c56f98SSadaf Ebrahimi  *                          core guarantees that this length is a valid MAC
221*62c56f98SSadaf Ebrahimi  *                          length for the algorithm and key combination passed
222*62c56f98SSadaf Ebrahimi  *                          to mbedtls_psa_mac_verify_setup().
223*62c56f98SSadaf Ebrahimi  *
224*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
225*62c56f98SSadaf Ebrahimi  *         The expected MAC is identical to the actual MAC of the message.
226*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_SIGNATURE
227*62c56f98SSadaf Ebrahimi  *         The MAC of the message was calculated successfully, but it
228*62c56f98SSadaf Ebrahimi  *         differs from the expected MAC.
229*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
230*62c56f98SSadaf Ebrahimi  *         The operation state is not valid (it must be an active mac verify
231*62c56f98SSadaf Ebrahimi  *         operation).
232*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
233*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
234*62c56f98SSadaf Ebrahimi  */
235*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_mac_verify_finish(
236*62c56f98SSadaf Ebrahimi     mbedtls_psa_mac_operation_t *operation,
237*62c56f98SSadaf Ebrahimi     const uint8_t *mac,
238*62c56f98SSadaf Ebrahimi     size_t mac_length);
239*62c56f98SSadaf Ebrahimi 
240*62c56f98SSadaf Ebrahimi /** Abort a MAC operation using Mbed TLS.
241*62c56f98SSadaf Ebrahimi  *
242*62c56f98SSadaf Ebrahimi  * Aborting an operation frees all associated resources except for the
243*62c56f98SSadaf Ebrahimi  * \p operation structure itself. Once aborted, the operation object
244*62c56f98SSadaf Ebrahimi  * can be reused for another operation by calling
245*62c56f98SSadaf Ebrahimi  * mbedtls_psa_mac_sign_setup() or mbedtls_psa_mac_verify_setup() again.
246*62c56f98SSadaf Ebrahimi  *
247*62c56f98SSadaf Ebrahimi  * The PSA core may call this function any time after the operation object has
248*62c56f98SSadaf Ebrahimi  * been initialized by one of the methods described in
249*62c56f98SSadaf Ebrahimi  * #mbedtls_psa_mac_operation_t.
250*62c56f98SSadaf Ebrahimi  *
251*62c56f98SSadaf Ebrahimi  * In particular, calling mbedtls_psa_mac_abort() after the operation has been
252*62c56f98SSadaf Ebrahimi  * terminated by a call to mbedtls_psa_mac_abort(),
253*62c56f98SSadaf Ebrahimi  * mbedtls_psa_mac_sign_finish() or mbedtls_psa_mac_verify_finish() is safe and
254*62c56f98SSadaf Ebrahimi  * has no effect.
255*62c56f98SSadaf Ebrahimi  *
256*62c56f98SSadaf Ebrahimi  * \param[in,out] operation Initialized MAC operation.
257*62c56f98SSadaf Ebrahimi  *
258*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS \emptydescription
259*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
260*62c56f98SSadaf Ebrahimi  */
261*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_mac_abort(
262*62c56f98SSadaf Ebrahimi     mbedtls_psa_mac_operation_t *operation);
263*62c56f98SSadaf Ebrahimi 
264*62c56f98SSadaf Ebrahimi #endif /* PSA_CRYPTO_MAC_H */
265