blob: 2f614bcc6e7ec4277ca919e1184e3bc7e800f500 [file] [log] [blame]
Steven Cooremand13a70f2021-03-19 15:24:23 +01001/*
2 * PSA MAC layer on top of Mbed TLS software crypto
3 */
4/*
5 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Steven Cooremand13a70f2021-03-19 15:24:23 +01007 */
8
9#ifndef PSA_CRYPTO_MAC_H
10#define PSA_CRYPTO_MAC_H
11
12#include <psa/crypto.h>
13
Steven Cooremand13a70f2021-03-19 15:24:23 +010014/** Calculate the MAC (message authentication code) of a message using Mbed TLS.
15 *
16 * \note The signature of this function is that of a PSA driver mac_compute
17 * entry point. This function behaves as a mac_compute entry point as
18 * defined in the PSA driver interface specification for transparent
19 * drivers.
20 *
21 * \param[in] attributes The attributes of the key to use for the
22 * operation.
23 * \param[in] key_buffer The buffer containing the key to use for
24 * computing the MAC. This buffer contains the key
25 * in export representation as defined by
26 * psa_export_key() (i.e. the raw key bytes).
27 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
28 * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
29 * such that #PSA_ALG_IS_MAC(\p alg) is true).
30 * \param[in] input Buffer containing the input message.
31 * \param input_length Size of the \p input buffer in bytes.
32 * \param[out] mac Buffer where the MAC value is to be written.
33 * \param mac_size Size of the \p mac buffer in bytes.
34 * \param[out] mac_length On success, the number of bytes
35 * that make up the MAC value.
36 *
37 * \retval #PSA_SUCCESS
38 * Success.
Steven Cooremand13a70f2021-03-19 15:24:23 +010039 * \retval #PSA_ERROR_NOT_SUPPORTED
40 * \p alg is not supported.
41 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
42 * \p mac_size is too small
Gilles Peskineed733552023-02-14 19:21:09 +010043 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
44 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooremand13a70f2021-03-19 15:24:23 +010045 */
46psa_status_t mbedtls_psa_mac_compute(
47 const psa_key_attributes_t *attributes,
48 const uint8_t *key_buffer,
49 size_t key_buffer_size,
50 psa_algorithm_t alg,
51 const uint8_t *input,
52 size_t input_length,
53 uint8_t *mac,
54 size_t mac_size,
55 size_t *mac_length);
56
57/** Set up a multipart MAC calculation operation using Mbed TLS.
58 *
59 * \note The signature of this function is that of a PSA driver mac_sign_setup
60 * entry point. This function behaves as a mac_sign_setup entry point as
61 * defined in the PSA driver interface specification for transparent
62 * drivers.
63 *
64 * \param[in,out] operation The operation object to set up. It must have
65 * been initialized and not yet in use.
66 * \param[in] attributes The attributes of the key to use for the
67 * operation.
68 * \param[in] key_buffer The buffer containing the key to use for
69 * computing the MAC. This buffer contains the key
70 * in export representation as defined by
71 * psa_export_key() (i.e. the raw key bytes).
72 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
73 * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
74 * such that #PSA_ALG_IS_MAC(\p alg) is true).
75 *
76 * \retval #PSA_SUCCESS
77 * Success.
Steven Cooremand13a70f2021-03-19 15:24:23 +010078 * \retval #PSA_ERROR_NOT_SUPPORTED
79 * \p alg is not supported.
Gilles Peskineed733552023-02-14 19:21:09 +010080 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
81 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooremand13a70f2021-03-19 15:24:23 +010082 * \retval #PSA_ERROR_BAD_STATE
83 * The operation state is not valid (it must be inactive).
84 */
85psa_status_t mbedtls_psa_mac_sign_setup(
86 mbedtls_psa_mac_operation_t *operation,
87 const psa_key_attributes_t *attributes,
88 const uint8_t *key_buffer,
89 size_t key_buffer_size,
90 psa_algorithm_t alg);
91
92/** Set up a multipart MAC verification operation using Mbed TLS.
93 *
94 * \note The signature of this function is that of a PSA driver mac_verify_setup
95 * entry point. This function behaves as a mac_verify_setup entry point as
96 * defined in the PSA driver interface specification for transparent
97 * drivers.
98 *
99 * \param[in,out] operation The operation object to set up. It must have
100 * been initialized and not yet in use.
101 * \param[in] attributes The attributes of the key to use for the
102 * operation.
103 * \param[in] key_buffer The buffer containing the key to use for
104 * computing the MAC. This buffer contains the key
105 * in export representation as defined by
106 * psa_export_key() (i.e. the raw key bytes).
107 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
108 * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
109 * such that #PSA_ALG_IS_MAC(\p alg) is true).
110 *
111 * \retval #PSA_SUCCESS
112 * Success.
Steven Cooremand13a70f2021-03-19 15:24:23 +0100113 * \retval #PSA_ERROR_NOT_SUPPORTED
114 * \p alg is not supported.
Gilles Peskineed733552023-02-14 19:21:09 +0100115 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
116 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooremand13a70f2021-03-19 15:24:23 +0100117 * \retval #PSA_ERROR_BAD_STATE
118 * The operation state is not valid (it must be inactive).
119 */
120psa_status_t mbedtls_psa_mac_verify_setup(
121 mbedtls_psa_mac_operation_t *operation,
122 const psa_key_attributes_t *attributes,
123 const uint8_t *key_buffer,
124 size_t key_buffer_size,
125 psa_algorithm_t alg);
126
127/** Add a message fragment to a multipart MAC operation using Mbed TLS.
128 *
129 * \note The signature of this function is that of a PSA driver mac_update
130 * entry point. This function behaves as a mac_update entry point as
131 * defined in the PSA driver interface specification for transparent
132 * drivers.
133 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200134 * The PSA core calls mbedtls_psa_mac_sign_setup() or
Steven Cooremand13a70f2021-03-19 15:24:23 +0100135 * mbedtls_psa_mac_verify_setup() before calling this function.
136 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200137 * If this function returns an error status, the PSA core aborts the
138 * operation by calling mbedtls_psa_mac_abort().
Steven Cooremand13a70f2021-03-19 15:24:23 +0100139 *
140 * \param[in,out] operation Active MAC operation.
141 * \param[in] input Buffer containing the message fragment to add to
142 * the MAC calculation.
143 * \param input_length Size of the \p input buffer in bytes.
144 *
145 * \retval #PSA_SUCCESS
146 * Success.
147 * \retval #PSA_ERROR_BAD_STATE
148 * The operation state is not valid (it must be active).
Gilles Peskineed733552023-02-14 19:21:09 +0100149 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
150 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooremand13a70f2021-03-19 15:24:23 +0100151 */
152psa_status_t mbedtls_psa_mac_update(
153 mbedtls_psa_mac_operation_t *operation,
154 const uint8_t *input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 size_t input_length);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100156
157/** Finish the calculation of the MAC of a message using Mbed TLS.
158 *
159 * \note The signature of this function is that of a PSA driver mac_sign_finish
160 * entry point. This function behaves as a mac_sign_finish entry point as
161 * defined in the PSA driver interface specification for transparent
162 * drivers.
163 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200164 * The PSA core calls mbedtls_psa_mac_sign_setup() before calling this function.
Steven Cooremand13a70f2021-03-19 15:24:23 +0100165 * This function calculates the MAC of the message formed by concatenating
166 * the inputs passed to preceding calls to mbedtls_psa_mac_update().
167 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200168 * Whether this function returns successfully or not, the PSA core subsequently
169 * aborts the operation by calling mbedtls_psa_mac_abort().
Steven Cooremand13a70f2021-03-19 15:24:23 +0100170 *
171 * \param[in,out] operation Active MAC operation.
172 * \param[out] mac Buffer where the MAC value is to be written.
Steven Cooreman72f736a2021-05-07 14:14:37 +0200173 * \param mac_size Output size requested for the MAC algorithm. The PSA
174 * core guarantees this is a valid MAC length for the
175 * algorithm and key combination passed to
176 * mbedtls_psa_mac_sign_setup(). It also guarantees the
177 * \p mac buffer is large enough to contain the
178 * requested output size.
179 * \param[out] mac_length On success, the number of bytes output to buffer
180 * \p mac, which will be equal to the requested length
181 * \p mac_size.
Steven Cooremand13a70f2021-03-19 15:24:23 +0100182 *
183 * \retval #PSA_SUCCESS
184 * Success.
185 * \retval #PSA_ERROR_BAD_STATE
186 * The operation state is not valid (it must be an active mac sign
187 * operation).
188 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
189 * The size of the \p mac buffer is too small. A sufficient buffer size
190 * can be determined by calling PSA_MAC_LENGTH().
Gilles Peskineed733552023-02-14 19:21:09 +0100191 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
192 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooremand13a70f2021-03-19 15:24:23 +0100193 */
194psa_status_t mbedtls_psa_mac_sign_finish(
195 mbedtls_psa_mac_operation_t *operation,
196 uint8_t *mac,
197 size_t mac_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 size_t *mac_length);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100199
200/** Finish the calculation of the MAC of a message and compare it with
201 * an expected value using Mbed TLS.
202 *
203 * \note The signature of this function is that of a PSA driver
204 * mac_verify_finish entry point. This function behaves as a
205 * mac_verify_finish entry point as defined in the PSA driver interface
206 * specification for transparent drivers.
207 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200208 * The PSA core calls mbedtls_psa_mac_verify_setup() before calling this
Steven Cooremand13a70f2021-03-19 15:24:23 +0100209 * function. This function calculates the MAC of the message formed by
210 * concatenating the inputs passed to preceding calls to
211 * mbedtls_psa_mac_update(). It then compares the calculated MAC with the
212 * expected MAC passed as a parameter to this function.
213 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200214 * Whether this function returns successfully or not, the PSA core subsequently
215 * aborts the operation by calling mbedtls_psa_mac_abort().
Steven Cooremand13a70f2021-03-19 15:24:23 +0100216 *
217 * \param[in,out] operation Active MAC operation.
218 * \param[in] mac Buffer containing the expected MAC value.
Steven Cooreman72f736a2021-05-07 14:14:37 +0200219 * \param mac_length Length in bytes of the expected MAC value. The PSA
220 * core guarantees that this length is a valid MAC
221 * length for the algorithm and key combination passed
222 * to mbedtls_psa_mac_verify_setup().
Steven Cooremand13a70f2021-03-19 15:24:23 +0100223 *
224 * \retval #PSA_SUCCESS
225 * The expected MAC is identical to the actual MAC of the message.
226 * \retval #PSA_ERROR_INVALID_SIGNATURE
227 * The MAC of the message was calculated successfully, but it
228 * differs from the expected MAC.
229 * \retval #PSA_ERROR_BAD_STATE
230 * The operation state is not valid (it must be an active mac verify
231 * operation).
Gilles Peskineed733552023-02-14 19:21:09 +0100232 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
233 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooremand13a70f2021-03-19 15:24:23 +0100234 */
235psa_status_t mbedtls_psa_mac_verify_finish(
236 mbedtls_psa_mac_operation_t *operation,
237 const uint8_t *mac,
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 size_t mac_length);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100239
240/** Abort a MAC operation using Mbed TLS.
241 *
242 * Aborting an operation frees all associated resources except for the
243 * \p operation structure itself. Once aborted, the operation object
244 * can be reused for another operation by calling
245 * mbedtls_psa_mac_sign_setup() or mbedtls_psa_mac_verify_setup() again.
246 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200247 * The PSA core may call this function any time after the operation object has
Steven Cooremand13a70f2021-03-19 15:24:23 +0100248 * been initialized by one of the methods described in
249 * #mbedtls_psa_mac_operation_t.
250 *
251 * In particular, calling mbedtls_psa_mac_abort() after the operation has been
252 * terminated by a call to mbedtls_psa_mac_abort(),
253 * mbedtls_psa_mac_sign_finish() or mbedtls_psa_mac_verify_finish() is safe and
254 * has no effect.
255 *
256 * \param[in,out] operation Initialized MAC operation.
257 *
Gilles Peskineed733552023-02-14 19:21:09 +0100258 * \retval #PSA_SUCCESS \emptydescription
259 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooremand13a70f2021-03-19 15:24:23 +0100260 */
261psa_status_t mbedtls_psa_mac_abort(
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 mbedtls_psa_mac_operation_t *operation);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100263
Steven Cooremand13a70f2021-03-19 15:24:23 +0100264#endif /* PSA_CRYPTO_MAC_H */