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 | |
| 22 | #include "psa_crypto_core.h" |
| 23 | #include "psa_crypto_driver_wrappers.h" |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 24 | #include "mbedtls/platform.h" |
| 25 | |
| 26 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 27 | |
| 28 | /* Include test driver definition when running tests */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 29 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 30 | #ifndef PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 31 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 32 | #endif |
| 33 | #ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 34 | #define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 35 | #endif |
Steven Cooreman | 0d7c64d | 2020-09-07 16:17:55 +0200 | [diff] [blame] | 36 | #include "test/drivers/test_driver.h" |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 37 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 38 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 39 | /* Repeat above block for each JSON-declared driver during autogeneration */ |
| 40 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 41 | /* Auto-generated values depending on which drivers are registered. ID 0 is |
| 42 | * reserved for unallocated operations. */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 43 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 44 | #define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (1) |
| 45 | #define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (2) |
| 46 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 47 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */ |
| 48 | |
| 49 | /* Support the 'old' SE interface when asked to */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 50 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 51 | /* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style |
| 52 | * SE driver is present, to avoid unused argument errors at compile time. */ |
| 53 | #ifndef PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 54 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 55 | #endif |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 56 | #include "psa_crypto_se.h" |
| 57 | #endif |
| 58 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 59 | /* Start delegation functions */ |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 60 | psa_status_t psa_driver_wrapper_sign_hash( |
| 61 | const psa_key_attributes_t *attributes, |
| 62 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 63 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 64 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 65 | { |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 66 | (void)key_buffer_size; |
| 67 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 68 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 69 | /* Try dynamically-registered SE interface first */ |
| 70 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 71 | const psa_drv_se_t *drv; |
| 72 | psa_drv_se_context_t *drv_context; |
| 73 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 74 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 75 | { |
| 76 | if( drv->asymmetric == NULL || |
| 77 | drv->asymmetric->p_sign == NULL ) |
| 78 | { |
| 79 | /* Key is defined in SE, but we have no way to exercise it */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 80 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 81 | } |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 82 | return( drv->asymmetric->p_sign( |
| 83 | drv_context, *( (psa_key_slot_number_t *)key_buffer ), |
| 84 | alg, hash, hash_length, |
| 85 | signature, signature_size, signature_length ) ); |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 86 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 87 | #endif /* PSA_CRYPTO_SE_C */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 88 | |
| 89 | /* Then try accelerator API */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 90 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 91 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 92 | psa_key_location_t location = |
| 93 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 94 | |
| 95 | switch( location ) |
| 96 | { |
| 97 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 98 | /* Key is stored in the slot in export representation, so |
| 99 | * cycle through all known transparent accelerators */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 100 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 101 | status = test_transparent_signature_sign_hash( attributes, |
| 102 | key_buffer, |
| 103 | key_buffer_size, |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 104 | alg, |
| 105 | hash, |
| 106 | hash_length, |
| 107 | signature, |
| 108 | signature_size, |
| 109 | signature_length ); |
| 110 | /* Declared with fallback == true */ |
| 111 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 112 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 113 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 114 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 115 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 116 | /* Add cases for opaque driver here */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 117 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 118 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 119 | return( test_opaque_signature_sign_hash( attributes, |
| 120 | key_buffer, |
| 121 | key_buffer_size, |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 122 | alg, |
| 123 | hash, |
| 124 | hash_length, |
| 125 | signature, |
| 126 | signature_size, |
| 127 | signature_length ) ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 128 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 129 | default: |
| 130 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 131 | return( status ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 132 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 133 | #else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 134 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 135 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 136 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 137 | (void)attributes; |
| 138 | (void)key_buffer; |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 139 | (void)alg; |
| 140 | (void)hash; |
| 141 | (void)hash_length; |
| 142 | (void)signature; |
| 143 | (void)signature_size; |
| 144 | (void)signature_length; |
| 145 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 146 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 147 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 150 | psa_status_t psa_driver_wrapper_verify_hash( |
| 151 | const psa_key_attributes_t *attributes, |
| 152 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 153 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 154 | const uint8_t *signature, size_t signature_length ) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 155 | { |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 156 | (void)key_buffer_size; |
| 157 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 158 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 159 | /* Try dynamically-registered SE interface first */ |
| 160 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 161 | const psa_drv_se_t *drv; |
| 162 | psa_drv_se_context_t *drv_context; |
| 163 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 164 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 165 | { |
| 166 | if( drv->asymmetric == NULL || |
| 167 | drv->asymmetric->p_verify == NULL ) |
| 168 | { |
| 169 | /* Key is defined in SE, but we have no way to exercise it */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 170 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 171 | } |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 172 | return( drv->asymmetric->p_verify( |
| 173 | drv_context, *( (psa_key_slot_number_t *)key_buffer ), |
| 174 | alg, hash, hash_length, |
| 175 | signature, signature_length ) ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 176 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 177 | #endif /* PSA_CRYPTO_SE_C */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 178 | |
| 179 | /* Then try accelerator API */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 180 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 181 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 182 | psa_key_location_t location = |
| 183 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 184 | |
| 185 | switch( location ) |
| 186 | { |
| 187 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 188 | /* Key is stored in the slot in export representation, so |
| 189 | * cycle through all known transparent accelerators */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 190 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 191 | status = test_transparent_signature_verify_hash( attributes, |
| 192 | key_buffer, |
| 193 | key_buffer_size, |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 194 | alg, |
| 195 | hash, |
| 196 | hash_length, |
| 197 | signature, |
| 198 | signature_length ); |
| 199 | /* Declared with fallback == true */ |
| 200 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 201 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 202 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 203 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 204 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 205 | /* Add cases for opaque driver here */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 206 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 207 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 208 | return( test_opaque_signature_verify_hash( attributes, |
| 209 | key_buffer, |
| 210 | key_buffer_size, |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 211 | alg, |
| 212 | hash, |
| 213 | hash_length, |
| 214 | signature, |
| 215 | signature_length ) ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 216 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 217 | default: |
| 218 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 219 | return( status ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 220 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 221 | #else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 222 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 223 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 224 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame^] | 225 | (void)attributes; |
| 226 | (void)key_buffer; |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 227 | (void)alg; |
| 228 | (void)hash; |
| 229 | (void)hash_length; |
| 230 | (void)signature; |
| 231 | (void)signature_length; |
| 232 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 233 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 234 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 237 | /** Get the key buffer size for the key material of a generated key in the |
| 238 | * case of an opaque driver without storage. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 239 | * |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 240 | * \param[in] attributes The key attributes. |
| 241 | * \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] | 242 | * |
| 243 | * \retval #PSA_SUCCESS |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 244 | * The minimum size for a buffer to contain the key material has been |
| 245 | * returned successfully. |
| 246 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 247 | * The size in bits of the key is not valid. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 248 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 249 | * The type and/or the size in bits of the key or the combination of |
| 250 | * the two is not supported. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 251 | */ |
Ronald Cron | 9df74be | 2020-12-05 19:15:23 +0100 | [diff] [blame] | 252 | psa_status_t psa_driver_wrapper_get_key_buffer_size( |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 253 | const psa_key_attributes_t *attributes, |
| 254 | size_t *key_buffer_size ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 255 | { |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 256 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 257 | psa_key_type_t key_type = attributes->core.type; |
| 258 | size_t key_bits = attributes->core.bits; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 259 | |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 260 | *key_buffer_size = 0; |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 261 | switch( location ) |
| 262 | { |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 263 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 264 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
John Durkop | ac93e3b | 2020-10-16 06:48:55 -0700 | [diff] [blame] | 265 | #ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 266 | *key_buffer_size = test_size_function( key_type, key_bits ); |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 267 | return( PSA_SUCCESS ); |
| 268 | #else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */ |
| 269 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) ) |
| 270 | { |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 271 | int public_key_overhead = |
| 272 | ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ? |
| 273 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 ); |
| 274 | *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE |
John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 275 | + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE |
| 276 | + public_key_overhead; |
| 277 | } |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 278 | else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) ) |
John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 279 | { |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 280 | *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 281 | + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE; |
| 282 | } |
John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 283 | else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) && |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 284 | !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) ) |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 285 | { |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 286 | *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 287 | + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR |
| 288 | * ( ( key_bits + 7 ) / 8 ); |
| 289 | } |
| 290 | else |
| 291 | { |
| 292 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 293 | } |
| 294 | return( PSA_SUCCESS ); |
| 295 | #endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */ |
| 296 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 297 | |
| 298 | default: |
Ronald Cron | 9df74be | 2020-12-05 19:15:23 +0100 | [diff] [blame] | 299 | (void)key_type; |
| 300 | (void)key_bits; |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 301 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 302 | } |
| 303 | } |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 304 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 305 | psa_status_t psa_driver_wrapper_generate_key( |
| 306 | const psa_key_attributes_t *attributes, |
| 307 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 308 | { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 309 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 310 | psa_key_location_t location = |
| 311 | PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime); |
| 312 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 313 | /* Try dynamically-registered SE interface first */ |
| 314 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 315 | const psa_drv_se_t *drv; |
| 316 | psa_drv_se_context_t *drv_context; |
| 317 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 318 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 319 | { |
| 320 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 321 | if( drv->key_management == NULL || |
| 322 | drv->key_management->p_generate == NULL ) |
| 323 | { |
| 324 | /* Key is defined as being in SE, but we have no way to generate it */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 325 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 326 | } |
| 327 | return( drv->key_management->p_generate( |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 328 | drv_context, |
| 329 | *( (psa_key_slot_number_t *)key_buffer ), |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 330 | attributes, NULL, 0, &pubkey_length ) ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 331 | } |
| 332 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 333 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 334 | switch( location ) |
| 335 | { |
| 336 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 337 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 338 | /* Transparent drivers are limited to generating asymmetric keys */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 339 | if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 340 | { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 341 | /* Cycle through all known transparent accelerators */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 342 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 343 | status = test_transparent_generate_key( |
| 344 | attributes, key_buffer, key_buffer_size, |
| 345 | key_buffer_length ); |
| 346 | /* Declared with fallback == true */ |
| 347 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 348 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 349 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 350 | } |
| 351 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 352 | |
| 353 | /* Software fallback */ |
| 354 | status = psa_generate_key_internal( |
| 355 | attributes, key_buffer, key_buffer_size, key_buffer_length ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 356 | break; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 357 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 358 | /* Add cases for opaque driver here */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 359 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 360 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 361 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 362 | status = test_opaque_generate_key( |
| 363 | attributes, key_buffer, key_buffer_size, key_buffer_length ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 364 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 365 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 366 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 367 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 368 | default: |
| 369 | /* Key is declared with a lifetime not known to us */ |
| 370 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 371 | break; |
| 372 | } |
| 373 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 374 | return( status ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 377 | psa_status_t psa_driver_wrapper_import_key( |
| 378 | const psa_key_attributes_t *attributes, |
| 379 | const uint8_t *data, |
| 380 | size_t data_length, |
| 381 | uint8_t *key_buffer, |
| 382 | size_t key_buffer_size, |
| 383 | size_t *key_buffer_length, |
| 384 | size_t *bits ) |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 385 | { |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 386 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 387 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 388 | psa_get_key_lifetime( attributes ) ); |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 389 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 390 | /* Try dynamically-registered SE interface first */ |
| 391 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 392 | const psa_drv_se_t *drv; |
| 393 | psa_drv_se_context_t *drv_context; |
| 394 | |
| 395 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 396 | { |
| 397 | if( drv->key_management == NULL || |
| 398 | drv->key_management->p_import == NULL ) |
| 399 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 400 | |
| 401 | /* The driver should set the number of key bits, however in |
| 402 | * case it doesn't, we initialize bits to an invalid value. */ |
| 403 | *bits = PSA_MAX_KEY_BITS + 1; |
| 404 | status = drv->key_management->p_import( |
| 405 | drv_context, |
| 406 | *( (psa_key_slot_number_t *)key_buffer ), |
| 407 | attributes, data, data_length, bits ); |
| 408 | |
| 409 | if( status != PSA_SUCCESS ) |
| 410 | return( status ); |
| 411 | |
| 412 | if( (*bits) > PSA_MAX_KEY_BITS ) |
| 413 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 414 | |
| 415 | return( PSA_SUCCESS ); |
| 416 | } |
| 417 | #endif /* PSA_CRYPTO_SE_C */ |
| 418 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 419 | switch( location ) |
| 420 | { |
| 421 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 422 | /* Key is stored in the slot in export representation, so |
| 423 | * cycle through all known transparent accelerators */ |
| 424 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 425 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 426 | status = test_transparent_import_key( attributes, |
| 427 | data, data_length, |
| 428 | key_buffer, key_buffer_size, |
| 429 | key_buffer_length, bits ); |
| 430 | /* Declared with fallback == true */ |
| 431 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 432 | return( status ); |
| 433 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 434 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 435 | /* Fell through, meaning no accelerator supports this operation */ |
| 436 | return( psa_import_key_into_slot( attributes, |
| 437 | data, data_length, |
| 438 | key_buffer, key_buffer_size, |
| 439 | key_buffer_length, bits ) ); |
| 440 | |
| 441 | default: |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 442 | /* Importing a key with external storage in not yet supported. |
| 443 | * Return in error indicating that the lifetime is not valid. */ |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 444 | (void)status; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 445 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 450 | psa_status_t psa_driver_wrapper_export_key( |
| 451 | const psa_key_attributes_t *attributes, |
| 452 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 453 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 454 | |
| 455 | { |
| 456 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 457 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 458 | psa_get_key_lifetime( attributes ) ); |
| 459 | |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 460 | /* Try dynamically-registered SE interface first */ |
| 461 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 462 | const psa_drv_se_t *drv; |
| 463 | psa_drv_se_context_t *drv_context; |
| 464 | |
| 465 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 466 | { |
| 467 | if( ( drv->key_management == NULL ) || |
| 468 | ( drv->key_management->p_export == NULL ) ) |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 469 | { |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 470 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 471 | } |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 472 | |
| 473 | return( drv->key_management->p_export( |
| 474 | drv_context, |
| 475 | *( (psa_key_slot_number_t *)key_buffer ), |
| 476 | data, data_size, data_length ) ); |
| 477 | } |
| 478 | #endif /* PSA_CRYPTO_SE_C */ |
| 479 | |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 480 | switch( location ) |
| 481 | { |
| 482 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 483 | return( psa_export_key_internal( attributes, |
| 484 | key_buffer, |
| 485 | key_buffer_size, |
| 486 | data, |
| 487 | data_size, |
| 488 | data_length ) ); |
| 489 | |
| 490 | /* Add cases for opaque driver here */ |
| 491 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 492 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 493 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 494 | return( test_opaque_export_key( attributes, |
| 495 | key_buffer, |
| 496 | key_buffer_size, |
| 497 | data, |
| 498 | data_size, |
| 499 | data_length ) ); |
| 500 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 501 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 502 | default: |
| 503 | /* Key is declared with a lifetime not known to us */ |
| 504 | return( status ); |
| 505 | } |
| 506 | } |
| 507 | |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 508 | psa_status_t psa_driver_wrapper_export_public_key( |
| 509 | const psa_key_attributes_t *attributes, |
| 510 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 511 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 512 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 513 | { |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 514 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 515 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 516 | psa_get_key_lifetime( attributes ) ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 517 | |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 518 | /* Try dynamically-registered SE interface first */ |
| 519 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 520 | const psa_drv_se_t *drv; |
| 521 | psa_drv_se_context_t *drv_context; |
| 522 | |
| 523 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 524 | { |
| 525 | if( ( drv->key_management == NULL ) || |
| 526 | ( drv->key_management->p_export_public == NULL ) ) |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 527 | { |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 528 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 529 | } |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 530 | |
| 531 | return( drv->key_management->p_export_public( |
| 532 | drv_context, |
| 533 | *( (psa_key_slot_number_t *)key_buffer ), |
| 534 | data, data_size, data_length ) ); |
| 535 | } |
| 536 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 537 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 538 | switch( location ) |
| 539 | { |
| 540 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 541 | /* Key is stored in the slot in export representation, so |
| 542 | * cycle through all known transparent accelerators */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 543 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 544 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 545 | status = test_transparent_export_public_key( attributes, |
| 546 | key_buffer, |
| 547 | key_buffer_size, |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 548 | data, |
| 549 | data_size, |
| 550 | data_length ); |
| 551 | /* Declared with fallback == true */ |
| 552 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 553 | return( status ); |
| 554 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 555 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 556 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 557 | return( psa_export_public_key_internal( attributes, |
| 558 | key_buffer, |
| 559 | key_buffer_size, |
| 560 | data, |
| 561 | data_size, |
| 562 | data_length ) ); |
| 563 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 564 | /* Add cases for opaque driver here */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 565 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 566 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 567 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 568 | return( test_opaque_export_public_key( attributes, |
| 569 | key_buffer, |
| 570 | key_buffer_size, |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 571 | data, |
| 572 | data_size, |
| 573 | data_length ) ); |
| 574 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 575 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 576 | default: |
| 577 | /* Key is declared with a lifetime not known to us */ |
| 578 | return( status ); |
| 579 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 580 | } |
| 581 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 582 | /* |
| 583 | * Cipher functions |
| 584 | */ |
| 585 | psa_status_t psa_driver_wrapper_cipher_encrypt( |
| 586 | psa_key_slot_t *slot, |
| 587 | psa_algorithm_t alg, |
| 588 | const uint8_t *input, |
| 589 | size_t input_length, |
| 590 | uint8_t *output, |
| 591 | size_t output_size, |
| 592 | size_t *output_length ) |
| 593 | { |
| 594 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 595 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 596 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 597 | psa_key_attributes_t attributes = { |
| 598 | .core = slot->attr |
| 599 | }; |
| 600 | |
| 601 | switch( location ) |
| 602 | { |
| 603 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 604 | /* Key is stored in the slot in export representation, so |
| 605 | * cycle through all known transparent accelerators */ |
| 606 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 607 | status = test_transparent_cipher_encrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 608 | slot->key.data, |
| 609 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 610 | alg, |
| 611 | input, |
| 612 | input_length, |
| 613 | output, |
| 614 | output_size, |
| 615 | output_length ); |
| 616 | /* Declared with fallback == true */ |
| 617 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 618 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 619 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 620 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 621 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 622 | /* Add cases for opaque driver here */ |
| 623 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 624 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 625 | return( test_opaque_cipher_encrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 626 | slot->key.data, |
| 627 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 628 | alg, |
| 629 | input, |
| 630 | input_length, |
| 631 | output, |
| 632 | output_size, |
| 633 | output_length ) ); |
| 634 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 635 | default: |
| 636 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 637 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 638 | } |
| 639 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 640 | (void) slot; |
| 641 | (void) alg; |
| 642 | (void) input; |
| 643 | (void) input_length; |
| 644 | (void) output; |
| 645 | (void) output_size; |
| 646 | (void) output_length; |
| 647 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 648 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 649 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 650 | } |
| 651 | |
| 652 | psa_status_t psa_driver_wrapper_cipher_decrypt( |
| 653 | psa_key_slot_t *slot, |
| 654 | psa_algorithm_t alg, |
| 655 | const uint8_t *input, |
| 656 | size_t input_length, |
| 657 | uint8_t *output, |
| 658 | size_t output_size, |
| 659 | size_t *output_length ) |
| 660 | { |
| 661 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 662 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 663 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 664 | psa_key_attributes_t attributes = { |
| 665 | .core = slot->attr |
| 666 | }; |
| 667 | |
| 668 | switch( location ) |
| 669 | { |
| 670 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 671 | /* Key is stored in the slot in export representation, so |
| 672 | * cycle through all known transparent accelerators */ |
| 673 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 674 | status = test_transparent_cipher_decrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 675 | slot->key.data, |
| 676 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 677 | alg, |
| 678 | input, |
| 679 | input_length, |
| 680 | output, |
| 681 | output_size, |
| 682 | output_length ); |
| 683 | /* Declared with fallback == true */ |
| 684 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 685 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 686 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 687 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 688 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 689 | /* Add cases for opaque driver here */ |
| 690 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 691 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 692 | return( test_opaque_cipher_decrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 693 | slot->key.data, |
| 694 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 695 | alg, |
| 696 | input, |
| 697 | input_length, |
| 698 | output, |
| 699 | output_size, |
| 700 | output_length ) ); |
| 701 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 702 | default: |
| 703 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 704 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 705 | } |
| 706 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 707 | (void) slot; |
| 708 | (void) alg; |
| 709 | (void) input; |
| 710 | (void) input_length; |
| 711 | (void) output; |
| 712 | (void) output_size; |
| 713 | (void) output_length; |
| 714 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 715 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 716 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 717 | } |
| 718 | |
| 719 | psa_status_t psa_driver_wrapper_cipher_encrypt_setup( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 720 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 721 | psa_key_slot_t *slot, |
| 722 | psa_algorithm_t alg ) |
| 723 | { |
| 724 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 725 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 726 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 727 | psa_key_attributes_t attributes = { |
| 728 | .core = slot->attr |
| 729 | }; |
| 730 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 731 | switch( location ) |
| 732 | { |
| 733 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 734 | /* Key is stored in the slot in export representation, so |
| 735 | * cycle through all known transparent accelerators */ |
| 736 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 737 | operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) ); |
| 738 | if( operation->ctx == NULL ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 739 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 740 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 741 | status = test_transparent_cipher_encrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 742 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 743 | slot->key.data, |
| 744 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 745 | alg ); |
| 746 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 747 | if( status == PSA_SUCCESS ) |
| 748 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 749 | else |
| 750 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 751 | mbedtls_platform_zeroize( |
| 752 | operation->ctx, |
| 753 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 754 | mbedtls_free( operation->ctx ); |
| 755 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 756 | } |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 757 | |
| 758 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 759 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 760 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 761 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 762 | /* Add cases for opaque driver here */ |
| 763 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 764 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 765 | operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) ); |
| 766 | if( operation->ctx == NULL ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 767 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 768 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 769 | status = test_opaque_cipher_encrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 770 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 771 | slot->key.data, |
| 772 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 773 | alg ); |
| 774 | if( status == PSA_SUCCESS ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 775 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 776 | else |
| 777 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 778 | mbedtls_platform_zeroize( |
| 779 | operation->ctx, |
| 780 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 781 | mbedtls_free( operation->ctx ); |
| 782 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 783 | } |
| 784 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 785 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 786 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 787 | default: |
| 788 | /* Key is declared with a lifetime not known to us */ |
John Durkop | 714e3a1 | 2020-09-29 22:07:04 -0700 | [diff] [blame] | 789 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 790 | } |
| 791 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 792 | (void)slot; |
| 793 | (void)alg; |
| 794 | (void)operation; |
| 795 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 796 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 797 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 798 | } |
| 799 | |
| 800 | psa_status_t psa_driver_wrapper_cipher_decrypt_setup( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 801 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 802 | psa_key_slot_t *slot, |
| 803 | psa_algorithm_t alg ) |
| 804 | { |
| 805 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 806 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 807 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 808 | psa_key_attributes_t attributes = { |
| 809 | .core = slot->attr |
| 810 | }; |
| 811 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 812 | switch( location ) |
| 813 | { |
| 814 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 815 | /* Key is stored in the slot in export representation, so |
| 816 | * cycle through all known transparent accelerators */ |
| 817 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 818 | operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) ); |
| 819 | if( operation->ctx == NULL ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 820 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 821 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 822 | status = test_transparent_cipher_decrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 823 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 824 | slot->key.data, |
| 825 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 826 | alg ); |
| 827 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 828 | if( status == PSA_SUCCESS ) |
| 829 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 830 | else |
| 831 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 832 | mbedtls_platform_zeroize( |
| 833 | operation->ctx, |
| 834 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 835 | mbedtls_free( operation->ctx ); |
| 836 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 837 | } |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 838 | |
| 839 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 840 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 841 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 842 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 843 | /* Add cases for opaque driver here */ |
| 844 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 845 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 846 | operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) ); |
| 847 | if( operation->ctx == NULL ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 848 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 849 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 850 | status = test_opaque_cipher_decrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 851 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 852 | slot->key.data, |
| 853 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 854 | alg ); |
| 855 | if( status == PSA_SUCCESS ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 856 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 857 | else |
| 858 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 859 | mbedtls_platform_zeroize( |
| 860 | operation->ctx, |
| 861 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 862 | mbedtls_free( operation->ctx ); |
| 863 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 864 | } |
| 865 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 866 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 867 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 868 | default: |
| 869 | /* Key is declared with a lifetime not known to us */ |
John Durkop | 814dca7 | 2020-10-05 06:31:12 -0700 | [diff] [blame] | 870 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 871 | } |
| 872 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 873 | (void)slot; |
| 874 | (void)alg; |
| 875 | (void)operation; |
| 876 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 877 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 878 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 879 | } |
| 880 | |
| 881 | psa_status_t psa_driver_wrapper_cipher_generate_iv( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 882 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 883 | uint8_t *iv, |
| 884 | size_t iv_size, |
| 885 | size_t *iv_length ) |
| 886 | { |
| 887 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 888 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 889 | { |
| 890 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 891 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 892 | return( test_transparent_cipher_generate_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 893 | iv, |
| 894 | iv_size, |
| 895 | iv_length ) ); |
| 896 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 897 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 898 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 899 | return( test_opaque_cipher_generate_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 900 | iv, |
| 901 | iv_size, |
| 902 | iv_length ) ); |
| 903 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 904 | default: |
| 905 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 906 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 907 | } |
| 908 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 909 | (void) operation; |
| 910 | (void) iv; |
| 911 | (void) iv_size; |
| 912 | (void) iv_length; |
| 913 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 914 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 915 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 916 | } |
| 917 | |
| 918 | psa_status_t psa_driver_wrapper_cipher_set_iv( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 919 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 920 | const uint8_t *iv, |
| 921 | size_t iv_length ) |
| 922 | { |
| 923 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 924 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 925 | { |
| 926 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 927 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 928 | return( test_transparent_cipher_set_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 929 | iv, |
| 930 | iv_length ) ); |
| 931 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 932 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 933 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 934 | return( test_opaque_cipher_set_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 935 | iv, |
| 936 | iv_length ) ); |
| 937 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 938 | default: |
| 939 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 940 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 941 | } |
| 942 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 943 | (void) operation; |
| 944 | (void) iv; |
| 945 | (void) iv_length; |
| 946 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 947 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 948 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 949 | } |
| 950 | |
| 951 | psa_status_t psa_driver_wrapper_cipher_update( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 952 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 953 | const uint8_t *input, |
| 954 | size_t input_length, |
| 955 | uint8_t *output, |
| 956 | size_t output_size, |
| 957 | size_t *output_length ) |
| 958 | { |
| 959 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 960 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 961 | { |
| 962 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 963 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 964 | return( test_transparent_cipher_update( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 965 | input, |
| 966 | input_length, |
| 967 | output, |
| 968 | output_size, |
| 969 | output_length ) ); |
| 970 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 971 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 972 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 973 | return( test_opaque_cipher_update( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 974 | input, |
| 975 | input_length, |
| 976 | output, |
| 977 | output_size, |
| 978 | output_length ) ); |
| 979 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 980 | default: |
| 981 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 982 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 983 | } |
| 984 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 985 | (void) operation; |
| 986 | (void) input; |
| 987 | (void) input_length; |
| 988 | (void) output; |
| 989 | (void) output_length; |
| 990 | (void) output_size; |
| 991 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 992 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 993 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 994 | } |
| 995 | |
| 996 | psa_status_t psa_driver_wrapper_cipher_finish( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 997 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 998 | uint8_t *output, |
| 999 | size_t output_size, |
| 1000 | size_t *output_length ) |
| 1001 | { |
| 1002 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1003 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1004 | { |
| 1005 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1006 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1007 | return( test_transparent_cipher_finish( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1008 | output, |
| 1009 | output_size, |
| 1010 | output_length ) ); |
| 1011 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1012 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1013 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1014 | return( test_opaque_cipher_finish( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1015 | output, |
| 1016 | output_size, |
| 1017 | output_length ) ); |
| 1018 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1019 | default: |
| 1020 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1021 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1022 | } |
| 1023 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1024 | (void) operation; |
| 1025 | (void) output; |
| 1026 | (void) output_size; |
| 1027 | (void) output_length; |
| 1028 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1029 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1030 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1031 | } |
| 1032 | |
| 1033 | psa_status_t psa_driver_wrapper_cipher_abort( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1034 | psa_operation_driver_context_t *operation ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1035 | { |
| 1036 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1037 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1038 | |
| 1039 | /* The object has (apparently) been initialized but it is not in use. It's |
| 1040 | * ok to call abort on such an object, and there's nothing to do. */ |
| 1041 | if( operation->ctx == NULL && operation->id == 0 ) |
| 1042 | return( PSA_SUCCESS ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1043 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1044 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1045 | { |
| 1046 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1047 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1048 | status = test_transparent_cipher_abort( operation->ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1049 | mbedtls_platform_zeroize( |
| 1050 | operation->ctx, |
| 1051 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1052 | mbedtls_free( operation->ctx ); |
| 1053 | operation->ctx = NULL; |
| 1054 | operation->id = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1055 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1056 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1057 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1058 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1059 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1060 | status = test_opaque_cipher_abort( operation->ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1061 | mbedtls_platform_zeroize( |
| 1062 | operation->ctx, |
| 1063 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1064 | mbedtls_free( operation->ctx ); |
| 1065 | operation->ctx = NULL; |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1066 | operation->id = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1067 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1068 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1069 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1070 | default: |
| 1071 | /* Operation is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1072 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1073 | } |
| 1074 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1075 | (void)operation; |
| 1076 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1077 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1078 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1079 | } |
| 1080 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 1081 | /* End of automatically generated file. */ |