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, |
| 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 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 61 | if( mbedtls_test_driver_cipher_hooks.forced_output != NULL ) |
Steven Cooreman | fe0ab55 | 2020-09-10 13:07:02 +0200 | [diff] [blame] | 62 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 63 | if( output_size < mbedtls_test_driver_cipher_hooks.forced_output_length ) |
Steven Cooreman | fe0ab55 | 2020-09-10 13:07:02 +0200 | [diff] [blame] | 64 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 65 | |
| 66 | memcpy( output, |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 67 | mbedtls_test_driver_cipher_hooks.forced_output, |
| 68 | mbedtls_test_driver_cipher_hooks.forced_output_length ); |
| 69 | *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length; |
Steven Cooreman | fe0ab55 | 2020-09-10 13:07:02 +0200 | [diff] [blame] | 70 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [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 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 74 | if( mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS ) |
| 75 | return( mbedtls_test_driver_cipher_hooks.forced_status ); |
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, |
Dave Rodgman | 08412e2 | 2021-12-14 12:52:51 +0000 | [diff] [blame] | 82 | alg, iv, iv_length, input, input_length, |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 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, |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 87 | alg, iv, iv_length, input, input_length, |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 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, |
Ronald Cron | a833169 | 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 | a833169 | 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; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 339 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Ronald Cron | c4bc12e | 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; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 358 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Ronald Cron | c4bc12e | 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; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 372 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Ronald Cron | c4bc12e | 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; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 386 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 387 | } |
| 388 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 389 | psa_status_t mbedtls_test_opaque_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +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; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 393 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Ronald Cron | c4bc12e | 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; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 404 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Ronald Cron | c4bc12e | 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; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 421 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Ronald Cron | c4bc12e | 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; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 434 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 435 | } |
| 436 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |