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 | |
Steven Cooreman | fe0ab55 | 2020-09-10 13:07:02 +0200 | [diff] [blame] | 45 | static psa_status_t test_transparent_cipher_oneshot( |
| 46 | mbedtls_operation_t direction, |
| 47 | const psa_key_attributes_t *attributes, |
| 48 | const uint8_t *key, size_t key_length, |
| 49 | psa_algorithm_t alg, |
| 50 | const uint8_t *input, size_t input_length, |
| 51 | uint8_t *output, size_t output_size, size_t *output_length) |
| 52 | { |
| 53 | test_driver_cipher_hooks.hits++; |
| 54 | |
| 55 | /* Test driver supports AES-CTR only, to verify operation calls. */ |
| 56 | if( alg != PSA_ALG_CTR || |
| 57 | psa_get_key_type( attributes ) != PSA_KEY_TYPE_AES ) |
| 58 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 59 | |
| 60 | /* If test driver response code is not SUCCESS, we can return early */ |
| 61 | if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS ) |
| 62 | return( test_driver_cipher_hooks.forced_status ); |
| 63 | |
| 64 | /* If test driver output is overridden, we don't need to do actual crypto */ |
| 65 | if( test_driver_cipher_hooks.forced_output != NULL ) |
| 66 | { |
| 67 | if( output_size < test_driver_cipher_hooks.forced_output_length ) |
| 68 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 69 | |
| 70 | memcpy( output, |
| 71 | test_driver_cipher_hooks.forced_output, |
| 72 | test_driver_cipher_hooks.forced_output_length ); |
| 73 | *output_length = test_driver_cipher_hooks.forced_output_length; |
| 74 | |
| 75 | return( test_driver_cipher_hooks.forced_status ); |
| 76 | } |
| 77 | |
| 78 | /* Run AES-CTR using the cipher module */ |
| 79 | { |
| 80 | mbedtls_test_rnd_pseudo_info rnd_info; |
| 81 | memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
| 82 | |
| 83 | const mbedtls_cipher_info_t *cipher_info = |
| 84 | mbedtls_cipher_info_from_values( MBEDTLS_CIPHER_ID_AES, |
| 85 | key_length * 8, |
| 86 | MBEDTLS_MODE_CTR ); |
| 87 | mbedtls_cipher_context_t cipher; |
| 88 | int ret = 0; |
| 89 | uint8_t temp_output_buffer[16] = {0}; |
| 90 | size_t temp_output_length = 0; |
| 91 | |
| 92 | if( direction == MBEDTLS_ENCRYPT ) |
| 93 | { |
| 94 | /* Oneshot encrypt needs to prepend the IV to the output */ |
| 95 | if( output_size < ( input_length + 16 ) ) |
| 96 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | /* Oneshot decrypt has the IV prepended to the input */ |
| 101 | if( output_size < ( input_length - 16 ) ) |
| 102 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 103 | } |
| 104 | |
| 105 | if( cipher_info == NULL ) |
| 106 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 107 | |
| 108 | mbedtls_cipher_init( &cipher ); |
| 109 | ret = mbedtls_cipher_setup( &cipher, cipher_info ); |
| 110 | if( ret != 0 ) |
| 111 | goto exit; |
| 112 | |
| 113 | ret = mbedtls_cipher_setkey( &cipher, |
| 114 | key, |
| 115 | key_length * 8, direction ); |
| 116 | if( ret != 0 ) |
| 117 | goto exit; |
| 118 | |
| 119 | if( direction == MBEDTLS_ENCRYPT ) |
| 120 | { |
| 121 | mbedtls_test_rnd_pseudo_info rnd_info; |
| 122 | memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
| 123 | |
| 124 | ret = mbedtls_test_rnd_pseudo_rand( &rnd_info, |
| 125 | temp_output_buffer, |
| 126 | 16 ); |
| 127 | if( ret != 0 ) |
| 128 | goto exit; |
| 129 | |
| 130 | ret = mbedtls_cipher_set_iv( &cipher, temp_output_buffer, 16 ); |
| 131 | } |
| 132 | else |
| 133 | ret = mbedtls_cipher_set_iv( &cipher, input, 16 ); |
| 134 | |
| 135 | if( ret != 0 ) |
| 136 | goto exit; |
| 137 | |
| 138 | if( direction == MBEDTLS_ENCRYPT ) |
| 139 | { |
| 140 | ret = mbedtls_cipher_update( &cipher, |
| 141 | input, input_length, |
| 142 | &output[16], output_length ); |
| 143 | if( ret == 0 ) |
| 144 | { |
| 145 | memcpy( output, temp_output_buffer, 16 ); |
| 146 | *output_length += 16; |
| 147 | } |
| 148 | } |
| 149 | else |
| 150 | ret = mbedtls_cipher_update( &cipher, |
| 151 | &input[16], input_length - 16, |
| 152 | output, output_length ); |
| 153 | |
| 154 | if( ret != 0 ) |
| 155 | goto exit; |
| 156 | |
| 157 | ret = mbedtls_cipher_finish( &cipher, |
| 158 | temp_output_buffer, |
| 159 | &temp_output_length ); |
| 160 | |
| 161 | exit: |
| 162 | if( ret != 0 ) |
| 163 | { |
| 164 | *output_length = 0; |
| 165 | memset(output, 0, output_size); |
| 166 | } |
| 167 | |
| 168 | mbedtls_cipher_free( &cipher ); |
| 169 | return( mbedtls_to_psa_error( ret ) ); |
| 170 | } |
| 171 | } |
| 172 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 173 | psa_status_t test_transparent_cipher_encrypt( |
| 174 | const psa_key_attributes_t *attributes, |
| 175 | const uint8_t *key, size_t key_length, |
| 176 | psa_algorithm_t alg, |
| 177 | const uint8_t *input, size_t input_length, |
| 178 | uint8_t *output, size_t output_size, size_t *output_length) |
| 179 | { |
Steven Cooreman | fe0ab55 | 2020-09-10 13:07:02 +0200 | [diff] [blame] | 180 | return ( |
| 181 | test_transparent_cipher_oneshot( |
| 182 | MBEDTLS_ENCRYPT, |
| 183 | attributes, |
| 184 | key, key_length, |
| 185 | alg, |
| 186 | input, input_length, |
| 187 | output, output_size, output_length) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | psa_status_t test_transparent_cipher_decrypt( |
| 191 | const psa_key_attributes_t *attributes, |
| 192 | const uint8_t *key, size_t key_length, |
| 193 | psa_algorithm_t alg, |
| 194 | const uint8_t *input, size_t input_length, |
| 195 | uint8_t *output, size_t output_size, size_t *output_length) |
| 196 | { |
Steven Cooreman | fe0ab55 | 2020-09-10 13:07:02 +0200 | [diff] [blame] | 197 | return ( |
| 198 | test_transparent_cipher_oneshot( |
| 199 | MBEDTLS_DECRYPT, |
| 200 | attributes, |
| 201 | key, key_length, |
| 202 | alg, |
| 203 | input, input_length, |
| 204 | output, output_size, output_length) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Steven Cooreman | 16afd3d | 2020-09-09 15:36:39 +0200 | [diff] [blame] | 207 | static psa_status_t test_transparent_cipher_setup( |
| 208 | mbedtls_operation_t direction, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 209 | test_transparent_cipher_operation_t *operation, |
| 210 | const psa_key_attributes_t *attributes, |
| 211 | const uint8_t *key, size_t key_length, |
| 212 | psa_algorithm_t alg) |
| 213 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 214 | const mbedtls_cipher_info_t *cipher_info = NULL; |
| 215 | int ret = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 216 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 217 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 218 | |
| 219 | if( operation->alg != 0 ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 220 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 221 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 222 | /* Wiping the entire struct here, instead of member-by-member. This is useful |
| 223 | * for the test suite, since it gives a chance of catching memory corruption |
| 224 | * errors should the core not have allocated (enough) memory for our context |
| 225 | * struct. */ |
| 226 | memset( operation, 0, sizeof( *operation ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 227 | |
| 228 | /* Test driver supports AES-CTR only, to verify operation calls. */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 229 | if( alg != PSA_ALG_CTR || |
| 230 | psa_get_key_type( attributes ) != PSA_KEY_TYPE_AES ) |
| 231 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 232 | |
| 233 | operation->alg = alg; |
| 234 | operation->iv_size = 16; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 235 | |
| 236 | cipher_info = mbedtls_cipher_info_from_values( MBEDTLS_CIPHER_ID_AES, |
| 237 | key_length * 8, |
| 238 | MBEDTLS_MODE_CTR ); |
| 239 | if( cipher_info == NULL ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 240 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 241 | |
| 242 | mbedtls_cipher_init( &operation->cipher ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 243 | ret = mbedtls_cipher_setup( &operation->cipher, cipher_info ); |
| 244 | if( ret != 0 ) { |
| 245 | mbedtls_cipher_free( &operation->cipher ); |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 246 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | ret = mbedtls_cipher_setkey( &operation->cipher, |
| 250 | key, |
Steven Cooreman | 16afd3d | 2020-09-09 15:36:39 +0200 | [diff] [blame] | 251 | key_length * 8, direction ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 252 | if( ret != 0 ) { |
| 253 | mbedtls_cipher_free( &operation->cipher ); |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 254 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | operation->iv_set = 0; |
| 258 | operation->iv_required = 1; |
| 259 | operation->key_set = 1; |
| 260 | |
| 261 | /* Allow overriding return value for testing purposes */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 262 | if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 263 | mbedtls_cipher_free( &operation->cipher ); |
| 264 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 265 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Steven Cooreman | 16afd3d | 2020-09-09 15:36:39 +0200 | [diff] [blame] | 268 | psa_status_t test_transparent_cipher_encrypt_setup( |
| 269 | test_transparent_cipher_operation_t *operation, |
| 270 | const psa_key_attributes_t *attributes, |
| 271 | const uint8_t *key, size_t key_length, |
| 272 | psa_algorithm_t alg) |
| 273 | { |
| 274 | return ( test_transparent_cipher_setup( MBEDTLS_ENCRYPT, |
| 275 | operation, |
| 276 | attributes, |
| 277 | key, |
| 278 | key_length, |
| 279 | alg ) ); |
| 280 | } |
| 281 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 282 | psa_status_t test_transparent_cipher_decrypt_setup( |
| 283 | test_transparent_cipher_operation_t *operation, |
| 284 | const psa_key_attributes_t *attributes, |
| 285 | const uint8_t *key, size_t key_length, |
| 286 | psa_algorithm_t alg) |
| 287 | { |
Steven Cooreman | 16afd3d | 2020-09-09 15:36:39 +0200 | [diff] [blame] | 288 | return ( test_transparent_cipher_setup( MBEDTLS_DECRYPT, |
| 289 | operation, |
| 290 | attributes, |
| 291 | key, |
| 292 | key_length, |
| 293 | alg ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | psa_status_t test_transparent_cipher_abort( |
| 297 | test_transparent_cipher_operation_t *operation) |
| 298 | { |
Steven Cooreman | 89e54f2 | 2020-09-10 18:07:57 +0200 | [diff] [blame^] | 299 | test_driver_cipher_hooks.hits++; |
| 300 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 301 | if( operation->alg == 0 ) |
| 302 | return( PSA_SUCCESS ); |
| 303 | if( operation->alg != PSA_ALG_CTR ) |
| 304 | return( PSA_ERROR_BAD_STATE ); |
| 305 | |
| 306 | mbedtls_cipher_free( &operation->cipher ); |
| 307 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 308 | /* Wiping the entire struct here, instead of member-by-member. This is useful |
| 309 | * for the test suite, since it gives a chance of catching memory corruption |
| 310 | * errors should the core not have allocated (enough) memory for our context |
| 311 | * struct. */ |
| 312 | memset( operation, 0, sizeof( *operation ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 313 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 314 | return( PSA_SUCCESS ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | psa_status_t test_transparent_cipher_generate_iv( |
| 318 | test_transparent_cipher_operation_t *operation, |
| 319 | uint8_t *iv, |
| 320 | size_t iv_size, |
| 321 | size_t *iv_length) |
| 322 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 323 | psa_status_t status; |
| 324 | mbedtls_test_rnd_pseudo_info rnd_info; |
| 325 | memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 326 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 327 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 328 | |
Steven Cooreman | 89e54f2 | 2020-09-10 18:07:57 +0200 | [diff] [blame^] | 329 | if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS ) |
| 330 | return( test_driver_cipher_hooks.forced_status ); |
| 331 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 332 | if( operation->alg != PSA_ALG_CTR ) |
| 333 | return( PSA_ERROR_BAD_STATE ); |
| 334 | |
| 335 | if( operation->iv_set || ! operation->iv_required ) |
| 336 | return( PSA_ERROR_BAD_STATE ); |
| 337 | |
| 338 | if( iv_size < operation->iv_size ) |
| 339 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 340 | |
| 341 | status = mbedtls_to_psa_error( |
| 342 | mbedtls_test_rnd_pseudo_rand( &rnd_info, |
| 343 | iv, |
| 344 | operation->iv_size ) ); |
| 345 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 346 | return( status ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 347 | |
| 348 | *iv_length = operation->iv_size; |
| 349 | status = test_transparent_cipher_set_iv( operation, iv, *iv_length ); |
| 350 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 351 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | psa_status_t test_transparent_cipher_set_iv( |
| 355 | test_transparent_cipher_operation_t *operation, |
| 356 | const uint8_t *iv, |
| 357 | size_t iv_length) |
| 358 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 359 | psa_status_t status; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 360 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 361 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 362 | |
Steven Cooreman | 89e54f2 | 2020-09-10 18:07:57 +0200 | [diff] [blame^] | 363 | if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS ) |
| 364 | return( test_driver_cipher_hooks.forced_status ); |
| 365 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 366 | if( operation->alg != PSA_ALG_CTR ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 367 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 368 | |
| 369 | if( operation->iv_set || ! operation->iv_required ) |
| 370 | return( PSA_ERROR_BAD_STATE ); |
| 371 | |
| 372 | if( iv_length != operation->iv_size ) |
| 373 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 374 | |
| 375 | status = mbedtls_to_psa_error( |
| 376 | mbedtls_cipher_set_iv( &operation->cipher, iv, iv_length ) ); |
| 377 | |
| 378 | if( status == PSA_SUCCESS ) |
| 379 | operation->iv_set = 1; |
| 380 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 381 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | psa_status_t test_transparent_cipher_update( |
| 385 | test_transparent_cipher_operation_t *operation, |
| 386 | const uint8_t *input, |
| 387 | size_t input_length, |
| 388 | uint8_t *output, |
| 389 | size_t output_size, |
| 390 | size_t *output_length) |
| 391 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 392 | psa_status_t status; |
| 393 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 394 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 395 | |
Steven Cooreman | 89e54f2 | 2020-09-10 18:07:57 +0200 | [diff] [blame^] | 396 | if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS ) |
| 397 | return( test_driver_cipher_hooks.forced_status ); |
| 398 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 399 | if( operation->alg != PSA_ALG_CTR ) |
| 400 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 401 | |
Steven Cooreman | 16afd3d | 2020-09-09 15:36:39 +0200 | [diff] [blame] | 402 | /* CTR is a stream cipher, so data in and out are always the same size */ |
| 403 | if( output_size < input_length ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 404 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 405 | |
| 406 | status = mbedtls_to_psa_error( |
| 407 | mbedtls_cipher_update( &operation->cipher, input, |
| 408 | input_length, output, output_length ) ); |
| 409 | |
| 410 | if( status != PSA_SUCCESS ) |
| 411 | return status; |
| 412 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 413 | if( test_driver_cipher_hooks.forced_output != NULL ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 414 | { |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 415 | if( output_size < test_driver_cipher_hooks.forced_output_length ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 416 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 417 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 418 | memcpy( output, |
| 419 | test_driver_cipher_hooks.forced_output, |
| 420 | test_driver_cipher_hooks.forced_output_length ); |
| 421 | *output_length = test_driver_cipher_hooks.forced_output_length; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 422 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 423 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 424 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | psa_status_t test_transparent_cipher_finish( |
| 428 | test_transparent_cipher_operation_t *operation, |
| 429 | uint8_t *output, |
| 430 | size_t output_size, |
| 431 | size_t *output_length) |
| 432 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 433 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 434 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
| 435 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 436 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 437 | |
Steven Cooreman | 89e54f2 | 2020-09-10 18:07:57 +0200 | [diff] [blame^] | 438 | if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS ) |
| 439 | return( test_driver_cipher_hooks.forced_status ); |
| 440 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 441 | if( operation->alg != PSA_ALG_CTR ) |
| 442 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 443 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 444 | if( ! operation->key_set ) |
| 445 | return( PSA_ERROR_BAD_STATE ); |
| 446 | |
| 447 | if( operation->iv_required && ! operation->iv_set ) |
| 448 | return( PSA_ERROR_BAD_STATE ); |
| 449 | |
| 450 | status = mbedtls_to_psa_error( |
| 451 | mbedtls_cipher_finish( &operation->cipher, |
| 452 | temp_output_buffer, |
| 453 | output_length ) ); |
| 454 | |
| 455 | mbedtls_cipher_free( &operation->cipher ); |
| 456 | |
| 457 | if( status != PSA_SUCCESS ) |
| 458 | return( status ); |
| 459 | |
| 460 | if( *output_length == 0 ) |
| 461 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
| 462 | else if( output_size >= *output_length ) |
| 463 | memcpy( output, temp_output_buffer, *output_length ); |
| 464 | else |
| 465 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 466 | |
| 467 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 468 | if( test_driver_cipher_hooks.forced_output != NULL ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 469 | { |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 470 | if( output_size < test_driver_cipher_hooks.forced_output_length ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 471 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 472 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 473 | memcpy( output, |
| 474 | test_driver_cipher_hooks.forced_output, |
| 475 | test_driver_cipher_hooks.forced_output_length ); |
| 476 | *output_length = test_driver_cipher_hooks.forced_output_length; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 477 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 478 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 479 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /* |
| 483 | * opaque versions, to do |
| 484 | */ |
| 485 | psa_status_t test_opaque_cipher_encrypt( |
| 486 | const psa_key_attributes_t *attributes, |
| 487 | const uint8_t *key, size_t key_length, |
| 488 | psa_algorithm_t alg, |
| 489 | const uint8_t *input, size_t input_length, |
| 490 | uint8_t *output, size_t output_size, size_t *output_length) |
| 491 | { |
| 492 | (void) attributes; |
| 493 | (void) key; |
| 494 | (void) key_length; |
| 495 | (void) alg; |
| 496 | (void) input; |
| 497 | (void) input_length; |
| 498 | (void) output; |
| 499 | (void) output_size; |
| 500 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 501 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | psa_status_t test_opaque_cipher_decrypt( |
| 505 | const psa_key_attributes_t *attributes, |
| 506 | const uint8_t *key, size_t key_length, |
| 507 | psa_algorithm_t alg, |
| 508 | const uint8_t *input, size_t input_length, |
| 509 | uint8_t *output, size_t output_size, size_t *output_length) |
| 510 | { |
| 511 | (void) attributes; |
| 512 | (void) key; |
| 513 | (void) key_length; |
| 514 | (void) alg; |
| 515 | (void) input; |
| 516 | (void) input_length; |
| 517 | (void) output; |
| 518 | (void) output_size; |
| 519 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 520 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | psa_status_t test_opaque_cipher_encrypt_setup( |
| 524 | test_opaque_cipher_operation_t *operation, |
| 525 | const psa_key_attributes_t *attributes, |
| 526 | const uint8_t *key, size_t key_length, |
| 527 | psa_algorithm_t alg) |
| 528 | { |
| 529 | (void) operation; |
| 530 | (void) attributes; |
| 531 | (void) key; |
| 532 | (void) key_length; |
| 533 | (void) alg; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 534 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | psa_status_t test_opaque_cipher_decrypt_setup( |
| 538 | test_opaque_cipher_operation_t *operation, |
| 539 | const psa_key_attributes_t *attributes, |
| 540 | const uint8_t *key, size_t key_length, |
| 541 | psa_algorithm_t alg) |
| 542 | { |
| 543 | (void) operation; |
| 544 | (void) attributes; |
| 545 | (void) key; |
| 546 | (void) key_length; |
| 547 | (void) alg; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 548 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | psa_status_t test_opaque_cipher_abort( |
| 552 | test_opaque_cipher_operation_t *operation) |
| 553 | { |
| 554 | (void) operation; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 555 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | psa_status_t test_opaque_cipher_generate_iv( |
| 559 | test_opaque_cipher_operation_t *operation, |
| 560 | uint8_t *iv, |
| 561 | size_t iv_size, |
| 562 | size_t *iv_length) |
| 563 | { |
| 564 | (void) operation; |
| 565 | (void) iv; |
| 566 | (void) iv_size; |
| 567 | (void) iv_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 568 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | psa_status_t test_opaque_cipher_set_iv( |
| 572 | test_opaque_cipher_operation_t *operation, |
| 573 | const uint8_t *iv, |
| 574 | size_t iv_length) |
| 575 | { |
| 576 | (void) operation; |
| 577 | (void) iv; |
| 578 | (void) iv_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 579 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | psa_status_t test_opaque_cipher_update( |
| 583 | test_opaque_cipher_operation_t *operation, |
| 584 | const uint8_t *input, |
| 585 | size_t input_length, |
| 586 | uint8_t *output, |
| 587 | size_t output_size, |
| 588 | size_t *output_length) |
| 589 | { |
| 590 | (void) operation; |
| 591 | (void) input; |
| 592 | (void) input_length; |
| 593 | (void) output; |
| 594 | (void) output_size; |
| 595 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 596 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | psa_status_t test_opaque_cipher_finish( |
| 600 | test_opaque_cipher_operation_t *operation, |
| 601 | uint8_t *output, |
| 602 | size_t output_size, |
| 603 | size_t *output_length) |
| 604 | { |
| 605 | (void) operation; |
| 606 | (void) output; |
| 607 | (void) output_size; |
| 608 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 609 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 610 | } |
| 611 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |