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