blob: f905ea4f930d234a0cda905fc0c9e338fe8076d0 [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 {
282 *expected_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
283 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
284 }
285 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( attributes->core.type ) )
286 {
287 *expected_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
288 + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
289 * ( ( key_bits + 7 ) / 8 );
290 }
291 else
292 {
293 return( PSA_ERROR_NOT_SUPPORTED );
294 }
295 return( PSA_SUCCESS );
296#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
297#endif /* PSA_CRYPTO_DRIVER_TEST */
298
299 default:
Steven Cooreman56250fd2020-09-04 13:07:15 +0200300 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200301 }
302}
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200303#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200304
Steven Cooreman55ae2172020-07-17 19:46:15 +0200305psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes,
306 psa_key_slot_t *slot )
307{
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200308#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200309 /* Try dynamically-registered SE interface first */
310#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
311 const psa_drv_se_t *drv;
312 psa_drv_se_context_t *drv_context;
313
314 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
315 {
316 size_t pubkey_length = 0; /* We don't support this feature yet */
317 if( drv->key_management == NULL ||
318 drv->key_management->p_generate == NULL )
319 {
320 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200321 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200322 }
323 return( drv->key_management->p_generate(
324 drv_context,
325 slot->data.se.slot_number, attributes,
326 NULL, 0, &pubkey_length ) );
327 }
328#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
329
330 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200331#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200332 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
333 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
334 size_t export_size = 0;
335
336 status = get_expected_key_size( attributes, &export_size );
337 if( status != PSA_SUCCESS )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200338 return( status );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200339
340 slot->data.key.data = mbedtls_calloc(1, export_size);
341 if( slot->data.key.data == NULL )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200342 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200343 slot->data.key.bytes = export_size;
344
345 switch( location )
346 {
347 case PSA_KEY_LOCATION_LOCAL_STORAGE:
348 /* Key is stored in the slot in export representation, so
349 * cycle through all known transparent accelerators */
350
351 /* Transparent drivers are limited to generating asymmetric keys */
352 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
353 {
354 status = PSA_ERROR_NOT_SUPPORTED;
355 break;
356 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200357#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200358 status = test_transparent_generate_key( attributes,
359 slot->data.key.data,
360 slot->data.key.bytes,
361 &slot->data.key.bytes );
362 /* Declared with fallback == true */
363 if( status != PSA_ERROR_NOT_SUPPORTED )
364 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200365#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200366 /* Fell through, meaning no accelerator supports this operation */
367 status = PSA_ERROR_NOT_SUPPORTED;
368 break;
369 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200370#if defined(PSA_CRYPTO_DRIVER_TEST)
371 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200372 status = test_opaque_generate_key( attributes,
373 slot->data.key.data,
374 slot->data.key.bytes,
375 &slot->data.key.bytes );
376 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200377#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200378 default:
379 /* Key is declared with a lifetime not known to us */
380 status = PSA_ERROR_INVALID_ARGUMENT;
381 break;
382 }
383
384 if( status != PSA_SUCCESS )
385 {
386 /* free allocated buffer */
387 mbedtls_free( slot->data.key.data );
388 slot->data.key.data = NULL;
389 slot->data.key.bytes = 0;
390 }
391
392 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200393#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200394 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200395#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
396#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200397 (void) attributes;
398 (void) slot;
399
Steven Cooreman56250fd2020-09-04 13:07:15 +0200400 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200401#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200402}
403
Steven Cooreman37941cb2020-07-28 18:49:51 +0200404/*
405 * Cipher functions
406 */
407psa_status_t psa_driver_wrapper_cipher_encrypt(
408 psa_key_slot_t *slot,
409 psa_algorithm_t alg,
410 const uint8_t *input,
411 size_t input_length,
412 uint8_t *output,
413 size_t output_size,
414 size_t *output_length )
415{
416#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
417 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
418 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
419 psa_key_attributes_t attributes = {
420 .core = slot->attr
421 };
422
423 switch( location )
424 {
425 case PSA_KEY_LOCATION_LOCAL_STORAGE:
426 /* Key is stored in the slot in export representation, so
427 * cycle through all known transparent accelerators */
428#if defined(PSA_CRYPTO_DRIVER_TEST)
429 status = test_transparent_cipher_encrypt( &attributes,
430 slot->data.key.data,
431 slot->data.key.bytes,
432 alg,
433 input,
434 input_length,
435 output,
436 output_size,
437 output_length );
438 /* Declared with fallback == true */
439 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200440 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200441#endif /* PSA_CRYPTO_DRIVER_TEST */
442 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200443 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200444 /* Add cases for opaque driver here */
445#if defined(PSA_CRYPTO_DRIVER_TEST)
446 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
447 return( test_opaque_cipher_encrypt( &attributes,
448 slot->data.key.data,
449 slot->data.key.bytes,
450 alg,
451 input,
452 input_length,
453 output,
454 output_size,
455 output_length ) );
456#endif /* PSA_CRYPTO_DRIVER_TEST */
457 default:
458 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200459 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200460 }
461#else /* PSA_CRYPTO_DRIVER_PRESENT */
462 (void) slot;
463 (void) alg;
464 (void) input;
465 (void) input_length;
466 (void) output;
467 (void) output_size;
468 (void) output_length;
469
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200470 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200471#endif /* PSA_CRYPTO_DRIVER_PRESENT */
472}
473
474psa_status_t psa_driver_wrapper_cipher_decrypt(
475 psa_key_slot_t *slot,
476 psa_algorithm_t alg,
477 const uint8_t *input,
478 size_t input_length,
479 uint8_t *output,
480 size_t output_size,
481 size_t *output_length )
482{
483#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
484 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
485 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
486 psa_key_attributes_t attributes = {
487 .core = slot->attr
488 };
489
490 switch( location )
491 {
492 case PSA_KEY_LOCATION_LOCAL_STORAGE:
493 /* Key is stored in the slot in export representation, so
494 * cycle through all known transparent accelerators */
495#if defined(PSA_CRYPTO_DRIVER_TEST)
496 status = test_transparent_cipher_decrypt( &attributes,
497 slot->data.key.data,
498 slot->data.key.bytes,
499 alg,
500 input,
501 input_length,
502 output,
503 output_size,
504 output_length );
505 /* Declared with fallback == true */
506 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200507 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200508#endif /* PSA_CRYPTO_DRIVER_TEST */
509 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200510 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200511 /* Add cases for opaque driver here */
512#if defined(PSA_CRYPTO_DRIVER_TEST)
513 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
514 return( test_opaque_cipher_decrypt( &attributes,
515 slot->data.key.data,
516 slot->data.key.bytes,
517 alg,
518 input,
519 input_length,
520 output,
521 output_size,
522 output_length ) );
523#endif /* PSA_CRYPTO_DRIVER_TEST */
524 default:
525 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200526 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200527 }
528#else /* PSA_CRYPTO_DRIVER_PRESENT */
529 (void) slot;
530 (void) alg;
531 (void) input;
532 (void) input_length;
533 (void) output;
534 (void) output_size;
535 (void) output_length;
536
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200537 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200538#endif /* PSA_CRYPTO_DRIVER_PRESENT */
539}
540
541psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200542 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200543 psa_key_slot_t *slot,
544 psa_algorithm_t alg )
545{
546#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
547 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
548 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
549 psa_key_attributes_t attributes = {
550 .core = slot->attr
551 };
552
Steven Cooreman37941cb2020-07-28 18:49:51 +0200553 switch( location )
554 {
555 case PSA_KEY_LOCATION_LOCAL_STORAGE:
556 /* Key is stored in the slot in export representation, so
557 * cycle through all known transparent accelerators */
558#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200559 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
560 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200561 return PSA_ERROR_INSUFFICIENT_MEMORY;
562
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200563 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200564 &attributes,
565 slot->data.key.data,
566 slot->data.key.bytes,
567 alg );
568 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200569 if( status == PSA_SUCCESS )
570 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200571 else
572 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200573 mbedtls_platform_zeroize(
574 operation->ctx,
575 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200576 mbedtls_free( operation->ctx );
577 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200578 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200579
580 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200581#endif /* PSA_CRYPTO_DRIVER_TEST */
582 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200583 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200584 /* Add cases for opaque driver here */
585#if defined(PSA_CRYPTO_DRIVER_TEST)
586 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200587 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
588 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200589 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200590
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200591 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200592 &attributes,
593 slot->data.key.data,
594 slot->data.key.bytes,
595 alg );
596 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200597 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200598 else
599 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200600 mbedtls_platform_zeroize(
601 operation->ctx,
602 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200603 mbedtls_free( operation->ctx );
604 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200605 }
606
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200607 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200608#endif /* PSA_CRYPTO_DRIVER_TEST */
609 default:
610 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200611 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200612 }
613#else /* PSA_CRYPTO_DRIVER_PRESENT */
614 (void)slot;
615 (void)alg;
616 (void)operation;
617
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200618 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200619#endif /* PSA_CRYPTO_DRIVER_PRESENT */
620}
621
622psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200623 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200624 psa_key_slot_t *slot,
625 psa_algorithm_t alg )
626{
627#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
628 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
629 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
630 psa_key_attributes_t attributes = {
631 .core = slot->attr
632 };
633
Steven Cooreman37941cb2020-07-28 18:49:51 +0200634 switch( location )
635 {
636 case PSA_KEY_LOCATION_LOCAL_STORAGE:
637 /* Key is stored in the slot in export representation, so
638 * cycle through all known transparent accelerators */
639#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200640 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
641 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200642 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200643
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200644 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200645 &attributes,
646 slot->data.key.data,
647 slot->data.key.bytes,
648 alg );
649 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200650 if( status == PSA_SUCCESS )
651 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200652 else
653 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200654 mbedtls_platform_zeroize(
655 operation->ctx,
656 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200657 mbedtls_free( operation->ctx );
658 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200659 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200660
661 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200662#endif /* PSA_CRYPTO_DRIVER_TEST */
663 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200664 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200665 /* Add cases for opaque driver here */
666#if defined(PSA_CRYPTO_DRIVER_TEST)
667 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200668 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
669 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200670 return PSA_ERROR_INSUFFICIENT_MEMORY;
671
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200672 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200673 &attributes,
674 slot->data.key.data,
675 slot->data.key.bytes,
676 alg );
677 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200678 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200679 else
680 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200681 mbedtls_platform_zeroize(
682 operation->ctx,
683 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200684 mbedtls_free( operation->ctx );
685 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200686 }
687
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200688 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200689#endif /* PSA_CRYPTO_DRIVER_TEST */
690 default:
691 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200692 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200693 }
694#else /* PSA_CRYPTO_DRIVER_PRESENT */
695 (void)slot;
696 (void)alg;
697 (void)operation;
698
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200699 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200700#endif /* PSA_CRYPTO_DRIVER_PRESENT */
701}
702
703psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200704 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200705 uint8_t *iv,
706 size_t iv_size,
707 size_t *iv_length )
708{
709#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200710 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200711 {
712#if defined(PSA_CRYPTO_DRIVER_TEST)
713 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200714 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200715 iv,
716 iv_size,
717 iv_length ) );
718#endif /* PSA_CRYPTO_DRIVER_TEST */
719#if defined(PSA_CRYPTO_DRIVER_TEST)
720 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200721 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200722 iv,
723 iv_size,
724 iv_length ) );
725#endif /* PSA_CRYPTO_DRIVER_TEST */
726 default:
727 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200728 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200729 }
730#else /* PSA_CRYPTO_DRIVER_PRESENT */
731 (void) operation;
732 (void) iv;
733 (void) iv_size;
734 (void) iv_length;
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_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200741 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200742 const uint8_t *iv,
743 size_t iv_length )
744{
745#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200746 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200747 {
748#if defined(PSA_CRYPTO_DRIVER_TEST)
749 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200750 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200751 iv,
752 iv_length ) );
753#endif /* PSA_CRYPTO_DRIVER_TEST */
754#if defined(PSA_CRYPTO_DRIVER_TEST)
755 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200756 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200757 iv,
758 iv_length ) );
759#endif /* PSA_CRYPTO_DRIVER_TEST */
760 default:
761 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200762 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200763 }
764#else /* PSA_CRYPTO_DRIVER_PRESENT */
765 (void) operation;
766 (void) iv;
767 (void) iv_length;
768
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200769 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200770#endif /* PSA_CRYPTO_DRIVER_PRESENT */
771}
772
773psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200774 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200775 const uint8_t *input,
776 size_t input_length,
777 uint8_t *output,
778 size_t output_size,
779 size_t *output_length )
780{
781#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200782 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200783 {
784#if defined(PSA_CRYPTO_DRIVER_TEST)
785 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200786 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200787 input,
788 input_length,
789 output,
790 output_size,
791 output_length ) );
792#endif /* PSA_CRYPTO_DRIVER_TEST */
793#if defined(PSA_CRYPTO_DRIVER_TEST)
794 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200795 return( test_opaque_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 default:
803 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200804 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200805 }
806#else /* PSA_CRYPTO_DRIVER_PRESENT */
807 (void) operation;
808 (void) input;
809 (void) input_length;
810 (void) output;
811 (void) output_length;
812 (void) output_size;
813
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200814 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200815#endif /* PSA_CRYPTO_DRIVER_PRESENT */
816}
817
818psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200819 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200820 uint8_t *output,
821 size_t output_size,
822 size_t *output_length )
823{
824#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200825 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200826 {
827#if defined(PSA_CRYPTO_DRIVER_TEST)
828 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200829 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200830 output,
831 output_size,
832 output_length ) );
833#endif /* PSA_CRYPTO_DRIVER_TEST */
834#if defined(PSA_CRYPTO_DRIVER_TEST)
835 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200836 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200837 output,
838 output_size,
839 output_length ) );
840#endif /* PSA_CRYPTO_DRIVER_TEST */
841 default:
842 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200843 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200844 }
845#else /* PSA_CRYPTO_DRIVER_PRESENT */
846 (void) operation;
847 (void) output;
848 (void) output_size;
849 (void) output_length;
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_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200856 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200857{
858#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
859 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200860
861 /* The object has (apparently) been initialized but it is not in use. It's
862 * ok to call abort on such an object, and there's nothing to do. */
863 if( operation->ctx == NULL && operation->id == 0 )
864 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200865
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200866 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200867 {
868#if defined(PSA_CRYPTO_DRIVER_TEST)
869 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200870 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200871 mbedtls_platform_zeroize(
872 operation->ctx,
873 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200874 mbedtls_free( operation->ctx );
875 operation->ctx = NULL;
876 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200877
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200878 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200879#endif /* PSA_CRYPTO_DRIVER_TEST */
880#if defined(PSA_CRYPTO_DRIVER_TEST)
881 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200882 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200883 mbedtls_platform_zeroize(
884 operation->ctx,
885 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200886 mbedtls_free( operation->ctx );
887 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200888 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200889
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200890 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200891#endif /* PSA_CRYPTO_DRIVER_TEST */
892 default:
893 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200894 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200895 }
896#else /* PSA_CRYPTO_DRIVER_PRESENT */
897 (void)operation;
898
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200899 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200900#endif /* PSA_CRYPTO_DRIVER_PRESENT */
901}
902
Steven Cooremancd84cb42020-07-16 20:28:36 +0200903/* End of automatically generated file. */