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 | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 299 | if( operation->alg == 0 ) |
| 300 | return( PSA_SUCCESS ); |
| 301 | if( operation->alg != PSA_ALG_CTR ) |
| 302 | return( PSA_ERROR_BAD_STATE ); |
| 303 | |
| 304 | mbedtls_cipher_free( &operation->cipher ); |
| 305 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 306 | /* Wiping the entire struct here, instead of member-by-member. This is useful |
| 307 | * for the test suite, since it gives a chance of catching memory corruption |
| 308 | * errors should the core not have allocated (enough) memory for our context |
| 309 | * struct. */ |
| 310 | memset( operation, 0, sizeof( *operation ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 311 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 312 | test_driver_cipher_hooks.hits++; |
| 313 | return( PSA_SUCCESS ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | psa_status_t test_transparent_cipher_generate_iv( |
| 317 | test_transparent_cipher_operation_t *operation, |
| 318 | uint8_t *iv, |
| 319 | size_t iv_size, |
| 320 | size_t *iv_length) |
| 321 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 322 | psa_status_t status; |
| 323 | mbedtls_test_rnd_pseudo_info rnd_info; |
| 324 | memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 325 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 326 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 327 | |
| 328 | if( operation->alg != PSA_ALG_CTR ) |
| 329 | return( PSA_ERROR_BAD_STATE ); |
| 330 | |
| 331 | if( operation->iv_set || ! operation->iv_required ) |
| 332 | return( PSA_ERROR_BAD_STATE ); |
| 333 | |
| 334 | if( iv_size < operation->iv_size ) |
| 335 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 336 | |
| 337 | status = mbedtls_to_psa_error( |
| 338 | mbedtls_test_rnd_pseudo_rand( &rnd_info, |
| 339 | iv, |
| 340 | operation->iv_size ) ); |
| 341 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 342 | return( status ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 343 | |
| 344 | *iv_length = operation->iv_size; |
| 345 | status = test_transparent_cipher_set_iv( operation, iv, *iv_length ); |
| 346 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 347 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | psa_status_t test_transparent_cipher_set_iv( |
| 351 | test_transparent_cipher_operation_t *operation, |
| 352 | const uint8_t *iv, |
| 353 | size_t iv_length) |
| 354 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 355 | psa_status_t status; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 356 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 357 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 358 | |
| 359 | if( operation->alg != PSA_ALG_CTR ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 360 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 361 | |
| 362 | if( operation->iv_set || ! operation->iv_required ) |
| 363 | return( PSA_ERROR_BAD_STATE ); |
| 364 | |
| 365 | if( iv_length != operation->iv_size ) |
| 366 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 367 | |
| 368 | status = mbedtls_to_psa_error( |
| 369 | mbedtls_cipher_set_iv( &operation->cipher, iv, iv_length ) ); |
| 370 | |
| 371 | if( status == PSA_SUCCESS ) |
| 372 | operation->iv_set = 1; |
| 373 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 374 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | psa_status_t test_transparent_cipher_update( |
| 378 | test_transparent_cipher_operation_t *operation, |
| 379 | const uint8_t *input, |
| 380 | size_t input_length, |
| 381 | uint8_t *output, |
| 382 | size_t output_size, |
| 383 | size_t *output_length) |
| 384 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 385 | psa_status_t status; |
| 386 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 387 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 388 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 389 | if( operation->alg != PSA_ALG_CTR ) |
| 390 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 391 | |
Steven Cooreman | 16afd3d | 2020-09-09 15:36:39 +0200 | [diff] [blame] | 392 | /* CTR is a stream cipher, so data in and out are always the same size */ |
| 393 | if( output_size < input_length ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 394 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 395 | |
| 396 | status = mbedtls_to_psa_error( |
| 397 | mbedtls_cipher_update( &operation->cipher, input, |
| 398 | input_length, output, output_length ) ); |
| 399 | |
| 400 | if( status != PSA_SUCCESS ) |
| 401 | return status; |
| 402 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 403 | if( test_driver_cipher_hooks.forced_output != NULL ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 404 | { |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 405 | if( output_size < test_driver_cipher_hooks.forced_output_length ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 406 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 407 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 408 | memcpy( output, |
| 409 | test_driver_cipher_hooks.forced_output, |
| 410 | test_driver_cipher_hooks.forced_output_length ); |
| 411 | *output_length = test_driver_cipher_hooks.forced_output_length; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 412 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 413 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 414 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | psa_status_t test_transparent_cipher_finish( |
| 418 | test_transparent_cipher_operation_t *operation, |
| 419 | uint8_t *output, |
| 420 | size_t output_size, |
| 421 | size_t *output_length) |
| 422 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 423 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 424 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
| 425 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 426 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 427 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 428 | if( operation->alg != PSA_ALG_CTR ) |
| 429 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 430 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 431 | if( ! operation->key_set ) |
| 432 | return( PSA_ERROR_BAD_STATE ); |
| 433 | |
| 434 | if( operation->iv_required && ! operation->iv_set ) |
| 435 | return( PSA_ERROR_BAD_STATE ); |
| 436 | |
| 437 | status = mbedtls_to_psa_error( |
| 438 | mbedtls_cipher_finish( &operation->cipher, |
| 439 | temp_output_buffer, |
| 440 | output_length ) ); |
| 441 | |
| 442 | mbedtls_cipher_free( &operation->cipher ); |
| 443 | |
| 444 | if( status != PSA_SUCCESS ) |
| 445 | return( status ); |
| 446 | |
| 447 | if( *output_length == 0 ) |
| 448 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
| 449 | else if( output_size >= *output_length ) |
| 450 | memcpy( output, temp_output_buffer, *output_length ); |
| 451 | else |
| 452 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 453 | |
| 454 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 455 | if( test_driver_cipher_hooks.forced_output != NULL ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 456 | { |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 457 | if( output_size < test_driver_cipher_hooks.forced_output_length ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 458 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 459 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 460 | memcpy( output, |
| 461 | test_driver_cipher_hooks.forced_output, |
| 462 | test_driver_cipher_hooks.forced_output_length ); |
| 463 | *output_length = test_driver_cipher_hooks.forced_output_length; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 464 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 465 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 466 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* |
| 470 | * opaque versions, to do |
| 471 | */ |
| 472 | psa_status_t test_opaque_cipher_encrypt( |
| 473 | const psa_key_attributes_t *attributes, |
| 474 | const uint8_t *key, size_t key_length, |
| 475 | psa_algorithm_t alg, |
| 476 | const uint8_t *input, size_t input_length, |
| 477 | uint8_t *output, size_t output_size, size_t *output_length) |
| 478 | { |
| 479 | (void) attributes; |
| 480 | (void) key; |
| 481 | (void) key_length; |
| 482 | (void) alg; |
| 483 | (void) input; |
| 484 | (void) input_length; |
| 485 | (void) output; |
| 486 | (void) output_size; |
| 487 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 488 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | psa_status_t test_opaque_cipher_decrypt( |
| 492 | const psa_key_attributes_t *attributes, |
| 493 | const uint8_t *key, size_t key_length, |
| 494 | psa_algorithm_t alg, |
| 495 | const uint8_t *input, size_t input_length, |
| 496 | uint8_t *output, size_t output_size, size_t *output_length) |
| 497 | { |
| 498 | (void) attributes; |
| 499 | (void) key; |
| 500 | (void) key_length; |
| 501 | (void) alg; |
| 502 | (void) input; |
| 503 | (void) input_length; |
| 504 | (void) output; |
| 505 | (void) output_size; |
| 506 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 507 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | psa_status_t test_opaque_cipher_encrypt_setup( |
| 511 | test_opaque_cipher_operation_t *operation, |
| 512 | const psa_key_attributes_t *attributes, |
| 513 | const uint8_t *key, size_t key_length, |
| 514 | psa_algorithm_t alg) |
| 515 | { |
| 516 | (void) operation; |
| 517 | (void) attributes; |
| 518 | (void) key; |
| 519 | (void) key_length; |
| 520 | (void) alg; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 521 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | psa_status_t test_opaque_cipher_decrypt_setup( |
| 525 | test_opaque_cipher_operation_t *operation, |
| 526 | const psa_key_attributes_t *attributes, |
| 527 | const uint8_t *key, size_t key_length, |
| 528 | psa_algorithm_t alg) |
| 529 | { |
| 530 | (void) operation; |
| 531 | (void) attributes; |
| 532 | (void) key; |
| 533 | (void) key_length; |
| 534 | (void) alg; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 535 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | psa_status_t test_opaque_cipher_abort( |
| 539 | test_opaque_cipher_operation_t *operation) |
| 540 | { |
| 541 | (void) operation; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 542 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | psa_status_t test_opaque_cipher_generate_iv( |
| 546 | test_opaque_cipher_operation_t *operation, |
| 547 | uint8_t *iv, |
| 548 | size_t iv_size, |
| 549 | size_t *iv_length) |
| 550 | { |
| 551 | (void) operation; |
| 552 | (void) iv; |
| 553 | (void) iv_size; |
| 554 | (void) iv_length; |
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_set_iv( |
| 559 | test_opaque_cipher_operation_t *operation, |
| 560 | const uint8_t *iv, |
| 561 | size_t iv_length) |
| 562 | { |
| 563 | (void) operation; |
| 564 | (void) iv; |
| 565 | (void) iv_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 566 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | psa_status_t test_opaque_cipher_update( |
| 570 | test_opaque_cipher_operation_t *operation, |
| 571 | const uint8_t *input, |
| 572 | size_t input_length, |
| 573 | uint8_t *output, |
| 574 | size_t output_size, |
| 575 | size_t *output_length) |
| 576 | { |
| 577 | (void) operation; |
| 578 | (void) input; |
| 579 | (void) input_length; |
| 580 | (void) output; |
| 581 | (void) output_size; |
| 582 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 583 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | psa_status_t test_opaque_cipher_finish( |
| 587 | test_opaque_cipher_operation_t *operation, |
| 588 | uint8_t *output, |
| 589 | size_t output_size, |
| 590 | size_t *output_length) |
| 591 | { |
| 592 | (void) operation; |
| 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 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |