blob: 4635fe1d7b740ed0cdbbddf9360d2b7e8df1f700 [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
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 Cooremand13a70f2021-03-19 15:24:23 +010026/** Calculate the MAC (message authentication code) of a message using Mbed TLS.
27 *
28 * \note The signature of this function is that of a PSA driver mac_compute
29 * entry point. This function behaves as a mac_compute entry point as
30 * defined in the PSA driver interface specification for transparent
31 * drivers.
32 *
33 * \param[in] attributes The attributes of the key to use for the
34 * operation.
35 * \param[in] key_buffer The buffer containing the key to use for
36 * computing the MAC. This buffer contains the key
37 * in export representation as defined by
38 * psa_export_key() (i.e. the raw key bytes).
39 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
40 * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
41 * such that #PSA_ALG_IS_MAC(\p alg) is true).
42 * \param[in] input Buffer containing the input message.
43 * \param input_length Size of the \p input buffer in bytes.
44 * \param[out] mac Buffer where the MAC value is to be written.
45 * \param mac_size Size of the \p mac buffer in bytes.
46 * \param[out] mac_length On success, the number of bytes
47 * that make up the MAC value.
48 *
49 * \retval #PSA_SUCCESS
50 * Success.
Steven Cooremand13a70f2021-03-19 15:24:23 +010051 * \retval #PSA_ERROR_NOT_SUPPORTED
52 * \p alg is not supported.
53 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
54 * \p mac_size is too small
55 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
56 * \retval #PSA_ERROR_CORRUPTION_DETECTED
57 */
58psa_status_t mbedtls_psa_mac_compute(
59 const psa_key_attributes_t *attributes,
60 const uint8_t *key_buffer,
61 size_t key_buffer_size,
62 psa_algorithm_t alg,
63 const uint8_t *input,
64 size_t input_length,
65 uint8_t *mac,
66 size_t mac_size,
67 size_t *mac_length);
68
69/** Set up a multipart MAC calculation operation using Mbed TLS.
70 *
71 * \note The signature of this function is that of a PSA driver mac_sign_setup
72 * entry point. This function behaves as a mac_sign_setup entry point as
73 * defined in the PSA driver interface specification for transparent
74 * drivers.
75 *
76 * \param[in,out] operation The operation object to set up. It must have
77 * been initialized and not yet in use.
78 * \param[in] attributes The attributes of the key to use for the
79 * operation.
80 * \param[in] key_buffer The buffer containing the key to use for
81 * computing the MAC. This buffer contains the key
82 * in export representation as defined by
83 * psa_export_key() (i.e. the raw key bytes).
84 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
85 * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
86 * such that #PSA_ALG_IS_MAC(\p alg) is true).
87 *
88 * \retval #PSA_SUCCESS
89 * Success.
Steven Cooremand13a70f2021-03-19 15:24:23 +010090 * \retval #PSA_ERROR_NOT_SUPPORTED
91 * \p alg is not supported.
92 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
93 * \retval #PSA_ERROR_CORRUPTION_DETECTED
94 * \retval #PSA_ERROR_BAD_STATE
95 * The operation state is not valid (it must be inactive).
96 */
97psa_status_t mbedtls_psa_mac_sign_setup(
98 mbedtls_psa_mac_operation_t *operation,
99 const psa_key_attributes_t *attributes,
100 const uint8_t *key_buffer,
101 size_t key_buffer_size,
102 psa_algorithm_t alg);
103
104/** Set up a multipart MAC verification operation using Mbed TLS.
105 *
106 * \note The signature of this function is that of a PSA driver mac_verify_setup
107 * entry point. This function behaves as a mac_verify_setup entry point as
108 * defined in the PSA driver interface specification for transparent
109 * drivers.
110 *
111 * \param[in,out] operation The operation object to set up. It must have
112 * been initialized and not yet in use.
113 * \param[in] attributes The attributes of the key to use for the
114 * operation.
115 * \param[in] key_buffer The buffer containing the key to use for
116 * computing the MAC. This buffer contains the key
117 * in export representation as defined by
118 * psa_export_key() (i.e. the raw key bytes).
119 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
120 * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
121 * such that #PSA_ALG_IS_MAC(\p alg) is true).
122 *
123 * \retval #PSA_SUCCESS
124 * Success.
Steven Cooremand13a70f2021-03-19 15:24:23 +0100125 * \retval #PSA_ERROR_NOT_SUPPORTED
126 * \p alg is not supported.
127 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
128 * \retval #PSA_ERROR_CORRUPTION_DETECTED
129 * \retval #PSA_ERROR_BAD_STATE
130 * The operation state is not valid (it must be inactive).
131 */
132psa_status_t mbedtls_psa_mac_verify_setup(
133 mbedtls_psa_mac_operation_t *operation,
134 const psa_key_attributes_t *attributes,
135 const uint8_t *key_buffer,
136 size_t key_buffer_size,
137 psa_algorithm_t alg);
138
139/** Add a message fragment to a multipart MAC operation using Mbed TLS.
140 *
141 * \note The signature of this function is that of a PSA driver mac_update
142 * entry point. This function behaves as a mac_update entry point as
143 * defined in the PSA driver interface specification for transparent
144 * drivers.
145 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200146 * The PSA core calls mbedtls_psa_mac_sign_setup() or
Steven Cooremand13a70f2021-03-19 15:24:23 +0100147 * mbedtls_psa_mac_verify_setup() before calling this function.
148 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200149 * If this function returns an error status, the PSA core aborts the
150 * operation by calling mbedtls_psa_mac_abort().
Steven Cooremand13a70f2021-03-19 15:24:23 +0100151 *
152 * \param[in,out] operation Active MAC operation.
153 * \param[in] input Buffer containing the message fragment to add to
154 * the MAC calculation.
155 * \param input_length Size of the \p input buffer in bytes.
156 *
157 * \retval #PSA_SUCCESS
158 * Success.
159 * \retval #PSA_ERROR_BAD_STATE
160 * The operation state is not valid (it must be active).
161 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
162 * \retval #PSA_ERROR_CORRUPTION_DETECTED
163 */
164psa_status_t mbedtls_psa_mac_update(
165 mbedtls_psa_mac_operation_t *operation,
166 const uint8_t *input,
167 size_t input_length );
168
169/** Finish the calculation of the MAC of a message using Mbed TLS.
170 *
171 * \note The signature of this function is that of a PSA driver mac_sign_finish
172 * entry point. This function behaves as a mac_sign_finish entry point as
173 * defined in the PSA driver interface specification for transparent
174 * drivers.
175 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200176 * The PSA core calls mbedtls_psa_mac_sign_setup() before calling this function.
Steven Cooremand13a70f2021-03-19 15:24:23 +0100177 * This function calculates the MAC of the message formed by concatenating
178 * the inputs passed to preceding calls to mbedtls_psa_mac_update().
179 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200180 * Whether this function returns successfully or not, the PSA core subsequently
181 * aborts the operation by calling mbedtls_psa_mac_abort().
Steven Cooremand13a70f2021-03-19 15:24:23 +0100182 *
183 * \param[in,out] operation Active MAC operation.
184 * \param[out] mac Buffer where the MAC value is to be written.
185 * \param mac_size Size of the \p mac buffer in bytes.
186 * \param[out] mac_length On success, the number of bytes
187 * that make up the MAC value. This is always
188 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
189 * where \c key_type and \c key_bits are the type and
190 * bit-size respectively of the key and \c alg is the
191 * MAC algorithm that is calculated.
192 *
193 * \retval #PSA_SUCCESS
194 * Success.
195 * \retval #PSA_ERROR_BAD_STATE
196 * The operation state is not valid (it must be an active mac sign
197 * operation).
198 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
199 * The size of the \p mac buffer is too small. A sufficient buffer size
200 * can be determined by calling PSA_MAC_LENGTH().
201 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
202 * \retval #PSA_ERROR_CORRUPTION_DETECTED
203 */
204psa_status_t mbedtls_psa_mac_sign_finish(
205 mbedtls_psa_mac_operation_t *operation,
206 uint8_t *mac,
207 size_t mac_size,
208 size_t *mac_length );
209
210/** Finish the calculation of the MAC of a message and compare it with
211 * an expected value using Mbed TLS.
212 *
213 * \note The signature of this function is that of a PSA driver
214 * mac_verify_finish entry point. This function behaves as a
215 * mac_verify_finish entry point as defined in the PSA driver interface
216 * specification for transparent drivers.
217 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200218 * The PSA core calls mbedtls_psa_mac_verify_setup() before calling this
Steven Cooremand13a70f2021-03-19 15:24:23 +0100219 * function. This function calculates the MAC of the message formed by
220 * concatenating the inputs passed to preceding calls to
221 * mbedtls_psa_mac_update(). It then compares the calculated MAC with the
222 * expected MAC passed as a parameter to this function.
223 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200224 * Whether this function returns successfully or not, the PSA core subsequently
225 * aborts the operation by calling mbedtls_psa_mac_abort().
Steven Cooremand13a70f2021-03-19 15:24:23 +0100226 *
227 * \param[in,out] operation Active MAC operation.
228 * \param[in] mac Buffer containing the expected MAC value.
229 * \param mac_length Size of the \p mac buffer in bytes.
230 *
231 * \retval #PSA_SUCCESS
232 * The expected MAC is identical to the actual MAC of the message.
233 * \retval #PSA_ERROR_INVALID_SIGNATURE
234 * The MAC of the message was calculated successfully, but it
235 * differs from the expected MAC.
236 * \retval #PSA_ERROR_BAD_STATE
237 * The operation state is not valid (it must be an active mac verify
238 * operation).
239 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
240 * \retval #PSA_ERROR_CORRUPTION_DETECTED
241 */
242psa_status_t mbedtls_psa_mac_verify_finish(
243 mbedtls_psa_mac_operation_t *operation,
244 const uint8_t *mac,
245 size_t mac_length );
246
247/** Abort a MAC operation using Mbed TLS.
248 *
249 * Aborting an operation frees all associated resources except for the
250 * \p operation structure itself. Once aborted, the operation object
251 * can be reused for another operation by calling
252 * mbedtls_psa_mac_sign_setup() or mbedtls_psa_mac_verify_setup() again.
253 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200254 * The PSA core may call this function any time after the operation object has
Steven Cooremand13a70f2021-03-19 15:24:23 +0100255 * been initialized by one of the methods described in
256 * #mbedtls_psa_mac_operation_t.
257 *
258 * In particular, calling mbedtls_psa_mac_abort() after the operation has been
259 * terminated by a call to mbedtls_psa_mac_abort(),
260 * mbedtls_psa_mac_sign_finish() or mbedtls_psa_mac_verify_finish() is safe and
261 * has no effect.
262 *
263 * \param[in,out] operation Initialized MAC operation.
264 *
265 * \retval #PSA_SUCCESS
266 * \retval #PSA_ERROR_CORRUPTION_DETECTED
267 */
268psa_status_t mbedtls_psa_mac_abort(
269 mbedtls_psa_mac_operation_t *operation );
270
271/*
272 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
273 */
274
275#if defined(PSA_CRYPTO_DRIVER_TEST)
276
277psa_status_t mbedtls_transparent_test_driver_mac_compute(
278 const psa_key_attributes_t *attributes,
279 const uint8_t *key_buffer,
280 size_t key_buffer_size,
281 psa_algorithm_t alg,
282 const uint8_t *input,
283 size_t input_length,
284 uint8_t *mac,
285 size_t mac_size,
286 size_t *mac_length );
287
288psa_status_t mbedtls_transparent_test_driver_mac_sign_setup(
289 mbedtls_transparent_test_driver_mac_operation_t *operation,
290 const psa_key_attributes_t *attributes,
291 const uint8_t *key_buffer,
292 size_t key_buffer_size,
293 psa_algorithm_t alg );
294
295psa_status_t mbedtls_transparent_test_driver_mac_verify_setup(
296 mbedtls_transparent_test_driver_mac_operation_t *operation,
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
302psa_status_t mbedtls_transparent_test_driver_mac_update(
303 mbedtls_transparent_test_driver_mac_operation_t *operation,
304 const uint8_t *input,
305 size_t input_length );
306
307psa_status_t mbedtls_transparent_test_driver_mac_sign_finish(
308 mbedtls_transparent_test_driver_mac_operation_t *operation,
309 uint8_t *mac,
310 size_t mac_size,
311 size_t *mac_length );
312
313psa_status_t mbedtls_transparent_test_driver_mac_verify_finish(
314 mbedtls_transparent_test_driver_mac_operation_t *operation,
315 const uint8_t *mac,
316 size_t mac_length );
317
318psa_status_t mbedtls_transparent_test_driver_mac_abort(
319 mbedtls_transparent_test_driver_mac_operation_t *operation );
320
321psa_status_t mbedtls_opaque_test_driver_mac_compute(
322 const psa_key_attributes_t *attributes,
323 const uint8_t *key_buffer,
324 size_t key_buffer_size,
325 psa_algorithm_t alg,
326 const uint8_t *input,
327 size_t input_length,
328 uint8_t *mac,
329 size_t mac_size,
330 size_t *mac_length );
331
332psa_status_t mbedtls_opaque_test_driver_mac_sign_setup(
333 mbedtls_opaque_test_driver_mac_operation_t *operation,
334 const psa_key_attributes_t *attributes,
335 const uint8_t *key_buffer,
336 size_t key_buffer_size,
337 psa_algorithm_t alg );
338
339psa_status_t mbedtls_opaque_test_driver_mac_verify_setup(
340 mbedtls_opaque_test_driver_mac_operation_t *operation,
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
346psa_status_t mbedtls_opaque_test_driver_mac_update(
347 mbedtls_opaque_test_driver_mac_operation_t *operation,
348 const uint8_t *input,
349 size_t input_length );
350
351psa_status_t mbedtls_opaque_test_driver_mac_sign_finish(
352 mbedtls_opaque_test_driver_mac_operation_t *operation,
353 uint8_t *mac,
354 size_t mac_size,
355 size_t *mac_length );
356
357psa_status_t mbedtls_opaque_test_driver_mac_verify_finish(
358 mbedtls_opaque_test_driver_mac_operation_t *operation,
359 const uint8_t *mac,
360 size_t mac_length );
361
362psa_status_t mbedtls_opaque_test_driver_mac_abort(
363 mbedtls_opaque_test_driver_mac_operation_t *operation );
364
365#endif /* PSA_CRYPTO_DRIVER_TEST */
366
367#endif /* PSA_CRYPTO_MAC_H */