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