blob: 9440195cc8a8d0bc12419b2e79f1c5440ea2a82f [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
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200241#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman56250fd2020-09-04 13:07:15 +0200242/** Calculate the size to allocate for buffering a key with given attributes.
243 *
244 * This function provides a way to get the expected size for storing a key with
245 * the given attributes. This will be the size of the export representation for
246 * cleartext keys, and a driver-defined size for keys stored by opaque drivers.
247 *
248 * \param[in] attributes The key attribute structure of the key to store.
249 * \param[out] expected_size On success, a byte size large enough to contain
250 * the declared key.
251 *
252 * \retval #PSA_SUCCESS
253 * \retval #PSA_ERROR_NOT_SUPPORTED
254 */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200255static psa_status_t get_expected_key_size( const psa_key_attributes_t *attributes,
256 size_t *expected_size )
257{
Steven Cooreman56250fd2020-09-04 13:07:15 +0200258 size_t buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700259 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
260 psa_key_type_t key_type = attributes->core.type;
261 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200262
John Durkop2c618352020-09-22 06:54:01 -0700263 switch( location )
264 {
265 case PSA_KEY_LOCATION_LOCAL_STORAGE:
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100266 buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits );
John Durkop2c618352020-09-22 06:54:01 -0700267
268 if( buffer_size == 0 )
269 return( PSA_ERROR_NOT_SUPPORTED );
270
271 *expected_size = buffer_size;
272 return( PSA_SUCCESS );
273
274#if defined(PSA_CRYPTO_DRIVER_TEST)
275 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
John Durkopac93e3b2020-10-16 06:48:55 -0700276#ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
John Durkop2c618352020-09-22 06:54:01 -0700277 *expected_size = test_size_function( key_type, key_bits );
278 return( PSA_SUCCESS );
279#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
280 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
281 {
John Durkop135ce692020-10-19 07:12:28 -0700282 int public_key_overhead = ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ?
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100283 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
John Durkop135ce692020-10-19 07:12:28 -0700284 *expected_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
285 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
286 + public_key_overhead;
287 }
288 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( attributes->core.type ) )
289 {
John Durkop2c618352020-09-22 06:54:01 -0700290 *expected_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
291 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
292 }
John Durkop135ce692020-10-19 07:12:28 -0700293 else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
294 !PSA_KEY_TYPE_IS_PUBLIC_KEY ( attributes->core.type ) )
John Durkop2c618352020-09-22 06:54:01 -0700295 {
296 *expected_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
297 + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
298 * ( ( key_bits + 7 ) / 8 );
299 }
300 else
301 {
302 return( PSA_ERROR_NOT_SUPPORTED );
303 }
304 return( PSA_SUCCESS );
305#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
306#endif /* PSA_CRYPTO_DRIVER_TEST */
307
308 default:
Steven Cooreman56250fd2020-09-04 13:07:15 +0200309 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200310 }
311}
John Durkop135ce692020-10-19 07:12:28 -0700312#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200313
Steven Cooreman55ae2172020-07-17 19:46:15 +0200314psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes,
315 psa_key_slot_t *slot )
316{
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200317#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200318 /* Try dynamically-registered SE interface first */
319#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
320 const psa_drv_se_t *drv;
321 psa_drv_se_context_t *drv_context;
322
323 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
324 {
325 size_t pubkey_length = 0; /* We don't support this feature yet */
326 if( drv->key_management == NULL ||
327 drv->key_management->p_generate == NULL )
328 {
329 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200330 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200331 }
332 return( drv->key_management->p_generate(
Ronald Cronea0f8a62020-11-25 17:52:23 +0100333 drv_context, psa_key_slot_get_slot_number( slot ),
334 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200335 }
336#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
337
338 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200339#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200340 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
341 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
342 size_t export_size = 0;
343
Ronald Cron9cca3162020-12-05 19:07:47 +0100344 /* If this is not to generate a key in a secure element or cryptoprocessor
345 * with storage. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100346 if( slot->key.data == NULL )
Ronald Cron9cca3162020-12-05 19:07:47 +0100347 {
348 status = get_expected_key_size( attributes, &export_size );
349 if( status != PSA_SUCCESS )
350 return( status );
351
352 slot->key.data = mbedtls_calloc(1, export_size);
353 if( slot->key.data == NULL )
354 return( PSA_ERROR_INSUFFICIENT_MEMORY );
355 slot->key.bytes = export_size;
356 }
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200357
358 switch( location )
359 {
360 case PSA_KEY_LOCATION_LOCAL_STORAGE:
361 /* Key is stored in the slot in export representation, so
362 * cycle through all known transparent accelerators */
363
364 /* Transparent drivers are limited to generating asymmetric keys */
365 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
366 {
367 status = PSA_ERROR_NOT_SUPPORTED;
368 break;
369 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200370#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200371 status = test_transparent_generate_key( attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100372 slot->key.data,
373 slot->key.bytes,
374 &slot->key.bytes );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200375 /* Declared with fallback == true */
376 if( status != PSA_ERROR_NOT_SUPPORTED )
377 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200378#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200379 /* Fell through, meaning no accelerator supports this operation */
380 status = PSA_ERROR_NOT_SUPPORTED;
381 break;
382 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200383#if defined(PSA_CRYPTO_DRIVER_TEST)
384 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200385 status = test_opaque_generate_key( attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100386 slot->key.data,
387 slot->key.bytes,
388 &slot->key.bytes );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200389 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200390#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200391 default:
392 /* Key is declared with a lifetime not known to us */
393 status = PSA_ERROR_INVALID_ARGUMENT;
394 break;
395 }
396
397 if( status != PSA_SUCCESS )
398 {
399 /* free allocated buffer */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100400 mbedtls_free( slot->key.data );
401 slot->key.data = NULL;
402 slot->key.bytes = 0;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200403 }
404
405 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200406#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200407 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200408#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
409#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200410 (void) attributes;
411 (void) slot;
412
Steven Cooreman56250fd2020-09-04 13:07:15 +0200413 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200414#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200415}
416
Ronald Cron83282872020-11-22 14:02:39 +0100417psa_status_t psa_driver_wrapper_import_key(
418 const psa_key_attributes_t *attributes,
419 const uint8_t *data,
420 size_t data_length,
421 uint8_t *key_buffer,
422 size_t key_buffer_size,
423 size_t *key_buffer_length,
424 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200425{
Steven Cooreman04524762020-10-13 17:43:44 +0200426 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100427 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
428 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200429
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100430 /* Try dynamically-registered SE interface first */
431#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
432 const psa_drv_se_t *drv;
433 psa_drv_se_context_t *drv_context;
434
435 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
436 {
437 if( drv->key_management == NULL ||
438 drv->key_management->p_import == NULL )
439 return( PSA_ERROR_NOT_SUPPORTED );
440
441 /* The driver should set the number of key bits, however in
442 * case it doesn't, we initialize bits to an invalid value. */
443 *bits = PSA_MAX_KEY_BITS + 1;
444 status = drv->key_management->p_import(
445 drv_context,
446 *( (psa_key_slot_number_t *)key_buffer ),
447 attributes, data, data_length, bits );
448
449 if( status != PSA_SUCCESS )
450 return( status );
451
452 if( (*bits) > PSA_MAX_KEY_BITS )
453 return( PSA_ERROR_NOT_SUPPORTED );
454
455 return( PSA_SUCCESS );
456 }
457#endif /* PSA_CRYPTO_SE_C */
458
Ronald Cronbf33c932020-11-28 18:06:53 +0100459 switch( location )
460 {
461 case PSA_KEY_LOCATION_LOCAL_STORAGE:
462 /* Key is stored in the slot in export representation, so
463 * cycle through all known transparent accelerators */
464#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
465#if defined(PSA_CRYPTO_DRIVER_TEST)
466 status = test_transparent_import_key( attributes,
467 data, data_length,
468 key_buffer, key_buffer_size,
469 key_buffer_length, bits );
470 /* Declared with fallback == true */
471 if( status != PSA_ERROR_NOT_SUPPORTED )
472 return( status );
473#endif /* PSA_CRYPTO_DRIVER_TEST */
474#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
475 /* Fell through, meaning no accelerator supports this operation */
476 return( psa_import_key_into_slot( attributes,
477 data, data_length,
478 key_buffer, key_buffer_size,
479 key_buffer_length, bits ) );
480
481 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100482 /* Importing a key with external storage in not yet supported.
483 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100484 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100485 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100486 }
487
Steven Cooreman04524762020-10-13 17:43:44 +0200488}
489
Ronald Cron67227982020-11-26 15:16:05 +0100490psa_status_t psa_driver_wrapper_export_key(
491 const psa_key_attributes_t *attributes,
492 const uint8_t *key_buffer, size_t key_buffer_size,
493 uint8_t *data, size_t data_size, size_t *data_length )
494
495{
496 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
497 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
498 psa_get_key_lifetime( attributes ) );
499
Ronald Cron152e3f82020-11-26 16:06:41 +0100500 /* Try dynamically-registered SE interface first */
501#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
502 const psa_drv_se_t *drv;
503 psa_drv_se_context_t *drv_context;
504
505 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
506 {
507 if( ( drv->key_management == NULL ) ||
508 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100509 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100510 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100511 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100512
513 return( drv->key_management->p_export(
514 drv_context,
515 *( (psa_key_slot_number_t *)key_buffer ),
516 data, data_size, data_length ) );
517 }
518#endif /* PSA_CRYPTO_SE_C */
519
Ronald Cron67227982020-11-26 15:16:05 +0100520 switch( location )
521 {
522 case PSA_KEY_LOCATION_LOCAL_STORAGE:
523 return( psa_export_key_internal( attributes,
524 key_buffer,
525 key_buffer_size,
526 data,
527 data_size,
528 data_length ) );
529
530 /* Add cases for opaque driver here */
531#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
532#if defined(PSA_CRYPTO_DRIVER_TEST)
533 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
534 return( test_opaque_export_key( attributes,
535 key_buffer,
536 key_buffer_size,
537 data,
538 data_size,
539 data_length ) );
540#endif /* PSA_CRYPTO_DRIVER_TEST */
541#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
542 default:
543 /* Key is declared with a lifetime not known to us */
544 return( status );
545 }
546}
547
Ronald Cron84cc9942020-11-25 14:30:05 +0100548psa_status_t psa_driver_wrapper_export_public_key(
549 const psa_key_attributes_t *attributes,
550 const uint8_t *key_buffer, size_t key_buffer_size,
551 uint8_t *data, size_t data_size, size_t *data_length )
552
Steven Cooremanb9b84422020-10-14 14:39:20 +0200553{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200554 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100555 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
556 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200557
Ronald Cron152e3f82020-11-26 16:06:41 +0100558 /* Try dynamically-registered SE interface first */
559#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
560 const psa_drv_se_t *drv;
561 psa_drv_se_context_t *drv_context;
562
563 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
564 {
565 if( ( drv->key_management == NULL ) ||
566 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100567 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100568 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100569 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100570
571 return( drv->key_management->p_export_public(
572 drv_context,
573 *( (psa_key_slot_number_t *)key_buffer ),
574 data, data_size, data_length ) );
575 }
576#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
577
Steven Cooremanb9b84422020-10-14 14:39:20 +0200578 switch( location )
579 {
580 case PSA_KEY_LOCATION_LOCAL_STORAGE:
581 /* Key is stored in the slot in export representation, so
582 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100583#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200584#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron84cc9942020-11-25 14:30:05 +0100585 status = test_transparent_export_public_key( attributes,
586 key_buffer,
587 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200588 data,
589 data_size,
590 data_length );
591 /* Declared with fallback == true */
592 if( status != PSA_ERROR_NOT_SUPPORTED )
593 return( status );
594#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100595#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200596 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100597 return( psa_export_public_key_internal( attributes,
598 key_buffer,
599 key_buffer_size,
600 data,
601 data_size,
602 data_length ) );
603
Steven Cooremanb9b84422020-10-14 14:39:20 +0200604 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100605#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200606#if defined(PSA_CRYPTO_DRIVER_TEST)
607 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron84cc9942020-11-25 14:30:05 +0100608 return( test_opaque_export_public_key( attributes,
609 key_buffer,
610 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200611 data,
612 data_size,
613 data_length ) );
614#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100615#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200616 default:
617 /* Key is declared with a lifetime not known to us */
618 return( status );
619 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200620}
621
Steven Cooreman37941cb2020-07-28 18:49:51 +0200622/*
623 * Cipher functions
624 */
625psa_status_t psa_driver_wrapper_cipher_encrypt(
626 psa_key_slot_t *slot,
627 psa_algorithm_t alg,
628 const uint8_t *input,
629 size_t input_length,
630 uint8_t *output,
631 size_t output_size,
632 size_t *output_length )
633{
634#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
635 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
636 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
637 psa_key_attributes_t attributes = {
638 .core = slot->attr
639 };
640
641 switch( location )
642 {
643 case PSA_KEY_LOCATION_LOCAL_STORAGE:
644 /* Key is stored in the slot in export representation, so
645 * cycle through all known transparent accelerators */
646#if defined(PSA_CRYPTO_DRIVER_TEST)
647 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100648 slot->key.data,
649 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200650 alg,
651 input,
652 input_length,
653 output,
654 output_size,
655 output_length );
656 /* Declared with fallback == true */
657 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200658 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200659#endif /* PSA_CRYPTO_DRIVER_TEST */
660 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200661 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200662 /* Add cases for opaque driver here */
663#if defined(PSA_CRYPTO_DRIVER_TEST)
664 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
665 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100666 slot->key.data,
667 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200668 alg,
669 input,
670 input_length,
671 output,
672 output_size,
673 output_length ) );
674#endif /* PSA_CRYPTO_DRIVER_TEST */
675 default:
676 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200677 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200678 }
679#else /* PSA_CRYPTO_DRIVER_PRESENT */
680 (void) slot;
681 (void) alg;
682 (void) input;
683 (void) input_length;
684 (void) output;
685 (void) output_size;
686 (void) output_length;
687
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200688 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200689#endif /* PSA_CRYPTO_DRIVER_PRESENT */
690}
691
692psa_status_t psa_driver_wrapper_cipher_decrypt(
693 psa_key_slot_t *slot,
694 psa_algorithm_t alg,
695 const uint8_t *input,
696 size_t input_length,
697 uint8_t *output,
698 size_t output_size,
699 size_t *output_length )
700{
701#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
702 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
703 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
704 psa_key_attributes_t attributes = {
705 .core = slot->attr
706 };
707
708 switch( location )
709 {
710 case PSA_KEY_LOCATION_LOCAL_STORAGE:
711 /* Key is stored in the slot in export representation, so
712 * cycle through all known transparent accelerators */
713#if defined(PSA_CRYPTO_DRIVER_TEST)
714 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100715 slot->key.data,
716 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200717 alg,
718 input,
719 input_length,
720 output,
721 output_size,
722 output_length );
723 /* Declared with fallback == true */
724 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200725 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200726#endif /* PSA_CRYPTO_DRIVER_TEST */
727 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200728 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200729 /* Add cases for opaque driver here */
730#if defined(PSA_CRYPTO_DRIVER_TEST)
731 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
732 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100733 slot->key.data,
734 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200735 alg,
736 input,
737 input_length,
738 output,
739 output_size,
740 output_length ) );
741#endif /* PSA_CRYPTO_DRIVER_TEST */
742 default:
743 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200744 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200745 }
746#else /* PSA_CRYPTO_DRIVER_PRESENT */
747 (void) slot;
748 (void) alg;
749 (void) input;
750 (void) input_length;
751 (void) output;
752 (void) output_size;
753 (void) output_length;
754
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200755 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200756#endif /* PSA_CRYPTO_DRIVER_PRESENT */
757}
758
759psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200760 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200761 psa_key_slot_t *slot,
762 psa_algorithm_t alg )
763{
764#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
765 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
766 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
767 psa_key_attributes_t attributes = {
768 .core = slot->attr
769 };
770
Steven Cooreman37941cb2020-07-28 18:49:51 +0200771 switch( location )
772 {
773 case PSA_KEY_LOCATION_LOCAL_STORAGE:
774 /* Key is stored in the slot in export representation, so
775 * cycle through all known transparent accelerators */
776#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200777 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
778 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200779 return PSA_ERROR_INSUFFICIENT_MEMORY;
780
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200781 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200782 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100783 slot->key.data,
784 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200785 alg );
786 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200787 if( status == PSA_SUCCESS )
788 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200789 else
790 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200791 mbedtls_platform_zeroize(
792 operation->ctx,
793 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200794 mbedtls_free( operation->ctx );
795 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200796 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200797
798 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200799#endif /* PSA_CRYPTO_DRIVER_TEST */
800 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200801 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200802 /* Add cases for opaque driver here */
803#if defined(PSA_CRYPTO_DRIVER_TEST)
804 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200805 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
806 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200807 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200808
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200809 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200810 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100811 slot->key.data,
812 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200813 alg );
814 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200815 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200816 else
817 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200818 mbedtls_platform_zeroize(
819 operation->ctx,
820 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200821 mbedtls_free( operation->ctx );
822 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200823 }
824
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200825 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200826#endif /* PSA_CRYPTO_DRIVER_TEST */
827 default:
828 /* Key is declared with a lifetime not known to us */
John Durkop714e3a12020-09-29 22:07:04 -0700829 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200830 }
831#else /* PSA_CRYPTO_DRIVER_PRESENT */
832 (void)slot;
833 (void)alg;
834 (void)operation;
835
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200836 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200837#endif /* PSA_CRYPTO_DRIVER_PRESENT */
838}
839
840psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200841 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200842 psa_key_slot_t *slot,
843 psa_algorithm_t alg )
844{
845#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
846 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
847 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
848 psa_key_attributes_t attributes = {
849 .core = slot->attr
850 };
851
Steven Cooreman37941cb2020-07-28 18:49:51 +0200852 switch( location )
853 {
854 case PSA_KEY_LOCATION_LOCAL_STORAGE:
855 /* Key is stored in the slot in export representation, so
856 * cycle through all known transparent accelerators */
857#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200858 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
859 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200860 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200861
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200862 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200863 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100864 slot->key.data,
865 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200866 alg );
867 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200868 if( status == PSA_SUCCESS )
869 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200870 else
871 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200872 mbedtls_platform_zeroize(
873 operation->ctx,
874 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200875 mbedtls_free( operation->ctx );
876 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200877 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200878
879 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200880#endif /* PSA_CRYPTO_DRIVER_TEST */
881 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200882 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200883 /* Add cases for opaque driver here */
884#if defined(PSA_CRYPTO_DRIVER_TEST)
885 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200886 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
887 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200888 return PSA_ERROR_INSUFFICIENT_MEMORY;
889
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200890 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200891 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100892 slot->key.data,
893 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200894 alg );
895 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200896 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200897 else
898 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200899 mbedtls_platform_zeroize(
900 operation->ctx,
901 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200902 mbedtls_free( operation->ctx );
903 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200904 }
905
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200906 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200907#endif /* PSA_CRYPTO_DRIVER_TEST */
908 default:
909 /* Key is declared with a lifetime not known to us */
John Durkop814dca72020-10-05 06:31:12 -0700910 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200911 }
912#else /* PSA_CRYPTO_DRIVER_PRESENT */
913 (void)slot;
914 (void)alg;
915 (void)operation;
916
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200917 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200918#endif /* PSA_CRYPTO_DRIVER_PRESENT */
919}
920
921psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200922 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200923 uint8_t *iv,
924 size_t iv_size,
925 size_t *iv_length )
926{
927#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200928 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200929 {
930#if defined(PSA_CRYPTO_DRIVER_TEST)
931 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200932 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200933 iv,
934 iv_size,
935 iv_length ) );
936#endif /* PSA_CRYPTO_DRIVER_TEST */
937#if defined(PSA_CRYPTO_DRIVER_TEST)
938 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200939 return( test_opaque_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 default:
945 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200946 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200947 }
948#else /* PSA_CRYPTO_DRIVER_PRESENT */
949 (void) operation;
950 (void) iv;
951 (void) iv_size;
952 (void) iv_length;
953
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200954 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200955#endif /* PSA_CRYPTO_DRIVER_PRESENT */
956}
957
958psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200959 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200960 const uint8_t *iv,
961 size_t iv_length )
962{
963#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200964 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200965 {
966#if defined(PSA_CRYPTO_DRIVER_TEST)
967 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200968 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200969 iv,
970 iv_length ) );
971#endif /* PSA_CRYPTO_DRIVER_TEST */
972#if defined(PSA_CRYPTO_DRIVER_TEST)
973 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200974 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200975 iv,
976 iv_length ) );
977#endif /* PSA_CRYPTO_DRIVER_TEST */
978 default:
979 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200980 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200981 }
982#else /* PSA_CRYPTO_DRIVER_PRESENT */
983 (void) operation;
984 (void) iv;
985 (void) iv_length;
986
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200987 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200988#endif /* PSA_CRYPTO_DRIVER_PRESENT */
989}
990
991psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200992 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200993 const uint8_t *input,
994 size_t input_length,
995 uint8_t *output,
996 size_t output_size,
997 size_t *output_length )
998{
999#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001000 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001001 {
1002#if defined(PSA_CRYPTO_DRIVER_TEST)
1003 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001004 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001005 input,
1006 input_length,
1007 output,
1008 output_size,
1009 output_length ) );
1010#endif /* PSA_CRYPTO_DRIVER_TEST */
1011#if defined(PSA_CRYPTO_DRIVER_TEST)
1012 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001013 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001014 input,
1015 input_length,
1016 output,
1017 output_size,
1018 output_length ) );
1019#endif /* PSA_CRYPTO_DRIVER_TEST */
1020 default:
1021 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001022 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001023 }
1024#else /* PSA_CRYPTO_DRIVER_PRESENT */
1025 (void) operation;
1026 (void) input;
1027 (void) input_length;
1028 (void) output;
1029 (void) output_length;
1030 (void) output_size;
1031
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001032 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001033#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1034}
1035
1036psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001037 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001038 uint8_t *output,
1039 size_t output_size,
1040 size_t *output_length )
1041{
1042#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001043 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001044 {
1045#if defined(PSA_CRYPTO_DRIVER_TEST)
1046 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001047 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001048 output,
1049 output_size,
1050 output_length ) );
1051#endif /* PSA_CRYPTO_DRIVER_TEST */
1052#if defined(PSA_CRYPTO_DRIVER_TEST)
1053 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001054 return( test_opaque_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 default:
1060 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001061 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001062 }
1063#else /* PSA_CRYPTO_DRIVER_PRESENT */
1064 (void) operation;
1065 (void) output;
1066 (void) output_size;
1067 (void) output_length;
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
1073psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001074 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001075{
1076#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1077 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001078
1079 /* The object has (apparently) been initialized but it is not in use. It's
1080 * ok to call abort on such an object, and there's nothing to do. */
1081 if( operation->ctx == NULL && operation->id == 0 )
1082 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001083
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001084 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001085 {
1086#if defined(PSA_CRYPTO_DRIVER_TEST)
1087 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001088 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001089 mbedtls_platform_zeroize(
1090 operation->ctx,
1091 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001092 mbedtls_free( operation->ctx );
1093 operation->ctx = NULL;
1094 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001095
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001096 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001097#endif /* PSA_CRYPTO_DRIVER_TEST */
1098#if defined(PSA_CRYPTO_DRIVER_TEST)
1099 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001100 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001101 mbedtls_platform_zeroize(
1102 operation->ctx,
1103 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001104 mbedtls_free( operation->ctx );
1105 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001106 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001107
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001108 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001109#endif /* PSA_CRYPTO_DRIVER_TEST */
1110 default:
1111 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001112 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001113 }
1114#else /* PSA_CRYPTO_DRIVER_PRESENT */
1115 (void)operation;
1116
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001117 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001118#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1119}
1120
Steven Cooremancd84cb42020-07-16 20:28:36 +02001121/* End of automatically generated file. */