blob: a8dcc064d968c95f2f250f4becfbe7f9db1b9d94 [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:
276 /* TBD: opaque driver support: need to calculate size through a
277 * driver-defined size function, since the size of an opaque (wrapped)
278 * key will be different for each implementation. */
279#ifdef TEST_KEY_CONTEXT_SIZE_FUNCTION
280 *expected_size = test_size_function( key_type, key_bits );
281 return( PSA_SUCCESS );
282#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
283 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
284 {
285 *expected_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
286 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
287 }
288 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( attributes->core.type ) )
289 {
290 *expected_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
291 + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
292 * ( ( key_bits + 7 ) / 8 );
293 }
294 else
295 {
296 return( PSA_ERROR_NOT_SUPPORTED );
297 }
298 return( PSA_SUCCESS );
299#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
300#endif /* PSA_CRYPTO_DRIVER_TEST */
301
302 default:
Steven Cooreman56250fd2020-09-04 13:07:15 +0200303 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200304 }
305}
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200306#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200307
Steven Cooreman55ae2172020-07-17 19:46:15 +0200308psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes,
309 psa_key_slot_t *slot )
310{
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200311#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200312 /* Try dynamically-registered SE interface first */
313#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
314 const psa_drv_se_t *drv;
315 psa_drv_se_context_t *drv_context;
316
317 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
318 {
319 size_t pubkey_length = 0; /* We don't support this feature yet */
320 if( drv->key_management == NULL ||
321 drv->key_management->p_generate == NULL )
322 {
323 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200324 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200325 }
326 return( drv->key_management->p_generate(
327 drv_context,
328 slot->data.se.slot_number, attributes,
329 NULL, 0, &pubkey_length ) );
330 }
331#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
332
333 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200334#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200335 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
336 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
337 size_t export_size = 0;
338
339 status = get_expected_key_size( attributes, &export_size );
340 if( status != PSA_SUCCESS )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200341 return( status );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200342
343 slot->data.key.data = mbedtls_calloc(1, export_size);
344 if( slot->data.key.data == NULL )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200345 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200346 slot->data.key.bytes = export_size;
347
348 switch( location )
349 {
350 case PSA_KEY_LOCATION_LOCAL_STORAGE:
351 /* Key is stored in the slot in export representation, so
352 * cycle through all known transparent accelerators */
353
354 /* Transparent drivers are limited to generating asymmetric keys */
355 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
356 {
357 status = PSA_ERROR_NOT_SUPPORTED;
358 break;
359 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200360#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200361 status = test_transparent_generate_key( attributes,
362 slot->data.key.data,
363 slot->data.key.bytes,
364 &slot->data.key.bytes );
365 /* Declared with fallback == true */
366 if( status != PSA_ERROR_NOT_SUPPORTED )
367 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200368#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200369 /* Fell through, meaning no accelerator supports this operation */
370 status = PSA_ERROR_NOT_SUPPORTED;
371 break;
372 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200373#if defined(PSA_CRYPTO_DRIVER_TEST)
374 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200375 status = test_opaque_generate_key( attributes,
376 slot->data.key.data,
377 slot->data.key.bytes,
378 &slot->data.key.bytes );
379 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200380#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200381 default:
382 /* Key is declared with a lifetime not known to us */
383 status = PSA_ERROR_INVALID_ARGUMENT;
384 break;
385 }
386
387 if( status != PSA_SUCCESS )
388 {
389 /* free allocated buffer */
390 mbedtls_free( slot->data.key.data );
391 slot->data.key.data = NULL;
392 slot->data.key.bytes = 0;
393 }
394
395 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200396#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200397 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200398#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
399#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200400 (void) attributes;
401 (void) slot;
402
Steven Cooreman56250fd2020-09-04 13:07:15 +0200403 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200404#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200405}
406
Steven Cooreman37941cb2020-07-28 18:49:51 +0200407/*
408 * Cipher functions
409 */
410psa_status_t psa_driver_wrapper_cipher_encrypt(
411 psa_key_slot_t *slot,
412 psa_algorithm_t alg,
413 const uint8_t *input,
414 size_t input_length,
415 uint8_t *output,
416 size_t output_size,
417 size_t *output_length )
418{
419#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
420 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
421 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
422 psa_key_attributes_t attributes = {
423 .core = slot->attr
424 };
425
426 switch( location )
427 {
428 case PSA_KEY_LOCATION_LOCAL_STORAGE:
429 /* Key is stored in the slot in export representation, so
430 * cycle through all known transparent accelerators */
431#if defined(PSA_CRYPTO_DRIVER_TEST)
432 status = test_transparent_cipher_encrypt( &attributes,
433 slot->data.key.data,
434 slot->data.key.bytes,
435 alg,
436 input,
437 input_length,
438 output,
439 output_size,
440 output_length );
441 /* Declared with fallback == true */
442 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200443 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200444#endif /* PSA_CRYPTO_DRIVER_TEST */
445 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200446 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200447 /* Add cases for opaque driver here */
448#if defined(PSA_CRYPTO_DRIVER_TEST)
449 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
450 return( test_opaque_cipher_encrypt( &attributes,
451 slot->data.key.data,
452 slot->data.key.bytes,
453 alg,
454 input,
455 input_length,
456 output,
457 output_size,
458 output_length ) );
459#endif /* PSA_CRYPTO_DRIVER_TEST */
460 default:
461 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200462 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200463 }
464#else /* PSA_CRYPTO_DRIVER_PRESENT */
465 (void) slot;
466 (void) alg;
467 (void) input;
468 (void) input_length;
469 (void) output;
470 (void) output_size;
471 (void) output_length;
472
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200473 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200474#endif /* PSA_CRYPTO_DRIVER_PRESENT */
475}
476
477psa_status_t psa_driver_wrapper_cipher_decrypt(
478 psa_key_slot_t *slot,
479 psa_algorithm_t alg,
480 const uint8_t *input,
481 size_t input_length,
482 uint8_t *output,
483 size_t output_size,
484 size_t *output_length )
485{
486#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
487 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
488 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
489 psa_key_attributes_t attributes = {
490 .core = slot->attr
491 };
492
493 switch( location )
494 {
495 case PSA_KEY_LOCATION_LOCAL_STORAGE:
496 /* Key is stored in the slot in export representation, so
497 * cycle through all known transparent accelerators */
498#if defined(PSA_CRYPTO_DRIVER_TEST)
499 status = test_transparent_cipher_decrypt( &attributes,
500 slot->data.key.data,
501 slot->data.key.bytes,
502 alg,
503 input,
504 input_length,
505 output,
506 output_size,
507 output_length );
508 /* Declared with fallback == true */
509 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200510 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200511#endif /* PSA_CRYPTO_DRIVER_TEST */
512 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200513 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200514 /* Add cases for opaque driver here */
515#if defined(PSA_CRYPTO_DRIVER_TEST)
516 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
517 return( test_opaque_cipher_decrypt( &attributes,
518 slot->data.key.data,
519 slot->data.key.bytes,
520 alg,
521 input,
522 input_length,
523 output,
524 output_size,
525 output_length ) );
526#endif /* PSA_CRYPTO_DRIVER_TEST */
527 default:
528 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200529 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200530 }
531#else /* PSA_CRYPTO_DRIVER_PRESENT */
532 (void) slot;
533 (void) alg;
534 (void) input;
535 (void) input_length;
536 (void) output;
537 (void) output_size;
538 (void) output_length;
539
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200540 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200541#endif /* PSA_CRYPTO_DRIVER_PRESENT */
542}
543
544psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200545 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200546 psa_key_slot_t *slot,
547 psa_algorithm_t alg )
548{
549#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
550 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
551 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
552 psa_key_attributes_t attributes = {
553 .core = slot->attr
554 };
555
Steven Cooreman37941cb2020-07-28 18:49:51 +0200556 switch( location )
557 {
558 case PSA_KEY_LOCATION_LOCAL_STORAGE:
559 /* Key is stored in the slot in export representation, so
560 * cycle through all known transparent accelerators */
561#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200562 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
563 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200564 return PSA_ERROR_INSUFFICIENT_MEMORY;
565
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200566 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200567 &attributes,
568 slot->data.key.data,
569 slot->data.key.bytes,
570 alg );
571 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200572 if( status == PSA_SUCCESS )
573 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200574 else
575 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200576 mbedtls_platform_zeroize(
577 operation->ctx,
578 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200579 mbedtls_free( operation->ctx );
580 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200581 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200582
583 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200584#endif /* PSA_CRYPTO_DRIVER_TEST */
585 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200586 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200587 /* Add cases for opaque driver here */
588#if defined(PSA_CRYPTO_DRIVER_TEST)
589 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200590 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
591 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200592 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200593
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200594 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200595 &attributes,
596 slot->data.key.data,
597 slot->data.key.bytes,
598 alg );
599 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200600 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200601 else
602 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200603 mbedtls_platform_zeroize(
604 operation->ctx,
605 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200606 mbedtls_free( operation->ctx );
607 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200608 }
609
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200610 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200611#endif /* PSA_CRYPTO_DRIVER_TEST */
612 default:
613 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200614 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200615 }
616#else /* PSA_CRYPTO_DRIVER_PRESENT */
617 (void)slot;
618 (void)alg;
619 (void)operation;
620
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200621 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200622#endif /* PSA_CRYPTO_DRIVER_PRESENT */
623}
624
625psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200626 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200627 psa_key_slot_t *slot,
628 psa_algorithm_t alg )
629{
630#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
631 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
632 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
633 psa_key_attributes_t attributes = {
634 .core = slot->attr
635 };
636
Steven Cooreman37941cb2020-07-28 18:49:51 +0200637 switch( location )
638 {
639 case PSA_KEY_LOCATION_LOCAL_STORAGE:
640 /* Key is stored in the slot in export representation, so
641 * cycle through all known transparent accelerators */
642#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200643 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
644 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200645 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200646
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200647 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200648 &attributes,
649 slot->data.key.data,
650 slot->data.key.bytes,
651 alg );
652 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200653 if( status == PSA_SUCCESS )
654 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200655 else
656 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200657 mbedtls_platform_zeroize(
658 operation->ctx,
659 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200660 mbedtls_free( operation->ctx );
661 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200662 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200663
664 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200665#endif /* PSA_CRYPTO_DRIVER_TEST */
666 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200667 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200668 /* Add cases for opaque driver here */
669#if defined(PSA_CRYPTO_DRIVER_TEST)
670 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200671 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
672 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200673 return PSA_ERROR_INSUFFICIENT_MEMORY;
674
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200675 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200676 &attributes,
677 slot->data.key.data,
678 slot->data.key.bytes,
679 alg );
680 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200681 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200682 else
683 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200684 mbedtls_platform_zeroize(
685 operation->ctx,
686 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200687 mbedtls_free( operation->ctx );
688 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200689 }
690
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200691 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200692#endif /* PSA_CRYPTO_DRIVER_TEST */
693 default:
694 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200695 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200696 }
697#else /* PSA_CRYPTO_DRIVER_PRESENT */
698 (void)slot;
699 (void)alg;
700 (void)operation;
701
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200702 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200703#endif /* PSA_CRYPTO_DRIVER_PRESENT */
704}
705
706psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200707 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200708 uint8_t *iv,
709 size_t iv_size,
710 size_t *iv_length )
711{
712#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200713 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200714 {
715#if defined(PSA_CRYPTO_DRIVER_TEST)
716 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200717 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200718 iv,
719 iv_size,
720 iv_length ) );
721#endif /* PSA_CRYPTO_DRIVER_TEST */
722#if defined(PSA_CRYPTO_DRIVER_TEST)
723 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200724 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200725 iv,
726 iv_size,
727 iv_length ) );
728#endif /* PSA_CRYPTO_DRIVER_TEST */
729 default:
730 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200731 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200732 }
733#else /* PSA_CRYPTO_DRIVER_PRESENT */
734 (void) operation;
735 (void) iv;
736 (void) iv_size;
737 (void) iv_length;
738
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200739 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200740#endif /* PSA_CRYPTO_DRIVER_PRESENT */
741}
742
743psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200744 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200745 const uint8_t *iv,
746 size_t iv_length )
747{
748#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200749 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200750 {
751#if defined(PSA_CRYPTO_DRIVER_TEST)
752 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200753 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200754 iv,
755 iv_length ) );
756#endif /* PSA_CRYPTO_DRIVER_TEST */
757#if defined(PSA_CRYPTO_DRIVER_TEST)
758 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200759 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200760 iv,
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_length;
771
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200772 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200773#endif /* PSA_CRYPTO_DRIVER_PRESENT */
774}
775
776psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200777 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200778 const uint8_t *input,
779 size_t input_length,
780 uint8_t *output,
781 size_t output_size,
782 size_t *output_length )
783{
784#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200785 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200786 {
787#if defined(PSA_CRYPTO_DRIVER_TEST)
788 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200789 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200790 input,
791 input_length,
792 output,
793 output_size,
794 output_length ) );
795#endif /* PSA_CRYPTO_DRIVER_TEST */
796#if defined(PSA_CRYPTO_DRIVER_TEST)
797 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200798 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200799 input,
800 input_length,
801 output,
802 output_size,
803 output_length ) );
804#endif /* PSA_CRYPTO_DRIVER_TEST */
805 default:
806 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200807 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200808 }
809#else /* PSA_CRYPTO_DRIVER_PRESENT */
810 (void) operation;
811 (void) input;
812 (void) input_length;
813 (void) output;
814 (void) output_length;
815 (void) output_size;
816
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200817 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200818#endif /* PSA_CRYPTO_DRIVER_PRESENT */
819}
820
821psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200822 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200823 uint8_t *output,
824 size_t output_size,
825 size_t *output_length )
826{
827#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200828 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200829 {
830#if defined(PSA_CRYPTO_DRIVER_TEST)
831 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200832 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200833 output,
834 output_size,
835 output_length ) );
836#endif /* PSA_CRYPTO_DRIVER_TEST */
837#if defined(PSA_CRYPTO_DRIVER_TEST)
838 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200839 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200840 output,
841 output_size,
842 output_length ) );
843#endif /* PSA_CRYPTO_DRIVER_TEST */
844 default:
845 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200846 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200847 }
848#else /* PSA_CRYPTO_DRIVER_PRESENT */
849 (void) operation;
850 (void) output;
851 (void) output_size;
852 (void) output_length;
853
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200854 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200855#endif /* PSA_CRYPTO_DRIVER_PRESENT */
856}
857
858psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200859 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200860{
861#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
862 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200863
864 /* The object has (apparently) been initialized but it is not in use. It's
865 * ok to call abort on such an object, and there's nothing to do. */
866 if( operation->ctx == NULL && operation->id == 0 )
867 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200868
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200869 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200870 {
871#if defined(PSA_CRYPTO_DRIVER_TEST)
872 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200873 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200874 mbedtls_platform_zeroize(
875 operation->ctx,
876 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200877 mbedtls_free( operation->ctx );
878 operation->ctx = NULL;
879 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200880
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200881 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200882#endif /* PSA_CRYPTO_DRIVER_TEST */
883#if defined(PSA_CRYPTO_DRIVER_TEST)
884 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200885 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200886 mbedtls_platform_zeroize(
887 operation->ctx,
888 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200889 mbedtls_free( operation->ctx );
890 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200891 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200892
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200893 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200894#endif /* PSA_CRYPTO_DRIVER_TEST */
895 default:
896 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200897 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200898 }
899#else /* PSA_CRYPTO_DRIVER_PRESENT */
900 (void)operation;
901
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200902 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200903#endif /* PSA_CRYPTO_DRIVER_PRESENT */
904}
905
Steven Cooremancd84cb42020-07-16 20:28:36 +0200906/* End of automatically generated file. */