blob: f19f559202757a67c1c3bddab94d0234ca9e23df [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 Cooreman37941cb2020-07-28 18:49:51 +0200413/*
414 * Cipher functions
415 */
416psa_status_t psa_driver_wrapper_cipher_encrypt(
417 psa_key_slot_t *slot,
418 psa_algorithm_t alg,
419 const uint8_t *input,
420 size_t input_length,
421 uint8_t *output,
422 size_t output_size,
423 size_t *output_length )
424{
425#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
426 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
427 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
428 psa_key_attributes_t attributes = {
429 .core = slot->attr
430 };
431
432 switch( location )
433 {
434 case PSA_KEY_LOCATION_LOCAL_STORAGE:
435 /* Key is stored in the slot in export representation, so
436 * cycle through all known transparent accelerators */
437#if defined(PSA_CRYPTO_DRIVER_TEST)
438 status = test_transparent_cipher_encrypt( &attributes,
439 slot->data.key.data,
440 slot->data.key.bytes,
441 alg,
442 input,
443 input_length,
444 output,
445 output_size,
446 output_length );
447 /* Declared with fallback == true */
448 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200449 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200450#endif /* PSA_CRYPTO_DRIVER_TEST */
451 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200452 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200453 /* Add cases for opaque driver here */
454#if defined(PSA_CRYPTO_DRIVER_TEST)
455 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
456 return( test_opaque_cipher_encrypt( &attributes,
457 slot->data.key.data,
458 slot->data.key.bytes,
459 alg,
460 input,
461 input_length,
462 output,
463 output_size,
464 output_length ) );
465#endif /* PSA_CRYPTO_DRIVER_TEST */
466 default:
467 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200468 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200469 }
470#else /* PSA_CRYPTO_DRIVER_PRESENT */
471 (void) slot;
472 (void) alg;
473 (void) input;
474 (void) input_length;
475 (void) output;
476 (void) output_size;
477 (void) output_length;
478
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200479 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200480#endif /* PSA_CRYPTO_DRIVER_PRESENT */
481}
482
483psa_status_t psa_driver_wrapper_cipher_decrypt(
484 psa_key_slot_t *slot,
485 psa_algorithm_t alg,
486 const uint8_t *input,
487 size_t input_length,
488 uint8_t *output,
489 size_t output_size,
490 size_t *output_length )
491{
492#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
493 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
494 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
495 psa_key_attributes_t attributes = {
496 .core = slot->attr
497 };
498
499 switch( location )
500 {
501 case PSA_KEY_LOCATION_LOCAL_STORAGE:
502 /* Key is stored in the slot in export representation, so
503 * cycle through all known transparent accelerators */
504#if defined(PSA_CRYPTO_DRIVER_TEST)
505 status = test_transparent_cipher_decrypt( &attributes,
506 slot->data.key.data,
507 slot->data.key.bytes,
508 alg,
509 input,
510 input_length,
511 output,
512 output_size,
513 output_length );
514 /* Declared with fallback == true */
515 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200516 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200517#endif /* PSA_CRYPTO_DRIVER_TEST */
518 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200519 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200520 /* Add cases for opaque driver here */
521#if defined(PSA_CRYPTO_DRIVER_TEST)
522 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
523 return( test_opaque_cipher_decrypt( &attributes,
524 slot->data.key.data,
525 slot->data.key.bytes,
526 alg,
527 input,
528 input_length,
529 output,
530 output_size,
531 output_length ) );
532#endif /* PSA_CRYPTO_DRIVER_TEST */
533 default:
534 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200535 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200536 }
537#else /* PSA_CRYPTO_DRIVER_PRESENT */
538 (void) slot;
539 (void) alg;
540 (void) input;
541 (void) input_length;
542 (void) output;
543 (void) output_size;
544 (void) output_length;
545
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200546 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200547#endif /* PSA_CRYPTO_DRIVER_PRESENT */
548}
549
550psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200551 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200552 psa_key_slot_t *slot,
553 psa_algorithm_t alg )
554{
555#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
556 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
557 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
558 psa_key_attributes_t attributes = {
559 .core = slot->attr
560 };
561
Steven Cooreman37941cb2020-07-28 18:49:51 +0200562 switch( location )
563 {
564 case PSA_KEY_LOCATION_LOCAL_STORAGE:
565 /* Key is stored in the slot in export representation, so
566 * cycle through all known transparent accelerators */
567#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200568 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
569 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200570 return PSA_ERROR_INSUFFICIENT_MEMORY;
571
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200572 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200573 &attributes,
574 slot->data.key.data,
575 slot->data.key.bytes,
576 alg );
577 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200578 if( status == PSA_SUCCESS )
579 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200580 else
581 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200582 mbedtls_platform_zeroize(
583 operation->ctx,
584 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200585 mbedtls_free( operation->ctx );
586 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200587 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200588
589 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200590#endif /* PSA_CRYPTO_DRIVER_TEST */
591 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200592 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200593 /* Add cases for opaque driver here */
594#if defined(PSA_CRYPTO_DRIVER_TEST)
595 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200596 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
597 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200598 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200599
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200600 status = test_opaque_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 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200606 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200607 else
608 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200609 mbedtls_platform_zeroize(
610 operation->ctx,
611 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200612 mbedtls_free( operation->ctx );
613 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200614 }
615
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200616 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200617#endif /* PSA_CRYPTO_DRIVER_TEST */
618 default:
619 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200620 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200621 }
622#else /* PSA_CRYPTO_DRIVER_PRESENT */
623 (void)slot;
624 (void)alg;
625 (void)operation;
626
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200627 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200628#endif /* PSA_CRYPTO_DRIVER_PRESENT */
629}
630
631psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200632 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200633 psa_key_slot_t *slot,
634 psa_algorithm_t alg )
635{
636#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
637 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
638 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
639 psa_key_attributes_t attributes = {
640 .core = slot->attr
641 };
642
Steven Cooreman37941cb2020-07-28 18:49:51 +0200643 switch( location )
644 {
645 case PSA_KEY_LOCATION_LOCAL_STORAGE:
646 /* Key is stored in the slot in export representation, so
647 * cycle through all known transparent accelerators */
648#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200649 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
650 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200651 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200652
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200653 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200654 &attributes,
655 slot->data.key.data,
656 slot->data.key.bytes,
657 alg );
658 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200659 if( status == PSA_SUCCESS )
660 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200661 else
662 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200663 mbedtls_platform_zeroize(
664 operation->ctx,
665 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200666 mbedtls_free( operation->ctx );
667 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200668 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200669
670 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200671#endif /* PSA_CRYPTO_DRIVER_TEST */
672 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200673 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200674 /* Add cases for opaque driver here */
675#if defined(PSA_CRYPTO_DRIVER_TEST)
676 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200677 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
678 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200679 return PSA_ERROR_INSUFFICIENT_MEMORY;
680
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200681 status = test_opaque_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 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200687 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200688 else
689 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200690 mbedtls_platform_zeroize(
691 operation->ctx,
692 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200693 mbedtls_free( operation->ctx );
694 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200695 }
696
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200697 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200698#endif /* PSA_CRYPTO_DRIVER_TEST */
699 default:
700 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200701 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200702 }
703#else /* PSA_CRYPTO_DRIVER_PRESENT */
704 (void)slot;
705 (void)alg;
706 (void)operation;
707
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200708 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200709#endif /* PSA_CRYPTO_DRIVER_PRESENT */
710}
711
712psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200713 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200714 uint8_t *iv,
715 size_t iv_size,
716 size_t *iv_length )
717{
718#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200719 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200720 {
721#if defined(PSA_CRYPTO_DRIVER_TEST)
722 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200723 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200724 iv,
725 iv_size,
726 iv_length ) );
727#endif /* PSA_CRYPTO_DRIVER_TEST */
728#if defined(PSA_CRYPTO_DRIVER_TEST)
729 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200730 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200731 iv,
732 iv_size,
733 iv_length ) );
734#endif /* PSA_CRYPTO_DRIVER_TEST */
735 default:
736 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200737 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200738 }
739#else /* PSA_CRYPTO_DRIVER_PRESENT */
740 (void) operation;
741 (void) iv;
742 (void) iv_size;
743 (void) iv_length;
744
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200745 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200746#endif /* PSA_CRYPTO_DRIVER_PRESENT */
747}
748
749psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200750 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200751 const uint8_t *iv,
752 size_t iv_length )
753{
754#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200755 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200756 {
757#if defined(PSA_CRYPTO_DRIVER_TEST)
758 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200759 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200760 iv,
761 iv_length ) );
762#endif /* PSA_CRYPTO_DRIVER_TEST */
763#if defined(PSA_CRYPTO_DRIVER_TEST)
764 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200765 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200766 iv,
767 iv_length ) );
768#endif /* PSA_CRYPTO_DRIVER_TEST */
769 default:
770 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200771 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200772 }
773#else /* PSA_CRYPTO_DRIVER_PRESENT */
774 (void) operation;
775 (void) iv;
776 (void) iv_length;
777
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200778 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200779#endif /* PSA_CRYPTO_DRIVER_PRESENT */
780}
781
782psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200783 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200784 const uint8_t *input,
785 size_t input_length,
786 uint8_t *output,
787 size_t output_size,
788 size_t *output_length )
789{
790#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200791 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200792 {
793#if defined(PSA_CRYPTO_DRIVER_TEST)
794 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200795 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200796 input,
797 input_length,
798 output,
799 output_size,
800 output_length ) );
801#endif /* PSA_CRYPTO_DRIVER_TEST */
802#if defined(PSA_CRYPTO_DRIVER_TEST)
803 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200804 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200805 input,
806 input_length,
807 output,
808 output_size,
809 output_length ) );
810#endif /* PSA_CRYPTO_DRIVER_TEST */
811 default:
812 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200813 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200814 }
815#else /* PSA_CRYPTO_DRIVER_PRESENT */
816 (void) operation;
817 (void) input;
818 (void) input_length;
819 (void) output;
820 (void) output_length;
821 (void) output_size;
822
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200823 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200824#endif /* PSA_CRYPTO_DRIVER_PRESENT */
825}
826
827psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200828 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200829 uint8_t *output,
830 size_t output_size,
831 size_t *output_length )
832{
833#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200834 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200835 {
836#if defined(PSA_CRYPTO_DRIVER_TEST)
837 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200838 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200839 output,
840 output_size,
841 output_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_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200846 output,
847 output_size,
848 output_length ) );
849#endif /* PSA_CRYPTO_DRIVER_TEST */
850 default:
851 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200852 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200853 }
854#else /* PSA_CRYPTO_DRIVER_PRESENT */
855 (void) operation;
856 (void) output;
857 (void) output_size;
858 (void) output_length;
859
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200860 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200861#endif /* PSA_CRYPTO_DRIVER_PRESENT */
862}
863
864psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200865 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200866{
867#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
868 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200869
870 /* The object has (apparently) been initialized but it is not in use. It's
871 * ok to call abort on such an object, and there's nothing to do. */
872 if( operation->ctx == NULL && operation->id == 0 )
873 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200874
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200875 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200876 {
877#if defined(PSA_CRYPTO_DRIVER_TEST)
878 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200879 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200880 mbedtls_platform_zeroize(
881 operation->ctx,
882 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200883 mbedtls_free( operation->ctx );
884 operation->ctx = NULL;
885 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200886
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200887 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200888#endif /* PSA_CRYPTO_DRIVER_TEST */
889#if defined(PSA_CRYPTO_DRIVER_TEST)
890 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200891 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200892 mbedtls_platform_zeroize(
893 operation->ctx,
894 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200895 mbedtls_free( operation->ctx );
896 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200897 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200898
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200899 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200900#endif /* PSA_CRYPTO_DRIVER_TEST */
901 default:
902 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200903 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200904 }
905#else /* PSA_CRYPTO_DRIVER_PRESENT */
906 (void)operation;
907
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200908 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200909#endif /* PSA_CRYPTO_DRIVER_PRESENT */
910}
911
Steven Cooremancd84cb42020-07-16 20:28:36 +0200912/* End of automatically generated file. */