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