blob: b7f2a6c3b9136130e99bce9cda963d6c538507a3 [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 Cooreman76720f62021-03-22 12:21:10 +010026/** Internal API for starting an HMAC operation, using PSA hash primitives.
27 *
28 * \note This API is not meant for application use. Applications should always
29 * use the top-level psa_mac_xxx APIs for doing HMAC operations.
30 *
31 * \param[in] hmac Context structure for this HMAC operation. Needs to have
32 * been zero-initialized prior to calling this function.
33 * \param[in] key Key to initialize the HMAC operation with.
34 * \param key_length Length (in bytes) of key \p key.
35 * \param hash_alg Hash algorithm to use for calculating the HMAC.
36 *
37 * \retval #PSA_SUCCESS
38 * Success.
39 * \return Any error code reported by psa_hash_compute(), psa_hash_setup() or
40 * psa_hash_update().
41 */
Steven Cooreman4f7cae62021-04-29 17:49:11 +020042psa_status_t psa_hmac_setup_internal( mbedtls_psa_hmac_operation_t *hmac,
Steven Cooreman32d56942021-03-19 17:39:17 +010043 const uint8_t *key,
44 size_t key_length,
45 psa_algorithm_t hash_alg );
46
Steven Cooreman76720f62021-03-22 12:21:10 +010047/** Internal API for adding data to an HMAC operation, using PSA hash primitives.
48 *
49 * \note This API is not meant for application use. Applications should always
50 * use the top-level psa_mac_xxx APIs for doing HMAC operations.
51 *
52 * \param[in] hmac Context structure for this HMAC operation. Needs to have
53 * been initialized with psa_hmac_setup_internal().
54 * \param[in] data Buffer containing the data to add to the current HMAC
55 * calculation.
56 * \param data_length Length (in bytes) of the input buffer \p data.
57 *
58 * \retval #PSA_SUCCESS
59 * Success.
60 * \return Any error code reported by psa_hash_update().
61 */
Steven Cooreman4f7cae62021-04-29 17:49:11 +020062psa_status_t psa_hmac_update_internal( mbedtls_psa_hmac_operation_t *hmac,
Steven Cooreman76720f62021-03-22 12:21:10 +010063 const uint8_t *data,
64 size_t data_length );
65
66/** Internal API for finalizing an HMAC operation, using PSA hash primitives.
67 *
68 * \note This API is not meant for application use. Applications should always
69 * use the top-level psa_mac_xxx APIs for doing HMAC operations.
70 *
71 * \param[in] hmac Context structure for this HMAC operation. Needs to have
72 * been initialized with psa_hmac_setup_internal().
73 * \param[out] mac Buffer to output the calculated HMAC into.
74 * \param mac_size Size (in bytes) of the output buffer \p mac.
75 *
76 * \retval #PSA_SUCCESS
77 * Success.
78 * \return Any error code reported by psa_hash_setup(), psa_hash_update() or
79 * psa_hash_finish().
80 */
Steven Cooreman4f7cae62021-04-29 17:49:11 +020081psa_status_t psa_hmac_finish_internal( mbedtls_psa_hmac_operation_t *hmac,
Steven Cooreman32d56942021-03-19 17:39:17 +010082 uint8_t *mac,
83 size_t mac_size );
84
Steven Cooreman76720f62021-03-22 12:21:10 +010085/** Internal API for cloning an HMAC operation, using PSA hash primitives.
86 *
87 * \note This API is not meant for application use. Applications should always
88 * use the top-level psa_mac_xxx APIs for doing HMAC operations.
89 *
90 * \param[in] source Context structure to clone from. Needs to have been
91 * initialized with psa_hmac_setup_internal().
92 * \param[out] destination Context structure to clone to. Needs to have been
93 * zero-initialized.
94 *
95 * \retval #PSA_SUCCESS
96 * Success.
97 * \return Any error code reported by psa_hash_clone().
98 */
Steven Cooreman4f7cae62021-04-29 17:49:11 +020099psa_status_t psa_hmac_clone_internal(
100 const mbedtls_psa_hmac_operation_t *source,
101 mbedtls_psa_hmac_operation_t *destination );
Steven Cooreman76720f62021-03-22 12:21:10 +0100102
103/** Internal API for aborting an HMAC operation, using PSA hash primitives.
104 *
105 * \note This API is not meant for application use. Applications should always
106 * use the top-level psa_mac_xxx APIs for doing HMAC operations.
107 *
108 * \param[in] hmac Context structure for the HMAC operation to abort.
109 *
110 * \retval #PSA_SUCCESS
111 * Success.
112 * \return Any error code reported by psa_hash_abort().
113 */
Steven Cooreman4f7cae62021-04-29 17:49:11 +0200114psa_status_t psa_hmac_abort_internal( mbedtls_psa_hmac_operation_t *hmac );
Steven Cooreman32d56942021-03-19 17:39:17 +0100115
Steven Cooreman896d51e2021-03-19 15:24:23 +0100116/** Calculate the MAC (message authentication code) of a message using Mbed TLS.
117 *
118 * \note The signature of this function is that of a PSA driver mac_compute
119 * entry point. This function behaves as a mac_compute entry point as
120 * defined in the PSA driver interface specification for transparent
121 * drivers.
122 *
123 * \param[in] attributes The attributes of the key to use for the
124 * operation.
125 * \param[in] key_buffer The buffer containing the key to use for
126 * computing the MAC. This buffer contains the key
127 * in export representation as defined by
128 * psa_export_key() (i.e. the raw key bytes).
129 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
130 * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
131 * such that #PSA_ALG_IS_MAC(\p alg) is true).
132 * \param[in] input Buffer containing the input message.
133 * \param input_length Size of the \p input buffer in bytes.
134 * \param[out] mac Buffer where the MAC value is to be written.
135 * \param mac_size Size of the \p mac buffer in bytes.
136 * \param[out] mac_length On success, the number of bytes
137 * that make up the MAC value.
138 *
139 * \retval #PSA_SUCCESS
140 * Success.
141 * \retval #PSA_ERROR_INVALID_ARGUMENT
142 * The key is not compatible with \p alg.
143 * \retval #PSA_ERROR_NOT_SUPPORTED
144 * \p alg is not supported.
145 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
146 * \p mac_size is too small
147 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
148 * \retval #PSA_ERROR_CORRUPTION_DETECTED
149 */
150psa_status_t mbedtls_psa_mac_compute(
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 const uint8_t *input,
156 size_t input_length,
157 uint8_t *mac,
158 size_t mac_size,
159 size_t *mac_length);
160
161/** Set up a multipart MAC calculation operation using Mbed TLS.
162 *
163 * \note The signature of this function is that of a PSA driver mac_sign_setup
164 * entry point. This function behaves as a mac_sign_setup entry point as
165 * defined in the PSA driver interface specification for transparent
166 * drivers.
167 *
168 * \param[in,out] operation The operation object to set up. It must have
169 * been initialized and not yet in use.
170 * \param[in] attributes The attributes of the key to use for the
171 * operation.
172 * \param[in] key_buffer The buffer containing the key to use for
173 * computing the MAC. This buffer contains the key
174 * in export representation as defined by
175 * psa_export_key() (i.e. the raw key bytes).
176 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
177 * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
178 * such that #PSA_ALG_IS_MAC(\p alg) is true).
179 *
180 * \retval #PSA_SUCCESS
181 * Success.
182 * \retval #PSA_ERROR_INVALID_ARGUMENT
183 * The key is not compatible with \p alg.
184 * \retval #PSA_ERROR_NOT_SUPPORTED
185 * \p alg is not supported.
186 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
187 * \retval #PSA_ERROR_CORRUPTION_DETECTED
188 * \retval #PSA_ERROR_BAD_STATE
189 * The operation state is not valid (it must be inactive).
190 */
191psa_status_t mbedtls_psa_mac_sign_setup(
192 mbedtls_psa_mac_operation_t *operation,
193 const psa_key_attributes_t *attributes,
194 const uint8_t *key_buffer,
195 size_t key_buffer_size,
196 psa_algorithm_t alg);
197
198/** Set up a multipart MAC verification operation using Mbed TLS.
199 *
200 * \note The signature of this function is that of a PSA driver mac_verify_setup
201 * entry point. This function behaves as a mac_verify_setup entry point as
202 * defined in the PSA driver interface specification for transparent
203 * drivers.
204 *
205 * \param[in,out] operation The operation object to set up. It must have
206 * been initialized and not yet in use.
207 * \param[in] attributes The attributes of the key to use for the
208 * operation.
209 * \param[in] key_buffer The buffer containing the key to use for
210 * computing the MAC. This buffer contains the key
211 * in export representation as defined by
212 * psa_export_key() (i.e. the raw key bytes).
213 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
214 * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
215 * such that #PSA_ALG_IS_MAC(\p alg) is true).
216 *
217 * \retval #PSA_SUCCESS
218 * Success.
219 * \retval #PSA_ERROR_INVALID_ARGUMENT
220 * The key is not compatible with \p alg.
221 * \retval #PSA_ERROR_NOT_SUPPORTED
222 * \p alg is not supported.
223 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
224 * \retval #PSA_ERROR_CORRUPTION_DETECTED
225 * \retval #PSA_ERROR_BAD_STATE
226 * The operation state is not valid (it must be inactive).
227 */
228psa_status_t mbedtls_psa_mac_verify_setup(
229 mbedtls_psa_mac_operation_t *operation,
230 const psa_key_attributes_t *attributes,
231 const uint8_t *key_buffer,
232 size_t key_buffer_size,
233 psa_algorithm_t alg);
234
235/** Add a message fragment to a multipart MAC operation using Mbed TLS.
236 *
237 * \note The signature of this function is that of a PSA driver mac_update
238 * entry point. This function behaves as a mac_update entry point as
239 * defined in the PSA driver interface specification for transparent
240 * drivers.
241 *
242 * The core must call mbedtls_psa_mac_sign_setup() or
243 * mbedtls_psa_mac_verify_setup() before calling this function.
244 *
245 * If this function returns an error status, the operation enters an error
246 * state and must be aborted by calling psa_mac_abort().
247 *
248 * \param[in,out] operation Active MAC operation.
249 * \param[in] input Buffer containing the message fragment to add to
250 * the MAC calculation.
251 * \param input_length Size of the \p input buffer in bytes.
252 *
253 * \retval #PSA_SUCCESS
254 * Success.
255 * \retval #PSA_ERROR_BAD_STATE
256 * The operation state is not valid (it must be active).
257 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
258 * \retval #PSA_ERROR_CORRUPTION_DETECTED
259 */
260psa_status_t mbedtls_psa_mac_update(
261 mbedtls_psa_mac_operation_t *operation,
262 const uint8_t *input,
263 size_t input_length );
264
265/** Finish the calculation of the MAC of a message using Mbed TLS.
266 *
267 * \note The signature of this function is that of a PSA driver mac_sign_finish
268 * entry point. This function behaves as a mac_sign_finish entry point as
269 * defined in the PSA driver interface specification for transparent
270 * drivers.
271 *
272 * The core must call mbedtls_psa_mac_sign_setup() before calling this function.
273 * This function calculates the MAC of the message formed by concatenating
274 * the inputs passed to preceding calls to mbedtls_psa_mac_update().
275 *
276 * When this function returns successfully, the operation becomes inactive.
277 * If this function returns an error status, the operation enters an error
278 * state and must be aborted by calling mbedtls_psa_mac_abort().
279 *
280 * \param[in,out] operation Active MAC operation.
281 * \param[out] mac Buffer where the MAC value is to be written.
282 * \param mac_size Size of the \p mac buffer in bytes.
283 * \param[out] mac_length On success, the number of bytes
284 * that make up the MAC value. This is always
285 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
286 * where \c key_type and \c key_bits are the type and
287 * bit-size respectively of the key and \c alg is the
288 * MAC algorithm that is calculated.
289 *
290 * \retval #PSA_SUCCESS
291 * Success.
292 * \retval #PSA_ERROR_BAD_STATE
293 * The operation state is not valid (it must be an active mac sign
294 * operation).
295 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
296 * The size of the \p mac buffer is too small. A sufficient buffer size
297 * can be determined by calling PSA_MAC_LENGTH().
298 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
299 * \retval #PSA_ERROR_CORRUPTION_DETECTED
300 */
301psa_status_t mbedtls_psa_mac_sign_finish(
302 mbedtls_psa_mac_operation_t *operation,
303 uint8_t *mac,
304 size_t mac_size,
305 size_t *mac_length );
306
307/** Finish the calculation of the MAC of a message and compare it with
308 * an expected value using Mbed TLS.
309 *
310 * \note The signature of this function is that of a PSA driver
311 * mac_verify_finish entry point. This function behaves as a
312 * mac_verify_finish entry point as defined in the PSA driver interface
313 * specification for transparent drivers.
314 *
315 * The core must call mbedtls_psa_mac_verify_setup() before calling this
316 * function. This function calculates the MAC of the message formed by
317 * concatenating the inputs passed to preceding calls to
318 * mbedtls_psa_mac_update(). It then compares the calculated MAC with the
319 * expected MAC passed as a parameter to this function.
320 *
321 * When this function returns successfully, the operation becomes inactive.
322 * If this function returns an error status, the operation enters an error
323 * state and must be aborted by calling mbedtls_psa_mac_abort().
324 *
325 * \param[in,out] operation Active MAC operation.
326 * \param[in] mac Buffer containing the expected MAC value.
327 * \param mac_length Size of the \p mac buffer in bytes.
328 *
329 * \retval #PSA_SUCCESS
330 * The expected MAC is identical to the actual MAC of the message.
331 * \retval #PSA_ERROR_INVALID_SIGNATURE
332 * The MAC of the message was calculated successfully, but it
333 * differs from the expected MAC.
334 * \retval #PSA_ERROR_BAD_STATE
335 * The operation state is not valid (it must be an active mac verify
336 * operation).
337 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
338 * \retval #PSA_ERROR_CORRUPTION_DETECTED
339 */
340psa_status_t mbedtls_psa_mac_verify_finish(
341 mbedtls_psa_mac_operation_t *operation,
342 const uint8_t *mac,
343 size_t mac_length );
344
345/** Abort a MAC operation using Mbed TLS.
346 *
347 * Aborting an operation frees all associated resources except for the
348 * \p operation structure itself. Once aborted, the operation object
349 * can be reused for another operation by calling
350 * mbedtls_psa_mac_sign_setup() or mbedtls_psa_mac_verify_setup() again.
351 *
352 * The core may call this function any time after the operation object has
353 * been initialized by one of the methods described in
354 * #mbedtls_psa_mac_operation_t.
355 *
356 * In particular, calling mbedtls_psa_mac_abort() after the operation has been
357 * terminated by a call to mbedtls_psa_mac_abort(),
358 * mbedtls_psa_mac_sign_finish() or mbedtls_psa_mac_verify_finish() is safe and
359 * has no effect.
360 *
361 * \param[in,out] operation Initialized MAC operation.
362 *
363 * \retval #PSA_SUCCESS
364 * \retval #PSA_ERROR_CORRUPTION_DETECTED
365 */
366psa_status_t mbedtls_psa_mac_abort(
367 mbedtls_psa_mac_operation_t *operation );
368
369/*
370 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
371 */
372
373#if defined(PSA_CRYPTO_DRIVER_TEST)
374
375psa_status_t mbedtls_transparent_test_driver_mac_compute(
376 const psa_key_attributes_t *attributes,
377 const uint8_t *key_buffer,
378 size_t key_buffer_size,
379 psa_algorithm_t alg,
380 const uint8_t *input,
381 size_t input_length,
382 uint8_t *mac,
383 size_t mac_size,
384 size_t *mac_length );
385
386psa_status_t mbedtls_transparent_test_driver_mac_sign_setup(
387 mbedtls_transparent_test_driver_mac_operation_t *operation,
388 const psa_key_attributes_t *attributes,
389 const uint8_t *key_buffer,
390 size_t key_buffer_size,
391 psa_algorithm_t alg );
392
393psa_status_t mbedtls_transparent_test_driver_mac_verify_setup(
394 mbedtls_transparent_test_driver_mac_operation_t *operation,
395 const psa_key_attributes_t *attributes,
396 const uint8_t *key_buffer,
397 size_t key_buffer_size,
398 psa_algorithm_t alg );
399
400psa_status_t mbedtls_transparent_test_driver_mac_update(
401 mbedtls_transparent_test_driver_mac_operation_t *operation,
402 const uint8_t *input,
403 size_t input_length );
404
405psa_status_t mbedtls_transparent_test_driver_mac_sign_finish(
406 mbedtls_transparent_test_driver_mac_operation_t *operation,
407 uint8_t *mac,
408 size_t mac_size,
409 size_t *mac_length );
410
411psa_status_t mbedtls_transparent_test_driver_mac_verify_finish(
412 mbedtls_transparent_test_driver_mac_operation_t *operation,
413 const uint8_t *mac,
414 size_t mac_length );
415
416psa_status_t mbedtls_transparent_test_driver_mac_abort(
417 mbedtls_transparent_test_driver_mac_operation_t *operation );
418
419psa_status_t mbedtls_opaque_test_driver_mac_compute(
420 const psa_key_attributes_t *attributes,
421 const uint8_t *key_buffer,
422 size_t key_buffer_size,
423 psa_algorithm_t alg,
424 const uint8_t *input,
425 size_t input_length,
426 uint8_t *mac,
427 size_t mac_size,
428 size_t *mac_length );
429
430psa_status_t mbedtls_opaque_test_driver_mac_sign_setup(
431 mbedtls_opaque_test_driver_mac_operation_t *operation,
432 const psa_key_attributes_t *attributes,
433 const uint8_t *key_buffer,
434 size_t key_buffer_size,
435 psa_algorithm_t alg );
436
437psa_status_t mbedtls_opaque_test_driver_mac_verify_setup(
438 mbedtls_opaque_test_driver_mac_operation_t *operation,
439 const psa_key_attributes_t *attributes,
440 const uint8_t *key_buffer,
441 size_t key_buffer_size,
442 psa_algorithm_t alg );
443
444psa_status_t mbedtls_opaque_test_driver_mac_update(
445 mbedtls_opaque_test_driver_mac_operation_t *operation,
446 const uint8_t *input,
447 size_t input_length );
448
449psa_status_t mbedtls_opaque_test_driver_mac_sign_finish(
450 mbedtls_opaque_test_driver_mac_operation_t *operation,
451 uint8_t *mac,
452 size_t mac_size,
453 size_t *mac_length );
454
455psa_status_t mbedtls_opaque_test_driver_mac_verify_finish(
456 mbedtls_opaque_test_driver_mac_operation_t *operation,
457 const uint8_t *mac,
458 size_t mac_length );
459
460psa_status_t mbedtls_opaque_test_driver_mac_abort(
461 mbedtls_opaque_test_driver_mac_operation_t *operation );
462
463#endif /* PSA_CRYPTO_DRIVER_TEST */
464
465#endif /* PSA_CRYPTO_MAC_H */