blob: e145dd4d62b9faf8b94cbc1b308467c0898e884b [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
Archanad8a83dc2021-06-14 10:04:16 +0530383/** calculate the key buffer size required to store the key material of a key
384 * associated with an opaque driver from input key data.
385 *
386 *
387 * \param[in] attributes The key attributes
388 * \param[in] data The input key data.
389 * \param[in] data_length The input data length.
390 * \param[out] key_buffer_size Minimum buffer size to contain the key material.
391 *
392 * \retval #PSA_SUCCESS
393 * \retval #PSA_ERROR_INVALID_ARGUMENT
394 * \retval #PSA_ERROR_NOT_SUPPORTED
395 */
396psa_status_t psa_driver_wrapper_get_key_buffer_size_from_key_data(
397 const psa_key_attributes_t *attributes,
398 const uint8_t *data,
399 size_t data_length,
400 size_t *key_buffer_size )
401{
402 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
403 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 */
Archana4d7ae1d2021-07-07 02:50:22 +0530462 *key_buffer_size = mbedtls_test_opaque_size_function( key_type, key_bits );
Ronald Cron4607c822021-04-14 10:55:34 +0200463 return( ( *key_buffer_size != 0 ) ?
464 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
John Durkop2c618352020-09-22 06:54:01 -0700465#endif /* PSA_CRYPTO_DRIVER_TEST */
466
467 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100468 (void)key_type;
469 (void)key_bits;
Archanad8a83dc2021-06-14 10:04:16 +0530470 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200471 }
472}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200473
Ronald Cron977c2472020-10-13 08:32:21 +0200474psa_status_t psa_driver_wrapper_generate_key(
475 const psa_key_attributes_t *attributes,
476 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200477{
Ronald Cron977c2472020-10-13 08:32:21 +0200478 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
479 psa_key_location_t location =
480 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
481
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200482 /* Try dynamically-registered SE interface first */
483#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
484 const psa_drv_se_t *drv;
485 psa_drv_se_context_t *drv_context;
486
Ronald Cron977c2472020-10-13 08:32:21 +0200487 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200488 {
489 size_t pubkey_length = 0; /* We don't support this feature yet */
490 if( drv->key_management == NULL ||
491 drv->key_management->p_generate == NULL )
492 {
493 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200494 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200495 }
496 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200497 drv_context,
498 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100499 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200500 }
501#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
502
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200503 switch( location )
504 {
505 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200506#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200507 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200508 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200509 {
Ronald Cron977c2472020-10-13 08:32:21 +0200510 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200511#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200512 status = mbedtls_test_transparent_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200513 attributes, key_buffer, key_buffer_size,
514 key_buffer_length );
515 /* Declared with fallback == true */
516 if( status != PSA_ERROR_NOT_SUPPORTED )
517 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200518#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200519 }
520#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
521
522 /* Software fallback */
523 status = psa_generate_key_internal(
524 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200525 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200526
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200527 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200528#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200529#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100530 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200531 status = mbedtls_test_opaque_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200532 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200533 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200534#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200535#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
536
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200537 default:
538 /* Key is declared with a lifetime not known to us */
539 status = PSA_ERROR_INVALID_ARGUMENT;
540 break;
541 }
542
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200543 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200544}
545
Ronald Cron83282872020-11-22 14:02:39 +0100546psa_status_t psa_driver_wrapper_import_key(
547 const psa_key_attributes_t *attributes,
548 const uint8_t *data,
549 size_t data_length,
550 uint8_t *key_buffer,
551 size_t key_buffer_size,
552 size_t *key_buffer_length,
553 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200554{
Steven Cooreman04524762020-10-13 17:43:44 +0200555 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100556 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
557 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200558
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100559 /* Try dynamically-registered SE interface first */
560#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
561 const psa_drv_se_t *drv;
562 psa_drv_se_context_t *drv_context;
563
564 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
565 {
566 if( drv->key_management == NULL ||
567 drv->key_management->p_import == NULL )
568 return( PSA_ERROR_NOT_SUPPORTED );
569
570 /* The driver should set the number of key bits, however in
571 * case it doesn't, we initialize bits to an invalid value. */
572 *bits = PSA_MAX_KEY_BITS + 1;
573 status = drv->key_management->p_import(
574 drv_context,
575 *( (psa_key_slot_number_t *)key_buffer ),
576 attributes, data, data_length, bits );
577
578 if( status != PSA_SUCCESS )
579 return( status );
580
581 if( (*bits) > PSA_MAX_KEY_BITS )
582 return( PSA_ERROR_NOT_SUPPORTED );
583
584 return( PSA_SUCCESS );
585 }
586#endif /* PSA_CRYPTO_SE_C */
587
Ronald Cronbf33c932020-11-28 18:06:53 +0100588 switch( location )
589 {
590 case PSA_KEY_LOCATION_LOCAL_STORAGE:
591 /* Key is stored in the slot in export representation, so
592 * cycle through all known transparent accelerators */
593#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
594#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200595 status = mbedtls_test_transparent_import_key(
596 attributes,
597 data, data_length,
598 key_buffer, key_buffer_size,
599 key_buffer_length, bits );
Ronald Cronbf33c932020-11-28 18:06:53 +0100600 /* Declared with fallback == true */
601 if( status != PSA_ERROR_NOT_SUPPORTED )
602 return( status );
603#endif /* PSA_CRYPTO_DRIVER_TEST */
604#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
605 /* Fell through, meaning no accelerator supports this operation */
606 return( psa_import_key_into_slot( attributes,
607 data, data_length,
608 key_buffer, key_buffer_size,
609 key_buffer_length, bits ) );
Archana4d7ae1d2021-07-07 02:50:22 +0530610 /* Add cases for opaque driver here */
611#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
612#if defined(PSA_CRYPTO_DRIVER_TEST)
613 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
614 return( mbedtls_test_opaque_import_key(
615 attributes,
616 data, data_length,
617 key_buffer, key_buffer_size,
618 key_buffer_length, bits ) );
619#endif /* PSA_CRYPTO_DRIVER_TEST */
620#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cronbf33c932020-11-28 18:06:53 +0100621 default:
Ronald Cronbf33c932020-11-28 18:06:53 +0100622 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100623 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100624 }
625
Steven Cooreman04524762020-10-13 17:43:44 +0200626}
627
Ronald Cron67227982020-11-26 15:16:05 +0100628psa_status_t psa_driver_wrapper_export_key(
629 const psa_key_attributes_t *attributes,
630 const uint8_t *key_buffer, size_t key_buffer_size,
631 uint8_t *data, size_t data_size, size_t *data_length )
632
633{
634 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
635 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
636 psa_get_key_lifetime( attributes ) );
637
Ronald Cron152e3f82020-11-26 16:06:41 +0100638 /* Try dynamically-registered SE interface first */
639#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
640 const psa_drv_se_t *drv;
641 psa_drv_se_context_t *drv_context;
642
643 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
644 {
645 if( ( drv->key_management == NULL ) ||
646 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100647 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100648 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100649 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100650
651 return( drv->key_management->p_export(
652 drv_context,
653 *( (psa_key_slot_number_t *)key_buffer ),
654 data, data_size, data_length ) );
655 }
656#endif /* PSA_CRYPTO_SE_C */
657
Ronald Cron67227982020-11-26 15:16:05 +0100658 switch( location )
659 {
660 case PSA_KEY_LOCATION_LOCAL_STORAGE:
661 return( psa_export_key_internal( attributes,
662 key_buffer,
663 key_buffer_size,
664 data,
665 data_size,
666 data_length ) );
667
668 /* Add cases for opaque driver here */
669#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
670#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100671 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200672 return( mbedtls_test_opaque_export_key( attributes,
673 key_buffer,
674 key_buffer_size,
675 data,
676 data_size,
677 data_length ) );
Ronald Cron67227982020-11-26 15:16:05 +0100678#endif /* PSA_CRYPTO_DRIVER_TEST */
679#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
680 default:
681 /* Key is declared with a lifetime not known to us */
682 return( status );
683 }
684}
685
Ronald Cron84cc9942020-11-25 14:30:05 +0100686psa_status_t psa_driver_wrapper_export_public_key(
687 const psa_key_attributes_t *attributes,
688 const uint8_t *key_buffer, size_t key_buffer_size,
689 uint8_t *data, size_t data_size, size_t *data_length )
690
Steven Cooremanb9b84422020-10-14 14:39:20 +0200691{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200692 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100693 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
694 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200695
Ronald Cron152e3f82020-11-26 16:06:41 +0100696 /* Try dynamically-registered SE interface first */
697#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
698 const psa_drv_se_t *drv;
699 psa_drv_se_context_t *drv_context;
700
701 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
702 {
703 if( ( drv->key_management == NULL ) ||
704 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100705 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100706 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100707 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100708
709 return( drv->key_management->p_export_public(
710 drv_context,
711 *( (psa_key_slot_number_t *)key_buffer ),
712 data, data_size, data_length ) );
713 }
714#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
715
Steven Cooremanb9b84422020-10-14 14:39:20 +0200716 switch( location )
717 {
718 case PSA_KEY_LOCATION_LOCAL_STORAGE:
719 /* Key is stored in the slot in export representation, so
720 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100721#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200722#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200723 status = mbedtls_test_transparent_export_public_key(
724 attributes,
725 key_buffer,
726 key_buffer_size,
727 data,
728 data_size,
729 data_length );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200730 /* Declared with fallback == true */
731 if( status != PSA_ERROR_NOT_SUPPORTED )
732 return( status );
733#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100734#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200735 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100736 return( psa_export_public_key_internal( attributes,
737 key_buffer,
738 key_buffer_size,
739 data,
740 data_size,
741 data_length ) );
742
Steven Cooremanb9b84422020-10-14 14:39:20 +0200743 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100744#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200745#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100746 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200747 return( mbedtls_test_opaque_export_public_key( attributes,
748 key_buffer,
749 key_buffer_size,
750 data,
751 data_size,
752 data_length ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200753#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100754#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200755 default:
756 /* Key is declared with a lifetime not known to us */
757 return( status );
758 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200759}
760
Steven Cooreman6801f082021-02-19 17:21:22 +0100761psa_status_t psa_driver_wrapper_get_builtin_key(
762 psa_drv_slot_number_t slot_number,
763 psa_key_attributes_t *attributes,
764 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
765{
766 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
767 switch( location )
768 {
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100769#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100770 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200771 return( mbedtls_test_opaque_get_builtin_key(
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100772 slot_number,
773 attributes,
774 key_buffer, key_buffer_size, key_buffer_length ) );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100775#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman6801f082021-02-19 17:21:22 +0100776 default:
777 (void) slot_number;
778 (void) key_buffer;
779 (void) key_buffer_size;
780 (void) key_buffer_length;
781 return( PSA_ERROR_DOES_NOT_EXIST );
782 }
783}
784
Steven Cooreman37941cb2020-07-28 18:49:51 +0200785/*
786 * Cipher functions
787 */
788psa_status_t psa_driver_wrapper_cipher_encrypt(
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100789 const psa_key_attributes_t *attributes,
790 const uint8_t *key_buffer,
791 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200792 psa_algorithm_t alg,
793 const uint8_t *input,
794 size_t input_length,
795 uint8_t *output,
796 size_t output_size,
797 size_t *output_length )
798{
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100799 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
800 psa_key_location_t location =
801 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200802
803 switch( location )
804 {
805 case PSA_KEY_LOCATION_LOCAL_STORAGE:
806 /* Key is stored in the slot in export representation, so
807 * cycle through all known transparent accelerators */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100808#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200809#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100810 status = mbedtls_test_transparent_cipher_encrypt( attributes,
811 key_buffer,
812 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200813 alg,
814 input,
815 input_length,
816 output,
817 output_size,
818 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200819 /* Declared with fallback == true */
820 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200821 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200822#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100823#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
824
825#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
826 return( mbedtls_psa_cipher_encrypt( attributes,
827 key_buffer,
828 key_buffer_size,
829 alg,
830 input,
831 input_length,
832 output,
833 output_size,
834 output_length ) );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200835#else
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200836 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200837#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100838
Steven Cooreman37941cb2020-07-28 18:49:51 +0200839 /* Add cases for opaque driver here */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100840#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200841#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100842 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100843 return( mbedtls_test_opaque_cipher_encrypt( attributes,
844 key_buffer,
845 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200846 alg,
847 input,
848 input_length,
849 output,
850 output_size,
851 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200852#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100853#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
854
Steven Cooreman37941cb2020-07-28 18:49:51 +0200855 default:
856 /* Key is declared with a lifetime not known to us */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100857 (void)status;
858 (void)key_buffer;
859 (void)key_buffer_size;
860 (void)alg;
861 (void)input;
862 (void)input_length;
863 (void)output;
864 (void)output_size;
865 (void)output_length;
866 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200867 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200868}
869
870psa_status_t psa_driver_wrapper_cipher_decrypt(
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100871 const psa_key_attributes_t *attributes,
872 const uint8_t *key_buffer,
873 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200874 psa_algorithm_t alg,
875 const uint8_t *input,
876 size_t input_length,
877 uint8_t *output,
878 size_t output_size,
879 size_t *output_length )
880{
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100881 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
882 psa_key_location_t location =
883 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200884
885 switch( location )
886 {
887 case PSA_KEY_LOCATION_LOCAL_STORAGE:
888 /* Key is stored in the slot in export representation, so
889 * cycle through all known transparent accelerators */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100890#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200891#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100892 status = mbedtls_test_transparent_cipher_decrypt( attributes,
893 key_buffer,
894 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200895 alg,
896 input,
897 input_length,
898 output,
899 output_size,
900 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200901 /* Declared with fallback == true */
902 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200903 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200904#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100905#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
906
907#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
908 return( mbedtls_psa_cipher_decrypt( attributes,
909 key_buffer,
910 key_buffer_size,
911 alg,
912 input,
913 input_length,
914 output,
915 output_size,
916 output_length ) );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200917#else
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200918 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200919#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100920
Steven Cooreman37941cb2020-07-28 18:49:51 +0200921 /* Add cases for opaque driver here */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100922#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200923#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100924 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100925 return( mbedtls_test_opaque_cipher_decrypt( attributes,
926 key_buffer,
927 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200928 alg,
929 input,
930 input_length,
931 output,
932 output_size,
933 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200934#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100935#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
936
Steven Cooreman37941cb2020-07-28 18:49:51 +0200937 default:
938 /* Key is declared with a lifetime not known to us */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100939 (void)status;
940 (void)key_buffer;
941 (void)key_buffer_size;
942 (void)alg;
943 (void)input;
944 (void)input_length;
945 (void)output;
946 (void)output_size;
947 (void)output_length;
948 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200949 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200950}
951
952psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100953 psa_cipher_operation_t *operation,
954 const psa_key_attributes_t *attributes,
955 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200956 psa_algorithm_t alg )
957{
Ronald Cron0b805592020-12-14 18:08:20 +0100958 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +0100959 psa_key_location_t location =
960 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200961
Steven Cooreman37941cb2020-07-28 18:49:51 +0200962 switch( location )
963 {
964 case PSA_KEY_LOCATION_LOCAL_STORAGE:
965 /* Key is stored in the slot in export representation, so
966 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100967#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200968#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200969 status = mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100970 &operation->ctx.transparent_test_driver_ctx,
971 attributes,
972 key_buffer,
973 key_buffer_size,
974 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200975 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200976 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100977 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200978
Ronald Cron0b805592020-12-14 18:08:20 +0100979 if( status != PSA_ERROR_NOT_SUPPORTED )
980 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200981#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100982#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100983#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200984 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100985 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100986 attributes,
987 key_buffer,
988 key_buffer_size,
989 alg );
990 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +0100991 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +0100992
Ronald Cron7b4154d2021-03-19 14:49:41 +0100993 if( status != PSA_ERROR_NOT_SUPPORTED )
994 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100995#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
996 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +0100997
Steven Cooreman37941cb2020-07-28 18:49:51 +0200998 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100999#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001000#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001001 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001002 status = mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001003 &operation->ctx.opaque_test_driver_ctx,
1004 attributes,
1005 key_buffer, key_buffer_size,
1006 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001007
Steven Cooreman37941cb2020-07-28 18:49:51 +02001008 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001009 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001010
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001011 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001012#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001013#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001014 default:
1015 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001016 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001017 (void)key_buffer;
1018 (void)key_buffer_size;
1019 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001020 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001021 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001022}
1023
1024psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +01001025 psa_cipher_operation_t *operation,
1026 const psa_key_attributes_t *attributes,
1027 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001028 psa_algorithm_t alg )
1029{
Steven Cooreman37941cb2020-07-28 18:49:51 +02001030 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +01001031 psa_key_location_t location =
1032 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001033
Steven Cooreman37941cb2020-07-28 18:49:51 +02001034 switch( location )
1035 {
1036 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1037 /* Key is stored in the slot in export representation, so
1038 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +01001039#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001040#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001041 status = mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001042 &operation->ctx.transparent_test_driver_ctx,
1043 attributes,
1044 key_buffer,
1045 key_buffer_size,
1046 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001047 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +02001048 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001049 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +02001050
Ronald Cron0b805592020-12-14 18:08:20 +01001051 if( status != PSA_ERROR_NOT_SUPPORTED )
1052 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001053#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001054#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001055#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001056 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +01001057 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001058 attributes,
1059 key_buffer,
1060 key_buffer_size,
1061 alg );
1062 if( status == PSA_SUCCESS )
1063 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1064
1065 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001066#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1067 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +01001068
Steven Cooreman37941cb2020-07-28 18:49:51 +02001069 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001070#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001071#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001072 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001073 status = mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001074 &operation->ctx.opaque_test_driver_ctx,
1075 attributes,
1076 key_buffer, key_buffer_size,
1077 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001078
Steven Cooreman37941cb2020-07-28 18:49:51 +02001079 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001080 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001081
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001082 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001083#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001084#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001085 default:
1086 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001087 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001088 (void)key_buffer;
1089 (void)key_buffer_size;
1090 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001091 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001092 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001093}
1094
Steven Cooreman37941cb2020-07-28 18:49:51 +02001095psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +01001096 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001097 const uint8_t *iv,
1098 size_t iv_length )
1099{
Ronald Cron49fafa92021-03-10 08:34:23 +01001100 switch( operation->id )
1101 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001102#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001103 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001104 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001105 iv,
1106 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001107#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001108
1109#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001110#if defined(PSA_CRYPTO_DRIVER_TEST)
1111 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001112 return( mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001113 &operation->ctx.transparent_test_driver_ctx,
1114 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001115
Steven Cooreman37941cb2020-07-28 18:49:51 +02001116 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001117 return( mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001118 &operation->ctx.opaque_test_driver_ctx,
1119 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001120#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001121#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001122 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001123
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001124 (void)iv;
1125 (void)iv_length;
1126
Ronald Crondd24c9b2020-12-15 14:10:01 +01001127 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001128}
1129
1130psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +01001131 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001132 const uint8_t *input,
1133 size_t input_length,
1134 uint8_t *output,
1135 size_t output_size,
1136 size_t *output_length )
1137{
Ronald Cron49fafa92021-03-10 08:34:23 +01001138 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001139 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001140#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001141 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001142 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001143 input,
1144 input_length,
1145 output,
1146 output_size,
1147 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001148#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1149
Ronald Cron49fafa92021-03-10 08:34:23 +01001150#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001151#if defined(PSA_CRYPTO_DRIVER_TEST)
1152 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001153 return( mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001154 &operation->ctx.transparent_test_driver_ctx,
1155 input, input_length,
1156 output, output_size, output_length ) );
1157
Steven Cooreman37941cb2020-07-28 18:49:51 +02001158 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001159 return( mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001160 &operation->ctx.opaque_test_driver_ctx,
1161 input, input_length,
1162 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001163#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001164#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001165 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001166
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001167 (void)input;
1168 (void)input_length;
1169 (void)output;
1170 (void)output_size;
1171 (void)output_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_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +01001177 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001178 uint8_t *output,
1179 size_t output_size,
1180 size_t *output_length )
1181{
Ronald Cron49fafa92021-03-10 08:34:23 +01001182 switch( operation->id )
1183 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001184#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001185 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001186 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001187 output,
1188 output_size,
1189 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001190#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001191
1192#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001193#if defined(PSA_CRYPTO_DRIVER_TEST)
1194 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001195 return( mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001196 &operation->ctx.transparent_test_driver_ctx,
1197 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001198
Steven Cooreman37941cb2020-07-28 18:49:51 +02001199 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001200 return( mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001201 &operation->ctx.opaque_test_driver_ctx,
1202 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001203#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001204#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001205 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001206
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001207 (void)output;
1208 (void)output_size;
1209 (void)output_length;
1210
Ronald Crondd24c9b2020-12-15 14:10:01 +01001211 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001212}
1213
1214psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +01001215 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001216{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001217 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001218
Ronald Cron49fafa92021-03-10 08:34:23 +01001219 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001220 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001221#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001222 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001223 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001224#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001225
1226#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001227#if defined(PSA_CRYPTO_DRIVER_TEST)
1228 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001229 status = mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001230 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001231 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001232 &operation->ctx.transparent_test_driver_ctx,
1233 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001234 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001235
Steven Cooreman37941cb2020-07-28 18:49:51 +02001236 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001237 status = mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001238 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001239 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001240 &operation->ctx.opaque_test_driver_ctx,
1241 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001242 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001243#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001244#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001245 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001246
Ronald Cron49fafa92021-03-10 08:34:23 +01001247 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001248 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001249}
1250
Steven Cooreman1e582352021-02-18 17:24:37 +01001251/*
1252 * Hashing functions
1253 */
1254psa_status_t psa_driver_wrapper_hash_compute(
1255 psa_algorithm_t alg,
1256 const uint8_t *input,
1257 size_t input_length,
1258 uint8_t *hash,
1259 size_t hash_size,
1260 size_t *hash_length)
1261{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001262 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001263
1264 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001265#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001266 status = mbedtls_test_transparent_hash_compute(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001267 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001268 if( status != PSA_ERROR_NOT_SUPPORTED )
1269 return( status );
1270#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001271
1272 /* If software fallback is compiled in, try fallback */
1273#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1274 status = mbedtls_psa_hash_compute( alg, input, input_length,
1275 hash, hash_size, hash_length );
1276 if( status != PSA_ERROR_NOT_SUPPORTED )
1277 return( status );
1278#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001279 (void) status;
1280 (void) alg;
1281 (void) input;
1282 (void) input_length;
1283 (void) hash;
1284 (void) hash_size;
1285 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001286
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001287 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001288}
1289
1290psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001291 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001292 psa_algorithm_t alg )
1293{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001294 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001295
Steven Cooreman1e582352021-02-18 17:24:37 +01001296 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001297#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001298 status = mbedtls_test_transparent_hash_setup(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001299 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001300 if( status == PSA_SUCCESS )
1301 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1302
1303 if( status != PSA_ERROR_NOT_SUPPORTED )
1304 return( status );
1305#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001306
1307 /* If software fallback is compiled in, try fallback */
1308#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001309 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001310 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001311 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001312
1313 if( status != PSA_ERROR_NOT_SUPPORTED )
1314 return( status );
1315#endif
1316 /* Nothing left to try if we fall through here */
1317 (void) status;
1318 (void) operation;
1319 (void) alg;
1320 return( PSA_ERROR_NOT_SUPPORTED );
1321}
1322
1323psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001324 const psa_hash_operation_t *source_operation,
1325 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001326{
Steven Cooreman1e582352021-02-18 17:24:37 +01001327 switch( source_operation->id )
1328 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001329#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1330 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1331 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1332 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1333 &target_operation->ctx.mbedtls_ctx ) );
1334#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001335#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001336 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1337 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Ronald Cron7f13fa22021-04-13 12:41:34 +02001338 return( mbedtls_test_transparent_hash_clone(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001339 &source_operation->ctx.test_driver_ctx,
1340 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001341#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001342 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001343 (void) target_operation;
1344 return( PSA_ERROR_BAD_STATE );
1345 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001346}
1347
1348psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001349 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001350 const uint8_t *input,
1351 size_t input_length )
1352{
Steven Cooreman1e582352021-02-18 17:24:37 +01001353 switch( operation->id )
1354 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001355#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1356 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1357 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1358 input, input_length ) );
1359#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001360#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001361 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001362 return( mbedtls_test_transparent_hash_update(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001363 &operation->ctx.test_driver_ctx,
1364 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001365#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001366 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001367 (void) input;
1368 (void) input_length;
1369 return( PSA_ERROR_BAD_STATE );
1370 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001371}
1372
1373psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001374 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001375 uint8_t *hash,
1376 size_t hash_size,
1377 size_t *hash_length )
1378{
Steven Cooreman1e582352021-02-18 17:24:37 +01001379 switch( operation->id )
1380 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001381#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1382 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1383 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1384 hash, hash_size, hash_length ) );
1385#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001386#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001387 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001388 return( mbedtls_test_transparent_hash_finish(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001389 &operation->ctx.test_driver_ctx,
1390 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001391#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001392 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001393 (void) hash;
1394 (void) hash_size;
1395 (void) hash_length;
1396 return( PSA_ERROR_BAD_STATE );
1397 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001398}
1399
1400psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001401 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001402{
Steven Cooreman1e582352021-02-18 17:24:37 +01001403 switch( operation->id )
1404 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001405#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1406 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1407 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1408#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001409#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001410 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001411 return( mbedtls_test_transparent_hash_abort(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001412 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001413#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001414 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001415 return( PSA_ERROR_BAD_STATE );
1416 }
1417}
1418
Ronald Cronde822812021-03-17 16:08:20 +01001419psa_status_t psa_driver_wrapper_aead_encrypt(
1420 const psa_key_attributes_t *attributes,
1421 const uint8_t *key_buffer, size_t key_buffer_size,
1422 psa_algorithm_t alg,
1423 const uint8_t *nonce, size_t nonce_length,
1424 const uint8_t *additional_data, size_t additional_data_length,
1425 const uint8_t *plaintext, size_t plaintext_length,
1426 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1427{
1428 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1429 psa_key_location_t location =
1430 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1431
1432 switch( location )
1433 {
1434 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1435 /* Key is stored in the slot in export representation, so
1436 * cycle through all known transparent accelerators */
1437
1438#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1439#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001440 status = mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001441 attributes, key_buffer, key_buffer_size,
1442 alg,
1443 nonce, nonce_length,
1444 additional_data, additional_data_length,
1445 plaintext, plaintext_length,
1446 ciphertext, ciphertext_size, ciphertext_length );
1447 /* Declared with fallback == true */
1448 if( status != PSA_ERROR_NOT_SUPPORTED )
1449 return( status );
1450#endif /* PSA_CRYPTO_DRIVER_TEST */
1451#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1452
1453 /* Fell through, meaning no accelerator supports this operation */
1454 return( mbedtls_psa_aead_encrypt(
1455 attributes, key_buffer, key_buffer_size,
1456 alg,
1457 nonce, nonce_length,
1458 additional_data, additional_data_length,
1459 plaintext, plaintext_length,
1460 ciphertext, ciphertext_size, ciphertext_length ) );
1461
1462 /* Add cases for opaque driver here */
1463
1464 default:
1465 /* Key is declared with a lifetime not known to us */
1466 (void)status;
1467 return( PSA_ERROR_INVALID_ARGUMENT );
1468 }
1469}
1470
1471psa_status_t psa_driver_wrapper_aead_decrypt(
1472 const psa_key_attributes_t *attributes,
1473 const uint8_t *key_buffer, size_t key_buffer_size,
1474 psa_algorithm_t alg,
1475 const uint8_t *nonce, size_t nonce_length,
1476 const uint8_t *additional_data, size_t additional_data_length,
1477 const uint8_t *ciphertext, size_t ciphertext_length,
1478 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1479{
1480 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1481 psa_key_location_t location =
1482 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1483
1484 switch( location )
1485 {
1486 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1487 /* Key is stored in the slot in export representation, so
1488 * cycle through all known transparent accelerators */
1489
1490#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1491#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001492 status = mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001493 attributes, key_buffer, key_buffer_size,
1494 alg,
1495 nonce, nonce_length,
1496 additional_data, additional_data_length,
1497 ciphertext, ciphertext_length,
1498 plaintext, plaintext_size, plaintext_length );
1499 /* Declared with fallback == true */
1500 if( status != PSA_ERROR_NOT_SUPPORTED )
1501 return( status );
1502#endif /* PSA_CRYPTO_DRIVER_TEST */
1503#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1504
1505 /* Fell through, meaning no accelerator supports this operation */
1506 return( mbedtls_psa_aead_decrypt(
1507 attributes, key_buffer, key_buffer_size,
1508 alg,
1509 nonce, nonce_length,
1510 additional_data, additional_data_length,
1511 ciphertext, ciphertext_length,
1512 plaintext, plaintext_size, plaintext_length ) );
1513
1514 /* Add cases for opaque driver here */
1515
1516 default:
1517 /* Key is declared with a lifetime not known to us */
1518 (void)status;
1519 return( PSA_ERROR_INVALID_ARGUMENT );
1520 }
1521}
Steven Cooremand13a70f2021-03-19 15:24:23 +01001522
1523
1524/*
1525 * MAC functions
1526 */
1527psa_status_t psa_driver_wrapper_mac_compute(
1528 const psa_key_attributes_t *attributes,
1529 const uint8_t *key_buffer,
1530 size_t key_buffer_size,
1531 psa_algorithm_t alg,
1532 const uint8_t *input,
1533 size_t input_length,
1534 uint8_t *mac,
1535 size_t mac_size,
1536 size_t *mac_length )
1537{
1538 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1539 psa_key_location_t location =
1540 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1541
1542 switch( location )
1543 {
1544 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1545 /* Key is stored in the slot in export representation, so
1546 * cycle through all known transparent accelerators */
1547#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1548#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001549 status = mbedtls_test_transparent_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01001550 attributes, key_buffer, key_buffer_size, alg,
1551 input, input_length,
1552 mac, mac_size, mac_length );
1553 /* Declared with fallback == true */
1554 if( status != PSA_ERROR_NOT_SUPPORTED )
1555 return( status );
1556#endif /* PSA_CRYPTO_DRIVER_TEST */
1557#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1558#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1559 /* Fell through, meaning no accelerator supports this operation */
1560 status = mbedtls_psa_mac_compute(
1561 attributes, key_buffer, key_buffer_size, alg,
1562 input, input_length,
1563 mac, mac_size, mac_length );
1564 if( status != PSA_ERROR_NOT_SUPPORTED )
1565 return( status );
1566#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1567 return( PSA_ERROR_NOT_SUPPORTED );
1568
1569 /* Add cases for opaque driver here */
1570#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1571#if defined(PSA_CRYPTO_DRIVER_TEST)
1572 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001573 status = mbedtls_test_opaque_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01001574 attributes, key_buffer, key_buffer_size, alg,
1575 input, input_length,
1576 mac, mac_size, mac_length );
1577 return( status );
1578#endif /* PSA_CRYPTO_DRIVER_TEST */
1579#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1580 default:
1581 /* Key is declared with a lifetime not known to us */
1582 (void) key_buffer;
1583 (void) key_buffer_size;
1584 (void) alg;
1585 (void) input;
1586 (void) input_length;
1587 (void) mac;
1588 (void) mac_size;
1589 (void) mac_length;
1590 (void) status;
1591 return( PSA_ERROR_INVALID_ARGUMENT );
1592 }
1593}
1594
1595psa_status_t psa_driver_wrapper_mac_sign_setup(
1596 psa_mac_operation_t *operation,
1597 const psa_key_attributes_t *attributes,
1598 const uint8_t *key_buffer,
1599 size_t key_buffer_size,
1600 psa_algorithm_t alg )
1601{
1602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1603 psa_key_location_t location =
1604 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1605
1606 switch( location )
1607 {
1608 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1609 /* Key is stored in the slot in export representation, so
1610 * cycle through all known transparent accelerators */
1611#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1612#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001613 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001614 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001615 attributes,
1616 key_buffer, key_buffer_size,
1617 alg );
1618 /* Declared with fallback == true */
1619 if( status == PSA_SUCCESS )
1620 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1621
1622 if( status != PSA_ERROR_NOT_SUPPORTED )
1623 return( status );
1624#endif /* PSA_CRYPTO_DRIVER_TEST */
1625#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1626#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1627 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001628 status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001629 attributes,
1630 key_buffer, key_buffer_size,
1631 alg );
1632 if( status == PSA_SUCCESS )
1633 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1634
1635 if( status != PSA_ERROR_NOT_SUPPORTED )
1636 return( status );
1637#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1638 return( PSA_ERROR_NOT_SUPPORTED );
1639
1640 /* Add cases for opaque driver here */
1641#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1642#if defined(PSA_CRYPTO_DRIVER_TEST)
1643 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001644 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001645 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001646 attributes,
1647 key_buffer, key_buffer_size,
1648 alg );
1649
1650 if( status == PSA_SUCCESS )
1651 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
1652
1653 return( status );
1654#endif /* PSA_CRYPTO_DRIVER_TEST */
1655#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1656 default:
1657 /* Key is declared with a lifetime not known to us */
1658 (void) status;
1659 (void) key_buffer;
1660 (void) key_buffer_size;
1661 (void) alg;
1662 return( PSA_ERROR_INVALID_ARGUMENT );
1663 }
1664}
1665
1666psa_status_t psa_driver_wrapper_mac_verify_setup(
1667 psa_mac_operation_t *operation,
1668 const psa_key_attributes_t *attributes,
1669 const uint8_t *key_buffer,
1670 size_t key_buffer_size,
1671 psa_algorithm_t alg )
1672{
1673 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1674 psa_key_location_t location =
1675 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1676
1677 switch( location )
1678 {
1679 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1680 /* Key is stored in the slot in export representation, so
1681 * cycle through all known transparent accelerators */
1682#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1683#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001684 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001685 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001686 attributes,
1687 key_buffer, key_buffer_size,
1688 alg );
1689 /* Declared with fallback == true */
1690 if( status == PSA_SUCCESS )
1691 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1692
1693 if( status != PSA_ERROR_NOT_SUPPORTED )
1694 return( status );
1695#endif /* PSA_CRYPTO_DRIVER_TEST */
1696#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1697#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1698 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001699 status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001700 attributes,
1701 key_buffer, key_buffer_size,
1702 alg );
1703 if( status == PSA_SUCCESS )
1704 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1705
1706 if( status != PSA_ERROR_NOT_SUPPORTED )
1707 return( status );
1708#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1709 return( PSA_ERROR_NOT_SUPPORTED );
1710
1711 /* Add cases for opaque driver here */
1712#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1713#if defined(PSA_CRYPTO_DRIVER_TEST)
1714 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001715 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001716 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001717 attributes,
1718 key_buffer, key_buffer_size,
1719 alg );
1720
1721 if( status == PSA_SUCCESS )
1722 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
1723
1724 return( status );
1725#endif /* PSA_CRYPTO_DRIVER_TEST */
1726#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1727 default:
1728 /* Key is declared with a lifetime not known to us */
1729 (void) status;
1730 (void) key_buffer;
1731 (void) key_buffer_size;
1732 (void) alg;
1733 return( PSA_ERROR_INVALID_ARGUMENT );
1734 }
1735}
1736
1737psa_status_t psa_driver_wrapper_mac_update(
1738 psa_mac_operation_t *operation,
1739 const uint8_t *input,
1740 size_t input_length )
1741{
1742 switch( operation->id )
1743 {
1744#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1745 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001746 return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001747 input, input_length ) );
1748#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1749
1750#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1751#if defined(PSA_CRYPTO_DRIVER_TEST)
1752 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001753 return( mbedtls_test_transparent_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001754 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001755 input, input_length ) );
1756
1757 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001758 return( mbedtls_test_opaque_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001759 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001760 input, input_length ) );
1761#endif /* PSA_CRYPTO_DRIVER_TEST */
1762#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1763 default:
1764 (void) input;
1765 (void) input_length;
1766 return( PSA_ERROR_INVALID_ARGUMENT );
1767 }
1768}
1769
1770psa_status_t psa_driver_wrapper_mac_sign_finish(
1771 psa_mac_operation_t *operation,
1772 uint8_t *mac,
1773 size_t mac_size,
1774 size_t *mac_length )
1775{
1776 switch( operation->id )
1777 {
1778#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1779 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001780 return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001781 mac, mac_size, mac_length ) );
1782#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1783
1784#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1785#if defined(PSA_CRYPTO_DRIVER_TEST)
1786 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001787 return( mbedtls_test_transparent_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001788 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001789 mac, mac_size, mac_length ) );
1790
1791 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001792 return( mbedtls_test_opaque_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001793 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001794 mac, mac_size, mac_length ) );
1795#endif /* PSA_CRYPTO_DRIVER_TEST */
1796#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1797 default:
1798 (void) mac;
1799 (void) mac_size;
1800 (void) mac_length;
1801 return( PSA_ERROR_INVALID_ARGUMENT );
1802 }
1803}
1804
1805psa_status_t psa_driver_wrapper_mac_verify_finish(
1806 psa_mac_operation_t *operation,
1807 const uint8_t *mac,
1808 size_t mac_length )
1809{
1810 switch( operation->id )
1811 {
1812#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1813 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001814 return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001815 mac, mac_length ) );
1816#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1817
1818#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1819#if defined(PSA_CRYPTO_DRIVER_TEST)
1820 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001821 return( mbedtls_test_transparent_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001822 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001823 mac, mac_length ) );
1824
1825 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001826 return( mbedtls_test_opaque_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001827 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001828 mac, mac_length ) );
1829#endif /* PSA_CRYPTO_DRIVER_TEST */
1830#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1831 default:
1832 (void) mac;
1833 (void) mac_length;
1834 return( PSA_ERROR_INVALID_ARGUMENT );
1835 }
1836}
1837
1838psa_status_t psa_driver_wrapper_mac_abort(
1839 psa_mac_operation_t *operation )
1840{
Steven Cooremand13a70f2021-03-19 15:24:23 +01001841 switch( operation->id )
1842 {
1843#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1844 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooremane6804192021-03-19 18:28:56 +01001845 return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01001846#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1847
1848#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1849#if defined(PSA_CRYPTO_DRIVER_TEST)
1850 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001851 return( mbedtls_test_transparent_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01001852 &operation->ctx.transparent_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01001853 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001854 return( mbedtls_test_opaque_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01001855 &operation->ctx.opaque_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01001856#endif /* PSA_CRYPTO_DRIVER_TEST */
1857#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1858 default:
Steven Cooremane6804192021-03-19 18:28:56 +01001859 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand13a70f2021-03-19 15:24:23 +01001860 }
Steven Cooremand13a70f2021-03-19 15:24:23 +01001861}
Gilles Peskine1905a242021-04-24 13:19:45 +02001862
1863#endif /* MBEDTLS_PSA_CRYPTO_C */