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