blob: e0c86cf75118f86fe197712925e3707a8e5bce24 [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 */
60psa_status_t psa_driver_wrapper_sign_hash( psa_key_slot_t *slot,
61 psa_algorithm_t alg,
62 const uint8_t *hash,
63 size_t hash_length,
64 uint8_t *signature,
65 size_t signature_size,
66 size_t *signature_length )
67{
Steven Cooremanf1720ea2020-07-24 18:41:58 +020068#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman7a250572020-07-17 16:43:05 +020069 /* Try dynamically-registered SE interface first */
70#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
71 const psa_drv_se_t *drv;
72 psa_drv_se_context_t *drv_context;
73
74 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
75 {
76 if( drv->asymmetric == NULL ||
77 drv->asymmetric->p_sign == NULL )
78 {
79 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +020080 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +020081 }
82 return( drv->asymmetric->p_sign( drv_context,
Ronald Cronea0f8a62020-11-25 17:52:23 +010083 psa_key_slot_get_slot_number( slot ),
Steven Cooreman7a250572020-07-17 16:43:05 +020084 alg,
85 hash, hash_length,
86 signature, signature_size,
87 signature_length ) );
88 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +020089#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +020090
91 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020092#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremancd84cb42020-07-16 20:28:36 +020093 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
94 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
95 psa_key_attributes_t attributes = {
96 .core = slot->attr
97 };
98
99 switch( location )
100 {
101 case PSA_KEY_LOCATION_LOCAL_STORAGE:
102 /* Key is stored in the slot in export representation, so
103 * cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200104#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremancd84cb42020-07-16 20:28:36 +0200105 status = test_transparent_signature_sign_hash( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100106 slot->key.data,
107 slot->key.bytes,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200108 alg,
109 hash,
110 hash_length,
111 signature,
112 signature_size,
113 signature_length );
114 /* Declared with fallback == true */
115 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200116 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200117#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200118 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200119 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200120 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200121#if defined(PSA_CRYPTO_DRIVER_TEST)
122 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremancd84cb42020-07-16 20:28:36 +0200123 return( test_opaque_signature_sign_hash( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100124 slot->key.data,
125 slot->key.bytes,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200126 alg,
127 hash,
128 hash_length,
129 signature,
130 signature_size,
131 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200132#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200133 default:
134 /* Key is declared with a lifetime not known to us */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200135 return( status );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200136 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200137#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200138 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200139#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
140#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200141 (void)slot;
142 (void)alg;
143 (void)hash;
144 (void)hash_length;
145 (void)signature;
146 (void)signature_size;
147 (void)signature_length;
148
Steven Cooreman56250fd2020-09-04 13:07:15 +0200149 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200150#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200151}
152
Steven Cooreman55ae2172020-07-17 19:46:15 +0200153psa_status_t psa_driver_wrapper_verify_hash( psa_key_slot_t *slot,
154 psa_algorithm_t alg,
155 const uint8_t *hash,
156 size_t hash_length,
157 const uint8_t *signature,
158 size_t signature_length )
159{
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200160#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200161 /* Try dynamically-registered SE interface first */
162#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
163 const psa_drv_se_t *drv;
164 psa_drv_se_context_t *drv_context;
165
166 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
167 {
168 if( drv->asymmetric == NULL ||
169 drv->asymmetric->p_verify == NULL )
170 {
171 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200172 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200173 }
174 return( drv->asymmetric->p_verify( drv_context,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100175 psa_key_slot_get_slot_number( slot ),
Steven Cooreman55ae2172020-07-17 19:46:15 +0200176 alg,
177 hash, hash_length,
178 signature, signature_length ) );
179 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200180#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200181
182 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200183#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200184 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
185 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
186 psa_key_attributes_t attributes = {
187 .core = slot->attr
188 };
189
190 switch( location )
191 {
192 case PSA_KEY_LOCATION_LOCAL_STORAGE:
193 /* Key is stored in the slot in export representation, so
194 * cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200195#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200196 status = test_transparent_signature_verify_hash( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100197 slot->key.data,
198 slot->key.bytes,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200199 alg,
200 hash,
201 hash_length,
202 signature,
203 signature_length );
204 /* Declared with fallback == true */
205 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200206 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200207#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200208 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200209 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200210 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200211#if defined(PSA_CRYPTO_DRIVER_TEST)
212 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooreman55ae2172020-07-17 19:46:15 +0200213 return( test_opaque_signature_verify_hash( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100214 slot->key.data,
215 slot->key.bytes,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200216 alg,
217 hash,
218 hash_length,
219 signature,
220 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200221#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200222 default:
223 /* Key is declared with a lifetime not known to us */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200224 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200225 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200226#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200227 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200228#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
229#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200230 (void)slot;
231 (void)alg;
232 (void)hash;
233 (void)hash_length;
234 (void)signature;
235 (void)signature_length;
236
Steven Cooreman56250fd2020-09-04 13:07:15 +0200237 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200238#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200239}
240
Ronald Cron31216282020-12-05 18:47:56 +0100241/** Get the key buffer size for the key material of a generated key in the
242 * case of an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200243 *
Ronald Cron31216282020-12-05 18:47:56 +0100244 * \param[in] attributes The key attributes.
245 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200246 *
247 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100248 * The minimum size for a buffer to contain the key material has been
249 * returned successfully.
250 * \retval #PSA_ERROR_INVALID_ARGUMENT
251 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200252 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100253 * The type and/or the size in bits of the key or the combination of
254 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200255 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100256psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100257 const psa_key_attributes_t *attributes,
258 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200259{
John Durkop2c618352020-09-22 06:54:01 -0700260 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
261 psa_key_type_t key_type = attributes->core.type;
262 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200263
Ronald Cron31216282020-12-05 18:47:56 +0100264 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700265 switch( location )
266 {
John Durkop2c618352020-09-22 06:54:01 -0700267#if defined(PSA_CRYPTO_DRIVER_TEST)
268 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
John Durkopac93e3b2020-10-16 06:48:55 -0700269#ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
Ronald Cron31216282020-12-05 18:47:56 +0100270 *key_buffer_size = test_size_function( key_type, key_bits );
John Durkop2c618352020-09-22 06:54:01 -0700271 return( PSA_SUCCESS );
272#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
273 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
274 {
Ronald Cron31216282020-12-05 18:47:56 +0100275 int public_key_overhead =
276 ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ?
277 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
278 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop135ce692020-10-19 07:12:28 -0700279 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
280 + public_key_overhead;
281 }
Ronald Cron31216282020-12-05 18:47:56 +0100282 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
John Durkop135ce692020-10-19 07:12:28 -0700283 {
Ronald Cron31216282020-12-05 18:47:56 +0100284 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700285 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
286 }
John Durkop135ce692020-10-19 07:12:28 -0700287 else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
Ronald Cron31216282020-12-05 18:47:56 +0100288 !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
John Durkop2c618352020-09-22 06:54:01 -0700289 {
Ronald Cron31216282020-12-05 18:47:56 +0100290 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700291 + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
292 * ( ( key_bits + 7 ) / 8 );
293 }
294 else
295 {
296 return( PSA_ERROR_NOT_SUPPORTED );
297 }
298 return( PSA_SUCCESS );
299#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
300#endif /* PSA_CRYPTO_DRIVER_TEST */
301
302 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100303 (void)key_type;
304 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200305 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200306 }
307}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200308
Steven Cooreman55ae2172020-07-17 19:46:15 +0200309psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes,
310 psa_key_slot_t *slot )
311{
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200312#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200313 /* Try dynamically-registered SE interface first */
314#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
315 const psa_drv_se_t *drv;
316 psa_drv_se_context_t *drv_context;
317
318 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
319 {
320 size_t pubkey_length = 0; /* We don't support this feature yet */
321 if( drv->key_management == NULL ||
322 drv->key_management->p_generate == NULL )
323 {
324 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200325 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200326 }
327 return( drv->key_management->p_generate(
Ronald Cronea0f8a62020-11-25 17:52:23 +0100328 drv_context, psa_key_slot_get_slot_number( slot ),
329 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200330 }
331#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
332
333 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200334#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200335 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
336 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
337 size_t export_size = 0;
338
Ronald Cron9cca3162020-12-05 19:07:47 +0100339 /* If this is not to generate a key in a secure element or cryptoprocessor
340 * with storage. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100341 if( slot->key.data == NULL )
Ronald Cron9cca3162020-12-05 19:07:47 +0100342 {
Ronald Cron31216282020-12-05 18:47:56 +0100343 if( location == PSA_KEY_LOCATION_LOCAL_STORAGE )
344 {
345 export_size = PSA_KEY_EXPORT_MAX_SIZE( attributes->core.type,
346 attributes->core.bits );
Ronald Cron9cca3162020-12-05 19:07:47 +0100347
Ronald Cron31216282020-12-05 18:47:56 +0100348 if( export_size == 0 )
349 return( PSA_ERROR_NOT_SUPPORTED );
350 }
351 else
352 {
Ronald Cron9df74be2020-12-05 19:15:23 +0100353 status = psa_driver_wrapper_get_key_buffer_size( attributes,
354 &export_size );
Ronald Cron31216282020-12-05 18:47:56 +0100355 if( status != PSA_SUCCESS )
356 return( status );
357 }
358
Ronald Cron9cca3162020-12-05 19:07:47 +0100359 slot->key.data = mbedtls_calloc(1, export_size);
360 if( slot->key.data == NULL )
361 return( PSA_ERROR_INSUFFICIENT_MEMORY );
362 slot->key.bytes = export_size;
363 }
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200364
365 switch( location )
366 {
367 case PSA_KEY_LOCATION_LOCAL_STORAGE:
368 /* Key is stored in the slot in export representation, so
369 * cycle through all known transparent accelerators */
370
371 /* Transparent drivers are limited to generating asymmetric keys */
372 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
373 {
374 status = PSA_ERROR_NOT_SUPPORTED;
375 break;
376 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200377#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200378 status = test_transparent_generate_key( attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100379 slot->key.data,
380 slot->key.bytes,
381 &slot->key.bytes );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200382 /* Declared with fallback == true */
383 if( status != PSA_ERROR_NOT_SUPPORTED )
384 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200385#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200386 /* Fell through, meaning no accelerator supports this operation */
387 status = PSA_ERROR_NOT_SUPPORTED;
388 break;
389 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200390#if defined(PSA_CRYPTO_DRIVER_TEST)
391 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200392 status = test_opaque_generate_key( attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100393 slot->key.data,
394 slot->key.bytes,
395 &slot->key.bytes );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200396 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200397#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200398 default:
399 /* Key is declared with a lifetime not known to us */
400 status = PSA_ERROR_INVALID_ARGUMENT;
401 break;
402 }
403
404 if( status != PSA_SUCCESS )
405 {
406 /* free allocated buffer */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100407 mbedtls_free( slot->key.data );
408 slot->key.data = NULL;
409 slot->key.bytes = 0;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200410 }
411
412 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200413#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200414 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200415#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
416#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200417 (void) attributes;
418 (void) slot;
419
Steven Cooreman56250fd2020-09-04 13:07:15 +0200420 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200421#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200422}
423
Ronald Cron83282872020-11-22 14:02:39 +0100424psa_status_t psa_driver_wrapper_import_key(
425 const psa_key_attributes_t *attributes,
426 const uint8_t *data,
427 size_t data_length,
428 uint8_t *key_buffer,
429 size_t key_buffer_size,
430 size_t *key_buffer_length,
431 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200432{
Steven Cooreman04524762020-10-13 17:43:44 +0200433 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100434 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
435 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200436
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100437 /* Try dynamically-registered SE interface first */
438#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
439 const psa_drv_se_t *drv;
440 psa_drv_se_context_t *drv_context;
441
442 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
443 {
444 if( drv->key_management == NULL ||
445 drv->key_management->p_import == NULL )
446 return( PSA_ERROR_NOT_SUPPORTED );
447
448 /* The driver should set the number of key bits, however in
449 * case it doesn't, we initialize bits to an invalid value. */
450 *bits = PSA_MAX_KEY_BITS + 1;
451 status = drv->key_management->p_import(
452 drv_context,
453 *( (psa_key_slot_number_t *)key_buffer ),
454 attributes, data, data_length, bits );
455
456 if( status != PSA_SUCCESS )
457 return( status );
458
459 if( (*bits) > PSA_MAX_KEY_BITS )
460 return( PSA_ERROR_NOT_SUPPORTED );
461
462 return( PSA_SUCCESS );
463 }
464#endif /* PSA_CRYPTO_SE_C */
465
Ronald Cronbf33c932020-11-28 18:06:53 +0100466 switch( location )
467 {
468 case PSA_KEY_LOCATION_LOCAL_STORAGE:
469 /* Key is stored in the slot in export representation, so
470 * cycle through all known transparent accelerators */
471#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
472#if defined(PSA_CRYPTO_DRIVER_TEST)
473 status = test_transparent_import_key( attributes,
474 data, data_length,
475 key_buffer, key_buffer_size,
476 key_buffer_length, bits );
477 /* Declared with fallback == true */
478 if( status != PSA_ERROR_NOT_SUPPORTED )
479 return( status );
480#endif /* PSA_CRYPTO_DRIVER_TEST */
481#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
482 /* Fell through, meaning no accelerator supports this operation */
483 return( psa_import_key_into_slot( attributes,
484 data, data_length,
485 key_buffer, key_buffer_size,
486 key_buffer_length, bits ) );
487
488 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100489 /* Importing a key with external storage in not yet supported.
490 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100491 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100492 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100493 }
494
Steven Cooreman04524762020-10-13 17:43:44 +0200495}
496
Ronald Cron67227982020-11-26 15:16:05 +0100497psa_status_t psa_driver_wrapper_export_key(
498 const psa_key_attributes_t *attributes,
499 const uint8_t *key_buffer, size_t key_buffer_size,
500 uint8_t *data, size_t data_size, size_t *data_length )
501
502{
503 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
504 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
505 psa_get_key_lifetime( attributes ) );
506
Ronald Cron152e3f82020-11-26 16:06:41 +0100507 /* Try dynamically-registered SE interface first */
508#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
509 const psa_drv_se_t *drv;
510 psa_drv_se_context_t *drv_context;
511
512 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
513 {
514 if( ( drv->key_management == NULL ) ||
515 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100516 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100517 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100518 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100519
520 return( drv->key_management->p_export(
521 drv_context,
522 *( (psa_key_slot_number_t *)key_buffer ),
523 data, data_size, data_length ) );
524 }
525#endif /* PSA_CRYPTO_SE_C */
526
Ronald Cron67227982020-11-26 15:16:05 +0100527 switch( location )
528 {
529 case PSA_KEY_LOCATION_LOCAL_STORAGE:
530 return( psa_export_key_internal( attributes,
531 key_buffer,
532 key_buffer_size,
533 data,
534 data_size,
535 data_length ) );
536
537 /* Add cases for opaque driver here */
538#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
539#if defined(PSA_CRYPTO_DRIVER_TEST)
540 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
541 return( test_opaque_export_key( attributes,
542 key_buffer,
543 key_buffer_size,
544 data,
545 data_size,
546 data_length ) );
547#endif /* PSA_CRYPTO_DRIVER_TEST */
548#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
549 default:
550 /* Key is declared with a lifetime not known to us */
551 return( status );
552 }
553}
554
Ronald Cron84cc9942020-11-25 14:30:05 +0100555psa_status_t psa_driver_wrapper_export_public_key(
556 const psa_key_attributes_t *attributes,
557 const uint8_t *key_buffer, size_t key_buffer_size,
558 uint8_t *data, size_t data_size, size_t *data_length )
559
Steven Cooremanb9b84422020-10-14 14:39:20 +0200560{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200561 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100562 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
563 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200564
Ronald Cron152e3f82020-11-26 16:06:41 +0100565 /* Try dynamically-registered SE interface first */
566#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
567 const psa_drv_se_t *drv;
568 psa_drv_se_context_t *drv_context;
569
570 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
571 {
572 if( ( drv->key_management == NULL ) ||
573 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100574 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100575 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100576 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100577
578 return( drv->key_management->p_export_public(
579 drv_context,
580 *( (psa_key_slot_number_t *)key_buffer ),
581 data, data_size, data_length ) );
582 }
583#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
584
Steven Cooremanb9b84422020-10-14 14:39:20 +0200585 switch( location )
586 {
587 case PSA_KEY_LOCATION_LOCAL_STORAGE:
588 /* Key is stored in the slot in export representation, so
589 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100590#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200591#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron84cc9942020-11-25 14:30:05 +0100592 status = test_transparent_export_public_key( attributes,
593 key_buffer,
594 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200595 data,
596 data_size,
597 data_length );
598 /* Declared with fallback == true */
599 if( status != PSA_ERROR_NOT_SUPPORTED )
600 return( status );
601#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100602#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200603 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100604 return( psa_export_public_key_internal( attributes,
605 key_buffer,
606 key_buffer_size,
607 data,
608 data_size,
609 data_length ) );
610
Steven Cooremanb9b84422020-10-14 14:39:20 +0200611 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100612#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200613#if defined(PSA_CRYPTO_DRIVER_TEST)
614 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron84cc9942020-11-25 14:30:05 +0100615 return( test_opaque_export_public_key( attributes,
616 key_buffer,
617 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200618 data,
619 data_size,
620 data_length ) );
621#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100622#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200623 default:
624 /* Key is declared with a lifetime not known to us */
625 return( status );
626 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200627}
628
Steven Cooreman37941cb2020-07-28 18:49:51 +0200629/*
630 * Cipher functions
631 */
632psa_status_t psa_driver_wrapper_cipher_encrypt(
633 psa_key_slot_t *slot,
634 psa_algorithm_t alg,
635 const uint8_t *input,
636 size_t input_length,
637 uint8_t *output,
638 size_t output_size,
639 size_t *output_length )
640{
641#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
642 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
643 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
644 psa_key_attributes_t attributes = {
645 .core = slot->attr
646 };
647
648 switch( location )
649 {
650 case PSA_KEY_LOCATION_LOCAL_STORAGE:
651 /* Key is stored in the slot in export representation, so
652 * cycle through all known transparent accelerators */
653#if defined(PSA_CRYPTO_DRIVER_TEST)
654 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100655 slot->key.data,
656 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200657 alg,
658 input,
659 input_length,
660 output,
661 output_size,
662 output_length );
663 /* Declared with fallback == true */
664 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200665 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200666#endif /* PSA_CRYPTO_DRIVER_TEST */
667 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200668 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200669 /* Add cases for opaque driver here */
670#if defined(PSA_CRYPTO_DRIVER_TEST)
671 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
672 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100673 slot->key.data,
674 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200675 alg,
676 input,
677 input_length,
678 output,
679 output_size,
680 output_length ) );
681#endif /* PSA_CRYPTO_DRIVER_TEST */
682 default:
683 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200684 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200685 }
686#else /* PSA_CRYPTO_DRIVER_PRESENT */
687 (void) slot;
688 (void) alg;
689 (void) input;
690 (void) input_length;
691 (void) output;
692 (void) output_size;
693 (void) output_length;
694
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200695 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200696#endif /* PSA_CRYPTO_DRIVER_PRESENT */
697}
698
699psa_status_t psa_driver_wrapper_cipher_decrypt(
700 psa_key_slot_t *slot,
701 psa_algorithm_t alg,
702 const uint8_t *input,
703 size_t input_length,
704 uint8_t *output,
705 size_t output_size,
706 size_t *output_length )
707{
708#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
709 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
710 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
711 psa_key_attributes_t attributes = {
712 .core = slot->attr
713 };
714
715 switch( location )
716 {
717 case PSA_KEY_LOCATION_LOCAL_STORAGE:
718 /* Key is stored in the slot in export representation, so
719 * cycle through all known transparent accelerators */
720#if defined(PSA_CRYPTO_DRIVER_TEST)
721 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100722 slot->key.data,
723 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200724 alg,
725 input,
726 input_length,
727 output,
728 output_size,
729 output_length );
730 /* Declared with fallback == true */
731 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200732 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200733#endif /* PSA_CRYPTO_DRIVER_TEST */
734 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200735 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200736 /* Add cases for opaque driver here */
737#if defined(PSA_CRYPTO_DRIVER_TEST)
738 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
739 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100740 slot->key.data,
741 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200742 alg,
743 input,
744 input_length,
745 output,
746 output_size,
747 output_length ) );
748#endif /* PSA_CRYPTO_DRIVER_TEST */
749 default:
750 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200751 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200752 }
753#else /* PSA_CRYPTO_DRIVER_PRESENT */
754 (void) slot;
755 (void) alg;
756 (void) input;
757 (void) input_length;
758 (void) output;
759 (void) output_size;
760 (void) output_length;
761
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200762 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200763#endif /* PSA_CRYPTO_DRIVER_PRESENT */
764}
765
766psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200767 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200768 psa_key_slot_t *slot,
769 psa_algorithm_t alg )
770{
771#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
772 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
773 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
774 psa_key_attributes_t attributes = {
775 .core = slot->attr
776 };
777
Steven Cooreman37941cb2020-07-28 18:49:51 +0200778 switch( location )
779 {
780 case PSA_KEY_LOCATION_LOCAL_STORAGE:
781 /* Key is stored in the slot in export representation, so
782 * cycle through all known transparent accelerators */
783#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200784 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
785 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200786 return PSA_ERROR_INSUFFICIENT_MEMORY;
787
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200788 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200789 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100790 slot->key.data,
791 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200792 alg );
793 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200794 if( status == PSA_SUCCESS )
795 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200796 else
797 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200798 mbedtls_platform_zeroize(
799 operation->ctx,
800 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200801 mbedtls_free( operation->ctx );
802 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200803 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200804
805 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200806#endif /* PSA_CRYPTO_DRIVER_TEST */
807 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200808 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200809 /* Add cases for opaque driver here */
810#if defined(PSA_CRYPTO_DRIVER_TEST)
811 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200812 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
813 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200814 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200815
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200816 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200817 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100818 slot->key.data,
819 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200820 alg );
821 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200822 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200823 else
824 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200825 mbedtls_platform_zeroize(
826 operation->ctx,
827 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200828 mbedtls_free( operation->ctx );
829 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200830 }
831
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200832 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200833#endif /* PSA_CRYPTO_DRIVER_TEST */
834 default:
835 /* Key is declared with a lifetime not known to us */
John Durkop714e3a12020-09-29 22:07:04 -0700836 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200837 }
838#else /* PSA_CRYPTO_DRIVER_PRESENT */
839 (void)slot;
840 (void)alg;
841 (void)operation;
842
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200843 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200844#endif /* PSA_CRYPTO_DRIVER_PRESENT */
845}
846
847psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200848 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200849 psa_key_slot_t *slot,
850 psa_algorithm_t alg )
851{
852#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
853 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
854 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
855 psa_key_attributes_t attributes = {
856 .core = slot->attr
857 };
858
Steven Cooreman37941cb2020-07-28 18:49:51 +0200859 switch( location )
860 {
861 case PSA_KEY_LOCATION_LOCAL_STORAGE:
862 /* Key is stored in the slot in export representation, so
863 * cycle through all known transparent accelerators */
864#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200865 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
866 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200867 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200868
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200869 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200870 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100871 slot->key.data,
872 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200873 alg );
874 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200875 if( status == PSA_SUCCESS )
876 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200877 else
878 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200879 mbedtls_platform_zeroize(
880 operation->ctx,
881 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200882 mbedtls_free( operation->ctx );
883 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200884 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200885
886 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200887#endif /* PSA_CRYPTO_DRIVER_TEST */
888 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200889 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200890 /* Add cases for opaque driver here */
891#if defined(PSA_CRYPTO_DRIVER_TEST)
892 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200893 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
894 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200895 return PSA_ERROR_INSUFFICIENT_MEMORY;
896
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200897 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200898 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100899 slot->key.data,
900 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200901 alg );
902 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200903 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200904 else
905 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200906 mbedtls_platform_zeroize(
907 operation->ctx,
908 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200909 mbedtls_free( operation->ctx );
910 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200911 }
912
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200913 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200914#endif /* PSA_CRYPTO_DRIVER_TEST */
915 default:
916 /* Key is declared with a lifetime not known to us */
John Durkop814dca72020-10-05 06:31:12 -0700917 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200918 }
919#else /* PSA_CRYPTO_DRIVER_PRESENT */
920 (void)slot;
921 (void)alg;
922 (void)operation;
923
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200924 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200925#endif /* PSA_CRYPTO_DRIVER_PRESENT */
926}
927
928psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200929 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200930 uint8_t *iv,
931 size_t iv_size,
932 size_t *iv_length )
933{
934#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200935 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200936 {
937#if defined(PSA_CRYPTO_DRIVER_TEST)
938 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200939 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200940 iv,
941 iv_size,
942 iv_length ) );
943#endif /* PSA_CRYPTO_DRIVER_TEST */
944#if defined(PSA_CRYPTO_DRIVER_TEST)
945 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200946 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200947 iv,
948 iv_size,
949 iv_length ) );
950#endif /* PSA_CRYPTO_DRIVER_TEST */
951 default:
952 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200953 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200954 }
955#else /* PSA_CRYPTO_DRIVER_PRESENT */
956 (void) operation;
957 (void) iv;
958 (void) iv_size;
959 (void) iv_length;
960
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200961 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200962#endif /* PSA_CRYPTO_DRIVER_PRESENT */
963}
964
965psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200966 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200967 const uint8_t *iv,
968 size_t iv_length )
969{
970#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200971 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200972 {
973#if defined(PSA_CRYPTO_DRIVER_TEST)
974 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200975 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200976 iv,
977 iv_length ) );
978#endif /* PSA_CRYPTO_DRIVER_TEST */
979#if defined(PSA_CRYPTO_DRIVER_TEST)
980 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200981 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200982 iv,
983 iv_length ) );
984#endif /* PSA_CRYPTO_DRIVER_TEST */
985 default:
986 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200987 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200988 }
989#else /* PSA_CRYPTO_DRIVER_PRESENT */
990 (void) operation;
991 (void) iv;
992 (void) iv_length;
993
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200994 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200995#endif /* PSA_CRYPTO_DRIVER_PRESENT */
996}
997
998psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200999 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001000 const uint8_t *input,
1001 size_t input_length,
1002 uint8_t *output,
1003 size_t output_size,
1004 size_t *output_length )
1005{
1006#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001007 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001008 {
1009#if defined(PSA_CRYPTO_DRIVER_TEST)
1010 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001011 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001012 input,
1013 input_length,
1014 output,
1015 output_size,
1016 output_length ) );
1017#endif /* PSA_CRYPTO_DRIVER_TEST */
1018#if defined(PSA_CRYPTO_DRIVER_TEST)
1019 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001020 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001021 input,
1022 input_length,
1023 output,
1024 output_size,
1025 output_length ) );
1026#endif /* PSA_CRYPTO_DRIVER_TEST */
1027 default:
1028 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001029 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001030 }
1031#else /* PSA_CRYPTO_DRIVER_PRESENT */
1032 (void) operation;
1033 (void) input;
1034 (void) input_length;
1035 (void) output;
1036 (void) output_length;
1037 (void) output_size;
1038
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001039 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001040#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1041}
1042
1043psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001044 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001045 uint8_t *output,
1046 size_t output_size,
1047 size_t *output_length )
1048{
1049#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001050 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001051 {
1052#if defined(PSA_CRYPTO_DRIVER_TEST)
1053 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001054 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001055 output,
1056 output_size,
1057 output_length ) );
1058#endif /* PSA_CRYPTO_DRIVER_TEST */
1059#if defined(PSA_CRYPTO_DRIVER_TEST)
1060 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001061 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001062 output,
1063 output_size,
1064 output_length ) );
1065#endif /* PSA_CRYPTO_DRIVER_TEST */
1066 default:
1067 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001068 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001069 }
1070#else /* PSA_CRYPTO_DRIVER_PRESENT */
1071 (void) operation;
1072 (void) output;
1073 (void) output_size;
1074 (void) output_length;
1075
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001076 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001077#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1078}
1079
1080psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001081 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001082{
1083#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1084 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001085
1086 /* The object has (apparently) been initialized but it is not in use. It's
1087 * ok to call abort on such an object, and there's nothing to do. */
1088 if( operation->ctx == NULL && operation->id == 0 )
1089 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001090
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001091 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001092 {
1093#if defined(PSA_CRYPTO_DRIVER_TEST)
1094 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001095 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001096 mbedtls_platform_zeroize(
1097 operation->ctx,
1098 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001099 mbedtls_free( operation->ctx );
1100 operation->ctx = NULL;
1101 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001102
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001103 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001104#endif /* PSA_CRYPTO_DRIVER_TEST */
1105#if defined(PSA_CRYPTO_DRIVER_TEST)
1106 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001107 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001108 mbedtls_platform_zeroize(
1109 operation->ctx,
1110 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001111 mbedtls_free( operation->ctx );
1112 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001113 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001114
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001115 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001116#endif /* PSA_CRYPTO_DRIVER_TEST */
1117 default:
1118 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001119 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001120 }
1121#else /* PSA_CRYPTO_DRIVER_PRESENT */
1122 (void)operation;
1123
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001124 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001125#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1126}
1127
Steven Cooremancd84cb42020-07-16 20:28:36 +02001128/* End of automatically generated file. */