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 | 0d7c64d | 2020-09-07 16:17:55 +0200 | [diff] [blame] | 36 | #include "test/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 | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 41 | /* Auto-generated values depending on which drivers are registered. ID 0 is |
| 42 | * reserved for unallocated operations. */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 43 | #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 Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 47 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */ |
| 48 | |
| 49 | /* Support the 'old' SE interface when asked to */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 50 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 51 | /* 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 54 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 55 | #endif |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 56 | #include "psa_crypto_se.h" |
| 57 | #endif |
| 58 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 59 | /* Start delegation functions */ |
| 60 | psa_status_t psa_driver_wrapper_sign_hash( psa_key_slot_t *slot, |
| 61 | psa_algorithm_t alg, |
| 62 | const uint8_t *hash, |
| 63 | size_t hash_length, |
| 64 | uint8_t *signature, |
| 65 | size_t signature_size, |
| 66 | size_t *signature_length ) |
| 67 | { |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 68 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 69 | /* 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 | |
| 74 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 75 | { |
| 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 Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 80 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 81 | } |
| 82 | return( drv->asymmetric->p_sign( drv_context, |
| 83 | slot->data.se.slot_number, |
| 84 | alg, |
| 85 | hash, hash_length, |
| 86 | signature, signature_size, |
| 87 | signature_length ) ); |
| 88 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 89 | #endif /* PSA_CRYPTO_SE_C */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 90 | |
| 91 | /* Then try accelerator API */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 92 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 93 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 94 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 95 | psa_key_attributes_t attributes = { |
| 96 | .core = slot->attr |
| 97 | }; |
| 98 | |
| 99 | switch( location ) |
| 100 | { |
| 101 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 102 | /* Key is stored in the slot in export representation, so |
| 103 | * cycle through all known transparent accelerators */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 104 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 105 | status = test_transparent_signature_sign_hash( &attributes, |
| 106 | slot->data.key.data, |
| 107 | slot->data.key.bytes, |
| 108 | alg, |
| 109 | hash, |
| 110 | hash_length, |
| 111 | signature, |
| 112 | signature_size, |
| 113 | signature_length ); |
| 114 | /* Declared with fallback == true */ |
| 115 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 116 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 117 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 118 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 119 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 120 | /* Add cases for opaque driver here */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 121 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 122 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 123 | return( test_opaque_signature_sign_hash( &attributes, |
| 124 | slot->data.key.data, |
| 125 | slot->data.key.bytes, |
| 126 | alg, |
| 127 | hash, |
| 128 | hash_length, |
| 129 | signature, |
| 130 | signature_size, |
| 131 | signature_length ) ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 132 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 133 | default: |
| 134 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 135 | return( status ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 136 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 137 | #else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 138 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 139 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 140 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 141 | (void)slot; |
| 142 | (void)alg; |
| 143 | (void)hash; |
| 144 | (void)hash_length; |
| 145 | (void)signature; |
| 146 | (void)signature_size; |
| 147 | (void)signature_length; |
| 148 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 149 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 150 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 153 | psa_status_t psa_driver_wrapper_verify_hash( psa_key_slot_t *slot, |
| 154 | psa_algorithm_t alg, |
| 155 | const uint8_t *hash, |
| 156 | size_t hash_length, |
| 157 | const uint8_t *signature, |
| 158 | size_t signature_length ) |
| 159 | { |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 160 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 161 | /* Try dynamically-registered SE interface first */ |
| 162 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 163 | const psa_drv_se_t *drv; |
| 164 | psa_drv_se_context_t *drv_context; |
| 165 | |
| 166 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 167 | { |
| 168 | if( drv->asymmetric == NULL || |
| 169 | drv->asymmetric->p_verify == NULL ) |
| 170 | { |
| 171 | /* 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] | 172 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 173 | } |
| 174 | return( drv->asymmetric->p_verify( drv_context, |
| 175 | slot->data.se.slot_number, |
| 176 | alg, |
| 177 | hash, hash_length, |
| 178 | signature, signature_length ) ); |
| 179 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 180 | #endif /* PSA_CRYPTO_SE_C */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 181 | |
| 182 | /* Then try accelerator API */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 183 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 184 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 185 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 186 | psa_key_attributes_t attributes = { |
| 187 | .core = slot->attr |
| 188 | }; |
| 189 | |
| 190 | switch( location ) |
| 191 | { |
| 192 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 193 | /* Key is stored in the slot in export representation, so |
| 194 | * cycle through all known transparent accelerators */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 195 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 196 | status = test_transparent_signature_verify_hash( &attributes, |
| 197 | slot->data.key.data, |
| 198 | slot->data.key.bytes, |
| 199 | alg, |
| 200 | hash, |
| 201 | hash_length, |
| 202 | signature, |
| 203 | signature_length ); |
| 204 | /* Declared with fallback == true */ |
| 205 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 206 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 207 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 208 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 209 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 210 | /* Add cases for opaque driver here */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 211 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 212 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 213 | return( test_opaque_signature_verify_hash( &attributes, |
| 214 | slot->data.key.data, |
| 215 | slot->data.key.bytes, |
| 216 | alg, |
| 217 | hash, |
| 218 | hash_length, |
| 219 | signature, |
| 220 | signature_length ) ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 221 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 222 | default: |
| 223 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 224 | return( status ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 225 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 226 | #else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 227 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 228 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 229 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 230 | (void)slot; |
| 231 | (void)alg; |
| 232 | (void)hash; |
| 233 | (void)hash_length; |
| 234 | (void)signature; |
| 235 | (void)signature_length; |
| 236 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 237 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 238 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 241 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 242 | /** Calculate the size to allocate for buffering a key with given attributes. |
| 243 | * |
| 244 | * This function provides a way to get the expected size for storing a key with |
| 245 | * the given attributes. This will be the size of the export representation for |
| 246 | * cleartext keys, and a driver-defined size for keys stored by opaque drivers. |
| 247 | * |
| 248 | * \param[in] attributes The key attribute structure of the key to store. |
| 249 | * \param[out] expected_size On success, a byte size large enough to contain |
| 250 | * the declared key. |
| 251 | * |
| 252 | * \retval #PSA_SUCCESS |
| 253 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 254 | */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 255 | static psa_status_t get_expected_key_size( const psa_key_attributes_t *attributes, |
| 256 | size_t *expected_size ) |
| 257 | { |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 258 | size_t buffer_size = 0; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 259 | if( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) == PSA_KEY_LOCATION_LOCAL_STORAGE ) |
| 260 | { |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 261 | buffer_size = PSA_KEY_EXPORT_MAX_SIZE( attributes->core.type, |
| 262 | attributes->core.bits ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 263 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 264 | if( buffer_size == 0 ) |
| 265 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 266 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 267 | *expected_size = buffer_size; |
| 268 | return( PSA_SUCCESS ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 269 | } |
| 270 | else |
| 271 | { |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 272 | /* TBD: opaque driver support: need to calculate size through a |
| 273 | * driver-defined size function, since the size of an opaque (wrapped) |
| 274 | * key will be different for each implementation. */ |
| 275 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 276 | } |
| 277 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 278 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 279 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 280 | psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes, |
| 281 | psa_key_slot_t *slot ) |
| 282 | { |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 283 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 284 | /* Try dynamically-registered SE interface first */ |
| 285 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 286 | const psa_drv_se_t *drv; |
| 287 | psa_drv_se_context_t *drv_context; |
| 288 | |
| 289 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 290 | { |
| 291 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 292 | if( drv->key_management == NULL || |
| 293 | drv->key_management->p_generate == NULL ) |
| 294 | { |
| 295 | /* 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] | 296 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 297 | } |
| 298 | return( drv->key_management->p_generate( |
| 299 | drv_context, |
| 300 | slot->data.se.slot_number, attributes, |
| 301 | NULL, 0, &pubkey_length ) ); |
| 302 | } |
| 303 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 304 | |
| 305 | /* Then try accelerator API */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 306 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 307 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 308 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 309 | size_t export_size = 0; |
| 310 | |
| 311 | status = get_expected_key_size( attributes, &export_size ); |
| 312 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 313 | return( status ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 314 | |
| 315 | slot->data.key.data = mbedtls_calloc(1, export_size); |
| 316 | if( slot->data.key.data == NULL ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 317 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 318 | slot->data.key.bytes = export_size; |
| 319 | |
| 320 | switch( location ) |
| 321 | { |
| 322 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 323 | /* Key is stored in the slot in export representation, so |
| 324 | * cycle through all known transparent accelerators */ |
| 325 | |
| 326 | /* Transparent drivers are limited to generating asymmetric keys */ |
| 327 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 328 | { |
| 329 | status = PSA_ERROR_NOT_SUPPORTED; |
| 330 | break; |
| 331 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 332 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 333 | status = test_transparent_generate_key( attributes, |
| 334 | slot->data.key.data, |
| 335 | slot->data.key.bytes, |
| 336 | &slot->data.key.bytes ); |
| 337 | /* Declared with fallback == true */ |
| 338 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 339 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 340 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 341 | /* Fell through, meaning no accelerator supports this operation */ |
| 342 | status = PSA_ERROR_NOT_SUPPORTED; |
| 343 | break; |
| 344 | /* Add cases for opaque driver here */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 345 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 346 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 347 | status = test_opaque_generate_key( attributes, |
| 348 | slot->data.key.data, |
| 349 | slot->data.key.bytes, |
| 350 | &slot->data.key.bytes ); |
| 351 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 352 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 353 | default: |
| 354 | /* Key is declared with a lifetime not known to us */ |
| 355 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | if( status != PSA_SUCCESS ) |
| 360 | { |
| 361 | /* free allocated buffer */ |
| 362 | mbedtls_free( slot->data.key.data ); |
| 363 | slot->data.key.data = NULL; |
| 364 | slot->data.key.bytes = 0; |
| 365 | } |
| 366 | |
| 367 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 368 | #else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
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_ACCELERATOR_DRIVER_PRESENT */ |
| 371 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 372 | (void) attributes; |
| 373 | (void) slot; |
| 374 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 375 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 376 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 379 | /* |
| 380 | * Cipher functions |
| 381 | */ |
| 382 | psa_status_t psa_driver_wrapper_cipher_encrypt( |
| 383 | psa_key_slot_t *slot, |
| 384 | psa_algorithm_t alg, |
| 385 | const uint8_t *input, |
| 386 | size_t input_length, |
| 387 | uint8_t *output, |
| 388 | size_t output_size, |
| 389 | size_t *output_length ) |
| 390 | { |
| 391 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 392 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 393 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 394 | psa_key_attributes_t attributes = { |
| 395 | .core = slot->attr |
| 396 | }; |
| 397 | |
| 398 | switch( location ) |
| 399 | { |
| 400 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 401 | /* Key is stored in the slot in export representation, so |
| 402 | * cycle through all known transparent accelerators */ |
| 403 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 404 | status = test_transparent_cipher_encrypt( &attributes, |
| 405 | slot->data.key.data, |
| 406 | slot->data.key.bytes, |
| 407 | alg, |
| 408 | input, |
| 409 | input_length, |
| 410 | output, |
| 411 | output_size, |
| 412 | output_length ); |
| 413 | /* Declared with fallback == true */ |
| 414 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 415 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 416 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 417 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 418 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 419 | /* Add cases for opaque driver here */ |
| 420 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 421 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 422 | return( test_opaque_cipher_encrypt( &attributes, |
| 423 | slot->data.key.data, |
| 424 | slot->data.key.bytes, |
| 425 | alg, |
| 426 | input, |
| 427 | input_length, |
| 428 | output, |
| 429 | output_size, |
| 430 | output_length ) ); |
| 431 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 432 | default: |
| 433 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 434 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 435 | } |
| 436 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 437 | (void) slot; |
| 438 | (void) alg; |
| 439 | (void) input; |
| 440 | (void) input_length; |
| 441 | (void) output; |
| 442 | (void) output_size; |
| 443 | (void) output_length; |
| 444 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 445 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 446 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 447 | } |
| 448 | |
| 449 | psa_status_t psa_driver_wrapper_cipher_decrypt( |
| 450 | psa_key_slot_t *slot, |
| 451 | psa_algorithm_t alg, |
| 452 | const uint8_t *input, |
| 453 | size_t input_length, |
| 454 | uint8_t *output, |
| 455 | size_t output_size, |
| 456 | size_t *output_length ) |
| 457 | { |
| 458 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 459 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 460 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 461 | psa_key_attributes_t attributes = { |
| 462 | .core = slot->attr |
| 463 | }; |
| 464 | |
| 465 | switch( location ) |
| 466 | { |
| 467 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 468 | /* Key is stored in the slot in export representation, so |
| 469 | * cycle through all known transparent accelerators */ |
| 470 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 471 | status = test_transparent_cipher_decrypt( &attributes, |
| 472 | slot->data.key.data, |
| 473 | slot->data.key.bytes, |
| 474 | alg, |
| 475 | input, |
| 476 | input_length, |
| 477 | output, |
| 478 | output_size, |
| 479 | output_length ); |
| 480 | /* Declared with fallback == true */ |
| 481 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 482 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 483 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 484 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 485 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 486 | /* Add cases for opaque driver here */ |
| 487 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 488 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 489 | return( test_opaque_cipher_decrypt( &attributes, |
| 490 | slot->data.key.data, |
| 491 | slot->data.key.bytes, |
| 492 | alg, |
| 493 | input, |
| 494 | input_length, |
| 495 | output, |
| 496 | output_size, |
| 497 | output_length ) ); |
| 498 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 499 | default: |
| 500 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 501 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 502 | } |
| 503 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 504 | (void) slot; |
| 505 | (void) alg; |
| 506 | (void) input; |
| 507 | (void) input_length; |
| 508 | (void) output; |
| 509 | (void) output_size; |
| 510 | (void) output_length; |
| 511 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 512 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 513 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 514 | } |
| 515 | |
| 516 | psa_status_t psa_driver_wrapper_cipher_encrypt_setup( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 517 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 518 | psa_key_slot_t *slot, |
| 519 | psa_algorithm_t alg ) |
| 520 | { |
| 521 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 522 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 523 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 524 | psa_key_attributes_t attributes = { |
| 525 | .core = slot->attr |
| 526 | }; |
| 527 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 528 | switch( location ) |
| 529 | { |
| 530 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 531 | /* Key is stored in the slot in export representation, so |
| 532 | * cycle through all known transparent accelerators */ |
| 533 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 534 | operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) ); |
| 535 | if( operation->ctx == NULL ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 536 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 537 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 538 | status = test_transparent_cipher_encrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 539 | &attributes, |
| 540 | slot->data.key.data, |
| 541 | slot->data.key.bytes, |
| 542 | alg ); |
| 543 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 544 | if( status == PSA_SUCCESS ) |
| 545 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 546 | else |
| 547 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame^] | 548 | mbedtls_platform_zeroize( |
| 549 | operation->ctx, |
| 550 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 551 | mbedtls_free( operation->ctx ); |
| 552 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 553 | } |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 554 | |
| 555 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 556 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 557 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 558 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 559 | /* Add cases for opaque driver here */ |
| 560 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 561 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 562 | operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) ); |
| 563 | if( operation->ctx == NULL ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 564 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 565 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 566 | status = test_opaque_cipher_encrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 567 | &attributes, |
| 568 | slot->data.key.data, |
| 569 | slot->data.key.bytes, |
| 570 | alg ); |
| 571 | if( status == PSA_SUCCESS ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 572 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 573 | else |
| 574 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame^] | 575 | mbedtls_platform_zeroize( |
| 576 | operation->ctx, |
| 577 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 578 | mbedtls_free( operation->ctx ); |
| 579 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 580 | } |
| 581 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 582 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 583 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 584 | default: |
| 585 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 586 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 587 | } |
| 588 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 589 | (void)slot; |
| 590 | (void)alg; |
| 591 | (void)operation; |
| 592 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 593 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 594 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 595 | } |
| 596 | |
| 597 | psa_status_t psa_driver_wrapper_cipher_decrypt_setup( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 598 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 599 | psa_key_slot_t *slot, |
| 600 | psa_algorithm_t alg ) |
| 601 | { |
| 602 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 603 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 604 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 605 | psa_key_attributes_t attributes = { |
| 606 | .core = slot->attr |
| 607 | }; |
| 608 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 609 | switch( location ) |
| 610 | { |
| 611 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 612 | /* Key is stored in the slot in export representation, so |
| 613 | * cycle through all known transparent accelerators */ |
| 614 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 615 | operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) ); |
| 616 | if( operation->ctx == NULL ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 617 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 618 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 619 | status = test_transparent_cipher_decrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 620 | &attributes, |
| 621 | slot->data.key.data, |
| 622 | slot->data.key.bytes, |
| 623 | alg ); |
| 624 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 625 | if( status == PSA_SUCCESS ) |
| 626 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 627 | else |
| 628 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame^] | 629 | mbedtls_platform_zeroize( |
| 630 | operation->ctx, |
| 631 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 632 | mbedtls_free( operation->ctx ); |
| 633 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 634 | } |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 635 | |
| 636 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 637 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 638 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 639 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 640 | /* Add cases for opaque driver here */ |
| 641 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 642 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 643 | operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) ); |
| 644 | if( operation->ctx == NULL ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 645 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 646 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 647 | status = test_opaque_cipher_decrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 648 | &attributes, |
| 649 | slot->data.key.data, |
| 650 | slot->data.key.bytes, |
| 651 | alg ); |
| 652 | if( status == PSA_SUCCESS ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 653 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 654 | else |
| 655 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame^] | 656 | mbedtls_platform_zeroize( |
| 657 | operation->ctx, |
| 658 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 659 | mbedtls_free( operation->ctx ); |
| 660 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 661 | } |
| 662 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 663 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 664 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 665 | default: |
| 666 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 667 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 668 | } |
| 669 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 670 | (void)slot; |
| 671 | (void)alg; |
| 672 | (void)operation; |
| 673 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 674 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 675 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 676 | } |
| 677 | |
| 678 | psa_status_t psa_driver_wrapper_cipher_generate_iv( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 679 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 680 | uint8_t *iv, |
| 681 | size_t iv_size, |
| 682 | size_t *iv_length ) |
| 683 | { |
| 684 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 685 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 686 | { |
| 687 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 688 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 689 | return( test_transparent_cipher_generate_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 690 | iv, |
| 691 | iv_size, |
| 692 | iv_length ) ); |
| 693 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 694 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 695 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 696 | return( test_opaque_cipher_generate_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 697 | iv, |
| 698 | iv_size, |
| 699 | iv_length ) ); |
| 700 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 701 | default: |
| 702 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 703 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 704 | } |
| 705 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 706 | (void) operation; |
| 707 | (void) iv; |
| 708 | (void) iv_size; |
| 709 | (void) iv_length; |
| 710 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 711 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 712 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 713 | } |
| 714 | |
| 715 | psa_status_t psa_driver_wrapper_cipher_set_iv( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 716 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 717 | const uint8_t *iv, |
| 718 | size_t iv_length ) |
| 719 | { |
| 720 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 721 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 722 | { |
| 723 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 724 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 725 | return( test_transparent_cipher_set_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 726 | iv, |
| 727 | iv_length ) ); |
| 728 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 729 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 730 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 731 | return( test_opaque_cipher_set_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 732 | iv, |
| 733 | iv_length ) ); |
| 734 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 735 | default: |
| 736 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 737 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 738 | } |
| 739 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 740 | (void) operation; |
| 741 | (void) iv; |
| 742 | (void) iv_length; |
| 743 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 744 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 745 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 746 | } |
| 747 | |
| 748 | psa_status_t psa_driver_wrapper_cipher_update( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 749 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 750 | const uint8_t *input, |
| 751 | size_t input_length, |
| 752 | uint8_t *output, |
| 753 | size_t output_size, |
| 754 | size_t *output_length ) |
| 755 | { |
| 756 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 757 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 758 | { |
| 759 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 760 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 761 | return( test_transparent_cipher_update( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 762 | input, |
| 763 | input_length, |
| 764 | output, |
| 765 | output_size, |
| 766 | output_length ) ); |
| 767 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 768 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 769 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 770 | return( test_opaque_cipher_update( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 771 | input, |
| 772 | input_length, |
| 773 | output, |
| 774 | output_size, |
| 775 | output_length ) ); |
| 776 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 777 | default: |
| 778 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 779 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 780 | } |
| 781 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 782 | (void) operation; |
| 783 | (void) input; |
| 784 | (void) input_length; |
| 785 | (void) output; |
| 786 | (void) output_length; |
| 787 | (void) output_size; |
| 788 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 789 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 790 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 791 | } |
| 792 | |
| 793 | psa_status_t psa_driver_wrapper_cipher_finish( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 794 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 795 | uint8_t *output, |
| 796 | size_t output_size, |
| 797 | size_t *output_length ) |
| 798 | { |
| 799 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 800 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 801 | { |
| 802 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 803 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 804 | return( test_transparent_cipher_finish( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 805 | output, |
| 806 | output_size, |
| 807 | output_length ) ); |
| 808 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 809 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 810 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 811 | return( test_opaque_cipher_finish( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 812 | output, |
| 813 | output_size, |
| 814 | output_length ) ); |
| 815 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 816 | default: |
| 817 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 818 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 819 | } |
| 820 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 821 | (void) operation; |
| 822 | (void) output; |
| 823 | (void) output_size; |
| 824 | (void) output_length; |
| 825 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 826 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 827 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 828 | } |
| 829 | |
| 830 | psa_status_t psa_driver_wrapper_cipher_abort( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 831 | psa_operation_driver_context_t *operation ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 832 | { |
| 833 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 834 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame^] | 835 | |
| 836 | /* The object has (apparently) been initialized but it is not in use. It's |
| 837 | * ok to call abort on such an object, and there's nothing to do. */ |
| 838 | if( operation->ctx == NULL && operation->id == 0 ) |
| 839 | return( PSA_SUCCESS ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 840 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 841 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 842 | { |
| 843 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 844 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 845 | status = test_transparent_cipher_abort( operation->ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame^] | 846 | mbedtls_platform_zeroize( |
| 847 | operation->ctx, |
| 848 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 849 | mbedtls_free( operation->ctx ); |
| 850 | operation->ctx = NULL; |
| 851 | operation->id = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 852 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 853 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 854 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 855 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 856 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 857 | status = test_opaque_cipher_abort( operation->ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame^] | 858 | mbedtls_platform_zeroize( |
| 859 | operation->ctx, |
| 860 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 861 | mbedtls_free( operation->ctx ); |
| 862 | operation->ctx = NULL; |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame^] | 863 | operation->id = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 864 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 865 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 866 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 867 | default: |
| 868 | /* Operation is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 869 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 870 | } |
| 871 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 872 | (void)operation; |
| 873 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 874 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 875 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 876 | } |
| 877 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 878 | /* End of automatically generated file. */ |