Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions to delegate cryptographic operations to an available |
| 3 | * and appropriate accelerator. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 4 | * Warning: This file will be auto-generated in the future. |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 5 | */ |
Steven Cooreman | 2c7b2f8 | 2020-09-02 13:43:46 +0200 | [diff] [blame] | 6 | /* Copyright The Mbed TLS Contributors |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 7 | * 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 Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "psa_crypto_core.h" |
| 23 | #include "psa_crypto_driver_wrappers.h" |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 24 | #include "mbedtls/platform.h" |
| 25 | |
| 26 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 27 | |
| 28 | /* Include test driver definition when running tests */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 29 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 30 | #ifndef PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 31 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 32 | #endif |
| 33 | #ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 34 | #define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 35 | #endif |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 36 | #include "drivers/test_driver.h" |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 37 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 38 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 39 | /* Repeat above block for each JSON-declared driver during autogeneration */ |
| 40 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 41 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */ |
| 42 | |
| 43 | /* Support the 'old' SE interface when asked to */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 44 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 45 | /* 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 48 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 49 | #endif |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 50 | #include "psa_crypto_se.h" |
| 51 | #endif |
| 52 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 53 | /* Start delegation functions */ |
| 54 | psa_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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 62 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 63 | /* 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 Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 74 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 75 | } |
| 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 83 | #endif /* PSA_CRYPTO_SE_C */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 84 | |
| 85 | /* Then try accelerator API */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 86 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 87 | 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 98 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 99 | 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 Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 110 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 111 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 112 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 113 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 114 | /* Add cases for opaque driver here */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 115 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 116 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 117 | 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 126 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 127 | default: |
| 128 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 129 | return( status ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 130 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 131 | #else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 132 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 133 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 134 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 135 | (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 Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 143 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 144 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 147 | psa_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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 154 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 155 | /* 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 Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 166 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 167 | } |
| 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 174 | #endif /* PSA_CRYPTO_SE_C */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 175 | |
| 176 | /* Then try accelerator API */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 177 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 178 | 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 189 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 190 | 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 Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 200 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 201 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 202 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 203 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 204 | /* Add cases for opaque driver here */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 205 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 206 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 207 | 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 215 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 216 | default: |
| 217 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 218 | return( status ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 219 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 220 | #else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 221 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 222 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 223 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 224 | (void)slot; |
| 225 | (void)alg; |
| 226 | (void)hash; |
| 227 | (void)hash_length; |
| 228 | (void)signature; |
| 229 | (void)signature_length; |
| 230 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 231 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 232 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 235 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 236 | /** 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 Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 249 | static psa_status_t get_expected_key_size( const psa_key_attributes_t *attributes, |
| 250 | size_t *expected_size ) |
| 251 | { |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 252 | size_t buffer_size = 0; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 253 | if( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == PSA_KEY_LOCATION_LOCAL_STORAGE ) |
| 254 | { |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 255 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( attributes->core.type, |
| 256 | attributes->core.bits ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 257 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 258 | if( buffer_size == 0 ) |
| 259 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 260 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 261 | *expected_size = buffer_size; |
| 262 | return( PSA_SUCCESS ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 263 | } |
| 264 | else |
| 265 | { |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 266 | /* 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 Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 270 | } |
| 271 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 272 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 273 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 274 | psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes, |
| 275 | psa_key_slot_t *slot ) |
| 276 | { |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 277 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 278 | /* 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 Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 290 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 291 | } |
| 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 300 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 301 | 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 Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 307 | return( status ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 308 | |
| 309 | slot->data.key.data = mbedtls_calloc(1, export_size); |
| 310 | if( slot->data.key.data == NULL ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 311 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 312 | 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 326 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 327 | 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 334 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 335 | /* Fell through, meaning no accelerator supports this operation */ |
| 336 | status = PSA_ERROR_NOT_SUPPORTED; |
| 337 | break; |
| 338 | /* Add cases for opaque driver here */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 339 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 340 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 341 | status = test_opaque_generate_key( attributes, |
| 342 | slot->data.key.data, |
| 343 | slot->data.key.bytes, |
| 344 | &slot->data.key.bytes ); |
| 345 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 346 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 347 | 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 362 | #else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 363 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 364 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 365 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 366 | (void) attributes; |
| 367 | (void) slot; |
| 368 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame^] | 369 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 370 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 373 | /* End of automatically generated file. */ |