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