blob: 2bda2a6cbb4c4b92e0df000edded706d30129aa1 [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,
83 slot->data.se.slot_number,
84 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,
106 slot->data.key.data,
107 slot->data.key.bytes,
108 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,
124 slot->data.key.data,
125 slot->data.key.bytes,
126 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,
175 slot->data.se.slot_number,
176 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,
197 slot->data.key.data,
198 slot->data.key.bytes,
199 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,
214 slot->data.key.data,
215 slot->data.key.bytes,
216 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:
266 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( key_type, key_bits );
267
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 ) ?
283 PSA_KEY_EXPORT_MAX_SIZE( key_type, key_bits ) : 0 );
284 *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(
333 drv_context,
334 slot->data.se.slot_number, attributes,
335 NULL, 0, &pubkey_length ) );
336 }
337#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
338
339 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200340#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200341 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
342 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
343 size_t export_size = 0;
344
345 status = get_expected_key_size( attributes, &export_size );
346 if( status != PSA_SUCCESS )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200347 return( status );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200348
349 slot->data.key.data = mbedtls_calloc(1, export_size);
350 if( slot->data.key.data == NULL )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200352 slot->data.key.bytes = export_size;
353
354 switch( location )
355 {
356 case PSA_KEY_LOCATION_LOCAL_STORAGE:
357 /* Key is stored in the slot in export representation, so
358 * cycle through all known transparent accelerators */
359
360 /* Transparent drivers are limited to generating asymmetric keys */
361 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
362 {
363 status = PSA_ERROR_NOT_SUPPORTED;
364 break;
365 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200366#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200367 status = test_transparent_generate_key( attributes,
368 slot->data.key.data,
369 slot->data.key.bytes,
370 &slot->data.key.bytes );
371 /* Declared with fallback == true */
372 if( status != PSA_ERROR_NOT_SUPPORTED )
373 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200374#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200375 /* Fell through, meaning no accelerator supports this operation */
376 status = PSA_ERROR_NOT_SUPPORTED;
377 break;
378 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200379#if defined(PSA_CRYPTO_DRIVER_TEST)
380 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200381 status = test_opaque_generate_key( attributes,
382 slot->data.key.data,
383 slot->data.key.bytes,
384 &slot->data.key.bytes );
385 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200386#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200387 default:
388 /* Key is declared with a lifetime not known to us */
389 status = PSA_ERROR_INVALID_ARGUMENT;
390 break;
391 }
392
393 if( status != PSA_SUCCESS )
394 {
395 /* free allocated buffer */
396 mbedtls_free( slot->data.key.data );
397 slot->data.key.data = NULL;
398 slot->data.key.bytes = 0;
399 }
400
401 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200402#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200403 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200404#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
405#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200406 (void) attributes;
407 (void) slot;
408
Steven Cooreman56250fd2020-09-04 13:07:15 +0200409 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200410#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200411}
412
Steven Cooreman04524762020-10-13 17:43:44 +0200413psa_status_t psa_driver_wrapper_validate_key( const psa_key_attributes_t *attributes,
414 const uint8_t *data,
415 size_t data_length,
416 size_t *bits )
417{
418#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
419 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
420 /* Try accelerators in turn */
421#if defined(PSA_CRYPTO_DRIVER_TEST)
422 status = test_transparent_validate_key( attributes,
423 data,
424 data_length,
425 bits );
426 /* Declared with fallback == true */
427 if( status != PSA_ERROR_NOT_SUPPORTED )
428 return( status );
429#endif /* PSA_CRYPTO_DRIVER_TEST */
430
431 return( PSA_ERROR_NOT_SUPPORTED );
432#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
433 (void) attributes;
434 (void) data;
435 (void) data_length;
436 (void) bits;
437 return( PSA_ERROR_NOT_SUPPORTED );
438#endif /* PSA_CRYPTO_DRIVER_PRESENT */
439}
440
Steven Cooreman37941cb2020-07-28 18:49:51 +0200441/*
442 * Cipher functions
443 */
444psa_status_t psa_driver_wrapper_cipher_encrypt(
445 psa_key_slot_t *slot,
446 psa_algorithm_t alg,
447 const uint8_t *input,
448 size_t input_length,
449 uint8_t *output,
450 size_t output_size,
451 size_t *output_length )
452{
453#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
454 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
455 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
456 psa_key_attributes_t attributes = {
457 .core = slot->attr
458 };
459
460 switch( location )
461 {
462 case PSA_KEY_LOCATION_LOCAL_STORAGE:
463 /* Key is stored in the slot in export representation, so
464 * cycle through all known transparent accelerators */
465#if defined(PSA_CRYPTO_DRIVER_TEST)
466 status = test_transparent_cipher_encrypt( &attributes,
467 slot->data.key.data,
468 slot->data.key.bytes,
469 alg,
470 input,
471 input_length,
472 output,
473 output_size,
474 output_length );
475 /* Declared with fallback == true */
476 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200477 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200478#endif /* PSA_CRYPTO_DRIVER_TEST */
479 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200480 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200481 /* Add cases for opaque driver here */
482#if defined(PSA_CRYPTO_DRIVER_TEST)
483 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
484 return( test_opaque_cipher_encrypt( &attributes,
485 slot->data.key.data,
486 slot->data.key.bytes,
487 alg,
488 input,
489 input_length,
490 output,
491 output_size,
492 output_length ) );
493#endif /* PSA_CRYPTO_DRIVER_TEST */
494 default:
495 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200496 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200497 }
498#else /* PSA_CRYPTO_DRIVER_PRESENT */
499 (void) slot;
500 (void) alg;
501 (void) input;
502 (void) input_length;
503 (void) output;
504 (void) output_size;
505 (void) output_length;
506
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200507 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200508#endif /* PSA_CRYPTO_DRIVER_PRESENT */
509}
510
511psa_status_t psa_driver_wrapper_cipher_decrypt(
512 psa_key_slot_t *slot,
513 psa_algorithm_t alg,
514 const uint8_t *input,
515 size_t input_length,
516 uint8_t *output,
517 size_t output_size,
518 size_t *output_length )
519{
520#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
521 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
522 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
523 psa_key_attributes_t attributes = {
524 .core = slot->attr
525 };
526
527 switch( location )
528 {
529 case PSA_KEY_LOCATION_LOCAL_STORAGE:
530 /* Key is stored in the slot in export representation, so
531 * cycle through all known transparent accelerators */
532#if defined(PSA_CRYPTO_DRIVER_TEST)
533 status = test_transparent_cipher_decrypt( &attributes,
534 slot->data.key.data,
535 slot->data.key.bytes,
536 alg,
537 input,
538 input_length,
539 output,
540 output_size,
541 output_length );
542 /* Declared with fallback == true */
543 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200544 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200545#endif /* PSA_CRYPTO_DRIVER_TEST */
546 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200547 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200548 /* Add cases for opaque driver here */
549#if defined(PSA_CRYPTO_DRIVER_TEST)
550 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
551 return( test_opaque_cipher_decrypt( &attributes,
552 slot->data.key.data,
553 slot->data.key.bytes,
554 alg,
555 input,
556 input_length,
557 output,
558 output_size,
559 output_length ) );
560#endif /* PSA_CRYPTO_DRIVER_TEST */
561 default:
562 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200563 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200564 }
565#else /* PSA_CRYPTO_DRIVER_PRESENT */
566 (void) slot;
567 (void) alg;
568 (void) input;
569 (void) input_length;
570 (void) output;
571 (void) output_size;
572 (void) output_length;
573
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200574 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200575#endif /* PSA_CRYPTO_DRIVER_PRESENT */
576}
577
578psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200579 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200580 psa_key_slot_t *slot,
581 psa_algorithm_t alg )
582{
583#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
584 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
585 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
586 psa_key_attributes_t attributes = {
587 .core = slot->attr
588 };
589
Steven Cooreman37941cb2020-07-28 18:49:51 +0200590 switch( location )
591 {
592 case PSA_KEY_LOCATION_LOCAL_STORAGE:
593 /* Key is stored in the slot in export representation, so
594 * cycle through all known transparent accelerators */
595#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200596 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
597 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200598 return PSA_ERROR_INSUFFICIENT_MEMORY;
599
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200600 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200601 &attributes,
602 slot->data.key.data,
603 slot->data.key.bytes,
604 alg );
605 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200606 if( status == PSA_SUCCESS )
607 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200608 else
609 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200610 mbedtls_platform_zeroize(
611 operation->ctx,
612 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200613 mbedtls_free( operation->ctx );
614 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200615 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200616
617 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200618#endif /* PSA_CRYPTO_DRIVER_TEST */
619 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200620 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200621 /* Add cases for opaque driver here */
622#if defined(PSA_CRYPTO_DRIVER_TEST)
623 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200624 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
625 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200626 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200627
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200628 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200629 &attributes,
630 slot->data.key.data,
631 slot->data.key.bytes,
632 alg );
633 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200634 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200635 else
636 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200637 mbedtls_platform_zeroize(
638 operation->ctx,
639 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200640 mbedtls_free( operation->ctx );
641 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200642 }
643
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200644 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200645#endif /* PSA_CRYPTO_DRIVER_TEST */
646 default:
647 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200648 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200649 }
650#else /* PSA_CRYPTO_DRIVER_PRESENT */
651 (void)slot;
652 (void)alg;
653 (void)operation;
654
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200655 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200656#endif /* PSA_CRYPTO_DRIVER_PRESENT */
657}
658
659psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200660 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200661 psa_key_slot_t *slot,
662 psa_algorithm_t alg )
663{
664#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
665 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
666 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
667 psa_key_attributes_t attributes = {
668 .core = slot->attr
669 };
670
Steven Cooreman37941cb2020-07-28 18:49:51 +0200671 switch( location )
672 {
673 case PSA_KEY_LOCATION_LOCAL_STORAGE:
674 /* Key is stored in the slot in export representation, so
675 * cycle through all known transparent accelerators */
676#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200677 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
678 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200679 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200680
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200681 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200682 &attributes,
683 slot->data.key.data,
684 slot->data.key.bytes,
685 alg );
686 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200687 if( status == PSA_SUCCESS )
688 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200689 else
690 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200691 mbedtls_platform_zeroize(
692 operation->ctx,
693 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200694 mbedtls_free( operation->ctx );
695 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200696 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200697
698 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200699#endif /* PSA_CRYPTO_DRIVER_TEST */
700 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200701 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200702 /* Add cases for opaque driver here */
703#if defined(PSA_CRYPTO_DRIVER_TEST)
704 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200705 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
706 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200707 return PSA_ERROR_INSUFFICIENT_MEMORY;
708
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200709 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200710 &attributes,
711 slot->data.key.data,
712 slot->data.key.bytes,
713 alg );
714 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200715 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200716 else
717 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200718 mbedtls_platform_zeroize(
719 operation->ctx,
720 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200721 mbedtls_free( operation->ctx );
722 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200723 }
724
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200725 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200726#endif /* PSA_CRYPTO_DRIVER_TEST */
727 default:
728 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200729 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200730 }
731#else /* PSA_CRYPTO_DRIVER_PRESENT */
732 (void)slot;
733 (void)alg;
734 (void)operation;
735
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200736 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200737#endif /* PSA_CRYPTO_DRIVER_PRESENT */
738}
739
740psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200741 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200742 uint8_t *iv,
743 size_t iv_size,
744 size_t *iv_length )
745{
746#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200747 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200748 {
749#if defined(PSA_CRYPTO_DRIVER_TEST)
750 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200751 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200752 iv,
753 iv_size,
754 iv_length ) );
755#endif /* PSA_CRYPTO_DRIVER_TEST */
756#if defined(PSA_CRYPTO_DRIVER_TEST)
757 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200758 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200759 iv,
760 iv_size,
761 iv_length ) );
762#endif /* PSA_CRYPTO_DRIVER_TEST */
763 default:
764 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200765 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200766 }
767#else /* PSA_CRYPTO_DRIVER_PRESENT */
768 (void) operation;
769 (void) iv;
770 (void) iv_size;
771 (void) iv_length;
772
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200773 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200774#endif /* PSA_CRYPTO_DRIVER_PRESENT */
775}
776
777psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200778 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200779 const uint8_t *iv,
780 size_t iv_length )
781{
782#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200783 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200784 {
785#if defined(PSA_CRYPTO_DRIVER_TEST)
786 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200787 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200788 iv,
789 iv_length ) );
790#endif /* PSA_CRYPTO_DRIVER_TEST */
791#if defined(PSA_CRYPTO_DRIVER_TEST)
792 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200793 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200794 iv,
795 iv_length ) );
796#endif /* PSA_CRYPTO_DRIVER_TEST */
797 default:
798 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200799 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200800 }
801#else /* PSA_CRYPTO_DRIVER_PRESENT */
802 (void) operation;
803 (void) iv;
804 (void) iv_length;
805
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200806 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200807#endif /* PSA_CRYPTO_DRIVER_PRESENT */
808}
809
810psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200811 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200812 const uint8_t *input,
813 size_t input_length,
814 uint8_t *output,
815 size_t output_size,
816 size_t *output_length )
817{
818#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200819 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200820 {
821#if defined(PSA_CRYPTO_DRIVER_TEST)
822 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200823 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200824 input,
825 input_length,
826 output,
827 output_size,
828 output_length ) );
829#endif /* PSA_CRYPTO_DRIVER_TEST */
830#if defined(PSA_CRYPTO_DRIVER_TEST)
831 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200832 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200833 input,
834 input_length,
835 output,
836 output_size,
837 output_length ) );
838#endif /* PSA_CRYPTO_DRIVER_TEST */
839 default:
840 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200841 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200842 }
843#else /* PSA_CRYPTO_DRIVER_PRESENT */
844 (void) operation;
845 (void) input;
846 (void) input_length;
847 (void) output;
848 (void) output_length;
849 (void) output_size;
850
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200851 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200852#endif /* PSA_CRYPTO_DRIVER_PRESENT */
853}
854
855psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200856 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200857 uint8_t *output,
858 size_t output_size,
859 size_t *output_length )
860{
861#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200862 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200863 {
864#if defined(PSA_CRYPTO_DRIVER_TEST)
865 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200866 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200867 output,
868 output_size,
869 output_length ) );
870#endif /* PSA_CRYPTO_DRIVER_TEST */
871#if defined(PSA_CRYPTO_DRIVER_TEST)
872 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200873 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200874 output,
875 output_size,
876 output_length ) );
877#endif /* PSA_CRYPTO_DRIVER_TEST */
878 default:
879 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200880 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200881 }
882#else /* PSA_CRYPTO_DRIVER_PRESENT */
883 (void) operation;
884 (void) output;
885 (void) output_size;
886 (void) output_length;
887
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200888 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200889#endif /* PSA_CRYPTO_DRIVER_PRESENT */
890}
891
892psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200893 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200894{
895#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
896 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200897
898 /* The object has (apparently) been initialized but it is not in use. It's
899 * ok to call abort on such an object, and there's nothing to do. */
900 if( operation->ctx == NULL && operation->id == 0 )
901 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200902
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200903 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200904 {
905#if defined(PSA_CRYPTO_DRIVER_TEST)
906 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200907 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200908 mbedtls_platform_zeroize(
909 operation->ctx,
910 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200911 mbedtls_free( operation->ctx );
912 operation->ctx = NULL;
913 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200914
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200915 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200916#endif /* PSA_CRYPTO_DRIVER_TEST */
917#if defined(PSA_CRYPTO_DRIVER_TEST)
918 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200919 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200920 mbedtls_platform_zeroize(
921 operation->ctx,
922 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200923 mbedtls_free( operation->ctx );
924 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200925 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200926
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200927 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200928#endif /* PSA_CRYPTO_DRIVER_TEST */
929 default:
930 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200931 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200932 }
933#else /* PSA_CRYPTO_DRIVER_PRESENT */
934 (void)operation;
935
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200936 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200937#endif /* PSA_CRYPTO_DRIVER_PRESENT */
938}
939
Steven Cooremancd84cb42020-07-16 20:28:36 +0200940/* End of automatically generated file. */