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