blob: 3cb75576e118e7d8af5da204b4b5cb9a2694b006 [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,
Ronald Cronea0f8a62020-11-25 17:52:23 +010083 psa_key_slot_get_slot_number( slot ),
Steven Cooreman7a250572020-07-17 16:43:05 +020084 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,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100106 slot->key.data,
107 slot->key.bytes,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200108 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,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100124 slot->key.data,
125 slot->key.bytes,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200126 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,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100175 psa_key_slot_get_slot_number( slot ),
Steven Cooreman55ae2172020-07-17 19:46:15 +0200176 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,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100197 slot->key.data,
198 slot->key.bytes,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200199 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,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100214 slot->key.data,
215 slot->key.bytes,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200216 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
Ronald Cron31216282020-12-05 18:47:56 +0100241/** Get the key buffer size for the key material of a generated key in the
242 * case of an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200243 *
Ronald Cron31216282020-12-05 18:47:56 +0100244 * \param[in] attributes The key attributes.
245 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200246 *
247 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100248 * The minimum size for a buffer to contain the key material has been
249 * returned successfully.
250 * \retval #PSA_ERROR_INVALID_ARGUMENT
251 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200252 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100253 * The type and/or the size in bits of the key or the combination of
254 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200255 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100256psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100257 const psa_key_attributes_t *attributes,
258 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200259{
John Durkop2c618352020-09-22 06:54:01 -0700260 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
261 psa_key_type_t key_type = attributes->core.type;
262 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200263
Ronald Cron31216282020-12-05 18:47:56 +0100264 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700265 switch( location )
266 {
John Durkop2c618352020-09-22 06:54:01 -0700267#if defined(PSA_CRYPTO_DRIVER_TEST)
268 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
John Durkopac93e3b2020-10-16 06:48:55 -0700269#ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
Ronald Cron31216282020-12-05 18:47:56 +0100270 *key_buffer_size = test_size_function( key_type, key_bits );
John Durkop2c618352020-09-22 06:54:01 -0700271 return( PSA_SUCCESS );
272#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
273 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
274 {
Ronald Cron31216282020-12-05 18:47:56 +0100275 int public_key_overhead =
276 ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ?
277 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
278 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop135ce692020-10-19 07:12:28 -0700279 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
280 + public_key_overhead;
281 }
Ronald Cron31216282020-12-05 18:47:56 +0100282 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
John Durkop135ce692020-10-19 07:12:28 -0700283 {
Ronald Cron31216282020-12-05 18:47:56 +0100284 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700285 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
286 }
John Durkop135ce692020-10-19 07:12:28 -0700287 else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
Ronald Cron31216282020-12-05 18:47:56 +0100288 !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
John Durkop2c618352020-09-22 06:54:01 -0700289 {
Ronald Cron31216282020-12-05 18:47:56 +0100290 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700291 + 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:
Ronald Cron9df74be2020-12-05 19:15:23 +0100303 (void)key_type;
304 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200305 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200306 }
307}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200308
Ronald Cron977c2472020-10-13 08:32:21 +0200309psa_status_t psa_driver_wrapper_generate_key(
310 const psa_key_attributes_t *attributes,
311 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200312{
Ronald Cron977c2472020-10-13 08:32:21 +0200313 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
314 psa_key_location_t location =
315 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
316
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200317 /* Try dynamically-registered SE interface first */
318#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
319 const psa_drv_se_t *drv;
320 psa_drv_se_context_t *drv_context;
321
Ronald Cron977c2472020-10-13 08:32:21 +0200322 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200323 {
324 size_t pubkey_length = 0; /* We don't support this feature yet */
325 if( drv->key_management == NULL ||
326 drv->key_management->p_generate == NULL )
327 {
328 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200329 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200330 }
331 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200332 drv_context,
333 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100334 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200335 }
336#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
337
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200338 switch( location )
339 {
340 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200341#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200342 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200343 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200344 {
Ronald Cron977c2472020-10-13 08:32:21 +0200345 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200346#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron977c2472020-10-13 08:32:21 +0200347 status = test_transparent_generate_key(
348 attributes, key_buffer, key_buffer_size,
349 key_buffer_length );
350 /* Declared with fallback == true */
351 if( status != PSA_ERROR_NOT_SUPPORTED )
352 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200353#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200354 }
355#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
356
357 /* Software fallback */
358 status = psa_generate_key_internal(
359 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200360 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200361
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200362 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200363#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200364#if defined(PSA_CRYPTO_DRIVER_TEST)
365 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron977c2472020-10-13 08:32:21 +0200366 status = test_opaque_generate_key(
367 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200368 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200369#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200370#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
371
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200372 default:
373 /* Key is declared with a lifetime not known to us */
374 status = PSA_ERROR_INVALID_ARGUMENT;
375 break;
376 }
377
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200378 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200379}
380
Ronald Cron83282872020-11-22 14:02:39 +0100381psa_status_t psa_driver_wrapper_import_key(
382 const psa_key_attributes_t *attributes,
383 const uint8_t *data,
384 size_t data_length,
385 uint8_t *key_buffer,
386 size_t key_buffer_size,
387 size_t *key_buffer_length,
388 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200389{
Steven Cooreman04524762020-10-13 17:43:44 +0200390 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100391 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
392 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200393
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100394 /* Try dynamically-registered SE interface first */
395#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
396 const psa_drv_se_t *drv;
397 psa_drv_se_context_t *drv_context;
398
399 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
400 {
401 if( drv->key_management == NULL ||
402 drv->key_management->p_import == NULL )
403 return( PSA_ERROR_NOT_SUPPORTED );
404
405 /* The driver should set the number of key bits, however in
406 * case it doesn't, we initialize bits to an invalid value. */
407 *bits = PSA_MAX_KEY_BITS + 1;
408 status = drv->key_management->p_import(
409 drv_context,
410 *( (psa_key_slot_number_t *)key_buffer ),
411 attributes, data, data_length, bits );
412
413 if( status != PSA_SUCCESS )
414 return( status );
415
416 if( (*bits) > PSA_MAX_KEY_BITS )
417 return( PSA_ERROR_NOT_SUPPORTED );
418
419 return( PSA_SUCCESS );
420 }
421#endif /* PSA_CRYPTO_SE_C */
422
Ronald Cronbf33c932020-11-28 18:06:53 +0100423 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_ACCELERATOR_DRIVER_PRESENT)
429#if defined(PSA_CRYPTO_DRIVER_TEST)
430 status = test_transparent_import_key( attributes,
431 data, data_length,
432 key_buffer, key_buffer_size,
433 key_buffer_length, bits );
434 /* Declared with fallback == true */
435 if( status != PSA_ERROR_NOT_SUPPORTED )
436 return( status );
437#endif /* PSA_CRYPTO_DRIVER_TEST */
438#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
439 /* Fell through, meaning no accelerator supports this operation */
440 return( psa_import_key_into_slot( attributes,
441 data, data_length,
442 key_buffer, key_buffer_size,
443 key_buffer_length, bits ) );
444
445 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100446 /* Importing a key with external storage in not yet supported.
447 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100448 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100449 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100450 }
451
Steven Cooreman04524762020-10-13 17:43:44 +0200452}
453
Ronald Cron67227982020-11-26 15:16:05 +0100454psa_status_t psa_driver_wrapper_export_key(
455 const psa_key_attributes_t *attributes,
456 const uint8_t *key_buffer, size_t key_buffer_size,
457 uint8_t *data, size_t data_size, size_t *data_length )
458
459{
460 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
461 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
462 psa_get_key_lifetime( attributes ) );
463
Ronald Cron152e3f82020-11-26 16:06:41 +0100464 /* Try dynamically-registered SE interface first */
465#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
466 const psa_drv_se_t *drv;
467 psa_drv_se_context_t *drv_context;
468
469 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
470 {
471 if( ( drv->key_management == NULL ) ||
472 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100473 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100474 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100475 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100476
477 return( drv->key_management->p_export(
478 drv_context,
479 *( (psa_key_slot_number_t *)key_buffer ),
480 data, data_size, data_length ) );
481 }
482#endif /* PSA_CRYPTO_SE_C */
483
Ronald Cron67227982020-11-26 15:16:05 +0100484 switch( location )
485 {
486 case PSA_KEY_LOCATION_LOCAL_STORAGE:
487 return( psa_export_key_internal( attributes,
488 key_buffer,
489 key_buffer_size,
490 data,
491 data_size,
492 data_length ) );
493
494 /* Add cases for opaque driver here */
495#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
496#if defined(PSA_CRYPTO_DRIVER_TEST)
497 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
498 return( test_opaque_export_key( attributes,
499 key_buffer,
500 key_buffer_size,
501 data,
502 data_size,
503 data_length ) );
504#endif /* PSA_CRYPTO_DRIVER_TEST */
505#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
506 default:
507 /* Key is declared with a lifetime not known to us */
508 return( status );
509 }
510}
511
Ronald Cron84cc9942020-11-25 14:30:05 +0100512psa_status_t psa_driver_wrapper_export_public_key(
513 const psa_key_attributes_t *attributes,
514 const uint8_t *key_buffer, size_t key_buffer_size,
515 uint8_t *data, size_t data_size, size_t *data_length )
516
Steven Cooremanb9b84422020-10-14 14:39:20 +0200517{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200518 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100519 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
520 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200521
Ronald Cron152e3f82020-11-26 16:06:41 +0100522 /* Try dynamically-registered SE interface first */
523#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
524 const psa_drv_se_t *drv;
525 psa_drv_se_context_t *drv_context;
526
527 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
528 {
529 if( ( drv->key_management == NULL ) ||
530 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100531 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100532 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100533 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100534
535 return( drv->key_management->p_export_public(
536 drv_context,
537 *( (psa_key_slot_number_t *)key_buffer ),
538 data, data_size, data_length ) );
539 }
540#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
541
Steven Cooremanb9b84422020-10-14 14:39:20 +0200542 switch( location )
543 {
544 case PSA_KEY_LOCATION_LOCAL_STORAGE:
545 /* Key is stored in the slot in export representation, so
546 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100547#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200548#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron84cc9942020-11-25 14:30:05 +0100549 status = test_transparent_export_public_key( attributes,
550 key_buffer,
551 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200552 data,
553 data_size,
554 data_length );
555 /* Declared with fallback == true */
556 if( status != PSA_ERROR_NOT_SUPPORTED )
557 return( status );
558#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100559#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200560 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100561 return( psa_export_public_key_internal( attributes,
562 key_buffer,
563 key_buffer_size,
564 data,
565 data_size,
566 data_length ) );
567
Steven Cooremanb9b84422020-10-14 14:39:20 +0200568 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100569#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200570#if defined(PSA_CRYPTO_DRIVER_TEST)
571 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron84cc9942020-11-25 14:30:05 +0100572 return( test_opaque_export_public_key( attributes,
573 key_buffer,
574 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200575 data,
576 data_size,
577 data_length ) );
578#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100579#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200580 default:
581 /* Key is declared with a lifetime not known to us */
582 return( status );
583 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200584}
585
Steven Cooreman37941cb2020-07-28 18:49:51 +0200586/*
587 * Cipher functions
588 */
589psa_status_t psa_driver_wrapper_cipher_encrypt(
590 psa_key_slot_t *slot,
591 psa_algorithm_t alg,
592 const uint8_t *input,
593 size_t input_length,
594 uint8_t *output,
595 size_t output_size,
596 size_t *output_length )
597{
598#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
599 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
600 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
601 psa_key_attributes_t attributes = {
602 .core = slot->attr
603 };
604
605 switch( location )
606 {
607 case PSA_KEY_LOCATION_LOCAL_STORAGE:
608 /* Key is stored in the slot in export representation, so
609 * cycle through all known transparent accelerators */
610#if defined(PSA_CRYPTO_DRIVER_TEST)
611 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100612 slot->key.data,
613 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200614 alg,
615 input,
616 input_length,
617 output,
618 output_size,
619 output_length );
620 /* Declared with fallback == true */
621 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200622 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200623#endif /* PSA_CRYPTO_DRIVER_TEST */
624 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200625 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200626 /* Add cases for opaque driver here */
627#if defined(PSA_CRYPTO_DRIVER_TEST)
628 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
629 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100630 slot->key.data,
631 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200632 alg,
633 input,
634 input_length,
635 output,
636 output_size,
637 output_length ) );
638#endif /* PSA_CRYPTO_DRIVER_TEST */
639 default:
640 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200641 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200642 }
643#else /* PSA_CRYPTO_DRIVER_PRESENT */
644 (void) slot;
645 (void) alg;
646 (void) input;
647 (void) input_length;
648 (void) output;
649 (void) output_size;
650 (void) output_length;
651
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200652 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200653#endif /* PSA_CRYPTO_DRIVER_PRESENT */
654}
655
656psa_status_t psa_driver_wrapper_cipher_decrypt(
657 psa_key_slot_t *slot,
658 psa_algorithm_t alg,
659 const uint8_t *input,
660 size_t input_length,
661 uint8_t *output,
662 size_t output_size,
663 size_t *output_length )
664{
665#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
666 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
667 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
668 psa_key_attributes_t attributes = {
669 .core = slot->attr
670 };
671
672 switch( location )
673 {
674 case PSA_KEY_LOCATION_LOCAL_STORAGE:
675 /* Key is stored in the slot in export representation, so
676 * cycle through all known transparent accelerators */
677#if defined(PSA_CRYPTO_DRIVER_TEST)
678 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100679 slot->key.data,
680 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200681 alg,
682 input,
683 input_length,
684 output,
685 output_size,
686 output_length );
687 /* Declared with fallback == true */
688 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200689 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200690#endif /* PSA_CRYPTO_DRIVER_TEST */
691 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200692 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200693 /* Add cases for opaque driver here */
694#if defined(PSA_CRYPTO_DRIVER_TEST)
695 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
696 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100697 slot->key.data,
698 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200699 alg,
700 input,
701 input_length,
702 output,
703 output_size,
704 output_length ) );
705#endif /* PSA_CRYPTO_DRIVER_TEST */
706 default:
707 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200708 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200709 }
710#else /* PSA_CRYPTO_DRIVER_PRESENT */
711 (void) slot;
712 (void) alg;
713 (void) input;
714 (void) input_length;
715 (void) output;
716 (void) output_size;
717 (void) output_length;
718
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200719 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200720#endif /* PSA_CRYPTO_DRIVER_PRESENT */
721}
722
723psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200724 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200725 psa_key_slot_t *slot,
726 psa_algorithm_t alg )
727{
728#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
729 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
730 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
731 psa_key_attributes_t attributes = {
732 .core = slot->attr
733 };
734
Steven Cooreman37941cb2020-07-28 18:49:51 +0200735 switch( location )
736 {
737 case PSA_KEY_LOCATION_LOCAL_STORAGE:
738 /* Key is stored in the slot in export representation, so
739 * cycle through all known transparent accelerators */
740#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200741 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
742 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200743 return PSA_ERROR_INSUFFICIENT_MEMORY;
744
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200745 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200746 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100747 slot->key.data,
748 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200749 alg );
750 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200751 if( status == PSA_SUCCESS )
752 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200753 else
754 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200755 mbedtls_platform_zeroize(
756 operation->ctx,
757 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200758 mbedtls_free( operation->ctx );
759 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200760 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200761
762 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200763#endif /* PSA_CRYPTO_DRIVER_TEST */
764 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200765 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200766 /* Add cases for opaque driver here */
767#if defined(PSA_CRYPTO_DRIVER_TEST)
768 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200769 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
770 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200771 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200772
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200773 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200774 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100775 slot->key.data,
776 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200777 alg );
778 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200779 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200780 else
781 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200782 mbedtls_platform_zeroize(
783 operation->ctx,
784 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200785 mbedtls_free( operation->ctx );
786 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200787 }
788
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200789 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200790#endif /* PSA_CRYPTO_DRIVER_TEST */
791 default:
792 /* Key is declared with a lifetime not known to us */
John Durkop714e3a12020-09-29 22:07:04 -0700793 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200794 }
795#else /* PSA_CRYPTO_DRIVER_PRESENT */
796 (void)slot;
797 (void)alg;
798 (void)operation;
799
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200800 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200801#endif /* PSA_CRYPTO_DRIVER_PRESENT */
802}
803
804psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200805 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200806 psa_key_slot_t *slot,
807 psa_algorithm_t alg )
808{
809#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
810 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
811 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
812 psa_key_attributes_t attributes = {
813 .core = slot->attr
814 };
815
Steven Cooreman37941cb2020-07-28 18:49:51 +0200816 switch( location )
817 {
818 case PSA_KEY_LOCATION_LOCAL_STORAGE:
819 /* Key is stored in the slot in export representation, so
820 * cycle through all known transparent accelerators */
821#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200822 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
823 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200824 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200825
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200826 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200827 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100828 slot->key.data,
829 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200830 alg );
831 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200832 if( status == PSA_SUCCESS )
833 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200834 else
835 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200836 mbedtls_platform_zeroize(
837 operation->ctx,
838 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200839 mbedtls_free( operation->ctx );
840 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200841 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200842
843 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200844#endif /* PSA_CRYPTO_DRIVER_TEST */
845 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200846 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200847 /* Add cases for opaque driver here */
848#if defined(PSA_CRYPTO_DRIVER_TEST)
849 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200850 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
851 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200852 return PSA_ERROR_INSUFFICIENT_MEMORY;
853
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200854 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200855 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100856 slot->key.data,
857 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200858 alg );
859 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200860 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200861 else
862 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200863 mbedtls_platform_zeroize(
864 operation->ctx,
865 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200866 mbedtls_free( operation->ctx );
867 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200868 }
869
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200870 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200871#endif /* PSA_CRYPTO_DRIVER_TEST */
872 default:
873 /* Key is declared with a lifetime not known to us */
John Durkop814dca72020-10-05 06:31:12 -0700874 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200875 }
876#else /* PSA_CRYPTO_DRIVER_PRESENT */
877 (void)slot;
878 (void)alg;
879 (void)operation;
880
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200881 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200882#endif /* PSA_CRYPTO_DRIVER_PRESENT */
883}
884
885psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200886 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200887 uint8_t *iv,
888 size_t iv_size,
889 size_t *iv_length )
890{
891#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200892 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200893 {
894#if defined(PSA_CRYPTO_DRIVER_TEST)
895 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200896 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200897 iv,
898 iv_size,
899 iv_length ) );
900#endif /* PSA_CRYPTO_DRIVER_TEST */
901#if defined(PSA_CRYPTO_DRIVER_TEST)
902 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200903 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200904 iv,
905 iv_size,
906 iv_length ) );
907#endif /* PSA_CRYPTO_DRIVER_TEST */
908 default:
909 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200910 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200911 }
912#else /* PSA_CRYPTO_DRIVER_PRESENT */
913 (void) operation;
914 (void) iv;
915 (void) iv_size;
916 (void) iv_length;
917
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200918 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200919#endif /* PSA_CRYPTO_DRIVER_PRESENT */
920}
921
922psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200923 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200924 const uint8_t *iv,
925 size_t iv_length )
926{
927#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200928 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200929 {
930#if defined(PSA_CRYPTO_DRIVER_TEST)
931 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200932 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200933 iv,
934 iv_length ) );
935#endif /* PSA_CRYPTO_DRIVER_TEST */
936#if defined(PSA_CRYPTO_DRIVER_TEST)
937 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200938 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200939 iv,
940 iv_length ) );
941#endif /* PSA_CRYPTO_DRIVER_TEST */
942 default:
943 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200944 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200945 }
946#else /* PSA_CRYPTO_DRIVER_PRESENT */
947 (void) operation;
948 (void) iv;
949 (void) iv_length;
950
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200951 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200952#endif /* PSA_CRYPTO_DRIVER_PRESENT */
953}
954
955psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200956 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200957 const uint8_t *input,
958 size_t input_length,
959 uint8_t *output,
960 size_t output_size,
961 size_t *output_length )
962{
963#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200964 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200965 {
966#if defined(PSA_CRYPTO_DRIVER_TEST)
967 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200968 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200969 input,
970 input_length,
971 output,
972 output_size,
973 output_length ) );
974#endif /* PSA_CRYPTO_DRIVER_TEST */
975#if defined(PSA_CRYPTO_DRIVER_TEST)
976 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200977 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200978 input,
979 input_length,
980 output,
981 output_size,
982 output_length ) );
983#endif /* PSA_CRYPTO_DRIVER_TEST */
984 default:
985 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200986 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200987 }
988#else /* PSA_CRYPTO_DRIVER_PRESENT */
989 (void) operation;
990 (void) input;
991 (void) input_length;
992 (void) output;
993 (void) output_length;
994 (void) output_size;
995
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200996 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200997#endif /* PSA_CRYPTO_DRIVER_PRESENT */
998}
999
1000psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001001 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001002 uint8_t *output,
1003 size_t output_size,
1004 size_t *output_length )
1005{
1006#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001007 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001008 {
1009#if defined(PSA_CRYPTO_DRIVER_TEST)
1010 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001011 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001012 output,
1013 output_size,
1014 output_length ) );
1015#endif /* PSA_CRYPTO_DRIVER_TEST */
1016#if defined(PSA_CRYPTO_DRIVER_TEST)
1017 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001018 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001019 output,
1020 output_size,
1021 output_length ) );
1022#endif /* PSA_CRYPTO_DRIVER_TEST */
1023 default:
1024 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001025 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001026 }
1027#else /* PSA_CRYPTO_DRIVER_PRESENT */
1028 (void) operation;
1029 (void) output;
1030 (void) output_size;
1031 (void) output_length;
1032
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001033 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001034#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1035}
1036
1037psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001038 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001039{
1040#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1041 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001042
1043 /* The object has (apparently) been initialized but it is not in use. It's
1044 * ok to call abort on such an object, and there's nothing to do. */
1045 if( operation->ctx == NULL && operation->id == 0 )
1046 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001047
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001048 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001049 {
1050#if defined(PSA_CRYPTO_DRIVER_TEST)
1051 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001052 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001053 mbedtls_platform_zeroize(
1054 operation->ctx,
1055 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001056 mbedtls_free( operation->ctx );
1057 operation->ctx = NULL;
1058 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001059
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001060 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001061#endif /* PSA_CRYPTO_DRIVER_TEST */
1062#if defined(PSA_CRYPTO_DRIVER_TEST)
1063 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001064 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001065 mbedtls_platform_zeroize(
1066 operation->ctx,
1067 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001068 mbedtls_free( operation->ctx );
1069 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001070 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001071
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001072 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001073#endif /* PSA_CRYPTO_DRIVER_TEST */
1074 default:
1075 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001076 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001077 }
1078#else /* PSA_CRYPTO_DRIVER_PRESENT */
1079 (void)operation;
1080
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001081 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001082#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1083}
1084
Steven Cooremancd84cb42020-07-16 20:28:36 +02001085/* End of automatically generated file. */