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 | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 38 | /* Test driver implements AES-CTR by default when it's status is not overridden. |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 39 | * Set test_driver_cipher_hooks.forced_status to PSA_ERROR_NOT_SUPPORTED to use |
| 40 | * fallback even for AES-CTR. |
| 41 | * Keep in mind this code is only exercised with the crypto drivers test target, |
| 42 | * meaning the other test runs will only test the non-driver implementation. */ |
| 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 | |
| 115 | /* write our struct, this will trigger memory corruption failures |
| 116 | * in test when we go outside of bounds, or when the function is called |
| 117 | * without first destroying the context object. */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 118 | memset( operation, 0, sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 119 | |
| 120 | /* Test driver supports AES-CTR only, to verify operation calls. */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 121 | if( alg != PSA_ALG_CTR || |
| 122 | psa_get_key_type( attributes ) != PSA_KEY_TYPE_AES ) |
| 123 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 124 | |
| 125 | operation->alg = alg; |
| 126 | operation->iv_size = 16; |
| 127 | operation->block_size = 16; |
| 128 | |
| 129 | cipher_info = mbedtls_cipher_info_from_values( MBEDTLS_CIPHER_ID_AES, |
| 130 | key_length * 8, |
| 131 | MBEDTLS_MODE_CTR ); |
| 132 | if( cipher_info == NULL ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 133 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 134 | |
| 135 | mbedtls_cipher_init( &operation->cipher ); |
| 136 | |
| 137 | ret = mbedtls_cipher_setup( &operation->cipher, cipher_info ); |
| 138 | if( ret != 0 ) { |
| 139 | mbedtls_cipher_free( &operation->cipher ); |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 140 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | ret = mbedtls_cipher_setkey( &operation->cipher, |
| 144 | key, |
| 145 | key_length * 8, MBEDTLS_ENCRYPT ); |
| 146 | if( ret != 0 ) { |
| 147 | mbedtls_cipher_free( &operation->cipher ); |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 148 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | operation->iv_set = 0; |
| 152 | operation->iv_required = 1; |
| 153 | operation->key_set = 1; |
| 154 | |
| 155 | /* Allow overriding return value for testing purposes */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 156 | if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 157 | mbedtls_cipher_free( &operation->cipher ); |
| 158 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 159 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | psa_status_t test_transparent_cipher_decrypt_setup( |
| 163 | test_transparent_cipher_operation_t *operation, |
| 164 | const psa_key_attributes_t *attributes, |
| 165 | const uint8_t *key, size_t key_length, |
| 166 | psa_algorithm_t alg) |
| 167 | { |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 168 | const mbedtls_cipher_info_t *cipher_info = NULL; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 169 | int ret = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 170 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 171 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 172 | |
| 173 | if( operation->alg != 0 ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 174 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 175 | |
| 176 | /* write our struct, this will trigger memory corruption failures |
| 177 | * in test when we go outside of bounds, or when the function is called |
| 178 | * without first destroying the context object. */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 179 | memset( operation, 0, sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 180 | |
| 181 | /* Test driver supports AES-CTR only, to verify operation calls. */ |
| 182 | if( alg != PSA_ALG_CTR || psa_get_key_type( attributes ) != PSA_KEY_TYPE_AES ) |
| 183 | return PSA_ERROR_NOT_SUPPORTED; |
| 184 | |
| 185 | operation->alg = alg; |
| 186 | operation->iv_size = 16; |
| 187 | operation->block_size = 16; |
| 188 | |
| 189 | mbedtls_cipher_init( &operation->cipher ); |
| 190 | |
| 191 | cipher_info = mbedtls_cipher_info_from_values( MBEDTLS_CIPHER_ID_AES, |
| 192 | key_length * 8, |
| 193 | MBEDTLS_MODE_CTR ); |
| 194 | if( cipher_info == NULL ) |
| 195 | return PSA_ERROR_NOT_SUPPORTED; |
| 196 | |
| 197 | ret = mbedtls_cipher_setup( &operation->cipher, cipher_info ); |
| 198 | if( ret != 0 ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 199 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 200 | |
| 201 | ret = mbedtls_cipher_setkey( &operation->cipher, |
| 202 | key, |
| 203 | key_length * 8, MBEDTLS_DECRYPT ); |
| 204 | if( ret != 0 ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 205 | return( mbedtls_to_psa_error( ret ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 206 | |
| 207 | operation->iv_set = 0; |
| 208 | operation->iv_required = 1; |
| 209 | operation->key_set = 1; |
| 210 | |
| 211 | /* Allow overriding return value for testing purposes */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 212 | if( test_driver_cipher_hooks.forced_status != PSA_SUCCESS ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 213 | mbedtls_cipher_free( &operation->cipher ); |
| 214 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 215 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | psa_status_t test_transparent_cipher_abort( |
| 219 | test_transparent_cipher_operation_t *operation) |
| 220 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 221 | if( operation->alg == 0 ) |
| 222 | return( PSA_SUCCESS ); |
| 223 | if( operation->alg != PSA_ALG_CTR ) |
| 224 | return( PSA_ERROR_BAD_STATE ); |
| 225 | |
| 226 | mbedtls_cipher_free( &operation->cipher ); |
| 227 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 228 | /* write our struct, this will trigger memory corruption failures |
| 229 | * in test when we go outside of bounds. */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 230 | memset( operation, 0, sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 231 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 232 | test_driver_cipher_hooks.hits++; |
| 233 | return( PSA_SUCCESS ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | psa_status_t test_transparent_cipher_generate_iv( |
| 237 | test_transparent_cipher_operation_t *operation, |
| 238 | uint8_t *iv, |
| 239 | size_t iv_size, |
| 240 | size_t *iv_length) |
| 241 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 242 | psa_status_t status; |
| 243 | mbedtls_test_rnd_pseudo_info rnd_info; |
| 244 | memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 245 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 246 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 247 | |
| 248 | if( operation->alg != PSA_ALG_CTR ) |
| 249 | return( PSA_ERROR_BAD_STATE ); |
| 250 | |
| 251 | if( operation->iv_set || ! operation->iv_required ) |
| 252 | return( PSA_ERROR_BAD_STATE ); |
| 253 | |
| 254 | if( iv_size < operation->iv_size ) |
| 255 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 256 | |
| 257 | status = mbedtls_to_psa_error( |
| 258 | mbedtls_test_rnd_pseudo_rand( &rnd_info, |
| 259 | iv, |
| 260 | operation->iv_size ) ); |
| 261 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 262 | return( status ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 263 | |
| 264 | *iv_length = operation->iv_size; |
| 265 | status = test_transparent_cipher_set_iv( operation, iv, *iv_length ); |
| 266 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 267 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | psa_status_t test_transparent_cipher_set_iv( |
| 271 | test_transparent_cipher_operation_t *operation, |
| 272 | const uint8_t *iv, |
| 273 | size_t iv_length) |
| 274 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 275 | psa_status_t status; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 276 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 277 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 278 | |
| 279 | if( operation->alg != PSA_ALG_CTR ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 280 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 281 | |
| 282 | if( operation->iv_set || ! operation->iv_required ) |
| 283 | return( PSA_ERROR_BAD_STATE ); |
| 284 | |
| 285 | if( iv_length != operation->iv_size ) |
| 286 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 287 | |
| 288 | status = mbedtls_to_psa_error( |
| 289 | mbedtls_cipher_set_iv( &operation->cipher, iv, iv_length ) ); |
| 290 | |
| 291 | if( status == PSA_SUCCESS ) |
| 292 | operation->iv_set = 1; |
| 293 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 294 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | psa_status_t test_transparent_cipher_update( |
| 298 | test_transparent_cipher_operation_t *operation, |
| 299 | const uint8_t *input, |
| 300 | size_t input_length, |
| 301 | uint8_t *output, |
| 302 | size_t output_size, |
| 303 | size_t *output_length) |
| 304 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 305 | size_t expected_output_size; |
| 306 | psa_status_t status; |
| 307 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 308 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 309 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 310 | if( operation->alg != PSA_ALG_CTR ) |
| 311 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 312 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 313 | expected_output_size = ( operation->cipher.unprocessed_len + input_length ) |
| 314 | / operation->block_size * operation->block_size; |
| 315 | |
| 316 | if( output_size < expected_output_size ) |
| 317 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 318 | |
| 319 | status = mbedtls_to_psa_error( |
| 320 | mbedtls_cipher_update( &operation->cipher, input, |
| 321 | input_length, output, output_length ) ); |
| 322 | |
| 323 | if( status != PSA_SUCCESS ) |
| 324 | return status; |
| 325 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 326 | if( test_driver_cipher_hooks.forced_output != NULL ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 327 | { |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 328 | if( output_size < test_driver_cipher_hooks.forced_output_length ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 329 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 330 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 331 | memcpy( output, |
| 332 | test_driver_cipher_hooks.forced_output, |
| 333 | test_driver_cipher_hooks.forced_output_length ); |
| 334 | *output_length = test_driver_cipher_hooks.forced_output_length; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 335 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 336 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 337 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | psa_status_t test_transparent_cipher_finish( |
| 341 | test_transparent_cipher_operation_t *operation, |
| 342 | uint8_t *output, |
| 343 | size_t output_size, |
| 344 | size_t *output_length) |
| 345 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 346 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 347 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
| 348 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 349 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 350 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 351 | if( operation->alg != PSA_ALG_CTR ) |
| 352 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 353 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 354 | if( ! operation->key_set ) |
| 355 | return( PSA_ERROR_BAD_STATE ); |
| 356 | |
| 357 | if( operation->iv_required && ! operation->iv_set ) |
| 358 | return( PSA_ERROR_BAD_STATE ); |
| 359 | |
| 360 | status = mbedtls_to_psa_error( |
| 361 | mbedtls_cipher_finish( &operation->cipher, |
| 362 | temp_output_buffer, |
| 363 | output_length ) ); |
| 364 | |
| 365 | mbedtls_cipher_free( &operation->cipher ); |
| 366 | |
| 367 | if( status != PSA_SUCCESS ) |
| 368 | return( status ); |
| 369 | |
| 370 | if( *output_length == 0 ) |
| 371 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
| 372 | else if( output_size >= *output_length ) |
| 373 | memcpy( output, temp_output_buffer, *output_length ); |
| 374 | else |
| 375 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 376 | |
| 377 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 378 | if( test_driver_cipher_hooks.forced_output != NULL ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 379 | { |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 380 | if( output_size < test_driver_cipher_hooks.forced_output_length ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 381 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 382 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 383 | memcpy( output, |
| 384 | test_driver_cipher_hooks.forced_output, |
| 385 | test_driver_cipher_hooks.forced_output_length ); |
| 386 | *output_length = test_driver_cipher_hooks.forced_output_length; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 387 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 388 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 389 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /* |
| 393 | * opaque versions, to do |
| 394 | */ |
| 395 | psa_status_t test_opaque_cipher_encrypt( |
| 396 | const psa_key_attributes_t *attributes, |
| 397 | const uint8_t *key, size_t key_length, |
| 398 | psa_algorithm_t alg, |
| 399 | const uint8_t *input, size_t input_length, |
| 400 | uint8_t *output, size_t output_size, size_t *output_length) |
| 401 | { |
| 402 | (void) attributes; |
| 403 | (void) key; |
| 404 | (void) key_length; |
| 405 | (void) alg; |
| 406 | (void) input; |
| 407 | (void) input_length; |
| 408 | (void) output; |
| 409 | (void) output_size; |
| 410 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 411 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | psa_status_t test_opaque_cipher_decrypt( |
| 415 | const psa_key_attributes_t *attributes, |
| 416 | const uint8_t *key, size_t key_length, |
| 417 | psa_algorithm_t alg, |
| 418 | const uint8_t *input, size_t input_length, |
| 419 | uint8_t *output, size_t output_size, size_t *output_length) |
| 420 | { |
| 421 | (void) attributes; |
| 422 | (void) key; |
| 423 | (void) key_length; |
| 424 | (void) alg; |
| 425 | (void) input; |
| 426 | (void) input_length; |
| 427 | (void) output; |
| 428 | (void) output_size; |
| 429 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 430 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | psa_status_t test_opaque_cipher_encrypt_setup( |
| 434 | test_opaque_cipher_operation_t *operation, |
| 435 | const psa_key_attributes_t *attributes, |
| 436 | const uint8_t *key, size_t key_length, |
| 437 | psa_algorithm_t alg) |
| 438 | { |
| 439 | (void) operation; |
| 440 | (void) attributes; |
| 441 | (void) key; |
| 442 | (void) key_length; |
| 443 | (void) alg; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 444 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | psa_status_t test_opaque_cipher_decrypt_setup( |
| 448 | test_opaque_cipher_operation_t *operation, |
| 449 | const psa_key_attributes_t *attributes, |
| 450 | const uint8_t *key, size_t key_length, |
| 451 | psa_algorithm_t alg) |
| 452 | { |
| 453 | (void) operation; |
| 454 | (void) attributes; |
| 455 | (void) key; |
| 456 | (void) key_length; |
| 457 | (void) alg; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 458 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | psa_status_t test_opaque_cipher_abort( |
| 462 | test_opaque_cipher_operation_t *operation) |
| 463 | { |
| 464 | (void) operation; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 465 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | psa_status_t test_opaque_cipher_generate_iv( |
| 469 | test_opaque_cipher_operation_t *operation, |
| 470 | uint8_t *iv, |
| 471 | size_t iv_size, |
| 472 | size_t *iv_length) |
| 473 | { |
| 474 | (void) operation; |
| 475 | (void) iv; |
| 476 | (void) iv_size; |
| 477 | (void) iv_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 478 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | psa_status_t test_opaque_cipher_set_iv( |
| 482 | test_opaque_cipher_operation_t *operation, |
| 483 | const uint8_t *iv, |
| 484 | size_t iv_length) |
| 485 | { |
| 486 | (void) operation; |
| 487 | (void) iv; |
| 488 | (void) iv_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 489 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | psa_status_t test_opaque_cipher_update( |
| 493 | test_opaque_cipher_operation_t *operation, |
| 494 | const uint8_t *input, |
| 495 | size_t input_length, |
| 496 | uint8_t *output, |
| 497 | size_t output_size, |
| 498 | size_t *output_length) |
| 499 | { |
| 500 | (void) operation; |
| 501 | (void) input; |
| 502 | (void) input_length; |
| 503 | (void) output; |
| 504 | (void) output_size; |
| 505 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 506 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | psa_status_t test_opaque_cipher_finish( |
| 510 | test_opaque_cipher_operation_t *operation, |
| 511 | uint8_t *output, |
| 512 | size_t output_size, |
| 513 | size_t *output_length) |
| 514 | { |
| 515 | (void) operation; |
| 516 | (void) output; |
| 517 | (void) output_size; |
| 518 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame^] | 519 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 520 | } |
| 521 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |