blob: fd03ed2bf64b4c385b66675ee529177a7baeeff3 [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
344 status = get_expected_key_size( attributes, &export_size );
345 if( status != PSA_SUCCESS )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200346 return( status );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200347
Ronald Cronea0f8a62020-11-25 17:52:23 +0100348 slot->key.data = mbedtls_calloc(1, export_size);
349 if( slot->key.data == NULL )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200350 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Ronald Cronea0f8a62020-11-25 17:52:23 +0100351 slot->key.bytes = export_size;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200352
353 switch( location )
354 {
355 case PSA_KEY_LOCATION_LOCAL_STORAGE:
356 /* Key is stored in the slot in export representation, so
357 * cycle through all known transparent accelerators */
358
359 /* Transparent drivers are limited to generating asymmetric keys */
360 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
361 {
362 status = PSA_ERROR_NOT_SUPPORTED;
363 break;
364 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200365#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200366 status = test_transparent_generate_key( attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100367 slot->key.data,
368 slot->key.bytes,
369 &slot->key.bytes );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200370 /* Declared with fallback == true */
371 if( status != PSA_ERROR_NOT_SUPPORTED )
372 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200373#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200374 /* Fell through, meaning no accelerator supports this operation */
375 status = PSA_ERROR_NOT_SUPPORTED;
376 break;
377 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200378#if defined(PSA_CRYPTO_DRIVER_TEST)
379 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200380 status = test_opaque_generate_key( attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100381 slot->key.data,
382 slot->key.bytes,
383 &slot->key.bytes );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200384 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200385#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200386 default:
387 /* Key is declared with a lifetime not known to us */
388 status = PSA_ERROR_INVALID_ARGUMENT;
389 break;
390 }
391
392 if( status != PSA_SUCCESS )
393 {
394 /* free allocated buffer */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100395 mbedtls_free( slot->key.data );
396 slot->key.data = NULL;
397 slot->key.bytes = 0;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200398 }
399
400 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200401#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200402 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200403#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
404#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200405 (void) attributes;
406 (void) slot;
407
Steven Cooreman56250fd2020-09-04 13:07:15 +0200408 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200409#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200410}
411
Ronald Cron83282872020-11-22 14:02:39 +0100412psa_status_t psa_driver_wrapper_import_key(
413 const psa_key_attributes_t *attributes,
414 const uint8_t *data,
415 size_t data_length,
416 uint8_t *key_buffer,
417 size_t key_buffer_size,
418 size_t *key_buffer_length,
419 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200420{
Steven Cooreman04524762020-10-13 17:43:44 +0200421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100422 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
423 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200424
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100425 /* Try dynamically-registered SE interface first */
426#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
427 const psa_drv_se_t *drv;
428 psa_drv_se_context_t *drv_context;
429
430 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
431 {
432 if( drv->key_management == NULL ||
433 drv->key_management->p_import == NULL )
434 return( PSA_ERROR_NOT_SUPPORTED );
435
436 /* The driver should set the number of key bits, however in
437 * case it doesn't, we initialize bits to an invalid value. */
438 *bits = PSA_MAX_KEY_BITS + 1;
439 status = drv->key_management->p_import(
440 drv_context,
441 *( (psa_key_slot_number_t *)key_buffer ),
442 attributes, data, data_length, bits );
443
444 if( status != PSA_SUCCESS )
445 return( status );
446
447 if( (*bits) > PSA_MAX_KEY_BITS )
448 return( PSA_ERROR_NOT_SUPPORTED );
449
450 return( PSA_SUCCESS );
451 }
452#endif /* PSA_CRYPTO_SE_C */
453
Ronald Cronbf33c932020-11-28 18:06:53 +0100454 switch( location )
455 {
456 case PSA_KEY_LOCATION_LOCAL_STORAGE:
457 /* Key is stored in the slot in export representation, so
458 * cycle through all known transparent accelerators */
459#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
460#if defined(PSA_CRYPTO_DRIVER_TEST)
461 status = test_transparent_import_key( attributes,
462 data, data_length,
463 key_buffer, key_buffer_size,
464 key_buffer_length, bits );
465 /* Declared with fallback == true */
466 if( status != PSA_ERROR_NOT_SUPPORTED )
467 return( status );
468#endif /* PSA_CRYPTO_DRIVER_TEST */
469#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
470 /* Fell through, meaning no accelerator supports this operation */
471 return( psa_import_key_into_slot( attributes,
472 data, data_length,
473 key_buffer, key_buffer_size,
474 key_buffer_length, bits ) );
475
476 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100477 /* Importing a key with external storage in not yet supported.
478 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100479 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100480 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100481 }
482
Steven Cooreman04524762020-10-13 17:43:44 +0200483}
484
Ronald Cron67227982020-11-26 15:16:05 +0100485psa_status_t psa_driver_wrapper_export_key(
486 const psa_key_attributes_t *attributes,
487 const uint8_t *key_buffer, size_t key_buffer_size,
488 uint8_t *data, size_t data_size, size_t *data_length )
489
490{
491 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
492 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
493 psa_get_key_lifetime( attributes ) );
494
Ronald Cron152e3f82020-11-26 16:06:41 +0100495 /* Try dynamically-registered SE interface first */
496#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
497 const psa_drv_se_t *drv;
498 psa_drv_se_context_t *drv_context;
499
500 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
501 {
502 if( ( drv->key_management == NULL ) ||
503 ( drv->key_management->p_export == NULL ) )
504 return( PSA_ERROR_NOT_SUPPORTED );
505
506 return( drv->key_management->p_export(
507 drv_context,
508 *( (psa_key_slot_number_t *)key_buffer ),
509 data, data_size, data_length ) );
510 }
511#endif /* PSA_CRYPTO_SE_C */
512
Ronald Cron67227982020-11-26 15:16:05 +0100513 switch( location )
514 {
515 case PSA_KEY_LOCATION_LOCAL_STORAGE:
516 return( psa_export_key_internal( attributes,
517 key_buffer,
518 key_buffer_size,
519 data,
520 data_size,
521 data_length ) );
522
523 /* Add cases for opaque driver here */
524#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
525#if defined(PSA_CRYPTO_DRIVER_TEST)
526 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
527 return( test_opaque_export_key( attributes,
528 key_buffer,
529 key_buffer_size,
530 data,
531 data_size,
532 data_length ) );
533#endif /* PSA_CRYPTO_DRIVER_TEST */
534#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
535 default:
536 /* Key is declared with a lifetime not known to us */
537 return( status );
538 }
539}
540
Ronald Cron84cc9942020-11-25 14:30:05 +0100541psa_status_t psa_driver_wrapper_export_public_key(
542 const psa_key_attributes_t *attributes,
543 const uint8_t *key_buffer, size_t key_buffer_size,
544 uint8_t *data, size_t data_size, size_t *data_length )
545
Steven Cooremanb9b84422020-10-14 14:39:20 +0200546{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200547 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100548 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
549 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200550
Ronald Cron152e3f82020-11-26 16:06:41 +0100551 /* Try dynamically-registered SE interface first */
552#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
553 const psa_drv_se_t *drv;
554 psa_drv_se_context_t *drv_context;
555
556 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
557 {
558 if( ( drv->key_management == NULL ) ||
559 ( drv->key_management->p_export_public == NULL ) )
560 return( PSA_ERROR_NOT_SUPPORTED );
561
562 return( drv->key_management->p_export_public(
563 drv_context,
564 *( (psa_key_slot_number_t *)key_buffer ),
565 data, data_size, data_length ) );
566 }
567#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
568
Steven Cooremanb9b84422020-10-14 14:39:20 +0200569 switch( location )
570 {
571 case PSA_KEY_LOCATION_LOCAL_STORAGE:
572 /* Key is stored in the slot in export representation, so
573 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100574#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200575#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron84cc9942020-11-25 14:30:05 +0100576 status = test_transparent_export_public_key( attributes,
577 key_buffer,
578 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200579 data,
580 data_size,
581 data_length );
582 /* Declared with fallback == true */
583 if( status != PSA_ERROR_NOT_SUPPORTED )
584 return( status );
585#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100586#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200587 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100588 return( psa_export_public_key_internal( attributes,
589 key_buffer,
590 key_buffer_size,
591 data,
592 data_size,
593 data_length ) );
594
Steven Cooremanb9b84422020-10-14 14:39:20 +0200595 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100596#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200597#if defined(PSA_CRYPTO_DRIVER_TEST)
598 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron84cc9942020-11-25 14:30:05 +0100599 return( test_opaque_export_public_key( attributes,
600 key_buffer,
601 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200602 data,
603 data_size,
604 data_length ) );
605#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100606#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200607 default:
608 /* Key is declared with a lifetime not known to us */
609 return( status );
610 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200611}
612
Steven Cooreman37941cb2020-07-28 18:49:51 +0200613/*
614 * Cipher functions
615 */
616psa_status_t psa_driver_wrapper_cipher_encrypt(
617 psa_key_slot_t *slot,
618 psa_algorithm_t alg,
619 const uint8_t *input,
620 size_t input_length,
621 uint8_t *output,
622 size_t output_size,
623 size_t *output_length )
624{
625#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
626 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
627 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
628 psa_key_attributes_t attributes = {
629 .core = slot->attr
630 };
631
632 switch( location )
633 {
634 case PSA_KEY_LOCATION_LOCAL_STORAGE:
635 /* Key is stored in the slot in export representation, so
636 * cycle through all known transparent accelerators */
637#if defined(PSA_CRYPTO_DRIVER_TEST)
638 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100639 slot->key.data,
640 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200641 alg,
642 input,
643 input_length,
644 output,
645 output_size,
646 output_length );
647 /* Declared with fallback == true */
648 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200649 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200650#endif /* PSA_CRYPTO_DRIVER_TEST */
651 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200652 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200653 /* Add cases for opaque driver here */
654#if defined(PSA_CRYPTO_DRIVER_TEST)
655 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
656 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100657 slot->key.data,
658 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200659 alg,
660 input,
661 input_length,
662 output,
663 output_size,
664 output_length ) );
665#endif /* PSA_CRYPTO_DRIVER_TEST */
666 default:
667 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200668 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200669 }
670#else /* PSA_CRYPTO_DRIVER_PRESENT */
671 (void) slot;
672 (void) alg;
673 (void) input;
674 (void) input_length;
675 (void) output;
676 (void) output_size;
677 (void) output_length;
678
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200679 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200680#endif /* PSA_CRYPTO_DRIVER_PRESENT */
681}
682
683psa_status_t psa_driver_wrapper_cipher_decrypt(
684 psa_key_slot_t *slot,
685 psa_algorithm_t alg,
686 const uint8_t *input,
687 size_t input_length,
688 uint8_t *output,
689 size_t output_size,
690 size_t *output_length )
691{
692#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
693 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
694 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
695 psa_key_attributes_t attributes = {
696 .core = slot->attr
697 };
698
699 switch( location )
700 {
701 case PSA_KEY_LOCATION_LOCAL_STORAGE:
702 /* Key is stored in the slot in export representation, so
703 * cycle through all known transparent accelerators */
704#if defined(PSA_CRYPTO_DRIVER_TEST)
705 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100706 slot->key.data,
707 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200708 alg,
709 input,
710 input_length,
711 output,
712 output_size,
713 output_length );
714 /* Declared with fallback == true */
715 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200716 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200717#endif /* PSA_CRYPTO_DRIVER_TEST */
718 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200719 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200720 /* Add cases for opaque driver here */
721#if defined(PSA_CRYPTO_DRIVER_TEST)
722 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
723 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100724 slot->key.data,
725 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200726 alg,
727 input,
728 input_length,
729 output,
730 output_size,
731 output_length ) );
732#endif /* PSA_CRYPTO_DRIVER_TEST */
733 default:
734 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200735 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200736 }
737#else /* PSA_CRYPTO_DRIVER_PRESENT */
738 (void) slot;
739 (void) alg;
740 (void) input;
741 (void) input_length;
742 (void) output;
743 (void) output_size;
744 (void) output_length;
745
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200746 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200747#endif /* PSA_CRYPTO_DRIVER_PRESENT */
748}
749
750psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200751 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200752 psa_key_slot_t *slot,
753 psa_algorithm_t alg )
754{
755#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
756 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
757 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
758 psa_key_attributes_t attributes = {
759 .core = slot->attr
760 };
761
Steven Cooreman37941cb2020-07-28 18:49:51 +0200762 switch( location )
763 {
764 case PSA_KEY_LOCATION_LOCAL_STORAGE:
765 /* Key is stored in the slot in export representation, so
766 * cycle through all known transparent accelerators */
767#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200768 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
769 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200770 return PSA_ERROR_INSUFFICIENT_MEMORY;
771
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200772 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200773 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100774 slot->key.data,
775 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200776 alg );
777 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200778 if( status == PSA_SUCCESS )
779 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200780 else
781 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200782 mbedtls_platform_zeroize(
783 operation->ctx,
784 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200785 mbedtls_free( operation->ctx );
786 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200787 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200788
789 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200790#endif /* PSA_CRYPTO_DRIVER_TEST */
791 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200792 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200793 /* Add cases for opaque driver here */
794#if defined(PSA_CRYPTO_DRIVER_TEST)
795 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200796 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
797 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200798 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200799
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200800 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200801 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100802 slot->key.data,
803 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200804 alg );
805 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200806 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200807 else
808 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200809 mbedtls_platform_zeroize(
810 operation->ctx,
811 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200812 mbedtls_free( operation->ctx );
813 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200814 }
815
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200816 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200817#endif /* PSA_CRYPTO_DRIVER_TEST */
818 default:
819 /* Key is declared with a lifetime not known to us */
John Durkop714e3a12020-09-29 22:07:04 -0700820 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200821 }
822#else /* PSA_CRYPTO_DRIVER_PRESENT */
823 (void)slot;
824 (void)alg;
825 (void)operation;
826
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200827 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200828#endif /* PSA_CRYPTO_DRIVER_PRESENT */
829}
830
831psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200832 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200833 psa_key_slot_t *slot,
834 psa_algorithm_t alg )
835{
836#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
837 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
838 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
839 psa_key_attributes_t attributes = {
840 .core = slot->attr
841 };
842
Steven Cooreman37941cb2020-07-28 18:49:51 +0200843 switch( location )
844 {
845 case PSA_KEY_LOCATION_LOCAL_STORAGE:
846 /* Key is stored in the slot in export representation, so
847 * cycle through all known transparent accelerators */
848#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200849 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
850 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200851 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200852
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200853 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200854 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100855 slot->key.data,
856 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200857 alg );
858 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200859 if( status == PSA_SUCCESS )
860 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200861 else
862 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200863 mbedtls_platform_zeroize(
864 operation->ctx,
865 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200866 mbedtls_free( operation->ctx );
867 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200868 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200869
870 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200871#endif /* PSA_CRYPTO_DRIVER_TEST */
872 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200873 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200874 /* Add cases for opaque driver here */
875#if defined(PSA_CRYPTO_DRIVER_TEST)
876 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200877 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
878 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200879 return PSA_ERROR_INSUFFICIENT_MEMORY;
880
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200881 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200882 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100883 slot->key.data,
884 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200885 alg );
886 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200887 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200888 else
889 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200890 mbedtls_platform_zeroize(
891 operation->ctx,
892 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200893 mbedtls_free( operation->ctx );
894 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200895 }
896
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200897 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200898#endif /* PSA_CRYPTO_DRIVER_TEST */
899 default:
900 /* Key is declared with a lifetime not known to us */
John Durkop814dca72020-10-05 06:31:12 -0700901 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200902 }
903#else /* PSA_CRYPTO_DRIVER_PRESENT */
904 (void)slot;
905 (void)alg;
906 (void)operation;
907
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200908 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200909#endif /* PSA_CRYPTO_DRIVER_PRESENT */
910}
911
912psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200913 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200914 uint8_t *iv,
915 size_t iv_size,
916 size_t *iv_length )
917{
918#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200919 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200920 {
921#if defined(PSA_CRYPTO_DRIVER_TEST)
922 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200923 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200924 iv,
925 iv_size,
926 iv_length ) );
927#endif /* PSA_CRYPTO_DRIVER_TEST */
928#if defined(PSA_CRYPTO_DRIVER_TEST)
929 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200930 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200931 iv,
932 iv_size,
933 iv_length ) );
934#endif /* PSA_CRYPTO_DRIVER_TEST */
935 default:
936 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200937 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200938 }
939#else /* PSA_CRYPTO_DRIVER_PRESENT */
940 (void) operation;
941 (void) iv;
942 (void) iv_size;
943 (void) iv_length;
944
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200945 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200946#endif /* PSA_CRYPTO_DRIVER_PRESENT */
947}
948
949psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200950 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200951 const uint8_t *iv,
952 size_t iv_length )
953{
954#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200955 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200956 {
957#if defined(PSA_CRYPTO_DRIVER_TEST)
958 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200959 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200960 iv,
961 iv_length ) );
962#endif /* PSA_CRYPTO_DRIVER_TEST */
963#if defined(PSA_CRYPTO_DRIVER_TEST)
964 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200965 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200966 iv,
967 iv_length ) );
968#endif /* PSA_CRYPTO_DRIVER_TEST */
969 default:
970 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200971 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200972 }
973#else /* PSA_CRYPTO_DRIVER_PRESENT */
974 (void) operation;
975 (void) iv;
976 (void) iv_length;
977
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200978 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200979#endif /* PSA_CRYPTO_DRIVER_PRESENT */
980}
981
982psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200983 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200984 const uint8_t *input,
985 size_t input_length,
986 uint8_t *output,
987 size_t output_size,
988 size_t *output_length )
989{
990#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200991 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200992 {
993#if defined(PSA_CRYPTO_DRIVER_TEST)
994 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200995 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200996 input,
997 input_length,
998 output,
999 output_size,
1000 output_length ) );
1001#endif /* PSA_CRYPTO_DRIVER_TEST */
1002#if defined(PSA_CRYPTO_DRIVER_TEST)
1003 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001004 return( test_opaque_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 default:
1012 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001013 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001014 }
1015#else /* PSA_CRYPTO_DRIVER_PRESENT */
1016 (void) operation;
1017 (void) input;
1018 (void) input_length;
1019 (void) output;
1020 (void) output_length;
1021 (void) output_size;
1022
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001023 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001024#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1025}
1026
1027psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001028 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001029 uint8_t *output,
1030 size_t output_size,
1031 size_t *output_length )
1032{
1033#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001034 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001035 {
1036#if defined(PSA_CRYPTO_DRIVER_TEST)
1037 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001038 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001039 output,
1040 output_size,
1041 output_length ) );
1042#endif /* PSA_CRYPTO_DRIVER_TEST */
1043#if defined(PSA_CRYPTO_DRIVER_TEST)
1044 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001045 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001046 output,
1047 output_size,
1048 output_length ) );
1049#endif /* PSA_CRYPTO_DRIVER_TEST */
1050 default:
1051 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001052 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001053 }
1054#else /* PSA_CRYPTO_DRIVER_PRESENT */
1055 (void) operation;
1056 (void) output;
1057 (void) output_size;
1058 (void) output_length;
1059
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001060 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001061#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1062}
1063
1064psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001065 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001066{
1067#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1068 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001069
1070 /* The object has (apparently) been initialized but it is not in use. It's
1071 * ok to call abort on such an object, and there's nothing to do. */
1072 if( operation->ctx == NULL && operation->id == 0 )
1073 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001074
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001075 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001076 {
1077#if defined(PSA_CRYPTO_DRIVER_TEST)
1078 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001079 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001080 mbedtls_platform_zeroize(
1081 operation->ctx,
1082 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001083 mbedtls_free( operation->ctx );
1084 operation->ctx = NULL;
1085 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001086
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001087 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001088#endif /* PSA_CRYPTO_DRIVER_TEST */
1089#if defined(PSA_CRYPTO_DRIVER_TEST)
1090 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001091 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001092 mbedtls_platform_zeroize(
1093 operation->ctx,
1094 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001095 mbedtls_free( operation->ctx );
1096 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001097 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001098
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001099 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001100#endif /* PSA_CRYPTO_DRIVER_TEST */
1101 default:
1102 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001103 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001104 }
1105#else /* PSA_CRYPTO_DRIVER_PRESENT */
1106 (void)operation;
1107
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001108 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001109#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1110}
1111
Steven Cooremancd84cb42020-07-16 20:28:36 +02001112/* End of automatically generated file. */