blob: 536505ef41ea29451d81ff66dc8a849ed9d9372d [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
Ronald Cronde822812021-03-17 16:08:20 +010022#include "psa_crypto_aead.h"
Ronald Cron0b805592020-12-14 18:08:20 +010023#include "psa_crypto_cipher.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020024#include "psa_crypto_core.h"
25#include "psa_crypto_driver_wrappers.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010026#include "psa_crypto_hash.h"
27
Steven Cooreman2a1664c2020-07-20 15:33:08 +020028#include "mbedtls/platform.h"
29
30#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020031
32/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020033#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020034#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020035#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020036#endif
37#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020038#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020039#endif
Steven Cooreman0d7c64d2020-09-07 16:17:55 +020040#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020041#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020042
Steven Cooreman56250fd2020-09-04 13:07:15 +020043/* Repeat above block for each JSON-declared driver during autogeneration */
Steven Cooremanaa87fd02021-03-15 18:54:03 +010044#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Steven Cooreman56250fd2020-09-04 13:07:15 +020045
Steven Cooreman1e582352021-02-18 17:24:37 +010046/* Auto-generated values depending on which drivers are registered.
47 * ID 0 is reserved for unallocated operations.
48 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman830aff22021-03-09 09:50:44 +010049#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
50
Steven Cooreman37941cb2020-07-28 18:49:51 +020051#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010052#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
53#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020054#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020055
56/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020057#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020058/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
59 * SE driver is present, to avoid unused argument errors at compile time. */
60#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020061#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020062#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020063#include "psa_crypto_se.h"
64#endif
65
Steven Cooremancd84cb42020-07-16 20:28:36 +020066/* Start delegation functions */
Ronald Cron9f17aa42020-12-08 17:07:25 +010067psa_status_t psa_driver_wrapper_sign_hash(
68 const psa_key_attributes_t *attributes,
69 const uint8_t *key_buffer, size_t key_buffer_size,
70 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
71 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +020072{
Steven Cooreman7a250572020-07-17 16:43:05 +020073 /* Try dynamically-registered SE interface first */
74#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
75 const psa_drv_se_t *drv;
76 psa_drv_se_context_t *drv_context;
77
Ronald Cron9f17aa42020-12-08 17:07:25 +010078 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +020079 {
80 if( drv->asymmetric == NULL ||
81 drv->asymmetric->p_sign == NULL )
82 {
83 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +020084 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +020085 }
Ronald Cron9f17aa42020-12-08 17:07:25 +010086 return( drv->asymmetric->p_sign(
87 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
88 alg, hash, hash_length,
89 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +020090 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +020091#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +020092
Ronald Cronfce9df22020-12-08 18:06:03 +010093 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +010094 psa_key_location_t location =
95 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +020096
97 switch( location )
98 {
99 case PSA_KEY_LOCATION_LOCAL_STORAGE:
100 /* Key is stored in the slot in export representation, so
101 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100102#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200103#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100104 status = test_transparent_signature_sign_hash( attributes,
105 key_buffer,
106 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200107 alg,
108 hash,
109 hash_length,
110 signature,
111 signature_size,
112 signature_length );
113 /* Declared with fallback == true */
114 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200115 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200116#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100117#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200118 /* Fell through, meaning no accelerator supports this operation */
Ronald Cronfce9df22020-12-08 18:06:03 +0100119 return( psa_sign_hash_internal( attributes,
120 key_buffer,
121 key_buffer_size,
122 alg,
123 hash,
124 hash_length,
125 signature,
126 signature_size,
127 signature_length ) );
128
Steven Cooremancd84cb42020-07-16 20:28:36 +0200129 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100130#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200131#if defined(PSA_CRYPTO_DRIVER_TEST)
132 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100133 return( test_opaque_signature_sign_hash( attributes,
134 key_buffer,
135 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200136 alg,
137 hash,
138 hash_length,
139 signature,
140 signature_size,
141 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200142#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100143#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200144 default:
145 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100146 (void)status;
147 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200148 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200149}
150
Ronald Cron9f17aa42020-12-08 17:07:25 +0100151psa_status_t psa_driver_wrapper_verify_hash(
152 const psa_key_attributes_t *attributes,
153 const uint8_t *key_buffer, size_t key_buffer_size,
154 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
155 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200156{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200157 /* Try dynamically-registered SE interface first */
158#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
159 const psa_drv_se_t *drv;
160 psa_drv_se_context_t *drv_context;
161
Ronald Cron9f17aa42020-12-08 17:07:25 +0100162 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200163 {
164 if( drv->asymmetric == NULL ||
165 drv->asymmetric->p_verify == NULL )
166 {
167 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200168 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200169 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100170 return( drv->asymmetric->p_verify(
171 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
172 alg, hash, hash_length,
173 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200174 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200175#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200176
Ronald Cronfce9df22020-12-08 18:06:03 +0100177 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100178 psa_key_location_t location =
179 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200180
181 switch( location )
182 {
183 case PSA_KEY_LOCATION_LOCAL_STORAGE:
184 /* Key is stored in the slot in export representation, so
185 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100186#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200187#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100188 status = test_transparent_signature_verify_hash( attributes,
189 key_buffer,
190 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200191 alg,
192 hash,
193 hash_length,
194 signature,
195 signature_length );
196 /* Declared with fallback == true */
197 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200198 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200199#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100200#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
201
202 return( psa_verify_hash_internal( attributes,
203 key_buffer,
204 key_buffer_size,
205 alg,
206 hash,
207 hash_length,
208 signature,
209 signature_length ) );
210
Steven Cooreman55ae2172020-07-17 19:46:15 +0200211 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100212#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200213#if defined(PSA_CRYPTO_DRIVER_TEST)
214 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100215 return( test_opaque_signature_verify_hash( attributes,
216 key_buffer,
217 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200218 alg,
219 hash,
220 hash_length,
221 signature,
222 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200223#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100224#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200225 default:
226 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100227 (void)status;
228 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200229 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200230}
231
Ronald Cron31216282020-12-05 18:47:56 +0100232/** Get the key buffer size for the key material of a generated key in the
233 * case of an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200234 *
Ronald Cron31216282020-12-05 18:47:56 +0100235 * \param[in] attributes The key attributes.
236 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200237 *
238 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100239 * The minimum size for a buffer to contain the key material has been
240 * returned successfully.
241 * \retval #PSA_ERROR_INVALID_ARGUMENT
242 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200243 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100244 * The type and/or the size in bits of the key or the combination of
245 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200246 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100247psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100248 const psa_key_attributes_t *attributes,
249 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200250{
John Durkop2c618352020-09-22 06:54:01 -0700251 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
252 psa_key_type_t key_type = attributes->core.type;
253 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200254
Ronald Cron31216282020-12-05 18:47:56 +0100255 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700256 switch( location )
257 {
John Durkop2c618352020-09-22 06:54:01 -0700258#if defined(PSA_CRYPTO_DRIVER_TEST)
259 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
John Durkopac93e3b2020-10-16 06:48:55 -0700260#ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
Ronald Cron31216282020-12-05 18:47:56 +0100261 *key_buffer_size = test_size_function( key_type, key_bits );
John Durkop2c618352020-09-22 06:54:01 -0700262 return( PSA_SUCCESS );
263#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
264 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
265 {
Ronald Cron31216282020-12-05 18:47:56 +0100266 int public_key_overhead =
267 ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ?
268 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
269 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop135ce692020-10-19 07:12:28 -0700270 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
271 + public_key_overhead;
272 }
Ronald Cron31216282020-12-05 18:47:56 +0100273 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
John Durkop135ce692020-10-19 07:12:28 -0700274 {
Ronald Cron31216282020-12-05 18:47:56 +0100275 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700276 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
277 }
John Durkop135ce692020-10-19 07:12:28 -0700278 else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
Ronald Cron31216282020-12-05 18:47:56 +0100279 !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
John Durkop2c618352020-09-22 06:54:01 -0700280 {
Ronald Cron31216282020-12-05 18:47:56 +0100281 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700282 + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
283 * ( ( key_bits + 7 ) / 8 );
284 }
285 else
286 {
287 return( PSA_ERROR_NOT_SUPPORTED );
288 }
289 return( PSA_SUCCESS );
290#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
291#endif /* PSA_CRYPTO_DRIVER_TEST */
292
293 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100294 (void)key_type;
295 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200296 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200297 }
298}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200299
Ronald Cron977c2472020-10-13 08:32:21 +0200300psa_status_t psa_driver_wrapper_generate_key(
301 const psa_key_attributes_t *attributes,
302 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200303{
Ronald Cron977c2472020-10-13 08:32:21 +0200304 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
305 psa_key_location_t location =
306 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
307
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200308 /* Try dynamically-registered SE interface first */
309#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
310 const psa_drv_se_t *drv;
311 psa_drv_se_context_t *drv_context;
312
Ronald Cron977c2472020-10-13 08:32:21 +0200313 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200314 {
315 size_t pubkey_length = 0; /* We don't support this feature yet */
316 if( drv->key_management == NULL ||
317 drv->key_management->p_generate == NULL )
318 {
319 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200320 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200321 }
322 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200323 drv_context,
324 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100325 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200326 }
327#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
328
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200329 switch( location )
330 {
331 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200332#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200333 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200334 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200335 {
Ronald Cron977c2472020-10-13 08:32:21 +0200336 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200337#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron977c2472020-10-13 08:32:21 +0200338 status = test_transparent_generate_key(
339 attributes, key_buffer, key_buffer_size,
340 key_buffer_length );
341 /* Declared with fallback == true */
342 if( status != PSA_ERROR_NOT_SUPPORTED )
343 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200344#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200345 }
346#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
347
348 /* Software fallback */
349 status = psa_generate_key_internal(
350 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200351 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200352
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200353 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200354#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200355#if defined(PSA_CRYPTO_DRIVER_TEST)
356 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron977c2472020-10-13 08:32:21 +0200357 status = test_opaque_generate_key(
358 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200359 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200360#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200361#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
362
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200363 default:
364 /* Key is declared with a lifetime not known to us */
365 status = PSA_ERROR_INVALID_ARGUMENT;
366 break;
367 }
368
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200369 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200370}
371
Ronald Cron83282872020-11-22 14:02:39 +0100372psa_status_t psa_driver_wrapper_import_key(
373 const psa_key_attributes_t *attributes,
374 const uint8_t *data,
375 size_t data_length,
376 uint8_t *key_buffer,
377 size_t key_buffer_size,
378 size_t *key_buffer_length,
379 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200380{
Steven Cooreman04524762020-10-13 17:43:44 +0200381 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100382 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
383 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200384
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100385 /* Try dynamically-registered SE interface first */
386#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
387 const psa_drv_se_t *drv;
388 psa_drv_se_context_t *drv_context;
389
390 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
391 {
392 if( drv->key_management == NULL ||
393 drv->key_management->p_import == NULL )
394 return( PSA_ERROR_NOT_SUPPORTED );
395
396 /* The driver should set the number of key bits, however in
397 * case it doesn't, we initialize bits to an invalid value. */
398 *bits = PSA_MAX_KEY_BITS + 1;
399 status = drv->key_management->p_import(
400 drv_context,
401 *( (psa_key_slot_number_t *)key_buffer ),
402 attributes, data, data_length, bits );
403
404 if( status != PSA_SUCCESS )
405 return( status );
406
407 if( (*bits) > PSA_MAX_KEY_BITS )
408 return( PSA_ERROR_NOT_SUPPORTED );
409
410 return( PSA_SUCCESS );
411 }
412#endif /* PSA_CRYPTO_SE_C */
413
Ronald Cronbf33c932020-11-28 18:06:53 +0100414 switch( location )
415 {
416 case PSA_KEY_LOCATION_LOCAL_STORAGE:
417 /* Key is stored in the slot in export representation, so
418 * cycle through all known transparent accelerators */
419#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
420#if defined(PSA_CRYPTO_DRIVER_TEST)
421 status = test_transparent_import_key( attributes,
422 data, data_length,
423 key_buffer, key_buffer_size,
424 key_buffer_length, bits );
425 /* Declared with fallback == true */
426 if( status != PSA_ERROR_NOT_SUPPORTED )
427 return( status );
428#endif /* PSA_CRYPTO_DRIVER_TEST */
429#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
430 /* Fell through, meaning no accelerator supports this operation */
431 return( psa_import_key_into_slot( attributes,
432 data, data_length,
433 key_buffer, key_buffer_size,
434 key_buffer_length, bits ) );
435
436 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100437 /* Importing a key with external storage in not yet supported.
438 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100439 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100440 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100441 }
442
Steven Cooreman04524762020-10-13 17:43:44 +0200443}
444
Ronald Cron67227982020-11-26 15:16:05 +0100445psa_status_t psa_driver_wrapper_export_key(
446 const psa_key_attributes_t *attributes,
447 const uint8_t *key_buffer, size_t key_buffer_size,
448 uint8_t *data, size_t data_size, size_t *data_length )
449
450{
451 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
452 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
453 psa_get_key_lifetime( attributes ) );
454
Ronald Cron152e3f82020-11-26 16:06:41 +0100455 /* Try dynamically-registered SE interface first */
456#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
457 const psa_drv_se_t *drv;
458 psa_drv_se_context_t *drv_context;
459
460 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
461 {
462 if( ( drv->key_management == NULL ) ||
463 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100464 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100465 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100466 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100467
468 return( drv->key_management->p_export(
469 drv_context,
470 *( (psa_key_slot_number_t *)key_buffer ),
471 data, data_size, data_length ) );
472 }
473#endif /* PSA_CRYPTO_SE_C */
474
Ronald Cron67227982020-11-26 15:16:05 +0100475 switch( location )
476 {
477 case PSA_KEY_LOCATION_LOCAL_STORAGE:
478 return( psa_export_key_internal( attributes,
479 key_buffer,
480 key_buffer_size,
481 data,
482 data_size,
483 data_length ) );
484
485 /* Add cases for opaque driver here */
486#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
487#if defined(PSA_CRYPTO_DRIVER_TEST)
488 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
489 return( test_opaque_export_key( attributes,
490 key_buffer,
491 key_buffer_size,
492 data,
493 data_size,
494 data_length ) );
495#endif /* PSA_CRYPTO_DRIVER_TEST */
496#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
497 default:
498 /* Key is declared with a lifetime not known to us */
499 return( status );
500 }
501}
502
Ronald Cron84cc9942020-11-25 14:30:05 +0100503psa_status_t psa_driver_wrapper_export_public_key(
504 const psa_key_attributes_t *attributes,
505 const uint8_t *key_buffer, size_t key_buffer_size,
506 uint8_t *data, size_t data_size, size_t *data_length )
507
Steven Cooremanb9b84422020-10-14 14:39:20 +0200508{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200509 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100510 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
511 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200512
Ronald Cron152e3f82020-11-26 16:06:41 +0100513 /* Try dynamically-registered SE interface first */
514#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
515 const psa_drv_se_t *drv;
516 psa_drv_se_context_t *drv_context;
517
518 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
519 {
520 if( ( drv->key_management == NULL ) ||
521 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100522 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100523 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100524 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100525
526 return( drv->key_management->p_export_public(
527 drv_context,
528 *( (psa_key_slot_number_t *)key_buffer ),
529 data, data_size, data_length ) );
530 }
531#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
532
Steven Cooremanb9b84422020-10-14 14:39:20 +0200533 switch( location )
534 {
535 case PSA_KEY_LOCATION_LOCAL_STORAGE:
536 /* Key is stored in the slot in export representation, so
537 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100538#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200539#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron84cc9942020-11-25 14:30:05 +0100540 status = test_transparent_export_public_key( attributes,
541 key_buffer,
542 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200543 data,
544 data_size,
545 data_length );
546 /* Declared with fallback == true */
547 if( status != PSA_ERROR_NOT_SUPPORTED )
548 return( status );
549#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100550#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200551 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100552 return( psa_export_public_key_internal( attributes,
553 key_buffer,
554 key_buffer_size,
555 data,
556 data_size,
557 data_length ) );
558
Steven Cooremanb9b84422020-10-14 14:39:20 +0200559 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100560#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200561#if defined(PSA_CRYPTO_DRIVER_TEST)
562 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron84cc9942020-11-25 14:30:05 +0100563 return( test_opaque_export_public_key( attributes,
564 key_buffer,
565 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200566 data,
567 data_size,
568 data_length ) );
569#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100570#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200571 default:
572 /* Key is declared with a lifetime not known to us */
573 return( status );
574 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200575}
576
Steven Cooreman37941cb2020-07-28 18:49:51 +0200577/*
578 * Cipher functions
579 */
580psa_status_t psa_driver_wrapper_cipher_encrypt(
581 psa_key_slot_t *slot,
582 psa_algorithm_t alg,
583 const uint8_t *input,
584 size_t input_length,
585 uint8_t *output,
586 size_t output_size,
587 size_t *output_length )
588{
589#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
590 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
591 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
592 psa_key_attributes_t attributes = {
593 .core = slot->attr
594 };
595
596 switch( location )
597 {
598 case PSA_KEY_LOCATION_LOCAL_STORAGE:
599 /* Key is stored in the slot in export representation, so
600 * cycle through all known transparent accelerators */
601#if defined(PSA_CRYPTO_DRIVER_TEST)
602 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100603 slot->key.data,
604 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200605 alg,
606 input,
607 input_length,
608 output,
609 output_size,
610 output_length );
611 /* Declared with fallback == true */
612 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200613 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200614#endif /* PSA_CRYPTO_DRIVER_TEST */
615 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200616 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200617 /* Add cases for opaque driver here */
618#if defined(PSA_CRYPTO_DRIVER_TEST)
619 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
620 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100621 slot->key.data,
622 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200623 alg,
624 input,
625 input_length,
626 output,
627 output_size,
628 output_length ) );
629#endif /* PSA_CRYPTO_DRIVER_TEST */
630 default:
631 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200632 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200633 }
634#else /* PSA_CRYPTO_DRIVER_PRESENT */
635 (void) slot;
636 (void) alg;
637 (void) input;
638 (void) input_length;
639 (void) output;
640 (void) output_size;
641 (void) output_length;
642
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200643 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200644#endif /* PSA_CRYPTO_DRIVER_PRESENT */
645}
646
647psa_status_t psa_driver_wrapper_cipher_decrypt(
648 psa_key_slot_t *slot,
649 psa_algorithm_t alg,
650 const uint8_t *input,
651 size_t input_length,
652 uint8_t *output,
653 size_t output_size,
654 size_t *output_length )
655{
656#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
657 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
658 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
659 psa_key_attributes_t attributes = {
660 .core = slot->attr
661 };
662
663 switch( location )
664 {
665 case PSA_KEY_LOCATION_LOCAL_STORAGE:
666 /* Key is stored in the slot in export representation, so
667 * cycle through all known transparent accelerators */
668#if defined(PSA_CRYPTO_DRIVER_TEST)
669 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100670 slot->key.data,
671 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200672 alg,
673 input,
674 input_length,
675 output,
676 output_size,
677 output_length );
678 /* Declared with fallback == true */
679 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200680 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200681#endif /* PSA_CRYPTO_DRIVER_TEST */
682 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200683 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200684 /* Add cases for opaque driver here */
685#if defined(PSA_CRYPTO_DRIVER_TEST)
686 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
687 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100688 slot->key.data,
689 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200690 alg,
691 input,
692 input_length,
693 output,
694 output_size,
695 output_length ) );
696#endif /* PSA_CRYPTO_DRIVER_TEST */
697 default:
698 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200699 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200700 }
701#else /* PSA_CRYPTO_DRIVER_PRESENT */
702 (void) slot;
703 (void) alg;
704 (void) input;
705 (void) input_length;
706 (void) output;
707 (void) output_size;
708 (void) output_length;
709
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200710 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200711#endif /* PSA_CRYPTO_DRIVER_PRESENT */
712}
713
714psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100715 psa_cipher_operation_t *operation,
716 const psa_key_attributes_t *attributes,
717 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200718 psa_algorithm_t alg )
719{
Ronald Cron0b805592020-12-14 18:08:20 +0100720 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +0100721 psa_key_location_t location =
722 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200723
Steven Cooreman37941cb2020-07-28 18:49:51 +0200724 switch( location )
725 {
726 case PSA_KEY_LOCATION_LOCAL_STORAGE:
727 /* Key is stored in the slot in export representation, so
728 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100729#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200730#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100731 status = test_transparent_cipher_encrypt_setup(
732 &operation->ctx.transparent_test_driver_ctx,
733 attributes,
734 key_buffer,
735 key_buffer_size,
736 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200737 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200738 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100739 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200740
Ronald Cron0b805592020-12-14 18:08:20 +0100741 if( status != PSA_ERROR_NOT_SUPPORTED )
742 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200743#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100744#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100745#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200746 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100747 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100748 attributes,
749 key_buffer,
750 key_buffer_size,
751 alg );
752 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +0100753 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +0100754
Ronald Cron7b4154d2021-03-19 14:49:41 +0100755 if( status != PSA_ERROR_NOT_SUPPORTED )
756 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100757#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
758 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +0100759
Steven Cooreman37941cb2020-07-28 18:49:51 +0200760 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100761#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200762#if defined(PSA_CRYPTO_DRIVER_TEST)
763 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100764 status = test_opaque_cipher_encrypt_setup(
765 &operation->ctx.opaque_test_driver_ctx,
766 attributes,
767 key_buffer, key_buffer_size,
768 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200769
Steven Cooreman37941cb2020-07-28 18:49:51 +0200770 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100771 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200772
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200773 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200774#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100775#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200776 default:
777 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100778 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100779 (void)key_buffer;
780 (void)key_buffer_size;
781 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200782 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200783 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200784}
785
786psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100787 psa_cipher_operation_t *operation,
788 const psa_key_attributes_t *attributes,
789 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200790 psa_algorithm_t alg )
791{
Steven Cooreman37941cb2020-07-28 18:49:51 +0200792 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +0100793 psa_key_location_t location =
794 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200795
Steven Cooreman37941cb2020-07-28 18:49:51 +0200796 switch( location )
797 {
798 case PSA_KEY_LOCATION_LOCAL_STORAGE:
799 /* Key is stored in the slot in export representation, so
800 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100801#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200802#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100803 status = test_transparent_cipher_decrypt_setup(
804 &operation->ctx.transparent_test_driver_ctx,
805 attributes,
806 key_buffer,
807 key_buffer_size,
808 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200809 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200810 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100811 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200812
Ronald Cron0b805592020-12-14 18:08:20 +0100813 if( status != PSA_ERROR_NOT_SUPPORTED )
814 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200815#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100816#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100817#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200818 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100819 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100820 attributes,
821 key_buffer,
822 key_buffer_size,
823 alg );
824 if( status == PSA_SUCCESS )
825 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
826
827 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100828#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
829 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +0100830
Steven Cooreman37941cb2020-07-28 18:49:51 +0200831 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100832#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200833#if defined(PSA_CRYPTO_DRIVER_TEST)
834 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100835 status = test_opaque_cipher_decrypt_setup(
836 &operation->ctx.opaque_test_driver_ctx,
837 attributes,
838 key_buffer, key_buffer_size,
839 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200840
Steven Cooreman37941cb2020-07-28 18:49:51 +0200841 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100842 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200843
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200844 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200845#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100846#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200847 default:
848 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100849 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100850 (void)key_buffer;
851 (void)key_buffer_size;
852 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200853 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200854 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200855}
856
Steven Cooreman37941cb2020-07-28 18:49:51 +0200857psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +0100858 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200859 const uint8_t *iv,
860 size_t iv_length )
861{
Ronald Cron49fafa92021-03-10 08:34:23 +0100862 switch( operation->id )
863 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100864#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100865 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100866 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100867 iv,
868 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100869#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100870
871#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200872#if defined(PSA_CRYPTO_DRIVER_TEST)
873 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100874 return( test_transparent_cipher_set_iv(
875 &operation->ctx.transparent_test_driver_ctx,
876 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +0100877
Steven Cooreman37941cb2020-07-28 18:49:51 +0200878 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100879 return( test_opaque_cipher_set_iv(
880 &operation->ctx.opaque_test_driver_ctx,
881 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200882#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100883#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100884 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200885
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100886 (void)iv;
887 (void)iv_length;
888
Ronald Crondd24c9b2020-12-15 14:10:01 +0100889 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200890}
891
892psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +0100893 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200894 const uint8_t *input,
895 size_t input_length,
896 uint8_t *output,
897 size_t output_size,
898 size_t *output_length )
899{
Ronald Cron49fafa92021-03-10 08:34:23 +0100900 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200901 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100902#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100903 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100904 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100905 input,
906 input_length,
907 output,
908 output_size,
909 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100910#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
911
Ronald Cron49fafa92021-03-10 08:34:23 +0100912#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200913#if defined(PSA_CRYPTO_DRIVER_TEST)
914 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100915 return( test_transparent_cipher_update(
916 &operation->ctx.transparent_test_driver_ctx,
917 input, input_length,
918 output, output_size, output_length ) );
919
Steven Cooreman37941cb2020-07-28 18:49:51 +0200920 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100921 return( test_opaque_cipher_update(
922 &operation->ctx.opaque_test_driver_ctx,
923 input, input_length,
924 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200925#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100926#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100927 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200928
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100929 (void)input;
930 (void)input_length;
931 (void)output;
932 (void)output_size;
933 (void)output_length;
934
Ronald Crondd24c9b2020-12-15 14:10:01 +0100935 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200936}
937
938psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +0100939 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200940 uint8_t *output,
941 size_t output_size,
942 size_t *output_length )
943{
Ronald Cron49fafa92021-03-10 08:34:23 +0100944 switch( operation->id )
945 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100946#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100947 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100948 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100949 output,
950 output_size,
951 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100952#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100953
954#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200955#if defined(PSA_CRYPTO_DRIVER_TEST)
956 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100957 return( test_transparent_cipher_finish(
958 &operation->ctx.transparent_test_driver_ctx,
959 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +0100960
Steven Cooreman37941cb2020-07-28 18:49:51 +0200961 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100962 return( test_opaque_cipher_finish(
963 &operation->ctx.opaque_test_driver_ctx,
964 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200965#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100966#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100967 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200968
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100969 (void)output;
970 (void)output_size;
971 (void)output_length;
972
Ronald Crondd24c9b2020-12-15 14:10:01 +0100973 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200974}
975
976psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +0100977 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200978{
Ronald Crondd24c9b2020-12-15 14:10:01 +0100979 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200980
Ronald Cron49fafa92021-03-10 08:34:23 +0100981 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200982 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100983#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100984 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100985 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100986#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +0100987
988#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200989#if defined(PSA_CRYPTO_DRIVER_TEST)
990 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100991 status = test_transparent_cipher_abort(
992 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200993 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100994 &operation->ctx.transparent_test_driver_ctx,
995 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200996 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +0100997
Steven Cooreman37941cb2020-07-28 18:49:51 +0200998 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100999 status = test_opaque_cipher_abort(
1000 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001001 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001002 &operation->ctx.opaque_test_driver_ctx,
1003 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001004 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001005#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001006#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001007 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001008
Ronald Cron49fafa92021-03-10 08:34:23 +01001009 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001010 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001011}
1012
Steven Cooreman1e582352021-02-18 17:24:37 +01001013/*
1014 * Hashing functions
1015 */
1016psa_status_t psa_driver_wrapper_hash_compute(
1017 psa_algorithm_t alg,
1018 const uint8_t *input,
1019 size_t input_length,
1020 uint8_t *hash,
1021 size_t hash_size,
1022 size_t *hash_length)
1023{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001024 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001025
1026 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001027#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001028 status = mbedtls_transparent_test_driver_hash_compute(
1029 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001030 if( status != PSA_ERROR_NOT_SUPPORTED )
1031 return( status );
1032#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001033
1034 /* If software fallback is compiled in, try fallback */
1035#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1036 status = mbedtls_psa_hash_compute( alg, input, input_length,
1037 hash, hash_size, hash_length );
1038 if( status != PSA_ERROR_NOT_SUPPORTED )
1039 return( status );
1040#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001041 (void) status;
1042 (void) alg;
1043 (void) input;
1044 (void) input_length;
1045 (void) hash;
1046 (void) hash_size;
1047 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001048
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001049 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001050}
1051
1052psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001053 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001054 psa_algorithm_t alg )
1055{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001056 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001057
Steven Cooreman1e582352021-02-18 17:24:37 +01001058 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001059#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001060 status = mbedtls_transparent_test_driver_hash_setup(
1061 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001062 if( status == PSA_SUCCESS )
1063 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1064
1065 if( status != PSA_ERROR_NOT_SUPPORTED )
1066 return( status );
1067#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001068
1069 /* If software fallback is compiled in, try fallback */
1070#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001071 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001072 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001073 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001074
1075 if( status != PSA_ERROR_NOT_SUPPORTED )
1076 return( status );
1077#endif
1078 /* Nothing left to try if we fall through here */
1079 (void) status;
1080 (void) operation;
1081 (void) alg;
1082 return( PSA_ERROR_NOT_SUPPORTED );
1083}
1084
1085psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001086 const psa_hash_operation_t *source_operation,
1087 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001088{
Steven Cooreman1e582352021-02-18 17:24:37 +01001089 switch( source_operation->id )
1090 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001091#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1092 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1093 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1094 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1095 &target_operation->ctx.mbedtls_ctx ) );
1096#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001097#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001098 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1099 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001100 return( mbedtls_transparent_test_driver_hash_clone(
1101 &source_operation->ctx.test_driver_ctx,
1102 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001103#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001104 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001105 (void) target_operation;
1106 return( PSA_ERROR_BAD_STATE );
1107 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001108}
1109
1110psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001111 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001112 const uint8_t *input,
1113 size_t input_length )
1114{
Steven Cooreman1e582352021-02-18 17:24:37 +01001115 switch( operation->id )
1116 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001117#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1118 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1119 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1120 input, input_length ) );
1121#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001122#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001123 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001124 return( mbedtls_transparent_test_driver_hash_update(
1125 &operation->ctx.test_driver_ctx,
1126 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001127#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001128 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001129 (void) input;
1130 (void) input_length;
1131 return( PSA_ERROR_BAD_STATE );
1132 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001133}
1134
1135psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001136 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001137 uint8_t *hash,
1138 size_t hash_size,
1139 size_t *hash_length )
1140{
Steven Cooreman1e582352021-02-18 17:24:37 +01001141 switch( operation->id )
1142 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001143#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1144 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1145 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1146 hash, hash_size, hash_length ) );
1147#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001148#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001149 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001150 return( mbedtls_transparent_test_driver_hash_finish(
1151 &operation->ctx.test_driver_ctx,
1152 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001153#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001154 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001155 (void) hash;
1156 (void) hash_size;
1157 (void) hash_length;
1158 return( PSA_ERROR_BAD_STATE );
1159 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001160}
1161
1162psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001163 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001164{
Steven Cooreman1e582352021-02-18 17:24:37 +01001165 switch( operation->id )
1166 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001167#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1168 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1169 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1170#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001171#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001172 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001173 return( mbedtls_transparent_test_driver_hash_abort(
1174 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001175#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001176 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001177 return( PSA_ERROR_BAD_STATE );
1178 }
1179}
1180
Ronald Cronde822812021-03-17 16:08:20 +01001181psa_status_t psa_driver_wrapper_aead_encrypt(
1182 const psa_key_attributes_t *attributes,
1183 const uint8_t *key_buffer, size_t key_buffer_size,
1184 psa_algorithm_t alg,
1185 const uint8_t *nonce, size_t nonce_length,
1186 const uint8_t *additional_data, size_t additional_data_length,
1187 const uint8_t *plaintext, size_t plaintext_length,
1188 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1189{
1190 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1191 psa_key_location_t location =
1192 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1193
1194 switch( location )
1195 {
1196 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1197 /* Key is stored in the slot in export representation, so
1198 * cycle through all known transparent accelerators */
1199
1200#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1201#if defined(PSA_CRYPTO_DRIVER_TEST)
1202 status = test_transparent_aead_encrypt(
1203 attributes, key_buffer, key_buffer_size,
1204 alg,
1205 nonce, nonce_length,
1206 additional_data, additional_data_length,
1207 plaintext, plaintext_length,
1208 ciphertext, ciphertext_size, ciphertext_length );
1209 /* Declared with fallback == true */
1210 if( status != PSA_ERROR_NOT_SUPPORTED )
1211 return( status );
1212#endif /* PSA_CRYPTO_DRIVER_TEST */
1213#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1214
1215 /* Fell through, meaning no accelerator supports this operation */
1216 return( mbedtls_psa_aead_encrypt(
1217 attributes, key_buffer, key_buffer_size,
1218 alg,
1219 nonce, nonce_length,
1220 additional_data, additional_data_length,
1221 plaintext, plaintext_length,
1222 ciphertext, ciphertext_size, ciphertext_length ) );
1223
1224 /* Add cases for opaque driver here */
1225
1226 default:
1227 /* Key is declared with a lifetime not known to us */
1228 (void)status;
1229 return( PSA_ERROR_INVALID_ARGUMENT );
1230 }
1231}
1232
1233psa_status_t psa_driver_wrapper_aead_decrypt(
1234 const psa_key_attributes_t *attributes,
1235 const uint8_t *key_buffer, size_t key_buffer_size,
1236 psa_algorithm_t alg,
1237 const uint8_t *nonce, size_t nonce_length,
1238 const uint8_t *additional_data, size_t additional_data_length,
1239 const uint8_t *ciphertext, size_t ciphertext_length,
1240 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1241{
1242 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1243 psa_key_location_t location =
1244 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1245
1246 switch( location )
1247 {
1248 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1249 /* Key is stored in the slot in export representation, so
1250 * cycle through all known transparent accelerators */
1251
1252#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1253#if defined(PSA_CRYPTO_DRIVER_TEST)
1254 status = test_transparent_aead_decrypt(
1255 attributes, key_buffer, key_buffer_size,
1256 alg,
1257 nonce, nonce_length,
1258 additional_data, additional_data_length,
1259 ciphertext, ciphertext_length,
1260 plaintext, plaintext_size, plaintext_length );
1261 /* Declared with fallback == true */
1262 if( status != PSA_ERROR_NOT_SUPPORTED )
1263 return( status );
1264#endif /* PSA_CRYPTO_DRIVER_TEST */
1265#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1266
1267 /* Fell through, meaning no accelerator supports this operation */
1268 return( mbedtls_psa_aead_decrypt(
1269 attributes, key_buffer, key_buffer_size,
1270 alg,
1271 nonce, nonce_length,
1272 additional_data, additional_data_length,
1273 ciphertext, ciphertext_length,
1274 plaintext, plaintext_size, plaintext_length ) );
1275
1276 /* Add cases for opaque driver here */
1277
1278 default:
1279 /* Key is declared with a lifetime not known to us */
1280 (void)status;
1281 return( PSA_ERROR_INVALID_ARGUMENT );
1282 }
1283}
Steven Cooremancd84cb42020-07-16 20:28:36 +02001284/* End of automatically generated file. */