blob: 56bb0035ffc068e19d5712e5f883f87ebfd05170 [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 Cooremancd84cb42020-07-16 20:28:36 +020036#include "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 Cooreman2a1664c2020-07-20 15:33:08 +020041#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
42
43/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020044#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020045/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
46 * SE driver is present, to avoid unused argument errors at compile time. */
47#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020048#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020049#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020050#include "psa_crypto_se.h"
51#endif
52
Steven Cooremancd84cb42020-07-16 20:28:36 +020053/* Start delegation functions */
54psa_status_t psa_driver_wrapper_sign_hash( psa_key_slot_t *slot,
55 psa_algorithm_t alg,
56 const uint8_t *hash,
57 size_t hash_length,
58 uint8_t *signature,
59 size_t signature_size,
60 size_t *signature_length )
61{
Steven Cooremanf1720ea2020-07-24 18:41:58 +020062#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman7a250572020-07-17 16:43:05 +020063 /* Try dynamically-registered SE interface first */
64#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
65 const psa_drv_se_t *drv;
66 psa_drv_se_context_t *drv_context;
67
68 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
69 {
70 if( drv->asymmetric == NULL ||
71 drv->asymmetric->p_sign == NULL )
72 {
73 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +020074 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +020075 }
76 return( drv->asymmetric->p_sign( drv_context,
77 slot->data.se.slot_number,
78 alg,
79 hash, hash_length,
80 signature, signature_size,
81 signature_length ) );
82 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +020083#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +020084
85 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020086#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremancd84cb42020-07-16 20:28:36 +020087 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
88 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
89 psa_key_attributes_t attributes = {
90 .core = slot->attr
91 };
92
93 switch( location )
94 {
95 case PSA_KEY_LOCATION_LOCAL_STORAGE:
96 /* Key is stored in the slot in export representation, so
97 * cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020098#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremancd84cb42020-07-16 20:28:36 +020099 status = test_transparent_signature_sign_hash( &attributes,
100 slot->data.key.data,
101 slot->data.key.bytes,
102 alg,
103 hash,
104 hash_length,
105 signature,
106 signature_size,
107 signature_length );
108 /* Declared with fallback == true */
109 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200110 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200111#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200112 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200113 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200114 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200115#if defined(PSA_CRYPTO_DRIVER_TEST)
116 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremancd84cb42020-07-16 20:28:36 +0200117 return( test_opaque_signature_sign_hash( &attributes,
118 slot->data.key.data,
119 slot->data.key.bytes,
120 alg,
121 hash,
122 hash_length,
123 signature,
124 signature_size,
125 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200126#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200127 default:
128 /* Key is declared with a lifetime not known to us */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200129 return( status );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200130 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200131#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200132 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200133#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
134#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200135 (void)slot;
136 (void)alg;
137 (void)hash;
138 (void)hash_length;
139 (void)signature;
140 (void)signature_size;
141 (void)signature_length;
142
Steven Cooreman56250fd2020-09-04 13:07:15 +0200143 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200144#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200145}
146
Steven Cooreman55ae2172020-07-17 19:46:15 +0200147psa_status_t psa_driver_wrapper_verify_hash( psa_key_slot_t *slot,
148 psa_algorithm_t alg,
149 const uint8_t *hash,
150 size_t hash_length,
151 const uint8_t *signature,
152 size_t signature_length )
153{
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200154#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200155 /* Try dynamically-registered SE interface first */
156#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
157 const psa_drv_se_t *drv;
158 psa_drv_se_context_t *drv_context;
159
160 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
161 {
162 if( drv->asymmetric == NULL ||
163 drv->asymmetric->p_verify == NULL )
164 {
165 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200166 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200167 }
168 return( drv->asymmetric->p_verify( drv_context,
169 slot->data.se.slot_number,
170 alg,
171 hash, hash_length,
172 signature, signature_length ) );
173 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200174#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200175
176 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200177#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200178 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
179 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
180 psa_key_attributes_t attributes = {
181 .core = slot->attr
182 };
183
184 switch( location )
185 {
186 case PSA_KEY_LOCATION_LOCAL_STORAGE:
187 /* Key is stored in the slot in export representation, so
188 * cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200189#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200190 status = test_transparent_signature_verify_hash( &attributes,
191 slot->data.key.data,
192 slot->data.key.bytes,
193 alg,
194 hash,
195 hash_length,
196 signature,
197 signature_length );
198 /* Declared with fallback == true */
199 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200200 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200201#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200202 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200203 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200204 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200205#if defined(PSA_CRYPTO_DRIVER_TEST)
206 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooreman55ae2172020-07-17 19:46:15 +0200207 return( test_opaque_signature_verify_hash( &attributes,
208 slot->data.key.data,
209 slot->data.key.bytes,
210 alg,
211 hash,
212 hash_length,
213 signature,
214 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200215#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200216 default:
217 /* Key is declared with a lifetime not known to us */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200218 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200219 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200220#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200221 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200222#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
223#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200224 (void)slot;
225 (void)alg;
226 (void)hash;
227 (void)hash_length;
228 (void)signature;
229 (void)signature_length;
230
Steven Cooreman56250fd2020-09-04 13:07:15 +0200231 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200232#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200233}
234
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200235#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman56250fd2020-09-04 13:07:15 +0200236/** Calculate the size to allocate for buffering a key with given attributes.
237 *
238 * This function provides a way to get the expected size for storing a key with
239 * the given attributes. This will be the size of the export representation for
240 * cleartext keys, and a driver-defined size for keys stored by opaque drivers.
241 *
242 * \param[in] attributes The key attribute structure of the key to store.
243 * \param[out] expected_size On success, a byte size large enough to contain
244 * the declared key.
245 *
246 * \retval #PSA_SUCCESS
247 * \retval #PSA_ERROR_NOT_SUPPORTED
248 */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200249static psa_status_t get_expected_key_size( const psa_key_attributes_t *attributes,
250 size_t *expected_size )
251{
Steven Cooreman56250fd2020-09-04 13:07:15 +0200252 size_t buffer_size = 0;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200253 if( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == PSA_KEY_LOCATION_LOCAL_STORAGE )
254 {
Steven Cooreman56250fd2020-09-04 13:07:15 +0200255 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( attributes->core.type,
256 attributes->core.bits );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200257
Steven Cooreman56250fd2020-09-04 13:07:15 +0200258 if( buffer_size == 0 )
259 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200260
Steven Cooreman56250fd2020-09-04 13:07:15 +0200261 *expected_size = buffer_size;
262 return( PSA_SUCCESS );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200263 }
264 else
265 {
Steven Cooreman56250fd2020-09-04 13:07:15 +0200266 /* TBD: opaque driver support: need to calculate size through a
267 * driver-defined size function, since the size of an opaque (wrapped)
268 * key will be different for each implementation. */
269 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200270 }
271}
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200272#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200273
Steven Cooreman55ae2172020-07-17 19:46:15 +0200274psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes,
275 psa_key_slot_t *slot )
276{
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200277#if defined(PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200278 /* Try dynamically-registered SE interface first */
279#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
280 const psa_drv_se_t *drv;
281 psa_drv_se_context_t *drv_context;
282
283 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
284 {
285 size_t pubkey_length = 0; /* We don't support this feature yet */
286 if( drv->key_management == NULL ||
287 drv->key_management->p_generate == NULL )
288 {
289 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200290 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200291 }
292 return( drv->key_management->p_generate(
293 drv_context,
294 slot->data.se.slot_number, attributes,
295 NULL, 0, &pubkey_length ) );
296 }
297#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
298
299 /* Then try accelerator API */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200300#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200301 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
302 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
303 size_t export_size = 0;
304
305 status = get_expected_key_size( attributes, &export_size );
306 if( status != PSA_SUCCESS )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200307 return( status );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200308
309 slot->data.key.data = mbedtls_calloc(1, export_size);
310 if( slot->data.key.data == NULL )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200311 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200312 slot->data.key.bytes = export_size;
313
314 switch( location )
315 {
316 case PSA_KEY_LOCATION_LOCAL_STORAGE:
317 /* Key is stored in the slot in export representation, so
318 * cycle through all known transparent accelerators */
319
320 /* Transparent drivers are limited to generating asymmetric keys */
321 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
322 {
323 status = PSA_ERROR_NOT_SUPPORTED;
324 break;
325 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200326#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200327 status = test_transparent_generate_key( attributes,
328 slot->data.key.data,
329 slot->data.key.bytes,
330 &slot->data.key.bytes );
331 /* Declared with fallback == true */
332 if( status != PSA_ERROR_NOT_SUPPORTED )
333 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200334#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200335 /* Fell through, meaning no accelerator supports this operation */
336 status = PSA_ERROR_NOT_SUPPORTED;
337 break;
338 /* Add cases for opaque driver here */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200339#if defined(PSA_CRYPTO_DRIVER_TEST)
340 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200341 status = test_opaque_generate_key( attributes,
342 slot->data.key.data,
343 slot->data.key.bytes,
344 &slot->data.key.bytes );
345 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200346#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200347 default:
348 /* Key is declared with a lifetime not known to us */
349 status = PSA_ERROR_INVALID_ARGUMENT;
350 break;
351 }
352
353 if( status != PSA_SUCCESS )
354 {
355 /* free allocated buffer */
356 mbedtls_free( slot->data.key.data );
357 slot->data.key.data = NULL;
358 slot->data.key.bytes = 0;
359 }
360
361 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200362#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200363 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200364#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
365#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200366 (void) attributes;
367 (void) slot;
368
Steven Cooreman56250fd2020-09-04 13:07:15 +0200369 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200370#endif /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200371}
372
Steven Cooremancd84cb42020-07-16 20:28:36 +0200373/* End of automatically generated file. */