blob: 7e42e48f984fd331e28a3dc0ddfe42ae3cd497ed [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
22#include "psa_crypto_core.h"
23#include "psa_crypto_driver_wrappers.h"
Steven Cooreman2a1664c2020-07-20 15:33:08 +020024#include "mbedtls/platform.h"
25
26#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020027
28/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020029#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020030#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020031#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020032#endif
33#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020034#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020035#endif
Steven Cooreman0d7c64d2020-09-07 16:17:55 +020036#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020037#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020038
Steven Cooreman56250fd2020-09-04 13:07:15 +020039/* Repeat above block for each JSON-declared driver during autogeneration */
40
Steven Cooremanfb81aa52020-09-09 12:01:43 +020041/* Auto-generated values depending on which drivers are registered. ID 0 is
42 * reserved for unallocated operations. */
Steven Cooreman37941cb2020-07-28 18:49:51 +020043#if defined(PSA_CRYPTO_DRIVER_TEST)
44#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (1)
45#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (2)
46#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020047#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
48
49/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020050#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020051/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
52 * SE driver is present, to avoid unused argument errors at compile time. */
53#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020054#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020055#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020056#include "psa_crypto_se.h"
57#endif
58
Steven Cooremancd84cb42020-07-16 20:28:36 +020059/* Start delegation functions */
Ronald Cron9f17aa42020-12-08 17:07:25 +010060psa_status_t psa_driver_wrapper_sign_hash(
61 const psa_key_attributes_t *attributes,
62 const uint8_t *key_buffer, size_t key_buffer_size,
63 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
64 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +020065{
Ronald Cron9f17aa42020-12-08 17:07:25 +010066 (void)key_buffer_size;
67
Steven Cooreman7a250572020-07-17 16:43:05 +020068 /* Try dynamically-registered SE interface first */
69#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
70 const psa_drv_se_t *drv;
71 psa_drv_se_context_t *drv_context;
72
Ronald Cron9f17aa42020-12-08 17:07:25 +010073 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +020074 {
75 if( drv->asymmetric == NULL ||
76 drv->asymmetric->p_sign == NULL )
77 {
78 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +020079 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +020080 }
Ronald Cron9f17aa42020-12-08 17:07:25 +010081 return( drv->asymmetric->p_sign(
82 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
83 alg, hash, hash_length,
84 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +020085 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +020086#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +020087
Ronald Cronfce9df22020-12-08 18:06:03 +010088 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +010089 psa_key_location_t location =
90 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +020091
92 switch( location )
93 {
94 case PSA_KEY_LOCATION_LOCAL_STORAGE:
95 /* Key is stored in the slot in export representation, so
96 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +010097#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +020098#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +010099 status = test_transparent_signature_sign_hash( attributes,
100 key_buffer,
101 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200102 alg,
103 hash,
104 hash_length,
105 signature,
106 signature_size,
107 signature_length );
108 /* Declared with fallback == true */
109 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200110 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200111#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100112#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200113 /* Fell through, meaning no accelerator supports this operation */
Ronald Cronfce9df22020-12-08 18:06:03 +0100114 return( psa_sign_hash_internal( attributes,
115 key_buffer,
116 key_buffer_size,
117 alg,
118 hash,
119 hash_length,
120 signature,
121 signature_size,
122 signature_length ) );
123
Steven Cooremancd84cb42020-07-16 20:28:36 +0200124 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100125#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200126#if defined(PSA_CRYPTO_DRIVER_TEST)
127 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100128 return( test_opaque_signature_sign_hash( attributes,
129 key_buffer,
130 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200131 alg,
132 hash,
133 hash_length,
134 signature,
135 signature_size,
136 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200137#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100138#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200139 default:
140 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100141 (void)status;
142 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200143 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200144}
145
Ronald Cron9f17aa42020-12-08 17:07:25 +0100146psa_status_t psa_driver_wrapper_verify_hash(
147 const psa_key_attributes_t *attributes,
148 const uint8_t *key_buffer, size_t key_buffer_size,
149 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
150 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200151{
Ronald Cron9f17aa42020-12-08 17:07:25 +0100152 (void)key_buffer_size;
153
Steven Cooreman55ae2172020-07-17 19:46:15 +0200154 /* Try dynamically-registered SE interface first */
155#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
156 const psa_drv_se_t *drv;
157 psa_drv_se_context_t *drv_context;
158
Ronald Cron9f17aa42020-12-08 17:07:25 +0100159 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200160 {
161 if( drv->asymmetric == NULL ||
162 drv->asymmetric->p_verify == NULL )
163 {
164 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200165 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200166 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100167 return( drv->asymmetric->p_verify(
168 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
169 alg, hash, hash_length,
170 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200171 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200172#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200173
Ronald Cronfce9df22020-12-08 18:06:03 +0100174 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100175 psa_key_location_t location =
176 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200177
178 switch( location )
179 {
180 case PSA_KEY_LOCATION_LOCAL_STORAGE:
181 /* Key is stored in the slot in export representation, so
182 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100183#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200184#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100185 status = test_transparent_signature_verify_hash( attributes,
186 key_buffer,
187 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200188 alg,
189 hash,
190 hash_length,
191 signature,
192 signature_length );
193 /* Declared with fallback == true */
194 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200195 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200196#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100197#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
198
199 return( psa_verify_hash_internal( attributes,
200 key_buffer,
201 key_buffer_size,
202 alg,
203 hash,
204 hash_length,
205 signature,
206 signature_length ) );
207
Steven Cooreman55ae2172020-07-17 19:46:15 +0200208 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100209#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200210#if defined(PSA_CRYPTO_DRIVER_TEST)
211 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100212 return( test_opaque_signature_verify_hash( attributes,
213 key_buffer,
214 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200215 alg,
216 hash,
217 hash_length,
218 signature,
219 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200220#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100221#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200222 default:
223 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100224 (void)status;
225 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200226 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200227}
228
Ronald Cron31216282020-12-05 18:47:56 +0100229/** Get the key buffer size for the key material of a generated key in the
230 * case of an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200231 *
Ronald Cron31216282020-12-05 18:47:56 +0100232 * \param[in] attributes The key attributes.
233 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200234 *
235 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100236 * The minimum size for a buffer to contain the key material has been
237 * returned successfully.
238 * \retval #PSA_ERROR_INVALID_ARGUMENT
239 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200240 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100241 * The type and/or the size in bits of the key or the combination of
242 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200243 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100244psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100245 const psa_key_attributes_t *attributes,
246 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200247{
John Durkop2c618352020-09-22 06:54:01 -0700248 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
249 psa_key_type_t key_type = attributes->core.type;
250 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200251
Ronald Cron31216282020-12-05 18:47:56 +0100252 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700253 switch( location )
254 {
John Durkop2c618352020-09-22 06:54:01 -0700255#if defined(PSA_CRYPTO_DRIVER_TEST)
256 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
John Durkopac93e3b2020-10-16 06:48:55 -0700257#ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
Ronald Cron31216282020-12-05 18:47:56 +0100258 *key_buffer_size = test_size_function( key_type, key_bits );
John Durkop2c618352020-09-22 06:54:01 -0700259 return( PSA_SUCCESS );
260#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
261 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
262 {
Ronald Cron31216282020-12-05 18:47:56 +0100263 int public_key_overhead =
264 ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ?
265 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
266 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop135ce692020-10-19 07:12:28 -0700267 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
268 + public_key_overhead;
269 }
Ronald Cron31216282020-12-05 18:47:56 +0100270 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
John Durkop135ce692020-10-19 07:12:28 -0700271 {
Ronald Cron31216282020-12-05 18:47:56 +0100272 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700273 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
274 }
John Durkop135ce692020-10-19 07:12:28 -0700275 else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
Ronald Cron31216282020-12-05 18:47:56 +0100276 !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
John Durkop2c618352020-09-22 06:54:01 -0700277 {
Ronald Cron31216282020-12-05 18:47:56 +0100278 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700279 + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
280 * ( ( key_bits + 7 ) / 8 );
281 }
282 else
283 {
284 return( PSA_ERROR_NOT_SUPPORTED );
285 }
286 return( PSA_SUCCESS );
287#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
288#endif /* PSA_CRYPTO_DRIVER_TEST */
289
290 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100291 (void)key_type;
292 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200293 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200294 }
295}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200296
Ronald Cron977c2472020-10-13 08:32:21 +0200297psa_status_t psa_driver_wrapper_generate_key(
298 const psa_key_attributes_t *attributes,
299 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200300{
Ronald Cron977c2472020-10-13 08:32:21 +0200301 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
302 psa_key_location_t location =
303 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
304
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200305 /* Try dynamically-registered SE interface first */
306#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
307 const psa_drv_se_t *drv;
308 psa_drv_se_context_t *drv_context;
309
Ronald Cron977c2472020-10-13 08:32:21 +0200310 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200311 {
312 size_t pubkey_length = 0; /* We don't support this feature yet */
313 if( drv->key_management == NULL ||
314 drv->key_management->p_generate == NULL )
315 {
316 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200317 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200318 }
319 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200320 drv_context,
321 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100322 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200323 }
324#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
325
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200326 switch( location )
327 {
328 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200329#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200330 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200331 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200332 {
Ronald Cron977c2472020-10-13 08:32:21 +0200333 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200334#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron977c2472020-10-13 08:32:21 +0200335 status = test_transparent_generate_key(
336 attributes, key_buffer, key_buffer_size,
337 key_buffer_length );
338 /* Declared with fallback == true */
339 if( status != PSA_ERROR_NOT_SUPPORTED )
340 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200341#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200342 }
343#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
344
345 /* Software fallback */
346 status = psa_generate_key_internal(
347 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200348 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200349
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200350 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200351#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200352#if defined(PSA_CRYPTO_DRIVER_TEST)
353 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron977c2472020-10-13 08:32:21 +0200354 status = test_opaque_generate_key(
355 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200356 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200357#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200358#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
359
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200360 default:
361 /* Key is declared with a lifetime not known to us */
362 status = PSA_ERROR_INVALID_ARGUMENT;
363 break;
364 }
365
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200366 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200367}
368
Ronald Cron83282872020-11-22 14:02:39 +0100369psa_status_t psa_driver_wrapper_import_key(
370 const psa_key_attributes_t *attributes,
371 const uint8_t *data,
372 size_t data_length,
373 uint8_t *key_buffer,
374 size_t key_buffer_size,
375 size_t *key_buffer_length,
376 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200377{
Steven Cooreman04524762020-10-13 17:43:44 +0200378 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100379 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
380 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200381
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100382 /* Try dynamically-registered SE interface first */
383#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
384 const psa_drv_se_t *drv;
385 psa_drv_se_context_t *drv_context;
386
387 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
388 {
389 if( drv->key_management == NULL ||
390 drv->key_management->p_import == NULL )
391 return( PSA_ERROR_NOT_SUPPORTED );
392
393 /* The driver should set the number of key bits, however in
394 * case it doesn't, we initialize bits to an invalid value. */
395 *bits = PSA_MAX_KEY_BITS + 1;
396 status = drv->key_management->p_import(
397 drv_context,
398 *( (psa_key_slot_number_t *)key_buffer ),
399 attributes, data, data_length, bits );
400
401 if( status != PSA_SUCCESS )
402 return( status );
403
404 if( (*bits) > PSA_MAX_KEY_BITS )
405 return( PSA_ERROR_NOT_SUPPORTED );
406
407 return( PSA_SUCCESS );
408 }
409#endif /* PSA_CRYPTO_SE_C */
410
Ronald Cronbf33c932020-11-28 18:06:53 +0100411 switch( location )
412 {
413 case PSA_KEY_LOCATION_LOCAL_STORAGE:
414 /* Key is stored in the slot in export representation, so
415 * cycle through all known transparent accelerators */
416#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
417#if defined(PSA_CRYPTO_DRIVER_TEST)
418 status = test_transparent_import_key( attributes,
419 data, data_length,
420 key_buffer, key_buffer_size,
421 key_buffer_length, bits );
422 /* Declared with fallback == true */
423 if( status != PSA_ERROR_NOT_SUPPORTED )
424 return( status );
425#endif /* PSA_CRYPTO_DRIVER_TEST */
426#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
427 /* Fell through, meaning no accelerator supports this operation */
428 return( psa_import_key_into_slot( attributes,
429 data, data_length,
430 key_buffer, key_buffer_size,
431 key_buffer_length, bits ) );
432
433 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100434 /* Importing a key with external storage in not yet supported.
435 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100436 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100437 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100438 }
439
Steven Cooreman04524762020-10-13 17:43:44 +0200440}
441
Ronald Cron67227982020-11-26 15:16:05 +0100442psa_status_t psa_driver_wrapper_export_key(
443 const psa_key_attributes_t *attributes,
444 const uint8_t *key_buffer, size_t key_buffer_size,
445 uint8_t *data, size_t data_size, size_t *data_length )
446
447{
448 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
449 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
450 psa_get_key_lifetime( attributes ) );
451
Ronald Cron152e3f82020-11-26 16:06:41 +0100452 /* Try dynamically-registered SE interface first */
453#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
454 const psa_drv_se_t *drv;
455 psa_drv_se_context_t *drv_context;
456
457 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
458 {
459 if( ( drv->key_management == NULL ) ||
460 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100461 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100462 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100463 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100464
465 return( drv->key_management->p_export(
466 drv_context,
467 *( (psa_key_slot_number_t *)key_buffer ),
468 data, data_size, data_length ) );
469 }
470#endif /* PSA_CRYPTO_SE_C */
471
Ronald Cron67227982020-11-26 15:16:05 +0100472 switch( location )
473 {
474 case PSA_KEY_LOCATION_LOCAL_STORAGE:
475 return( psa_export_key_internal( attributes,
476 key_buffer,
477 key_buffer_size,
478 data,
479 data_size,
480 data_length ) );
481
482 /* Add cases for opaque driver here */
483#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
484#if defined(PSA_CRYPTO_DRIVER_TEST)
485 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
486 return( test_opaque_export_key( attributes,
487 key_buffer,
488 key_buffer_size,
489 data,
490 data_size,
491 data_length ) );
492#endif /* PSA_CRYPTO_DRIVER_TEST */
493#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
494 default:
495 /* Key is declared with a lifetime not known to us */
496 return( status );
497 }
498}
499
Ronald Cron84cc9942020-11-25 14:30:05 +0100500psa_status_t psa_driver_wrapper_export_public_key(
501 const psa_key_attributes_t *attributes,
502 const uint8_t *key_buffer, size_t key_buffer_size,
503 uint8_t *data, size_t data_size, size_t *data_length )
504
Steven Cooremanb9b84422020-10-14 14:39:20 +0200505{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200506 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100507 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
508 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200509
Ronald Cron152e3f82020-11-26 16:06:41 +0100510 /* Try dynamically-registered SE interface first */
511#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
512 const psa_drv_se_t *drv;
513 psa_drv_se_context_t *drv_context;
514
515 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
516 {
517 if( ( drv->key_management == NULL ) ||
518 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100519 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100520 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100521 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100522
523 return( drv->key_management->p_export_public(
524 drv_context,
525 *( (psa_key_slot_number_t *)key_buffer ),
526 data, data_size, data_length ) );
527 }
528#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
529
Steven Cooremanb9b84422020-10-14 14:39:20 +0200530 switch( location )
531 {
532 case PSA_KEY_LOCATION_LOCAL_STORAGE:
533 /* Key is stored in the slot in export representation, so
534 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100535#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200536#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron84cc9942020-11-25 14:30:05 +0100537 status = test_transparent_export_public_key( attributes,
538 key_buffer,
539 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200540 data,
541 data_size,
542 data_length );
543 /* Declared with fallback == true */
544 if( status != PSA_ERROR_NOT_SUPPORTED )
545 return( status );
546#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100547#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200548 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100549 return( psa_export_public_key_internal( attributes,
550 key_buffer,
551 key_buffer_size,
552 data,
553 data_size,
554 data_length ) );
555
Steven Cooremanb9b84422020-10-14 14:39:20 +0200556 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100557#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200558#if defined(PSA_CRYPTO_DRIVER_TEST)
559 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron84cc9942020-11-25 14:30:05 +0100560 return( test_opaque_export_public_key( attributes,
561 key_buffer,
562 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200563 data,
564 data_size,
565 data_length ) );
566#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100567#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200568 default:
569 /* Key is declared with a lifetime not known to us */
570 return( status );
571 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200572}
573
Steven Cooreman37941cb2020-07-28 18:49:51 +0200574/*
575 * Cipher functions
576 */
577psa_status_t psa_driver_wrapper_cipher_encrypt(
578 psa_key_slot_t *slot,
579 psa_algorithm_t alg,
580 const uint8_t *input,
581 size_t input_length,
582 uint8_t *output,
583 size_t output_size,
584 size_t *output_length )
585{
586#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
587 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
588 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
589 psa_key_attributes_t attributes = {
590 .core = slot->attr
591 };
592
593 switch( location )
594 {
595 case PSA_KEY_LOCATION_LOCAL_STORAGE:
596 /* Key is stored in the slot in export representation, so
597 * cycle through all known transparent accelerators */
598#if defined(PSA_CRYPTO_DRIVER_TEST)
599 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100600 slot->key.data,
601 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200602 alg,
603 input,
604 input_length,
605 output,
606 output_size,
607 output_length );
608 /* Declared with fallback == true */
609 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200610 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200611#endif /* PSA_CRYPTO_DRIVER_TEST */
612 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200613 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200614 /* Add cases for opaque driver here */
615#if defined(PSA_CRYPTO_DRIVER_TEST)
616 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
617 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100618 slot->key.data,
619 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200620 alg,
621 input,
622 input_length,
623 output,
624 output_size,
625 output_length ) );
626#endif /* PSA_CRYPTO_DRIVER_TEST */
627 default:
628 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200629 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200630 }
631#else /* PSA_CRYPTO_DRIVER_PRESENT */
632 (void) slot;
633 (void) alg;
634 (void) input;
635 (void) input_length;
636 (void) output;
637 (void) output_size;
638 (void) output_length;
639
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200640 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200641#endif /* PSA_CRYPTO_DRIVER_PRESENT */
642}
643
644psa_status_t psa_driver_wrapper_cipher_decrypt(
645 psa_key_slot_t *slot,
646 psa_algorithm_t alg,
647 const uint8_t *input,
648 size_t input_length,
649 uint8_t *output,
650 size_t output_size,
651 size_t *output_length )
652{
653#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
654 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
655 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
656 psa_key_attributes_t attributes = {
657 .core = slot->attr
658 };
659
660 switch( location )
661 {
662 case PSA_KEY_LOCATION_LOCAL_STORAGE:
663 /* Key is stored in the slot in export representation, so
664 * cycle through all known transparent accelerators */
665#if defined(PSA_CRYPTO_DRIVER_TEST)
666 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100667 slot->key.data,
668 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200669 alg,
670 input,
671 input_length,
672 output,
673 output_size,
674 output_length );
675 /* Declared with fallback == true */
676 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200677 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200678#endif /* PSA_CRYPTO_DRIVER_TEST */
679 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200680 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200681 /* Add cases for opaque driver here */
682#if defined(PSA_CRYPTO_DRIVER_TEST)
683 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
684 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100685 slot->key.data,
686 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200687 alg,
688 input,
689 input_length,
690 output,
691 output_size,
692 output_length ) );
693#endif /* PSA_CRYPTO_DRIVER_TEST */
694 default:
695 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200696 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200697 }
698#else /* PSA_CRYPTO_DRIVER_PRESENT */
699 (void) slot;
700 (void) alg;
701 (void) input;
702 (void) input_length;
703 (void) output;
704 (void) output_size;
705 (void) output_length;
706
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200707 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200708#endif /* PSA_CRYPTO_DRIVER_PRESENT */
709}
710
711psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200712 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200713 psa_key_slot_t *slot,
714 psa_algorithm_t alg )
715{
716#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
717 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
718 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
719 psa_key_attributes_t attributes = {
720 .core = slot->attr
721 };
722
Steven Cooreman37941cb2020-07-28 18:49:51 +0200723 switch( location )
724 {
725 case PSA_KEY_LOCATION_LOCAL_STORAGE:
726 /* Key is stored in the slot in export representation, so
727 * cycle through all known transparent accelerators */
728#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200729 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
730 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200731 return PSA_ERROR_INSUFFICIENT_MEMORY;
732
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200733 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200734 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100735 slot->key.data,
736 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200737 alg );
738 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200739 if( status == PSA_SUCCESS )
740 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200741 else
742 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200743 mbedtls_platform_zeroize(
744 operation->ctx,
745 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200746 mbedtls_free( operation->ctx );
747 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200748 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200749
750 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200751#endif /* PSA_CRYPTO_DRIVER_TEST */
752 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200753 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200754 /* Add cases for opaque driver here */
755#if defined(PSA_CRYPTO_DRIVER_TEST)
756 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200757 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
758 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200759 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200760
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200761 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200762 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100763 slot->key.data,
764 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200765 alg );
766 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200767 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200768 else
769 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200770 mbedtls_platform_zeroize(
771 operation->ctx,
772 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200773 mbedtls_free( operation->ctx );
774 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200775 }
776
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200777 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200778#endif /* PSA_CRYPTO_DRIVER_TEST */
779 default:
780 /* Key is declared with a lifetime not known to us */
John Durkop714e3a12020-09-29 22:07:04 -0700781 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200782 }
783#else /* PSA_CRYPTO_DRIVER_PRESENT */
784 (void)slot;
785 (void)alg;
786 (void)operation;
787
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200788 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200789#endif /* PSA_CRYPTO_DRIVER_PRESENT */
790}
791
792psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200793 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200794 psa_key_slot_t *slot,
795 psa_algorithm_t alg )
796{
797#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
798 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
799 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
800 psa_key_attributes_t attributes = {
801 .core = slot->attr
802 };
803
Steven Cooreman37941cb2020-07-28 18:49:51 +0200804 switch( location )
805 {
806 case PSA_KEY_LOCATION_LOCAL_STORAGE:
807 /* Key is stored in the slot in export representation, so
808 * cycle through all known transparent accelerators */
809#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200810 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
811 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200812 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200813
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200814 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200815 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100816 slot->key.data,
817 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200818 alg );
819 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200820 if( status == PSA_SUCCESS )
821 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200822 else
823 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200824 mbedtls_platform_zeroize(
825 operation->ctx,
826 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200827 mbedtls_free( operation->ctx );
828 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200829 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200830
831 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200832#endif /* PSA_CRYPTO_DRIVER_TEST */
833 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200834 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200835 /* Add cases for opaque driver here */
836#if defined(PSA_CRYPTO_DRIVER_TEST)
837 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200838 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
839 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200840 return PSA_ERROR_INSUFFICIENT_MEMORY;
841
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200842 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200843 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100844 slot->key.data,
845 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200846 alg );
847 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200848 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200849 else
850 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200851 mbedtls_platform_zeroize(
852 operation->ctx,
853 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200854 mbedtls_free( operation->ctx );
855 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200856 }
857
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200858 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200859#endif /* PSA_CRYPTO_DRIVER_TEST */
860 default:
861 /* Key is declared with a lifetime not known to us */
John Durkop814dca72020-10-05 06:31:12 -0700862 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200863 }
864#else /* PSA_CRYPTO_DRIVER_PRESENT */
865 (void)slot;
866 (void)alg;
867 (void)operation;
868
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200869 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200870#endif /* PSA_CRYPTO_DRIVER_PRESENT */
871}
872
873psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200874 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200875 uint8_t *iv,
876 size_t iv_size,
877 size_t *iv_length )
878{
879#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200880 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200881 {
882#if defined(PSA_CRYPTO_DRIVER_TEST)
883 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200884 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200885 iv,
886 iv_size,
887 iv_length ) );
888#endif /* PSA_CRYPTO_DRIVER_TEST */
889#if defined(PSA_CRYPTO_DRIVER_TEST)
890 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200891 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200892 iv,
893 iv_size,
894 iv_length ) );
895#endif /* PSA_CRYPTO_DRIVER_TEST */
896 default:
897 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200898 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200899 }
900#else /* PSA_CRYPTO_DRIVER_PRESENT */
901 (void) operation;
902 (void) iv;
903 (void) iv_size;
904 (void) iv_length;
905
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200906 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200907#endif /* PSA_CRYPTO_DRIVER_PRESENT */
908}
909
910psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200911 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200912 const uint8_t *iv,
913 size_t iv_length )
914{
915#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200916 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200917 {
918#if defined(PSA_CRYPTO_DRIVER_TEST)
919 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200920 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200921 iv,
922 iv_length ) );
923#endif /* PSA_CRYPTO_DRIVER_TEST */
924#if defined(PSA_CRYPTO_DRIVER_TEST)
925 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200926 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200927 iv,
928 iv_length ) );
929#endif /* PSA_CRYPTO_DRIVER_TEST */
930 default:
931 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200932 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200933 }
934#else /* PSA_CRYPTO_DRIVER_PRESENT */
935 (void) operation;
936 (void) iv;
937 (void) iv_length;
938
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200939 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200940#endif /* PSA_CRYPTO_DRIVER_PRESENT */
941}
942
943psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200944 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200945 const uint8_t *input,
946 size_t input_length,
947 uint8_t *output,
948 size_t output_size,
949 size_t *output_length )
950{
951#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200952 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200953 {
954#if defined(PSA_CRYPTO_DRIVER_TEST)
955 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200956 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200957 input,
958 input_length,
959 output,
960 output_size,
961 output_length ) );
962#endif /* PSA_CRYPTO_DRIVER_TEST */
963#if defined(PSA_CRYPTO_DRIVER_TEST)
964 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200965 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200966 input,
967 input_length,
968 output,
969 output_size,
970 output_length ) );
971#endif /* PSA_CRYPTO_DRIVER_TEST */
972 default:
973 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200974 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200975 }
976#else /* PSA_CRYPTO_DRIVER_PRESENT */
977 (void) operation;
978 (void) input;
979 (void) input_length;
980 (void) output;
981 (void) output_length;
982 (void) output_size;
983
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200984 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200985#endif /* PSA_CRYPTO_DRIVER_PRESENT */
986}
987
988psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200989 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200990 uint8_t *output,
991 size_t output_size,
992 size_t *output_length )
993{
994#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200995 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200996 {
997#if defined(PSA_CRYPTO_DRIVER_TEST)
998 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200999 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001000 output,
1001 output_size,
1002 output_length ) );
1003#endif /* PSA_CRYPTO_DRIVER_TEST */
1004#if defined(PSA_CRYPTO_DRIVER_TEST)
1005 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001006 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001007 output,
1008 output_size,
1009 output_length ) );
1010#endif /* PSA_CRYPTO_DRIVER_TEST */
1011 default:
1012 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001013 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001014 }
1015#else /* PSA_CRYPTO_DRIVER_PRESENT */
1016 (void) operation;
1017 (void) output;
1018 (void) output_size;
1019 (void) output_length;
1020
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001021 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001022#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1023}
1024
1025psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001026 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001027{
1028#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1029 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001030
1031 /* The object has (apparently) been initialized but it is not in use. It's
1032 * ok to call abort on such an object, and there's nothing to do. */
1033 if( operation->ctx == NULL && operation->id == 0 )
1034 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001035
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001036 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001037 {
1038#if defined(PSA_CRYPTO_DRIVER_TEST)
1039 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001040 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001041 mbedtls_platform_zeroize(
1042 operation->ctx,
1043 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001044 mbedtls_free( operation->ctx );
1045 operation->ctx = NULL;
1046 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001047
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001048 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001049#endif /* PSA_CRYPTO_DRIVER_TEST */
1050#if defined(PSA_CRYPTO_DRIVER_TEST)
1051 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001052 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001053 mbedtls_platform_zeroize(
1054 operation->ctx,
1055 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001056 mbedtls_free( operation->ctx );
1057 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001058 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001059
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001060 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001061#endif /* PSA_CRYPTO_DRIVER_TEST */
1062 default:
1063 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001064 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001065 }
1066#else /* PSA_CRYPTO_DRIVER_PRESENT */
1067 (void)operation;
1068
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001069 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001070#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1071}
1072
Steven Cooremancd84cb42020-07-16 20:28:36 +02001073/* End of automatically generated file. */