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