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, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 83 | psa_key_slot_get_slot_number( slot ), |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 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, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 106 | slot->key.data, |
| 107 | slot->key.bytes, |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 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, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 124 | slot->key.data, |
| 125 | slot->key.bytes, |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 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, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 175 | psa_key_slot_get_slot_number( slot ), |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 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, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 197 | slot->key.data, |
| 198 | slot->key.bytes, |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 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, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 214 | slot->key.data, |
| 215 | slot->key.bytes, |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 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) |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame^] | 242 | /** Get the key buffer size for the key material of a generated key in the |
| 243 | * case of an opaque driver without storage. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 244 | * |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame^] | 245 | * \param[in] attributes The key attributes. |
| 246 | * \param[out] key_buffer_size Minimum buffer size to contain the key material |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 247 | * |
| 248 | * \retval #PSA_SUCCESS |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame^] | 249 | * The minimum size for a buffer to contain the key material has been |
| 250 | * returned successfully. |
| 251 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 252 | * The size in bits of the key is not valid. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 253 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame^] | 254 | * The type and/or the size in bits of the key or the combination of |
| 255 | * the two is not supported. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 256 | */ |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame^] | 257 | static psa_status_t get_key_buffer_size( |
| 258 | const psa_key_attributes_t *attributes, |
| 259 | size_t *key_buffer_size ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 260 | { |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 261 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 262 | psa_key_type_t key_type = attributes->core.type; |
| 263 | size_t key_bits = attributes->core.bits; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 264 | |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame^] | 265 | *key_buffer_size = 0; |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 266 | switch( location ) |
| 267 | { |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 268 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 269 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
John Durkop | ac93e3b | 2020-10-16 06:48:55 -0700 | [diff] [blame] | 270 | #ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame^] | 271 | *key_buffer_size = test_size_function( key_type, key_bits ); |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 272 | return( PSA_SUCCESS ); |
| 273 | #else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */ |
| 274 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) ) |
| 275 | { |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame^] | 276 | int public_key_overhead = |
| 277 | ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ? |
| 278 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 ); |
| 279 | *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE |
John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 280 | + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE |
| 281 | + public_key_overhead; |
| 282 | } |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame^] | 283 | else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) ) |
John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 284 | { |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame^] | 285 | *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 286 | + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE; |
| 287 | } |
John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 288 | else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) && |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame^] | 289 | !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) ) |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 290 | { |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame^] | 291 | *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 292 | + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR |
| 293 | * ( ( key_bits + 7 ) / 8 ); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 298 | } |
| 299 | return( PSA_SUCCESS ); |
| 300 | #endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */ |
| 301 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 302 | |
| 303 | default: |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 304 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 305 | } |
| 306 | } |
John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 307 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 308 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 309 | psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes, |
| 310 | psa_key_slot_t *slot ) |
| 311 | { |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 312 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 313 | /* Try dynamically-registered SE interface first */ |
| 314 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 315 | const psa_drv_se_t *drv; |
| 316 | psa_drv_se_context_t *drv_context; |
| 317 | |
| 318 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 319 | { |
| 320 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 321 | if( drv->key_management == NULL || |
| 322 | drv->key_management->p_generate == NULL ) |
| 323 | { |
| 324 | /* Key is defined as being in SE, but we have no way to generate it */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 325 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 326 | } |
| 327 | return( drv->key_management->p_generate( |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 328 | drv_context, psa_key_slot_get_slot_number( slot ), |
| 329 | attributes, NULL, 0, &pubkey_length ) ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 330 | } |
| 331 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 332 | |
| 333 | /* Then try accelerator API */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 334 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 335 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 336 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 337 | size_t export_size = 0; |
| 338 | |
Ronald Cron | 9cca316 | 2020-12-05 19:07:47 +0100 | [diff] [blame] | 339 | /* If this is not to generate a key in a secure element or cryptoprocessor |
| 340 | * with storage. */ |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 341 | if( slot->key.data == NULL ) |
Ronald Cron | 9cca316 | 2020-12-05 19:07:47 +0100 | [diff] [blame] | 342 | { |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame^] | 343 | if( location == PSA_KEY_LOCATION_LOCAL_STORAGE ) |
| 344 | { |
| 345 | export_size = PSA_KEY_EXPORT_MAX_SIZE( attributes->core.type, |
| 346 | attributes->core.bits ); |
Ronald Cron | 9cca316 | 2020-12-05 19:07:47 +0100 | [diff] [blame] | 347 | |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame^] | 348 | if( export_size == 0 ) |
| 349 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | status = get_key_buffer_size( attributes, &export_size ); |
| 354 | if( status != PSA_SUCCESS ) |
| 355 | return( status ); |
| 356 | } |
| 357 | |
Ronald Cron | 9cca316 | 2020-12-05 19:07:47 +0100 | [diff] [blame] | 358 | slot->key.data = mbedtls_calloc(1, export_size); |
| 359 | if( slot->key.data == NULL ) |
| 360 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 361 | slot->key.bytes = export_size; |
| 362 | } |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 363 | |
| 364 | switch( location ) |
| 365 | { |
| 366 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 367 | /* Key is stored in the slot in export representation, so |
| 368 | * cycle through all known transparent accelerators */ |
| 369 | |
| 370 | /* Transparent drivers are limited to generating asymmetric keys */ |
| 371 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 372 | { |
| 373 | status = PSA_ERROR_NOT_SUPPORTED; |
| 374 | break; |
| 375 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 376 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 377 | status = test_transparent_generate_key( attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 378 | slot->key.data, |
| 379 | slot->key.bytes, |
| 380 | &slot->key.bytes ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 381 | /* Declared with fallback == true */ |
| 382 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 383 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 384 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 385 | /* Fell through, meaning no accelerator supports this operation */ |
| 386 | status = PSA_ERROR_NOT_SUPPORTED; |
| 387 | break; |
| 388 | /* Add cases for opaque driver here */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 389 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 390 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 391 | status = test_opaque_generate_key( attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 392 | slot->key.data, |
| 393 | slot->key.bytes, |
| 394 | &slot->key.bytes ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 395 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 396 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 397 | default: |
| 398 | /* Key is declared with a lifetime not known to us */ |
| 399 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 400 | break; |
| 401 | } |
| 402 | |
| 403 | if( status != PSA_SUCCESS ) |
| 404 | { |
| 405 | /* free allocated buffer */ |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 406 | mbedtls_free( slot->key.data ); |
| 407 | slot->key.data = NULL; |
| 408 | slot->key.bytes = 0; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 412 | #else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 413 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 414 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 415 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 416 | (void) attributes; |
| 417 | (void) slot; |
| 418 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 419 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 420 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 421 | } |
| 422 | |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 423 | psa_status_t psa_driver_wrapper_import_key( |
| 424 | const psa_key_attributes_t *attributes, |
| 425 | const uint8_t *data, |
| 426 | size_t data_length, |
| 427 | uint8_t *key_buffer, |
| 428 | size_t key_buffer_size, |
| 429 | size_t *key_buffer_length, |
| 430 | size_t *bits ) |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 431 | { |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 432 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 433 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 434 | psa_get_key_lifetime( attributes ) ); |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 435 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 436 | /* Try dynamically-registered SE interface first */ |
| 437 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 438 | const psa_drv_se_t *drv; |
| 439 | psa_drv_se_context_t *drv_context; |
| 440 | |
| 441 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 442 | { |
| 443 | if( drv->key_management == NULL || |
| 444 | drv->key_management->p_import == NULL ) |
| 445 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 446 | |
| 447 | /* The driver should set the number of key bits, however in |
| 448 | * case it doesn't, we initialize bits to an invalid value. */ |
| 449 | *bits = PSA_MAX_KEY_BITS + 1; |
| 450 | status = drv->key_management->p_import( |
| 451 | drv_context, |
| 452 | *( (psa_key_slot_number_t *)key_buffer ), |
| 453 | attributes, data, data_length, bits ); |
| 454 | |
| 455 | if( status != PSA_SUCCESS ) |
| 456 | return( status ); |
| 457 | |
| 458 | if( (*bits) > PSA_MAX_KEY_BITS ) |
| 459 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 460 | |
| 461 | return( PSA_SUCCESS ); |
| 462 | } |
| 463 | #endif /* PSA_CRYPTO_SE_C */ |
| 464 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 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_ACCELERATOR_DRIVER_PRESENT) |
| 471 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 472 | status = test_transparent_import_key( attributes, |
| 473 | data, data_length, |
| 474 | key_buffer, key_buffer_size, |
| 475 | key_buffer_length, bits ); |
| 476 | /* Declared with fallback == true */ |
| 477 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 478 | return( status ); |
| 479 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 480 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 481 | /* Fell through, meaning no accelerator supports this operation */ |
| 482 | return( psa_import_key_into_slot( attributes, |
| 483 | data, data_length, |
| 484 | key_buffer, key_buffer_size, |
| 485 | key_buffer_length, bits ) ); |
| 486 | |
| 487 | default: |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 488 | /* Importing a key with external storage in not yet supported. |
| 489 | * Return in error indicating that the lifetime is not valid. */ |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 490 | (void)status; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 491 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 492 | } |
| 493 | |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 494 | } |
| 495 | |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 496 | psa_status_t psa_driver_wrapper_export_key( |
| 497 | const psa_key_attributes_t *attributes, |
| 498 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 499 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 500 | |
| 501 | { |
| 502 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 503 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 504 | psa_get_key_lifetime( attributes ) ); |
| 505 | |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 506 | /* Try dynamically-registered SE interface first */ |
| 507 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 508 | const psa_drv_se_t *drv; |
| 509 | psa_drv_se_context_t *drv_context; |
| 510 | |
| 511 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 512 | { |
| 513 | if( ( drv->key_management == NULL ) || |
| 514 | ( drv->key_management->p_export == NULL ) ) |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 515 | { |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 516 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 517 | } |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 518 | |
| 519 | return( drv->key_management->p_export( |
| 520 | drv_context, |
| 521 | *( (psa_key_slot_number_t *)key_buffer ), |
| 522 | data, data_size, data_length ) ); |
| 523 | } |
| 524 | #endif /* PSA_CRYPTO_SE_C */ |
| 525 | |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 526 | switch( location ) |
| 527 | { |
| 528 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 529 | return( psa_export_key_internal( attributes, |
| 530 | key_buffer, |
| 531 | key_buffer_size, |
| 532 | data, |
| 533 | data_size, |
| 534 | data_length ) ); |
| 535 | |
| 536 | /* Add cases for opaque driver here */ |
| 537 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 538 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 539 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 540 | return( test_opaque_export_key( attributes, |
| 541 | key_buffer, |
| 542 | key_buffer_size, |
| 543 | data, |
| 544 | data_size, |
| 545 | data_length ) ); |
| 546 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 547 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 548 | default: |
| 549 | /* Key is declared with a lifetime not known to us */ |
| 550 | return( status ); |
| 551 | } |
| 552 | } |
| 553 | |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 554 | psa_status_t psa_driver_wrapper_export_public_key( |
| 555 | const psa_key_attributes_t *attributes, |
| 556 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 557 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 558 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 559 | { |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 560 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 561 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 562 | psa_get_key_lifetime( attributes ) ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 563 | |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 564 | /* Try dynamically-registered SE interface first */ |
| 565 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 566 | const psa_drv_se_t *drv; |
| 567 | psa_drv_se_context_t *drv_context; |
| 568 | |
| 569 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 570 | { |
| 571 | if( ( drv->key_management == NULL ) || |
| 572 | ( drv->key_management->p_export_public == NULL ) ) |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 573 | { |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 574 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 575 | } |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 576 | |
| 577 | return( drv->key_management->p_export_public( |
| 578 | drv_context, |
| 579 | *( (psa_key_slot_number_t *)key_buffer ), |
| 580 | data, data_size, data_length ) ); |
| 581 | } |
| 582 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 583 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 584 | switch( location ) |
| 585 | { |
| 586 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 587 | /* Key is stored in the slot in export representation, so |
| 588 | * cycle through all known transparent accelerators */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 589 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 590 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 591 | status = test_transparent_export_public_key( attributes, |
| 592 | key_buffer, |
| 593 | key_buffer_size, |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 594 | data, |
| 595 | data_size, |
| 596 | data_length ); |
| 597 | /* Declared with fallback == true */ |
| 598 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 599 | return( status ); |
| 600 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 601 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 602 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 603 | return( psa_export_public_key_internal( attributes, |
| 604 | key_buffer, |
| 605 | key_buffer_size, |
| 606 | data, |
| 607 | data_size, |
| 608 | data_length ) ); |
| 609 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 610 | /* Add cases for opaque driver here */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 611 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 612 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 613 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 614 | return( test_opaque_export_public_key( attributes, |
| 615 | key_buffer, |
| 616 | key_buffer_size, |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 617 | data, |
| 618 | data_size, |
| 619 | data_length ) ); |
| 620 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 621 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 622 | default: |
| 623 | /* Key is declared with a lifetime not known to us */ |
| 624 | return( status ); |
| 625 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 626 | } |
| 627 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 628 | /* |
| 629 | * Cipher functions |
| 630 | */ |
| 631 | psa_status_t psa_driver_wrapper_cipher_encrypt( |
| 632 | psa_key_slot_t *slot, |
| 633 | psa_algorithm_t alg, |
| 634 | const uint8_t *input, |
| 635 | size_t input_length, |
| 636 | uint8_t *output, |
| 637 | size_t output_size, |
| 638 | size_t *output_length ) |
| 639 | { |
| 640 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 641 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 642 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 643 | psa_key_attributes_t attributes = { |
| 644 | .core = slot->attr |
| 645 | }; |
| 646 | |
| 647 | switch( location ) |
| 648 | { |
| 649 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 650 | /* Key is stored in the slot in export representation, so |
| 651 | * cycle through all known transparent accelerators */ |
| 652 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 653 | status = test_transparent_cipher_encrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 654 | slot->key.data, |
| 655 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 656 | alg, |
| 657 | input, |
| 658 | input_length, |
| 659 | output, |
| 660 | output_size, |
| 661 | output_length ); |
| 662 | /* Declared with fallback == true */ |
| 663 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 664 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 665 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 666 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 667 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 668 | /* Add cases for opaque driver here */ |
| 669 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 670 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 671 | return( test_opaque_cipher_encrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 672 | slot->key.data, |
| 673 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 674 | alg, |
| 675 | input, |
| 676 | input_length, |
| 677 | output, |
| 678 | output_size, |
| 679 | output_length ) ); |
| 680 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 681 | default: |
| 682 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 683 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 684 | } |
| 685 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 686 | (void) slot; |
| 687 | (void) alg; |
| 688 | (void) input; |
| 689 | (void) input_length; |
| 690 | (void) output; |
| 691 | (void) output_size; |
| 692 | (void) output_length; |
| 693 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 694 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 695 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 696 | } |
| 697 | |
| 698 | psa_status_t psa_driver_wrapper_cipher_decrypt( |
| 699 | psa_key_slot_t *slot, |
| 700 | psa_algorithm_t alg, |
| 701 | const uint8_t *input, |
| 702 | size_t input_length, |
| 703 | uint8_t *output, |
| 704 | size_t output_size, |
| 705 | size_t *output_length ) |
| 706 | { |
| 707 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 708 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 709 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 710 | psa_key_attributes_t attributes = { |
| 711 | .core = slot->attr |
| 712 | }; |
| 713 | |
| 714 | switch( location ) |
| 715 | { |
| 716 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 717 | /* Key is stored in the slot in export representation, so |
| 718 | * cycle through all known transparent accelerators */ |
| 719 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 720 | status = test_transparent_cipher_decrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 721 | slot->key.data, |
| 722 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 723 | alg, |
| 724 | input, |
| 725 | input_length, |
| 726 | output, |
| 727 | output_size, |
| 728 | output_length ); |
| 729 | /* Declared with fallback == true */ |
| 730 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 731 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 732 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 733 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 734 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 735 | /* Add cases for opaque driver here */ |
| 736 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 737 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 738 | return( test_opaque_cipher_decrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 739 | slot->key.data, |
| 740 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 741 | alg, |
| 742 | input, |
| 743 | input_length, |
| 744 | output, |
| 745 | output_size, |
| 746 | output_length ) ); |
| 747 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 748 | default: |
| 749 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 750 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 751 | } |
| 752 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 753 | (void) slot; |
| 754 | (void) alg; |
| 755 | (void) input; |
| 756 | (void) input_length; |
| 757 | (void) output; |
| 758 | (void) output_size; |
| 759 | (void) output_length; |
| 760 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 761 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 762 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 763 | } |
| 764 | |
| 765 | psa_status_t psa_driver_wrapper_cipher_encrypt_setup( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 766 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 767 | psa_key_slot_t *slot, |
| 768 | psa_algorithm_t alg ) |
| 769 | { |
| 770 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 771 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 772 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 773 | psa_key_attributes_t attributes = { |
| 774 | .core = slot->attr |
| 775 | }; |
| 776 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 777 | switch( location ) |
| 778 | { |
| 779 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 780 | /* Key is stored in the slot in export representation, so |
| 781 | * cycle through all known transparent accelerators */ |
| 782 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 783 | operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) ); |
| 784 | if( operation->ctx == NULL ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 785 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 786 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 787 | status = test_transparent_cipher_encrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 788 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 789 | slot->key.data, |
| 790 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 791 | alg ); |
| 792 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 793 | if( status == PSA_SUCCESS ) |
| 794 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 795 | else |
| 796 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 797 | mbedtls_platform_zeroize( |
| 798 | operation->ctx, |
| 799 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 800 | mbedtls_free( operation->ctx ); |
| 801 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 802 | } |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 803 | |
| 804 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 805 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 806 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 807 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 808 | /* Add cases for opaque driver here */ |
| 809 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 810 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 811 | operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) ); |
| 812 | if( operation->ctx == NULL ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 813 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 814 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 815 | status = test_opaque_cipher_encrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 816 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 817 | slot->key.data, |
| 818 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 819 | alg ); |
| 820 | if( status == PSA_SUCCESS ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 821 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 822 | else |
| 823 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 824 | mbedtls_platform_zeroize( |
| 825 | operation->ctx, |
| 826 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 827 | mbedtls_free( operation->ctx ); |
| 828 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 829 | } |
| 830 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 831 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 832 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 833 | default: |
| 834 | /* Key is declared with a lifetime not known to us */ |
John Durkop | 714e3a1 | 2020-09-29 22:07:04 -0700 | [diff] [blame] | 835 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 836 | } |
| 837 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 838 | (void)slot; |
| 839 | (void)alg; |
| 840 | (void)operation; |
| 841 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 842 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 843 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 844 | } |
| 845 | |
| 846 | psa_status_t psa_driver_wrapper_cipher_decrypt_setup( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 847 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 848 | psa_key_slot_t *slot, |
| 849 | psa_algorithm_t alg ) |
| 850 | { |
| 851 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 852 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 853 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 854 | psa_key_attributes_t attributes = { |
| 855 | .core = slot->attr |
| 856 | }; |
| 857 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 858 | switch( location ) |
| 859 | { |
| 860 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 861 | /* Key is stored in the slot in export representation, so |
| 862 | * cycle through all known transparent accelerators */ |
| 863 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 864 | operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) ); |
| 865 | if( operation->ctx == NULL ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 866 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 867 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 868 | status = test_transparent_cipher_decrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 869 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 870 | slot->key.data, |
| 871 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 872 | alg ); |
| 873 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 874 | if( status == PSA_SUCCESS ) |
| 875 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 876 | else |
| 877 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 878 | mbedtls_platform_zeroize( |
| 879 | operation->ctx, |
| 880 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 881 | mbedtls_free( operation->ctx ); |
| 882 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 883 | } |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 884 | |
| 885 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 886 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 887 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 888 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 889 | /* Add cases for opaque driver here */ |
| 890 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 891 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 892 | operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) ); |
| 893 | if( operation->ctx == NULL ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 894 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 895 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 896 | status = test_opaque_cipher_decrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 897 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 898 | slot->key.data, |
| 899 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 900 | alg ); |
| 901 | if( status == PSA_SUCCESS ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 902 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 903 | else |
| 904 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 905 | mbedtls_platform_zeroize( |
| 906 | operation->ctx, |
| 907 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 908 | mbedtls_free( operation->ctx ); |
| 909 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 910 | } |
| 911 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 912 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 913 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 914 | default: |
| 915 | /* Key is declared with a lifetime not known to us */ |
John Durkop | 814dca7 | 2020-10-05 06:31:12 -0700 | [diff] [blame] | 916 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 917 | } |
| 918 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 919 | (void)slot; |
| 920 | (void)alg; |
| 921 | (void)operation; |
| 922 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 923 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 924 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 925 | } |
| 926 | |
| 927 | psa_status_t psa_driver_wrapper_cipher_generate_iv( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 928 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 929 | uint8_t *iv, |
| 930 | size_t iv_size, |
| 931 | size_t *iv_length ) |
| 932 | { |
| 933 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 934 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 935 | { |
| 936 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 937 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 938 | return( test_transparent_cipher_generate_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 939 | iv, |
| 940 | iv_size, |
| 941 | iv_length ) ); |
| 942 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 943 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 944 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 945 | return( test_opaque_cipher_generate_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 946 | iv, |
| 947 | iv_size, |
| 948 | iv_length ) ); |
| 949 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 950 | default: |
| 951 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 952 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 953 | } |
| 954 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 955 | (void) operation; |
| 956 | (void) iv; |
| 957 | (void) iv_size; |
| 958 | (void) iv_length; |
| 959 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 960 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 961 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 962 | } |
| 963 | |
| 964 | psa_status_t psa_driver_wrapper_cipher_set_iv( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 965 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 966 | const uint8_t *iv, |
| 967 | size_t iv_length ) |
| 968 | { |
| 969 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 970 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 971 | { |
| 972 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 973 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 974 | return( test_transparent_cipher_set_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 975 | iv, |
| 976 | iv_length ) ); |
| 977 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 978 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 979 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 980 | return( test_opaque_cipher_set_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 981 | iv, |
| 982 | iv_length ) ); |
| 983 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 984 | default: |
| 985 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 986 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 987 | } |
| 988 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 989 | (void) operation; |
| 990 | (void) iv; |
| 991 | (void) iv_length; |
| 992 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 993 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 994 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 995 | } |
| 996 | |
| 997 | psa_status_t psa_driver_wrapper_cipher_update( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 998 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 999 | const uint8_t *input, |
| 1000 | size_t input_length, |
| 1001 | uint8_t *output, |
| 1002 | size_t output_size, |
| 1003 | size_t *output_length ) |
| 1004 | { |
| 1005 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1006 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1007 | { |
| 1008 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1009 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1010 | return( test_transparent_cipher_update( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1011 | input, |
| 1012 | input_length, |
| 1013 | output, |
| 1014 | output_size, |
| 1015 | output_length ) ); |
| 1016 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1017 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1018 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1019 | return( test_opaque_cipher_update( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1020 | input, |
| 1021 | input_length, |
| 1022 | output, |
| 1023 | output_size, |
| 1024 | output_length ) ); |
| 1025 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1026 | default: |
| 1027 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1028 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1029 | } |
| 1030 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1031 | (void) operation; |
| 1032 | (void) input; |
| 1033 | (void) input_length; |
| 1034 | (void) output; |
| 1035 | (void) output_length; |
| 1036 | (void) output_size; |
| 1037 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1038 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1039 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1040 | } |
| 1041 | |
| 1042 | psa_status_t psa_driver_wrapper_cipher_finish( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1043 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1044 | uint8_t *output, |
| 1045 | size_t output_size, |
| 1046 | size_t *output_length ) |
| 1047 | { |
| 1048 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1049 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1050 | { |
| 1051 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1052 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1053 | return( test_transparent_cipher_finish( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1054 | output, |
| 1055 | output_size, |
| 1056 | output_length ) ); |
| 1057 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1058 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1059 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1060 | return( test_opaque_cipher_finish( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1061 | output, |
| 1062 | output_size, |
| 1063 | output_length ) ); |
| 1064 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1065 | default: |
| 1066 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1067 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1068 | } |
| 1069 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1070 | (void) operation; |
| 1071 | (void) output; |
| 1072 | (void) output_size; |
| 1073 | (void) output_length; |
| 1074 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1075 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1076 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1077 | } |
| 1078 | |
| 1079 | psa_status_t psa_driver_wrapper_cipher_abort( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1080 | psa_operation_driver_context_t *operation ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1081 | { |
| 1082 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1083 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1084 | |
| 1085 | /* The object has (apparently) been initialized but it is not in use. It's |
| 1086 | * ok to call abort on such an object, and there's nothing to do. */ |
| 1087 | if( operation->ctx == NULL && operation->id == 0 ) |
| 1088 | return( PSA_SUCCESS ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1089 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1090 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1091 | { |
| 1092 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1093 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1094 | status = test_transparent_cipher_abort( operation->ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1095 | mbedtls_platform_zeroize( |
| 1096 | operation->ctx, |
| 1097 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1098 | mbedtls_free( operation->ctx ); |
| 1099 | operation->ctx = NULL; |
| 1100 | operation->id = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1101 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1102 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1103 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1104 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1105 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1106 | status = test_opaque_cipher_abort( operation->ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1107 | mbedtls_platform_zeroize( |
| 1108 | operation->ctx, |
| 1109 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1110 | mbedtls_free( operation->ctx ); |
| 1111 | operation->ctx = NULL; |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1112 | operation->id = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1113 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1114 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1115 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1116 | default: |
| 1117 | /* Operation is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1118 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1119 | } |
| 1120 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1121 | (void)operation; |
| 1122 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1123 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1124 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1125 | } |
| 1126 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 1127 | /* End of automatically generated file. */ |