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" |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 27 | #include "psa_crypto_mac.h" |
Steven Cooreman | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame] | 28 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
| 30 | |
| 31 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 32 | |
| 33 | /* Include test driver definition when running tests */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 34 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 35 | #ifndef PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 36 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 37 | #endif |
| 38 | #ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 39 | #define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 40 | #endif |
Ronald Cron | 05ee58d | 2021-04-30 17:00:34 +0200 | [diff] [blame] | 41 | #include "test/drivers/test_driver.h" |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 42 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 43 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 44 | /* Repeat above block for each JSON-declared driver during autogeneration */ |
Steven Cooreman | aa87fd0 | 2021-03-15 18:54:03 +0100 | [diff] [blame] | 45 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 46 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 47 | /* Auto-generated values depending on which drivers are registered. |
| 48 | * ID 0 is reserved for unallocated operations. |
| 49 | * ID 1 is reserved for the Mbed TLS software driver. */ |
Steven Cooreman | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame] | 50 | #define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1) |
| 51 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 52 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 53 | #define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2) |
| 54 | #define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 55 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 56 | |
| 57 | /* Support the 'old' SE interface when asked to */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 58 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 59 | /* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style |
| 60 | * SE driver is present, to avoid unused argument errors at compile time. */ |
| 61 | #ifndef PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 62 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 63 | #endif |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 64 | #include "psa_crypto_se.h" |
| 65 | #endif |
| 66 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 67 | /* Start delegation functions */ |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 68 | psa_status_t psa_driver_wrapper_sign_hash( |
| 69 | const psa_key_attributes_t *attributes, |
| 70 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 71 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 72 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 73 | { |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 74 | /* Try dynamically-registered SE interface first */ |
| 75 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 76 | const psa_drv_se_t *drv; |
| 77 | psa_drv_se_context_t *drv_context; |
| 78 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 79 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 80 | { |
| 81 | if( drv->asymmetric == NULL || |
| 82 | drv->asymmetric->p_sign == NULL ) |
| 83 | { |
| 84 | /* 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] | 85 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 86 | } |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 87 | return( drv->asymmetric->p_sign( |
| 88 | drv_context, *( (psa_key_slot_number_t *)key_buffer ), |
| 89 | alg, hash, hash_length, |
| 90 | signature, signature_size, signature_length ) ); |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 91 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 92 | #endif /* PSA_CRYPTO_SE_C */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 93 | |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 94 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 95 | psa_key_location_t location = |
| 96 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 97 | |
| 98 | switch( location ) |
| 99 | { |
| 100 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 101 | /* Key is stored in the slot in export representation, so |
| 102 | * cycle through all known transparent accelerators */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 103 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 104 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 105 | status = mbedtls_test_transparent_signature_sign_hash( attributes, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 106 | key_buffer, |
| 107 | key_buffer_size, |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 108 | alg, |
| 109 | hash, |
| 110 | hash_length, |
| 111 | signature, |
| 112 | signature_size, |
| 113 | signature_length ); |
| 114 | /* Declared with fallback == true */ |
| 115 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 116 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 117 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 118 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 119 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 120 | return( psa_sign_hash_internal( attributes, |
| 121 | key_buffer, |
| 122 | key_buffer_size, |
| 123 | alg, |
| 124 | hash, |
| 125 | hash_length, |
| 126 | signature, |
| 127 | signature_size, |
| 128 | signature_length ) ); |
| 129 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 130 | /* Add cases for opaque driver here */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 131 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 132 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 133 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 134 | return( mbedtls_test_opaque_signature_sign_hash( attributes, |
| 135 | key_buffer, |
| 136 | key_buffer_size, |
| 137 | alg, |
| 138 | hash, |
| 139 | hash_length, |
| 140 | signature, |
| 141 | signature_size, |
| 142 | signature_length ) ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 143 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 144 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 145 | default: |
| 146 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 147 | (void)status; |
| 148 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 149 | } |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 152 | psa_status_t psa_driver_wrapper_verify_hash( |
| 153 | const psa_key_attributes_t *attributes, |
| 154 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 155 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 156 | const uint8_t *signature, size_t signature_length ) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 157 | { |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 158 | /* Try dynamically-registered SE interface first */ |
| 159 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 160 | const psa_drv_se_t *drv; |
| 161 | psa_drv_se_context_t *drv_context; |
| 162 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 163 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 164 | { |
| 165 | if( drv->asymmetric == NULL || |
| 166 | drv->asymmetric->p_verify == NULL ) |
| 167 | { |
| 168 | /* 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] | 169 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 170 | } |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 171 | return( drv->asymmetric->p_verify( |
| 172 | drv_context, *( (psa_key_slot_number_t *)key_buffer ), |
| 173 | alg, hash, hash_length, |
| 174 | signature, signature_length ) ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 175 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 176 | #endif /* PSA_CRYPTO_SE_C */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 177 | |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 178 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 179 | psa_key_location_t location = |
| 180 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 181 | |
| 182 | switch( location ) |
| 183 | { |
| 184 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 185 | /* Key is stored in the slot in export representation, so |
| 186 | * cycle through all known transparent accelerators */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 187 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 188 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 189 | status = mbedtls_test_transparent_signature_verify_hash( |
| 190 | attributes, |
| 191 | key_buffer, |
| 192 | key_buffer_size, |
| 193 | alg, |
| 194 | hash, |
| 195 | hash_length, |
| 196 | signature, |
| 197 | signature_length ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 198 | /* Declared with fallback == true */ |
| 199 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 200 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 201 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 202 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 203 | |
| 204 | return( psa_verify_hash_internal( attributes, |
| 205 | key_buffer, |
| 206 | key_buffer_size, |
| 207 | alg, |
| 208 | hash, |
| 209 | hash_length, |
| 210 | signature, |
| 211 | signature_length ) ); |
| 212 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 213 | /* Add cases for opaque driver here */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 214 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 215 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 216 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 217 | return( mbedtls_test_opaque_signature_verify_hash( attributes, |
| 218 | key_buffer, |
| 219 | key_buffer_size, |
| 220 | alg, |
| 221 | hash, |
| 222 | hash_length, |
| 223 | signature, |
| 224 | signature_length ) ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 225 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 226 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 227 | default: |
| 228 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 229 | (void)status; |
| 230 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 231 | } |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Steven Cooreman | b938b0b | 2021-04-06 13:08:42 +0200 | [diff] [blame] | 234 | /** Get the key buffer size required to store the key material of a key |
| 235 | * associated with an opaque driver without storage. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 236 | * |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 237 | * \param[in] attributes The key attributes. |
| 238 | * \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] | 239 | * |
| 240 | * \retval #PSA_SUCCESS |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 241 | * The minimum size for a buffer to contain the key material has been |
| 242 | * returned successfully. |
| 243 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 244 | * The size in bits of the key is not valid. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 245 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 246 | * The type and/or the size in bits of the key or the combination of |
| 247 | * the two is not supported. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 248 | */ |
Ronald Cron | 9df74be | 2020-12-05 19:15:23 +0100 | [diff] [blame] | 249 | psa_status_t psa_driver_wrapper_get_key_buffer_size( |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 250 | const psa_key_attributes_t *attributes, |
| 251 | size_t *key_buffer_size ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 252 | { |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 253 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 254 | psa_key_type_t key_type = attributes->core.type; |
| 255 | size_t key_bits = attributes->core.bits; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 256 | |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 257 | *key_buffer_size = 0; |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 258 | switch( location ) |
| 259 | { |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 260 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 261 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 262 | #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) |
| 263 | /* Emulate property 'builtin_key_size' */ |
| 264 | if( psa_key_id_is_builtin( |
| 265 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( |
| 266 | psa_get_key_id( attributes ) ) ) ) |
| 267 | { |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 268 | *key_buffer_size = sizeof( psa_drv_slot_number_t ); |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 269 | return( PSA_SUCCESS ); |
| 270 | } |
| 271 | #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 272 | *key_buffer_size = mbedtls_test_size_function( key_type, key_bits ); |
Ronald Cron | b0737da | 2021-04-14 10:55:34 +0200 | [diff] [blame] | 273 | return( ( *key_buffer_size != 0 ) ? |
| 274 | PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 275 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 276 | |
| 277 | default: |
Ronald Cron | 9df74be | 2020-12-05 19:15:23 +0100 | [diff] [blame] | 278 | (void)key_type; |
| 279 | (void)key_bits; |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 280 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 281 | } |
| 282 | } |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 283 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 284 | psa_status_t psa_driver_wrapper_generate_key( |
| 285 | const psa_key_attributes_t *attributes, |
| 286 | 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] | 287 | { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 288 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 289 | psa_key_location_t location = |
| 290 | PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime); |
| 291 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 292 | /* Try dynamically-registered SE interface first */ |
| 293 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 294 | const psa_drv_se_t *drv; |
| 295 | psa_drv_se_context_t *drv_context; |
| 296 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 297 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 298 | { |
| 299 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 300 | if( drv->key_management == NULL || |
| 301 | drv->key_management->p_generate == NULL ) |
| 302 | { |
| 303 | /* 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] | 304 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 305 | } |
| 306 | return( drv->key_management->p_generate( |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 307 | drv_context, |
| 308 | *( (psa_key_slot_number_t *)key_buffer ), |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 309 | attributes, NULL, 0, &pubkey_length ) ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 310 | } |
| 311 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 312 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 313 | switch( location ) |
| 314 | { |
| 315 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 316 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 317 | /* Transparent drivers are limited to generating asymmetric keys */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 318 | if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 319 | { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 320 | /* Cycle through all known transparent accelerators */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 321 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 322 | status = mbedtls_test_transparent_generate_key( |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 323 | attributes, key_buffer, key_buffer_size, |
| 324 | key_buffer_length ); |
| 325 | /* Declared with fallback == true */ |
| 326 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 327 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 328 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 329 | } |
| 330 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 331 | |
| 332 | /* Software fallback */ |
| 333 | status = psa_generate_key_internal( |
| 334 | attributes, key_buffer, key_buffer_size, key_buffer_length ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 335 | break; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 336 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 337 | /* Add cases for opaque driver here */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 338 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 339 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 340 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 341 | status = mbedtls_test_opaque_generate_key( |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 342 | attributes, key_buffer, key_buffer_size, key_buffer_length ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 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 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 346 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 347 | default: |
| 348 | /* Key is declared with a lifetime not known to us */ |
| 349 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 350 | break; |
| 351 | } |
| 352 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 353 | return( status ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 356 | psa_status_t psa_driver_wrapper_import_key( |
| 357 | const psa_key_attributes_t *attributes, |
| 358 | const uint8_t *data, |
| 359 | size_t data_length, |
| 360 | uint8_t *key_buffer, |
| 361 | size_t key_buffer_size, |
| 362 | size_t *key_buffer_length, |
| 363 | size_t *bits ) |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 364 | { |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 365 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 366 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 367 | psa_get_key_lifetime( attributes ) ); |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 368 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 369 | /* Try dynamically-registered SE interface first */ |
| 370 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 371 | const psa_drv_se_t *drv; |
| 372 | psa_drv_se_context_t *drv_context; |
| 373 | |
| 374 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 375 | { |
| 376 | if( drv->key_management == NULL || |
| 377 | drv->key_management->p_import == NULL ) |
| 378 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 379 | |
| 380 | /* The driver should set the number of key bits, however in |
| 381 | * case it doesn't, we initialize bits to an invalid value. */ |
| 382 | *bits = PSA_MAX_KEY_BITS + 1; |
| 383 | status = drv->key_management->p_import( |
| 384 | drv_context, |
| 385 | *( (psa_key_slot_number_t *)key_buffer ), |
| 386 | attributes, data, data_length, bits ); |
| 387 | |
| 388 | if( status != PSA_SUCCESS ) |
| 389 | return( status ); |
| 390 | |
| 391 | if( (*bits) > PSA_MAX_KEY_BITS ) |
| 392 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 393 | |
| 394 | return( PSA_SUCCESS ); |
| 395 | } |
| 396 | #endif /* PSA_CRYPTO_SE_C */ |
| 397 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 398 | switch( location ) |
| 399 | { |
| 400 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 401 | /* Key is stored in the slot in export representation, so |
| 402 | * cycle through all known transparent accelerators */ |
| 403 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 404 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 405 | status = mbedtls_test_transparent_import_key( |
| 406 | attributes, |
| 407 | data, data_length, |
| 408 | key_buffer, key_buffer_size, |
| 409 | key_buffer_length, bits ); |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 410 | /* Declared with fallback == true */ |
| 411 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 412 | return( status ); |
| 413 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 414 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 415 | /* Fell through, meaning no accelerator supports this operation */ |
| 416 | return( psa_import_key_into_slot( attributes, |
| 417 | data, data_length, |
| 418 | key_buffer, key_buffer_size, |
| 419 | key_buffer_length, bits ) ); |
| 420 | |
| 421 | default: |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 422 | /* Importing a key with external storage in not yet supported. |
| 423 | * Return in error indicating that the lifetime is not valid. */ |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 424 | (void)status; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 425 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 426 | } |
| 427 | |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 430 | psa_status_t psa_driver_wrapper_export_key( |
| 431 | const psa_key_attributes_t *attributes, |
| 432 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 433 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 434 | |
| 435 | { |
| 436 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 437 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 438 | psa_get_key_lifetime( attributes ) ); |
| 439 | |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 440 | /* Try dynamically-registered SE interface first */ |
| 441 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 442 | const psa_drv_se_t *drv; |
| 443 | psa_drv_se_context_t *drv_context; |
| 444 | |
| 445 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 446 | { |
| 447 | if( ( drv->key_management == NULL ) || |
| 448 | ( drv->key_management->p_export == NULL ) ) |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 449 | { |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 450 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 451 | } |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 452 | |
| 453 | return( drv->key_management->p_export( |
| 454 | drv_context, |
| 455 | *( (psa_key_slot_number_t *)key_buffer ), |
| 456 | data, data_size, data_length ) ); |
| 457 | } |
| 458 | #endif /* PSA_CRYPTO_SE_C */ |
| 459 | |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 460 | switch( location ) |
| 461 | { |
| 462 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 463 | return( psa_export_key_internal( attributes, |
| 464 | key_buffer, |
| 465 | key_buffer_size, |
| 466 | data, |
| 467 | data_size, |
| 468 | data_length ) ); |
| 469 | |
| 470 | /* Add cases for opaque driver here */ |
| 471 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 472 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 473 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 474 | return( mbedtls_test_opaque_export_key( attributes, |
| 475 | key_buffer, |
| 476 | key_buffer_size, |
| 477 | data, |
| 478 | data_size, |
| 479 | data_length ) ); |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 480 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 481 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 482 | default: |
| 483 | /* Key is declared with a lifetime not known to us */ |
| 484 | return( status ); |
| 485 | } |
| 486 | } |
| 487 | |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 488 | psa_status_t psa_driver_wrapper_export_public_key( |
| 489 | const psa_key_attributes_t *attributes, |
| 490 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 491 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 492 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 493 | { |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 494 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 495 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 496 | psa_get_key_lifetime( attributes ) ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 497 | |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 498 | /* Try dynamically-registered SE interface first */ |
| 499 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 500 | const psa_drv_se_t *drv; |
| 501 | psa_drv_se_context_t *drv_context; |
| 502 | |
| 503 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 504 | { |
| 505 | if( ( drv->key_management == NULL ) || |
| 506 | ( drv->key_management->p_export_public == NULL ) ) |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 507 | { |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 508 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 509 | } |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 510 | |
| 511 | return( drv->key_management->p_export_public( |
| 512 | drv_context, |
| 513 | *( (psa_key_slot_number_t *)key_buffer ), |
| 514 | data, data_size, data_length ) ); |
| 515 | } |
| 516 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 517 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 518 | switch( location ) |
| 519 | { |
| 520 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 521 | /* Key is stored in the slot in export representation, so |
| 522 | * cycle through all known transparent accelerators */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 523 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 524 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 525 | status = mbedtls_test_transparent_export_public_key( |
| 526 | attributes, |
| 527 | key_buffer, |
| 528 | key_buffer_size, |
| 529 | data, |
| 530 | data_size, |
| 531 | data_length ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 532 | /* Declared with fallback == true */ |
| 533 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 534 | return( status ); |
| 535 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 536 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 537 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 538 | return( psa_export_public_key_internal( attributes, |
| 539 | key_buffer, |
| 540 | key_buffer_size, |
| 541 | data, |
| 542 | data_size, |
| 543 | data_length ) ); |
| 544 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 545 | /* Add cases for opaque driver here */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 546 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 547 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 548 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 549 | return( mbedtls_test_opaque_export_public_key( attributes, |
| 550 | key_buffer, |
| 551 | key_buffer_size, |
| 552 | data, |
| 553 | data_size, |
| 554 | data_length ) ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 555 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 556 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 557 | default: |
| 558 | /* Key is declared with a lifetime not known to us */ |
| 559 | return( status ); |
| 560 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 561 | } |
| 562 | |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 563 | psa_status_t psa_driver_wrapper_get_builtin_key( |
| 564 | psa_drv_slot_number_t slot_number, |
| 565 | psa_key_attributes_t *attributes, |
| 566 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
| 567 | { |
| 568 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 569 | switch( location ) |
| 570 | { |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 571 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 572 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 573 | return( mbedtls_test_opaque_get_builtin_key( |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 574 | slot_number, |
| 575 | attributes, |
| 576 | key_buffer, key_buffer_size, key_buffer_length ) ); |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 577 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 578 | default: |
| 579 | (void) slot_number; |
| 580 | (void) key_buffer; |
| 581 | (void) key_buffer_size; |
| 582 | (void) key_buffer_length; |
| 583 | return( PSA_ERROR_DOES_NOT_EXIST ); |
| 584 | } |
| 585 | } |
| 586 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 587 | /* |
| 588 | * Cipher functions |
| 589 | */ |
| 590 | psa_status_t psa_driver_wrapper_cipher_encrypt( |
| 591 | psa_key_slot_t *slot, |
| 592 | psa_algorithm_t alg, |
| 593 | const uint8_t *input, |
| 594 | size_t input_length, |
| 595 | uint8_t *output, |
| 596 | size_t output_size, |
| 597 | size_t *output_length ) |
| 598 | { |
| 599 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 600 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 601 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 602 | psa_key_attributes_t attributes = { |
| 603 | .core = slot->attr |
| 604 | }; |
| 605 | |
| 606 | switch( location ) |
| 607 | { |
| 608 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 609 | /* Key is stored in the slot in export representation, so |
| 610 | * cycle through all known transparent accelerators */ |
| 611 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 612 | status = mbedtls_test_transparent_cipher_encrypt( &attributes, |
| 613 | slot->key.data, |
| 614 | slot->key.bytes, |
| 615 | alg, |
| 616 | input, |
| 617 | input_length, |
| 618 | output, |
| 619 | output_size, |
| 620 | output_length ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 621 | /* Declared with fallback == true */ |
| 622 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 623 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 624 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 625 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 626 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 627 | /* Add cases for opaque driver here */ |
| 628 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 629 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 630 | return( mbedtls_test_opaque_cipher_encrypt( &attributes, |
| 631 | slot->key.data, |
| 632 | slot->key.bytes, |
| 633 | alg, |
| 634 | input, |
| 635 | input_length, |
| 636 | output, |
| 637 | output_size, |
| 638 | output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 639 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 640 | default: |
| 641 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 642 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 643 | } |
| 644 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 645 | (void) slot; |
| 646 | (void) alg; |
| 647 | (void) input; |
| 648 | (void) input_length; |
| 649 | (void) output; |
| 650 | (void) output_size; |
| 651 | (void) output_length; |
| 652 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 653 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 654 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 655 | } |
| 656 | |
| 657 | psa_status_t psa_driver_wrapper_cipher_decrypt( |
| 658 | psa_key_slot_t *slot, |
| 659 | psa_algorithm_t alg, |
| 660 | const uint8_t *input, |
| 661 | size_t input_length, |
| 662 | uint8_t *output, |
| 663 | size_t output_size, |
| 664 | size_t *output_length ) |
| 665 | { |
| 666 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 667 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 668 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 669 | psa_key_attributes_t attributes = { |
| 670 | .core = slot->attr |
| 671 | }; |
| 672 | |
| 673 | switch( location ) |
| 674 | { |
| 675 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 676 | /* Key is stored in the slot in export representation, so |
| 677 | * cycle through all known transparent accelerators */ |
| 678 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 679 | status = mbedtls_test_transparent_cipher_decrypt( &attributes, |
| 680 | slot->key.data, |
| 681 | slot->key.bytes, |
| 682 | alg, |
| 683 | input, |
| 684 | input_length, |
| 685 | output, |
| 686 | output_size, |
| 687 | output_length ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 688 | /* Declared with fallback == true */ |
| 689 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 690 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 691 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 692 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 693 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 694 | /* Add cases for opaque driver here */ |
| 695 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 696 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 697 | return( mbedtls_test_opaque_cipher_decrypt( &attributes, |
| 698 | slot->key.data, |
| 699 | slot->key.bytes, |
| 700 | alg, |
| 701 | input, |
| 702 | input_length, |
| 703 | output, |
| 704 | output_size, |
| 705 | output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 706 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 707 | default: |
| 708 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 709 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 710 | } |
| 711 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 712 | (void) slot; |
| 713 | (void) alg; |
| 714 | (void) input; |
| 715 | (void) input_length; |
| 716 | (void) output; |
| 717 | (void) output_size; |
| 718 | (void) output_length; |
| 719 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 720 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 721 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 722 | } |
| 723 | |
| 724 | psa_status_t psa_driver_wrapper_cipher_encrypt_setup( |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 725 | psa_cipher_operation_t *operation, |
| 726 | const psa_key_attributes_t *attributes, |
| 727 | const uint8_t *key_buffer, size_t key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 728 | psa_algorithm_t alg ) |
| 729 | { |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 730 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 731 | psa_key_location_t location = |
| 732 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 733 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 734 | switch( location ) |
| 735 | { |
| 736 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 737 | /* Key is stored in the slot in export representation, so |
| 738 | * cycle through all known transparent accelerators */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 739 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 740 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 741 | status = mbedtls_test_transparent_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 742 | &operation->ctx.transparent_test_driver_ctx, |
| 743 | attributes, |
| 744 | key_buffer, |
| 745 | key_buffer_size, |
| 746 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 747 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 748 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 749 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 750 | |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 751 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 752 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 753 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 754 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 755 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 756 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 757 | status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 758 | attributes, |
| 759 | key_buffer, |
| 760 | key_buffer_size, |
| 761 | alg ); |
| 762 | if( status == PSA_SUCCESS ) |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 763 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 764 | |
Ronald Cron | 7b4154d | 2021-03-19 14:49:41 +0100 | [diff] [blame] | 765 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 766 | return( status ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 767 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
| 768 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 769 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 770 | /* Add cases for opaque driver here */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 771 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 772 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 773 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 774 | status = mbedtls_test_opaque_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 775 | &operation->ctx.opaque_test_driver_ctx, |
| 776 | attributes, |
| 777 | key_buffer, key_buffer_size, |
| 778 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 779 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 780 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 781 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 782 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 783 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 784 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 785 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 786 | default: |
| 787 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 788 | (void)status; |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 789 | (void)key_buffer; |
| 790 | (void)key_buffer_size; |
| 791 | (void)alg; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 792 | return( PSA_ERROR_INVALID_ARGUMENT ); |
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 | } |
| 795 | |
| 796 | psa_status_t psa_driver_wrapper_cipher_decrypt_setup( |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 797 | psa_cipher_operation_t *operation, |
| 798 | const psa_key_attributes_t *attributes, |
| 799 | const uint8_t *key_buffer, size_t key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 800 | psa_algorithm_t alg ) |
| 801 | { |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 802 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 803 | psa_key_location_t location = |
| 804 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 805 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 806 | switch( location ) |
| 807 | { |
| 808 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 809 | /* Key is stored in the slot in export representation, so |
| 810 | * cycle through all known transparent accelerators */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 811 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 812 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 813 | status = mbedtls_test_transparent_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 814 | &operation->ctx.transparent_test_driver_ctx, |
| 815 | attributes, |
| 816 | key_buffer, |
| 817 | key_buffer_size, |
| 818 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 819 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 820 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 821 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 822 | |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 823 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 824 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 825 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 826 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 827 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 828 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 829 | status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 830 | attributes, |
| 831 | key_buffer, |
| 832 | key_buffer_size, |
| 833 | alg ); |
| 834 | if( status == PSA_SUCCESS ) |
| 835 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 836 | |
| 837 | return( status ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 838 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
| 839 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 840 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 841 | /* Add cases for opaque driver here */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 842 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 843 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 844 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 845 | status = mbedtls_test_opaque_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 846 | &operation->ctx.opaque_test_driver_ctx, |
| 847 | attributes, |
| 848 | key_buffer, key_buffer_size, |
| 849 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 850 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 851 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 852 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 853 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 854 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 855 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 856 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 857 | default: |
| 858 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 859 | (void)status; |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 860 | (void)key_buffer; |
| 861 | (void)key_buffer_size; |
| 862 | (void)alg; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 863 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 864 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 865 | } |
| 866 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 867 | psa_status_t psa_driver_wrapper_cipher_set_iv( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 868 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 869 | const uint8_t *iv, |
| 870 | size_t iv_length ) |
| 871 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 872 | switch( operation->id ) |
| 873 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 874 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 875 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 876 | return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 877 | iv, |
| 878 | iv_length ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 879 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 880 | |
| 881 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 882 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 883 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 884 | return( mbedtls_test_transparent_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 885 | &operation->ctx.transparent_test_driver_ctx, |
| 886 | iv, iv_length ) ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 887 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 888 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 889 | return( mbedtls_test_opaque_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 890 | &operation->ctx.opaque_test_driver_ctx, |
| 891 | iv, iv_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 892 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 893 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
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 | |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 896 | (void)iv; |
| 897 | (void)iv_length; |
| 898 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 899 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | psa_status_t psa_driver_wrapper_cipher_update( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 903 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 904 | const uint8_t *input, |
| 905 | size_t input_length, |
| 906 | uint8_t *output, |
| 907 | size_t output_size, |
| 908 | size_t *output_length ) |
| 909 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 910 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 911 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 912 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 913 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 914 | return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 915 | input, |
| 916 | input_length, |
| 917 | output, |
| 918 | output_size, |
| 919 | output_length ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 920 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
| 921 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 922 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 923 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 924 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 925 | return( mbedtls_test_transparent_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 926 | &operation->ctx.transparent_test_driver_ctx, |
| 927 | input, input_length, |
| 928 | output, output_size, output_length ) ); |
| 929 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 930 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 931 | return( mbedtls_test_opaque_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 932 | &operation->ctx.opaque_test_driver_ctx, |
| 933 | input, input_length, |
| 934 | output, output_size, output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 935 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 936 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 937 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 938 | |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 939 | (void)input; |
| 940 | (void)input_length; |
| 941 | (void)output; |
| 942 | (void)output_size; |
| 943 | (void)output_length; |
| 944 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 945 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | psa_status_t psa_driver_wrapper_cipher_finish( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 949 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 950 | uint8_t *output, |
| 951 | size_t output_size, |
| 952 | size_t *output_length ) |
| 953 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 954 | switch( operation->id ) |
| 955 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 956 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 957 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 958 | return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 959 | output, |
| 960 | output_size, |
| 961 | output_length ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 962 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 963 | |
| 964 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 965 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 966 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 967 | return( mbedtls_test_transparent_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 968 | &operation->ctx.transparent_test_driver_ctx, |
| 969 | output, output_size, output_length ) ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 970 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 971 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 972 | return( mbedtls_test_opaque_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 973 | &operation->ctx.opaque_test_driver_ctx, |
| 974 | output, output_size, output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 975 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 976 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
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 | |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 979 | (void)output; |
| 980 | (void)output_size; |
| 981 | (void)output_length; |
| 982 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 983 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | psa_status_t psa_driver_wrapper_cipher_abort( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 987 | psa_cipher_operation_t *operation ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 988 | { |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 989 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 990 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 991 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 992 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 993 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 994 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 995 | return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 996 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 997 | |
| 998 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 999 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1000 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1001 | status = mbedtls_test_transparent_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1002 | &operation->ctx.transparent_test_driver_ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1003 | mbedtls_platform_zeroize( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1004 | &operation->ctx.transparent_test_driver_ctx, |
| 1005 | sizeof( operation->ctx.transparent_test_driver_ctx ) ); |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1006 | return( status ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1007 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1008 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1009 | status = mbedtls_test_opaque_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1010 | &operation->ctx.opaque_test_driver_ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1011 | mbedtls_platform_zeroize( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1012 | &operation->ctx.opaque_test_driver_ctx, |
| 1013 | sizeof( operation->ctx.opaque_test_driver_ctx ) ); |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1014 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1015 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1016 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1017 | } |
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 | (void)status; |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1020 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1021 | } |
| 1022 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1023 | /* |
| 1024 | * Hashing functions |
| 1025 | */ |
| 1026 | psa_status_t psa_driver_wrapper_hash_compute( |
| 1027 | psa_algorithm_t alg, |
| 1028 | const uint8_t *input, |
| 1029 | size_t input_length, |
| 1030 | uint8_t *hash, |
| 1031 | size_t hash_size, |
| 1032 | size_t *hash_length) |
| 1033 | { |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1034 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1035 | |
| 1036 | /* Try accelerators first */ |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1037 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1038 | status = mbedtls_test_transparent_hash_compute( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1039 | alg, input, input_length, hash, hash_size, hash_length ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1040 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1041 | return( status ); |
| 1042 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1043 | |
| 1044 | /* If software fallback is compiled in, try fallback */ |
| 1045 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1046 | status = mbedtls_psa_hash_compute( alg, input, input_length, |
| 1047 | hash, hash_size, hash_length ); |
| 1048 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1049 | return( status ); |
| 1050 | #endif |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1051 | (void) status; |
| 1052 | (void) alg; |
| 1053 | (void) input; |
| 1054 | (void) input_length; |
| 1055 | (void) hash; |
| 1056 | (void) hash_size; |
| 1057 | (void) hash_length; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1058 | |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1059 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | psa_status_t psa_driver_wrapper_hash_setup( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1063 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1064 | psa_algorithm_t alg ) |
| 1065 | { |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1066 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1067 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1068 | /* Try setup on accelerators first */ |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1069 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1070 | status = mbedtls_test_transparent_hash_setup( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1071 | &operation->ctx.test_driver_ctx, alg ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1072 | if( status == PSA_SUCCESS ) |
| 1073 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
| 1074 | |
| 1075 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1076 | return( status ); |
| 1077 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1078 | |
| 1079 | /* If software fallback is compiled in, try fallback */ |
| 1080 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1081 | status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1082 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1083 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1084 | |
| 1085 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1086 | return( status ); |
| 1087 | #endif |
| 1088 | /* Nothing left to try if we fall through here */ |
| 1089 | (void) status; |
| 1090 | (void) operation; |
| 1091 | (void) alg; |
| 1092 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1093 | } |
| 1094 | |
| 1095 | psa_status_t psa_driver_wrapper_hash_clone( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1096 | const psa_hash_operation_t *source_operation, |
| 1097 | psa_hash_operation_t *target_operation ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1098 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1099 | switch( source_operation->id ) |
| 1100 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1101 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1102 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1103 | target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 1104 | return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx, |
| 1105 | &target_operation->ctx.mbedtls_ctx ) ); |
| 1106 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1107 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1108 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
| 1109 | target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1110 | return( mbedtls_test_transparent_hash_clone( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1111 | &source_operation->ctx.test_driver_ctx, |
| 1112 | &target_operation->ctx.test_driver_ctx ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1113 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1114 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1115 | (void) target_operation; |
| 1116 | return( PSA_ERROR_BAD_STATE ); |
| 1117 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | psa_status_t psa_driver_wrapper_hash_update( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1121 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1122 | const uint8_t *input, |
| 1123 | size_t input_length ) |
| 1124 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1125 | switch( operation->id ) |
| 1126 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1127 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1128 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1129 | return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx, |
| 1130 | input, input_length ) ); |
| 1131 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1132 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1133 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1134 | return( mbedtls_test_transparent_hash_update( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1135 | &operation->ctx.test_driver_ctx, |
| 1136 | input, input_length ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1137 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1138 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1139 | (void) input; |
| 1140 | (void) input_length; |
| 1141 | return( PSA_ERROR_BAD_STATE ); |
| 1142 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | psa_status_t psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1146 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1147 | uint8_t *hash, |
| 1148 | size_t hash_size, |
| 1149 | size_t *hash_length ) |
| 1150 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1151 | switch( operation->id ) |
| 1152 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1153 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1154 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1155 | return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx, |
| 1156 | hash, hash_size, hash_length ) ); |
| 1157 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1158 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1159 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1160 | return( mbedtls_test_transparent_hash_finish( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1161 | &operation->ctx.test_driver_ctx, |
| 1162 | hash, hash_size, hash_length ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1163 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1164 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1165 | (void) hash; |
| 1166 | (void) hash_size; |
| 1167 | (void) hash_length; |
| 1168 | return( PSA_ERROR_BAD_STATE ); |
| 1169 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | psa_status_t psa_driver_wrapper_hash_abort( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1173 | psa_hash_operation_t *operation ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1174 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1175 | switch( operation->id ) |
| 1176 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1177 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1178 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1179 | return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) ); |
| 1180 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1181 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1182 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1183 | return( mbedtls_test_transparent_hash_abort( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1184 | &operation->ctx.test_driver_ctx ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1185 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1186 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1187 | return( PSA_ERROR_BAD_STATE ); |
| 1188 | } |
| 1189 | } |
| 1190 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1191 | psa_status_t psa_driver_wrapper_aead_encrypt( |
| 1192 | const psa_key_attributes_t *attributes, |
| 1193 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1194 | psa_algorithm_t alg, |
| 1195 | const uint8_t *nonce, size_t nonce_length, |
| 1196 | const uint8_t *additional_data, size_t additional_data_length, |
| 1197 | const uint8_t *plaintext, size_t plaintext_length, |
| 1198 | uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length ) |
| 1199 | { |
| 1200 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1201 | psa_key_location_t location = |
| 1202 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1203 | |
| 1204 | switch( location ) |
| 1205 | { |
| 1206 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1207 | /* Key is stored in the slot in export representation, so |
| 1208 | * cycle through all known transparent accelerators */ |
| 1209 | |
| 1210 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1211 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1212 | status = mbedtls_test_transparent_aead_encrypt( |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1213 | attributes, key_buffer, key_buffer_size, |
| 1214 | alg, |
| 1215 | nonce, nonce_length, |
| 1216 | additional_data, additional_data_length, |
| 1217 | plaintext, plaintext_length, |
| 1218 | ciphertext, ciphertext_size, ciphertext_length ); |
| 1219 | /* Declared with fallback == true */ |
| 1220 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1221 | return( status ); |
| 1222 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1223 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1224 | |
| 1225 | /* Fell through, meaning no accelerator supports this operation */ |
| 1226 | return( mbedtls_psa_aead_encrypt( |
| 1227 | attributes, key_buffer, key_buffer_size, |
| 1228 | alg, |
| 1229 | nonce, nonce_length, |
| 1230 | additional_data, additional_data_length, |
| 1231 | plaintext, plaintext_length, |
| 1232 | ciphertext, ciphertext_size, ciphertext_length ) ); |
| 1233 | |
| 1234 | /* Add cases for opaque driver here */ |
| 1235 | |
| 1236 | default: |
| 1237 | /* Key is declared with a lifetime not known to us */ |
| 1238 | (void)status; |
| 1239 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | psa_status_t psa_driver_wrapper_aead_decrypt( |
| 1244 | const psa_key_attributes_t *attributes, |
| 1245 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1246 | psa_algorithm_t alg, |
| 1247 | const uint8_t *nonce, size_t nonce_length, |
| 1248 | const uint8_t *additional_data, size_t additional_data_length, |
| 1249 | const uint8_t *ciphertext, size_t ciphertext_length, |
| 1250 | uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length ) |
| 1251 | { |
| 1252 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1253 | psa_key_location_t location = |
| 1254 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1255 | |
| 1256 | switch( location ) |
| 1257 | { |
| 1258 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1259 | /* Key is stored in the slot in export representation, so |
| 1260 | * cycle through all known transparent accelerators */ |
| 1261 | |
| 1262 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1263 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1264 | status = mbedtls_test_transparent_aead_decrypt( |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1265 | attributes, key_buffer, key_buffer_size, |
| 1266 | alg, |
| 1267 | nonce, nonce_length, |
| 1268 | additional_data, additional_data_length, |
| 1269 | ciphertext, ciphertext_length, |
| 1270 | plaintext, plaintext_size, plaintext_length ); |
| 1271 | /* Declared with fallback == true */ |
| 1272 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1273 | return( status ); |
| 1274 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1275 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1276 | |
| 1277 | /* Fell through, meaning no accelerator supports this operation */ |
| 1278 | return( mbedtls_psa_aead_decrypt( |
| 1279 | attributes, key_buffer, key_buffer_size, |
| 1280 | alg, |
| 1281 | nonce, nonce_length, |
| 1282 | additional_data, additional_data_length, |
| 1283 | ciphertext, ciphertext_length, |
| 1284 | plaintext, plaintext_size, plaintext_length ) ); |
| 1285 | |
| 1286 | /* Add cases for opaque driver here */ |
| 1287 | |
| 1288 | default: |
| 1289 | /* Key is declared with a lifetime not known to us */ |
| 1290 | (void)status; |
| 1291 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1292 | } |
| 1293 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1294 | |
| 1295 | |
| 1296 | /* |
| 1297 | * MAC functions |
| 1298 | */ |
| 1299 | psa_status_t psa_driver_wrapper_mac_compute( |
| 1300 | const psa_key_attributes_t *attributes, |
| 1301 | const uint8_t *key_buffer, |
| 1302 | size_t key_buffer_size, |
| 1303 | psa_algorithm_t alg, |
| 1304 | const uint8_t *input, |
| 1305 | size_t input_length, |
| 1306 | uint8_t *mac, |
| 1307 | size_t mac_size, |
| 1308 | size_t *mac_length ) |
| 1309 | { |
| 1310 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1311 | psa_key_location_t location = |
| 1312 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1313 | |
| 1314 | switch( location ) |
| 1315 | { |
| 1316 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1317 | /* Key is stored in the slot in export representation, so |
| 1318 | * cycle through all known transparent accelerators */ |
| 1319 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1320 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame^] | 1321 | status = mbedtls_test_transparent_mac_compute( |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1322 | attributes, key_buffer, key_buffer_size, alg, |
| 1323 | input, input_length, |
| 1324 | mac, mac_size, mac_length ); |
| 1325 | /* Declared with fallback == true */ |
| 1326 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1327 | return( status ); |
| 1328 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1329 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1330 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1331 | /* Fell through, meaning no accelerator supports this operation */ |
| 1332 | status = mbedtls_psa_mac_compute( |
| 1333 | attributes, key_buffer, key_buffer_size, alg, |
| 1334 | input, input_length, |
| 1335 | mac, mac_size, mac_length ); |
| 1336 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1337 | return( status ); |
| 1338 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1339 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1340 | |
| 1341 | /* Add cases for opaque driver here */ |
| 1342 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1343 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1344 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame^] | 1345 | status = mbedtls_test_opaque_mac_compute( |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1346 | attributes, key_buffer, key_buffer_size, alg, |
| 1347 | input, input_length, |
| 1348 | mac, mac_size, mac_length ); |
| 1349 | return( status ); |
| 1350 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1351 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1352 | default: |
| 1353 | /* Key is declared with a lifetime not known to us */ |
| 1354 | (void) key_buffer; |
| 1355 | (void) key_buffer_size; |
| 1356 | (void) alg; |
| 1357 | (void) input; |
| 1358 | (void) input_length; |
| 1359 | (void) mac; |
| 1360 | (void) mac_size; |
| 1361 | (void) mac_length; |
| 1362 | (void) status; |
| 1363 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | psa_status_t psa_driver_wrapper_mac_sign_setup( |
| 1368 | psa_mac_operation_t *operation, |
| 1369 | const psa_key_attributes_t *attributes, |
| 1370 | const uint8_t *key_buffer, |
| 1371 | size_t key_buffer_size, |
| 1372 | psa_algorithm_t alg ) |
| 1373 | { |
| 1374 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1375 | psa_key_location_t location = |
| 1376 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1377 | |
| 1378 | switch( location ) |
| 1379 | { |
| 1380 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1381 | /* Key is stored in the slot in export representation, so |
| 1382 | * cycle through all known transparent accelerators */ |
| 1383 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1384 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame^] | 1385 | status = mbedtls_test_transparent_mac_sign_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1386 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1387 | attributes, |
| 1388 | key_buffer, key_buffer_size, |
| 1389 | alg ); |
| 1390 | /* Declared with fallback == true */ |
| 1391 | if( status == PSA_SUCCESS ) |
| 1392 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
| 1393 | |
| 1394 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1395 | return( status ); |
| 1396 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1397 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1398 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1399 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1400 | status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1401 | attributes, |
| 1402 | key_buffer, key_buffer_size, |
| 1403 | alg ); |
| 1404 | if( status == PSA_SUCCESS ) |
| 1405 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 1406 | |
| 1407 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1408 | return( status ); |
| 1409 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1410 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1411 | |
| 1412 | /* Add cases for opaque driver here */ |
| 1413 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1414 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1415 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame^] | 1416 | status = mbedtls_test_opaque_mac_sign_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1417 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1418 | attributes, |
| 1419 | key_buffer, key_buffer_size, |
| 1420 | alg ); |
| 1421 | |
| 1422 | if( status == PSA_SUCCESS ) |
| 1423 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
| 1424 | |
| 1425 | return( status ); |
| 1426 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1427 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1428 | default: |
| 1429 | /* Key is declared with a lifetime not known to us */ |
| 1430 | (void) status; |
| 1431 | (void) key_buffer; |
| 1432 | (void) key_buffer_size; |
| 1433 | (void) alg; |
| 1434 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | psa_status_t psa_driver_wrapper_mac_verify_setup( |
| 1439 | psa_mac_operation_t *operation, |
| 1440 | const psa_key_attributes_t *attributes, |
| 1441 | const uint8_t *key_buffer, |
| 1442 | size_t key_buffer_size, |
| 1443 | psa_algorithm_t alg ) |
| 1444 | { |
| 1445 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1446 | psa_key_location_t location = |
| 1447 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1448 | |
| 1449 | switch( location ) |
| 1450 | { |
| 1451 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1452 | /* Key is stored in the slot in export representation, so |
| 1453 | * cycle through all known transparent accelerators */ |
| 1454 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1455 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame^] | 1456 | status = mbedtls_test_transparent_mac_verify_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1457 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1458 | attributes, |
| 1459 | key_buffer, key_buffer_size, |
| 1460 | alg ); |
| 1461 | /* Declared with fallback == true */ |
| 1462 | if( status == PSA_SUCCESS ) |
| 1463 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
| 1464 | |
| 1465 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1466 | return( status ); |
| 1467 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1468 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1469 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1470 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1471 | status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1472 | attributes, |
| 1473 | key_buffer, key_buffer_size, |
| 1474 | alg ); |
| 1475 | if( status == PSA_SUCCESS ) |
| 1476 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 1477 | |
| 1478 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1479 | return( status ); |
| 1480 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1481 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1482 | |
| 1483 | /* Add cases for opaque driver here */ |
| 1484 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1485 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1486 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame^] | 1487 | status = mbedtls_test_opaque_mac_verify_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1488 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1489 | attributes, |
| 1490 | key_buffer, key_buffer_size, |
| 1491 | alg ); |
| 1492 | |
| 1493 | if( status == PSA_SUCCESS ) |
| 1494 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
| 1495 | |
| 1496 | return( status ); |
| 1497 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1498 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1499 | default: |
| 1500 | /* Key is declared with a lifetime not known to us */ |
| 1501 | (void) status; |
| 1502 | (void) key_buffer; |
| 1503 | (void) key_buffer_size; |
| 1504 | (void) alg; |
| 1505 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | psa_status_t psa_driver_wrapper_mac_update( |
| 1510 | psa_mac_operation_t *operation, |
| 1511 | const uint8_t *input, |
| 1512 | size_t input_length ) |
| 1513 | { |
| 1514 | switch( operation->id ) |
| 1515 | { |
| 1516 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1517 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1518 | return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1519 | input, input_length ) ); |
| 1520 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1521 | |
| 1522 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1523 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1524 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame^] | 1525 | return( mbedtls_test_transparent_mac_update( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1526 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1527 | input, input_length ) ); |
| 1528 | |
| 1529 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame^] | 1530 | return( mbedtls_test_opaque_mac_update( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1531 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1532 | input, input_length ) ); |
| 1533 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1534 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1535 | default: |
| 1536 | (void) input; |
| 1537 | (void) input_length; |
| 1538 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | psa_status_t psa_driver_wrapper_mac_sign_finish( |
| 1543 | psa_mac_operation_t *operation, |
| 1544 | uint8_t *mac, |
| 1545 | size_t mac_size, |
| 1546 | size_t *mac_length ) |
| 1547 | { |
| 1548 | switch( operation->id ) |
| 1549 | { |
| 1550 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1551 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1552 | return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1553 | mac, mac_size, mac_length ) ); |
| 1554 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1555 | |
| 1556 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1557 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1558 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame^] | 1559 | return( mbedtls_test_transparent_mac_sign_finish( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1560 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1561 | mac, mac_size, mac_length ) ); |
| 1562 | |
| 1563 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame^] | 1564 | return( mbedtls_test_opaque_mac_sign_finish( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1565 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1566 | mac, mac_size, mac_length ) ); |
| 1567 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1568 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1569 | default: |
| 1570 | (void) mac; |
| 1571 | (void) mac_size; |
| 1572 | (void) mac_length; |
| 1573 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | psa_status_t psa_driver_wrapper_mac_verify_finish( |
| 1578 | psa_mac_operation_t *operation, |
| 1579 | const uint8_t *mac, |
| 1580 | size_t mac_length ) |
| 1581 | { |
| 1582 | switch( operation->id ) |
| 1583 | { |
| 1584 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1585 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1586 | return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1587 | mac, mac_length ) ); |
| 1588 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1589 | |
| 1590 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1591 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1592 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame^] | 1593 | return( mbedtls_test_transparent_mac_verify_finish( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1594 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1595 | mac, mac_length ) ); |
| 1596 | |
| 1597 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame^] | 1598 | return( mbedtls_test_opaque_mac_verify_finish( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1599 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1600 | mac, mac_length ) ); |
| 1601 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1602 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1603 | default: |
| 1604 | (void) mac; |
| 1605 | (void) mac_length; |
| 1606 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | psa_status_t psa_driver_wrapper_mac_abort( |
| 1611 | psa_mac_operation_t *operation ) |
| 1612 | { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1613 | switch( operation->id ) |
| 1614 | { |
| 1615 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1616 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1617 | return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1618 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1619 | |
| 1620 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1621 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1622 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame^] | 1623 | return( mbedtls_test_transparent_mac_abort( |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1624 | &operation->ctx.transparent_test_driver_ctx ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1625 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame^] | 1626 | return( mbedtls_test_opaque_mac_abort( |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1627 | &operation->ctx.opaque_test_driver_ctx ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1628 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1629 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1630 | default: |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1631 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1632 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1633 | } |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 1634 | /* End of automatically generated file. */ |