blob: 70c3026ce48510717e70e71195548df9855c20ca [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 Cooreman6801f082021-02-19 17:21:22 +0100577psa_status_t psa_driver_wrapper_get_builtin_key(
578 psa_drv_slot_number_t slot_number,
579 psa_key_attributes_t *attributes,
580 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
581{
582 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
583 switch( location )
584 {
585 default:
586 (void) slot_number;
587 (void) key_buffer;
588 (void) key_buffer_size;
589 (void) key_buffer_length;
590 return( PSA_ERROR_DOES_NOT_EXIST );
591 }
592}
593
Steven Cooreman37941cb2020-07-28 18:49:51 +0200594/*
595 * Cipher functions
596 */
597psa_status_t psa_driver_wrapper_cipher_encrypt(
598 psa_key_slot_t *slot,
599 psa_algorithm_t alg,
600 const uint8_t *input,
601 size_t input_length,
602 uint8_t *output,
603 size_t output_size,
604 size_t *output_length )
605{
606#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
607 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
608 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
609 psa_key_attributes_t attributes = {
610 .core = slot->attr
611 };
612
613 switch( location )
614 {
615 case PSA_KEY_LOCATION_LOCAL_STORAGE:
616 /* Key is stored in the slot in export representation, so
617 * cycle through all known transparent accelerators */
618#if defined(PSA_CRYPTO_DRIVER_TEST)
619 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100620 slot->key.data,
621 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200622 alg,
623 input,
624 input_length,
625 output,
626 output_size,
627 output_length );
628 /* Declared with fallback == true */
629 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200630 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200631#endif /* PSA_CRYPTO_DRIVER_TEST */
632 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200633 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200634 /* Add cases for opaque driver here */
635#if defined(PSA_CRYPTO_DRIVER_TEST)
636 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
637 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100638 slot->key.data,
639 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200640 alg,
641 input,
642 input_length,
643 output,
644 output_size,
645 output_length ) );
646#endif /* PSA_CRYPTO_DRIVER_TEST */
647 default:
648 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200649 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200650 }
651#else /* PSA_CRYPTO_DRIVER_PRESENT */
652 (void) slot;
653 (void) alg;
654 (void) input;
655 (void) input_length;
656 (void) output;
657 (void) output_size;
658 (void) output_length;
659
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200660 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200661#endif /* PSA_CRYPTO_DRIVER_PRESENT */
662}
663
664psa_status_t psa_driver_wrapper_cipher_decrypt(
665 psa_key_slot_t *slot,
666 psa_algorithm_t alg,
667 const uint8_t *input,
668 size_t input_length,
669 uint8_t *output,
670 size_t output_size,
671 size_t *output_length )
672{
673#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
674 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
675 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
676 psa_key_attributes_t attributes = {
677 .core = slot->attr
678 };
679
680 switch( location )
681 {
682 case PSA_KEY_LOCATION_LOCAL_STORAGE:
683 /* Key is stored in the slot in export representation, so
684 * cycle through all known transparent accelerators */
685#if defined(PSA_CRYPTO_DRIVER_TEST)
686 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100687 slot->key.data,
688 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200689 alg,
690 input,
691 input_length,
692 output,
693 output_size,
694 output_length );
695 /* Declared with fallback == true */
696 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200697 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200698#endif /* PSA_CRYPTO_DRIVER_TEST */
699 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200700 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200701 /* Add cases for opaque driver here */
702#if defined(PSA_CRYPTO_DRIVER_TEST)
703 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
704 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100705 slot->key.data,
706 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200707 alg,
708 input,
709 input_length,
710 output,
711 output_size,
712 output_length ) );
713#endif /* PSA_CRYPTO_DRIVER_TEST */
714 default:
715 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200716 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200717 }
718#else /* PSA_CRYPTO_DRIVER_PRESENT */
719 (void) slot;
720 (void) alg;
721 (void) input;
722 (void) input_length;
723 (void) output;
724 (void) output_size;
725 (void) output_length;
726
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200727 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200728#endif /* PSA_CRYPTO_DRIVER_PRESENT */
729}
730
731psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100732 psa_cipher_operation_t *operation,
733 const psa_key_attributes_t *attributes,
734 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200735 psa_algorithm_t alg )
736{
Ronald Cron0b805592020-12-14 18:08:20 +0100737 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +0100738 psa_key_location_t location =
739 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200740
Steven Cooreman37941cb2020-07-28 18:49:51 +0200741 switch( location )
742 {
743 case PSA_KEY_LOCATION_LOCAL_STORAGE:
744 /* Key is stored in the slot in export representation, so
745 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100746#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200747#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100748 status = test_transparent_cipher_encrypt_setup(
749 &operation->ctx.transparent_test_driver_ctx,
750 attributes,
751 key_buffer,
752 key_buffer_size,
753 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200754 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200755 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100756 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200757
Ronald Cron0b805592020-12-14 18:08:20 +0100758 if( status != PSA_ERROR_NOT_SUPPORTED )
759 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200760#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100761#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100762#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200763 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100764 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100765 attributes,
766 key_buffer,
767 key_buffer_size,
768 alg );
769 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +0100770 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +0100771
Ronald Cron7b4154d2021-03-19 14:49:41 +0100772 if( status != PSA_ERROR_NOT_SUPPORTED )
773 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100774#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
775 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +0100776
Steven Cooreman37941cb2020-07-28 18:49:51 +0200777 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100778#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200779#if defined(PSA_CRYPTO_DRIVER_TEST)
780 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100781 status = test_opaque_cipher_encrypt_setup(
782 &operation->ctx.opaque_test_driver_ctx,
783 attributes,
784 key_buffer, key_buffer_size,
785 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200786
Steven Cooreman37941cb2020-07-28 18:49:51 +0200787 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100788 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200789
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200790 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200791#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100792#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200793 default:
794 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100795 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100796 (void)key_buffer;
797 (void)key_buffer_size;
798 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200799 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200800 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200801}
802
803psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100804 psa_cipher_operation_t *operation,
805 const psa_key_attributes_t *attributes,
806 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200807 psa_algorithm_t alg )
808{
Steven Cooreman37941cb2020-07-28 18:49:51 +0200809 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +0100810 psa_key_location_t location =
811 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200812
Steven Cooreman37941cb2020-07-28 18:49:51 +0200813 switch( location )
814 {
815 case PSA_KEY_LOCATION_LOCAL_STORAGE:
816 /* Key is stored in the slot in export representation, so
817 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100818#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200819#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100820 status = test_transparent_cipher_decrypt_setup(
821 &operation->ctx.transparent_test_driver_ctx,
822 attributes,
823 key_buffer,
824 key_buffer_size,
825 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200826 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200827 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100828 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200829
Ronald Cron0b805592020-12-14 18:08:20 +0100830 if( status != PSA_ERROR_NOT_SUPPORTED )
831 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200832#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100833#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100834#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200835 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100836 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100837 attributes,
838 key_buffer,
839 key_buffer_size,
840 alg );
841 if( status == PSA_SUCCESS )
842 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
843
844 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100845#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
846 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +0100847
Steven Cooreman37941cb2020-07-28 18:49:51 +0200848 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100849#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200850#if defined(PSA_CRYPTO_DRIVER_TEST)
851 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100852 status = test_opaque_cipher_decrypt_setup(
853 &operation->ctx.opaque_test_driver_ctx,
854 attributes,
855 key_buffer, key_buffer_size,
856 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200857
Steven Cooreman37941cb2020-07-28 18:49:51 +0200858 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100859 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200860
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200861 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200862#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100863#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200864 default:
865 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100866 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100867 (void)key_buffer;
868 (void)key_buffer_size;
869 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200870 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200871 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200872}
873
Steven Cooreman37941cb2020-07-28 18:49:51 +0200874psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +0100875 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200876 const uint8_t *iv,
877 size_t iv_length )
878{
Ronald Cron49fafa92021-03-10 08:34:23 +0100879 switch( operation->id )
880 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100881#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100882 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100883 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100884 iv,
885 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100886#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100887
888#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200889#if defined(PSA_CRYPTO_DRIVER_TEST)
890 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100891 return( test_transparent_cipher_set_iv(
892 &operation->ctx.transparent_test_driver_ctx,
893 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +0100894
Steven Cooreman37941cb2020-07-28 18:49:51 +0200895 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100896 return( test_opaque_cipher_set_iv(
897 &operation->ctx.opaque_test_driver_ctx,
898 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200899#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100900#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100901 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200902
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100903 (void)iv;
904 (void)iv_length;
905
Ronald Crondd24c9b2020-12-15 14:10:01 +0100906 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200907}
908
909psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +0100910 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200911 const uint8_t *input,
912 size_t input_length,
913 uint8_t *output,
914 size_t output_size,
915 size_t *output_length )
916{
Ronald Cron49fafa92021-03-10 08:34:23 +0100917 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200918 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100919#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100920 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100921 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100922 input,
923 input_length,
924 output,
925 output_size,
926 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100927#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
928
Ronald Cron49fafa92021-03-10 08:34:23 +0100929#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200930#if defined(PSA_CRYPTO_DRIVER_TEST)
931 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100932 return( test_transparent_cipher_update(
933 &operation->ctx.transparent_test_driver_ctx,
934 input, input_length,
935 output, output_size, output_length ) );
936
Steven Cooreman37941cb2020-07-28 18:49:51 +0200937 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100938 return( test_opaque_cipher_update(
939 &operation->ctx.opaque_test_driver_ctx,
940 input, input_length,
941 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200942#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100943#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100944 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200945
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100946 (void)input;
947 (void)input_length;
948 (void)output;
949 (void)output_size;
950 (void)output_length;
951
Ronald Crondd24c9b2020-12-15 14:10:01 +0100952 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200953}
954
955psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +0100956 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200957 uint8_t *output,
958 size_t output_size,
959 size_t *output_length )
960{
Ronald Cron49fafa92021-03-10 08:34:23 +0100961 switch( operation->id )
962 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100963#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100964 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100965 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100966 output,
967 output_size,
968 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100969#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100970
971#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200972#if defined(PSA_CRYPTO_DRIVER_TEST)
973 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100974 return( test_transparent_cipher_finish(
975 &operation->ctx.transparent_test_driver_ctx,
976 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +0100977
Steven Cooreman37941cb2020-07-28 18:49:51 +0200978 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100979 return( test_opaque_cipher_finish(
980 &operation->ctx.opaque_test_driver_ctx,
981 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200982#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100983#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100984 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200985
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100986 (void)output;
987 (void)output_size;
988 (void)output_length;
989
Ronald Crondd24c9b2020-12-15 14:10:01 +0100990 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200991}
992
993psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +0100994 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200995{
Ronald Crondd24c9b2020-12-15 14:10:01 +0100996 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200997
Ronald Cron49fafa92021-03-10 08:34:23 +0100998 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200999 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001000#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001001 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001002 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001003#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001004
1005#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001006#if defined(PSA_CRYPTO_DRIVER_TEST)
1007 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001008 status = test_transparent_cipher_abort(
1009 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001010 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001011 &operation->ctx.transparent_test_driver_ctx,
1012 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001013 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001014
Steven Cooreman37941cb2020-07-28 18:49:51 +02001015 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001016 status = test_opaque_cipher_abort(
1017 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001018 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001019 &operation->ctx.opaque_test_driver_ctx,
1020 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001021 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001022#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001023#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001024 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001025
Ronald Cron49fafa92021-03-10 08:34:23 +01001026 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001027 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001028}
1029
Steven Cooreman1e582352021-02-18 17:24:37 +01001030/*
1031 * Hashing functions
1032 */
1033psa_status_t psa_driver_wrapper_hash_compute(
1034 psa_algorithm_t alg,
1035 const uint8_t *input,
1036 size_t input_length,
1037 uint8_t *hash,
1038 size_t hash_size,
1039 size_t *hash_length)
1040{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001041 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001042
1043 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001044#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001045 status = mbedtls_transparent_test_driver_hash_compute(
1046 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001047 if( status != PSA_ERROR_NOT_SUPPORTED )
1048 return( status );
1049#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001050
1051 /* If software fallback is compiled in, try fallback */
1052#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1053 status = mbedtls_psa_hash_compute( alg, input, input_length,
1054 hash, hash_size, hash_length );
1055 if( status != PSA_ERROR_NOT_SUPPORTED )
1056 return( status );
1057#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001058 (void) status;
1059 (void) alg;
1060 (void) input;
1061 (void) input_length;
1062 (void) hash;
1063 (void) hash_size;
1064 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001065
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001066 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001067}
1068
1069psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001070 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001071 psa_algorithm_t alg )
1072{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001073 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001074
Steven Cooreman1e582352021-02-18 17:24:37 +01001075 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001076#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001077 status = mbedtls_transparent_test_driver_hash_setup(
1078 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001079 if( status == PSA_SUCCESS )
1080 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1081
1082 if( status != PSA_ERROR_NOT_SUPPORTED )
1083 return( status );
1084#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001085
1086 /* If software fallback is compiled in, try fallback */
1087#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001088 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001089 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001090 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001091
1092 if( status != PSA_ERROR_NOT_SUPPORTED )
1093 return( status );
1094#endif
1095 /* Nothing left to try if we fall through here */
1096 (void) status;
1097 (void) operation;
1098 (void) alg;
1099 return( PSA_ERROR_NOT_SUPPORTED );
1100}
1101
1102psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001103 const psa_hash_operation_t *source_operation,
1104 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001105{
Steven Cooreman1e582352021-02-18 17:24:37 +01001106 switch( source_operation->id )
1107 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001108#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1109 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1110 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1111 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1112 &target_operation->ctx.mbedtls_ctx ) );
1113#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001114#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001115 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1116 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001117 return( mbedtls_transparent_test_driver_hash_clone(
1118 &source_operation->ctx.test_driver_ctx,
1119 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001120#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001121 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001122 (void) target_operation;
1123 return( PSA_ERROR_BAD_STATE );
1124 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001125}
1126
1127psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001128 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001129 const uint8_t *input,
1130 size_t input_length )
1131{
Steven Cooreman1e582352021-02-18 17:24:37 +01001132 switch( operation->id )
1133 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001134#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1135 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1136 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1137 input, input_length ) );
1138#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001139#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001140 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001141 return( mbedtls_transparent_test_driver_hash_update(
1142 &operation->ctx.test_driver_ctx,
1143 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001144#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001145 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001146 (void) input;
1147 (void) input_length;
1148 return( PSA_ERROR_BAD_STATE );
1149 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001150}
1151
1152psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001153 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001154 uint8_t *hash,
1155 size_t hash_size,
1156 size_t *hash_length )
1157{
Steven Cooreman1e582352021-02-18 17:24:37 +01001158 switch( operation->id )
1159 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001160#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1161 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1162 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1163 hash, hash_size, hash_length ) );
1164#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001165#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001166 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001167 return( mbedtls_transparent_test_driver_hash_finish(
1168 &operation->ctx.test_driver_ctx,
1169 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001170#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001171 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001172 (void) hash;
1173 (void) hash_size;
1174 (void) hash_length;
1175 return( PSA_ERROR_BAD_STATE );
1176 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001177}
1178
1179psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001180 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001181{
Steven Cooreman1e582352021-02-18 17:24:37 +01001182 switch( operation->id )
1183 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001184#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1185 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1186 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1187#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001188#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001189 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001190 return( mbedtls_transparent_test_driver_hash_abort(
1191 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001192#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001193 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001194 return( PSA_ERROR_BAD_STATE );
1195 }
1196}
1197
Ronald Cronde822812021-03-17 16:08:20 +01001198psa_status_t psa_driver_wrapper_aead_encrypt(
1199 const psa_key_attributes_t *attributes,
1200 const uint8_t *key_buffer, size_t key_buffer_size,
1201 psa_algorithm_t alg,
1202 const uint8_t *nonce, size_t nonce_length,
1203 const uint8_t *additional_data, size_t additional_data_length,
1204 const uint8_t *plaintext, size_t plaintext_length,
1205 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1206{
1207 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1208 psa_key_location_t location =
1209 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1210
1211 switch( location )
1212 {
1213 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1214 /* Key is stored in the slot in export representation, so
1215 * cycle through all known transparent accelerators */
1216
1217#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1218#if defined(PSA_CRYPTO_DRIVER_TEST)
1219 status = test_transparent_aead_encrypt(
1220 attributes, key_buffer, key_buffer_size,
1221 alg,
1222 nonce, nonce_length,
1223 additional_data, additional_data_length,
1224 plaintext, plaintext_length,
1225 ciphertext, ciphertext_size, ciphertext_length );
1226 /* Declared with fallback == true */
1227 if( status != PSA_ERROR_NOT_SUPPORTED )
1228 return( status );
1229#endif /* PSA_CRYPTO_DRIVER_TEST */
1230#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1231
1232 /* Fell through, meaning no accelerator supports this operation */
1233 return( mbedtls_psa_aead_encrypt(
1234 attributes, key_buffer, key_buffer_size,
1235 alg,
1236 nonce, nonce_length,
1237 additional_data, additional_data_length,
1238 plaintext, plaintext_length,
1239 ciphertext, ciphertext_size, ciphertext_length ) );
1240
1241 /* Add cases for opaque driver here */
1242
1243 default:
1244 /* Key is declared with a lifetime not known to us */
1245 (void)status;
1246 return( PSA_ERROR_INVALID_ARGUMENT );
1247 }
1248}
1249
1250psa_status_t psa_driver_wrapper_aead_decrypt(
1251 const psa_key_attributes_t *attributes,
1252 const uint8_t *key_buffer, size_t key_buffer_size,
1253 psa_algorithm_t alg,
1254 const uint8_t *nonce, size_t nonce_length,
1255 const uint8_t *additional_data, size_t additional_data_length,
1256 const uint8_t *ciphertext, size_t ciphertext_length,
1257 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1258{
1259 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1260 psa_key_location_t location =
1261 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1262
1263 switch( location )
1264 {
1265 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1266 /* Key is stored in the slot in export representation, so
1267 * cycle through all known transparent accelerators */
1268
1269#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1270#if defined(PSA_CRYPTO_DRIVER_TEST)
1271 status = test_transparent_aead_decrypt(
1272 attributes, key_buffer, key_buffer_size,
1273 alg,
1274 nonce, nonce_length,
1275 additional_data, additional_data_length,
1276 ciphertext, ciphertext_length,
1277 plaintext, plaintext_size, plaintext_length );
1278 /* Declared with fallback == true */
1279 if( status != PSA_ERROR_NOT_SUPPORTED )
1280 return( status );
1281#endif /* PSA_CRYPTO_DRIVER_TEST */
1282#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1283
1284 /* Fell through, meaning no accelerator supports this operation */
1285 return( mbedtls_psa_aead_decrypt(
1286 attributes, key_buffer, key_buffer_size,
1287 alg,
1288 nonce, nonce_length,
1289 additional_data, additional_data_length,
1290 ciphertext, ciphertext_length,
1291 plaintext, plaintext_size, plaintext_length ) );
1292
1293 /* Add cases for opaque driver here */
1294
1295 default:
1296 /* Key is declared with a lifetime not known to us */
1297 (void)status;
1298 return( PSA_ERROR_INVALID_ARGUMENT );
1299 }
1300}
Steven Cooremancd84cb42020-07-16 20:28:36 +02001301/* End of automatically generated file. */