Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Test driver for cipher functions. |
| 3 | * Currently only supports multi-part operations using AES-CTR. |
| 4 | */ |
Steven Cooreman | 3ec4018 | 2020-09-02 16:27:46 +0200 | [diff] [blame] | 5 | /* Copyright The Mbed TLS Contributors |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 22 | #include "mbedtls/config.h" |
| 23 | #else |
| 24 | #include MBEDTLS_CONFIG_FILE |
| 25 | #endif |
| 26 | |
| 27 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST) |
| 28 | #include "psa/crypto.h" |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame] | 29 | #include "psa_crypto_cipher.h" |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 30 | #include "psa_crypto_core.h" |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 31 | #include "mbedtls/cipher.h" |
| 32 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 33 | #include "test/drivers/cipher.h" |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 34 | |
| 35 | #include "test/random.h" |
| 36 | |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 37 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) |
| 38 | #include "libtestdriver1/library/psa_crypto_cipher.h" |
| 39 | #endif |
| 40 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 41 | #include <string.h> |
| 42 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 43 | mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks = |
| 44 | MBEDTLS_TEST_DRIVER_CIPHER_INIT; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 45 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 46 | psa_status_t mbedtls_test_transparent_cipher_encrypt( |
Steven Cooreman | fe0ab55 | 2020-09-10 13:07:02 +0200 | [diff] [blame] | 47 | const psa_key_attributes_t *attributes, |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 48 | const uint8_t *key_buffer, |
| 49 | size_t key_buffer_size, |
Steven Cooreman | fe0ab55 | 2020-09-10 13:07:02 +0200 | [diff] [blame] | 50 | psa_algorithm_t alg, |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 51 | const uint8_t *iv, |
| 52 | size_t iv_length, |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 53 | const uint8_t *input, |
| 54 | size_t input_length, |
| 55 | uint8_t *output, |
| 56 | size_t output_size, |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 57 | size_t *output_length) |
Steven Cooreman | fe0ab55 | 2020-09-10 13:07:02 +0200 | [diff] [blame] | 58 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 59 | mbedtls_test_driver_cipher_hooks.hits++; |
Steven Cooreman | fe0ab55 | 2020-09-10 13:07:02 +0200 | [diff] [blame] | 60 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 61 | if (mbedtls_test_driver_cipher_hooks.forced_output != NULL) { |
| 62 | if (output_size < mbedtls_test_driver_cipher_hooks.forced_output_length) { |
| 63 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 64 | } |
Steven Cooreman | fe0ab55 | 2020-09-10 13:07:02 +0200 | [diff] [blame] | 65 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 66 | memcpy(output, |
| 67 | mbedtls_test_driver_cipher_hooks.forced_output, |
| 68 | mbedtls_test_driver_cipher_hooks.forced_output_length); |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 69 | *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length; |
Steven Cooreman | fe0ab55 | 2020-09-10 13:07:02 +0200 | [diff] [blame] | 70 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 71 | return mbedtls_test_driver_cipher_hooks.forced_status; |
Steven Cooreman | fe0ab55 | 2020-09-10 13:07:02 +0200 | [diff] [blame] | 72 | } |
| 73 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 74 | if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) { |
| 75 | return mbedtls_test_driver_cipher_hooks.forced_status; |
| 76 | } |
Steven Cooreman | fe0ab55 | 2020-09-10 13:07:02 +0200 | [diff] [blame] | 77 | |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 78 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 79 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 80 | return libtestdriver1_mbedtls_psa_cipher_encrypt( |
| 81 | (const libtestdriver1_psa_key_attributes_t *) attributes, |
| 82 | key_buffer, key_buffer_size, |
| 83 | alg, iv, iv_length, input, input_length, |
| 84 | output, output_size, output_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 85 | #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 86 | return mbedtls_psa_cipher_encrypt( |
| 87 | attributes, key_buffer, key_buffer_size, |
| 88 | alg, iv, iv_length, input, input_length, |
| 89 | output, output_size, output_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 90 | #endif |
| 91 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 92 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 95 | psa_status_t mbedtls_test_transparent_cipher_decrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 96 | const psa_key_attributes_t *attributes, |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 97 | const uint8_t *key_buffer, |
| 98 | size_t key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 99 | psa_algorithm_t alg, |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 100 | const uint8_t *input, |
| 101 | size_t input_length, |
| 102 | uint8_t *output, |
| 103 | size_t output_size, |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 104 | size_t *output_length) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 105 | { |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 106 | mbedtls_test_driver_cipher_hooks.hits++; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 107 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 108 | if (mbedtls_test_driver_cipher_hooks.forced_output != NULL) { |
| 109 | if (output_size < mbedtls_test_driver_cipher_hooks.forced_output_length) { |
| 110 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 111 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 112 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 113 | memcpy(output, |
| 114 | mbedtls_test_driver_cipher_hooks.forced_output, |
| 115 | mbedtls_test_driver_cipher_hooks.forced_output_length); |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 116 | *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length; |
| 117 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 118 | return mbedtls_test_driver_cipher_hooks.forced_status; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 119 | } |
| 120 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 121 | if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) { |
| 122 | return mbedtls_test_driver_cipher_hooks.forced_status; |
| 123 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 124 | |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 125 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 126 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 127 | return libtestdriver1_mbedtls_psa_cipher_decrypt( |
| 128 | (const libtestdriver1_psa_key_attributes_t *) attributes, |
| 129 | key_buffer, key_buffer_size, |
| 130 | alg, input, input_length, |
| 131 | output, output_size, output_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 132 | #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 133 | return mbedtls_psa_cipher_decrypt( |
| 134 | attributes, key_buffer, key_buffer_size, |
| 135 | alg, input, input_length, |
| 136 | output, output_size, output_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 137 | #endif |
| 138 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 139 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 142 | psa_status_t mbedtls_test_transparent_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 143 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 16afd3d | 2020-09-09 15:36:39 +0200 | [diff] [blame] | 144 | const psa_key_attributes_t *attributes, |
| 145 | const uint8_t *key, size_t key_length, |
| 146 | psa_algorithm_t alg) |
| 147 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 148 | mbedtls_test_driver_cipher_hooks.hits++; |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame] | 149 | |
| 150 | /* Wiping the entire struct here, instead of member-by-member. This is |
| 151 | * useful for the test suite, since it gives a chance of catching memory |
| 152 | * corruption errors should the core not have allocated (enough) memory for |
| 153 | * our context struct. */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 154 | memset(operation, 0, sizeof(*operation)); |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame] | 155 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 156 | if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) { |
| 157 | return mbedtls_test_driver_cipher_hooks.forced_status; |
| 158 | } |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame] | 159 | |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 160 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 161 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 162 | return libtestdriver1_mbedtls_psa_cipher_encrypt_setup( |
| 163 | operation, |
| 164 | (const libtestdriver1_psa_key_attributes_t *) attributes, |
| 165 | key, key_length, alg); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 166 | #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 167 | return mbedtls_psa_cipher_encrypt_setup( |
| 168 | operation, attributes, key, key_length, alg); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 169 | #endif |
| 170 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 171 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 16afd3d | 2020-09-09 15:36:39 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 174 | psa_status_t mbedtls_test_transparent_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 175 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 176 | const psa_key_attributes_t *attributes, |
| 177 | const uint8_t *key, size_t key_length, |
| 178 | psa_algorithm_t alg) |
| 179 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 180 | mbedtls_test_driver_cipher_hooks.hits++; |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame] | 181 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 182 | if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) { |
| 183 | return mbedtls_test_driver_cipher_hooks.forced_status; |
| 184 | } |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame] | 185 | |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 186 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 187 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 188 | return libtestdriver1_mbedtls_psa_cipher_decrypt_setup( |
| 189 | operation, |
| 190 | (const libtestdriver1_psa_key_attributes_t *) attributes, |
| 191 | key, key_length, alg); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 192 | #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 193 | return mbedtls_psa_cipher_decrypt_setup( |
| 194 | operation, attributes, key, key_length, alg); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 195 | #endif |
| 196 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 197 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 200 | psa_status_t mbedtls_test_transparent_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 201 | mbedtls_transparent_test_driver_cipher_operation_t *operation) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 202 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 203 | mbedtls_test_driver_cipher_hooks.hits++; |
Steven Cooreman | 89e54f2 | 2020-09-10 18:07:57 +0200 | [diff] [blame] | 204 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 205 | if (operation->alg == 0) { |
| 206 | return PSA_SUCCESS; |
| 207 | } |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 208 | |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 209 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 210 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 211 | libtestdriver1_mbedtls_psa_cipher_abort(operation); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 212 | #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 213 | mbedtls_psa_cipher_abort(operation); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 214 | #endif |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 215 | |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame] | 216 | /* Wiping the entire struct here, instead of member-by-member. This is |
| 217 | * useful for the test suite, since it gives a chance of catching memory |
| 218 | * corruption errors should the core not have allocated (enough) memory for |
| 219 | * our context struct. */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 220 | memset(operation, 0, sizeof(*operation)); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 221 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 222 | return mbedtls_test_driver_cipher_hooks.forced_status; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 225 | psa_status_t mbedtls_test_transparent_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 226 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 227 | const uint8_t *iv, |
| 228 | size_t iv_length) |
| 229 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 230 | mbedtls_test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 231 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 232 | if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) { |
| 233 | return mbedtls_test_driver_cipher_hooks.forced_status; |
| 234 | } |
Steven Cooreman | 89e54f2 | 2020-09-10 18:07:57 +0200 | [diff] [blame] | 235 | |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 236 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 237 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 238 | return libtestdriver1_mbedtls_psa_cipher_set_iv( |
| 239 | operation, iv, iv_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 240 | #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 241 | return mbedtls_psa_cipher_set_iv(operation, iv, iv_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 242 | #endif |
| 243 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 244 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 247 | psa_status_t mbedtls_test_transparent_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 248 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 249 | const uint8_t *input, |
| 250 | size_t input_length, |
| 251 | uint8_t *output, |
| 252 | size_t output_size, |
| 253 | size_t *output_length) |
| 254 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 255 | mbedtls_test_driver_cipher_hooks.hits++; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 256 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 257 | if (mbedtls_test_driver_cipher_hooks.forced_output != NULL) { |
| 258 | if (output_size < mbedtls_test_driver_cipher_hooks.forced_output_length) { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 259 | return PSA_ERROR_BUFFER_TOO_SMALL; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 260 | } |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 261 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 262 | memcpy(output, |
| 263 | mbedtls_test_driver_cipher_hooks.forced_output, |
| 264 | mbedtls_test_driver_cipher_hooks.forced_output_length); |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 265 | *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length; |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame] | 266 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 267 | return mbedtls_test_driver_cipher_hooks.forced_status; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 268 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 269 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 270 | if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) { |
| 271 | return mbedtls_test_driver_cipher_hooks.forced_status; |
| 272 | } |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame] | 273 | |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 274 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 275 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 276 | return libtestdriver1_mbedtls_psa_cipher_update( |
| 277 | operation, input, input_length, |
| 278 | output, output_size, output_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 279 | #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 280 | return mbedtls_psa_cipher_update( |
| 281 | operation, input, input_length, |
| 282 | output, output_size, output_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 283 | #endif |
| 284 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 285 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 288 | psa_status_t mbedtls_test_transparent_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 289 | mbedtls_transparent_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 290 | uint8_t *output, |
| 291 | size_t output_size, |
| 292 | size_t *output_length) |
| 293 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 294 | mbedtls_test_driver_cipher_hooks.hits++; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 295 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 296 | if (mbedtls_test_driver_cipher_hooks.forced_output != NULL) { |
| 297 | if (output_size < mbedtls_test_driver_cipher_hooks.forced_output_length) { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 298 | return PSA_ERROR_BUFFER_TOO_SMALL; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 299 | } |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 300 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 301 | memcpy(output, |
| 302 | mbedtls_test_driver_cipher_hooks.forced_output, |
| 303 | mbedtls_test_driver_cipher_hooks.forced_output_length); |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 304 | *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length; |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame] | 305 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 306 | return mbedtls_test_driver_cipher_hooks.forced_status; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 307 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 308 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 309 | if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) { |
| 310 | return mbedtls_test_driver_cipher_hooks.forced_status; |
| 311 | } |
Ronald Cron | 8d310ad | 2020-12-15 15:17:20 +0100 | [diff] [blame] | 312 | |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 313 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 314 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 315 | return libtestdriver1_mbedtls_psa_cipher_finish( |
| 316 | operation, output, output_size, output_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 317 | #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 318 | return mbedtls_psa_cipher_finish( |
| 319 | operation, output, output_size, output_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 320 | #endif |
| 321 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 322 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* |
| 326 | * opaque versions, to do |
| 327 | */ |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 328 | psa_status_t mbedtls_test_opaque_cipher_encrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 329 | const psa_key_attributes_t *attributes, |
| 330 | const uint8_t *key, size_t key_length, |
| 331 | psa_algorithm_t alg, |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 332 | const uint8_t *iv, size_t iv_length, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 333 | const uint8_t *input, size_t input_length, |
| 334 | uint8_t *output, size_t output_size, size_t *output_length) |
| 335 | { |
| 336 | (void) attributes; |
| 337 | (void) key; |
| 338 | (void) key_length; |
| 339 | (void) alg; |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 340 | (void) iv; |
| 341 | (void) iv_length; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 342 | (void) input; |
| 343 | (void) input_length; |
| 344 | (void) output; |
| 345 | (void) output_size; |
| 346 | (void) output_length; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 347 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 350 | psa_status_t mbedtls_test_opaque_cipher_decrypt( |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 351 | const psa_key_attributes_t *attributes, |
| 352 | const uint8_t *key, size_t key_length, |
| 353 | psa_algorithm_t alg, |
| 354 | const uint8_t *input, size_t input_length, |
| 355 | uint8_t *output, size_t output_size, size_t *output_length) |
| 356 | { |
| 357 | (void) attributes; |
| 358 | (void) key; |
| 359 | (void) key_length; |
| 360 | (void) alg; |
| 361 | (void) input; |
| 362 | (void) input_length; |
| 363 | (void) output; |
| 364 | (void) output_size; |
| 365 | (void) output_length; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 366 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 369 | psa_status_t mbedtls_test_opaque_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 370 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 371 | const psa_key_attributes_t *attributes, |
| 372 | const uint8_t *key, size_t key_length, |
| 373 | psa_algorithm_t alg) |
| 374 | { |
| 375 | (void) operation; |
| 376 | (void) attributes; |
| 377 | (void) key; |
| 378 | (void) key_length; |
| 379 | (void) alg; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 380 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 383 | psa_status_t mbedtls_test_opaque_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 384 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 385 | const psa_key_attributes_t *attributes, |
| 386 | const uint8_t *key, size_t key_length, |
| 387 | psa_algorithm_t alg) |
| 388 | { |
| 389 | (void) operation; |
| 390 | (void) attributes; |
| 391 | (void) key; |
| 392 | (void) key_length; |
| 393 | (void) alg; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 394 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 397 | psa_status_t mbedtls_test_opaque_cipher_abort( |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 398 | mbedtls_opaque_test_driver_cipher_operation_t *operation) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 399 | { |
| 400 | (void) operation; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 401 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 404 | psa_status_t mbedtls_test_opaque_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 405 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 406 | const uint8_t *iv, |
| 407 | size_t iv_length) |
| 408 | { |
| 409 | (void) operation; |
| 410 | (void) iv; |
| 411 | (void) iv_length; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 412 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 415 | psa_status_t mbedtls_test_opaque_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 416 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 417 | const uint8_t *input, |
| 418 | size_t input_length, |
| 419 | uint8_t *output, |
| 420 | size_t output_size, |
| 421 | size_t *output_length) |
| 422 | { |
| 423 | (void) operation; |
| 424 | (void) input; |
| 425 | (void) input_length; |
| 426 | (void) output; |
| 427 | (void) output_size; |
| 428 | (void) output_length; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 429 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 430 | } |
| 431 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 432 | psa_status_t mbedtls_test_opaque_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 433 | mbedtls_opaque_test_driver_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 434 | uint8_t *output, |
| 435 | size_t output_size, |
| 436 | size_t *output_length) |
| 437 | { |
| 438 | (void) operation; |
| 439 | (void) output; |
| 440 | (void) output_size; |
| 441 | (void) output_length; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 442 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 443 | } |
| 444 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |