blob: 6cf23cef9a00f3658eedf84c390a71351be1b453 [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 */
Ronald Cron9f17aa42020-12-08 17:07:25 +010060psa_status_t psa_driver_wrapper_sign_hash(
61 const psa_key_attributes_t *attributes,
62 const uint8_t *key_buffer, size_t key_buffer_size,
63 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
64 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +020065{
Steven Cooreman7a250572020-07-17 16:43:05 +020066 /* Try dynamically-registered SE interface first */
67#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
68 const psa_drv_se_t *drv;
69 psa_drv_se_context_t *drv_context;
70
Ronald Cron9f17aa42020-12-08 17:07:25 +010071 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +020072 {
73 if( drv->asymmetric == NULL ||
74 drv->asymmetric->p_sign == NULL )
75 {
76 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +020077 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +020078 }
Ronald Cron9f17aa42020-12-08 17:07:25 +010079 return( drv->asymmetric->p_sign(
80 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
81 alg, hash, hash_length,
82 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +020083 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +020084#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +020085
Ronald Cronfce9df22020-12-08 18:06:03 +010086 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +010087 psa_key_location_t location =
88 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +020089
90 switch( location )
91 {
92 case PSA_KEY_LOCATION_LOCAL_STORAGE:
93 /* Key is stored in the slot in export representation, so
94 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +010095#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +020096#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +010097 status = test_transparent_signature_sign_hash( attributes,
98 key_buffer,
99 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200100 alg,
101 hash,
102 hash_length,
103 signature,
104 signature_size,
105 signature_length );
106 /* Declared with fallback == true */
107 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200108 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200109#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100110#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200111 /* Fell through, meaning no accelerator supports this operation */
Ronald Cronfce9df22020-12-08 18:06:03 +0100112 return( psa_sign_hash_internal( attributes,
113 key_buffer,
114 key_buffer_size,
115 alg,
116 hash,
117 hash_length,
118 signature,
119 signature_size,
120 signature_length ) );
121
Steven Cooremancd84cb42020-07-16 20:28:36 +0200122 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100123#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200124#if defined(PSA_CRYPTO_DRIVER_TEST)
125 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100126 return( test_opaque_signature_sign_hash( attributes,
127 key_buffer,
128 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200129 alg,
130 hash,
131 hash_length,
132 signature,
133 signature_size,
134 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200135#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100136#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200137 default:
138 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100139 (void)status;
140 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200141 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200142}
143
Ronald Cron9f17aa42020-12-08 17:07:25 +0100144psa_status_t psa_driver_wrapper_verify_hash(
145 const psa_key_attributes_t *attributes,
146 const uint8_t *key_buffer, size_t key_buffer_size,
147 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
148 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200149{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200150 /* Try dynamically-registered SE interface first */
151#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
152 const psa_drv_se_t *drv;
153 psa_drv_se_context_t *drv_context;
154
Ronald Cron9f17aa42020-12-08 17:07:25 +0100155 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200156 {
157 if( drv->asymmetric == NULL ||
158 drv->asymmetric->p_verify == NULL )
159 {
160 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200161 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200162 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100163 return( drv->asymmetric->p_verify(
164 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
165 alg, hash, hash_length,
166 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200167 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200168#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200169
Ronald Cronfce9df22020-12-08 18:06:03 +0100170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100171 psa_key_location_t location =
172 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200173
174 switch( location )
175 {
176 case PSA_KEY_LOCATION_LOCAL_STORAGE:
177 /* Key is stored in the slot in export representation, so
178 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100179#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200180#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100181 status = test_transparent_signature_verify_hash( attributes,
182 key_buffer,
183 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200184 alg,
185 hash,
186 hash_length,
187 signature,
188 signature_length );
189 /* Declared with fallback == true */
190 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200191 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200192#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100193#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
194
195 return( psa_verify_hash_internal( attributes,
196 key_buffer,
197 key_buffer_size,
198 alg,
199 hash,
200 hash_length,
201 signature,
202 signature_length ) );
203
Steven Cooreman55ae2172020-07-17 19:46:15 +0200204 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100205#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200206#if defined(PSA_CRYPTO_DRIVER_TEST)
207 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100208 return( test_opaque_signature_verify_hash( attributes,
209 key_buffer,
210 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200211 alg,
212 hash,
213 hash_length,
214 signature,
215 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200216#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100217#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200218 default:
219 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100220 (void)status;
221 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200222 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200223}
224
Ronald Cron31216282020-12-05 18:47:56 +0100225/** Get the key buffer size for the key material of a generated key in the
226 * case of an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200227 *
Ronald Cron31216282020-12-05 18:47:56 +0100228 * \param[in] attributes The key attributes.
229 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200230 *
231 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100232 * The minimum size for a buffer to contain the key material has been
233 * returned successfully.
234 * \retval #PSA_ERROR_INVALID_ARGUMENT
235 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200236 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100237 * The type and/or the size in bits of the key or the combination of
238 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200239 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100240psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100241 const psa_key_attributes_t *attributes,
242 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200243{
John Durkop2c618352020-09-22 06:54:01 -0700244 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
245 psa_key_type_t key_type = attributes->core.type;
246 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200247
Ronald Cron31216282020-12-05 18:47:56 +0100248 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700249 switch( location )
250 {
John Durkop2c618352020-09-22 06:54:01 -0700251#if defined(PSA_CRYPTO_DRIVER_TEST)
252 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
John Durkopac93e3b2020-10-16 06:48:55 -0700253#ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
Ronald Cron31216282020-12-05 18:47:56 +0100254 *key_buffer_size = test_size_function( key_type, key_bits );
John Durkop2c618352020-09-22 06:54:01 -0700255 return( PSA_SUCCESS );
256#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
257 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
258 {
Ronald Cron31216282020-12-05 18:47:56 +0100259 int public_key_overhead =
260 ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ?
261 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
262 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop135ce692020-10-19 07:12:28 -0700263 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
264 + public_key_overhead;
265 }
Ronald Cron31216282020-12-05 18:47:56 +0100266 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
John Durkop135ce692020-10-19 07:12:28 -0700267 {
Ronald Cron31216282020-12-05 18:47:56 +0100268 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700269 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
270 }
John Durkop135ce692020-10-19 07:12:28 -0700271 else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
Ronald Cron31216282020-12-05 18:47:56 +0100272 !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
John Durkop2c618352020-09-22 06:54:01 -0700273 {
Ronald Cron31216282020-12-05 18:47:56 +0100274 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700275 + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
276 * ( ( key_bits + 7 ) / 8 );
277 }
278 else
279 {
280 return( PSA_ERROR_NOT_SUPPORTED );
281 }
282 return( PSA_SUCCESS );
283#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
284#endif /* PSA_CRYPTO_DRIVER_TEST */
285
286 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100287 (void)key_type;
288 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200289 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200290 }
291}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200292
Ronald Cron977c2472020-10-13 08:32:21 +0200293psa_status_t psa_driver_wrapper_generate_key(
294 const psa_key_attributes_t *attributes,
295 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200296{
Ronald Cron977c2472020-10-13 08:32:21 +0200297 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
298 psa_key_location_t location =
299 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
300
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200301 /* Try dynamically-registered SE interface first */
302#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
303 const psa_drv_se_t *drv;
304 psa_drv_se_context_t *drv_context;
305
Ronald Cron977c2472020-10-13 08:32:21 +0200306 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200307 {
308 size_t pubkey_length = 0; /* We don't support this feature yet */
309 if( drv->key_management == NULL ||
310 drv->key_management->p_generate == NULL )
311 {
312 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200313 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200314 }
315 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200316 drv_context,
317 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100318 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200319 }
320#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
321
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200322 switch( location )
323 {
324 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200325#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200326 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200327 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200328 {
Ronald Cron977c2472020-10-13 08:32:21 +0200329 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200330#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron977c2472020-10-13 08:32:21 +0200331 status = test_transparent_generate_key(
332 attributes, key_buffer, key_buffer_size,
333 key_buffer_length );
334 /* Declared with fallback == true */
335 if( status != PSA_ERROR_NOT_SUPPORTED )
336 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200337#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200338 }
339#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
340
341 /* Software fallback */
342 status = psa_generate_key_internal(
343 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200344 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200345
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200346 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200347#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200348#if defined(PSA_CRYPTO_DRIVER_TEST)
349 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron977c2472020-10-13 08:32:21 +0200350 status = test_opaque_generate_key(
351 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200352 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200353#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200354#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
355
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200356 default:
357 /* Key is declared with a lifetime not known to us */
358 status = PSA_ERROR_INVALID_ARGUMENT;
359 break;
360 }
361
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200362 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200363}
364
Ronald Cron83282872020-11-22 14:02:39 +0100365psa_status_t psa_driver_wrapper_import_key(
366 const psa_key_attributes_t *attributes,
367 const uint8_t *data,
368 size_t data_length,
369 uint8_t *key_buffer,
370 size_t key_buffer_size,
371 size_t *key_buffer_length,
372 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200373{
Steven Cooreman04524762020-10-13 17:43:44 +0200374 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100375 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
376 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200377
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100378 /* Try dynamically-registered SE interface first */
379#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
380 const psa_drv_se_t *drv;
381 psa_drv_se_context_t *drv_context;
382
383 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
384 {
385 if( drv->key_management == NULL ||
386 drv->key_management->p_import == NULL )
387 return( PSA_ERROR_NOT_SUPPORTED );
388
389 /* The driver should set the number of key bits, however in
390 * case it doesn't, we initialize bits to an invalid value. */
391 *bits = PSA_MAX_KEY_BITS + 1;
392 status = drv->key_management->p_import(
393 drv_context,
394 *( (psa_key_slot_number_t *)key_buffer ),
395 attributes, data, data_length, bits );
396
397 if( status != PSA_SUCCESS )
398 return( status );
399
400 if( (*bits) > PSA_MAX_KEY_BITS )
401 return( PSA_ERROR_NOT_SUPPORTED );
402
403 return( PSA_SUCCESS );
404 }
405#endif /* PSA_CRYPTO_SE_C */
406
Ronald Cronbf33c932020-11-28 18:06:53 +0100407 switch( location )
408 {
409 case PSA_KEY_LOCATION_LOCAL_STORAGE:
410 /* Key is stored in the slot in export representation, so
411 * cycle through all known transparent accelerators */
412#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
413#if defined(PSA_CRYPTO_DRIVER_TEST)
414 status = test_transparent_import_key( attributes,
415 data, data_length,
416 key_buffer, key_buffer_size,
417 key_buffer_length, bits );
418 /* Declared with fallback == true */
419 if( status != PSA_ERROR_NOT_SUPPORTED )
420 return( status );
421#endif /* PSA_CRYPTO_DRIVER_TEST */
422#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
423 /* Fell through, meaning no accelerator supports this operation */
424 return( psa_import_key_into_slot( attributes,
425 data, data_length,
426 key_buffer, key_buffer_size,
427 key_buffer_length, bits ) );
428
429 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100430 /* Importing a key with external storage in not yet supported.
431 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100432 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100433 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100434 }
435
Steven Cooreman04524762020-10-13 17:43:44 +0200436}
437
Ronald Cron67227982020-11-26 15:16:05 +0100438psa_status_t psa_driver_wrapper_export_key(
439 const psa_key_attributes_t *attributes,
440 const uint8_t *key_buffer, size_t key_buffer_size,
441 uint8_t *data, size_t data_size, size_t *data_length )
442
443{
444 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
445 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
446 psa_get_key_lifetime( attributes ) );
447
Ronald Cron152e3f82020-11-26 16:06:41 +0100448 /* Try dynamically-registered SE interface first */
449#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
450 const psa_drv_se_t *drv;
451 psa_drv_se_context_t *drv_context;
452
453 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
454 {
455 if( ( drv->key_management == NULL ) ||
456 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100457 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100458 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100459 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100460
461 return( drv->key_management->p_export(
462 drv_context,
463 *( (psa_key_slot_number_t *)key_buffer ),
464 data, data_size, data_length ) );
465 }
466#endif /* PSA_CRYPTO_SE_C */
467
Ronald Cron67227982020-11-26 15:16:05 +0100468 switch( location )
469 {
470 case PSA_KEY_LOCATION_LOCAL_STORAGE:
471 return( psa_export_key_internal( attributes,
472 key_buffer,
473 key_buffer_size,
474 data,
475 data_size,
476 data_length ) );
477
478 /* Add cases for opaque driver here */
479#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
480#if defined(PSA_CRYPTO_DRIVER_TEST)
481 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
482 return( test_opaque_export_key( attributes,
483 key_buffer,
484 key_buffer_size,
485 data,
486 data_size,
487 data_length ) );
488#endif /* PSA_CRYPTO_DRIVER_TEST */
489#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
490 default:
491 /* Key is declared with a lifetime not known to us */
492 return( status );
493 }
494}
495
Ronald Cron84cc9942020-11-25 14:30:05 +0100496psa_status_t psa_driver_wrapper_export_public_key(
497 const psa_key_attributes_t *attributes,
498 const uint8_t *key_buffer, size_t key_buffer_size,
499 uint8_t *data, size_t data_size, size_t *data_length )
500
Steven Cooremanb9b84422020-10-14 14:39:20 +0200501{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200502 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100503 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
504 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200505
Ronald Cron152e3f82020-11-26 16:06:41 +0100506 /* Try dynamically-registered SE interface first */
507#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
508 const psa_drv_se_t *drv;
509 psa_drv_se_context_t *drv_context;
510
511 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
512 {
513 if( ( drv->key_management == NULL ) ||
514 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100515 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100516 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100517 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100518
519 return( drv->key_management->p_export_public(
520 drv_context,
521 *( (psa_key_slot_number_t *)key_buffer ),
522 data, data_size, data_length ) );
523 }
524#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
525
Steven Cooremanb9b84422020-10-14 14:39:20 +0200526 switch( location )
527 {
528 case PSA_KEY_LOCATION_LOCAL_STORAGE:
529 /* Key is stored in the slot in export representation, so
530 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100531#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200532#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron84cc9942020-11-25 14:30:05 +0100533 status = test_transparent_export_public_key( attributes,
534 key_buffer,
535 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200536 data,
537 data_size,
538 data_length );
539 /* Declared with fallback == true */
540 if( status != PSA_ERROR_NOT_SUPPORTED )
541 return( status );
542#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100543#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200544 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100545 return( psa_export_public_key_internal( attributes,
546 key_buffer,
547 key_buffer_size,
548 data,
549 data_size,
550 data_length ) );
551
Steven Cooremanb9b84422020-10-14 14:39:20 +0200552 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100553#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200554#if defined(PSA_CRYPTO_DRIVER_TEST)
555 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron84cc9942020-11-25 14:30:05 +0100556 return( test_opaque_export_public_key( attributes,
557 key_buffer,
558 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200559 data,
560 data_size,
561 data_length ) );
562#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100563#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200564 default:
565 /* Key is declared with a lifetime not known to us */
566 return( status );
567 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200568}
569
Steven Cooreman37941cb2020-07-28 18:49:51 +0200570/*
571 * Cipher functions
572 */
573psa_status_t psa_driver_wrapper_cipher_encrypt(
574 psa_key_slot_t *slot,
575 psa_algorithm_t alg,
576 const uint8_t *input,
577 size_t input_length,
578 uint8_t *output,
579 size_t output_size,
580 size_t *output_length )
581{
582#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
583 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
584 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
585 psa_key_attributes_t attributes = {
586 .core = slot->attr
587 };
588
589 switch( location )
590 {
591 case PSA_KEY_LOCATION_LOCAL_STORAGE:
592 /* Key is stored in the slot in export representation, so
593 * cycle through all known transparent accelerators */
594#if defined(PSA_CRYPTO_DRIVER_TEST)
595 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100596 slot->key.data,
597 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200598 alg,
599 input,
600 input_length,
601 output,
602 output_size,
603 output_length );
604 /* Declared with fallback == true */
605 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200606 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200607#endif /* PSA_CRYPTO_DRIVER_TEST */
608 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200609 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200610 /* Add cases for opaque driver here */
611#if defined(PSA_CRYPTO_DRIVER_TEST)
612 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
613 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100614 slot->key.data,
615 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200616 alg,
617 input,
618 input_length,
619 output,
620 output_size,
621 output_length ) );
622#endif /* PSA_CRYPTO_DRIVER_TEST */
623 default:
624 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200625 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200626 }
627#else /* PSA_CRYPTO_DRIVER_PRESENT */
628 (void) slot;
629 (void) alg;
630 (void) input;
631 (void) input_length;
632 (void) output;
633 (void) output_size;
634 (void) output_length;
635
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200636 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200637#endif /* PSA_CRYPTO_DRIVER_PRESENT */
638}
639
640psa_status_t psa_driver_wrapper_cipher_decrypt(
641 psa_key_slot_t *slot,
642 psa_algorithm_t alg,
643 const uint8_t *input,
644 size_t input_length,
645 uint8_t *output,
646 size_t output_size,
647 size_t *output_length )
648{
649#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
650 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
651 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
652 psa_key_attributes_t attributes = {
653 .core = slot->attr
654 };
655
656 switch( location )
657 {
658 case PSA_KEY_LOCATION_LOCAL_STORAGE:
659 /* Key is stored in the slot in export representation, so
660 * cycle through all known transparent accelerators */
661#if defined(PSA_CRYPTO_DRIVER_TEST)
662 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100663 slot->key.data,
664 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200665 alg,
666 input,
667 input_length,
668 output,
669 output_size,
670 output_length );
671 /* Declared with fallback == true */
672 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200673 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200674#endif /* PSA_CRYPTO_DRIVER_TEST */
675 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200676 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200677 /* Add cases for opaque driver here */
678#if defined(PSA_CRYPTO_DRIVER_TEST)
679 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
680 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100681 slot->key.data,
682 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200683 alg,
684 input,
685 input_length,
686 output,
687 output_size,
688 output_length ) );
689#endif /* PSA_CRYPTO_DRIVER_TEST */
690 default:
691 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200692 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200693 }
694#else /* PSA_CRYPTO_DRIVER_PRESENT */
695 (void) slot;
696 (void) alg;
697 (void) input;
698 (void) input_length;
699 (void) output;
700 (void) output_size;
701 (void) output_length;
702
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200703 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200704#endif /* PSA_CRYPTO_DRIVER_PRESENT */
705}
706
707psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200708 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200709 psa_key_slot_t *slot,
710 psa_algorithm_t alg )
711{
712#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
713 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
714 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
715 psa_key_attributes_t attributes = {
716 .core = slot->attr
717 };
718
Steven Cooreman37941cb2020-07-28 18:49:51 +0200719 switch( location )
720 {
721 case PSA_KEY_LOCATION_LOCAL_STORAGE:
722 /* Key is stored in the slot in export representation, so
723 * cycle through all known transparent accelerators */
724#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200725 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
726 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200727 return PSA_ERROR_INSUFFICIENT_MEMORY;
728
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200729 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200730 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100731 slot->key.data,
732 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200733 alg );
734 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200735 if( status == PSA_SUCCESS )
736 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200737 else
738 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200739 mbedtls_platform_zeroize(
740 operation->ctx,
741 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200742 mbedtls_free( operation->ctx );
743 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200744 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200745
746 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200747#endif /* PSA_CRYPTO_DRIVER_TEST */
748 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200749 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200750 /* Add cases for opaque driver here */
751#if defined(PSA_CRYPTO_DRIVER_TEST)
752 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200753 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
754 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200755 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200756
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200757 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200758 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100759 slot->key.data,
760 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200761 alg );
762 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200763 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200764 else
765 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200766 mbedtls_platform_zeroize(
767 operation->ctx,
768 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200769 mbedtls_free( operation->ctx );
770 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200771 }
772
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200773 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200774#endif /* PSA_CRYPTO_DRIVER_TEST */
775 default:
776 /* Key is declared with a lifetime not known to us */
John Durkop714e3a12020-09-29 22:07:04 -0700777 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200778 }
779#else /* PSA_CRYPTO_DRIVER_PRESENT */
780 (void)slot;
781 (void)alg;
782 (void)operation;
783
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200784 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200785#endif /* PSA_CRYPTO_DRIVER_PRESENT */
786}
787
788psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200789 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200790 psa_key_slot_t *slot,
791 psa_algorithm_t alg )
792{
793#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
794 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
795 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
796 psa_key_attributes_t attributes = {
797 .core = slot->attr
798 };
799
Steven Cooreman37941cb2020-07-28 18:49:51 +0200800 switch( location )
801 {
802 case PSA_KEY_LOCATION_LOCAL_STORAGE:
803 /* Key is stored in the slot in export representation, so
804 * cycle through all known transparent accelerators */
805#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200806 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
807 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200808 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200809
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200810 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200811 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100812 slot->key.data,
813 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200814 alg );
815 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200816 if( status == PSA_SUCCESS )
817 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200818 else
819 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200820 mbedtls_platform_zeroize(
821 operation->ctx,
822 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200823 mbedtls_free( operation->ctx );
824 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200825 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200826
827 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200828#endif /* PSA_CRYPTO_DRIVER_TEST */
829 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200830 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200831 /* Add cases for opaque driver here */
832#if defined(PSA_CRYPTO_DRIVER_TEST)
833 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200834 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
835 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200836 return PSA_ERROR_INSUFFICIENT_MEMORY;
837
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200838 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200839 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100840 slot->key.data,
841 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200842 alg );
843 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200844 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200845 else
846 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200847 mbedtls_platform_zeroize(
848 operation->ctx,
849 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200850 mbedtls_free( operation->ctx );
851 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200852 }
853
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200854 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200855#endif /* PSA_CRYPTO_DRIVER_TEST */
856 default:
857 /* Key is declared with a lifetime not known to us */
John Durkop814dca72020-10-05 06:31:12 -0700858 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200859 }
860#else /* PSA_CRYPTO_DRIVER_PRESENT */
861 (void)slot;
862 (void)alg;
863 (void)operation;
864
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200865 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200866#endif /* PSA_CRYPTO_DRIVER_PRESENT */
867}
868
869psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200870 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200871 uint8_t *iv,
872 size_t iv_size,
873 size_t *iv_length )
874{
875#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200876 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200877 {
878#if defined(PSA_CRYPTO_DRIVER_TEST)
879 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200880 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200881 iv,
882 iv_size,
883 iv_length ) );
884#endif /* PSA_CRYPTO_DRIVER_TEST */
885#if defined(PSA_CRYPTO_DRIVER_TEST)
886 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200887 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200888 iv,
889 iv_size,
890 iv_length ) );
891#endif /* PSA_CRYPTO_DRIVER_TEST */
892 default:
893 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200894 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200895 }
896#else /* PSA_CRYPTO_DRIVER_PRESENT */
897 (void) operation;
898 (void) iv;
899 (void) iv_size;
900 (void) iv_length;
901
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200902 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200903#endif /* PSA_CRYPTO_DRIVER_PRESENT */
904}
905
906psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200907 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200908 const uint8_t *iv,
909 size_t iv_length )
910{
911#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200912 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200913 {
914#if defined(PSA_CRYPTO_DRIVER_TEST)
915 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200916 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200917 iv,
918 iv_length ) );
919#endif /* PSA_CRYPTO_DRIVER_TEST */
920#if defined(PSA_CRYPTO_DRIVER_TEST)
921 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200922 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200923 iv,
924 iv_length ) );
925#endif /* PSA_CRYPTO_DRIVER_TEST */
926 default:
927 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200928 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200929 }
930#else /* PSA_CRYPTO_DRIVER_PRESENT */
931 (void) operation;
932 (void) iv;
933 (void) iv_length;
934
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200935 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200936#endif /* PSA_CRYPTO_DRIVER_PRESENT */
937}
938
939psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200940 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200941 const uint8_t *input,
942 size_t input_length,
943 uint8_t *output,
944 size_t output_size,
945 size_t *output_length )
946{
947#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200948 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200949 {
950#if defined(PSA_CRYPTO_DRIVER_TEST)
951 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200952 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200953 input,
954 input_length,
955 output,
956 output_size,
957 output_length ) );
958#endif /* PSA_CRYPTO_DRIVER_TEST */
959#if defined(PSA_CRYPTO_DRIVER_TEST)
960 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200961 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200962 input,
963 input_length,
964 output,
965 output_size,
966 output_length ) );
967#endif /* PSA_CRYPTO_DRIVER_TEST */
968 default:
969 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200970 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200971 }
972#else /* PSA_CRYPTO_DRIVER_PRESENT */
973 (void) operation;
974 (void) input;
975 (void) input_length;
976 (void) output;
977 (void) output_length;
978 (void) output_size;
979
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200980 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200981#endif /* PSA_CRYPTO_DRIVER_PRESENT */
982}
983
984psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200985 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200986 uint8_t *output,
987 size_t output_size,
988 size_t *output_length )
989{
990#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200991 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200992 {
993#if defined(PSA_CRYPTO_DRIVER_TEST)
994 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200995 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200996 output,
997 output_size,
998 output_length ) );
999#endif /* PSA_CRYPTO_DRIVER_TEST */
1000#if defined(PSA_CRYPTO_DRIVER_TEST)
1001 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001002 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001003 output,
1004 output_size,
1005 output_length ) );
1006#endif /* PSA_CRYPTO_DRIVER_TEST */
1007 default:
1008 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001009 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001010 }
1011#else /* PSA_CRYPTO_DRIVER_PRESENT */
1012 (void) operation;
1013 (void) output;
1014 (void) output_size;
1015 (void) output_length;
1016
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001017 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001018#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1019}
1020
1021psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001022 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001023{
1024#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1025 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001026
1027 /* The object has (apparently) been initialized but it is not in use. It's
1028 * ok to call abort on such an object, and there's nothing to do. */
1029 if( operation->ctx == NULL && operation->id == 0 )
1030 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001031
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001032 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001033 {
1034#if defined(PSA_CRYPTO_DRIVER_TEST)
1035 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001036 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001037 mbedtls_platform_zeroize(
1038 operation->ctx,
1039 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001040 mbedtls_free( operation->ctx );
1041 operation->ctx = NULL;
1042 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001043
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001044 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001045#endif /* PSA_CRYPTO_DRIVER_TEST */
1046#if defined(PSA_CRYPTO_DRIVER_TEST)
1047 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001048 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001049 mbedtls_platform_zeroize(
1050 operation->ctx,
1051 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001052 mbedtls_free( operation->ctx );
1053 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001054 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001055
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001056 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001057#endif /* PSA_CRYPTO_DRIVER_TEST */
1058 default:
1059 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001060 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001061 }
1062#else /* PSA_CRYPTO_DRIVER_PRESENT */
1063 (void)operation;
1064
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001065 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001066#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1067}
1068
Steven Cooremancd84cb42020-07-16 20:28:36 +02001069/* End of automatically generated file. */