blob: 2d433830d0fbebcbeb7c4cc5360a5ebedabe2c63 [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
Steven Cooreman04524762020-10-13 17:43:44 +0200412psa_status_t psa_driver_wrapper_validate_key( const psa_key_attributes_t *attributes,
413 const uint8_t *data,
414 size_t data_length,
415 size_t *bits )
416{
417#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
418 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
419 /* Try accelerators in turn */
420#if defined(PSA_CRYPTO_DRIVER_TEST)
421 status = test_transparent_validate_key( attributes,
422 data,
423 data_length,
424 bits );
425 /* Declared with fallback == true */
426 if( status != PSA_ERROR_NOT_SUPPORTED )
427 return( status );
428#endif /* PSA_CRYPTO_DRIVER_TEST */
429
430 return( PSA_ERROR_NOT_SUPPORTED );
431#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
432 (void) attributes;
433 (void) data;
434 (void) data_length;
435 (void) bits;
436 return( PSA_ERROR_NOT_SUPPORTED );
437#endif /* PSA_CRYPTO_DRIVER_PRESENT */
438}
439
Steven Cooremanb9b84422020-10-14 14:39:20 +0200440psa_status_t psa_driver_wrapper_export_public_key( const psa_key_slot_t *slot,
441 uint8_t *data,
442 size_t data_size,
443 size_t *data_length )
444{
445#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
446 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
447 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
448 psa_key_attributes_t attributes = {
449 .core = slot->attr
450 };
451
452 switch( location )
453 {
454 case PSA_KEY_LOCATION_LOCAL_STORAGE:
455 /* Key is stored in the slot in export representation, so
456 * cycle through all known transparent accelerators */
457#if defined(PSA_CRYPTO_DRIVER_TEST)
458 status = test_transparent_export_public_key( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100459 slot->key.data,
460 slot->key.bytes,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200461 data,
462 data_size,
463 data_length );
464 /* Declared with fallback == true */
465 if( status != PSA_ERROR_NOT_SUPPORTED )
466 return( status );
467#endif /* PSA_CRYPTO_DRIVER_TEST */
468 /* Fell through, meaning no accelerator supports this operation */
469 return( PSA_ERROR_NOT_SUPPORTED );
470 /* Add cases for opaque driver here */
471#if defined(PSA_CRYPTO_DRIVER_TEST)
472 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
473 return( test_opaque_export_public_key( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100474 slot->key.data,
475 slot->key.bytes,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200476 data,
477 data_size,
478 data_length ) );
479#endif /* PSA_CRYPTO_DRIVER_TEST */
480 default:
481 /* Key is declared with a lifetime not known to us */
482 return( status );
483 }
484#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
485 (void) slot;
486 (void) data;
487 (void) data_size;
488 (void) data_length;
489 return( PSA_ERROR_NOT_SUPPORTED );
490#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
491}
492
Steven Cooreman37941cb2020-07-28 18:49:51 +0200493/*
494 * Cipher functions
495 */
496psa_status_t psa_driver_wrapper_cipher_encrypt(
497 psa_key_slot_t *slot,
498 psa_algorithm_t alg,
499 const uint8_t *input,
500 size_t input_length,
501 uint8_t *output,
502 size_t output_size,
503 size_t *output_length )
504{
505#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
506 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
507 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
508 psa_key_attributes_t attributes = {
509 .core = slot->attr
510 };
511
512 switch( location )
513 {
514 case PSA_KEY_LOCATION_LOCAL_STORAGE:
515 /* Key is stored in the slot in export representation, so
516 * cycle through all known transparent accelerators */
517#if defined(PSA_CRYPTO_DRIVER_TEST)
518 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100519 slot->key.data,
520 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200521 alg,
522 input,
523 input_length,
524 output,
525 output_size,
526 output_length );
527 /* Declared with fallback == true */
528 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200529 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200530#endif /* PSA_CRYPTO_DRIVER_TEST */
531 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200532 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200533 /* Add cases for opaque driver here */
534#if defined(PSA_CRYPTO_DRIVER_TEST)
535 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
536 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100537 slot->key.data,
538 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200539 alg,
540 input,
541 input_length,
542 output,
543 output_size,
544 output_length ) );
545#endif /* PSA_CRYPTO_DRIVER_TEST */
546 default:
547 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200548 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200549 }
550#else /* PSA_CRYPTO_DRIVER_PRESENT */
551 (void) slot;
552 (void) alg;
553 (void) input;
554 (void) input_length;
555 (void) output;
556 (void) output_size;
557 (void) output_length;
558
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200559 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200560#endif /* PSA_CRYPTO_DRIVER_PRESENT */
561}
562
563psa_status_t psa_driver_wrapper_cipher_decrypt(
564 psa_key_slot_t *slot,
565 psa_algorithm_t alg,
566 const uint8_t *input,
567 size_t input_length,
568 uint8_t *output,
569 size_t output_size,
570 size_t *output_length )
571{
572#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
573 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
574 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
575 psa_key_attributes_t attributes = {
576 .core = slot->attr
577 };
578
579 switch( location )
580 {
581 case PSA_KEY_LOCATION_LOCAL_STORAGE:
582 /* Key is stored in the slot in export representation, so
583 * cycle through all known transparent accelerators */
584#if defined(PSA_CRYPTO_DRIVER_TEST)
585 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100586 slot->key.data,
587 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200588 alg,
589 input,
590 input_length,
591 output,
592 output_size,
593 output_length );
594 /* Declared with fallback == true */
595 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200596 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200597#endif /* PSA_CRYPTO_DRIVER_TEST */
598 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200599 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200600 /* Add cases for opaque driver here */
601#if defined(PSA_CRYPTO_DRIVER_TEST)
602 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
603 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100604 slot->key.data,
605 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200606 alg,
607 input,
608 input_length,
609 output,
610 output_size,
611 output_length ) );
612#endif /* PSA_CRYPTO_DRIVER_TEST */
613 default:
614 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200615 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200616 }
617#else /* PSA_CRYPTO_DRIVER_PRESENT */
618 (void) slot;
619 (void) alg;
620 (void) input;
621 (void) input_length;
622 (void) output;
623 (void) output_size;
624 (void) output_length;
625
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200626 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200627#endif /* PSA_CRYPTO_DRIVER_PRESENT */
628}
629
630psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200631 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200632 psa_key_slot_t *slot,
633 psa_algorithm_t alg )
634{
635#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
636 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
637 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
638 psa_key_attributes_t attributes = {
639 .core = slot->attr
640 };
641
Steven Cooreman37941cb2020-07-28 18:49:51 +0200642 switch( location )
643 {
644 case PSA_KEY_LOCATION_LOCAL_STORAGE:
645 /* Key is stored in the slot in export representation, so
646 * cycle through all known transparent accelerators */
647#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200648 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
649 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200650 return PSA_ERROR_INSUFFICIENT_MEMORY;
651
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200652 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200653 &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 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200658 if( status == PSA_SUCCESS )
659 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200660 else
661 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200662 mbedtls_platform_zeroize(
663 operation->ctx,
664 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200665 mbedtls_free( operation->ctx );
666 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200667 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200668
669 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200670#endif /* PSA_CRYPTO_DRIVER_TEST */
671 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200672 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200673 /* Add cases for opaque driver here */
674#if defined(PSA_CRYPTO_DRIVER_TEST)
675 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200676 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
677 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200678 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200679
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200680 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200681 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100682 slot->key.data,
683 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200684 alg );
685 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200686 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200687 else
688 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200689 mbedtls_platform_zeroize(
690 operation->ctx,
691 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200692 mbedtls_free( operation->ctx );
693 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200694 }
695
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200696 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200697#endif /* PSA_CRYPTO_DRIVER_TEST */
698 default:
699 /* Key is declared with a lifetime not known to us */
John Durkop714e3a12020-09-29 22:07:04 -0700700 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200701 }
702#else /* PSA_CRYPTO_DRIVER_PRESENT */
703 (void)slot;
704 (void)alg;
705 (void)operation;
706
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200707 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200708#endif /* PSA_CRYPTO_DRIVER_PRESENT */
709}
710
711psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200712 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200713 psa_key_slot_t *slot,
714 psa_algorithm_t alg )
715{
716#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
717 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
718 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
719 psa_key_attributes_t attributes = {
720 .core = slot->attr
721 };
722
Steven Cooreman37941cb2020-07-28 18:49:51 +0200723 switch( location )
724 {
725 case PSA_KEY_LOCATION_LOCAL_STORAGE:
726 /* Key is stored in the slot in export representation, so
727 * cycle through all known transparent accelerators */
728#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200729 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
730 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200731 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200732
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200733 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200734 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100735 slot->key.data,
736 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200737 alg );
738 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200739 if( status == PSA_SUCCESS )
740 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200741 else
742 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200743 mbedtls_platform_zeroize(
744 operation->ctx,
745 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200746 mbedtls_free( operation->ctx );
747 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200748 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200749
750 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200751#endif /* PSA_CRYPTO_DRIVER_TEST */
752 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200753 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200754 /* Add cases for opaque driver here */
755#if defined(PSA_CRYPTO_DRIVER_TEST)
756 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200757 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
758 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200759 return PSA_ERROR_INSUFFICIENT_MEMORY;
760
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200761 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200762 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100763 slot->key.data,
764 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200765 alg );
766 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200767 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200768 else
769 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200770 mbedtls_platform_zeroize(
771 operation->ctx,
772 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200773 mbedtls_free( operation->ctx );
774 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200775 }
776
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200777 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200778#endif /* PSA_CRYPTO_DRIVER_TEST */
779 default:
780 /* Key is declared with a lifetime not known to us */
John Durkop814dca72020-10-05 06:31:12 -0700781 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200782 }
783#else /* PSA_CRYPTO_DRIVER_PRESENT */
784 (void)slot;
785 (void)alg;
786 (void)operation;
787
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200788 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200789#endif /* PSA_CRYPTO_DRIVER_PRESENT */
790}
791
792psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200793 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200794 uint8_t *iv,
795 size_t iv_size,
796 size_t *iv_length )
797{
798#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200799 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200800 {
801#if defined(PSA_CRYPTO_DRIVER_TEST)
802 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200803 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200804 iv,
805 iv_size,
806 iv_length ) );
807#endif /* PSA_CRYPTO_DRIVER_TEST */
808#if defined(PSA_CRYPTO_DRIVER_TEST)
809 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200810 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200811 iv,
812 iv_size,
813 iv_length ) );
814#endif /* PSA_CRYPTO_DRIVER_TEST */
815 default:
816 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200817 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200818 }
819#else /* PSA_CRYPTO_DRIVER_PRESENT */
820 (void) operation;
821 (void) iv;
822 (void) iv_size;
823 (void) iv_length;
824
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200825 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200826#endif /* PSA_CRYPTO_DRIVER_PRESENT */
827}
828
829psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200830 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200831 const uint8_t *iv,
832 size_t iv_length )
833{
834#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200835 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200836 {
837#if defined(PSA_CRYPTO_DRIVER_TEST)
838 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200839 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200840 iv,
841 iv_length ) );
842#endif /* PSA_CRYPTO_DRIVER_TEST */
843#if defined(PSA_CRYPTO_DRIVER_TEST)
844 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200845 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200846 iv,
847 iv_length ) );
848#endif /* PSA_CRYPTO_DRIVER_TEST */
849 default:
850 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200851 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200852 }
853#else /* PSA_CRYPTO_DRIVER_PRESENT */
854 (void) operation;
855 (void) iv;
856 (void) iv_length;
857
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200858 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200859#endif /* PSA_CRYPTO_DRIVER_PRESENT */
860}
861
862psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200863 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200864 const uint8_t *input,
865 size_t input_length,
866 uint8_t *output,
867 size_t output_size,
868 size_t *output_length )
869{
870#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200871 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200872 {
873#if defined(PSA_CRYPTO_DRIVER_TEST)
874 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200875 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200876 input,
877 input_length,
878 output,
879 output_size,
880 output_length ) );
881#endif /* PSA_CRYPTO_DRIVER_TEST */
882#if defined(PSA_CRYPTO_DRIVER_TEST)
883 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200884 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200885 input,
886 input_length,
887 output,
888 output_size,
889 output_length ) );
890#endif /* PSA_CRYPTO_DRIVER_TEST */
891 default:
892 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200893 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200894 }
895#else /* PSA_CRYPTO_DRIVER_PRESENT */
896 (void) operation;
897 (void) input;
898 (void) input_length;
899 (void) output;
900 (void) output_length;
901 (void) output_size;
902
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200903 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200904#endif /* PSA_CRYPTO_DRIVER_PRESENT */
905}
906
907psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200908 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200909 uint8_t *output,
910 size_t output_size,
911 size_t *output_length )
912{
913#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200914 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200915 {
916#if defined(PSA_CRYPTO_DRIVER_TEST)
917 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200918 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200919 output,
920 output_size,
921 output_length ) );
922#endif /* PSA_CRYPTO_DRIVER_TEST */
923#if defined(PSA_CRYPTO_DRIVER_TEST)
924 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200925 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200926 output,
927 output_size,
928 output_length ) );
929#endif /* PSA_CRYPTO_DRIVER_TEST */
930 default:
931 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200932 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200933 }
934#else /* PSA_CRYPTO_DRIVER_PRESENT */
935 (void) operation;
936 (void) output;
937 (void) output_size;
938 (void) output_length;
939
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200940 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200941#endif /* PSA_CRYPTO_DRIVER_PRESENT */
942}
943
944psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200945 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200946{
947#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
948 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200949
950 /* The object has (apparently) been initialized but it is not in use. It's
951 * ok to call abort on such an object, and there's nothing to do. */
952 if( operation->ctx == NULL && operation->id == 0 )
953 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200954
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 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200960 mbedtls_platform_zeroize(
961 operation->ctx,
962 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200963 mbedtls_free( operation->ctx );
964 operation->ctx = NULL;
965 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200966
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200967 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200968#endif /* PSA_CRYPTO_DRIVER_TEST */
969#if defined(PSA_CRYPTO_DRIVER_TEST)
970 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200971 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200972 mbedtls_platform_zeroize(
973 operation->ctx,
974 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200975 mbedtls_free( operation->ctx );
976 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200977 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200978
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200979 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200980#endif /* PSA_CRYPTO_DRIVER_TEST */
981 default:
982 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200983 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200984 }
985#else /* PSA_CRYPTO_DRIVER_PRESENT */
986 (void)operation;
987
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200988 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200989#endif /* PSA_CRYPTO_DRIVER_PRESENT */
990}
991
Steven Cooremancd84cb42020-07-16 20:28:36 +0200992/* End of automatically generated file. */