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" |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 29 | #include "psa_crypto_core.h" |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 30 | #include "mbedtls/cipher.h" |
| 31 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 32 | #include "test/drivers/cipher.h" |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 33 | |
| 34 | #include "test/random.h" |
| 35 | |
| 36 | #include <string.h> |
| 37 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 38 | /* Test driver implements AES-CTR only. Its default behaviour (when its return |
| 39 | * status is not overridden through the hooks) is to take care of all AES-CTR |
| 40 | * operations, and return PSA_ERROR_NOT_SUPPORTED for all others. |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 41 | * Set test_driver_cipher_hooks.forced_status to PSA_ERROR_NOT_SUPPORTED to use |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 42 | * fallback even for AES-CTR. */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 43 | test_driver_cipher_hooks_t test_driver_cipher_hooks = TEST_DRIVER_CIPHER_INIT; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 44 | |
| 45 | psa_status_t test_transparent_cipher_encrypt( |
| 46 | const psa_key_attributes_t *attributes, |
| 47 | const uint8_t *key, size_t key_length, |
| 48 | psa_algorithm_t alg, |
| 49 | const uint8_t *input, size_t input_length, |
| 50 | uint8_t *output, size_t output_size, size_t *output_length) |
| 51 | { |
| 52 | (void) attributes; |
| 53 | (void) key; |
| 54 | (void) key_length; |
| 55 | (void) alg; |
| 56 | (void) input; |
| 57 | (void) input_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 58 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 59 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 60 | if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS ) |
| 61 | return( test_driver_cipher_hooks.forced_status ); |
| 62 | if( output_size < test_driver_cipher_hooks.forced_output_length ) |
| 63 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 64 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 65 | memcpy( output, |
| 66 | test_driver_cipher_hooks.forced_output, |
| 67 | test_driver_cipher_hooks.forced_output_length ); |
| 68 | *output_length = test_driver_cipher_hooks.forced_output_length; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 69 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 70 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | psa_status_t test_transparent_cipher_decrypt( |
| 74 | const psa_key_attributes_t *attributes, |
| 75 | const uint8_t *key, size_t key_length, |
| 76 | psa_algorithm_t alg, |
| 77 | const uint8_t *input, size_t input_length, |
| 78 | uint8_t *output, size_t output_size, size_t *output_length) |
| 79 | { |
| 80 | (void) attributes; |
| 81 | (void) key; |
| 82 | (void) key_length; |
| 83 | (void) alg; |
| 84 | (void) input; |
| 85 | (void) input_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 86 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 87 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 88 | if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS ) |
| 89 | return( test_driver_cipher_hooks.forced_status ); |
| 90 | if( output_size < test_driver_cipher_hooks.forced_output_length ) |
| 91 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 92 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 93 | memcpy( output, |
| 94 | test_driver_cipher_hooks.forced_output, |
| 95 | test_driver_cipher_hooks.forced_output_length ); |
| 96 | *output_length = test_driver_cipher_hooks.forced_output_length; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 97 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 98 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | psa_status_t test_transparent_cipher_encrypt_setup( |
| 102 | test_transparent_cipher_operation_t *operation, |
| 103 | const psa_key_attributes_t *attributes, |
| 104 | const uint8_t *key, size_t key_length, |
| 105 | psa_algorithm_t alg) |
| 106 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 107 | const mbedtls_cipher_info_t *cipher_info = NULL; |
| 108 | int ret = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 109 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 110 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 111 | |
| 112 | if( operation->alg != 0 ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 113 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 114 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 115 | /* Wiping the entire struct here, instead of member-by-member. This is useful |
| 116 | * for the test suite, since it gives a chance of catching memory corruption |
| 117 | * errors should the core not have allocated (enough) memory for our context |
| 118 | * struct. */ |
| 119 | memset( operation, 0, sizeof( *operation ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 120 | |
| 121 | /* Test driver supports AES-CTR only, to verify operation calls. */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 122 | if( alg != PSA_ALG_CTR || |
| 123 | psa_get_key_type( attributes ) != PSA_KEY_TYPE_AES ) |
| 124 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 125 | |
| 126 | operation->alg = alg; |
| 127 | operation->iv_size = 16; |
| 128 | operation->block_size = 16; |
| 129 | |
| 130 | cipher_info = mbedtls_cipher_info_from_values( MBEDTLS_CIPHER_ID_AES, |
| 131 | key_length * 8, |
| 132 | MBEDTLS_MODE_CTR ); |
| 133 | if( cipher_info == NULL ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 134 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 135 | |
| 136 | mbedtls_cipher_init( &operation->cipher ); |
| 137 | |
| 138 | ret = mbedtls_cipher_setup( &operation->cipher, cipher_info ); |
| 139 | if( ret != 0 ) { |
| 140 | mbedtls_cipher_free( &operation->cipher ); |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 141 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | ret = mbedtls_cipher_setkey( &operation->cipher, |
| 145 | key, |
| 146 | key_length * 8, MBEDTLS_ENCRYPT ); |
| 147 | if( ret != 0 ) { |
| 148 | mbedtls_cipher_free( &operation->cipher ); |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 149 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | operation->iv_set = 0; |
| 153 | operation->iv_required = 1; |
| 154 | operation->key_set = 1; |
| 155 | |
| 156 | /* Allow overriding return value for testing purposes */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 157 | if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 158 | mbedtls_cipher_free( &operation->cipher ); |
| 159 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 160 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | psa_status_t test_transparent_cipher_decrypt_setup( |
| 164 | test_transparent_cipher_operation_t *operation, |
| 165 | const psa_key_attributes_t *attributes, |
| 166 | const uint8_t *key, size_t key_length, |
| 167 | psa_algorithm_t alg) |
| 168 | { |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 169 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 170 | int ret = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 171 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 172 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 173 | |
| 174 | if( operation->alg != 0 ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 175 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 176 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 177 | /* Wiping the entire struct here, instead of member-by-member. This is useful |
| 178 | * for the test suite, since it gives a chance of catching memory corruption |
| 179 | * errors should the core not have allocated (enough) memory for our context |
| 180 | * struct. */ |
| 181 | memset( operation, 0, sizeof( *operation ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 182 | |
| 183 | /* Test driver supports AES-CTR only, to verify operation calls. */ |
| 184 | if( alg != PSA_ALG_CTR || psa_get_key_type( attributes ) != PSA_KEY_TYPE_AES ) |
| 185 | return PSA_ERROR_NOT_SUPPORTED; |
| 186 | |
| 187 | operation->alg = alg; |
| 188 | operation->iv_size = 16; |
| 189 | operation->block_size = 16; |
| 190 | |
| 191 | mbedtls_cipher_init( &operation->cipher ); |
| 192 | |
| 193 | cipher_info = mbedtls_cipher_info_from_values( MBEDTLS_CIPHER_ID_AES, |
| 194 | key_length * 8, |
| 195 | MBEDTLS_MODE_CTR ); |
| 196 | if( cipher_info == NULL ) |
| 197 | return PSA_ERROR_NOT_SUPPORTED; |
| 198 | |
| 199 | ret = mbedtls_cipher_setup( &operation->cipher, cipher_info ); |
| 200 | if( ret != 0 ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 201 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 202 | |
| 203 | ret = mbedtls_cipher_setkey( &operation->cipher, |
| 204 | key, |
| 205 | key_length * 8, MBEDTLS_DECRYPT ); |
| 206 | if( ret != 0 ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 207 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 208 | |
| 209 | operation->iv_set = 0; |
| 210 | operation->iv_required = 1; |
| 211 | operation->key_set = 1; |
| 212 | |
| 213 | /* Allow overriding return value for testing purposes */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 214 | if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 215 | mbedtls_cipher_free( &operation->cipher ); |
| 216 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 217 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | psa_status_t test_transparent_cipher_abort( |
| 221 | test_transparent_cipher_operation_t *operation) |
| 222 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 223 | if( operation->alg == 0 ) |
| 224 | return( PSA_SUCCESS ); |
| 225 | if( operation->alg != PSA_ALG_CTR ) |
| 226 | return( PSA_ERROR_BAD_STATE ); |
| 227 | |
| 228 | mbedtls_cipher_free( &operation->cipher ); |
| 229 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 230 | /* Wiping the entire struct here, instead of member-by-member. This is useful |
| 231 | * for the test suite, since it gives a chance of catching memory corruption |
| 232 | * errors should the core not have allocated (enough) memory for our context |
| 233 | * struct. */ |
| 234 | memset( operation, 0, sizeof( *operation ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 235 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 236 | test_driver_cipher_hooks.hits++; |
| 237 | return( PSA_SUCCESS ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | psa_status_t test_transparent_cipher_generate_iv( |
| 241 | test_transparent_cipher_operation_t *operation, |
| 242 | uint8_t *iv, |
| 243 | size_t iv_size, |
| 244 | size_t *iv_length) |
| 245 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 246 | psa_status_t status; |
| 247 | mbedtls_test_rnd_pseudo_info rnd_info; |
| 248 | memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 249 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 250 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 251 | |
| 252 | if( operation->alg != PSA_ALG_CTR ) |
| 253 | return( PSA_ERROR_BAD_STATE ); |
| 254 | |
| 255 | if( operation->iv_set || ! operation->iv_required ) |
| 256 | return( PSA_ERROR_BAD_STATE ); |
| 257 | |
| 258 | if( iv_size < operation->iv_size ) |
| 259 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 260 | |
| 261 | status = mbedtls_to_psa_error( |
| 262 | mbedtls_test_rnd_pseudo_rand( &rnd_info, |
| 263 | iv, |
| 264 | operation->iv_size ) ); |
| 265 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 266 | return( status ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 267 | |
| 268 | *iv_length = operation->iv_size; |
| 269 | status = test_transparent_cipher_set_iv( operation, iv, *iv_length ); |
| 270 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 271 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | psa_status_t test_transparent_cipher_set_iv( |
| 275 | test_transparent_cipher_operation_t *operation, |
| 276 | const uint8_t *iv, |
| 277 | size_t iv_length) |
| 278 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 279 | psa_status_t status; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 280 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 281 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 282 | |
| 283 | if( operation->alg != PSA_ALG_CTR ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 284 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 285 | |
| 286 | if( operation->iv_set || ! operation->iv_required ) |
| 287 | return( PSA_ERROR_BAD_STATE ); |
| 288 | |
| 289 | if( iv_length != operation->iv_size ) |
| 290 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 291 | |
| 292 | status = mbedtls_to_psa_error( |
| 293 | mbedtls_cipher_set_iv( &operation->cipher, iv, iv_length ) ); |
| 294 | |
| 295 | if( status == PSA_SUCCESS ) |
| 296 | operation->iv_set = 1; |
| 297 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 298 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | psa_status_t test_transparent_cipher_update( |
| 302 | test_transparent_cipher_operation_t *operation, |
| 303 | const uint8_t *input, |
| 304 | size_t input_length, |
| 305 | uint8_t *output, |
| 306 | size_t output_size, |
| 307 | size_t *output_length) |
| 308 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 309 | size_t expected_output_size; |
| 310 | psa_status_t status; |
| 311 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 312 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 313 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 314 | if( operation->alg != PSA_ALG_CTR ) |
| 315 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 316 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 317 | expected_output_size = ( operation->cipher.unprocessed_len + input_length ) |
| 318 | / operation->block_size * operation->block_size; |
| 319 | |
| 320 | if( output_size < expected_output_size ) |
| 321 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 322 | |
| 323 | status = mbedtls_to_psa_error( |
| 324 | mbedtls_cipher_update( &operation->cipher, input, |
| 325 | input_length, output, output_length ) ); |
| 326 | |
| 327 | if( status != PSA_SUCCESS ) |
| 328 | return status; |
| 329 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 330 | if( test_driver_cipher_hooks.forced_output != NULL ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 331 | { |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 332 | if( output_size < test_driver_cipher_hooks.forced_output_length ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 333 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 334 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 335 | memcpy( output, |
| 336 | test_driver_cipher_hooks.forced_output, |
| 337 | test_driver_cipher_hooks.forced_output_length ); |
| 338 | *output_length = test_driver_cipher_hooks.forced_output_length; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 339 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 340 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 341 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | psa_status_t test_transparent_cipher_finish( |
| 345 | test_transparent_cipher_operation_t *operation, |
| 346 | uint8_t *output, |
| 347 | size_t output_size, |
| 348 | size_t *output_length) |
| 349 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 350 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 351 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
| 352 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 353 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 354 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 355 | if( operation->alg != PSA_ALG_CTR ) |
| 356 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 357 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 358 | if( ! operation->key_set ) |
| 359 | return( PSA_ERROR_BAD_STATE ); |
| 360 | |
| 361 | if( operation->iv_required && ! operation->iv_set ) |
| 362 | return( PSA_ERROR_BAD_STATE ); |
| 363 | |
| 364 | status = mbedtls_to_psa_error( |
| 365 | mbedtls_cipher_finish( &operation->cipher, |
| 366 | temp_output_buffer, |
| 367 | output_length ) ); |
| 368 | |
| 369 | mbedtls_cipher_free( &operation->cipher ); |
| 370 | |
| 371 | if( status != PSA_SUCCESS ) |
| 372 | return( status ); |
| 373 | |
| 374 | if( *output_length == 0 ) |
| 375 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
| 376 | else if( output_size >= *output_length ) |
| 377 | memcpy( output, temp_output_buffer, *output_length ); |
| 378 | else |
| 379 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 380 | |
| 381 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 382 | if( test_driver_cipher_hooks.forced_output != NULL ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 383 | { |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 384 | if( output_size < test_driver_cipher_hooks.forced_output_length ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 385 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 386 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 387 | memcpy( output, |
| 388 | test_driver_cipher_hooks.forced_output, |
| 389 | test_driver_cipher_hooks.forced_output_length ); |
| 390 | *output_length = test_driver_cipher_hooks.forced_output_length; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 391 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 392 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 393 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* |
| 397 | * opaque versions, to do |
| 398 | */ |
| 399 | psa_status_t test_opaque_cipher_encrypt( |
| 400 | const psa_key_attributes_t *attributes, |
| 401 | const uint8_t *key, size_t key_length, |
| 402 | psa_algorithm_t alg, |
| 403 | const uint8_t *input, size_t input_length, |
| 404 | uint8_t *output, size_t output_size, size_t *output_length) |
| 405 | { |
| 406 | (void) attributes; |
| 407 | (void) key; |
| 408 | (void) key_length; |
| 409 | (void) alg; |
| 410 | (void) input; |
| 411 | (void) input_length; |
| 412 | (void) output; |
| 413 | (void) output_size; |
| 414 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 415 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | psa_status_t test_opaque_cipher_decrypt( |
| 419 | const psa_key_attributes_t *attributes, |
| 420 | const uint8_t *key, size_t key_length, |
| 421 | psa_algorithm_t alg, |
| 422 | const uint8_t *input, size_t input_length, |
| 423 | uint8_t *output, size_t output_size, size_t *output_length) |
| 424 | { |
| 425 | (void) attributes; |
| 426 | (void) key; |
| 427 | (void) key_length; |
| 428 | (void) alg; |
| 429 | (void) input; |
| 430 | (void) input_length; |
| 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 | |
| 437 | psa_status_t test_opaque_cipher_encrypt_setup( |
| 438 | test_opaque_cipher_operation_t *operation, |
| 439 | const psa_key_attributes_t *attributes, |
| 440 | const uint8_t *key, size_t key_length, |
| 441 | psa_algorithm_t alg) |
| 442 | { |
| 443 | (void) operation; |
| 444 | (void) attributes; |
| 445 | (void) key; |
| 446 | (void) key_length; |
| 447 | (void) alg; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 448 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | psa_status_t test_opaque_cipher_decrypt_setup( |
| 452 | test_opaque_cipher_operation_t *operation, |
| 453 | const psa_key_attributes_t *attributes, |
| 454 | const uint8_t *key, size_t key_length, |
| 455 | psa_algorithm_t alg) |
| 456 | { |
| 457 | (void) operation; |
| 458 | (void) attributes; |
| 459 | (void) key; |
| 460 | (void) key_length; |
| 461 | (void) alg; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 462 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | psa_status_t test_opaque_cipher_abort( |
| 466 | test_opaque_cipher_operation_t *operation) |
| 467 | { |
| 468 | (void) operation; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 469 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | psa_status_t test_opaque_cipher_generate_iv( |
| 473 | test_opaque_cipher_operation_t *operation, |
| 474 | uint8_t *iv, |
| 475 | size_t iv_size, |
| 476 | size_t *iv_length) |
| 477 | { |
| 478 | (void) operation; |
| 479 | (void) iv; |
| 480 | (void) iv_size; |
| 481 | (void) iv_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 482 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | psa_status_t test_opaque_cipher_set_iv( |
| 486 | test_opaque_cipher_operation_t *operation, |
| 487 | const uint8_t *iv, |
| 488 | size_t iv_length) |
| 489 | { |
| 490 | (void) operation; |
| 491 | (void) iv; |
| 492 | (void) iv_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 493 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | psa_status_t test_opaque_cipher_update( |
| 497 | test_opaque_cipher_operation_t *operation, |
| 498 | const uint8_t *input, |
| 499 | size_t input_length, |
| 500 | uint8_t *output, |
| 501 | size_t output_size, |
| 502 | size_t *output_length) |
| 503 | { |
| 504 | (void) operation; |
| 505 | (void) input; |
| 506 | (void) input_length; |
| 507 | (void) output; |
| 508 | (void) output_size; |
| 509 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 510 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | psa_status_t test_opaque_cipher_finish( |
| 514 | test_opaque_cipher_operation_t *operation, |
| 515 | uint8_t *output, |
| 516 | size_t output_size, |
| 517 | size_t *output_length) |
| 518 | { |
| 519 | (void) operation; |
| 520 | (void) output; |
| 521 | (void) output_size; |
| 522 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 523 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 524 | } |
| 525 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |