blob: 992b7a72b6113b158c98805e63f80a0d1d22a6d1 [file] [log] [blame]
Steven Cooremancd84cb42020-07-16 20:28:36 +02001/*
2 * Functions to delegate cryptographic operations to an available
3 * and appropriate accelerator.
Steven Cooreman56250fd2020-09-04 13:07:15 +02004 * Warning: This file will be auto-generated in the future.
Steven Cooremancd84cb42020-07-16 20:28:36 +02005 */
Steven Cooreman2c7b2f82020-09-02 13:43:46 +02006/* Copyright The Mbed TLS Contributors
Steven Cooremancd84cb42020-07-16 20:28:36 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Steven Cooremancd84cb42020-07-16 20:28:36 +020020 */
21
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020022#include "common.h"
Ronald Cronde822812021-03-17 16:08:20 +010023#include "psa_crypto_aead.h"
Ronald Cron0b805592020-12-14 18:08:20 +010024#include "psa_crypto_cipher.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020025#include "psa_crypto_core.h"
26#include "psa_crypto_driver_wrappers.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010027#include "psa_crypto_hash.h"
Steven Cooremand13a70f2021-03-19 15:24:23 +010028#include "psa_crypto_mac.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010029
Steven Cooreman2a1664c2020-07-20 15:33:08 +020030#include "mbedtls/platform.h"
31
Gilles Peskine1905a242021-04-24 13:19:45 +020032#if defined(MBEDTLS_PSA_CRYPTO_C)
33
Steven Cooreman2a1664c2020-07-20 15:33:08 +020034#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020035
36/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020037#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020038#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020039#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020040#endif
41#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020042#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020043#endif
Ronald Cronce1d8d22021-04-30 17:00:34 +020044#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020045#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020046
Steven Cooreman56250fd2020-09-04 13:07:15 +020047/* Repeat above block for each JSON-declared driver during autogeneration */
Steven Cooremanaa87fd02021-03-15 18:54:03 +010048#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Steven Cooreman56250fd2020-09-04 13:07:15 +020049
Steven Cooreman1e582352021-02-18 17:24:37 +010050/* Auto-generated values depending on which drivers are registered.
51 * ID 0 is reserved for unallocated operations.
52 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman830aff22021-03-09 09:50:44 +010053#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
54
Steven Cooreman37941cb2020-07-28 18:49:51 +020055#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010056#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
57#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020058#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020059
60/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020061#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020062/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
63 * SE driver is present, to avoid unused argument errors at compile time. */
64#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020065#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020066#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020067#include "psa_crypto_se.h"
68#endif
69
Steven Cooremancd84cb42020-07-16 20:28:36 +020070/* Start delegation functions */
gabor-mezei-arm50eac352021-04-22 11:32:19 +020071psa_status_t psa_driver_wrapper_sign_message(
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 *signature,
79 size_t signature_size,
80 size_t *signature_length )
81{
82 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +020083 psa_key_location_t location =
84 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
85
86 switch( location )
87 {
88 case PSA_KEY_LOCATION_LOCAL_STORAGE:
89 /* Key is stored in the slot in export representation, so
90 * cycle through all known transparent accelerators */
91#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
92#if defined(PSA_CRYPTO_DRIVER_TEST)
93 status = mbedtls_test_transparent_signature_sign_message(
94 attributes,
95 key_buffer,
96 key_buffer_size,
97 alg,
98 input,
99 input_length,
100 signature,
101 signature_size,
102 signature_length );
103 /* Declared with fallback == true */
104 if( status != PSA_ERROR_NOT_SUPPORTED )
105 return( status );
106#endif /* PSA_CRYPTO_DRIVER_TEST */
107#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200108 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200109
110 /* Add cases for opaque driver here */
111#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
112#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm2522c0b2021-04-27 18:52:51 +0200113 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200114 status = mbedtls_test_opaque_signature_sign_message(
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200115 attributes,
116 key_buffer,
117 key_buffer_size,
118 alg,
119 input,
120 input_length,
121 signature,
122 signature_size,
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200123 signature_length );
124 if( status != PSA_ERROR_NOT_SUPPORTED )
125 return( status );
126 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200127#endif /* PSA_CRYPTO_DRIVER_TEST */
128#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
129 default:
130 /* Key is declared with a lifetime not known to us */
131 (void)status;
gabor-mezei-armd5218df2021-05-12 11:12:25 +0200132 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200133 }
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200134
135 return( psa_sign_message_builtin( attributes,
136 key_buffer,
137 key_buffer_size,
138 alg,
139 input,
140 input_length,
141 signature,
142 signature_size,
143 signature_length ) );
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200144}
145
146psa_status_t psa_driver_wrapper_verify_message(
147 const psa_key_attributes_t *attributes,
148 const uint8_t *key_buffer,
149 size_t key_buffer_size,
150 psa_algorithm_t alg,
151 const uint8_t *input,
152 size_t input_length,
153 const uint8_t *signature,
154 size_t signature_length )
155{
156 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200157 psa_key_location_t location =
158 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
159
160 switch( location )
161 {
162 case PSA_KEY_LOCATION_LOCAL_STORAGE:
163 /* Key is stored in the slot in export representation, so
164 * cycle through all known transparent accelerators */
165#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
166#if defined(PSA_CRYPTO_DRIVER_TEST)
167 status = mbedtls_test_transparent_signature_verify_message(
168 attributes,
169 key_buffer,
170 key_buffer_size,
171 alg,
172 input,
173 input_length,
174 signature,
175 signature_length );
176 /* Declared with fallback == true */
177 if( status != PSA_ERROR_NOT_SUPPORTED )
178 return( status );
179#endif /* PSA_CRYPTO_DRIVER_TEST */
180#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200181 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200182
183 /* Add cases for opaque driver here */
184#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
185#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm2522c0b2021-04-27 18:52:51 +0200186 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200187 return( mbedtls_test_opaque_signature_verify_message(
188 attributes,
189 key_buffer,
190 key_buffer_size,
191 alg,
192 input,
193 input_length,
194 signature,
195 signature_length ) );
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200196 if( status != PSA_ERROR_NOT_SUPPORTED )
197 return( status );
198 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200199#endif /* PSA_CRYPTO_DRIVER_TEST */
200#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
201 default:
202 /* Key is declared with a lifetime not known to us */
203 (void)status;
gabor-mezei-armd5218df2021-05-12 11:12:25 +0200204 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200205 }
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200206
207 return( psa_verify_message_builtin( attributes,
208 key_buffer,
209 key_buffer_size,
210 alg,
211 input,
212 input_length,
213 signature,
214 signature_length ) );
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200215}
216
Ronald Cron9f17aa42020-12-08 17:07:25 +0100217psa_status_t psa_driver_wrapper_sign_hash(
218 const psa_key_attributes_t *attributes,
219 const uint8_t *key_buffer, size_t key_buffer_size,
220 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
221 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +0200222{
Steven Cooreman7a250572020-07-17 16:43:05 +0200223 /* Try dynamically-registered SE interface first */
224#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
225 const psa_drv_se_t *drv;
226 psa_drv_se_context_t *drv_context;
227
Ronald Cron9f17aa42020-12-08 17:07:25 +0100228 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +0200229 {
230 if( drv->asymmetric == NULL ||
231 drv->asymmetric->p_sign == NULL )
232 {
233 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200234 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +0200235 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100236 return( drv->asymmetric->p_sign(
237 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
238 alg, hash, hash_length,
239 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +0200240 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200241#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +0200242
Ronald Cronfce9df22020-12-08 18:06:03 +0100243 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100244 psa_key_location_t location =
245 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200246
247 switch( location )
248 {
249 case PSA_KEY_LOCATION_LOCAL_STORAGE:
250 /* Key is stored in the slot in export representation, so
251 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100252#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200253#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200254 status = mbedtls_test_transparent_signature_sign_hash( attributes,
Ronald Cron9f17aa42020-12-08 17:07:25 +0100255 key_buffer,
256 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200257 alg,
258 hash,
259 hash_length,
260 signature,
261 signature_size,
262 signature_length );
263 /* Declared with fallback == true */
264 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200265 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200266#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100267#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200268 /* Fell through, meaning no accelerator supports this operation */
gabor-mezei-arm6883fd22021-05-05 14:18:36 +0200269 return( psa_sign_hash_builtin( attributes,
270 key_buffer,
271 key_buffer_size,
272 alg,
273 hash,
274 hash_length,
275 signature,
276 signature_size,
277 signature_length ) );
Ronald Cronfce9df22020-12-08 18:06:03 +0100278
Steven Cooremancd84cb42020-07-16 20:28:36 +0200279 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100280#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200281#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100282 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200283 return( mbedtls_test_opaque_signature_sign_hash( attributes,
284 key_buffer,
285 key_buffer_size,
286 alg,
287 hash,
288 hash_length,
289 signature,
290 signature_size,
291 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200292#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100293#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200294 default:
295 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100296 (void)status;
297 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200298 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200299}
300
Ronald Cron9f17aa42020-12-08 17:07:25 +0100301psa_status_t psa_driver_wrapper_verify_hash(
302 const psa_key_attributes_t *attributes,
303 const uint8_t *key_buffer, size_t key_buffer_size,
304 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
305 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200306{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200307 /* Try dynamically-registered SE interface first */
308#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
309 const psa_drv_se_t *drv;
310 psa_drv_se_context_t *drv_context;
311
Ronald Cron9f17aa42020-12-08 17:07:25 +0100312 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200313 {
314 if( drv->asymmetric == NULL ||
315 drv->asymmetric->p_verify == NULL )
316 {
317 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200318 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200319 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100320 return( drv->asymmetric->p_verify(
321 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
322 alg, hash, hash_length,
323 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200324 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200325#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200326
Ronald Cronfce9df22020-12-08 18:06:03 +0100327 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100328 psa_key_location_t location =
329 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200330
331 switch( location )
332 {
333 case PSA_KEY_LOCATION_LOCAL_STORAGE:
334 /* Key is stored in the slot in export representation, so
335 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100336#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200337#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200338 status = mbedtls_test_transparent_signature_verify_hash(
339 attributes,
340 key_buffer,
341 key_buffer_size,
342 alg,
343 hash,
344 hash_length,
345 signature,
346 signature_length );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200347 /* Declared with fallback == true */
348 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200349 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200350#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100351#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
352
gabor-mezei-arm6883fd22021-05-05 14:18:36 +0200353 return( psa_verify_hash_builtin( attributes,
354 key_buffer,
355 key_buffer_size,
356 alg,
357 hash,
358 hash_length,
359 signature,
360 signature_length ) );
Ronald Cronfce9df22020-12-08 18:06:03 +0100361
Steven Cooreman55ae2172020-07-17 19:46:15 +0200362 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100363#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200364#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100365 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200366 return( mbedtls_test_opaque_signature_verify_hash( attributes,
367 key_buffer,
368 key_buffer_size,
369 alg,
370 hash,
371 hash_length,
372 signature,
373 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200374#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100375#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200376 default:
377 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100378 (void)status;
379 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200380 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200381}
382
Archana449608b2021-09-08 15:36:05 +0530383/** Calculate the key buffer size required to store the key material of a key
Archanad8a83dc2021-06-14 10:04:16 +0530384 * associated with an opaque driver from input key data.
385 *
Archanad8a83dc2021-06-14 10:04:16 +0530386 * \param[in] attributes The key attributes
387 * \param[in] data The input key data.
388 * \param[in] data_length The input data length.
389 * \param[out] key_buffer_size Minimum buffer size to contain the key material.
390 *
391 * \retval #PSA_SUCCESS
392 * \retval #PSA_ERROR_INVALID_ARGUMENT
393 * \retval #PSA_ERROR_NOT_SUPPORTED
394 */
395psa_status_t psa_driver_wrapper_get_key_buffer_size_from_key_data(
396 const psa_key_attributes_t *attributes,
397 const uint8_t *data,
398 size_t data_length,
399 size_t *key_buffer_size )
400{
Archana449608b2021-09-08 15:36:05 +0530401 psa_key_location_t location =
402 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Archanad8a83dc2021-06-14 10:04:16 +0530403 psa_key_type_t key_type = attributes->core.type;
404
405 *key_buffer_size = 0;
406 switch( location )
407 {
408#if defined(PSA_CRYPTO_DRIVER_TEST)
409 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
410 *key_buffer_size = mbedtls_test_opaque_size_function( key_type,
411 PSA_BYTES_TO_BITS( data_length ) );
412 return( ( *key_buffer_size != 0 ) ?
413 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
414#endif /* PSA_CRYPTO_DRIVER_TEST */
415
416 default:
417 (void)key_type;
418 (void)data;
419 (void)data_length;
420 return( PSA_ERROR_INVALID_ARGUMENT );
421 }
422}
423
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200424/** Get the key buffer size required to store the key material of a key
Archanad8a83dc2021-06-14 10:04:16 +0530425 * associated with an opaque driver.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200426 *
Ronald Cron31216282020-12-05 18:47:56 +0100427 * \param[in] attributes The key attributes.
428 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200429 *
430 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100431 * The minimum size for a buffer to contain the key material has been
432 * returned successfully.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200433 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100434 * The type and/or the size in bits of the key or the combination of
435 * the two is not supported.
Archanad8a83dc2021-06-14 10:04:16 +0530436 * \retval #PSA_ERROR_INVALID_ARGUMENT
437 * The key is declared with a lifetime not known to us.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200438 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100439psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100440 const psa_key_attributes_t *attributes,
441 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200442{
John Durkop2c618352020-09-22 06:54:01 -0700443 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
444 psa_key_type_t key_type = attributes->core.type;
445 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200446
Ronald Cron31216282020-12-05 18:47:56 +0100447 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700448 switch( location )
449 {
John Durkop2c618352020-09-22 06:54:01 -0700450#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100451 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100452#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
453 /* Emulate property 'builtin_key_size' */
454 if( psa_key_id_is_builtin(
455 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
456 psa_get_key_id( attributes ) ) ) )
457 {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100458 *key_buffer_size = sizeof( psa_drv_slot_number_t );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100459 return( PSA_SUCCESS );
460 }
461#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Archana449608b2021-09-08 15:36:05 +0530462 *key_buffer_size = mbedtls_test_opaque_size_function( key_type,
463 key_bits );
Ronald Cron4607c822021-04-14 10:55:34 +0200464 return( ( *key_buffer_size != 0 ) ?
465 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
John Durkop2c618352020-09-22 06:54:01 -0700466#endif /* PSA_CRYPTO_DRIVER_TEST */
467
468 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100469 (void)key_type;
470 (void)key_bits;
Archanad8a83dc2021-06-14 10:04:16 +0530471 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200472 }
473}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200474
Ronald Cron977c2472020-10-13 08:32:21 +0200475psa_status_t psa_driver_wrapper_generate_key(
476 const psa_key_attributes_t *attributes,
477 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200478{
Ronald Cron977c2472020-10-13 08:32:21 +0200479 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
480 psa_key_location_t location =
481 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
482
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200483 /* Try dynamically-registered SE interface first */
484#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
485 const psa_drv_se_t *drv;
486 psa_drv_se_context_t *drv_context;
487
Ronald Cron977c2472020-10-13 08:32:21 +0200488 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200489 {
490 size_t pubkey_length = 0; /* We don't support this feature yet */
491 if( drv->key_management == NULL ||
492 drv->key_management->p_generate == NULL )
493 {
494 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200495 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200496 }
497 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200498 drv_context,
499 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100500 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200501 }
502#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
503
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200504 switch( location )
505 {
506 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200507#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200508 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200509 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200510 {
Ronald Cron977c2472020-10-13 08:32:21 +0200511 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200512#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200513 status = mbedtls_test_transparent_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200514 attributes, key_buffer, key_buffer_size,
515 key_buffer_length );
516 /* Declared with fallback == true */
517 if( status != PSA_ERROR_NOT_SUPPORTED )
518 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200519#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200520 }
521#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
522
523 /* Software fallback */
524 status = psa_generate_key_internal(
525 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200526 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200527
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200528 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200529#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200530#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100531 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200532 status = mbedtls_test_opaque_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200533 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200534 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200535#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200536#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
537
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200538 default:
539 /* Key is declared with a lifetime not known to us */
540 status = PSA_ERROR_INVALID_ARGUMENT;
541 break;
542 }
543
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200544 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200545}
546
Ronald Cron83282872020-11-22 14:02:39 +0100547psa_status_t psa_driver_wrapper_import_key(
548 const psa_key_attributes_t *attributes,
549 const uint8_t *data,
550 size_t data_length,
551 uint8_t *key_buffer,
552 size_t key_buffer_size,
553 size_t *key_buffer_length,
554 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200555{
Steven Cooreman04524762020-10-13 17:43:44 +0200556 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100557 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
558 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200559
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100560 /* Try dynamically-registered SE interface first */
561#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
562 const psa_drv_se_t *drv;
563 psa_drv_se_context_t *drv_context;
564
565 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
566 {
567 if( drv->key_management == NULL ||
568 drv->key_management->p_import == NULL )
569 return( PSA_ERROR_NOT_SUPPORTED );
570
571 /* The driver should set the number of key bits, however in
572 * case it doesn't, we initialize bits to an invalid value. */
573 *bits = PSA_MAX_KEY_BITS + 1;
574 status = drv->key_management->p_import(
575 drv_context,
576 *( (psa_key_slot_number_t *)key_buffer ),
577 attributes, data, data_length, bits );
578
579 if( status != PSA_SUCCESS )
580 return( status );
581
582 if( (*bits) > PSA_MAX_KEY_BITS )
583 return( PSA_ERROR_NOT_SUPPORTED );
584
585 return( PSA_SUCCESS );
586 }
587#endif /* PSA_CRYPTO_SE_C */
588
Ronald Cronbf33c932020-11-28 18:06:53 +0100589 switch( location )
590 {
591 case PSA_KEY_LOCATION_LOCAL_STORAGE:
592 /* Key is stored in the slot in export representation, so
593 * cycle through all known transparent accelerators */
594#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
595#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200596 status = mbedtls_test_transparent_import_key(
597 attributes,
598 data, data_length,
599 key_buffer, key_buffer_size,
600 key_buffer_length, bits );
Ronald Cronbf33c932020-11-28 18:06:53 +0100601 /* Declared with fallback == true */
602 if( status != PSA_ERROR_NOT_SUPPORTED )
603 return( status );
604#endif /* PSA_CRYPTO_DRIVER_TEST */
605#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
606 /* Fell through, meaning no accelerator supports this operation */
607 return( psa_import_key_into_slot( attributes,
608 data, data_length,
609 key_buffer, key_buffer_size,
610 key_buffer_length, bits ) );
Archana4d7ae1d2021-07-07 02:50:22 +0530611 /* Add cases for opaque driver here */
612#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
613#if defined(PSA_CRYPTO_DRIVER_TEST)
614 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
615 return( mbedtls_test_opaque_import_key(
616 attributes,
617 data, data_length,
618 key_buffer, key_buffer_size,
619 key_buffer_length, bits ) );
620#endif /* PSA_CRYPTO_DRIVER_TEST */
621#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cronbf33c932020-11-28 18:06:53 +0100622 default:
Ronald Cronbf33c932020-11-28 18:06:53 +0100623 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100624 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100625 }
626
Steven Cooreman04524762020-10-13 17:43:44 +0200627}
628
Ronald Cron67227982020-11-26 15:16:05 +0100629psa_status_t psa_driver_wrapper_export_key(
630 const psa_key_attributes_t *attributes,
631 const uint8_t *key_buffer, size_t key_buffer_size,
632 uint8_t *data, size_t data_size, size_t *data_length )
633
634{
635 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
636 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
637 psa_get_key_lifetime( attributes ) );
638
Ronald Cron152e3f82020-11-26 16:06:41 +0100639 /* Try dynamically-registered SE interface first */
640#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
641 const psa_drv_se_t *drv;
642 psa_drv_se_context_t *drv_context;
643
644 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
645 {
646 if( ( drv->key_management == NULL ) ||
647 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100648 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100649 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100650 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100651
652 return( drv->key_management->p_export(
653 drv_context,
654 *( (psa_key_slot_number_t *)key_buffer ),
655 data, data_size, data_length ) );
656 }
657#endif /* PSA_CRYPTO_SE_C */
658
Ronald Cron67227982020-11-26 15:16:05 +0100659 switch( location )
660 {
661 case PSA_KEY_LOCATION_LOCAL_STORAGE:
662 return( psa_export_key_internal( attributes,
663 key_buffer,
664 key_buffer_size,
665 data,
666 data_size,
667 data_length ) );
668
669 /* Add cases for opaque driver here */
670#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
671#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100672 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200673 return( mbedtls_test_opaque_export_key( attributes,
674 key_buffer,
675 key_buffer_size,
676 data,
677 data_size,
678 data_length ) );
Ronald Cron67227982020-11-26 15:16:05 +0100679#endif /* PSA_CRYPTO_DRIVER_TEST */
680#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
681 default:
682 /* Key is declared with a lifetime not known to us */
683 return( status );
684 }
685}
686
Ronald Cron84cc9942020-11-25 14:30:05 +0100687psa_status_t psa_driver_wrapper_export_public_key(
688 const psa_key_attributes_t *attributes,
689 const uint8_t *key_buffer, size_t key_buffer_size,
690 uint8_t *data, size_t data_size, size_t *data_length )
691
Steven Cooremanb9b84422020-10-14 14:39:20 +0200692{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200693 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100694 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
695 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200696
Ronald Cron152e3f82020-11-26 16:06:41 +0100697 /* Try dynamically-registered SE interface first */
698#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
699 const psa_drv_se_t *drv;
700 psa_drv_se_context_t *drv_context;
701
702 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
703 {
704 if( ( drv->key_management == NULL ) ||
705 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100706 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100707 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100708 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100709
710 return( drv->key_management->p_export_public(
711 drv_context,
712 *( (psa_key_slot_number_t *)key_buffer ),
713 data, data_size, data_length ) );
714 }
715#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
716
Steven Cooremanb9b84422020-10-14 14:39:20 +0200717 switch( location )
718 {
719 case PSA_KEY_LOCATION_LOCAL_STORAGE:
720 /* Key is stored in the slot in export representation, so
721 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100722#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200723#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200724 status = mbedtls_test_transparent_export_public_key(
725 attributes,
726 key_buffer,
727 key_buffer_size,
728 data,
729 data_size,
730 data_length );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200731 /* Declared with fallback == true */
732 if( status != PSA_ERROR_NOT_SUPPORTED )
733 return( status );
734#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100735#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200736 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100737 return( psa_export_public_key_internal( attributes,
738 key_buffer,
739 key_buffer_size,
740 data,
741 data_size,
742 data_length ) );
743
Steven Cooremanb9b84422020-10-14 14:39:20 +0200744 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100745#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200746#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100747 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200748 return( mbedtls_test_opaque_export_public_key( attributes,
749 key_buffer,
750 key_buffer_size,
751 data,
752 data_size,
753 data_length ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200754#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100755#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200756 default:
757 /* Key is declared with a lifetime not known to us */
758 return( status );
759 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200760}
761
Steven Cooreman6801f082021-02-19 17:21:22 +0100762psa_status_t psa_driver_wrapper_get_builtin_key(
763 psa_drv_slot_number_t slot_number,
764 psa_key_attributes_t *attributes,
765 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
766{
767 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
768 switch( location )
769 {
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100770#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100771 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200772 return( mbedtls_test_opaque_get_builtin_key(
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100773 slot_number,
774 attributes,
775 key_buffer, key_buffer_size, key_buffer_length ) );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100776#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman6801f082021-02-19 17:21:22 +0100777 default:
778 (void) slot_number;
779 (void) key_buffer;
780 (void) key_buffer_size;
781 (void) key_buffer_length;
782 return( PSA_ERROR_DOES_NOT_EXIST );
783 }
784}
785
Archana8a180362021-07-05 02:18:48 +0530786psa_status_t psa_driver_wrapper_copy_key(
787 psa_key_attributes_t *attributes,
Archana9d17bf42021-09-10 06:22:44 +0530788 const uint8_t *source_key, size_t source_key_length,
Archana449608b2021-09-08 15:36:05 +0530789 uint8_t *target_key_buffer, size_t target_key_buffer_size,
790 size_t *target_key_buffer_length )
Archana8a180362021-07-05 02:18:48 +0530791{
Archana449608b2021-09-08 15:36:05 +0530792 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
793 psa_key_location_t location =
794 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Archana8a180362021-07-05 02:18:48 +0530795
Archana9d17bf42021-09-10 06:22:44 +0530796#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
797 const psa_drv_se_t *drv;
798 psa_drv_se_context_t *drv_context;
799
800 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
801 {
802 /* Copying to a secure element is not implemented yet. */
803 return( PSA_ERROR_NOT_SUPPORTED );
804 }
805#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
806
Archana8a180362021-07-05 02:18:48 +0530807 switch( location )
808 {
809#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
810#if defined(PSA_CRYPTO_DRIVER_TEST)
811 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
812 return( mbedtls_test_opaque_copy_key( attributes, source_key,
Archana9d17bf42021-09-10 06:22:44 +0530813 source_key_length,
Archana8a180362021-07-05 02:18:48 +0530814 target_key_buffer,
Archana449608b2021-09-08 15:36:05 +0530815 target_key_buffer_size,
816 target_key_buffer_length) );
Archana8a180362021-07-05 02:18:48 +0530817#endif /* PSA_CRYPTO_DRIVER_TEST */
818#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
819 default:
820 (void)source_key;
Archana9d17bf42021-09-10 06:22:44 +0530821 (void)source_key_length;
Archana8a180362021-07-05 02:18:48 +0530822 (void)target_key_buffer;
Archana449608b2021-09-08 15:36:05 +0530823 (void)target_key_buffer_size;
824 (void)target_key_buffer_length;
Archana8a180362021-07-05 02:18:48 +0530825 status = PSA_ERROR_INVALID_ARGUMENT;
826 }
827 return( status );
828}
829
Steven Cooreman37941cb2020-07-28 18:49:51 +0200830/*
831 * Cipher functions
832 */
833psa_status_t psa_driver_wrapper_cipher_encrypt(
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100834 const psa_key_attributes_t *attributes,
835 const uint8_t *key_buffer,
836 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200837 psa_algorithm_t alg,
838 const uint8_t *input,
839 size_t input_length,
840 uint8_t *output,
841 size_t output_size,
842 size_t *output_length )
843{
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100844 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
845 psa_key_location_t location =
846 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200847
848 switch( location )
849 {
850 case PSA_KEY_LOCATION_LOCAL_STORAGE:
851 /* Key is stored in the slot in export representation, so
852 * cycle through all known transparent accelerators */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100853#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200854#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100855 status = mbedtls_test_transparent_cipher_encrypt( attributes,
856 key_buffer,
857 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200858 alg,
859 input,
860 input_length,
861 output,
862 output_size,
863 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200864 /* Declared with fallback == true */
865 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200866 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200867#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100868#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
869
870#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
871 return( mbedtls_psa_cipher_encrypt( attributes,
872 key_buffer,
873 key_buffer_size,
874 alg,
875 input,
876 input_length,
877 output,
878 output_size,
879 output_length ) );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200880#else
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200881 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200882#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100883
Steven Cooreman37941cb2020-07-28 18:49:51 +0200884 /* Add cases for opaque driver here */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100885#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200886#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100887 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100888 return( mbedtls_test_opaque_cipher_encrypt( attributes,
889 key_buffer,
890 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200891 alg,
892 input,
893 input_length,
894 output,
895 output_size,
896 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200897#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100898#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
899
Steven Cooreman37941cb2020-07-28 18:49:51 +0200900 default:
901 /* Key is declared with a lifetime not known to us */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100902 (void)status;
903 (void)key_buffer;
904 (void)key_buffer_size;
905 (void)alg;
906 (void)input;
907 (void)input_length;
908 (void)output;
909 (void)output_size;
910 (void)output_length;
911 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200912 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200913}
914
915psa_status_t psa_driver_wrapper_cipher_decrypt(
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100916 const psa_key_attributes_t *attributes,
917 const uint8_t *key_buffer,
918 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200919 psa_algorithm_t alg,
920 const uint8_t *input,
921 size_t input_length,
922 uint8_t *output,
923 size_t output_size,
924 size_t *output_length )
925{
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
927 psa_key_location_t location =
928 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200929
930 switch( location )
931 {
932 case PSA_KEY_LOCATION_LOCAL_STORAGE:
933 /* Key is stored in the slot in export representation, so
934 * cycle through all known transparent accelerators */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100935#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200936#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100937 status = mbedtls_test_transparent_cipher_decrypt( attributes,
938 key_buffer,
939 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200940 alg,
941 input,
942 input_length,
943 output,
944 output_size,
945 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200946 /* Declared with fallback == true */
947 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200948 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200949#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100950#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
951
952#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
953 return( mbedtls_psa_cipher_decrypt( attributes,
954 key_buffer,
955 key_buffer_size,
956 alg,
957 input,
958 input_length,
959 output,
960 output_size,
961 output_length ) );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200962#else
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200963 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200964#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100965
Steven Cooreman37941cb2020-07-28 18:49:51 +0200966 /* Add cases for opaque driver here */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100967#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200968#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100969 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100970 return( mbedtls_test_opaque_cipher_decrypt( attributes,
971 key_buffer,
972 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200973 alg,
974 input,
975 input_length,
976 output,
977 output_size,
978 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200979#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100980#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
981
Steven Cooreman37941cb2020-07-28 18:49:51 +0200982 default:
983 /* Key is declared with a lifetime not known to us */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100984 (void)status;
985 (void)key_buffer;
986 (void)key_buffer_size;
987 (void)alg;
988 (void)input;
989 (void)input_length;
990 (void)output;
991 (void)output_size;
992 (void)output_length;
993 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200994 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200995}
996
997psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100998 psa_cipher_operation_t *operation,
999 const psa_key_attributes_t *attributes,
1000 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001001 psa_algorithm_t alg )
1002{
Ronald Cron0b805592020-12-14 18:08:20 +01001003 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +01001004 psa_key_location_t location =
1005 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001006
Steven Cooreman37941cb2020-07-28 18:49:51 +02001007 switch( location )
1008 {
1009 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1010 /* Key is stored in the slot in export representation, so
1011 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +01001012#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001013#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001014 status = mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001015 &operation->ctx.transparent_test_driver_ctx,
1016 attributes,
1017 key_buffer,
1018 key_buffer_size,
1019 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001020 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +02001021 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001022 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +02001023
Ronald Cron0b805592020-12-14 18:08:20 +01001024 if( status != PSA_ERROR_NOT_SUPPORTED )
1025 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001026#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001027#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001028#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001029 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +01001030 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001031 attributes,
1032 key_buffer,
1033 key_buffer_size,
1034 alg );
1035 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +01001036 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +01001037
Ronald Cron7b4154d2021-03-19 14:49:41 +01001038 if( status != PSA_ERROR_NOT_SUPPORTED )
1039 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001040#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1041 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +01001042
Steven Cooreman37941cb2020-07-28 18:49:51 +02001043 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001044#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001045#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001046 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001047 status = mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001048 &operation->ctx.opaque_test_driver_ctx,
1049 attributes,
1050 key_buffer, key_buffer_size,
1051 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001052
Steven Cooreman37941cb2020-07-28 18:49:51 +02001053 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001054 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001055
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001056 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001057#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001058#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001059 default:
1060 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001061 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001062 (void)key_buffer;
1063 (void)key_buffer_size;
1064 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001065 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001066 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001067}
1068
1069psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +01001070 psa_cipher_operation_t *operation,
1071 const psa_key_attributes_t *attributes,
1072 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001073 psa_algorithm_t alg )
1074{
Steven Cooreman37941cb2020-07-28 18:49:51 +02001075 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +01001076 psa_key_location_t location =
1077 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001078
Steven Cooreman37941cb2020-07-28 18:49:51 +02001079 switch( location )
1080 {
1081 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1082 /* Key is stored in the slot in export representation, so
1083 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +01001084#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001085#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001086 status = mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001087 &operation->ctx.transparent_test_driver_ctx,
1088 attributes,
1089 key_buffer,
1090 key_buffer_size,
1091 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001092 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +02001093 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001094 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +02001095
Ronald Cron0b805592020-12-14 18:08:20 +01001096 if( status != PSA_ERROR_NOT_SUPPORTED )
1097 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001098#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001099#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001100#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001101 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +01001102 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001103 attributes,
1104 key_buffer,
1105 key_buffer_size,
1106 alg );
1107 if( status == PSA_SUCCESS )
1108 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1109
1110 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001111#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1112 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +01001113
Steven Cooreman37941cb2020-07-28 18:49:51 +02001114 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001115#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001116#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001117 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001118 status = mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001119 &operation->ctx.opaque_test_driver_ctx,
1120 attributes,
1121 key_buffer, key_buffer_size,
1122 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001123
Steven Cooreman37941cb2020-07-28 18:49:51 +02001124 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001125 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001126
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001127 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001128#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001129#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001130 default:
1131 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001132 (void)status;
Ronald Cron17006702021-12-03 15:25:24 +01001133 (void)operation;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001134 (void)key_buffer;
1135 (void)key_buffer_size;
1136 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001137 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001138 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001139}
1140
Steven Cooreman37941cb2020-07-28 18:49:51 +02001141psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +01001142 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001143 const uint8_t *iv,
1144 size_t iv_length )
1145{
Ronald Cron49fafa92021-03-10 08:34:23 +01001146 switch( operation->id )
1147 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001148#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001149 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001150 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001151 iv,
1152 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001153#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001154
1155#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001156#if defined(PSA_CRYPTO_DRIVER_TEST)
1157 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001158 return( mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001159 &operation->ctx.transparent_test_driver_ctx,
1160 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001161
Steven Cooreman37941cb2020-07-28 18:49:51 +02001162 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001163 return( mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001164 &operation->ctx.opaque_test_driver_ctx,
1165 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001166#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001167#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001168 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001169
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001170 (void)iv;
1171 (void)iv_length;
1172
Ronald Crondd24c9b2020-12-15 14:10:01 +01001173 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001174}
1175
1176psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +01001177 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001178 const uint8_t *input,
1179 size_t input_length,
1180 uint8_t *output,
1181 size_t output_size,
1182 size_t *output_length )
1183{
Ronald Cron49fafa92021-03-10 08:34:23 +01001184 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001185 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001186#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001187 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001188 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001189 input,
1190 input_length,
1191 output,
1192 output_size,
1193 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001194#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1195
Ronald Cron49fafa92021-03-10 08:34:23 +01001196#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001197#if defined(PSA_CRYPTO_DRIVER_TEST)
1198 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001199 return( mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001200 &operation->ctx.transparent_test_driver_ctx,
1201 input, input_length,
1202 output, output_size, output_length ) );
1203
Steven Cooreman37941cb2020-07-28 18:49:51 +02001204 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001205 return( mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001206 &operation->ctx.opaque_test_driver_ctx,
1207 input, input_length,
1208 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001209#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001210#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001211 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001212
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001213 (void)input;
1214 (void)input_length;
1215 (void)output;
1216 (void)output_size;
1217 (void)output_length;
1218
Ronald Crondd24c9b2020-12-15 14:10:01 +01001219 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001220}
1221
1222psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +01001223 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001224 uint8_t *output,
1225 size_t output_size,
1226 size_t *output_length )
1227{
Ronald Cron49fafa92021-03-10 08:34:23 +01001228 switch( operation->id )
1229 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001230#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001231 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001232 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001233 output,
1234 output_size,
1235 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001236#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001237
1238#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001239#if defined(PSA_CRYPTO_DRIVER_TEST)
1240 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001241 return( mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001242 &operation->ctx.transparent_test_driver_ctx,
1243 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001244
Steven Cooreman37941cb2020-07-28 18:49:51 +02001245 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001246 return( mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001247 &operation->ctx.opaque_test_driver_ctx,
1248 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001249#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001250#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001251 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001252
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001253 (void)output;
1254 (void)output_size;
1255 (void)output_length;
1256
Ronald Crondd24c9b2020-12-15 14:10:01 +01001257 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001258}
1259
1260psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +01001261 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001262{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001263 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001264
Ronald Cron49fafa92021-03-10 08:34:23 +01001265 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001266 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001267#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001268 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001269 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001270#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001271
1272#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001273#if defined(PSA_CRYPTO_DRIVER_TEST)
1274 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001275 status = mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001276 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001277 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001278 &operation->ctx.transparent_test_driver_ctx,
1279 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001280 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001281
Steven Cooreman37941cb2020-07-28 18:49:51 +02001282 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001283 status = mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001284 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001285 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001286 &operation->ctx.opaque_test_driver_ctx,
1287 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001288 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001289#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001290#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001291 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001292
Ronald Cron49fafa92021-03-10 08:34:23 +01001293 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001294 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001295}
1296
Steven Cooreman1e582352021-02-18 17:24:37 +01001297/*
1298 * Hashing functions
1299 */
1300psa_status_t psa_driver_wrapper_hash_compute(
1301 psa_algorithm_t alg,
1302 const uint8_t *input,
1303 size_t input_length,
1304 uint8_t *hash,
1305 size_t hash_size,
1306 size_t *hash_length)
1307{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001308 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001309
1310 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001311#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001312 status = mbedtls_test_transparent_hash_compute(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001313 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001314 if( status != PSA_ERROR_NOT_SUPPORTED )
1315 return( status );
1316#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001317
1318 /* If software fallback is compiled in, try fallback */
1319#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1320 status = mbedtls_psa_hash_compute( alg, input, input_length,
1321 hash, hash_size, hash_length );
1322 if( status != PSA_ERROR_NOT_SUPPORTED )
1323 return( status );
1324#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001325 (void) status;
1326 (void) alg;
1327 (void) input;
1328 (void) input_length;
1329 (void) hash;
1330 (void) hash_size;
1331 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001332
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001333 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001334}
1335
1336psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001337 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001338 psa_algorithm_t alg )
1339{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001340 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001341
Steven Cooreman1e582352021-02-18 17:24:37 +01001342 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001343#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001344 status = mbedtls_test_transparent_hash_setup(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001345 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001346 if( status == PSA_SUCCESS )
1347 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1348
1349 if( status != PSA_ERROR_NOT_SUPPORTED )
1350 return( status );
1351#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001352
1353 /* If software fallback is compiled in, try fallback */
1354#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001355 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001356 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001357 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001358
1359 if( status != PSA_ERROR_NOT_SUPPORTED )
1360 return( status );
1361#endif
1362 /* Nothing left to try if we fall through here */
1363 (void) status;
1364 (void) operation;
1365 (void) alg;
1366 return( PSA_ERROR_NOT_SUPPORTED );
1367}
1368
1369psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001370 const psa_hash_operation_t *source_operation,
1371 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001372{
Steven Cooreman1e582352021-02-18 17:24:37 +01001373 switch( source_operation->id )
1374 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001375#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1376 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1377 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1378 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1379 &target_operation->ctx.mbedtls_ctx ) );
1380#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001381#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001382 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1383 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Ronald Cron7f13fa22021-04-13 12:41:34 +02001384 return( mbedtls_test_transparent_hash_clone(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001385 &source_operation->ctx.test_driver_ctx,
1386 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001387#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001388 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001389 (void) target_operation;
1390 return( PSA_ERROR_BAD_STATE );
1391 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001392}
1393
1394psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001395 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001396 const uint8_t *input,
1397 size_t input_length )
1398{
Steven Cooreman1e582352021-02-18 17:24:37 +01001399 switch( operation->id )
1400 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001401#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1402 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1403 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1404 input, input_length ) );
1405#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001406#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001407 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001408 return( mbedtls_test_transparent_hash_update(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001409 &operation->ctx.test_driver_ctx,
1410 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001411#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001412 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001413 (void) input;
1414 (void) input_length;
1415 return( PSA_ERROR_BAD_STATE );
1416 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001417}
1418
1419psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001420 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001421 uint8_t *hash,
1422 size_t hash_size,
1423 size_t *hash_length )
1424{
Steven Cooreman1e582352021-02-18 17:24:37 +01001425 switch( operation->id )
1426 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001427#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1428 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1429 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1430 hash, hash_size, hash_length ) );
1431#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001432#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001433 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001434 return( mbedtls_test_transparent_hash_finish(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001435 &operation->ctx.test_driver_ctx,
1436 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001437#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001438 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001439 (void) hash;
1440 (void) hash_size;
1441 (void) hash_length;
1442 return( PSA_ERROR_BAD_STATE );
1443 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001444}
1445
1446psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001447 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001448{
Steven Cooreman1e582352021-02-18 17:24:37 +01001449 switch( operation->id )
1450 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001451#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1452 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1453 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1454#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001455#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001456 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001457 return( mbedtls_test_transparent_hash_abort(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001458 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001459#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001460 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001461 return( PSA_ERROR_BAD_STATE );
1462 }
1463}
1464
Ronald Cronde822812021-03-17 16:08:20 +01001465psa_status_t psa_driver_wrapper_aead_encrypt(
1466 const psa_key_attributes_t *attributes,
1467 const uint8_t *key_buffer, size_t key_buffer_size,
1468 psa_algorithm_t alg,
1469 const uint8_t *nonce, size_t nonce_length,
1470 const uint8_t *additional_data, size_t additional_data_length,
1471 const uint8_t *plaintext, size_t plaintext_length,
1472 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1473{
1474 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1475 psa_key_location_t location =
1476 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1477
1478 switch( location )
1479 {
1480 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1481 /* Key is stored in the slot in export representation, so
1482 * cycle through all known transparent accelerators */
1483
1484#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1485#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001486 status = mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001487 attributes, key_buffer, key_buffer_size,
1488 alg,
1489 nonce, nonce_length,
1490 additional_data, additional_data_length,
1491 plaintext, plaintext_length,
1492 ciphertext, ciphertext_size, ciphertext_length );
1493 /* Declared with fallback == true */
1494 if( status != PSA_ERROR_NOT_SUPPORTED )
1495 return( status );
1496#endif /* PSA_CRYPTO_DRIVER_TEST */
1497#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1498
1499 /* Fell through, meaning no accelerator supports this operation */
1500 return( mbedtls_psa_aead_encrypt(
1501 attributes, key_buffer, key_buffer_size,
1502 alg,
1503 nonce, nonce_length,
1504 additional_data, additional_data_length,
1505 plaintext, plaintext_length,
1506 ciphertext, ciphertext_size, ciphertext_length ) );
1507
1508 /* Add cases for opaque driver here */
1509
1510 default:
1511 /* Key is declared with a lifetime not known to us */
1512 (void)status;
1513 return( PSA_ERROR_INVALID_ARGUMENT );
1514 }
1515}
1516
1517psa_status_t psa_driver_wrapper_aead_decrypt(
1518 const psa_key_attributes_t *attributes,
1519 const uint8_t *key_buffer, size_t key_buffer_size,
1520 psa_algorithm_t alg,
1521 const uint8_t *nonce, size_t nonce_length,
1522 const uint8_t *additional_data, size_t additional_data_length,
1523 const uint8_t *ciphertext, size_t ciphertext_length,
1524 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1525{
1526 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1527 psa_key_location_t location =
1528 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1529
1530 switch( location )
1531 {
1532 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1533 /* Key is stored in the slot in export representation, so
1534 * cycle through all known transparent accelerators */
1535
1536#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1537#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001538 status = mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001539 attributes, key_buffer, key_buffer_size,
1540 alg,
1541 nonce, nonce_length,
1542 additional_data, additional_data_length,
1543 ciphertext, ciphertext_length,
1544 plaintext, plaintext_size, plaintext_length );
1545 /* Declared with fallback == true */
1546 if( status != PSA_ERROR_NOT_SUPPORTED )
1547 return( status );
1548#endif /* PSA_CRYPTO_DRIVER_TEST */
1549#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1550
1551 /* Fell through, meaning no accelerator supports this operation */
1552 return( mbedtls_psa_aead_decrypt(
1553 attributes, key_buffer, key_buffer_size,
1554 alg,
1555 nonce, nonce_length,
1556 additional_data, additional_data_length,
1557 ciphertext, ciphertext_length,
1558 plaintext, plaintext_size, plaintext_length ) );
1559
1560 /* Add cases for opaque driver here */
1561
1562 default:
1563 /* Key is declared with a lifetime not known to us */
1564 (void)status;
1565 return( PSA_ERROR_INVALID_ARGUMENT );
1566 }
1567}
Steven Cooremand13a70f2021-03-19 15:24:23 +01001568
Paul Elliott6504aa62021-04-20 17:09:36 +01001569psa_status_t psa_driver_wrapper_aead_encrypt_setup(
1570 psa_aead_operation_t *operation,
1571 const psa_key_attributes_t *attributes,
1572 const uint8_t *key_buffer, size_t key_buffer_size,
1573 psa_algorithm_t alg )
1574{
1575 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1576 psa_key_location_t location =
1577 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1578
1579 switch( location )
1580 {
1581 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1582 /* Key is stored in the slot in export representation, so
1583 * cycle through all known transparent accelerators */
1584
1585#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1586#if defined(PSA_CRYPTO_DRIVER_TEST)
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001587 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Paul Elliotta218ceb2021-05-07 15:10:31 +01001588 status = mbedtls_test_transparent_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001589 &operation->ctx.transparent_test_driver_ctx,
1590 attributes, key_buffer, key_buffer_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001591 alg );
Paul Elliott6504aa62021-04-20 17:09:36 +01001592
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001593 /* Declared with fallback == true */
Paul Elliott6504aa62021-04-20 17:09:36 +01001594 if( status != PSA_ERROR_NOT_SUPPORTED )
1595 return( status );
1596#endif /* PSA_CRYPTO_DRIVER_TEST */
1597#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1598
1599 /* Fell through, meaning no accelerator supports this operation */
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001600 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Paul Elliott6504aa62021-04-20 17:09:36 +01001601 status = mbedtls_psa_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001602 &operation->ctx.mbedtls_ctx, attributes,
Paul Elliott6504aa62021-04-20 17:09:36 +01001603 key_buffer, key_buffer_size,
1604 alg );
1605
Paul Elliott6504aa62021-04-20 17:09:36 +01001606 return( status );
1607
1608 /* Add cases for opaque driver here */
1609
1610 default:
1611 /* Key is declared with a lifetime not known to us */
1612 (void)status;
1613 return( PSA_ERROR_INVALID_ARGUMENT );
1614 }
1615}
1616
1617psa_status_t psa_driver_wrapper_aead_decrypt_setup(
1618 psa_aead_operation_t *operation,
1619 const psa_key_attributes_t *attributes,
1620 const uint8_t *key_buffer, size_t key_buffer_size,
1621 psa_algorithm_t alg )
1622{
1623 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1624 psa_key_location_t location =
1625 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1626
1627 switch( location )
1628 {
1629 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1630 /* Key is stored in the slot in export representation, so
1631 * cycle through all known transparent accelerators */
1632
1633#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1634#if defined(PSA_CRYPTO_DRIVER_TEST)
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001635 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Paul Elliotta218ceb2021-05-07 15:10:31 +01001636 status = mbedtls_test_transparent_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001637 &operation->ctx.transparent_test_driver_ctx,
Paul Elliott5d3a3c32021-08-19 18:34:41 +01001638 attributes,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001639 key_buffer, key_buffer_size,
1640 alg );
Paul Elliott6504aa62021-04-20 17:09:36 +01001641
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001642 /* Declared with fallback == true */
Paul Elliott6504aa62021-04-20 17:09:36 +01001643 if( status != PSA_ERROR_NOT_SUPPORTED )
1644 return( status );
1645#endif /* PSA_CRYPTO_DRIVER_TEST */
1646#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1647
1648 /* Fell through, meaning no accelerator supports this operation */
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001649 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Paul Elliott6504aa62021-04-20 17:09:36 +01001650 status = mbedtls_psa_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001651 &operation->ctx.mbedtls_ctx,
Paul Elliott5d3a3c32021-08-19 18:34:41 +01001652 attributes,
Paul Elliott6504aa62021-04-20 17:09:36 +01001653 key_buffer, key_buffer_size,
1654 alg );
1655
Paul Elliott6504aa62021-04-20 17:09:36 +01001656 return( status );
1657
1658 /* Add cases for opaque driver here */
1659
1660 default:
1661 /* Key is declared with a lifetime not known to us */
1662 (void)status;
1663 return( PSA_ERROR_INVALID_ARGUMENT );
1664 }
1665}
1666
Paul Elliott6504aa62021-04-20 17:09:36 +01001667psa_status_t psa_driver_wrapper_aead_set_nonce(
1668 psa_aead_operation_t *operation,
1669 const uint8_t *nonce,
1670 size_t nonce_length )
1671{
1672 switch( operation->id )
1673 {
Paul Elliott5c656cb2021-05-19 14:15:01 +01001674#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001675 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001676 return( mbedtls_psa_aead_set_nonce( &operation->ctx.mbedtls_ctx,
1677 nonce,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001678 nonce_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001679
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001680#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001681
1682#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1683#if defined(PSA_CRYPTO_DRIVER_TEST)
1684 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001685 return( mbedtls_test_transparent_aead_set_nonce(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001686 &operation->ctx.transparent_test_driver_ctx,
1687 nonce, nonce_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001688
1689 /* Add cases for opaque driver here */
1690
1691#endif /* PSA_CRYPTO_DRIVER_TEST */
1692#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1693 }
1694
1695 (void)nonce;
1696 (void)nonce_length;
1697
1698 return( PSA_ERROR_INVALID_ARGUMENT );
1699}
1700
1701psa_status_t psa_driver_wrapper_aead_set_lengths(
1702 psa_aead_operation_t *operation,
1703 size_t ad_length,
1704 size_t plaintext_length )
1705{
1706 switch( operation->id )
1707 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001708#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001709 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottdff6c5d2021-09-28 11:00:20 +01001710 return( mbedtls_psa_aead_set_lengths( &operation->ctx.mbedtls_ctx,
1711 ad_length,
1712 plaintext_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001713
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001714#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001715
1716#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1717#if defined(PSA_CRYPTO_DRIVER_TEST)
1718 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001719 return( mbedtls_test_transparent_aead_set_lengths(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001720 &operation->ctx.transparent_test_driver_ctx,
1721 ad_length, plaintext_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001722
1723 /* Add cases for opaque driver here */
1724
1725#endif /* PSA_CRYPTO_DRIVER_TEST */
1726#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1727 }
1728
1729 (void)ad_length;
1730 (void)plaintext_length;
1731
1732 return( PSA_ERROR_INVALID_ARGUMENT );
1733}
1734
1735psa_status_t psa_driver_wrapper_aead_update_ad(
1736 psa_aead_operation_t *operation,
1737 const uint8_t *input,
1738 size_t input_length )
1739{
1740 switch( operation->id )
1741 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001742#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001743 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001744 return( mbedtls_psa_aead_update_ad( &operation->ctx.mbedtls_ctx,
1745 input,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001746 input_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001747
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001748#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001749
1750#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1751#if defined(PSA_CRYPTO_DRIVER_TEST)
1752 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001753 return( mbedtls_test_transparent_aead_update_ad(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001754 &operation->ctx.transparent_test_driver_ctx,
1755 input, input_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001756
1757 /* Add cases for opaque driver here */
1758
1759#endif /* PSA_CRYPTO_DRIVER_TEST */
1760#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1761 }
1762
1763 (void)input;
1764 (void)input_length;
1765
1766 return( PSA_ERROR_INVALID_ARGUMENT );
1767}
1768
1769psa_status_t psa_driver_wrapper_aead_update(
1770 psa_aead_operation_t *operation,
1771 const uint8_t *input,
1772 size_t input_length,
1773 uint8_t *output,
1774 size_t output_size,
1775 size_t *output_length )
1776{
1777 switch( operation->id )
1778 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001779#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001780 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001781 return( mbedtls_psa_aead_update( &operation->ctx.mbedtls_ctx,
1782 input, input_length,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001783 output, output_size,
1784 output_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001785
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001786#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001787
1788#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1789#if defined(PSA_CRYPTO_DRIVER_TEST)
1790 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001791 return( mbedtls_test_transparent_aead_update(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001792 &operation->ctx.transparent_test_driver_ctx,
1793 input, input_length, output, output_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001794 output_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001795
1796 /* Add cases for opaque driver here */
1797
1798#endif /* PSA_CRYPTO_DRIVER_TEST */
1799#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1800 }
1801
1802 (void)input;
1803 (void)input_length;
1804 (void)output;
1805 (void)output_size;
1806 (void)output_length;
1807
1808 return( PSA_ERROR_INVALID_ARGUMENT );
1809}
1810
1811psa_status_t psa_driver_wrapper_aead_finish(
1812 psa_aead_operation_t *operation,
1813 uint8_t *ciphertext,
1814 size_t ciphertext_size,
1815 size_t *ciphertext_length,
1816 uint8_t *tag,
1817 size_t tag_size,
1818 size_t *tag_length )
1819{
1820 switch( operation->id )
1821 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001822#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001823 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001824 return( mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
1825 ciphertext,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001826 ciphertext_size,
1827 ciphertext_length, tag,
1828 tag_size, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001829
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001830#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001831
1832#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1833#if defined(PSA_CRYPTO_DRIVER_TEST)
1834 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001835 return( mbedtls_test_transparent_aead_finish(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001836 &operation->ctx.transparent_test_driver_ctx,
1837 ciphertext, ciphertext_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001838 ciphertext_length, tag, tag_size, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001839
1840 /* Add cases for opaque driver here */
1841
1842#endif /* PSA_CRYPTO_DRIVER_TEST */
1843#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1844 }
1845
1846 (void)ciphertext;
1847 (void)ciphertext_size;
1848 (void)ciphertext_length;
1849 (void)tag;
1850 (void)tag_size;
1851 (void)tag_length;
1852
1853 return( PSA_ERROR_INVALID_ARGUMENT );
1854}
1855
1856psa_status_t psa_driver_wrapper_aead_verify(
1857 psa_aead_operation_t *operation,
1858 uint8_t *plaintext,
1859 size_t plaintext_size,
1860 size_t *plaintext_length,
1861 const uint8_t *tag,
1862 size_t tag_length )
1863{
1864 switch( operation->id )
1865 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001866#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001867 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliott315628d2021-07-20 18:25:54 +01001868 {
1869 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1870 uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
1871 size_t check_tag_length;
1872
1873 status = mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
1874 plaintext,
1875 plaintext_size,
1876 plaintext_length,
1877 check_tag,
Paul Elliottb183d562021-09-13 19:02:57 +01001878 sizeof( check_tag ),
Paul Elliott315628d2021-07-20 18:25:54 +01001879 &check_tag_length );
1880
1881 if( status == PSA_SUCCESS )
1882 {
1883 if( tag_length != check_tag_length ||
1884 mbedtls_psa_safer_memcmp( tag, check_tag, tag_length )
1885 != 0 )
1886 status = PSA_ERROR_INVALID_SIGNATURE;
1887 }
1888
Paul Elliott06b6b8c2021-09-13 19:02:04 +01001889 mbedtls_platform_zeroize( check_tag, sizeof( check_tag ) );
1890
Paul Elliott315628d2021-07-20 18:25:54 +01001891 return( status );
1892 }
Paul Elliott6504aa62021-04-20 17:09:36 +01001893
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001894#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001895
1896#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1897#if defined(PSA_CRYPTO_DRIVER_TEST)
1898 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001899 return( mbedtls_test_transparent_aead_verify(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001900 &operation->ctx.transparent_test_driver_ctx,
1901 plaintext, plaintext_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001902 plaintext_length, tag, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001903
1904 /* Add cases for opaque driver here */
1905
1906#endif /* PSA_CRYPTO_DRIVER_TEST */
1907#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1908 }
1909
1910 (void)plaintext;
1911 (void)plaintext_size;
1912 (void)plaintext_length;
1913 (void)tag;
1914 (void)tag_length;
1915
1916 return( PSA_ERROR_INVALID_ARGUMENT );
1917}
1918
1919psa_status_t psa_driver_wrapper_aead_abort(
1920 psa_aead_operation_t *operation )
1921{
1922 switch( operation->id )
1923 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001924#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001925 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001926 return( mbedtls_psa_aead_abort( &operation->ctx.mbedtls_ctx ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001927
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001928#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001929
1930#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1931#if defined(PSA_CRYPTO_DRIVER_TEST)
1932 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001933 return( mbedtls_test_transparent_aead_abort(
1934 &operation->ctx.transparent_test_driver_ctx ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001935
1936 /* Add cases for opaque driver here */
1937
1938#endif /* PSA_CRYPTO_DRIVER_TEST */
1939#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1940 }
1941
1942 return( PSA_ERROR_INVALID_ARGUMENT );
1943}
Steven Cooremand13a70f2021-03-19 15:24:23 +01001944
1945/*
1946 * MAC functions
1947 */
1948psa_status_t psa_driver_wrapper_mac_compute(
1949 const psa_key_attributes_t *attributes,
1950 const uint8_t *key_buffer,
1951 size_t key_buffer_size,
1952 psa_algorithm_t alg,
1953 const uint8_t *input,
1954 size_t input_length,
1955 uint8_t *mac,
1956 size_t mac_size,
1957 size_t *mac_length )
1958{
1959 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1960 psa_key_location_t location =
1961 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1962
1963 switch( location )
1964 {
1965 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1966 /* Key is stored in the slot in export representation, so
1967 * cycle through all known transparent accelerators */
1968#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1969#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001970 status = mbedtls_test_transparent_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01001971 attributes, key_buffer, key_buffer_size, alg,
1972 input, input_length,
1973 mac, mac_size, mac_length );
1974 /* Declared with fallback == true */
1975 if( status != PSA_ERROR_NOT_SUPPORTED )
1976 return( status );
1977#endif /* PSA_CRYPTO_DRIVER_TEST */
1978#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1979#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1980 /* Fell through, meaning no accelerator supports this operation */
1981 status = mbedtls_psa_mac_compute(
1982 attributes, key_buffer, key_buffer_size, alg,
1983 input, input_length,
1984 mac, mac_size, mac_length );
1985 if( status != PSA_ERROR_NOT_SUPPORTED )
1986 return( status );
1987#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1988 return( PSA_ERROR_NOT_SUPPORTED );
1989
1990 /* Add cases for opaque driver here */
1991#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1992#if defined(PSA_CRYPTO_DRIVER_TEST)
1993 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001994 status = mbedtls_test_opaque_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01001995 attributes, key_buffer, key_buffer_size, alg,
1996 input, input_length,
1997 mac, mac_size, mac_length );
1998 return( status );
1999#endif /* PSA_CRYPTO_DRIVER_TEST */
2000#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2001 default:
2002 /* Key is declared with a lifetime not known to us */
2003 (void) key_buffer;
2004 (void) key_buffer_size;
2005 (void) alg;
2006 (void) input;
2007 (void) input_length;
2008 (void) mac;
2009 (void) mac_size;
2010 (void) mac_length;
2011 (void) status;
2012 return( PSA_ERROR_INVALID_ARGUMENT );
2013 }
2014}
2015
2016psa_status_t psa_driver_wrapper_mac_sign_setup(
2017 psa_mac_operation_t *operation,
2018 const psa_key_attributes_t *attributes,
2019 const uint8_t *key_buffer,
2020 size_t key_buffer_size,
2021 psa_algorithm_t alg )
2022{
2023 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2024 psa_key_location_t location =
2025 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
2026
2027 switch( location )
2028 {
2029 case PSA_KEY_LOCATION_LOCAL_STORAGE:
2030 /* Key is stored in the slot in export representation, so
2031 * cycle through all known transparent accelerators */
2032#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2033#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002034 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002035 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002036 attributes,
2037 key_buffer, key_buffer_size,
2038 alg );
2039 /* Declared with fallback == true */
2040 if( status == PSA_SUCCESS )
2041 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
2042
2043 if( status != PSA_ERROR_NOT_SUPPORTED )
2044 return( status );
2045#endif /* PSA_CRYPTO_DRIVER_TEST */
2046#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2047#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2048 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002049 status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002050 attributes,
2051 key_buffer, key_buffer_size,
2052 alg );
2053 if( status == PSA_SUCCESS )
2054 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
2055
2056 if( status != PSA_ERROR_NOT_SUPPORTED )
2057 return( status );
2058#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2059 return( PSA_ERROR_NOT_SUPPORTED );
2060
2061 /* Add cases for opaque driver here */
2062#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2063#if defined(PSA_CRYPTO_DRIVER_TEST)
2064 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002065 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002066 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002067 attributes,
2068 key_buffer, key_buffer_size,
2069 alg );
2070
2071 if( status == PSA_SUCCESS )
2072 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
2073
2074 return( status );
2075#endif /* PSA_CRYPTO_DRIVER_TEST */
2076#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2077 default:
2078 /* Key is declared with a lifetime not known to us */
2079 (void) status;
2080 (void) key_buffer;
2081 (void) key_buffer_size;
2082 (void) alg;
2083 return( PSA_ERROR_INVALID_ARGUMENT );
2084 }
2085}
2086
2087psa_status_t psa_driver_wrapper_mac_verify_setup(
2088 psa_mac_operation_t *operation,
2089 const psa_key_attributes_t *attributes,
2090 const uint8_t *key_buffer,
2091 size_t key_buffer_size,
2092 psa_algorithm_t alg )
2093{
2094 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2095 psa_key_location_t location =
2096 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
2097
2098 switch( location )
2099 {
2100 case PSA_KEY_LOCATION_LOCAL_STORAGE:
2101 /* Key is stored in the slot in export representation, so
2102 * cycle through all known transparent accelerators */
2103#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2104#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002105 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002106 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002107 attributes,
2108 key_buffer, key_buffer_size,
2109 alg );
2110 /* Declared with fallback == true */
2111 if( status == PSA_SUCCESS )
2112 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
2113
2114 if( status != PSA_ERROR_NOT_SUPPORTED )
2115 return( status );
2116#endif /* PSA_CRYPTO_DRIVER_TEST */
2117#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2118#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2119 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002120 status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002121 attributes,
2122 key_buffer, key_buffer_size,
2123 alg );
2124 if( status == PSA_SUCCESS )
2125 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
2126
2127 if( status != PSA_ERROR_NOT_SUPPORTED )
2128 return( status );
2129#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2130 return( PSA_ERROR_NOT_SUPPORTED );
2131
2132 /* Add cases for opaque driver here */
2133#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2134#if defined(PSA_CRYPTO_DRIVER_TEST)
2135 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002136 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002137 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002138 attributes,
2139 key_buffer, key_buffer_size,
2140 alg );
2141
2142 if( status == PSA_SUCCESS )
2143 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
2144
2145 return( status );
2146#endif /* PSA_CRYPTO_DRIVER_TEST */
2147#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2148 default:
2149 /* Key is declared with a lifetime not known to us */
2150 (void) status;
2151 (void) key_buffer;
2152 (void) key_buffer_size;
2153 (void) alg;
2154 return( PSA_ERROR_INVALID_ARGUMENT );
2155 }
2156}
2157
2158psa_status_t psa_driver_wrapper_mac_update(
2159 psa_mac_operation_t *operation,
2160 const uint8_t *input,
2161 size_t input_length )
2162{
2163 switch( operation->id )
2164 {
2165#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2166 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002167 return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002168 input, input_length ) );
2169#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2170
2171#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2172#if defined(PSA_CRYPTO_DRIVER_TEST)
2173 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002174 return( mbedtls_test_transparent_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002175 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002176 input, input_length ) );
2177
2178 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002179 return( mbedtls_test_opaque_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002180 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002181 input, input_length ) );
2182#endif /* PSA_CRYPTO_DRIVER_TEST */
2183#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2184 default:
2185 (void) input;
2186 (void) input_length;
2187 return( PSA_ERROR_INVALID_ARGUMENT );
2188 }
2189}
2190
2191psa_status_t psa_driver_wrapper_mac_sign_finish(
2192 psa_mac_operation_t *operation,
2193 uint8_t *mac,
2194 size_t mac_size,
2195 size_t *mac_length )
2196{
2197 switch( operation->id )
2198 {
2199#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2200 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002201 return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002202 mac, mac_size, mac_length ) );
2203#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2204
2205#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2206#if defined(PSA_CRYPTO_DRIVER_TEST)
2207 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002208 return( mbedtls_test_transparent_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002209 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002210 mac, mac_size, mac_length ) );
2211
2212 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002213 return( mbedtls_test_opaque_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002214 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002215 mac, mac_size, mac_length ) );
2216#endif /* PSA_CRYPTO_DRIVER_TEST */
2217#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2218 default:
2219 (void) mac;
2220 (void) mac_size;
2221 (void) mac_length;
2222 return( PSA_ERROR_INVALID_ARGUMENT );
2223 }
2224}
2225
2226psa_status_t psa_driver_wrapper_mac_verify_finish(
2227 psa_mac_operation_t *operation,
2228 const uint8_t *mac,
2229 size_t mac_length )
2230{
2231 switch( operation->id )
2232 {
2233#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2234 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002235 return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002236 mac, mac_length ) );
2237#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2238
2239#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2240#if defined(PSA_CRYPTO_DRIVER_TEST)
2241 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002242 return( mbedtls_test_transparent_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002243 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002244 mac, mac_length ) );
2245
2246 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002247 return( mbedtls_test_opaque_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002248 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002249 mac, mac_length ) );
2250#endif /* PSA_CRYPTO_DRIVER_TEST */
2251#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2252 default:
2253 (void) mac;
2254 (void) mac_length;
2255 return( PSA_ERROR_INVALID_ARGUMENT );
2256 }
2257}
2258
2259psa_status_t psa_driver_wrapper_mac_abort(
2260 psa_mac_operation_t *operation )
2261{
Steven Cooremand13a70f2021-03-19 15:24:23 +01002262 switch( operation->id )
2263 {
2264#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2265 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooremane6804192021-03-19 18:28:56 +01002266 return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002267#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2268
2269#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2270#if defined(PSA_CRYPTO_DRIVER_TEST)
2271 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002272 return( mbedtls_test_transparent_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01002273 &operation->ctx.transparent_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002274 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002275 return( mbedtls_test_opaque_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01002276 &operation->ctx.opaque_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002277#endif /* PSA_CRYPTO_DRIVER_TEST */
2278#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2279 default:
Steven Cooremane6804192021-03-19 18:28:56 +01002280 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002281 }
Steven Cooremand13a70f2021-03-19 15:24:23 +01002282}
Gilles Peskine1905a242021-04-24 13:19:45 +02002283
2284#endif /* MBEDTLS_PSA_CRYPTO_C */