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 | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame^] | 24 | #include "psa_crypto_hash.h" |
| 25 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 26 | #include "mbedtls/platform.h" |
| 27 | |
| 28 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 29 | |
| 30 | /* Include test driver definition when running tests */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 31 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 32 | #ifndef PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 33 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 34 | #endif |
| 35 | #ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 36 | #define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 37 | #endif |
Steven Cooreman | 0d7c64d | 2020-09-07 16:17:55 +0200 | [diff] [blame] | 38 | #include "test/drivers/test_driver.h" |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 39 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 40 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 41 | /* Repeat above block for each JSON-declared driver during autogeneration */ |
| 42 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 43 | /* Auto-generated values depending on which drivers are registered. |
| 44 | * ID 0 is reserved for unallocated operations. |
| 45 | * ID 1 is reserved for the Mbed TLS software driver. */ |
Steven Cooreman | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame^] | 46 | #define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1) |
| 47 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 48 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 49 | #define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2) |
| 50 | #define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 51 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 52 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */ |
| 53 | |
| 54 | /* Support the 'old' SE interface when asked to */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 55 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 56 | /* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style |
| 57 | * SE driver is present, to avoid unused argument errors at compile time. */ |
| 58 | #ifndef PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 59 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 60 | #endif |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 61 | #include "psa_crypto_se.h" |
| 62 | #endif |
| 63 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 64 | /* Start delegation functions */ |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 65 | psa_status_t psa_driver_wrapper_sign_hash( |
| 66 | const psa_key_attributes_t *attributes, |
| 67 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 68 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 69 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 70 | { |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 71 | /* Try dynamically-registered SE interface first */ |
| 72 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 73 | const psa_drv_se_t *drv; |
| 74 | psa_drv_se_context_t *drv_context; |
| 75 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 76 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 77 | { |
| 78 | if( drv->asymmetric == NULL || |
| 79 | drv->asymmetric->p_sign == NULL ) |
| 80 | { |
| 81 | /* 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] | 82 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 83 | } |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 84 | return( drv->asymmetric->p_sign( |
| 85 | drv_context, *( (psa_key_slot_number_t *)key_buffer ), |
| 86 | alg, hash, hash_length, |
| 87 | signature, signature_size, signature_length ) ); |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 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 | |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 91 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 92 | psa_key_location_t location = |
| 93 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 94 | |
| 95 | switch( location ) |
| 96 | { |
| 97 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 98 | /* Key is stored in the slot in export representation, so |
| 99 | * cycle through all known transparent accelerators */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 100 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 101 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 102 | status = test_transparent_signature_sign_hash( attributes, |
| 103 | key_buffer, |
| 104 | key_buffer_size, |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 105 | alg, |
| 106 | hash, |
| 107 | hash_length, |
| 108 | signature, |
| 109 | signature_size, |
| 110 | signature_length ); |
| 111 | /* Declared with fallback == true */ |
| 112 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 113 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 114 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 115 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 116 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 117 | return( psa_sign_hash_internal( attributes, |
| 118 | key_buffer, |
| 119 | key_buffer_size, |
| 120 | alg, |
| 121 | hash, |
| 122 | hash_length, |
| 123 | signature, |
| 124 | signature_size, |
| 125 | signature_length ) ); |
| 126 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 127 | /* Add cases for opaque driver here */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 128 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 129 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 130 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 131 | return( test_opaque_signature_sign_hash( attributes, |
| 132 | key_buffer, |
| 133 | key_buffer_size, |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 134 | alg, |
| 135 | hash, |
| 136 | hash_length, |
| 137 | signature, |
| 138 | signature_size, |
| 139 | signature_length ) ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 140 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 141 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 142 | default: |
| 143 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 144 | (void)status; |
| 145 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 146 | } |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 149 | psa_status_t psa_driver_wrapper_verify_hash( |
| 150 | const psa_key_attributes_t *attributes, |
| 151 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 152 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 153 | const uint8_t *signature, size_t signature_length ) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 154 | { |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 155 | /* Try dynamically-registered SE interface first */ |
| 156 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 157 | const psa_drv_se_t *drv; |
| 158 | psa_drv_se_context_t *drv_context; |
| 159 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 160 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 161 | { |
| 162 | if( drv->asymmetric == NULL || |
| 163 | drv->asymmetric->p_verify == NULL ) |
| 164 | { |
| 165 | /* Key is defined in SE, but we have no way to exercise it */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 166 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 167 | } |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 168 | return( drv->asymmetric->p_verify( |
| 169 | drv_context, *( (psa_key_slot_number_t *)key_buffer ), |
| 170 | alg, hash, hash_length, |
| 171 | signature, signature_length ) ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 172 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 173 | #endif /* PSA_CRYPTO_SE_C */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 174 | |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 175 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 176 | psa_key_location_t location = |
| 177 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 178 | |
| 179 | switch( location ) |
| 180 | { |
| 181 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 182 | /* Key is stored in the slot in export representation, so |
| 183 | * cycle through all known transparent accelerators */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 184 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 185 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 186 | status = test_transparent_signature_verify_hash( attributes, |
| 187 | key_buffer, |
| 188 | key_buffer_size, |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 189 | alg, |
| 190 | hash, |
| 191 | hash_length, |
| 192 | signature, |
| 193 | signature_length ); |
| 194 | /* Declared with fallback == true */ |
| 195 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 196 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 197 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 198 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 199 | |
| 200 | return( psa_verify_hash_internal( attributes, |
| 201 | key_buffer, |
| 202 | key_buffer_size, |
| 203 | alg, |
| 204 | hash, |
| 205 | hash_length, |
| 206 | signature, |
| 207 | signature_length ) ); |
| 208 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 209 | /* Add cases for opaque driver here */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 210 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
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: |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 213 | return( test_opaque_signature_verify_hash( attributes, |
| 214 | key_buffer, |
| 215 | key_buffer_size, |
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 */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 222 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 223 | default: |
| 224 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 225 | (void)status; |
| 226 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 227 | } |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 230 | /** Get the key buffer size for the key material of a generated key in the |
| 231 | * case of an opaque driver without storage. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 232 | * |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 233 | * \param[in] attributes The key attributes. |
| 234 | * \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] | 235 | * |
| 236 | * \retval #PSA_SUCCESS |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 237 | * The minimum size for a buffer to contain the key material has been |
| 238 | * returned successfully. |
| 239 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 240 | * The size in bits of the key is not valid. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 241 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 242 | * The type and/or the size in bits of the key or the combination of |
| 243 | * the two is not supported. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 244 | */ |
Ronald Cron | 9df74be | 2020-12-05 19:15:23 +0100 | [diff] [blame] | 245 | psa_status_t psa_driver_wrapper_get_key_buffer_size( |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 246 | const psa_key_attributes_t *attributes, |
| 247 | size_t *key_buffer_size ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 248 | { |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 249 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 250 | psa_key_type_t key_type = attributes->core.type; |
| 251 | size_t key_bits = attributes->core.bits; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 252 | |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 253 | *key_buffer_size = 0; |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 254 | switch( location ) |
| 255 | { |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 256 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 257 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
John Durkop | ac93e3b | 2020-10-16 06:48:55 -0700 | [diff] [blame] | 258 | #ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 259 | *key_buffer_size = test_size_function( key_type, key_bits ); |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 260 | return( PSA_SUCCESS ); |
| 261 | #else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */ |
| 262 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) ) |
| 263 | { |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 264 | int public_key_overhead = |
| 265 | ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ? |
| 266 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 ); |
| 267 | *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE |
John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 268 | + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE |
| 269 | + public_key_overhead; |
| 270 | } |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 271 | else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) ) |
John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 272 | { |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 273 | *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 274 | + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE; |
| 275 | } |
John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 276 | else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) && |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 277 | !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) ) |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 278 | { |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 279 | *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 280 | + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR |
| 281 | * ( ( key_bits + 7 ) / 8 ); |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 286 | } |
| 287 | return( PSA_SUCCESS ); |
| 288 | #endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */ |
| 289 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 290 | |
| 291 | default: |
Ronald Cron | 9df74be | 2020-12-05 19:15:23 +0100 | [diff] [blame] | 292 | (void)key_type; |
| 293 | (void)key_bits; |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 294 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 295 | } |
| 296 | } |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 297 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 298 | psa_status_t psa_driver_wrapper_generate_key( |
| 299 | const psa_key_attributes_t *attributes, |
| 300 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 301 | { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 302 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 303 | psa_key_location_t location = |
| 304 | PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime); |
| 305 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 306 | /* Try dynamically-registered SE interface first */ |
| 307 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 308 | const psa_drv_se_t *drv; |
| 309 | psa_drv_se_context_t *drv_context; |
| 310 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 311 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 312 | { |
| 313 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 314 | if( drv->key_management == NULL || |
| 315 | drv->key_management->p_generate == NULL ) |
| 316 | { |
| 317 | /* 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] | 318 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 319 | } |
| 320 | return( drv->key_management->p_generate( |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 321 | drv_context, |
| 322 | *( (psa_key_slot_number_t *)key_buffer ), |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 323 | attributes, NULL, 0, &pubkey_length ) ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 324 | } |
| 325 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 326 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 327 | switch( location ) |
| 328 | { |
| 329 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 330 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 331 | /* Transparent drivers are limited to generating asymmetric keys */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 332 | if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 333 | { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 334 | /* Cycle through all known transparent accelerators */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 335 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 336 | status = test_transparent_generate_key( |
| 337 | attributes, key_buffer, key_buffer_size, |
| 338 | key_buffer_length ); |
| 339 | /* Declared with fallback == true */ |
| 340 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 341 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 342 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 343 | } |
| 344 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 345 | |
| 346 | /* Software fallback */ |
| 347 | status = psa_generate_key_internal( |
| 348 | attributes, key_buffer, key_buffer_size, key_buffer_length ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 349 | break; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 350 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 351 | /* Add cases for opaque driver here */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 352 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 353 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 354 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 355 | status = test_opaque_generate_key( |
| 356 | attributes, key_buffer, key_buffer_size, key_buffer_length ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 357 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 358 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 359 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 360 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 361 | default: |
| 362 | /* Key is declared with a lifetime not known to us */ |
| 363 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 364 | break; |
| 365 | } |
| 366 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 367 | return( status ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 368 | } |
| 369 | |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 370 | psa_status_t psa_driver_wrapper_import_key( |
| 371 | const psa_key_attributes_t *attributes, |
| 372 | const uint8_t *data, |
| 373 | size_t data_length, |
| 374 | uint8_t *key_buffer, |
| 375 | size_t key_buffer_size, |
| 376 | size_t *key_buffer_length, |
| 377 | size_t *bits ) |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 378 | { |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 379 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 380 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 381 | psa_get_key_lifetime( attributes ) ); |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 382 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 383 | /* Try dynamically-registered SE interface first */ |
| 384 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 385 | const psa_drv_se_t *drv; |
| 386 | psa_drv_se_context_t *drv_context; |
| 387 | |
| 388 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 389 | { |
| 390 | if( drv->key_management == NULL || |
| 391 | drv->key_management->p_import == NULL ) |
| 392 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 393 | |
| 394 | /* The driver should set the number of key bits, however in |
| 395 | * case it doesn't, we initialize bits to an invalid value. */ |
| 396 | *bits = PSA_MAX_KEY_BITS + 1; |
| 397 | status = drv->key_management->p_import( |
| 398 | drv_context, |
| 399 | *( (psa_key_slot_number_t *)key_buffer ), |
| 400 | attributes, data, data_length, bits ); |
| 401 | |
| 402 | if( status != PSA_SUCCESS ) |
| 403 | return( status ); |
| 404 | |
| 405 | if( (*bits) > PSA_MAX_KEY_BITS ) |
| 406 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 407 | |
| 408 | return( PSA_SUCCESS ); |
| 409 | } |
| 410 | #endif /* PSA_CRYPTO_SE_C */ |
| 411 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 412 | switch( location ) |
| 413 | { |
| 414 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 415 | /* Key is stored in the slot in export representation, so |
| 416 | * cycle through all known transparent accelerators */ |
| 417 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 418 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 419 | status = test_transparent_import_key( attributes, |
| 420 | data, data_length, |
| 421 | key_buffer, key_buffer_size, |
| 422 | key_buffer_length, bits ); |
| 423 | /* Declared with fallback == true */ |
| 424 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 425 | return( status ); |
| 426 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 427 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 428 | /* Fell through, meaning no accelerator supports this operation */ |
| 429 | return( psa_import_key_into_slot( attributes, |
| 430 | data, data_length, |
| 431 | key_buffer, key_buffer_size, |
| 432 | key_buffer_length, bits ) ); |
| 433 | |
| 434 | default: |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 435 | /* Importing a key with external storage in not yet supported. |
| 436 | * Return in error indicating that the lifetime is not valid. */ |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 437 | (void)status; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 438 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 439 | } |
| 440 | |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 443 | psa_status_t psa_driver_wrapper_export_key( |
| 444 | const psa_key_attributes_t *attributes, |
| 445 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 446 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 447 | |
| 448 | { |
| 449 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 450 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 451 | psa_get_key_lifetime( attributes ) ); |
| 452 | |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 453 | /* Try dynamically-registered SE interface first */ |
| 454 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 455 | const psa_drv_se_t *drv; |
| 456 | psa_drv_se_context_t *drv_context; |
| 457 | |
| 458 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 459 | { |
| 460 | if( ( drv->key_management == NULL ) || |
| 461 | ( drv->key_management->p_export == NULL ) ) |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 462 | { |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 463 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 464 | } |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 465 | |
| 466 | return( drv->key_management->p_export( |
| 467 | drv_context, |
| 468 | *( (psa_key_slot_number_t *)key_buffer ), |
| 469 | data, data_size, data_length ) ); |
| 470 | } |
| 471 | #endif /* PSA_CRYPTO_SE_C */ |
| 472 | |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 473 | switch( location ) |
| 474 | { |
| 475 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 476 | return( psa_export_key_internal( attributes, |
| 477 | key_buffer, |
| 478 | key_buffer_size, |
| 479 | data, |
| 480 | data_size, |
| 481 | data_length ) ); |
| 482 | |
| 483 | /* Add cases for opaque driver here */ |
| 484 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 485 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 486 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 487 | return( test_opaque_export_key( attributes, |
| 488 | key_buffer, |
| 489 | key_buffer_size, |
| 490 | data, |
| 491 | data_size, |
| 492 | data_length ) ); |
| 493 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 494 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 495 | default: |
| 496 | /* Key is declared with a lifetime not known to us */ |
| 497 | return( status ); |
| 498 | } |
| 499 | } |
| 500 | |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 501 | psa_status_t psa_driver_wrapper_export_public_key( |
| 502 | const psa_key_attributes_t *attributes, |
| 503 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 504 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 505 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 506 | { |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 507 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 508 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 509 | psa_get_key_lifetime( attributes ) ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 510 | |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 511 | /* Try dynamically-registered SE interface first */ |
| 512 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 513 | const psa_drv_se_t *drv; |
| 514 | psa_drv_se_context_t *drv_context; |
| 515 | |
| 516 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 517 | { |
| 518 | if( ( drv->key_management == NULL ) || |
| 519 | ( drv->key_management->p_export_public == NULL ) ) |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 520 | { |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 521 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 522 | } |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 523 | |
| 524 | return( drv->key_management->p_export_public( |
| 525 | drv_context, |
| 526 | *( (psa_key_slot_number_t *)key_buffer ), |
| 527 | data, data_size, data_length ) ); |
| 528 | } |
| 529 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 530 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 531 | switch( location ) |
| 532 | { |
| 533 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 534 | /* Key is stored in the slot in export representation, so |
| 535 | * cycle through all known transparent accelerators */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 536 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 537 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 538 | status = test_transparent_export_public_key( attributes, |
| 539 | key_buffer, |
| 540 | key_buffer_size, |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 541 | data, |
| 542 | data_size, |
| 543 | data_length ); |
| 544 | /* Declared with fallback == true */ |
| 545 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 546 | return( status ); |
| 547 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 548 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 549 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 550 | return( psa_export_public_key_internal( attributes, |
| 551 | key_buffer, |
| 552 | key_buffer_size, |
| 553 | data, |
| 554 | data_size, |
| 555 | data_length ) ); |
| 556 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 557 | /* Add cases for opaque driver here */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 558 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 559 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 560 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 561 | return( test_opaque_export_public_key( attributes, |
| 562 | key_buffer, |
| 563 | key_buffer_size, |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 564 | data, |
| 565 | data_size, |
| 566 | data_length ) ); |
| 567 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 568 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 569 | default: |
| 570 | /* Key is declared with a lifetime not known to us */ |
| 571 | return( status ); |
| 572 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 573 | } |
| 574 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 575 | /* |
| 576 | * Cipher functions |
| 577 | */ |
| 578 | psa_status_t psa_driver_wrapper_cipher_encrypt( |
| 579 | psa_key_slot_t *slot, |
| 580 | psa_algorithm_t alg, |
| 581 | const uint8_t *input, |
| 582 | size_t input_length, |
| 583 | uint8_t *output, |
| 584 | size_t output_size, |
| 585 | size_t *output_length ) |
| 586 | { |
| 587 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 588 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 589 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 590 | psa_key_attributes_t attributes = { |
| 591 | .core = slot->attr |
| 592 | }; |
| 593 | |
| 594 | switch( location ) |
| 595 | { |
| 596 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 597 | /* Key is stored in the slot in export representation, so |
| 598 | * cycle through all known transparent accelerators */ |
| 599 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 600 | status = test_transparent_cipher_encrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 601 | slot->key.data, |
| 602 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 603 | alg, |
| 604 | input, |
| 605 | input_length, |
| 606 | output, |
| 607 | output_size, |
| 608 | output_length ); |
| 609 | /* Declared with fallback == true */ |
| 610 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 611 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 612 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 613 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 614 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 615 | /* Add cases for opaque driver here */ |
| 616 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 617 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 618 | return( test_opaque_cipher_encrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 619 | slot->key.data, |
| 620 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 621 | alg, |
| 622 | input, |
| 623 | input_length, |
| 624 | output, |
| 625 | output_size, |
| 626 | output_length ) ); |
| 627 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 628 | default: |
| 629 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 630 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 631 | } |
| 632 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 633 | (void) slot; |
| 634 | (void) alg; |
| 635 | (void) input; |
| 636 | (void) input_length; |
| 637 | (void) output; |
| 638 | (void) output_size; |
| 639 | (void) output_length; |
| 640 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 641 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 642 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 643 | } |
| 644 | |
| 645 | psa_status_t psa_driver_wrapper_cipher_decrypt( |
| 646 | psa_key_slot_t *slot, |
| 647 | psa_algorithm_t alg, |
| 648 | const uint8_t *input, |
| 649 | size_t input_length, |
| 650 | uint8_t *output, |
| 651 | size_t output_size, |
| 652 | size_t *output_length ) |
| 653 | { |
| 654 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 655 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 656 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 657 | psa_key_attributes_t attributes = { |
| 658 | .core = slot->attr |
| 659 | }; |
| 660 | |
| 661 | switch( location ) |
| 662 | { |
| 663 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 664 | /* Key is stored in the slot in export representation, so |
| 665 | * cycle through all known transparent accelerators */ |
| 666 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 667 | status = test_transparent_cipher_decrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 668 | slot->key.data, |
| 669 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 670 | alg, |
| 671 | input, |
| 672 | input_length, |
| 673 | output, |
| 674 | output_size, |
| 675 | output_length ); |
| 676 | /* Declared with fallback == true */ |
| 677 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 678 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 679 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 680 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 681 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 682 | /* Add cases for opaque driver here */ |
| 683 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 684 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 685 | return( test_opaque_cipher_decrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 686 | slot->key.data, |
| 687 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 688 | alg, |
| 689 | input, |
| 690 | input_length, |
| 691 | output, |
| 692 | output_size, |
| 693 | output_length ) ); |
| 694 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 695 | default: |
| 696 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 697 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 698 | } |
| 699 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 700 | (void) slot; |
| 701 | (void) alg; |
| 702 | (void) input; |
| 703 | (void) input_length; |
| 704 | (void) output; |
| 705 | (void) output_size; |
| 706 | (void) output_length; |
| 707 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 708 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 709 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 710 | } |
| 711 | |
| 712 | psa_status_t psa_driver_wrapper_cipher_encrypt_setup( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 713 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 714 | psa_key_slot_t *slot, |
| 715 | psa_algorithm_t alg ) |
| 716 | { |
| 717 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 718 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 719 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 720 | psa_key_attributes_t attributes = { |
| 721 | .core = slot->attr |
| 722 | }; |
| 723 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 724 | switch( location ) |
| 725 | { |
| 726 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 727 | /* Key is stored in the slot in export representation, so |
| 728 | * cycle through all known transparent accelerators */ |
| 729 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 730 | operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) ); |
| 731 | if( operation->ctx == NULL ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 732 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 733 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 734 | status = test_transparent_cipher_encrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 735 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 736 | slot->key.data, |
| 737 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 738 | alg ); |
| 739 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 740 | if( status == PSA_SUCCESS ) |
| 741 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 742 | else |
| 743 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 744 | mbedtls_platform_zeroize( |
| 745 | operation->ctx, |
| 746 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 747 | mbedtls_free( operation->ctx ); |
| 748 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 749 | } |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 750 | |
| 751 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 752 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 753 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 754 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 755 | /* Add cases for opaque driver here */ |
| 756 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 757 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 758 | operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) ); |
| 759 | if( operation->ctx == NULL ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 760 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 761 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 762 | status = test_opaque_cipher_encrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 763 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 764 | slot->key.data, |
| 765 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 766 | alg ); |
| 767 | if( status == PSA_SUCCESS ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 768 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 769 | else |
| 770 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 771 | mbedtls_platform_zeroize( |
| 772 | operation->ctx, |
| 773 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 774 | mbedtls_free( operation->ctx ); |
| 775 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 776 | } |
| 777 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 778 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 779 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 780 | default: |
| 781 | /* Key is declared with a lifetime not known to us */ |
John Durkop | 714e3a1 | 2020-09-29 22:07:04 -0700 | [diff] [blame] | 782 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 783 | } |
| 784 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 785 | (void)slot; |
| 786 | (void)alg; |
| 787 | (void)operation; |
| 788 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 789 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 790 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 791 | } |
| 792 | |
| 793 | psa_status_t psa_driver_wrapper_cipher_decrypt_setup( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 794 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 795 | psa_key_slot_t *slot, |
| 796 | psa_algorithm_t alg ) |
| 797 | { |
| 798 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 799 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 800 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 801 | psa_key_attributes_t attributes = { |
| 802 | .core = slot->attr |
| 803 | }; |
| 804 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 805 | switch( location ) |
| 806 | { |
| 807 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 808 | /* Key is stored in the slot in export representation, so |
| 809 | * cycle through all known transparent accelerators */ |
| 810 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 811 | operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_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_transparent_cipher_decrypt_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 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 821 | if( status == PSA_SUCCESS ) |
| 822 | operation->id = PSA_CRYPTO_TRANSPARENT_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_transparent_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 | } |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 831 | |
| 832 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 833 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 834 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 835 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 836 | /* Add cases for opaque driver here */ |
| 837 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 838 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 839 | operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) ); |
| 840 | if( operation->ctx == NULL ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 841 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 842 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 843 | status = test_opaque_cipher_decrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 844 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 845 | slot->key.data, |
| 846 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 847 | alg ); |
| 848 | if( status == PSA_SUCCESS ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 849 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 850 | else |
| 851 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 852 | mbedtls_platform_zeroize( |
| 853 | operation->ctx, |
| 854 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 855 | mbedtls_free( operation->ctx ); |
| 856 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 857 | } |
| 858 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 859 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 860 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 861 | default: |
| 862 | /* Key is declared with a lifetime not known to us */ |
John Durkop | 814dca7 | 2020-10-05 06:31:12 -0700 | [diff] [blame] | 863 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 864 | } |
| 865 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 866 | (void)slot; |
| 867 | (void)alg; |
| 868 | (void)operation; |
| 869 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 870 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 871 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 872 | } |
| 873 | |
| 874 | psa_status_t psa_driver_wrapper_cipher_generate_iv( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 875 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 876 | uint8_t *iv, |
| 877 | size_t iv_size, |
| 878 | size_t *iv_length ) |
| 879 | { |
| 880 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 881 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 882 | { |
| 883 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 884 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 885 | return( test_transparent_cipher_generate_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 886 | iv, |
| 887 | iv_size, |
| 888 | iv_length ) ); |
| 889 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 890 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 891 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 892 | return( test_opaque_cipher_generate_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 893 | iv, |
| 894 | iv_size, |
| 895 | iv_length ) ); |
| 896 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 897 | default: |
| 898 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 899 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 900 | } |
| 901 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 902 | (void) operation; |
| 903 | (void) iv; |
| 904 | (void) iv_size; |
| 905 | (void) iv_length; |
| 906 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 907 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 908 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 909 | } |
| 910 | |
| 911 | psa_status_t psa_driver_wrapper_cipher_set_iv( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 912 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 913 | const uint8_t *iv, |
| 914 | size_t iv_length ) |
| 915 | { |
| 916 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 917 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 918 | { |
| 919 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 920 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 921 | return( test_transparent_cipher_set_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 922 | iv, |
| 923 | iv_length ) ); |
| 924 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 925 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 926 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 927 | return( test_opaque_cipher_set_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 928 | iv, |
| 929 | iv_length ) ); |
| 930 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 931 | default: |
| 932 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 933 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 934 | } |
| 935 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 936 | (void) operation; |
| 937 | (void) iv; |
| 938 | (void) iv_length; |
| 939 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 940 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 941 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 942 | } |
| 943 | |
| 944 | psa_status_t psa_driver_wrapper_cipher_update( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 945 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 946 | const uint8_t *input, |
| 947 | size_t input_length, |
| 948 | uint8_t *output, |
| 949 | size_t output_size, |
| 950 | size_t *output_length ) |
| 951 | { |
| 952 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 953 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 954 | { |
| 955 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 956 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 957 | return( test_transparent_cipher_update( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 958 | input, |
| 959 | input_length, |
| 960 | output, |
| 961 | output_size, |
| 962 | output_length ) ); |
| 963 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 964 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 965 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 966 | return( test_opaque_cipher_update( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 967 | input, |
| 968 | input_length, |
| 969 | output, |
| 970 | output_size, |
| 971 | output_length ) ); |
| 972 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 973 | default: |
| 974 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 975 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 976 | } |
| 977 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 978 | (void) operation; |
| 979 | (void) input; |
| 980 | (void) input_length; |
| 981 | (void) output; |
| 982 | (void) output_length; |
| 983 | (void) output_size; |
| 984 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 985 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 986 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 987 | } |
| 988 | |
| 989 | psa_status_t psa_driver_wrapper_cipher_finish( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 990 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 991 | uint8_t *output, |
| 992 | size_t output_size, |
| 993 | size_t *output_length ) |
| 994 | { |
| 995 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 996 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 997 | { |
| 998 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 999 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1000 | return( test_transparent_cipher_finish( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1001 | output, |
| 1002 | output_size, |
| 1003 | output_length ) ); |
| 1004 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1005 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1006 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1007 | return( test_opaque_cipher_finish( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1008 | output, |
| 1009 | output_size, |
| 1010 | output_length ) ); |
| 1011 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1012 | default: |
| 1013 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1014 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1015 | } |
| 1016 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1017 | (void) operation; |
| 1018 | (void) output; |
| 1019 | (void) output_size; |
| 1020 | (void) output_length; |
| 1021 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1022 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1023 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1024 | } |
| 1025 | |
| 1026 | psa_status_t psa_driver_wrapper_cipher_abort( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1027 | psa_operation_driver_context_t *operation ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1028 | { |
| 1029 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1030 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1031 | |
| 1032 | /* The object has (apparently) been initialized but it is not in use. It's |
| 1033 | * ok to call abort on such an object, and there's nothing to do. */ |
| 1034 | if( operation->ctx == NULL && operation->id == 0 ) |
| 1035 | return( PSA_SUCCESS ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1036 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1037 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1038 | { |
| 1039 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1040 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1041 | status = test_transparent_cipher_abort( operation->ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1042 | mbedtls_platform_zeroize( |
| 1043 | operation->ctx, |
| 1044 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1045 | mbedtls_free( operation->ctx ); |
| 1046 | operation->ctx = NULL; |
| 1047 | operation->id = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1048 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1049 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1050 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1051 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1052 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1053 | status = test_opaque_cipher_abort( operation->ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1054 | mbedtls_platform_zeroize( |
| 1055 | operation->ctx, |
| 1056 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1057 | mbedtls_free( operation->ctx ); |
| 1058 | operation->ctx = NULL; |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1059 | operation->id = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1060 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1061 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1062 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1063 | default: |
| 1064 | /* Operation is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1065 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1066 | } |
| 1067 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1068 | (void)operation; |
| 1069 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1070 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1071 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1072 | } |
| 1073 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1074 | /* |
| 1075 | * Hashing functions |
| 1076 | */ |
| 1077 | psa_status_t psa_driver_wrapper_hash_compute( |
| 1078 | psa_algorithm_t alg, |
| 1079 | const uint8_t *input, |
| 1080 | size_t input_length, |
| 1081 | uint8_t *hash, |
| 1082 | size_t hash_size, |
| 1083 | size_t *hash_length) |
| 1084 | { |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1085 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1086 | |
| 1087 | /* Try accelerators first */ |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1088 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame] | 1089 | status = mbedtls_transparent_test_driver_hash_compute( alg, input, input_length, |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1090 | hash, hash_size, hash_length ); |
| 1091 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1092 | return( status ); |
| 1093 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1094 | |
| 1095 | /* If software fallback is compiled in, try fallback */ |
| 1096 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1097 | status = mbedtls_psa_hash_compute( alg, input, input_length, |
| 1098 | hash, hash_size, hash_length ); |
| 1099 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1100 | return( status ); |
| 1101 | #endif |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1102 | (void) status; |
| 1103 | (void) alg; |
| 1104 | (void) input; |
| 1105 | (void) input_length; |
| 1106 | (void) hash; |
| 1107 | (void) hash_size; |
| 1108 | (void) hash_length; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1109 | |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1110 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | psa_status_t psa_driver_wrapper_hash_setup( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1114 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1115 | psa_algorithm_t alg ) |
| 1116 | { |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1117 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1118 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1119 | /* Try setup on accelerators first */ |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1120 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame] | 1121 | status = mbedtls_transparent_test_driver_hash_setup( &operation->ctx.test_ctx, alg ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1122 | if( status == PSA_SUCCESS ) |
| 1123 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
| 1124 | |
| 1125 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1126 | return( status ); |
| 1127 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1128 | |
| 1129 | /* If software fallback is compiled in, try fallback */ |
| 1130 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1131 | status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1132 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1133 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1134 | |
| 1135 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1136 | return( status ); |
| 1137 | #endif |
| 1138 | /* Nothing left to try if we fall through here */ |
| 1139 | (void) status; |
| 1140 | (void) operation; |
| 1141 | (void) alg; |
| 1142 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1143 | } |
| 1144 | |
| 1145 | psa_status_t psa_driver_wrapper_hash_clone( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1146 | const psa_hash_operation_t *source_operation, |
| 1147 | psa_hash_operation_t *target_operation ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1148 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1149 | switch( source_operation->id ) |
| 1150 | { |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1151 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1152 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
| 1153 | target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame] | 1154 | return( mbedtls_transparent_test_driver_hash_clone( &source_operation->ctx.test_ctx, |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1155 | &target_operation->ctx.test_ctx ) ); |
| 1156 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1157 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1158 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1159 | target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1160 | return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx, |
| 1161 | &target_operation->ctx.mbedtls_ctx ) ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1162 | #endif |
| 1163 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1164 | (void) source_operation; |
| 1165 | (void) target_operation; |
| 1166 | return( PSA_ERROR_BAD_STATE ); |
| 1167 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | psa_status_t psa_driver_wrapper_hash_update( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1171 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1172 | const uint8_t *input, |
| 1173 | size_t input_length ) |
| 1174 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1175 | switch( operation->id ) |
| 1176 | { |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1177 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1178 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame] | 1179 | return( mbedtls_transparent_test_driver_hash_update( &operation->ctx.test_ctx, |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1180 | input, input_length ) ); |
| 1181 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1182 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1183 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1184 | return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx, |
| 1185 | input, input_length ) ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1186 | #endif |
| 1187 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1188 | (void) operation; |
| 1189 | (void) input; |
| 1190 | (void) input_length; |
| 1191 | return( PSA_ERROR_BAD_STATE ); |
| 1192 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | psa_status_t psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1196 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1197 | uint8_t *hash, |
| 1198 | size_t hash_size, |
| 1199 | size_t *hash_length ) |
| 1200 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1201 | switch( operation->id ) |
| 1202 | { |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1203 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1204 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame] | 1205 | return( mbedtls_transparent_test_driver_hash_finish( &operation->ctx.test_ctx, |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1206 | hash, hash_size, hash_length ) ); |
| 1207 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1208 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1209 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1210 | return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx, |
| 1211 | hash, hash_size, hash_length ) ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1212 | break; |
| 1213 | #endif |
| 1214 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1215 | (void) operation; |
| 1216 | (void) hash; |
| 1217 | (void) hash_size; |
| 1218 | (void) hash_length; |
| 1219 | return( PSA_ERROR_BAD_STATE ); |
| 1220 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | psa_status_t psa_driver_wrapper_hash_abort( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1224 | psa_hash_operation_t *operation ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1225 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1226 | switch( operation->id ) |
| 1227 | { |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1228 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1229 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame] | 1230 | return( mbedtls_transparent_test_driver_hash_abort( &operation->ctx.test_ctx ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1231 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1232 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1233 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1234 | return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1235 | #endif |
| 1236 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1237 | return( PSA_ERROR_BAD_STATE ); |
| 1238 | } |
| 1239 | } |
| 1240 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 1241 | /* End of automatically generated file. */ |