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