blob: 268b47c6210d6809ac4886392aa049ef96f473ee [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;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200259 if( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == PSA_KEY_LOCATION_LOCAL_STORAGE )
260 {
Steven Cooreman56250fd2020-09-04 13:07:15 +0200261 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( attributes->core.type,
262 attributes->core.bits );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200263
Steven Cooreman56250fd2020-09-04 13:07:15 +0200264 if( buffer_size == 0 )
265 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200266
Steven Cooreman56250fd2020-09-04 13:07:15 +0200267 *expected_size = buffer_size;
268 return( PSA_SUCCESS );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200269 }
270 else
271 {
Steven Cooreman56250fd2020-09-04 13:07:15 +0200272 /* TBD: opaque driver support: need to calculate size through a
273 * driver-defined size function, since the size of an opaque (wrapped)
274 * key will be different for each implementation. */
275 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200276 }
277}
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200278#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200279
Steven Cooreman55ae2172020-07-17 19:46:15 +0200280psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes,
281 psa_key_slot_t *slot )
282{
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200283#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200284 /* Try dynamically-registered SE interface first */
285#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
286 const psa_drv_se_t *drv;
287 psa_drv_se_context_t *drv_context;
288
289 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
290 {
291 size_t pubkey_length = 0; /* We don't support this feature yet */
292 if( drv->key_management == NULL ||
293 drv->key_management->p_generate == NULL )
294 {
295 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200296 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200297 }
298 return( drv->key_management->p_generate(
299 drv_context,
300 slot->data.se.slot_number, attributes,
301 NULL, 0, &pubkey_length ) );
302 }
303#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
304
305 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200306#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200307 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
308 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
309 size_t export_size = 0;
310
311 status = get_expected_key_size( attributes, &export_size );
312 if( status != PSA_SUCCESS )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200313 return( status );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200314
315 slot->data.key.data = mbedtls_calloc(1, export_size);
316 if( slot->data.key.data == NULL )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200317 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200318 slot->data.key.bytes = export_size;
319
320 switch( location )
321 {
322 case PSA_KEY_LOCATION_LOCAL_STORAGE:
323 /* Key is stored in the slot in export representation, so
324 * cycle through all known transparent accelerators */
325
326 /* Transparent drivers are limited to generating asymmetric keys */
327 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
328 {
329 status = PSA_ERROR_NOT_SUPPORTED;
330 break;
331 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200332#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200333 status = test_transparent_generate_key( attributes,
334 slot->data.key.data,
335 slot->data.key.bytes,
336 &slot->data.key.bytes );
337 /* Declared with fallback == true */
338 if( status != PSA_ERROR_NOT_SUPPORTED )
339 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200340#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200341 /* Fell through, meaning no accelerator supports this operation */
342 status = PSA_ERROR_NOT_SUPPORTED;
343 break;
344 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200345#if defined(PSA_CRYPTO_DRIVER_TEST)
346 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200347 status = test_opaque_generate_key( attributes,
348 slot->data.key.data,
349 slot->data.key.bytes,
350 &slot->data.key.bytes );
351 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200352#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200353 default:
354 /* Key is declared with a lifetime not known to us */
355 status = PSA_ERROR_INVALID_ARGUMENT;
356 break;
357 }
358
359 if( status != PSA_SUCCESS )
360 {
361 /* free allocated buffer */
362 mbedtls_free( slot->data.key.data );
363 slot->data.key.data = NULL;
364 slot->data.key.bytes = 0;
365 }
366
367 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200368#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200369 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200370#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
371#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200372 (void) attributes;
373 (void) slot;
374
Steven Cooreman56250fd2020-09-04 13:07:15 +0200375 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200376#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200377}
378
Steven Cooreman37941cb2020-07-28 18:49:51 +0200379/*
380 * Cipher functions
381 */
382psa_status_t psa_driver_wrapper_cipher_encrypt(
383 psa_key_slot_t *slot,
384 psa_algorithm_t alg,
385 const uint8_t *input,
386 size_t input_length,
387 uint8_t *output,
388 size_t output_size,
389 size_t *output_length )
390{
391#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
392 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
393 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
394 psa_key_attributes_t attributes = {
395 .core = slot->attr
396 };
397
398 switch( location )
399 {
400 case PSA_KEY_LOCATION_LOCAL_STORAGE:
401 /* Key is stored in the slot in export representation, so
402 * cycle through all known transparent accelerators */
403#if defined(PSA_CRYPTO_DRIVER_TEST)
404 status = test_transparent_cipher_encrypt( &attributes,
405 slot->data.key.data,
406 slot->data.key.bytes,
407 alg,
408 input,
409 input_length,
410 output,
411 output_size,
412 output_length );
413 /* Declared with fallback == true */
414 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200415 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200416#endif /* PSA_CRYPTO_DRIVER_TEST */
417 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200418 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200419 /* Add cases for opaque driver here */
420#if defined(PSA_CRYPTO_DRIVER_TEST)
421 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
422 return( test_opaque_cipher_encrypt( &attributes,
423 slot->data.key.data,
424 slot->data.key.bytes,
425 alg,
426 input,
427 input_length,
428 output,
429 output_size,
430 output_length ) );
431#endif /* PSA_CRYPTO_DRIVER_TEST */
432 default:
433 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200434 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200435 }
436#else /* PSA_CRYPTO_DRIVER_PRESENT */
437 (void) slot;
438 (void) alg;
439 (void) input;
440 (void) input_length;
441 (void) output;
442 (void) output_size;
443 (void) output_length;
444
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200445 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200446#endif /* PSA_CRYPTO_DRIVER_PRESENT */
447}
448
449psa_status_t psa_driver_wrapper_cipher_decrypt(
450 psa_key_slot_t *slot,
451 psa_algorithm_t alg,
452 const uint8_t *input,
453 size_t input_length,
454 uint8_t *output,
455 size_t output_size,
456 size_t *output_length )
457{
458#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
459 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
460 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
461 psa_key_attributes_t attributes = {
462 .core = slot->attr
463 };
464
465 switch( location )
466 {
467 case PSA_KEY_LOCATION_LOCAL_STORAGE:
468 /* Key is stored in the slot in export representation, so
469 * cycle through all known transparent accelerators */
470#if defined(PSA_CRYPTO_DRIVER_TEST)
471 status = test_transparent_cipher_decrypt( &attributes,
472 slot->data.key.data,
473 slot->data.key.bytes,
474 alg,
475 input,
476 input_length,
477 output,
478 output_size,
479 output_length );
480 /* Declared with fallback == true */
481 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200482 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200483#endif /* PSA_CRYPTO_DRIVER_TEST */
484 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200485 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200486 /* Add cases for opaque driver here */
487#if defined(PSA_CRYPTO_DRIVER_TEST)
488 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
489 return( test_opaque_cipher_decrypt( &attributes,
490 slot->data.key.data,
491 slot->data.key.bytes,
492 alg,
493 input,
494 input_length,
495 output,
496 output_size,
497 output_length ) );
498#endif /* PSA_CRYPTO_DRIVER_TEST */
499 default:
500 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200501 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200502 }
503#else /* PSA_CRYPTO_DRIVER_PRESENT */
504 (void) slot;
505 (void) alg;
506 (void) input;
507 (void) input_length;
508 (void) output;
509 (void) output_size;
510 (void) output_length;
511
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200512 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200513#endif /* PSA_CRYPTO_DRIVER_PRESENT */
514}
515
516psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200517 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200518 psa_key_slot_t *slot,
519 psa_algorithm_t alg )
520{
521#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
522 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
523 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
524 psa_key_attributes_t attributes = {
525 .core = slot->attr
526 };
527
528 /* Check for operation already allocated */
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200529 if( operation->ctx != NULL || operation->id != 0 )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200530 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200531
532 switch( location )
533 {
534 case PSA_KEY_LOCATION_LOCAL_STORAGE:
535 /* Key is stored in the slot in export representation, so
536 * cycle through all known transparent accelerators */
537#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200538 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
539 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200540 return PSA_ERROR_INSUFFICIENT_MEMORY;
541
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200542 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200543 &attributes,
544 slot->data.key.data,
545 slot->data.key.bytes,
546 alg );
547 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200548 if( status == PSA_SUCCESS )
549 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200550 else
551 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200552 mbedtls_free( operation->ctx );
553 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200554 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200555
556 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200557#endif /* PSA_CRYPTO_DRIVER_TEST */
558 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200559 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200560 /* Add cases for opaque driver here */
561#if defined(PSA_CRYPTO_DRIVER_TEST)
562 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200563 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
564 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200565 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200566
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200567 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200568 &attributes,
569 slot->data.key.data,
570 slot->data.key.bytes,
571 alg );
572 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200573 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200574 else
575 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200576 mbedtls_free( operation->ctx );
577 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200578 }
579
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200580 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200581#endif /* PSA_CRYPTO_DRIVER_TEST */
582 default:
583 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200584 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200585 }
586#else /* PSA_CRYPTO_DRIVER_PRESENT */
587 (void)slot;
588 (void)alg;
589 (void)operation;
590
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200591 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200592#endif /* PSA_CRYPTO_DRIVER_PRESENT */
593}
594
595psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200596 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200597 psa_key_slot_t *slot,
598 psa_algorithm_t alg )
599{
600#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
601 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
602 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
603 psa_key_attributes_t attributes = {
604 .core = slot->attr
605 };
606
607 /* Check for operation already allocated */
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200608 if( operation->ctx != NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200609 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200610
611 switch( location )
612 {
613 case PSA_KEY_LOCATION_LOCAL_STORAGE:
614 /* Key is stored in the slot in export representation, so
615 * cycle through all known transparent accelerators */
616#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200617 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
618 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200619 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200620
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200621 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200622 &attributes,
623 slot->data.key.data,
624 slot->data.key.bytes,
625 alg );
626 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200627 if( status == PSA_SUCCESS )
628 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200629 else
630 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200631 mbedtls_free( operation->ctx );
632 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200633 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200634
635 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200636#endif /* PSA_CRYPTO_DRIVER_TEST */
637 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200638 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200639 /* Add cases for opaque driver here */
640#if defined(PSA_CRYPTO_DRIVER_TEST)
641 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200642 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
643 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200644 return PSA_ERROR_INSUFFICIENT_MEMORY;
645
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200646 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200647 &attributes,
648 slot->data.key.data,
649 slot->data.key.bytes,
650 alg );
651 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200652 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200653 else
654 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200655 mbedtls_free( operation->ctx );
656 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200657 }
658
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200659 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200660#endif /* PSA_CRYPTO_DRIVER_TEST */
661 default:
662 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200663 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200664 }
665#else /* PSA_CRYPTO_DRIVER_PRESENT */
666 (void)slot;
667 (void)alg;
668 (void)operation;
669
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200670 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200671#endif /* PSA_CRYPTO_DRIVER_PRESENT */
672}
673
674psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200675 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200676 uint8_t *iv,
677 size_t iv_size,
678 size_t *iv_length )
679{
680#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
681 /* Check for operation already allocated */
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200682 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200683 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200684
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200685 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200686 {
687#if defined(PSA_CRYPTO_DRIVER_TEST)
688 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200689 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200690 iv,
691 iv_size,
692 iv_length ) );
693#endif /* PSA_CRYPTO_DRIVER_TEST */
694#if defined(PSA_CRYPTO_DRIVER_TEST)
695 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200696 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200697 iv,
698 iv_size,
699 iv_length ) );
700#endif /* PSA_CRYPTO_DRIVER_TEST */
701 default:
702 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200703 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200704 }
705#else /* PSA_CRYPTO_DRIVER_PRESENT */
706 (void) operation;
707 (void) iv;
708 (void) iv_size;
709 (void) iv_length;
710
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200711 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200712#endif /* PSA_CRYPTO_DRIVER_PRESENT */
713}
714
715psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200716 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200717 const uint8_t *iv,
718 size_t iv_length )
719{
720#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
721 /* Check for operation already allocated */
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200722 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200723 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200724
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200725 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200726 {
727#if defined(PSA_CRYPTO_DRIVER_TEST)
728 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200729 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200730 iv,
731 iv_length ) );
732#endif /* PSA_CRYPTO_DRIVER_TEST */
733#if defined(PSA_CRYPTO_DRIVER_TEST)
734 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200735 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200736 iv,
737 iv_length ) );
738#endif /* PSA_CRYPTO_DRIVER_TEST */
739 default:
740 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200741 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200742 }
743#else /* PSA_CRYPTO_DRIVER_PRESENT */
744 (void) operation;
745 (void) iv;
746 (void) iv_length;
747
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200748 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200749#endif /* PSA_CRYPTO_DRIVER_PRESENT */
750}
751
752psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200753 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200754 const uint8_t *input,
755 size_t input_length,
756 uint8_t *output,
757 size_t output_size,
758 size_t *output_length )
759{
760#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
761 /* Check for operation already allocated */
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200762 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200763 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200764
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200765 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200766 {
767#if defined(PSA_CRYPTO_DRIVER_TEST)
768 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200769 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200770 input,
771 input_length,
772 output,
773 output_size,
774 output_length ) );
775#endif /* PSA_CRYPTO_DRIVER_TEST */
776#if defined(PSA_CRYPTO_DRIVER_TEST)
777 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200778 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200779 input,
780 input_length,
781 output,
782 output_size,
783 output_length ) );
784#endif /* PSA_CRYPTO_DRIVER_TEST */
785 default:
786 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200787 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200788 }
789#else /* PSA_CRYPTO_DRIVER_PRESENT */
790 (void) operation;
791 (void) input;
792 (void) input_length;
793 (void) output;
794 (void) output_length;
795 (void) output_size;
796
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200797 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200798#endif /* PSA_CRYPTO_DRIVER_PRESENT */
799}
800
801psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200802 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200803 uint8_t *output,
804 size_t output_size,
805 size_t *output_length )
806{
807#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
808 /* Check for operation already allocated */
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200809 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200810 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200811
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200812 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200813 {
814#if defined(PSA_CRYPTO_DRIVER_TEST)
815 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200816 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200817 output,
818 output_size,
819 output_length ) );
820#endif /* PSA_CRYPTO_DRIVER_TEST */
821#if defined(PSA_CRYPTO_DRIVER_TEST)
822 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200823 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200824 output,
825 output_size,
826 output_length ) );
827#endif /* PSA_CRYPTO_DRIVER_TEST */
828 default:
829 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200830 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200831 }
832#else /* PSA_CRYPTO_DRIVER_PRESENT */
833 (void) operation;
834 (void) output;
835 (void) output_size;
836 (void) output_length;
837
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200838 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200839#endif /* PSA_CRYPTO_DRIVER_PRESENT */
840}
841
842psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200843 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200844{
845#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
846 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
847 /* Check for operation already allocated */
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200848 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200849 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200850
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200851 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200852 {
853#if defined(PSA_CRYPTO_DRIVER_TEST)
854 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200855 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200856
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200857 mbedtls_free( operation->ctx );
858 operation->ctx = NULL;
859 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200860
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200861 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200862#endif /* PSA_CRYPTO_DRIVER_TEST */
863#if defined(PSA_CRYPTO_DRIVER_TEST)
864 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200865 status = test_opaque_cipher_abort( operation->ctx );
866 mbedtls_free( operation->ctx );
867 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200868
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200869 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200870#endif /* PSA_CRYPTO_DRIVER_TEST */
871 default:
872 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200873 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200874 }
875#else /* PSA_CRYPTO_DRIVER_PRESENT */
876 (void)operation;
877
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200878 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200879#endif /* PSA_CRYPTO_DRIVER_PRESENT */
880}
881
Steven Cooremancd84cb42020-07-16 20:28:36 +0200882/* End of automatically generated file. */