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