blob: 8ca271c09eabfa969fb09b12577ea78c44adea67 [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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020058psa_status_t mbedtls_psa_mac_compute(const psa_key_attributes_t *attributes,
59 const uint8_t *key_buffer,
60 size_t key_buffer_size,
61 psa_algorithm_t alg,
62 const uint8_t *input,
63 size_t input_length,
64 uint8_t *mac,
65 size_t mac_size,
66 size_t *mac_length);
Steven Cooremand13a70f2021-03-19 15:24:23 +010067
68/** Set up a multipart MAC calculation operation using Mbed TLS.
69 *
70 * \note The signature of this function is that of a PSA driver mac_sign_setup
71 * entry point. This function behaves as a mac_sign_setup entry point as
72 * defined in the PSA driver interface specification for transparent
73 * drivers.
74 *
75 * \param[in,out] operation The operation object to set up. It must have
76 * been initialized and not yet in use.
77 * \param[in] attributes The attributes of the key to use for the
78 * operation.
79 * \param[in] key_buffer The buffer containing the key to use for
80 * computing the MAC. This buffer contains the key
81 * in export representation as defined by
82 * psa_export_key() (i.e. the raw key bytes).
83 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
84 * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
85 * such that #PSA_ALG_IS_MAC(\p alg) is true).
86 *
87 * \retval #PSA_SUCCESS
88 * Success.
Steven Cooremand13a70f2021-03-19 15:24:23 +010089 * \retval #PSA_ERROR_NOT_SUPPORTED
90 * \p alg is not supported.
91 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
92 * \retval #PSA_ERROR_CORRUPTION_DETECTED
93 * \retval #PSA_ERROR_BAD_STATE
94 * The operation state is not valid (it must be inactive).
95 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020096psa_status_t mbedtls_psa_mac_sign_setup(mbedtls_psa_mac_operation_t *operation,
97 const psa_key_attributes_t *attributes,
98 const uint8_t *key_buffer,
99 size_t key_buffer_size,
100 psa_algorithm_t alg);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100101
102/** Set up a multipart MAC verification operation using Mbed TLS.
103 *
104 * \note The signature of this function is that of a PSA driver mac_verify_setup
105 * entry point. This function behaves as a mac_verify_setup entry point as
106 * defined in the PSA driver interface specification for transparent
107 * drivers.
108 *
109 * \param[in,out] operation The operation object to set up. It must have
110 * been initialized and not yet in use.
111 * \param[in] attributes The attributes of the key to use for the
112 * operation.
113 * \param[in] key_buffer The buffer containing the key to use for
114 * computing the MAC. This buffer contains the key
115 * in export representation as defined by
116 * psa_export_key() (i.e. the raw key bytes).
117 * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
118 * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
119 * such that #PSA_ALG_IS_MAC(\p alg) is true).
120 *
121 * \retval #PSA_SUCCESS
122 * Success.
Steven Cooremand13a70f2021-03-19 15:24:23 +0100123 * \retval #PSA_ERROR_NOT_SUPPORTED
124 * \p alg is not supported.
125 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
126 * \retval #PSA_ERROR_CORRUPTION_DETECTED
127 * \retval #PSA_ERROR_BAD_STATE
128 * The operation state is not valid (it must be inactive).
129 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200130psa_status_t
131mbedtls_psa_mac_verify_setup(mbedtls_psa_mac_operation_t *operation,
132 const psa_key_attributes_t *attributes,
133 const uint8_t *key_buffer,
134 size_t key_buffer_size,
135 psa_algorithm_t alg);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100136
137/** Add a message fragment to a multipart MAC operation using Mbed TLS.
138 *
139 * \note The signature of this function is that of a PSA driver mac_update
140 * entry point. This function behaves as a mac_update entry point as
141 * defined in the PSA driver interface specification for transparent
142 * drivers.
143 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200144 * The PSA core calls mbedtls_psa_mac_sign_setup() or
Steven Cooremand13a70f2021-03-19 15:24:23 +0100145 * mbedtls_psa_mac_verify_setup() before calling this function.
146 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200147 * If this function returns an error status, the PSA core aborts the
148 * operation by calling mbedtls_psa_mac_abort().
Steven Cooremand13a70f2021-03-19 15:24:23 +0100149 *
150 * \param[in,out] operation Active MAC operation.
151 * \param[in] input Buffer containing the message fragment to add to
152 * the MAC calculation.
153 * \param input_length Size of the \p input buffer in bytes.
154 *
155 * \retval #PSA_SUCCESS
156 * Success.
157 * \retval #PSA_ERROR_BAD_STATE
158 * The operation state is not valid (it must be active).
159 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
160 * \retval #PSA_ERROR_CORRUPTION_DETECTED
161 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200162psa_status_t mbedtls_psa_mac_update(mbedtls_psa_mac_operation_t *operation,
163 const uint8_t *input,
164 size_t input_length);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100165
166/** Finish the calculation of the MAC of a message using Mbed TLS.
167 *
168 * \note The signature of this function is that of a PSA driver mac_sign_finish
169 * entry point. This function behaves as a mac_sign_finish entry point as
170 * defined in the PSA driver interface specification for transparent
171 * drivers.
172 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200173 * The PSA core calls mbedtls_psa_mac_sign_setup() before calling this function.
Steven Cooremand13a70f2021-03-19 15:24:23 +0100174 * This function calculates the MAC of the message formed by concatenating
175 * the inputs passed to preceding calls to mbedtls_psa_mac_update().
176 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200177 * Whether this function returns successfully or not, the PSA core subsequently
178 * aborts the operation by calling mbedtls_psa_mac_abort().
Steven Cooremand13a70f2021-03-19 15:24:23 +0100179 *
180 * \param[in,out] operation Active MAC operation.
181 * \param[out] mac Buffer where the MAC value is to be written.
Steven Cooreman72f736a2021-05-07 14:14:37 +0200182 * \param mac_size Output size requested for the MAC algorithm. The PSA
183 * core guarantees this is a valid MAC length for the
184 * algorithm and key combination passed to
185 * mbedtls_psa_mac_sign_setup(). It also guarantees the
186 * \p mac buffer is large enough to contain the
187 * requested output size.
188 * \param[out] mac_length On success, the number of bytes output to buffer
189 * \p mac, which will be equal to the requested length
190 * \p mac_size.
Steven Cooremand13a70f2021-03-19 15:24:23 +0100191 *
192 * \retval #PSA_SUCCESS
193 * Success.
194 * \retval #PSA_ERROR_BAD_STATE
195 * The operation state is not valid (it must be an active mac sign
196 * operation).
197 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
198 * The size of the \p mac buffer is too small. A sufficient buffer size
199 * can be determined by calling PSA_MAC_LENGTH().
200 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
201 * \retval #PSA_ERROR_CORRUPTION_DETECTED
202 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200203psa_status_t mbedtls_psa_mac_sign_finish(mbedtls_psa_mac_operation_t *operation,
204 uint8_t *mac,
205 size_t mac_size,
206 size_t *mac_length);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100207
208/** Finish the calculation of the MAC of a message and compare it with
209 * an expected value using Mbed TLS.
210 *
211 * \note The signature of this function is that of a PSA driver
212 * mac_verify_finish entry point. This function behaves as a
213 * mac_verify_finish entry point as defined in the PSA driver interface
214 * specification for transparent drivers.
215 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200216 * The PSA core calls mbedtls_psa_mac_verify_setup() before calling this
Steven Cooremand13a70f2021-03-19 15:24:23 +0100217 * function. This function calculates the MAC of the message formed by
218 * concatenating the inputs passed to preceding calls to
219 * mbedtls_psa_mac_update(). It then compares the calculated MAC with the
220 * expected MAC passed as a parameter to this function.
221 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200222 * Whether this function returns successfully or not, the PSA core subsequently
223 * aborts the operation by calling mbedtls_psa_mac_abort().
Steven Cooremand13a70f2021-03-19 15:24:23 +0100224 *
225 * \param[in,out] operation Active MAC operation.
226 * \param[in] mac Buffer containing the expected MAC value.
Steven Cooreman72f736a2021-05-07 14:14:37 +0200227 * \param mac_length Length in bytes of the expected MAC value. The PSA
228 * core guarantees that this length is a valid MAC
229 * length for the algorithm and key combination passed
230 * to mbedtls_psa_mac_verify_setup().
Steven Cooremand13a70f2021-03-19 15:24:23 +0100231 *
232 * \retval #PSA_SUCCESS
233 * The expected MAC is identical to the actual MAC of the message.
234 * \retval #PSA_ERROR_INVALID_SIGNATURE
235 * The MAC of the message was calculated successfully, but it
236 * differs from the expected MAC.
237 * \retval #PSA_ERROR_BAD_STATE
238 * The operation state is not valid (it must be an active mac verify
239 * operation).
240 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
241 * \retval #PSA_ERROR_CORRUPTION_DETECTED
242 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200243psa_status_t
244mbedtls_psa_mac_verify_finish(mbedtls_psa_mac_operation_t *operation,
245 const uint8_t *mac,
246 size_t mac_length);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100247
248/** Abort a MAC operation using Mbed TLS.
249 *
250 * Aborting an operation frees all associated resources except for the
251 * \p operation structure itself. Once aborted, the operation object
252 * can be reused for another operation by calling
253 * mbedtls_psa_mac_sign_setup() or mbedtls_psa_mac_verify_setup() again.
254 *
Steven Cooremanf45f0712021-05-06 19:23:00 +0200255 * The PSA core may call this function any time after the operation object has
Steven Cooremand13a70f2021-03-19 15:24:23 +0100256 * been initialized by one of the methods described in
257 * #mbedtls_psa_mac_operation_t.
258 *
259 * In particular, calling mbedtls_psa_mac_abort() after the operation has been
260 * terminated by a call to mbedtls_psa_mac_abort(),
261 * mbedtls_psa_mac_sign_finish() or mbedtls_psa_mac_verify_finish() is safe and
262 * has no effect.
263 *
264 * \param[in,out] operation Initialized MAC operation.
265 *
266 * \retval #PSA_SUCCESS
267 * \retval #PSA_ERROR_CORRUPTION_DETECTED
268 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200269psa_status_t mbedtls_psa_mac_abort(mbedtls_psa_mac_operation_t *operation);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100270
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,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200286 size_t *mac_length);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100287
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,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200293 psa_algorithm_t alg);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100294
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,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200300 psa_algorithm_t alg);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100301
302psa_status_t mbedtls_transparent_test_driver_mac_update(
303 mbedtls_transparent_test_driver_mac_operation_t *operation,
304 const uint8_t *input,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200305 size_t input_length);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100306
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,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200311 size_t *mac_length);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100312
313psa_status_t mbedtls_transparent_test_driver_mac_verify_finish(
314 mbedtls_transparent_test_driver_mac_operation_t *operation,
315 const uint8_t *mac,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200316 size_t mac_length);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100317
318psa_status_t mbedtls_transparent_test_driver_mac_abort(
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200319 mbedtls_transparent_test_driver_mac_operation_t *operation);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100320
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200321psa_status_t
322mbedtls_opaque_test_driver_mac_compute(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);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100331
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,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200337 psa_algorithm_t alg);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100338
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,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200344 psa_algorithm_t alg);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100345
346psa_status_t mbedtls_opaque_test_driver_mac_update(
347 mbedtls_opaque_test_driver_mac_operation_t *operation,
348 const uint8_t *input,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200349 size_t input_length);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100350
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,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200355 size_t *mac_length);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100356
357psa_status_t mbedtls_opaque_test_driver_mac_verify_finish(
358 mbedtls_opaque_test_driver_mac_operation_t *operation,
359 const uint8_t *mac,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200360 size_t mac_length);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100361
362psa_status_t mbedtls_opaque_test_driver_mac_abort(
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200363 mbedtls_opaque_test_driver_mac_operation_t *operation);
Steven Cooremand13a70f2021-03-19 15:24:23 +0100364
365#endif /* PSA_CRYPTO_DRIVER_TEST */
366
367#endif /* PSA_CRYPTO_MAC_H */