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