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; |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1063 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1064 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1065 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1066 | /* Add cases for opaque driver here */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1067 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1068 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 1069 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1070 | status = mbedtls_test_opaque_cipher_decrypt_setup( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1071 | &operation->ctx.opaque_test_driver_ctx, |
| 1072 | attributes, |
| 1073 | key_buffer, key_buffer_size, |
| 1074 | alg); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1075 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1076 | if (status == PSA_SUCCESS) { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1077 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1078 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1079 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1080 | return status; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1081 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1082 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1083 | default: |
| 1084 | /* Key is declared with a lifetime not known to us */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1085 | (void) status; |
| 1086 | (void) operation; |
| 1087 | (void) key_buffer; |
| 1088 | (void) key_buffer_size; |
| 1089 | (void) alg; |
| 1090 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1091 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1092 | } |
| 1093 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1094 | psa_status_t psa_driver_wrapper_cipher_set_iv( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1095 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1096 | const uint8_t *iv, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1097 | size_t iv_length) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1098 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1099 | switch (operation->id) { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1100 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1101 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1102 | return mbedtls_psa_cipher_set_iv(&operation->ctx.mbedtls_ctx, |
| 1103 | iv, |
| 1104 | iv_length); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1105 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1106 | |
| 1107 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1108 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1109 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1110 | return mbedtls_test_transparent_cipher_set_iv( |
| 1111 | &operation->ctx.transparent_test_driver_ctx, |
| 1112 | iv, iv_length); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1113 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1114 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1115 | return mbedtls_test_opaque_cipher_set_iv( |
| 1116 | &operation->ctx.opaque_test_driver_ctx, |
| 1117 | iv, iv_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1118 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1119 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1120 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1121 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1122 | (void) iv; |
| 1123 | (void) iv_length; |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1124 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1125 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | psa_status_t psa_driver_wrapper_cipher_update( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1129 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1130 | const uint8_t *input, |
| 1131 | size_t input_length, |
| 1132 | uint8_t *output, |
| 1133 | size_t output_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1134 | size_t *output_length) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1135 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1136 | switch (operation->id) { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1137 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1138 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1139 | return mbedtls_psa_cipher_update(&operation->ctx.mbedtls_ctx, |
| 1140 | input, |
| 1141 | input_length, |
| 1142 | output, |
| 1143 | output_size, |
| 1144 | output_length); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1145 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
| 1146 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1147 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1148 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1149 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1150 | return mbedtls_test_transparent_cipher_update( |
| 1151 | &operation->ctx.transparent_test_driver_ctx, |
| 1152 | input, input_length, |
| 1153 | output, output_size, output_length); |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1154 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1155 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1156 | return mbedtls_test_opaque_cipher_update( |
| 1157 | &operation->ctx.opaque_test_driver_ctx, |
| 1158 | input, input_length, |
| 1159 | output, output_size, output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1160 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1161 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1162 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1163 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1164 | (void) input; |
| 1165 | (void) input_length; |
| 1166 | (void) output; |
| 1167 | (void) output_size; |
| 1168 | (void) output_length; |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1169 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1170 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | psa_status_t psa_driver_wrapper_cipher_finish( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1174 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1175 | uint8_t *output, |
| 1176 | size_t output_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1177 | size_t *output_length) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1178 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1179 | switch (operation->id) { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1180 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1181 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1182 | return mbedtls_psa_cipher_finish(&operation->ctx.mbedtls_ctx, |
| 1183 | output, |
| 1184 | output_size, |
| 1185 | output_length); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1186 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1187 | |
| 1188 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1189 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1190 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1191 | return mbedtls_test_transparent_cipher_finish( |
| 1192 | &operation->ctx.transparent_test_driver_ctx, |
| 1193 | output, output_size, output_length); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1194 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1195 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1196 | return mbedtls_test_opaque_cipher_finish( |
| 1197 | &operation->ctx.opaque_test_driver_ctx, |
| 1198 | output, output_size, output_length); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1199 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1200 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1201 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1202 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1203 | (void) output; |
| 1204 | (void) output_size; |
| 1205 | (void) output_length; |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1206 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1207 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | psa_status_t psa_driver_wrapper_cipher_abort( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1211 | psa_cipher_operation_t *operation) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1212 | { |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1213 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1214 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1215 | switch (operation->id) { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1216 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1217 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1218 | return mbedtls_psa_cipher_abort(&operation->ctx.mbedtls_ctx); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1219 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1220 | |
| 1221 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1222 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1223 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1224 | status = mbedtls_test_transparent_cipher_abort( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1225 | &operation->ctx.transparent_test_driver_ctx); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1226 | mbedtls_platform_zeroize( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1227 | &operation->ctx.transparent_test_driver_ctx, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1228 | sizeof(operation->ctx.transparent_test_driver_ctx)); |
| 1229 | return status; |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1230 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1231 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1232 | status = mbedtls_test_opaque_cipher_abort( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1233 | &operation->ctx.opaque_test_driver_ctx); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1234 | mbedtls_platform_zeroize( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1235 | &operation->ctx.opaque_test_driver_ctx, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1236 | sizeof(operation->ctx.opaque_test_driver_ctx)); |
| 1237 | return status; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1238 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1239 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1240 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1241 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1242 | (void) status; |
| 1243 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1246 | /* |
| 1247 | * Hashing functions |
| 1248 | */ |
| 1249 | psa_status_t psa_driver_wrapper_hash_compute( |
| 1250 | psa_algorithm_t alg, |
| 1251 | const uint8_t *input, |
| 1252 | size_t input_length, |
| 1253 | uint8_t *hash, |
| 1254 | size_t hash_size, |
| 1255 | size_t *hash_length) |
| 1256 | { |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1257 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1258 | |
| 1259 | /* Try accelerators first */ |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1260 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1261 | status = mbedtls_test_transparent_hash_compute( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1262 | alg, input, input_length, hash, hash_size, hash_length); |
| 1263 | if (status != PSA_ERROR_NOT_SUPPORTED) { |
| 1264 | return status; |
| 1265 | } |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1266 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1267 | |
| 1268 | /* If software fallback is compiled in, try fallback */ |
| 1269 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1270 | status = mbedtls_psa_hash_compute(alg, input, input_length, |
| 1271 | hash, hash_size, hash_length); |
| 1272 | if (status != PSA_ERROR_NOT_SUPPORTED) { |
| 1273 | return status; |
| 1274 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1275 | #endif |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1276 | (void) status; |
| 1277 | (void) alg; |
| 1278 | (void) input; |
| 1279 | (void) input_length; |
| 1280 | (void) hash; |
| 1281 | (void) hash_size; |
| 1282 | (void) hash_length; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1283 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1284 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | psa_status_t psa_driver_wrapper_hash_setup( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1288 | psa_hash_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1289 | psa_algorithm_t alg) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1290 | { |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1291 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1292 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1293 | /* Try setup on accelerators first */ |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1294 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1295 | status = mbedtls_test_transparent_hash_setup( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1296 | &operation->ctx.test_driver_ctx, alg); |
| 1297 | if (status == PSA_SUCCESS) { |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1298 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1299 | } |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1300 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1301 | if (status != PSA_ERROR_NOT_SUPPORTED) { |
| 1302 | return status; |
| 1303 | } |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1304 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1305 | |
| 1306 | /* If software fallback is compiled in, try fallback */ |
| 1307 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1308 | status = mbedtls_psa_hash_setup(&operation->ctx.mbedtls_ctx, alg); |
| 1309 | if (status == PSA_SUCCESS) { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1310 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1311 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1312 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1313 | if (status != PSA_ERROR_NOT_SUPPORTED) { |
| 1314 | return status; |
| 1315 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1316 | #endif |
| 1317 | /* Nothing left to try if we fall through here */ |
| 1318 | (void) status; |
| 1319 | (void) operation; |
| 1320 | (void) alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1321 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | psa_status_t psa_driver_wrapper_hash_clone( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1325 | const psa_hash_operation_t *source_operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1326 | psa_hash_operation_t *target_operation) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1327 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1328 | switch (source_operation->id) { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1329 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1330 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1331 | target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1332 | return mbedtls_psa_hash_clone(&source_operation->ctx.mbedtls_ctx, |
| 1333 | &target_operation->ctx.mbedtls_ctx); |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1334 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1335 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1336 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
| 1337 | target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1338 | return mbedtls_test_transparent_hash_clone( |
| 1339 | &source_operation->ctx.test_driver_ctx, |
| 1340 | &target_operation->ctx.test_driver_ctx); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1341 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1342 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1343 | (void) target_operation; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1344 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1345 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | psa_status_t psa_driver_wrapper_hash_update( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1349 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1350 | const uint8_t *input, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1351 | size_t input_length) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1352 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1353 | switch (operation->id) { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1354 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1355 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1356 | return mbedtls_psa_hash_update(&operation->ctx.mbedtls_ctx, |
| 1357 | input, input_length); |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1358 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1359 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1360 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1361 | return mbedtls_test_transparent_hash_update( |
| 1362 | &operation->ctx.test_driver_ctx, |
| 1363 | input, input_length); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1364 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1365 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1366 | (void) input; |
| 1367 | (void) input_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1368 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1369 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | psa_status_t psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1373 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1374 | uint8_t *hash, |
| 1375 | size_t hash_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1376 | size_t *hash_length) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1377 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1378 | switch (operation->id) { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1379 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1380 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1381 | return mbedtls_psa_hash_finish(&operation->ctx.mbedtls_ctx, |
| 1382 | hash, hash_size, hash_length); |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1383 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1384 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1385 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1386 | return mbedtls_test_transparent_hash_finish( |
| 1387 | &operation->ctx.test_driver_ctx, |
| 1388 | hash, hash_size, hash_length); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1389 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1390 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1391 | (void) hash; |
| 1392 | (void) hash_size; |
| 1393 | (void) hash_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1394 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1395 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | psa_status_t psa_driver_wrapper_hash_abort( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1399 | psa_hash_operation_t *operation) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1400 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1401 | switch (operation->id) { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1402 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1403 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1404 | return mbedtls_psa_hash_abort(&operation->ctx.mbedtls_ctx); |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1405 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1406 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1407 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1408 | return mbedtls_test_transparent_hash_abort( |
| 1409 | &operation->ctx.test_driver_ctx); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1410 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1411 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1412 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1413 | } |
| 1414 | } |
| 1415 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1416 | psa_status_t psa_driver_wrapper_aead_encrypt( |
| 1417 | const psa_key_attributes_t *attributes, |
| 1418 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1419 | psa_algorithm_t alg, |
| 1420 | const uint8_t *nonce, size_t nonce_length, |
| 1421 | const uint8_t *additional_data, size_t additional_data_length, |
| 1422 | const uint8_t *plaintext, size_t plaintext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1423 | uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length) |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1424 | { |
| 1425 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1426 | psa_key_location_t location = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1427 | PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime); |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1428 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1429 | switch (location) { |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1430 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1431 | /* Key is stored in the slot in export representation, so |
| 1432 | * cycle through all known transparent accelerators */ |
| 1433 | |
| 1434 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1435 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1436 | status = mbedtls_test_transparent_aead_encrypt( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1437 | attributes, key_buffer, key_buffer_size, |
| 1438 | alg, |
| 1439 | nonce, nonce_length, |
| 1440 | additional_data, additional_data_length, |
| 1441 | plaintext, plaintext_length, |
| 1442 | ciphertext, ciphertext_size, ciphertext_length); |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1443 | /* Declared with fallback == true */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1444 | if (status != PSA_ERROR_NOT_SUPPORTED) { |
| 1445 | return status; |
| 1446 | } |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1447 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1448 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1449 | |
| 1450 | /* Fell through, meaning no accelerator supports this operation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1451 | return mbedtls_psa_aead_encrypt( |
| 1452 | attributes, key_buffer, key_buffer_size, |
| 1453 | alg, |
| 1454 | nonce, nonce_length, |
| 1455 | additional_data, additional_data_length, |
| 1456 | plaintext, plaintext_length, |
| 1457 | ciphertext, ciphertext_size, ciphertext_length); |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1458 | |
| 1459 | /* Add cases for opaque driver here */ |
| 1460 | |
| 1461 | default: |
| 1462 | /* Key is declared with a lifetime not known to us */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1463 | (void) status; |
| 1464 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | psa_status_t psa_driver_wrapper_aead_decrypt( |
| 1469 | const psa_key_attributes_t *attributes, |
| 1470 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1471 | psa_algorithm_t alg, |
| 1472 | const uint8_t *nonce, size_t nonce_length, |
| 1473 | const uint8_t *additional_data, size_t additional_data_length, |
| 1474 | const uint8_t *ciphertext, size_t ciphertext_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1475 | uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length) |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1476 | { |
| 1477 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1478 | psa_key_location_t location = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1479 | PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime); |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1480 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1481 | switch (location) { |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1482 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1483 | /* Key is stored in the slot in export representation, so |
| 1484 | * cycle through all known transparent accelerators */ |
| 1485 | |
| 1486 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1487 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1488 | status = mbedtls_test_transparent_aead_decrypt( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1489 | attributes, key_buffer, key_buffer_size, |
| 1490 | alg, |
| 1491 | nonce, nonce_length, |
| 1492 | additional_data, additional_data_length, |
| 1493 | ciphertext, ciphertext_length, |
| 1494 | plaintext, plaintext_size, plaintext_length); |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1495 | /* Declared with fallback == true */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1496 | if (status != PSA_ERROR_NOT_SUPPORTED) { |
| 1497 | return status; |
| 1498 | } |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1499 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1500 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1501 | |
| 1502 | /* Fell through, meaning no accelerator supports this operation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1503 | return mbedtls_psa_aead_decrypt( |
| 1504 | attributes, key_buffer, key_buffer_size, |
| 1505 | alg, |
| 1506 | nonce, nonce_length, |
| 1507 | additional_data, additional_data_length, |
| 1508 | ciphertext, ciphertext_length, |
| 1509 | plaintext, plaintext_size, plaintext_length); |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1510 | |
| 1511 | /* Add cases for opaque driver here */ |
| 1512 | |
| 1513 | default: |
| 1514 | /* Key is declared with a lifetime not known to us */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1515 | (void) status; |
| 1516 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1517 | } |
| 1518 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1519 | |
| 1520 | |
| 1521 | /* |
| 1522 | * MAC functions |
| 1523 | */ |
| 1524 | psa_status_t psa_driver_wrapper_mac_compute( |
| 1525 | const psa_key_attributes_t *attributes, |
| 1526 | const uint8_t *key_buffer, |
| 1527 | size_t key_buffer_size, |
| 1528 | psa_algorithm_t alg, |
| 1529 | const uint8_t *input, |
| 1530 | size_t input_length, |
| 1531 | uint8_t *mac, |
| 1532 | size_t mac_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1533 | size_t *mac_length) |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1534 | { |
| 1535 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1536 | psa_key_location_t location = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1537 | PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1538 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1539 | switch (location) { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1540 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1541 | /* Key is stored in the slot in export representation, so |
| 1542 | * cycle through all known transparent accelerators */ |
| 1543 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1544 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1545 | status = mbedtls_test_transparent_mac_compute( |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1546 | attributes, key_buffer, key_buffer_size, alg, |
| 1547 | input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1548 | mac, mac_size, mac_length); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1549 | /* Declared with fallback == true */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1550 | if (status != PSA_ERROR_NOT_SUPPORTED) { |
| 1551 | return status; |
| 1552 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1553 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1554 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1555 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1556 | /* Fell through, meaning no accelerator supports this operation */ |
| 1557 | status = mbedtls_psa_mac_compute( |
| 1558 | attributes, key_buffer, key_buffer_size, alg, |
| 1559 | input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1560 | mac, mac_size, mac_length); |
| 1561 | if (status != PSA_ERROR_NOT_SUPPORTED) { |
| 1562 | return status; |
| 1563 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1564 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1565 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1566 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1567 | /* Add cases for opaque driver here */ |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1568 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1569 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1570 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1571 | status = mbedtls_test_opaque_mac_compute( |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1572 | attributes, key_buffer, key_buffer_size, alg, |
| 1573 | input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1574 | mac, mac_size, mac_length); |
| 1575 | return status; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1576 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1577 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1578 | default: |
| 1579 | /* Key is declared with a lifetime not known to us */ |
| 1580 | (void) key_buffer; |
| 1581 | (void) key_buffer_size; |
| 1582 | (void) alg; |
| 1583 | (void) input; |
| 1584 | (void) input_length; |
| 1585 | (void) mac; |
| 1586 | (void) mac_size; |
| 1587 | (void) mac_length; |
| 1588 | (void) status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1589 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | psa_status_t psa_driver_wrapper_mac_sign_setup( |
| 1594 | psa_mac_operation_t *operation, |
| 1595 | const psa_key_attributes_t *attributes, |
| 1596 | const uint8_t *key_buffer, |
| 1597 | size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1598 | psa_algorithm_t alg) |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1599 | { |
| 1600 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1601 | psa_key_location_t location = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1602 | PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1603 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1604 | switch (location) { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1605 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1606 | /* Key is stored in the slot in export representation, so |
| 1607 | * cycle through all known transparent accelerators */ |
| 1608 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1609 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1610 | status = mbedtls_test_transparent_mac_sign_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1611 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1612 | attributes, |
| 1613 | key_buffer, key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1614 | alg); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1615 | /* Declared with fallback == true */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1616 | if (status == PSA_SUCCESS) { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1617 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1618 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1619 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1620 | if (status != PSA_ERROR_NOT_SUPPORTED) { |
| 1621 | return status; |
| 1622 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1623 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1624 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1625 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1626 | /* Fell through, meaning no accelerator supports this operation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1627 | status = mbedtls_psa_mac_sign_setup(&operation->ctx.mbedtls_ctx, |
| 1628 | attributes, |
| 1629 | key_buffer, key_buffer_size, |
| 1630 | alg); |
| 1631 | if (status == PSA_SUCCESS) { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1632 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1633 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1634 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1635 | if (status != PSA_ERROR_NOT_SUPPORTED) { |
| 1636 | return status; |
| 1637 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1638 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1639 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1640 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1641 | /* Add cases for opaque driver here */ |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1642 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1643 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1644 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1645 | status = mbedtls_test_opaque_mac_sign_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1646 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1647 | attributes, |
| 1648 | key_buffer, key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1649 | alg); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1650 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1651 | if (status == PSA_SUCCESS) { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1652 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1653 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1654 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1655 | return status; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1656 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1657 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1658 | default: |
| 1659 | /* Key is declared with a lifetime not known to us */ |
| 1660 | (void) status; |
Ronald Cron | 485559e | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 1661 | (void) operation; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1662 | (void) key_buffer; |
| 1663 | (void) key_buffer_size; |
| 1664 | (void) alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1665 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | psa_status_t psa_driver_wrapper_mac_verify_setup( |
| 1670 | psa_mac_operation_t *operation, |
| 1671 | const psa_key_attributes_t *attributes, |
| 1672 | const uint8_t *key_buffer, |
| 1673 | size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1674 | psa_algorithm_t alg) |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1675 | { |
| 1676 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1677 | psa_key_location_t location = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1678 | PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1679 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1680 | switch (location) { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1681 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1682 | /* Key is stored in the slot in export representation, so |
| 1683 | * cycle through all known transparent accelerators */ |
| 1684 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1685 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1686 | status = mbedtls_test_transparent_mac_verify_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1687 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1688 | attributes, |
| 1689 | key_buffer, key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1690 | alg); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1691 | /* Declared with fallback == true */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1692 | if (status == PSA_SUCCESS) { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1693 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1694 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1695 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1696 | if (status != PSA_ERROR_NOT_SUPPORTED) { |
| 1697 | return status; |
| 1698 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1699 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1700 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1701 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1702 | /* Fell through, meaning no accelerator supports this operation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1703 | status = mbedtls_psa_mac_verify_setup(&operation->ctx.mbedtls_ctx, |
| 1704 | attributes, |
| 1705 | key_buffer, key_buffer_size, |
| 1706 | alg); |
| 1707 | if (status == PSA_SUCCESS) { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1708 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1709 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1710 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1711 | if (status != PSA_ERROR_NOT_SUPPORTED) { |
| 1712 | return status; |
| 1713 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1714 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1715 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1716 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1717 | /* Add cases for opaque driver here */ |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1718 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1719 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1720 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1721 | status = mbedtls_test_opaque_mac_verify_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1722 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1723 | attributes, |
| 1724 | key_buffer, key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1725 | alg); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1726 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1727 | if (status == PSA_SUCCESS) { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1728 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1729 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1730 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1731 | return status; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1732 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1733 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1734 | default: |
| 1735 | /* Key is declared with a lifetime not known to us */ |
| 1736 | (void) status; |
Ronald Cron | 485559e | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 1737 | (void) operation; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1738 | (void) key_buffer; |
| 1739 | (void) key_buffer_size; |
| 1740 | (void) alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1741 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | psa_status_t psa_driver_wrapper_mac_update( |
| 1746 | psa_mac_operation_t *operation, |
| 1747 | const uint8_t *input, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1748 | size_t input_length) |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1749 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1750 | switch (operation->id) { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1751 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1752 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1753 | return mbedtls_psa_mac_update(&operation->ctx.mbedtls_ctx, |
| 1754 | input, input_length); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1755 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1756 | |
| 1757 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1758 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1759 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1760 | return mbedtls_test_transparent_mac_update( |
| 1761 | &operation->ctx.transparent_test_driver_ctx, |
| 1762 | input, input_length); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1763 | |
| 1764 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1765 | return mbedtls_test_opaque_mac_update( |
| 1766 | &operation->ctx.opaque_test_driver_ctx, |
| 1767 | input, input_length); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1768 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1769 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1770 | default: |
| 1771 | (void) input; |
| 1772 | (void) input_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1773 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | psa_status_t psa_driver_wrapper_mac_sign_finish( |
| 1778 | psa_mac_operation_t *operation, |
| 1779 | uint8_t *mac, |
| 1780 | size_t mac_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1781 | size_t *mac_length) |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1782 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1783 | switch (operation->id) { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1784 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1785 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1786 | return mbedtls_psa_mac_sign_finish(&operation->ctx.mbedtls_ctx, |
| 1787 | mac, mac_size, mac_length); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1788 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1789 | |
| 1790 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1791 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1792 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1793 | return mbedtls_test_transparent_mac_sign_finish( |
| 1794 | &operation->ctx.transparent_test_driver_ctx, |
| 1795 | mac, mac_size, mac_length); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1796 | |
| 1797 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1798 | return mbedtls_test_opaque_mac_sign_finish( |
| 1799 | &operation->ctx.opaque_test_driver_ctx, |
| 1800 | mac, mac_size, mac_length); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1801 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1802 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1803 | default: |
| 1804 | (void) mac; |
| 1805 | (void) mac_size; |
| 1806 | (void) mac_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1807 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | psa_status_t psa_driver_wrapper_mac_verify_finish( |
| 1812 | psa_mac_operation_t *operation, |
| 1813 | const uint8_t *mac, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1814 | size_t mac_length) |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1815 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1816 | switch (operation->id) { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1817 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1818 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1819 | return mbedtls_psa_mac_verify_finish(&operation->ctx.mbedtls_ctx, |
| 1820 | mac, mac_length); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1821 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1822 | |
| 1823 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1824 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1825 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1826 | return mbedtls_test_transparent_mac_verify_finish( |
| 1827 | &operation->ctx.transparent_test_driver_ctx, |
| 1828 | mac, mac_length); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1829 | |
| 1830 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1831 | return mbedtls_test_opaque_mac_verify_finish( |
| 1832 | &operation->ctx.opaque_test_driver_ctx, |
| 1833 | mac, mac_length); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1834 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1835 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1836 | default: |
| 1837 | (void) mac; |
| 1838 | (void) mac_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1839 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | psa_status_t psa_driver_wrapper_mac_abort( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1844 | psa_mac_operation_t *operation) |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1845 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1846 | switch (operation->id) { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1847 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1848 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1849 | return mbedtls_psa_mac_abort(&operation->ctx.mbedtls_ctx); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1850 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1851 | |
| 1852 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1853 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1854 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1855 | return mbedtls_test_transparent_mac_abort( |
| 1856 | &operation->ctx.transparent_test_driver_ctx); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1857 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1858 | return mbedtls_test_opaque_mac_abort( |
| 1859 | &operation->ctx.opaque_test_driver_ctx); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1860 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1861 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1862 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 1863 | return PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1864 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1865 | } |
Gilles Peskine | ad0e012 | 2021-04-24 13:19:45 +0200 | [diff] [blame] | 1866 | |
| 1867 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |