blob: d923511605468e12560d8d83dfdf0d970d7bbcac [file] [log] [blame]
Steven Cooreman896d51e2021-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
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#ifndef PSA_CRYPTO_MAC_H
22#define PSA_CRYPTO_MAC_H
23
24#include <psa/crypto.h>
25
Steven Cooreman32d56942021-03-19 17:39:17 +010026psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
27 const uint8_t *key,
28 size_t key_length,
29 psa_algorithm_t hash_alg );
30
31psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
32 uint8_t *mac,
33 size_t mac_size );
34
35psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac );
36
Steven Cooreman896d51e2021-03-19 15:24:23 +010037/** Calculate the MAC (message authentication code) of a message using Mbed TLS.
38 *
39 * \note The signature of this function is that of a PSA driver mac_compute
40 * entry point. This function behaves as a mac_compute entry point as
41 * defined in the PSA driver interface specification for transparent
42 * drivers.
43 *
44 * \param[in] attributes The attributes of the key to use for the
45 * operation.
46 * \param[in] key_buffer The buffer containing the key to use for
47 * computing the MAC. This buffer contains the key
48 * in export representation as defined by
49 * psa_export_key() (i.e. the raw key bytes).
50 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
51 * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
52 * such that #PSA_ALG_IS_MAC(\p alg) is true).
53 * \param[in] input Buffer containing the input message.
54 * \param input_length Size of the \p input buffer in bytes.
55 * \param[out] mac Buffer where the MAC value is to be written.
56 * \param mac_size Size of the \p mac buffer in bytes.
57 * \param[out] mac_length On success, the number of bytes
58 * that make up the MAC value.
59 *
60 * \retval #PSA_SUCCESS
61 * Success.
62 * \retval #PSA_ERROR_INVALID_ARGUMENT
63 * The key is not compatible with \p alg.
64 * \retval #PSA_ERROR_NOT_SUPPORTED
65 * \p alg is not supported.
66 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
67 * \p mac_size is too small
68 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
69 * \retval #PSA_ERROR_CORRUPTION_DETECTED
70 */
71psa_status_t mbedtls_psa_mac_compute(
72 const psa_key_attributes_t *attributes,
73 const uint8_t *key_buffer,
74 size_t key_buffer_size,
75 psa_algorithm_t alg,
76 const uint8_t *input,
77 size_t input_length,
78 uint8_t *mac,
79 size_t mac_size,
80 size_t *mac_length);
81
82/** Set up a multipart MAC calculation operation using Mbed TLS.
83 *
84 * \note The signature of this function is that of a PSA driver mac_sign_setup
85 * entry point. This function behaves as a mac_sign_setup entry point as
86 * defined in the PSA driver interface specification for transparent
87 * drivers.
88 *
89 * \param[in,out] operation The operation object to set up. It must have
90 * been initialized and not yet in use.
91 * \param[in] attributes The attributes of the key to use for the
92 * operation.
93 * \param[in] key_buffer The buffer containing the key to use for
94 * computing the MAC. This buffer contains the key
95 * in export representation as defined by
96 * psa_export_key() (i.e. the raw key bytes).
97 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
98 * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
99 * such that #PSA_ALG_IS_MAC(\p alg) is true).
100 *
101 * \retval #PSA_SUCCESS
102 * Success.
103 * \retval #PSA_ERROR_INVALID_ARGUMENT
104 * The key is not compatible with \p alg.
105 * \retval #PSA_ERROR_NOT_SUPPORTED
106 * \p alg is not supported.
107 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
108 * \retval #PSA_ERROR_CORRUPTION_DETECTED
109 * \retval #PSA_ERROR_BAD_STATE
110 * The operation state is not valid (it must be inactive).
111 */
112psa_status_t mbedtls_psa_mac_sign_setup(
113 mbedtls_psa_mac_operation_t *operation,
114 const psa_key_attributes_t *attributes,
115 const uint8_t *key_buffer,
116 size_t key_buffer_size,
117 psa_algorithm_t alg);
118
119/** Set up a multipart MAC verification operation using Mbed TLS.
120 *
121 * \note The signature of this function is that of a PSA driver mac_verify_setup
122 * entry point. This function behaves as a mac_verify_setup entry point as
123 * defined in the PSA driver interface specification for transparent
124 * drivers.
125 *
126 * \param[in,out] operation The operation object to set up. It must have
127 * been initialized and not yet in use.
128 * \param[in] attributes The attributes of the key to use for the
129 * operation.
130 * \param[in] key_buffer The buffer containing the key to use for
131 * computing the MAC. This buffer contains the key
132 * in export representation as defined by
133 * psa_export_key() (i.e. the raw key bytes).
134 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
135 * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
136 * such that #PSA_ALG_IS_MAC(\p alg) is true).
137 *
138 * \retval #PSA_SUCCESS
139 * Success.
140 * \retval #PSA_ERROR_INVALID_ARGUMENT
141 * The key is not compatible with \p alg.
142 * \retval #PSA_ERROR_NOT_SUPPORTED
143 * \p alg is not supported.
144 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
145 * \retval #PSA_ERROR_CORRUPTION_DETECTED
146 * \retval #PSA_ERROR_BAD_STATE
147 * The operation state is not valid (it must be inactive).
148 */
149psa_status_t mbedtls_psa_mac_verify_setup(
150 mbedtls_psa_mac_operation_t *operation,
151 const psa_key_attributes_t *attributes,
152 const uint8_t *key_buffer,
153 size_t key_buffer_size,
154 psa_algorithm_t alg);
155
156/** Add a message fragment to a multipart MAC operation using Mbed TLS.
157 *
158 * \note The signature of this function is that of a PSA driver mac_update
159 * entry point. This function behaves as a mac_update entry point as
160 * defined in the PSA driver interface specification for transparent
161 * drivers.
162 *
163 * The core must call mbedtls_psa_mac_sign_setup() or
164 * mbedtls_psa_mac_verify_setup() before calling this function.
165 *
166 * If this function returns an error status, the operation enters an error
167 * state and must be aborted by calling psa_mac_abort().
168 *
169 * \param[in,out] operation Active MAC operation.
170 * \param[in] input Buffer containing the message fragment to add to
171 * the MAC calculation.
172 * \param input_length Size of the \p input buffer in bytes.
173 *
174 * \retval #PSA_SUCCESS
175 * Success.
176 * \retval #PSA_ERROR_BAD_STATE
177 * The operation state is not valid (it must be active).
178 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
179 * \retval #PSA_ERROR_CORRUPTION_DETECTED
180 */
181psa_status_t mbedtls_psa_mac_update(
182 mbedtls_psa_mac_operation_t *operation,
183 const uint8_t *input,
184 size_t input_length );
185
186/** Finish the calculation of the MAC of a message using Mbed TLS.
187 *
188 * \note The signature of this function is that of a PSA driver mac_sign_finish
189 * entry point. This function behaves as a mac_sign_finish entry point as
190 * defined in the PSA driver interface specification for transparent
191 * drivers.
192 *
193 * The core must call mbedtls_psa_mac_sign_setup() before calling this function.
194 * This function calculates the MAC of the message formed by concatenating
195 * the inputs passed to preceding calls to mbedtls_psa_mac_update().
196 *
197 * When this function returns successfully, the operation becomes inactive.
198 * If this function returns an error status, the operation enters an error
199 * state and must be aborted by calling mbedtls_psa_mac_abort().
200 *
201 * \param[in,out] operation Active MAC operation.
202 * \param[out] mac Buffer where the MAC value is to be written.
203 * \param mac_size Size of the \p mac buffer in bytes.
204 * \param[out] mac_length On success, the number of bytes
205 * that make up the MAC value. This is always
206 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
207 * where \c key_type and \c key_bits are the type and
208 * bit-size respectively of the key and \c alg is the
209 * MAC algorithm that is calculated.
210 *
211 * \retval #PSA_SUCCESS
212 * Success.
213 * \retval #PSA_ERROR_BAD_STATE
214 * The operation state is not valid (it must be an active mac sign
215 * operation).
216 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
217 * The size of the \p mac buffer is too small. A sufficient buffer size
218 * can be determined by calling PSA_MAC_LENGTH().
219 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
220 * \retval #PSA_ERROR_CORRUPTION_DETECTED
221 */
222psa_status_t mbedtls_psa_mac_sign_finish(
223 mbedtls_psa_mac_operation_t *operation,
224 uint8_t *mac,
225 size_t mac_size,
226 size_t *mac_length );
227
228/** Finish the calculation of the MAC of a message and compare it with
229 * an expected value using Mbed TLS.
230 *
231 * \note The signature of this function is that of a PSA driver
232 * mac_verify_finish entry point. This function behaves as a
233 * mac_verify_finish entry point as defined in the PSA driver interface
234 * specification for transparent drivers.
235 *
236 * The core must call mbedtls_psa_mac_verify_setup() before calling this
237 * function. This function calculates the MAC of the message formed by
238 * concatenating the inputs passed to preceding calls to
239 * mbedtls_psa_mac_update(). It then compares the calculated MAC with the
240 * expected MAC passed as a parameter to this function.
241 *
242 * When this function returns successfully, the operation becomes inactive.
243 * If this function returns an error status, the operation enters an error
244 * state and must be aborted by calling mbedtls_psa_mac_abort().
245 *
246 * \param[in,out] operation Active MAC operation.
247 * \param[in] mac Buffer containing the expected MAC value.
248 * \param mac_length Size of the \p mac buffer in bytes.
249 *
250 * \retval #PSA_SUCCESS
251 * The expected MAC is identical to the actual MAC of the message.
252 * \retval #PSA_ERROR_INVALID_SIGNATURE
253 * The MAC of the message was calculated successfully, but it
254 * differs from the expected MAC.
255 * \retval #PSA_ERROR_BAD_STATE
256 * The operation state is not valid (it must be an active mac verify
257 * operation).
258 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
259 * \retval #PSA_ERROR_CORRUPTION_DETECTED
260 */
261psa_status_t mbedtls_psa_mac_verify_finish(
262 mbedtls_psa_mac_operation_t *operation,
263 const uint8_t *mac,
264 size_t mac_length );
265
266/** Abort a MAC operation using Mbed TLS.
267 *
268 * Aborting an operation frees all associated resources except for the
269 * \p operation structure itself. Once aborted, the operation object
270 * can be reused for another operation by calling
271 * mbedtls_psa_mac_sign_setup() or mbedtls_psa_mac_verify_setup() again.
272 *
273 * The core may call this function any time after the operation object has
274 * been initialized by one of the methods described in
275 * #mbedtls_psa_mac_operation_t.
276 *
277 * In particular, calling mbedtls_psa_mac_abort() after the operation has been
278 * terminated by a call to mbedtls_psa_mac_abort(),
279 * mbedtls_psa_mac_sign_finish() or mbedtls_psa_mac_verify_finish() is safe and
280 * has no effect.
281 *
282 * \param[in,out] operation Initialized MAC operation.
283 *
284 * \retval #PSA_SUCCESS
285 * \retval #PSA_ERROR_CORRUPTION_DETECTED
286 */
287psa_status_t mbedtls_psa_mac_abort(
288 mbedtls_psa_mac_operation_t *operation );
289
290/*
291 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
292 */
293
294#if defined(PSA_CRYPTO_DRIVER_TEST)
295
296psa_status_t mbedtls_transparent_test_driver_mac_compute(
297 const psa_key_attributes_t *attributes,
298 const uint8_t *key_buffer,
299 size_t key_buffer_size,
300 psa_algorithm_t alg,
301 const uint8_t *input,
302 size_t input_length,
303 uint8_t *mac,
304 size_t mac_size,
305 size_t *mac_length );
306
307psa_status_t mbedtls_transparent_test_driver_mac_sign_setup(
308 mbedtls_transparent_test_driver_mac_operation_t *operation,
309 const psa_key_attributes_t *attributes,
310 const uint8_t *key_buffer,
311 size_t key_buffer_size,
312 psa_algorithm_t alg );
313
314psa_status_t mbedtls_transparent_test_driver_mac_verify_setup(
315 mbedtls_transparent_test_driver_mac_operation_t *operation,
316 const psa_key_attributes_t *attributes,
317 const uint8_t *key_buffer,
318 size_t key_buffer_size,
319 psa_algorithm_t alg );
320
321psa_status_t mbedtls_transparent_test_driver_mac_update(
322 mbedtls_transparent_test_driver_mac_operation_t *operation,
323 const uint8_t *input,
324 size_t input_length );
325
326psa_status_t mbedtls_transparent_test_driver_mac_sign_finish(
327 mbedtls_transparent_test_driver_mac_operation_t *operation,
328 uint8_t *mac,
329 size_t mac_size,
330 size_t *mac_length );
331
332psa_status_t mbedtls_transparent_test_driver_mac_verify_finish(
333 mbedtls_transparent_test_driver_mac_operation_t *operation,
334 const uint8_t *mac,
335 size_t mac_length );
336
337psa_status_t mbedtls_transparent_test_driver_mac_abort(
338 mbedtls_transparent_test_driver_mac_operation_t *operation );
339
340psa_status_t mbedtls_opaque_test_driver_mac_compute(
341 const psa_key_attributes_t *attributes,
342 const uint8_t *key_buffer,
343 size_t key_buffer_size,
344 psa_algorithm_t alg,
345 const uint8_t *input,
346 size_t input_length,
347 uint8_t *mac,
348 size_t mac_size,
349 size_t *mac_length );
350
351psa_status_t mbedtls_opaque_test_driver_mac_sign_setup(
352 mbedtls_opaque_test_driver_mac_operation_t *operation,
353 const psa_key_attributes_t *attributes,
354 const uint8_t *key_buffer,
355 size_t key_buffer_size,
356 psa_algorithm_t alg );
357
358psa_status_t mbedtls_opaque_test_driver_mac_verify_setup(
359 mbedtls_opaque_test_driver_mac_operation_t *operation,
360 const psa_key_attributes_t *attributes,
361 const uint8_t *key_buffer,
362 size_t key_buffer_size,
363 psa_algorithm_t alg );
364
365psa_status_t mbedtls_opaque_test_driver_mac_update(
366 mbedtls_opaque_test_driver_mac_operation_t *operation,
367 const uint8_t *input,
368 size_t input_length );
369
370psa_status_t mbedtls_opaque_test_driver_mac_sign_finish(
371 mbedtls_opaque_test_driver_mac_operation_t *operation,
372 uint8_t *mac,
373 size_t mac_size,
374 size_t *mac_length );
375
376psa_status_t mbedtls_opaque_test_driver_mac_verify_finish(
377 mbedtls_opaque_test_driver_mac_operation_t *operation,
378 const uint8_t *mac,
379 size_t mac_length );
380
381psa_status_t mbedtls_opaque_test_driver_mac_abort(
382 mbedtls_opaque_test_driver_mac_operation_t *operation );
383
384#endif /* PSA_CRYPTO_DRIVER_TEST */
385
386#endif /* PSA_CRYPTO_MAC_H */