blob: 5fa7966d929dfeb72eb893cb67c5686a4b1670d7 [file] [log] [blame]
Steven Cooremancd84cb42020-07-16 20:28:36 +02001/*
2 * Functions to delegate cryptographic operations to an available
3 * and appropriate accelerator.
4 * Warning: auto-generated file.
5 */
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)
30#undef PSA_CRYPTO_DRIVER_PRESENT
31#define PSA_CRYPTO_DRIVER_PRESENT
32#undef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
33#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020035#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020036
Steven Cooreman2a1664c2020-07-20 15:33:08 +020037/* Include driver definition file for each registered driver here */
38#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
39
40/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooremanf1720ea2020-07-24 18:41:58 +020042#undef PSA_CRYPTO_DRIVER_PRESENT
43#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman7a250572020-07-17 16:43:05 +020044#include "psa_crypto_se.h"
45#endif
46
Steven Cooremancd84cb42020-07-16 20:28:36 +020047/* Start delegation functions */
48psa_status_t psa_driver_wrapper_sign_hash( psa_key_slot_t *slot,
49 psa_algorithm_t alg,
50 const uint8_t *hash,
51 size_t hash_length,
52 uint8_t *signature,
53 size_t signature_size,
54 size_t *signature_length )
55{
Steven Cooremanf1720ea2020-07-24 18:41:58 +020056#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman7a250572020-07-17 16:43:05 +020057 /* Try dynamically-registered SE interface first */
58#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
59 const psa_drv_se_t *drv;
60 psa_drv_se_context_t *drv_context;
61
62 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
63 {
64 if( drv->asymmetric == NULL ||
65 drv->asymmetric->p_sign == NULL )
66 {
67 /* Key is defined in SE, but we have no way to exercise it */
68 return PSA_ERROR_INVALID_ARGUMENT;
69 }
70 return( drv->asymmetric->p_sign( drv_context,
71 slot->data.se.slot_number,
72 alg,
73 hash, hash_length,
74 signature, signature_size,
75 signature_length ) );
76 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +020077#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +020078
79 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020080#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremancd84cb42020-07-16 20:28:36 +020081 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
82 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
83 psa_key_attributes_t attributes = {
84 .core = slot->attr
85 };
86
87 switch( location )
88 {
89 case PSA_KEY_LOCATION_LOCAL_STORAGE:
90 /* Key is stored in the slot in export representation, so
91 * cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020092#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremancd84cb42020-07-16 20:28:36 +020093 status = test_transparent_signature_sign_hash( &attributes,
94 slot->data.key.data,
95 slot->data.key.bytes,
96 alg,
97 hash,
98 hash_length,
99 signature,
100 signature_size,
101 signature_length );
102 /* Declared with fallback == true */
103 if( status != PSA_ERROR_NOT_SUPPORTED )
104 return status;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200105#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200106 /* Fell through, meaning no accelerator supports this operation */
107 return PSA_ERROR_NOT_SUPPORTED;
108 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200109#if defined(PSA_CRYPTO_DRIVER_TEST)
110 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremancd84cb42020-07-16 20:28:36 +0200111 return( test_opaque_signature_sign_hash( &attributes,
112 slot->data.key.data,
113 slot->data.key.bytes,
114 alg,
115 hash,
116 hash_length,
117 signature,
118 signature_size,
119 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200120#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200121 default:
122 /* Key is declared with a lifetime not known to us */
123 return status;
124 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200125#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman7a250572020-07-17 16:43:05 +0200126 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200127#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
128#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200129 (void)slot;
130 (void)alg;
131 (void)hash;
132 (void)hash_length;
133 (void)signature;
134 (void)signature_size;
135 (void)signature_length;
136
137 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200138#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200139}
140
Steven Cooreman55ae2172020-07-17 19:46:15 +0200141psa_status_t psa_driver_wrapper_verify_hash( psa_key_slot_t *slot,
142 psa_algorithm_t alg,
143 const uint8_t *hash,
144 size_t hash_length,
145 const uint8_t *signature,
146 size_t signature_length )
147{
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200148#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200149 /* Try dynamically-registered SE interface first */
150#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
151 const psa_drv_se_t *drv;
152 psa_drv_se_context_t *drv_context;
153
154 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
155 {
156 if( drv->asymmetric == NULL ||
157 drv->asymmetric->p_verify == NULL )
158 {
159 /* Key is defined in SE, but we have no way to exercise it */
160 return PSA_ERROR_INVALID_ARGUMENT;
161 }
162 return( drv->asymmetric->p_verify( drv_context,
163 slot->data.se.slot_number,
164 alg,
165 hash, hash_length,
166 signature, signature_length ) );
167 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200168#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200169
170 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200171#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200172 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
173 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
174 psa_key_attributes_t attributes = {
175 .core = slot->attr
176 };
177
178 switch( location )
179 {
180 case PSA_KEY_LOCATION_LOCAL_STORAGE:
181 /* Key is stored in the slot in export representation, so
182 * cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200183#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200184 status = test_transparent_signature_verify_hash( &attributes,
185 slot->data.key.data,
186 slot->data.key.bytes,
187 alg,
188 hash,
189 hash_length,
190 signature,
191 signature_length );
192 /* Declared with fallback == true */
193 if( status != PSA_ERROR_NOT_SUPPORTED )
194 return status;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200195#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200196 /* Fell through, meaning no accelerator supports this operation */
197 return PSA_ERROR_NOT_SUPPORTED;
198 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200199#if defined(PSA_CRYPTO_DRIVER_TEST)
200 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooreman55ae2172020-07-17 19:46:15 +0200201 return( test_opaque_signature_verify_hash( &attributes,
202 slot->data.key.data,
203 slot->data.key.bytes,
204 alg,
205 hash,
206 hash_length,
207 signature,
208 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200209#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200210 default:
211 /* Key is declared with a lifetime not known to us */
212 return status;
213 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200214#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200215 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200216#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
217#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200218 (void)slot;
219 (void)alg;
220 (void)hash;
221 (void)hash_length;
222 (void)signature;
223 (void)signature_length;
224
225 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200226#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200227}
228
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200229#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200230static psa_status_t get_expected_key_size( const psa_key_attributes_t *attributes,
231 size_t *expected_size )
232{
233 if( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == PSA_KEY_LOCATION_LOCAL_STORAGE )
234 {
235 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( attributes->core.type ) )
236 {
237 *expected_size = PSA_BITS_TO_BYTES( attributes->core.bits );
238 return PSA_SUCCESS;
239 }
240
241 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
242 {
243 if( PSA_KEY_TYPE_IS_KEY_PAIR( attributes->core.type ) )
244 {
245 *expected_size = PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE( attributes->core.bits );
246 return PSA_SUCCESS;
247 }
248 else
249 {
250 *expected_size = PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE( attributes->core.bits );
251 return PSA_SUCCESS;
252 }
253 }
254
255 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
256 {
257 if( PSA_KEY_TYPE_IS_KEY_PAIR( attributes->core.type ) )
258 {
259 *expected_size = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( attributes->core.bits );
260 return PSA_SUCCESS;
261 }
262 else
263 {
264 *expected_size = PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE( attributes->core.bits );
265 return PSA_SUCCESS;
266 }
267 }
268
269 return PSA_ERROR_NOT_SUPPORTED;
270 }
271 else
272 {
273 /* TBD: opaque driver support, need to calculate size through driver-defined size function */
274 return PSA_ERROR_NOT_SUPPORTED;
275 }
276}
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200277#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200278
Steven Cooreman55ae2172020-07-17 19:46:15 +0200279psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes,
280 psa_key_slot_t *slot )
281{
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200282#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200283 /* Try dynamically-registered SE interface first */
284#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
285 const psa_drv_se_t *drv;
286 psa_drv_se_context_t *drv_context;
287
288 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
289 {
290 size_t pubkey_length = 0; /* We don't support this feature yet */
291 if( drv->key_management == NULL ||
292 drv->key_management->p_generate == NULL )
293 {
294 /* Key is defined as being in SE, but we have no way to generate it */
295 return PSA_ERROR_NOT_SUPPORTED;
296 }
297 return( drv->key_management->p_generate(
298 drv_context,
299 slot->data.se.slot_number, attributes,
300 NULL, 0, &pubkey_length ) );
301 }
302#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
303
304 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200305#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200306 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
307 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
308 size_t export_size = 0;
309
310 status = get_expected_key_size( attributes, &export_size );
311 if( status != PSA_SUCCESS )
312 return status;
313
314 slot->data.key.data = mbedtls_calloc(1, export_size);
315 if( slot->data.key.data == NULL )
316 return PSA_ERROR_INSUFFICIENT_MEMORY;
317 slot->data.key.bytes = export_size;
318
319 switch( location )
320 {
321 case PSA_KEY_LOCATION_LOCAL_STORAGE:
322 /* Key is stored in the slot in export representation, so
323 * cycle through all known transparent accelerators */
324
325 /* Transparent drivers are limited to generating asymmetric keys */
326 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
327 {
328 status = PSA_ERROR_NOT_SUPPORTED;
329 break;
330 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200331#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200332 status = test_transparent_generate_key( attributes,
333 slot->data.key.data,
334 slot->data.key.bytes,
335 &slot->data.key.bytes );
336 /* Declared with fallback == true */
337 if( status != PSA_ERROR_NOT_SUPPORTED )
338 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200339#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200340 /* Fell through, meaning no accelerator supports this operation */
341 status = PSA_ERROR_NOT_SUPPORTED;
342 break;
343 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200344#if defined(PSA_CRYPTO_DRIVER_TEST)
345 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200346 status = test_opaque_generate_key( attributes,
347 slot->data.key.data,
348 slot->data.key.bytes,
349 &slot->data.key.bytes );
350 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200351#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200352 default:
353 /* Key is declared with a lifetime not known to us */
354 status = PSA_ERROR_INVALID_ARGUMENT;
355 break;
356 }
357
358 if( status != PSA_SUCCESS )
359 {
360 /* free allocated buffer */
361 mbedtls_free( slot->data.key.data );
362 slot->data.key.data = NULL;
363 slot->data.key.bytes = 0;
364 }
365
366 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200367#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200368 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200369#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
370#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200371 (void) attributes;
372 (void) slot;
373
374 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200375#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200376}
377
Steven Cooremancd84cb42020-07-16 20:28:36 +0200378/* End of automatically generated file. */