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 */ |
Steven Cooreman | aa87fd0 | 2021-03-15 18:54:03 +0100 | [diff] [blame] | 42 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 43 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 44 | /* Auto-generated values depending on which drivers are registered. |
| 45 | * ID 0 is reserved for unallocated operations. |
| 46 | * ID 1 is reserved for the Mbed TLS software driver. */ |
Steven Cooreman | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame] | 47 | #define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1) |
| 48 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 49 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 50 | #define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2) |
| 51 | #define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 52 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 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( |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 713 | psa_cipher_operation_t *operation, |
| 714 | const psa_key_attributes_t *attributes, |
| 715 | const uint8_t *key_buffer, size_t key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 716 | psa_algorithm_t alg ) |
| 717 | { |
| 718 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 719 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 720 | psa_key_location_t location = |
| 721 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 722 | void *driver_ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 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) |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 730 | driver_ctx = mbedtls_calloc( 1, |
| 731 | sizeof( test_transparent_cipher_operation_t ) ); |
| 732 | if( driver_ctx == NULL ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 733 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 734 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 735 | status = test_transparent_cipher_encrypt_setup( driver_ctx, |
| 736 | attributes, |
| 737 | key_buffer, |
| 738 | key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 739 | alg ); |
| 740 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 741 | if( status == PSA_SUCCESS ) |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 742 | { |
| 743 | operation->ctx.driver.id = |
| 744 | PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
| 745 | operation->ctx.driver.ctx = driver_ctx; |
| 746 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 747 | else |
| 748 | { |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 749 | mbedtls_platform_zeroize( driver_ctx, |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 750 | sizeof( test_transparent_cipher_operation_t ) ); |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 751 | mbedtls_free( driver_ctx ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 752 | } |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 753 | |
| 754 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 755 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 756 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 757 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 758 | /* Add cases for opaque driver here */ |
| 759 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 760 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 761 | driver_ctx = |
| 762 | mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) ); |
| 763 | if( driver_ctx == NULL ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 764 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 765 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 766 | status = test_opaque_cipher_encrypt_setup( driver_ctx, |
| 767 | attributes, |
| 768 | key_buffer, |
| 769 | key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 770 | alg ); |
| 771 | if( status == PSA_SUCCESS ) |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 772 | { |
| 773 | operation->ctx.driver.id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
| 774 | operation->ctx.driver.ctx = driver_ctx; |
| 775 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 776 | else |
| 777 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 778 | mbedtls_platform_zeroize( |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 779 | driver_ctx, sizeof( test_opaque_cipher_operation_t ) ); |
| 780 | mbedtls_free( driver_ctx ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 781 | } |
| 782 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 783 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 784 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 785 | default: |
| 786 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 787 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 788 | } |
| 789 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 790 | (void)operation; |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 791 | (void)attributes; |
| 792 | (void)key_buffer; |
| 793 | (void)key_buffer_size; |
| 794 | (void)alg; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 795 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 796 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 797 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 798 | } |
| 799 | |
| 800 | psa_status_t psa_driver_wrapper_cipher_decrypt_setup( |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 801 | psa_cipher_operation_t *operation, |
| 802 | const psa_key_attributes_t *attributes, |
| 803 | const uint8_t *key_buffer, size_t key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 804 | psa_algorithm_t alg ) |
| 805 | { |
| 806 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 807 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 808 | psa_key_location_t location = |
| 809 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 810 | void *driver_ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 811 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 812 | switch( location ) |
| 813 | { |
| 814 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 815 | /* Key is stored in the slot in export representation, so |
| 816 | * cycle through all known transparent accelerators */ |
| 817 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 818 | driver_ctx = mbedtls_calloc( 1, |
| 819 | sizeof( test_transparent_cipher_operation_t ) ); |
| 820 | if( driver_ctx == NULL ) |
| 821 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 822 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 823 | status = test_transparent_cipher_decrypt_setup( driver_ctx, |
| 824 | attributes, |
| 825 | key_buffer, |
| 826 | key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 827 | alg ); |
| 828 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 829 | if( status == PSA_SUCCESS ) |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 830 | { |
| 831 | operation->ctx.driver.id = |
| 832 | PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
| 833 | operation->ctx.driver.ctx = driver_ctx; |
| 834 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 835 | else |
| 836 | { |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 837 | mbedtls_platform_zeroize( driver_ctx, |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 838 | sizeof( test_transparent_cipher_operation_t ) ); |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 839 | mbedtls_free( driver_ctx ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 840 | } |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 841 | |
| 842 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 843 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 844 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 845 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 846 | /* Add cases for opaque driver here */ |
| 847 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 848 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 849 | driver_ctx = |
| 850 | mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) ); |
| 851 | if( driver_ctx == NULL ) |
| 852 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 853 | |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 854 | status = test_opaque_cipher_decrypt_setup( driver_ctx, |
| 855 | attributes, |
| 856 | key_buffer, |
| 857 | key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 858 | alg ); |
| 859 | if( status == PSA_SUCCESS ) |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 860 | { |
| 861 | operation->ctx.driver.id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
| 862 | operation->ctx.driver.ctx = driver_ctx; |
| 863 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 864 | else |
| 865 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 866 | mbedtls_platform_zeroize( |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 867 | driver_ctx, sizeof( test_opaque_cipher_operation_t ) ); |
| 868 | mbedtls_free( driver_ctx ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 869 | } |
| 870 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 871 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 872 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 873 | default: |
| 874 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 875 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 876 | } |
| 877 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 878 | (void)operation; |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame^] | 879 | (void)attributes; |
| 880 | (void)key_buffer; |
| 881 | (void)key_buffer_size; |
| 882 | (void)alg; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 883 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 884 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 885 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 886 | } |
| 887 | |
| 888 | psa_status_t psa_driver_wrapper_cipher_generate_iv( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 889 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 890 | uint8_t *iv, |
| 891 | size_t iv_size, |
| 892 | size_t *iv_length ) |
| 893 | { |
| 894 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 895 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 896 | { |
| 897 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 898 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 899 | return( test_transparent_cipher_generate_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 900 | iv, |
| 901 | iv_size, |
| 902 | iv_length ) ); |
| 903 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 904 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 905 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 906 | return( test_opaque_cipher_generate_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 907 | iv, |
| 908 | iv_size, |
| 909 | iv_length ) ); |
| 910 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 911 | default: |
| 912 | /* Key is attached to a driver not known to us */ |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 913 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 914 | } |
| 915 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 916 | (void) operation; |
| 917 | (void) iv; |
| 918 | (void) iv_size; |
| 919 | (void) iv_length; |
| 920 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 921 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 922 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 923 | } |
| 924 | |
| 925 | psa_status_t psa_driver_wrapper_cipher_set_iv( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 926 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 927 | const uint8_t *iv, |
| 928 | size_t iv_length ) |
| 929 | { |
| 930 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 931 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 932 | { |
| 933 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 934 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 935 | return( test_transparent_cipher_set_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 936 | iv, |
| 937 | iv_length ) ); |
| 938 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 939 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 940 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 941 | return( test_opaque_cipher_set_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 942 | iv, |
| 943 | iv_length ) ); |
| 944 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 945 | default: |
| 946 | /* Key is attached to a driver not known to us */ |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 947 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 948 | } |
| 949 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 950 | (void) operation; |
| 951 | (void) iv; |
| 952 | (void) iv_length; |
| 953 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 954 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 955 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 956 | } |
| 957 | |
| 958 | psa_status_t psa_driver_wrapper_cipher_update( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 959 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 960 | const uint8_t *input, |
| 961 | size_t input_length, |
| 962 | uint8_t *output, |
| 963 | size_t output_size, |
| 964 | size_t *output_length ) |
| 965 | { |
| 966 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 967 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 968 | { |
| 969 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 970 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 971 | return( test_transparent_cipher_update( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 972 | input, |
| 973 | input_length, |
| 974 | output, |
| 975 | output_size, |
| 976 | output_length ) ); |
| 977 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 978 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 979 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 980 | return( test_opaque_cipher_update( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 981 | input, |
| 982 | input_length, |
| 983 | output, |
| 984 | output_size, |
| 985 | output_length ) ); |
| 986 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 987 | default: |
| 988 | /* Key is attached to a driver not known to us */ |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 989 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 990 | } |
| 991 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 992 | (void) operation; |
| 993 | (void) input; |
| 994 | (void) input_length; |
| 995 | (void) output; |
| 996 | (void) output_length; |
| 997 | (void) output_size; |
| 998 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 999 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1000 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1001 | } |
| 1002 | |
| 1003 | psa_status_t psa_driver_wrapper_cipher_finish( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1004 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1005 | uint8_t *output, |
| 1006 | size_t output_size, |
| 1007 | size_t *output_length ) |
| 1008 | { |
| 1009 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1010 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1011 | { |
| 1012 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1013 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1014 | return( test_transparent_cipher_finish( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1015 | output, |
| 1016 | output_size, |
| 1017 | output_length ) ); |
| 1018 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1019 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1020 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1021 | return( test_opaque_cipher_finish( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1022 | output, |
| 1023 | output_size, |
| 1024 | output_length ) ); |
| 1025 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1026 | default: |
| 1027 | /* Key is attached to a driver not known to us */ |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 1028 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1029 | } |
| 1030 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1031 | (void) operation; |
| 1032 | (void) output; |
| 1033 | (void) output_size; |
| 1034 | (void) output_length; |
| 1035 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1036 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1037 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1038 | } |
| 1039 | |
| 1040 | psa_status_t psa_driver_wrapper_cipher_abort( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1041 | psa_operation_driver_context_t *operation ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1042 | { |
| 1043 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1044 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1045 | |
| 1046 | /* The object has (apparently) been initialized but it is not in use. It's |
| 1047 | * ok to call abort on such an object, and there's nothing to do. */ |
| 1048 | if( operation->ctx == NULL && operation->id == 0 ) |
| 1049 | return( PSA_SUCCESS ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1050 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1051 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1052 | { |
| 1053 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1054 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1055 | status = test_transparent_cipher_abort( operation->ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1056 | mbedtls_platform_zeroize( |
| 1057 | operation->ctx, |
| 1058 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1059 | mbedtls_free( operation->ctx ); |
| 1060 | operation->ctx = NULL; |
| 1061 | operation->id = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1062 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1063 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1064 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1065 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1066 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1067 | status = test_opaque_cipher_abort( operation->ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1068 | mbedtls_platform_zeroize( |
| 1069 | operation->ctx, |
| 1070 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1071 | mbedtls_free( operation->ctx ); |
| 1072 | operation->ctx = NULL; |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1073 | operation->id = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1074 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1075 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1076 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1077 | default: |
| 1078 | /* Operation is attached to a driver not known to us */ |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 1079 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1080 | } |
| 1081 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1082 | (void)operation; |
| 1083 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1084 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1085 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1086 | } |
| 1087 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1088 | /* |
| 1089 | * Hashing functions |
| 1090 | */ |
| 1091 | psa_status_t psa_driver_wrapper_hash_compute( |
| 1092 | psa_algorithm_t alg, |
| 1093 | const uint8_t *input, |
| 1094 | size_t input_length, |
| 1095 | uint8_t *hash, |
| 1096 | size_t hash_size, |
| 1097 | size_t *hash_length) |
| 1098 | { |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1099 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1100 | |
| 1101 | /* Try accelerators first */ |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1102 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1103 | status = mbedtls_transparent_test_driver_hash_compute( |
| 1104 | alg, input, input_length, hash, hash_size, hash_length ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1105 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1106 | return( status ); |
| 1107 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1108 | |
| 1109 | /* If software fallback is compiled in, try fallback */ |
| 1110 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1111 | status = mbedtls_psa_hash_compute( alg, input, input_length, |
| 1112 | hash, hash_size, hash_length ); |
| 1113 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1114 | return( status ); |
| 1115 | #endif |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1116 | (void) status; |
| 1117 | (void) alg; |
| 1118 | (void) input; |
| 1119 | (void) input_length; |
| 1120 | (void) hash; |
| 1121 | (void) hash_size; |
| 1122 | (void) hash_length; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1123 | |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1124 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | psa_status_t psa_driver_wrapper_hash_setup( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1128 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1129 | psa_algorithm_t alg ) |
| 1130 | { |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1131 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1132 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1133 | /* Try setup on accelerators first */ |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1134 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1135 | status = mbedtls_transparent_test_driver_hash_setup( |
| 1136 | &operation->ctx.test_driver_ctx, alg ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1137 | if( status == PSA_SUCCESS ) |
| 1138 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
| 1139 | |
| 1140 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1141 | return( status ); |
| 1142 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1143 | |
| 1144 | /* If software fallback is compiled in, try fallback */ |
| 1145 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1146 | status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1147 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1148 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1149 | |
| 1150 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1151 | return( status ); |
| 1152 | #endif |
| 1153 | /* Nothing left to try if we fall through here */ |
| 1154 | (void) status; |
| 1155 | (void) operation; |
| 1156 | (void) alg; |
| 1157 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1158 | } |
| 1159 | |
| 1160 | psa_status_t psa_driver_wrapper_hash_clone( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1161 | const psa_hash_operation_t *source_operation, |
| 1162 | psa_hash_operation_t *target_operation ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1163 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1164 | switch( source_operation->id ) |
| 1165 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1166 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1167 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1168 | target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 1169 | return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx, |
| 1170 | &target_operation->ctx.mbedtls_ctx ) ); |
| 1171 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1172 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1173 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
| 1174 | target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1175 | return( mbedtls_transparent_test_driver_hash_clone( |
| 1176 | &source_operation->ctx.test_driver_ctx, |
| 1177 | &target_operation->ctx.test_driver_ctx ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1178 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1179 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1180 | (void) target_operation; |
| 1181 | return( PSA_ERROR_BAD_STATE ); |
| 1182 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | psa_status_t psa_driver_wrapper_hash_update( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1186 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1187 | const uint8_t *input, |
| 1188 | size_t input_length ) |
| 1189 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1190 | switch( operation->id ) |
| 1191 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1192 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1193 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1194 | return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx, |
| 1195 | input, input_length ) ); |
| 1196 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1197 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1198 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1199 | return( mbedtls_transparent_test_driver_hash_update( |
| 1200 | &operation->ctx.test_driver_ctx, |
| 1201 | input, input_length ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1202 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1203 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1204 | (void) input; |
| 1205 | (void) input_length; |
| 1206 | return( PSA_ERROR_BAD_STATE ); |
| 1207 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | psa_status_t psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1211 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1212 | uint8_t *hash, |
| 1213 | size_t hash_size, |
| 1214 | size_t *hash_length ) |
| 1215 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1216 | switch( operation->id ) |
| 1217 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1218 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1219 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1220 | return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx, |
| 1221 | hash, hash_size, hash_length ) ); |
| 1222 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1223 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1224 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1225 | return( mbedtls_transparent_test_driver_hash_finish( |
| 1226 | &operation->ctx.test_driver_ctx, |
| 1227 | hash, hash_size, hash_length ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1228 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1229 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1230 | (void) hash; |
| 1231 | (void) hash_size; |
| 1232 | (void) hash_length; |
| 1233 | return( PSA_ERROR_BAD_STATE ); |
| 1234 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | psa_status_t psa_driver_wrapper_hash_abort( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1238 | psa_hash_operation_t *operation ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1239 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1240 | switch( operation->id ) |
| 1241 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1242 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1243 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1244 | return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) ); |
| 1245 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1246 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1247 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1248 | return( mbedtls_transparent_test_driver_hash_abort( |
| 1249 | &operation->ctx.test_driver_ctx ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1250 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1251 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1252 | return( PSA_ERROR_BAD_STATE ); |
| 1253 | } |
| 1254 | } |
| 1255 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 1256 | /* End of automatically generated file. */ |