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