blob: 07888f1de2caacacb5dca9589755265d2c6cfaa9 [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)
Ronald Cron31216282020-12-05 18:47:56 +0100242/** Get the key buffer size for the key material of a generated key in the
243 * case of an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200244 *
Ronald Cron31216282020-12-05 18:47:56 +0100245 * \param[in] attributes The key attributes.
246 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200247 *
248 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100249 * The minimum size for a buffer to contain the key material has been
250 * returned successfully.
251 * \retval #PSA_ERROR_INVALID_ARGUMENT
252 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200253 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100254 * The type and/or the size in bits of the key or the combination of
255 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200256 */
Ronald Cron31216282020-12-05 18:47:56 +0100257static psa_status_t get_key_buffer_size(
258 const psa_key_attributes_t *attributes,
259 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200260{
John Durkop2c618352020-09-22 06:54:01 -0700261 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
262 psa_key_type_t key_type = attributes->core.type;
263 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200264
Ronald Cron31216282020-12-05 18:47:56 +0100265 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700266 switch( location )
267 {
John Durkop2c618352020-09-22 06:54:01 -0700268#if defined(PSA_CRYPTO_DRIVER_TEST)
269 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
John Durkopac93e3b2020-10-16 06:48:55 -0700270#ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
Ronald Cron31216282020-12-05 18:47:56 +0100271 *key_buffer_size = test_size_function( key_type, key_bits );
John Durkop2c618352020-09-22 06:54:01 -0700272 return( PSA_SUCCESS );
273#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
274 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
275 {
Ronald Cron31216282020-12-05 18:47:56 +0100276 int public_key_overhead =
277 ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ?
278 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
279 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop135ce692020-10-19 07:12:28 -0700280 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
281 + public_key_overhead;
282 }
Ronald Cron31216282020-12-05 18:47:56 +0100283 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
John Durkop135ce692020-10-19 07:12:28 -0700284 {
Ronald Cron31216282020-12-05 18:47:56 +0100285 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700286 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
287 }
John Durkop135ce692020-10-19 07:12:28 -0700288 else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
Ronald Cron31216282020-12-05 18:47:56 +0100289 !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
John Durkop2c618352020-09-22 06:54:01 -0700290 {
Ronald Cron31216282020-12-05 18:47:56 +0100291 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700292 + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
293 * ( ( key_bits + 7 ) / 8 );
294 }
295 else
296 {
297 return( PSA_ERROR_NOT_SUPPORTED );
298 }
299 return( PSA_SUCCESS );
300#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
301#endif /* PSA_CRYPTO_DRIVER_TEST */
302
303 default:
Steven Cooreman56250fd2020-09-04 13:07:15 +0200304 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200305 }
306}
John Durkop135ce692020-10-19 07:12:28 -0700307#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200308
Steven Cooreman55ae2172020-07-17 19:46:15 +0200309psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes,
310 psa_key_slot_t *slot )
311{
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200312#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200313 /* Try dynamically-registered SE interface first */
314#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
315 const psa_drv_se_t *drv;
316 psa_drv_se_context_t *drv_context;
317
318 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
319 {
320 size_t pubkey_length = 0; /* We don't support this feature yet */
321 if( drv->key_management == NULL ||
322 drv->key_management->p_generate == NULL )
323 {
324 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200325 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200326 }
327 return( drv->key_management->p_generate(
Ronald Cronea0f8a62020-11-25 17:52:23 +0100328 drv_context, psa_key_slot_get_slot_number( slot ),
329 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200330 }
331#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
332
333 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200334#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200335 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
336 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
337 size_t export_size = 0;
338
Ronald Cron9cca3162020-12-05 19:07:47 +0100339 /* If this is not to generate a key in a secure element or cryptoprocessor
340 * with storage. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100341 if( slot->key.data == NULL )
Ronald Cron9cca3162020-12-05 19:07:47 +0100342 {
Ronald Cron31216282020-12-05 18:47:56 +0100343 if( location == PSA_KEY_LOCATION_LOCAL_STORAGE )
344 {
345 export_size = PSA_KEY_EXPORT_MAX_SIZE( attributes->core.type,
346 attributes->core.bits );
Ronald Cron9cca3162020-12-05 19:07:47 +0100347
Ronald Cron31216282020-12-05 18:47:56 +0100348 if( export_size == 0 )
349 return( PSA_ERROR_NOT_SUPPORTED );
350 }
351 else
352 {
353 status = get_key_buffer_size( attributes, &export_size );
354 if( status != PSA_SUCCESS )
355 return( status );
356 }
357
Ronald Cron9cca3162020-12-05 19:07:47 +0100358 slot->key.data = mbedtls_calloc(1, export_size);
359 if( slot->key.data == NULL )
360 return( PSA_ERROR_INSUFFICIENT_MEMORY );
361 slot->key.bytes = export_size;
362 }
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200363
364 switch( location )
365 {
366 case PSA_KEY_LOCATION_LOCAL_STORAGE:
367 /* Key is stored in the slot in export representation, so
368 * cycle through all known transparent accelerators */
369
370 /* Transparent drivers are limited to generating asymmetric keys */
371 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
372 {
373 status = PSA_ERROR_NOT_SUPPORTED;
374 break;
375 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200376#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200377 status = test_transparent_generate_key( attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100378 slot->key.data,
379 slot->key.bytes,
380 &slot->key.bytes );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200381 /* Declared with fallback == true */
382 if( status != PSA_ERROR_NOT_SUPPORTED )
383 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200384#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200385 /* Fell through, meaning no accelerator supports this operation */
386 status = PSA_ERROR_NOT_SUPPORTED;
387 break;
388 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200389#if defined(PSA_CRYPTO_DRIVER_TEST)
390 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200391 status = test_opaque_generate_key( attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100392 slot->key.data,
393 slot->key.bytes,
394 &slot->key.bytes );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200395 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200396#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200397 default:
398 /* Key is declared with a lifetime not known to us */
399 status = PSA_ERROR_INVALID_ARGUMENT;
400 break;
401 }
402
403 if( status != PSA_SUCCESS )
404 {
405 /* free allocated buffer */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100406 mbedtls_free( slot->key.data );
407 slot->key.data = NULL;
408 slot->key.bytes = 0;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200409 }
410
411 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200412#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200413 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200414#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
415#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200416 (void) attributes;
417 (void) slot;
418
Steven Cooreman56250fd2020-09-04 13:07:15 +0200419 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200420#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200421}
422
Ronald Cron83282872020-11-22 14:02:39 +0100423psa_status_t psa_driver_wrapper_import_key(
424 const psa_key_attributes_t *attributes,
425 const uint8_t *data,
426 size_t data_length,
427 uint8_t *key_buffer,
428 size_t key_buffer_size,
429 size_t *key_buffer_length,
430 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200431{
Steven Cooreman04524762020-10-13 17:43:44 +0200432 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100433 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
434 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200435
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100436 /* Try dynamically-registered SE interface first */
437#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
438 const psa_drv_se_t *drv;
439 psa_drv_se_context_t *drv_context;
440
441 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
442 {
443 if( drv->key_management == NULL ||
444 drv->key_management->p_import == NULL )
445 return( PSA_ERROR_NOT_SUPPORTED );
446
447 /* The driver should set the number of key bits, however in
448 * case it doesn't, we initialize bits to an invalid value. */
449 *bits = PSA_MAX_KEY_BITS + 1;
450 status = drv->key_management->p_import(
451 drv_context,
452 *( (psa_key_slot_number_t *)key_buffer ),
453 attributes, data, data_length, bits );
454
455 if( status != PSA_SUCCESS )
456 return( status );
457
458 if( (*bits) > PSA_MAX_KEY_BITS )
459 return( PSA_ERROR_NOT_SUPPORTED );
460
461 return( PSA_SUCCESS );
462 }
463#endif /* PSA_CRYPTO_SE_C */
464
Ronald Cronbf33c932020-11-28 18:06:53 +0100465 switch( location )
466 {
467 case PSA_KEY_LOCATION_LOCAL_STORAGE:
468 /* Key is stored in the slot in export representation, so
469 * cycle through all known transparent accelerators */
470#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
471#if defined(PSA_CRYPTO_DRIVER_TEST)
472 status = test_transparent_import_key( attributes,
473 data, data_length,
474 key_buffer, key_buffer_size,
475 key_buffer_length, bits );
476 /* Declared with fallback == true */
477 if( status != PSA_ERROR_NOT_SUPPORTED )
478 return( status );
479#endif /* PSA_CRYPTO_DRIVER_TEST */
480#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
481 /* Fell through, meaning no accelerator supports this operation */
482 return( psa_import_key_into_slot( attributes,
483 data, data_length,
484 key_buffer, key_buffer_size,
485 key_buffer_length, bits ) );
486
487 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100488 /* Importing a key with external storage in not yet supported.
489 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100490 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100491 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100492 }
493
Steven Cooreman04524762020-10-13 17:43:44 +0200494}
495
Ronald Cron67227982020-11-26 15:16:05 +0100496psa_status_t psa_driver_wrapper_export_key(
497 const psa_key_attributes_t *attributes,
498 const uint8_t *key_buffer, size_t key_buffer_size,
499 uint8_t *data, size_t data_size, size_t *data_length )
500
501{
502 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
503 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
504 psa_get_key_lifetime( attributes ) );
505
Ronald Cron152e3f82020-11-26 16:06:41 +0100506 /* Try dynamically-registered SE interface first */
507#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
508 const psa_drv_se_t *drv;
509 psa_drv_se_context_t *drv_context;
510
511 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
512 {
513 if( ( drv->key_management == NULL ) ||
514 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100515 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100516 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100517 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100518
519 return( drv->key_management->p_export(
520 drv_context,
521 *( (psa_key_slot_number_t *)key_buffer ),
522 data, data_size, data_length ) );
523 }
524#endif /* PSA_CRYPTO_SE_C */
525
Ronald Cron67227982020-11-26 15:16:05 +0100526 switch( location )
527 {
528 case PSA_KEY_LOCATION_LOCAL_STORAGE:
529 return( psa_export_key_internal( attributes,
530 key_buffer,
531 key_buffer_size,
532 data,
533 data_size,
534 data_length ) );
535
536 /* Add cases for opaque driver here */
537#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
538#if defined(PSA_CRYPTO_DRIVER_TEST)
539 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
540 return( test_opaque_export_key( attributes,
541 key_buffer,
542 key_buffer_size,
543 data,
544 data_size,
545 data_length ) );
546#endif /* PSA_CRYPTO_DRIVER_TEST */
547#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
548 default:
549 /* Key is declared with a lifetime not known to us */
550 return( status );
551 }
552}
553
Ronald Cron84cc9942020-11-25 14:30:05 +0100554psa_status_t psa_driver_wrapper_export_public_key(
555 const psa_key_attributes_t *attributes,
556 const uint8_t *key_buffer, size_t key_buffer_size,
557 uint8_t *data, size_t data_size, size_t *data_length )
558
Steven Cooremanb9b84422020-10-14 14:39:20 +0200559{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200560 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100561 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
562 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200563
Ronald Cron152e3f82020-11-26 16:06:41 +0100564 /* Try dynamically-registered SE interface first */
565#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
566 const psa_drv_se_t *drv;
567 psa_drv_se_context_t *drv_context;
568
569 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
570 {
571 if( ( drv->key_management == NULL ) ||
572 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100573 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100574 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100575 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100576
577 return( drv->key_management->p_export_public(
578 drv_context,
579 *( (psa_key_slot_number_t *)key_buffer ),
580 data, data_size, data_length ) );
581 }
582#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
583
Steven Cooremanb9b84422020-10-14 14:39:20 +0200584 switch( location )
585 {
586 case PSA_KEY_LOCATION_LOCAL_STORAGE:
587 /* Key is stored in the slot in export representation, so
588 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100589#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200590#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron84cc9942020-11-25 14:30:05 +0100591 status = test_transparent_export_public_key( attributes,
592 key_buffer,
593 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200594 data,
595 data_size,
596 data_length );
597 /* Declared with fallback == true */
598 if( status != PSA_ERROR_NOT_SUPPORTED )
599 return( status );
600#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100601#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200602 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100603 return( psa_export_public_key_internal( attributes,
604 key_buffer,
605 key_buffer_size,
606 data,
607 data_size,
608 data_length ) );
609
Steven Cooremanb9b84422020-10-14 14:39:20 +0200610 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100611#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200612#if defined(PSA_CRYPTO_DRIVER_TEST)
613 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron84cc9942020-11-25 14:30:05 +0100614 return( test_opaque_export_public_key( attributes,
615 key_buffer,
616 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200617 data,
618 data_size,
619 data_length ) );
620#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100621#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200622 default:
623 /* Key is declared with a lifetime not known to us */
624 return( status );
625 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200626}
627
Steven Cooreman37941cb2020-07-28 18:49:51 +0200628/*
629 * Cipher functions
630 */
631psa_status_t psa_driver_wrapper_cipher_encrypt(
632 psa_key_slot_t *slot,
633 psa_algorithm_t alg,
634 const uint8_t *input,
635 size_t input_length,
636 uint8_t *output,
637 size_t output_size,
638 size_t *output_length )
639{
640#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
641 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
642 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
643 psa_key_attributes_t attributes = {
644 .core = slot->attr
645 };
646
647 switch( location )
648 {
649 case PSA_KEY_LOCATION_LOCAL_STORAGE:
650 /* Key is stored in the slot in export representation, so
651 * cycle through all known transparent accelerators */
652#if defined(PSA_CRYPTO_DRIVER_TEST)
653 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100654 slot->key.data,
655 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200656 alg,
657 input,
658 input_length,
659 output,
660 output_size,
661 output_length );
662 /* Declared with fallback == true */
663 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200664 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200665#endif /* PSA_CRYPTO_DRIVER_TEST */
666 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200667 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200668 /* Add cases for opaque driver here */
669#if defined(PSA_CRYPTO_DRIVER_TEST)
670 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
671 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100672 slot->key.data,
673 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200674 alg,
675 input,
676 input_length,
677 output,
678 output_size,
679 output_length ) );
680#endif /* PSA_CRYPTO_DRIVER_TEST */
681 default:
682 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200683 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200684 }
685#else /* PSA_CRYPTO_DRIVER_PRESENT */
686 (void) slot;
687 (void) alg;
688 (void) input;
689 (void) input_length;
690 (void) output;
691 (void) output_size;
692 (void) output_length;
693
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200694 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200695#endif /* PSA_CRYPTO_DRIVER_PRESENT */
696}
697
698psa_status_t psa_driver_wrapper_cipher_decrypt(
699 psa_key_slot_t *slot,
700 psa_algorithm_t alg,
701 const uint8_t *input,
702 size_t input_length,
703 uint8_t *output,
704 size_t output_size,
705 size_t *output_length )
706{
707#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
708 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
709 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
710 psa_key_attributes_t attributes = {
711 .core = slot->attr
712 };
713
714 switch( location )
715 {
716 case PSA_KEY_LOCATION_LOCAL_STORAGE:
717 /* Key is stored in the slot in export representation, so
718 * cycle through all known transparent accelerators */
719#if defined(PSA_CRYPTO_DRIVER_TEST)
720 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100721 slot->key.data,
722 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200723 alg,
724 input,
725 input_length,
726 output,
727 output_size,
728 output_length );
729 /* Declared with fallback == true */
730 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200731 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200732#endif /* PSA_CRYPTO_DRIVER_TEST */
733 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200734 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200735 /* Add cases for opaque driver here */
736#if defined(PSA_CRYPTO_DRIVER_TEST)
737 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
738 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100739 slot->key.data,
740 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200741 alg,
742 input,
743 input_length,
744 output,
745 output_size,
746 output_length ) );
747#endif /* PSA_CRYPTO_DRIVER_TEST */
748 default:
749 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200750 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200751 }
752#else /* PSA_CRYPTO_DRIVER_PRESENT */
753 (void) slot;
754 (void) alg;
755 (void) input;
756 (void) input_length;
757 (void) output;
758 (void) output_size;
759 (void) output_length;
760
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200761 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200762#endif /* PSA_CRYPTO_DRIVER_PRESENT */
763}
764
765psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200766 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200767 psa_key_slot_t *slot,
768 psa_algorithm_t alg )
769{
770#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
771 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
772 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
773 psa_key_attributes_t attributes = {
774 .core = slot->attr
775 };
776
Steven Cooreman37941cb2020-07-28 18:49:51 +0200777 switch( location )
778 {
779 case PSA_KEY_LOCATION_LOCAL_STORAGE:
780 /* Key is stored in the slot in export representation, so
781 * cycle through all known transparent accelerators */
782#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200783 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
784 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200785 return PSA_ERROR_INSUFFICIENT_MEMORY;
786
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200787 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200788 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100789 slot->key.data,
790 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200791 alg );
792 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200793 if( status == PSA_SUCCESS )
794 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200795 else
796 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200797 mbedtls_platform_zeroize(
798 operation->ctx,
799 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200800 mbedtls_free( operation->ctx );
801 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200802 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200803
804 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200805#endif /* PSA_CRYPTO_DRIVER_TEST */
806 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200807 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200808 /* Add cases for opaque driver here */
809#if defined(PSA_CRYPTO_DRIVER_TEST)
810 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200811 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
812 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200813 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200814
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200815 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200816 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100817 slot->key.data,
818 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200819 alg );
820 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200821 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200822 else
823 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200824 mbedtls_platform_zeroize(
825 operation->ctx,
826 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200827 mbedtls_free( operation->ctx );
828 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200829 }
830
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200831 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200832#endif /* PSA_CRYPTO_DRIVER_TEST */
833 default:
834 /* Key is declared with a lifetime not known to us */
John Durkop714e3a12020-09-29 22:07:04 -0700835 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200836 }
837#else /* PSA_CRYPTO_DRIVER_PRESENT */
838 (void)slot;
839 (void)alg;
840 (void)operation;
841
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200842 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200843#endif /* PSA_CRYPTO_DRIVER_PRESENT */
844}
845
846psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200847 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200848 psa_key_slot_t *slot,
849 psa_algorithm_t alg )
850{
851#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
852 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
853 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
854 psa_key_attributes_t attributes = {
855 .core = slot->attr
856 };
857
Steven Cooreman37941cb2020-07-28 18:49:51 +0200858 switch( location )
859 {
860 case PSA_KEY_LOCATION_LOCAL_STORAGE:
861 /* Key is stored in the slot in export representation, so
862 * cycle through all known transparent accelerators */
863#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200864 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
865 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200866 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200867
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200868 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200869 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100870 slot->key.data,
871 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200872 alg );
873 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200874 if( status == PSA_SUCCESS )
875 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200876 else
877 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200878 mbedtls_platform_zeroize(
879 operation->ctx,
880 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200881 mbedtls_free( operation->ctx );
882 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200883 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200884
885 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200886#endif /* PSA_CRYPTO_DRIVER_TEST */
887 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200888 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200889 /* Add cases for opaque driver here */
890#if defined(PSA_CRYPTO_DRIVER_TEST)
891 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200892 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
893 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200894 return PSA_ERROR_INSUFFICIENT_MEMORY;
895
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200896 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200897 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100898 slot->key.data,
899 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200900 alg );
901 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200902 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200903 else
904 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200905 mbedtls_platform_zeroize(
906 operation->ctx,
907 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200908 mbedtls_free( operation->ctx );
909 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200910 }
911
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200912 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200913#endif /* PSA_CRYPTO_DRIVER_TEST */
914 default:
915 /* Key is declared with a lifetime not known to us */
John Durkop814dca72020-10-05 06:31:12 -0700916 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200917 }
918#else /* PSA_CRYPTO_DRIVER_PRESENT */
919 (void)slot;
920 (void)alg;
921 (void)operation;
922
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200923 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200924#endif /* PSA_CRYPTO_DRIVER_PRESENT */
925}
926
927psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200928 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200929 uint8_t *iv,
930 size_t iv_size,
931 size_t *iv_length )
932{
933#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200934 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200935 {
936#if defined(PSA_CRYPTO_DRIVER_TEST)
937 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200938 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200939 iv,
940 iv_size,
941 iv_length ) );
942#endif /* PSA_CRYPTO_DRIVER_TEST */
943#if defined(PSA_CRYPTO_DRIVER_TEST)
944 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200945 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200946 iv,
947 iv_size,
948 iv_length ) );
949#endif /* PSA_CRYPTO_DRIVER_TEST */
950 default:
951 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200952 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200953 }
954#else /* PSA_CRYPTO_DRIVER_PRESENT */
955 (void) operation;
956 (void) iv;
957 (void) iv_size;
958 (void) iv_length;
959
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200960 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200961#endif /* PSA_CRYPTO_DRIVER_PRESENT */
962}
963
964psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200965 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200966 const uint8_t *iv,
967 size_t iv_length )
968{
969#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200970 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200971 {
972#if defined(PSA_CRYPTO_DRIVER_TEST)
973 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200974 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200975 iv,
976 iv_length ) );
977#endif /* PSA_CRYPTO_DRIVER_TEST */
978#if defined(PSA_CRYPTO_DRIVER_TEST)
979 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200980 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200981 iv,
982 iv_length ) );
983#endif /* PSA_CRYPTO_DRIVER_TEST */
984 default:
985 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200986 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200987 }
988#else /* PSA_CRYPTO_DRIVER_PRESENT */
989 (void) operation;
990 (void) iv;
991 (void) iv_length;
992
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200993 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200994#endif /* PSA_CRYPTO_DRIVER_PRESENT */
995}
996
997psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200998 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200999 const uint8_t *input,
1000 size_t input_length,
1001 uint8_t *output,
1002 size_t output_size,
1003 size_t *output_length )
1004{
1005#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001006 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001007 {
1008#if defined(PSA_CRYPTO_DRIVER_TEST)
1009 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001010 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001011 input,
1012 input_length,
1013 output,
1014 output_size,
1015 output_length ) );
1016#endif /* PSA_CRYPTO_DRIVER_TEST */
1017#if defined(PSA_CRYPTO_DRIVER_TEST)
1018 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001019 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001020 input,
1021 input_length,
1022 output,
1023 output_size,
1024 output_length ) );
1025#endif /* PSA_CRYPTO_DRIVER_TEST */
1026 default:
1027 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001028 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001029 }
1030#else /* PSA_CRYPTO_DRIVER_PRESENT */
1031 (void) operation;
1032 (void) input;
1033 (void) input_length;
1034 (void) output;
1035 (void) output_length;
1036 (void) output_size;
1037
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001038 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001039#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1040}
1041
1042psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001043 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001044 uint8_t *output,
1045 size_t output_size,
1046 size_t *output_length )
1047{
1048#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001049 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001050 {
1051#if defined(PSA_CRYPTO_DRIVER_TEST)
1052 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001053 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001054 output,
1055 output_size,
1056 output_length ) );
1057#endif /* PSA_CRYPTO_DRIVER_TEST */
1058#if defined(PSA_CRYPTO_DRIVER_TEST)
1059 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001060 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001061 output,
1062 output_size,
1063 output_length ) );
1064#endif /* PSA_CRYPTO_DRIVER_TEST */
1065 default:
1066 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001067 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001068 }
1069#else /* PSA_CRYPTO_DRIVER_PRESENT */
1070 (void) operation;
1071 (void) output;
1072 (void) output_size;
1073 (void) output_length;
1074
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001075 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001076#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1077}
1078
1079psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001080 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001081{
1082#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1083 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001084
1085 /* The object has (apparently) been initialized but it is not in use. It's
1086 * ok to call abort on such an object, and there's nothing to do. */
1087 if( operation->ctx == NULL && operation->id == 0 )
1088 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001089
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001090 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001091 {
1092#if defined(PSA_CRYPTO_DRIVER_TEST)
1093 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001094 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001095 mbedtls_platform_zeroize(
1096 operation->ctx,
1097 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001098 mbedtls_free( operation->ctx );
1099 operation->ctx = NULL;
1100 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001101
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001102 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001103#endif /* PSA_CRYPTO_DRIVER_TEST */
1104#if defined(PSA_CRYPTO_DRIVER_TEST)
1105 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001106 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001107 mbedtls_platform_zeroize(
1108 operation->ctx,
1109 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001110 mbedtls_free( operation->ctx );
1111 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001112 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001113
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001114 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001115#endif /* PSA_CRYPTO_DRIVER_TEST */
1116 default:
1117 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001118 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001119 }
1120#else /* PSA_CRYPTO_DRIVER_PRESENT */
1121 (void)operation;
1122
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001123 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001124#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1125}
1126
Steven Cooremancd84cb42020-07-16 20:28:36 +02001127/* End of automatically generated file. */