blob: 759708075cebbf110f904127c6b5fcf8d5264b48 [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{
Ronald Cron9f17aa42020-12-08 17:07:25 +010066 (void)key_buffer_size;
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
Ronald Cron9f17aa42020-12-08 17:07:25 +010074 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +020075 {
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 }
Ronald Cron9f17aa42020-12-08 17:07:25 +010082 return( drv->asymmetric->p_sign(
83 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
84 alg, hash, hash_length,
85 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +020086 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +020087#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +020088
89 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020090#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremancd84cb42020-07-16 20:28:36 +020091 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron9f17aa42020-12-08 17:07:25 +010092 psa_key_location_t location =
93 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +020094
95 switch( location )
96 {
97 case PSA_KEY_LOCATION_LOCAL_STORAGE:
98 /* Key is stored in the slot in export representation, so
99 * cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200100#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100101 status = test_transparent_signature_sign_hash( attributes,
102 key_buffer,
103 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200104 alg,
105 hash,
106 hash_length,
107 signature,
108 signature_size,
109 signature_length );
110 /* Declared with fallback == true */
111 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200112 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200113#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200114 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200115 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200116 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200117#if defined(PSA_CRYPTO_DRIVER_TEST)
118 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100119 return( test_opaque_signature_sign_hash( attributes,
120 key_buffer,
121 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200122 alg,
123 hash,
124 hash_length,
125 signature,
126 signature_size,
127 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200128#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200129 default:
130 /* Key is declared with a lifetime not known to us */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200131 return( status );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200132 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200133#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200134 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200135#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
136#else /* PSA_CRYPTO_DRIVER_PRESENT */
Ronald Cron9f17aa42020-12-08 17:07:25 +0100137 (void)attributes;
138 (void)key_buffer;
Steven Cooremancd84cb42020-07-16 20:28:36 +0200139 (void)alg;
140 (void)hash;
141 (void)hash_length;
142 (void)signature;
143 (void)signature_size;
144 (void)signature_length;
145
Steven Cooreman56250fd2020-09-04 13:07:15 +0200146 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200147#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200148}
149
Ronald Cron9f17aa42020-12-08 17:07:25 +0100150psa_status_t psa_driver_wrapper_verify_hash(
151 const psa_key_attributes_t *attributes,
152 const uint8_t *key_buffer, size_t key_buffer_size,
153 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
154 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200155{
Ronald Cron9f17aa42020-12-08 17:07:25 +0100156 (void)key_buffer_size;
157
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200158#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200159 /* Try dynamically-registered SE interface first */
160#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
161 const psa_drv_se_t *drv;
162 psa_drv_se_context_t *drv_context;
163
Ronald Cron9f17aa42020-12-08 17:07:25 +0100164 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200165 {
166 if( drv->asymmetric == NULL ||
167 drv->asymmetric->p_verify == NULL )
168 {
169 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200170 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200171 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100172 return( drv->asymmetric->p_verify(
173 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
174 alg, hash, hash_length,
175 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200176 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200177#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200178
179 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200180#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200181 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100182 psa_key_location_t location =
183 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200184
185 switch( location )
186 {
187 case PSA_KEY_LOCATION_LOCAL_STORAGE:
188 /* Key is stored in the slot in export representation, so
189 * cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200190#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100191 status = test_transparent_signature_verify_hash( attributes,
192 key_buffer,
193 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200194 alg,
195 hash,
196 hash_length,
197 signature,
198 signature_length );
199 /* Declared with fallback == true */
200 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200201 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200202#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200203 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200204 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200205 /* Add cases for opaque driver here */
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 */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200217 default:
218 /* Key is declared with a lifetime not known to us */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200219 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200220 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200221#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200222 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200223#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
224#else /* PSA_CRYPTO_DRIVER_PRESENT */
Ronald Cron9f17aa42020-12-08 17:07:25 +0100225 (void)attributes;
226 (void)key_buffer;
Steven Cooreman55ae2172020-07-17 19:46:15 +0200227 (void)alg;
228 (void)hash;
229 (void)hash_length;
230 (void)signature;
231 (void)signature_length;
232
Steven Cooreman56250fd2020-09-04 13:07:15 +0200233 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200234#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200235}
236
Ronald Cron31216282020-12-05 18:47:56 +0100237/** Get the key buffer size for the key material of a generated key in the
238 * case of an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200239 *
Ronald Cron31216282020-12-05 18:47:56 +0100240 * \param[in] attributes The key attributes.
241 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200242 *
243 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100244 * The minimum size for a buffer to contain the key material has been
245 * returned successfully.
246 * \retval #PSA_ERROR_INVALID_ARGUMENT
247 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200248 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100249 * The type and/or the size in bits of the key or the combination of
250 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200251 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100252psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100253 const psa_key_attributes_t *attributes,
254 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200255{
John Durkop2c618352020-09-22 06:54:01 -0700256 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
257 psa_key_type_t key_type = attributes->core.type;
258 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200259
Ronald Cron31216282020-12-05 18:47:56 +0100260 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700261 switch( location )
262 {
John Durkop2c618352020-09-22 06:54:01 -0700263#if defined(PSA_CRYPTO_DRIVER_TEST)
264 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
John Durkopac93e3b2020-10-16 06:48:55 -0700265#ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
Ronald Cron31216282020-12-05 18:47:56 +0100266 *key_buffer_size = test_size_function( key_type, key_bits );
John Durkop2c618352020-09-22 06:54:01 -0700267 return( PSA_SUCCESS );
268#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
269 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
270 {
Ronald Cron31216282020-12-05 18:47:56 +0100271 int public_key_overhead =
272 ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ?
273 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
274 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop135ce692020-10-19 07:12:28 -0700275 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
276 + public_key_overhead;
277 }
Ronald Cron31216282020-12-05 18:47:56 +0100278 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
John Durkop135ce692020-10-19 07:12:28 -0700279 {
Ronald Cron31216282020-12-05 18:47:56 +0100280 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700281 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
282 }
John Durkop135ce692020-10-19 07:12:28 -0700283 else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
Ronald Cron31216282020-12-05 18:47:56 +0100284 !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
John Durkop2c618352020-09-22 06:54:01 -0700285 {
Ronald Cron31216282020-12-05 18:47:56 +0100286 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700287 + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
288 * ( ( key_bits + 7 ) / 8 );
289 }
290 else
291 {
292 return( PSA_ERROR_NOT_SUPPORTED );
293 }
294 return( PSA_SUCCESS );
295#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
296#endif /* PSA_CRYPTO_DRIVER_TEST */
297
298 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100299 (void)key_type;
300 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200301 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200302 }
303}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200304
Ronald Cron977c2472020-10-13 08:32:21 +0200305psa_status_t psa_driver_wrapper_generate_key(
306 const psa_key_attributes_t *attributes,
307 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200308{
Ronald Cron977c2472020-10-13 08:32:21 +0200309 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
310 psa_key_location_t location =
311 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
312
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200313 /* Try dynamically-registered SE interface first */
314#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
315 const psa_drv_se_t *drv;
316 psa_drv_se_context_t *drv_context;
317
Ronald Cron977c2472020-10-13 08:32:21 +0200318 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200319 {
320 size_t pubkey_length = 0; /* We don't support this feature yet */
321 if( drv->key_management == NULL ||
322 drv->key_management->p_generate == NULL )
323 {
324 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200325 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200326 }
327 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200328 drv_context,
329 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100330 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200331 }
332#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
333
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200334 switch( location )
335 {
336 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200337#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200338 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200339 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200340 {
Ronald Cron977c2472020-10-13 08:32:21 +0200341 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200342#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron977c2472020-10-13 08:32:21 +0200343 status = test_transparent_generate_key(
344 attributes, key_buffer, key_buffer_size,
345 key_buffer_length );
346 /* Declared with fallback == true */
347 if( status != PSA_ERROR_NOT_SUPPORTED )
348 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200349#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200350 }
351#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
352
353 /* Software fallback */
354 status = psa_generate_key_internal(
355 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200356 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200357
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200358 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200359#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200360#if defined(PSA_CRYPTO_DRIVER_TEST)
361 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron977c2472020-10-13 08:32:21 +0200362 status = test_opaque_generate_key(
363 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200364 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200365#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200366#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
367
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200368 default:
369 /* Key is declared with a lifetime not known to us */
370 status = PSA_ERROR_INVALID_ARGUMENT;
371 break;
372 }
373
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200374 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200375}
376
Ronald Cron83282872020-11-22 14:02:39 +0100377psa_status_t psa_driver_wrapper_import_key(
378 const psa_key_attributes_t *attributes,
379 const uint8_t *data,
380 size_t data_length,
381 uint8_t *key_buffer,
382 size_t key_buffer_size,
383 size_t *key_buffer_length,
384 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200385{
Steven Cooreman04524762020-10-13 17:43:44 +0200386 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100387 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
388 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200389
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100390 /* Try dynamically-registered SE interface first */
391#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
392 const psa_drv_se_t *drv;
393 psa_drv_se_context_t *drv_context;
394
395 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
396 {
397 if( drv->key_management == NULL ||
398 drv->key_management->p_import == NULL )
399 return( PSA_ERROR_NOT_SUPPORTED );
400
401 /* The driver should set the number of key bits, however in
402 * case it doesn't, we initialize bits to an invalid value. */
403 *bits = PSA_MAX_KEY_BITS + 1;
404 status = drv->key_management->p_import(
405 drv_context,
406 *( (psa_key_slot_number_t *)key_buffer ),
407 attributes, data, data_length, bits );
408
409 if( status != PSA_SUCCESS )
410 return( status );
411
412 if( (*bits) > PSA_MAX_KEY_BITS )
413 return( PSA_ERROR_NOT_SUPPORTED );
414
415 return( PSA_SUCCESS );
416 }
417#endif /* PSA_CRYPTO_SE_C */
418
Ronald Cronbf33c932020-11-28 18:06:53 +0100419 switch( location )
420 {
421 case PSA_KEY_LOCATION_LOCAL_STORAGE:
422 /* Key is stored in the slot in export representation, so
423 * cycle through all known transparent accelerators */
424#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
425#if defined(PSA_CRYPTO_DRIVER_TEST)
426 status = test_transparent_import_key( attributes,
427 data, data_length,
428 key_buffer, key_buffer_size,
429 key_buffer_length, bits );
430 /* Declared with fallback == true */
431 if( status != PSA_ERROR_NOT_SUPPORTED )
432 return( status );
433#endif /* PSA_CRYPTO_DRIVER_TEST */
434#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
435 /* Fell through, meaning no accelerator supports this operation */
436 return( psa_import_key_into_slot( attributes,
437 data, data_length,
438 key_buffer, key_buffer_size,
439 key_buffer_length, bits ) );
440
441 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100442 /* Importing a key with external storage in not yet supported.
443 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100444 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100445 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100446 }
447
Steven Cooreman04524762020-10-13 17:43:44 +0200448}
449
Ronald Cron67227982020-11-26 15:16:05 +0100450psa_status_t psa_driver_wrapper_export_key(
451 const psa_key_attributes_t *attributes,
452 const uint8_t *key_buffer, size_t key_buffer_size,
453 uint8_t *data, size_t data_size, size_t *data_length )
454
455{
456 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
457 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
458 psa_get_key_lifetime( attributes ) );
459
Ronald Cron152e3f82020-11-26 16:06:41 +0100460 /* Try dynamically-registered SE interface first */
461#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
462 const psa_drv_se_t *drv;
463 psa_drv_se_context_t *drv_context;
464
465 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
466 {
467 if( ( drv->key_management == NULL ) ||
468 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100469 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100470 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100471 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100472
473 return( drv->key_management->p_export(
474 drv_context,
475 *( (psa_key_slot_number_t *)key_buffer ),
476 data, data_size, data_length ) );
477 }
478#endif /* PSA_CRYPTO_SE_C */
479
Ronald Cron67227982020-11-26 15:16:05 +0100480 switch( location )
481 {
482 case PSA_KEY_LOCATION_LOCAL_STORAGE:
483 return( psa_export_key_internal( attributes,
484 key_buffer,
485 key_buffer_size,
486 data,
487 data_size,
488 data_length ) );
489
490 /* Add cases for opaque driver here */
491#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
492#if defined(PSA_CRYPTO_DRIVER_TEST)
493 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
494 return( test_opaque_export_key( attributes,
495 key_buffer,
496 key_buffer_size,
497 data,
498 data_size,
499 data_length ) );
500#endif /* PSA_CRYPTO_DRIVER_TEST */
501#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
502 default:
503 /* Key is declared with a lifetime not known to us */
504 return( status );
505 }
506}
507
Ronald Cron84cc9942020-11-25 14:30:05 +0100508psa_status_t psa_driver_wrapper_export_public_key(
509 const psa_key_attributes_t *attributes,
510 const uint8_t *key_buffer, size_t key_buffer_size,
511 uint8_t *data, size_t data_size, size_t *data_length )
512
Steven Cooremanb9b84422020-10-14 14:39:20 +0200513{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200514 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100515 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
516 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200517
Ronald Cron152e3f82020-11-26 16:06:41 +0100518 /* Try dynamically-registered SE interface first */
519#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
520 const psa_drv_se_t *drv;
521 psa_drv_se_context_t *drv_context;
522
523 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
524 {
525 if( ( drv->key_management == NULL ) ||
526 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100527 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100528 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100529 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100530
531 return( drv->key_management->p_export_public(
532 drv_context,
533 *( (psa_key_slot_number_t *)key_buffer ),
534 data, data_size, data_length ) );
535 }
536#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
537
Steven Cooremanb9b84422020-10-14 14:39:20 +0200538 switch( location )
539 {
540 case PSA_KEY_LOCATION_LOCAL_STORAGE:
541 /* Key is stored in the slot in export representation, so
542 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100543#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200544#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron84cc9942020-11-25 14:30:05 +0100545 status = test_transparent_export_public_key( attributes,
546 key_buffer,
547 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200548 data,
549 data_size,
550 data_length );
551 /* Declared with fallback == true */
552 if( status != PSA_ERROR_NOT_SUPPORTED )
553 return( status );
554#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100555#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200556 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100557 return( psa_export_public_key_internal( attributes,
558 key_buffer,
559 key_buffer_size,
560 data,
561 data_size,
562 data_length ) );
563
Steven Cooremanb9b84422020-10-14 14:39:20 +0200564 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100565#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200566#if defined(PSA_CRYPTO_DRIVER_TEST)
567 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron84cc9942020-11-25 14:30:05 +0100568 return( test_opaque_export_public_key( attributes,
569 key_buffer,
570 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200571 data,
572 data_size,
573 data_length ) );
574#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100575#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200576 default:
577 /* Key is declared with a lifetime not known to us */
578 return( status );
579 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200580}
581
Steven Cooreman37941cb2020-07-28 18:49:51 +0200582/*
583 * Cipher functions
584 */
585psa_status_t psa_driver_wrapper_cipher_encrypt(
586 psa_key_slot_t *slot,
587 psa_algorithm_t alg,
588 const uint8_t *input,
589 size_t input_length,
590 uint8_t *output,
591 size_t output_size,
592 size_t *output_length )
593{
594#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
595 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
596 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
597 psa_key_attributes_t attributes = {
598 .core = slot->attr
599 };
600
601 switch( location )
602 {
603 case PSA_KEY_LOCATION_LOCAL_STORAGE:
604 /* Key is stored in the slot in export representation, so
605 * cycle through all known transparent accelerators */
606#if defined(PSA_CRYPTO_DRIVER_TEST)
607 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100608 slot->key.data,
609 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200610 alg,
611 input,
612 input_length,
613 output,
614 output_size,
615 output_length );
616 /* Declared with fallback == true */
617 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200618 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200619#endif /* PSA_CRYPTO_DRIVER_TEST */
620 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200621 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200622 /* Add cases for opaque driver here */
623#if defined(PSA_CRYPTO_DRIVER_TEST)
624 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
625 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100626 slot->key.data,
627 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200628 alg,
629 input,
630 input_length,
631 output,
632 output_size,
633 output_length ) );
634#endif /* PSA_CRYPTO_DRIVER_TEST */
635 default:
636 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200637 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200638 }
639#else /* PSA_CRYPTO_DRIVER_PRESENT */
640 (void) slot;
641 (void) alg;
642 (void) input;
643 (void) input_length;
644 (void) output;
645 (void) output_size;
646 (void) output_length;
647
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200648 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200649#endif /* PSA_CRYPTO_DRIVER_PRESENT */
650}
651
652psa_status_t psa_driver_wrapper_cipher_decrypt(
653 psa_key_slot_t *slot,
654 psa_algorithm_t alg,
655 const uint8_t *input,
656 size_t input_length,
657 uint8_t *output,
658 size_t output_size,
659 size_t *output_length )
660{
661#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
662 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
663 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
664 psa_key_attributes_t attributes = {
665 .core = slot->attr
666 };
667
668 switch( location )
669 {
670 case PSA_KEY_LOCATION_LOCAL_STORAGE:
671 /* Key is stored in the slot in export representation, so
672 * cycle through all known transparent accelerators */
673#if defined(PSA_CRYPTO_DRIVER_TEST)
674 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100675 slot->key.data,
676 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200677 alg,
678 input,
679 input_length,
680 output,
681 output_size,
682 output_length );
683 /* Declared with fallback == true */
684 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200685 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200686#endif /* PSA_CRYPTO_DRIVER_TEST */
687 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200688 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200689 /* Add cases for opaque driver here */
690#if defined(PSA_CRYPTO_DRIVER_TEST)
691 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
692 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100693 slot->key.data,
694 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200695 alg,
696 input,
697 input_length,
698 output,
699 output_size,
700 output_length ) );
701#endif /* PSA_CRYPTO_DRIVER_TEST */
702 default:
703 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200704 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200705 }
706#else /* PSA_CRYPTO_DRIVER_PRESENT */
707 (void) slot;
708 (void) alg;
709 (void) input;
710 (void) input_length;
711 (void) output;
712 (void) output_size;
713 (void) output_length;
714
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200715 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200716#endif /* PSA_CRYPTO_DRIVER_PRESENT */
717}
718
719psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200720 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200721 psa_key_slot_t *slot,
722 psa_algorithm_t alg )
723{
724#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
725 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
726 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
727 psa_key_attributes_t attributes = {
728 .core = slot->attr
729 };
730
Steven Cooreman37941cb2020-07-28 18:49:51 +0200731 switch( location )
732 {
733 case PSA_KEY_LOCATION_LOCAL_STORAGE:
734 /* Key is stored in the slot in export representation, so
735 * cycle through all known transparent accelerators */
736#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200737 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
738 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200739 return PSA_ERROR_INSUFFICIENT_MEMORY;
740
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200741 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200742 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100743 slot->key.data,
744 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200745 alg );
746 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200747 if( status == PSA_SUCCESS )
748 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200749 else
750 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200751 mbedtls_platform_zeroize(
752 operation->ctx,
753 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200754 mbedtls_free( operation->ctx );
755 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200756 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200757
758 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200759#endif /* PSA_CRYPTO_DRIVER_TEST */
760 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200761 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200762 /* Add cases for opaque driver here */
763#if defined(PSA_CRYPTO_DRIVER_TEST)
764 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200765 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
766 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200767 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200768
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200769 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200770 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100771 slot->key.data,
772 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200773 alg );
774 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200775 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200776 else
777 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200778 mbedtls_platform_zeroize(
779 operation->ctx,
780 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200781 mbedtls_free( operation->ctx );
782 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200783 }
784
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200785 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200786#endif /* PSA_CRYPTO_DRIVER_TEST */
787 default:
788 /* Key is declared with a lifetime not known to us */
John Durkop714e3a12020-09-29 22:07:04 -0700789 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200790 }
791#else /* PSA_CRYPTO_DRIVER_PRESENT */
792 (void)slot;
793 (void)alg;
794 (void)operation;
795
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200796 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200797#endif /* PSA_CRYPTO_DRIVER_PRESENT */
798}
799
800psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200801 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200802 psa_key_slot_t *slot,
803 psa_algorithm_t alg )
804{
805#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
806 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
807 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
808 psa_key_attributes_t attributes = {
809 .core = slot->attr
810 };
811
Steven Cooreman37941cb2020-07-28 18:49:51 +0200812 switch( location )
813 {
814 case PSA_KEY_LOCATION_LOCAL_STORAGE:
815 /* Key is stored in the slot in export representation, so
816 * cycle through all known transparent accelerators */
817#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200818 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
819 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200820 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200821
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200822 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200823 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100824 slot->key.data,
825 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200826 alg );
827 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200828 if( status == PSA_SUCCESS )
829 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200830 else
831 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200832 mbedtls_platform_zeroize(
833 operation->ctx,
834 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200835 mbedtls_free( operation->ctx );
836 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200837 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200838
839 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200840#endif /* PSA_CRYPTO_DRIVER_TEST */
841 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200842 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200843 /* Add cases for opaque driver here */
844#if defined(PSA_CRYPTO_DRIVER_TEST)
845 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200846 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
847 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200848 return PSA_ERROR_INSUFFICIENT_MEMORY;
849
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200850 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200851 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100852 slot->key.data,
853 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200854 alg );
855 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200856 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200857 else
858 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200859 mbedtls_platform_zeroize(
860 operation->ctx,
861 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200862 mbedtls_free( operation->ctx );
863 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200864 }
865
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200866 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200867#endif /* PSA_CRYPTO_DRIVER_TEST */
868 default:
869 /* Key is declared with a lifetime not known to us */
John Durkop814dca72020-10-05 06:31:12 -0700870 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200871 }
872#else /* PSA_CRYPTO_DRIVER_PRESENT */
873 (void)slot;
874 (void)alg;
875 (void)operation;
876
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200877 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200878#endif /* PSA_CRYPTO_DRIVER_PRESENT */
879}
880
881psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200882 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200883 uint8_t *iv,
884 size_t iv_size,
885 size_t *iv_length )
886{
887#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200888 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200889 {
890#if defined(PSA_CRYPTO_DRIVER_TEST)
891 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200892 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200893 iv,
894 iv_size,
895 iv_length ) );
896#endif /* PSA_CRYPTO_DRIVER_TEST */
897#if defined(PSA_CRYPTO_DRIVER_TEST)
898 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200899 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200900 iv,
901 iv_size,
902 iv_length ) );
903#endif /* PSA_CRYPTO_DRIVER_TEST */
904 default:
905 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200906 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200907 }
908#else /* PSA_CRYPTO_DRIVER_PRESENT */
909 (void) operation;
910 (void) iv;
911 (void) iv_size;
912 (void) iv_length;
913
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200914 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200915#endif /* PSA_CRYPTO_DRIVER_PRESENT */
916}
917
918psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200919 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200920 const uint8_t *iv,
921 size_t iv_length )
922{
923#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200924 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200925 {
926#if defined(PSA_CRYPTO_DRIVER_TEST)
927 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200928 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200929 iv,
930 iv_length ) );
931#endif /* PSA_CRYPTO_DRIVER_TEST */
932#if defined(PSA_CRYPTO_DRIVER_TEST)
933 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200934 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200935 iv,
936 iv_length ) );
937#endif /* PSA_CRYPTO_DRIVER_TEST */
938 default:
939 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200940 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200941 }
942#else /* PSA_CRYPTO_DRIVER_PRESENT */
943 (void) operation;
944 (void) iv;
945 (void) iv_length;
946
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200947 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200948#endif /* PSA_CRYPTO_DRIVER_PRESENT */
949}
950
951psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200952 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200953 const uint8_t *input,
954 size_t input_length,
955 uint8_t *output,
956 size_t output_size,
957 size_t *output_length )
958{
959#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200960 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200961 {
962#if defined(PSA_CRYPTO_DRIVER_TEST)
963 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200964 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200965 input,
966 input_length,
967 output,
968 output_size,
969 output_length ) );
970#endif /* PSA_CRYPTO_DRIVER_TEST */
971#if defined(PSA_CRYPTO_DRIVER_TEST)
972 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200973 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200974 input,
975 input_length,
976 output,
977 output_size,
978 output_length ) );
979#endif /* PSA_CRYPTO_DRIVER_TEST */
980 default:
981 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200982 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200983 }
984#else /* PSA_CRYPTO_DRIVER_PRESENT */
985 (void) operation;
986 (void) input;
987 (void) input_length;
988 (void) output;
989 (void) output_length;
990 (void) output_size;
991
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200992 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200993#endif /* PSA_CRYPTO_DRIVER_PRESENT */
994}
995
996psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200997 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200998 uint8_t *output,
999 size_t output_size,
1000 size_t *output_length )
1001{
1002#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001003 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001004 {
1005#if defined(PSA_CRYPTO_DRIVER_TEST)
1006 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001007 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001008 output,
1009 output_size,
1010 output_length ) );
1011#endif /* PSA_CRYPTO_DRIVER_TEST */
1012#if defined(PSA_CRYPTO_DRIVER_TEST)
1013 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001014 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001015 output,
1016 output_size,
1017 output_length ) );
1018#endif /* PSA_CRYPTO_DRIVER_TEST */
1019 default:
1020 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001021 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001022 }
1023#else /* PSA_CRYPTO_DRIVER_PRESENT */
1024 (void) operation;
1025 (void) output;
1026 (void) output_size;
1027 (void) output_length;
1028
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001029 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001030#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1031}
1032
1033psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001034 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001035{
1036#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1037 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001038
1039 /* The object has (apparently) been initialized but it is not in use. It's
1040 * ok to call abort on such an object, and there's nothing to do. */
1041 if( operation->ctx == NULL && operation->id == 0 )
1042 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001043
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001044 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001045 {
1046#if defined(PSA_CRYPTO_DRIVER_TEST)
1047 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001048 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001049 mbedtls_platform_zeroize(
1050 operation->ctx,
1051 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001052 mbedtls_free( operation->ctx );
1053 operation->ctx = NULL;
1054 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#if defined(PSA_CRYPTO_DRIVER_TEST)
1059 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001060 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001061 mbedtls_platform_zeroize(
1062 operation->ctx,
1063 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001064 mbedtls_free( operation->ctx );
1065 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001066 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001067
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001068 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001069#endif /* PSA_CRYPTO_DRIVER_TEST */
1070 default:
1071 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001072 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001073 }
1074#else /* PSA_CRYPTO_DRIVER_PRESENT */
1075 (void)operation;
1076
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001077 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001078#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1079}
1080
Steven Cooremancd84cb42020-07-16 20:28:36 +02001081/* End of automatically generated file. */