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 | |
Steven Cooreman | 16afd3d | 2020-09-09 15:36:39 +0200 | [diff] [blame^] | 101 | static psa_status_t test_transparent_cipher_setup( |
| 102 | mbedtls_operation_t direction, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 103 | test_transparent_cipher_operation_t *operation, |
| 104 | const psa_key_attributes_t *attributes, |
| 105 | const uint8_t *key, size_t key_length, |
| 106 | psa_algorithm_t alg) |
| 107 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 108 | const mbedtls_cipher_info_t *cipher_info = NULL; |
| 109 | int ret = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 110 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 111 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 112 | |
| 113 | if( operation->alg != 0 ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 114 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 115 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 116 | /* Wiping the entire struct here, instead of member-by-member. This is useful |
| 117 | * for the test suite, since it gives a chance of catching memory corruption |
| 118 | * errors should the core not have allocated (enough) memory for our context |
| 119 | * struct. */ |
| 120 | memset( operation, 0, sizeof( *operation ) ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 121 | |
| 122 | /* Test driver supports AES-CTR only, to verify operation calls. */ |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 123 | if( alg != PSA_ALG_CTR || |
| 124 | psa_get_key_type( attributes ) != PSA_KEY_TYPE_AES ) |
| 125 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 126 | |
| 127 | operation->alg = alg; |
| 128 | operation->iv_size = 16; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 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 ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 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, |
Steven Cooreman | 16afd3d | 2020-09-09 15:36:39 +0200 | [diff] [blame^] | 145 | key_length * 8, direction ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 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 | |
Steven Cooreman | 16afd3d | 2020-09-09 15:36:39 +0200 | [diff] [blame^] | 162 | psa_status_t test_transparent_cipher_encrypt_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 | { |
| 168 | return ( test_transparent_cipher_setup( MBEDTLS_ENCRYPT, |
| 169 | operation, |
| 170 | attributes, |
| 171 | key, |
| 172 | key_length, |
| 173 | alg ) ); |
| 174 | } |
| 175 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 176 | psa_status_t test_transparent_cipher_decrypt_setup( |
| 177 | test_transparent_cipher_operation_t *operation, |
| 178 | const psa_key_attributes_t *attributes, |
| 179 | const uint8_t *key, size_t key_length, |
| 180 | psa_algorithm_t alg) |
| 181 | { |
Steven Cooreman | 16afd3d | 2020-09-09 15:36:39 +0200 | [diff] [blame^] | 182 | return ( test_transparent_cipher_setup( MBEDTLS_DECRYPT, |
| 183 | operation, |
| 184 | attributes, |
| 185 | key, |
| 186 | key_length, |
| 187 | alg ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | psa_status_t test_transparent_cipher_abort( |
| 191 | test_transparent_cipher_operation_t *operation) |
| 192 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 193 | if( operation->alg == 0 ) |
| 194 | return( PSA_SUCCESS ); |
| 195 | if( operation->alg != PSA_ALG_CTR ) |
| 196 | return( PSA_ERROR_BAD_STATE ); |
| 197 | |
| 198 | mbedtls_cipher_free( &operation->cipher ); |
| 199 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 200 | /* Wiping the entire struct here, instead of member-by-member. This is useful |
| 201 | * for the test suite, since it gives a chance of catching memory corruption |
| 202 | * errors should the core not have allocated (enough) memory for our context |
| 203 | * struct. */ |
| 204 | memset( operation, 0, sizeof( *operation ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 205 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 206 | test_driver_cipher_hooks.hits++; |
| 207 | return( PSA_SUCCESS ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | psa_status_t test_transparent_cipher_generate_iv( |
| 211 | test_transparent_cipher_operation_t *operation, |
| 212 | uint8_t *iv, |
| 213 | size_t iv_size, |
| 214 | size_t *iv_length) |
| 215 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 216 | psa_status_t status; |
| 217 | mbedtls_test_rnd_pseudo_info rnd_info; |
| 218 | memset( &rnd_info, 0x5A, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 219 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 220 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 221 | |
| 222 | if( operation->alg != PSA_ALG_CTR ) |
| 223 | return( PSA_ERROR_BAD_STATE ); |
| 224 | |
| 225 | if( operation->iv_set || ! operation->iv_required ) |
| 226 | return( PSA_ERROR_BAD_STATE ); |
| 227 | |
| 228 | if( iv_size < operation->iv_size ) |
| 229 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 230 | |
| 231 | status = mbedtls_to_psa_error( |
| 232 | mbedtls_test_rnd_pseudo_rand( &rnd_info, |
| 233 | iv, |
| 234 | operation->iv_size ) ); |
| 235 | if( status != PSA_SUCCESS ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 236 | return( status ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 237 | |
| 238 | *iv_length = operation->iv_size; |
| 239 | status = test_transparent_cipher_set_iv( operation, iv, *iv_length ); |
| 240 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 241 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | psa_status_t test_transparent_cipher_set_iv( |
| 245 | test_transparent_cipher_operation_t *operation, |
| 246 | const uint8_t *iv, |
| 247 | size_t iv_length) |
| 248 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 249 | psa_status_t status; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 250 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 251 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 252 | |
| 253 | if( operation->alg != PSA_ALG_CTR ) |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 254 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 255 | |
| 256 | if( operation->iv_set || ! operation->iv_required ) |
| 257 | return( PSA_ERROR_BAD_STATE ); |
| 258 | |
| 259 | if( iv_length != operation->iv_size ) |
| 260 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 261 | |
| 262 | status = mbedtls_to_psa_error( |
| 263 | mbedtls_cipher_set_iv( &operation->cipher, iv, iv_length ) ); |
| 264 | |
| 265 | if( status == PSA_SUCCESS ) |
| 266 | operation->iv_set = 1; |
| 267 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 268 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | psa_status_t test_transparent_cipher_update( |
| 272 | test_transparent_cipher_operation_t *operation, |
| 273 | const uint8_t *input, |
| 274 | size_t input_length, |
| 275 | uint8_t *output, |
| 276 | size_t output_size, |
| 277 | size_t *output_length) |
| 278 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 279 | psa_status_t status; |
| 280 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 281 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 282 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 283 | if( operation->alg != PSA_ALG_CTR ) |
| 284 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 285 | |
Steven Cooreman | 16afd3d | 2020-09-09 15:36:39 +0200 | [diff] [blame^] | 286 | /* CTR is a stream cipher, so data in and out are always the same size */ |
| 287 | if( output_size < input_length ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 288 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 289 | |
| 290 | status = mbedtls_to_psa_error( |
| 291 | mbedtls_cipher_update( &operation->cipher, input, |
| 292 | input_length, output, output_length ) ); |
| 293 | |
| 294 | if( status != PSA_SUCCESS ) |
| 295 | return status; |
| 296 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 297 | if( test_driver_cipher_hooks.forced_output != NULL ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 298 | { |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 299 | if( output_size < test_driver_cipher_hooks.forced_output_length ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 300 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 301 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 302 | memcpy( output, |
| 303 | test_driver_cipher_hooks.forced_output, |
| 304 | test_driver_cipher_hooks.forced_output_length ); |
| 305 | *output_length = test_driver_cipher_hooks.forced_output_length; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 306 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 307 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 308 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | psa_status_t test_transparent_cipher_finish( |
| 312 | test_transparent_cipher_operation_t *operation, |
| 313 | uint8_t *output, |
| 314 | size_t output_size, |
| 315 | size_t *output_length) |
| 316 | { |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 317 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 318 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
| 319 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 320 | test_driver_cipher_hooks.hits++; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 321 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 322 | if( operation->alg != PSA_ALG_CTR ) |
| 323 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 324 | |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 325 | if( ! operation->key_set ) |
| 326 | return( PSA_ERROR_BAD_STATE ); |
| 327 | |
| 328 | if( operation->iv_required && ! operation->iv_set ) |
| 329 | return( PSA_ERROR_BAD_STATE ); |
| 330 | |
| 331 | status = mbedtls_to_psa_error( |
| 332 | mbedtls_cipher_finish( &operation->cipher, |
| 333 | temp_output_buffer, |
| 334 | output_length ) ); |
| 335 | |
| 336 | mbedtls_cipher_free( &operation->cipher ); |
| 337 | |
| 338 | if( status != PSA_SUCCESS ) |
| 339 | return( status ); |
| 340 | |
| 341 | if( *output_length == 0 ) |
| 342 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
| 343 | else if( output_size >= *output_length ) |
| 344 | memcpy( output, temp_output_buffer, *output_length ); |
| 345 | else |
| 346 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 347 | |
| 348 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 349 | if( test_driver_cipher_hooks.forced_output != NULL ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 350 | { |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 351 | if( output_size < test_driver_cipher_hooks.forced_output_length ) |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 352 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 353 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 354 | memcpy( output, |
| 355 | test_driver_cipher_hooks.forced_output, |
| 356 | test_driver_cipher_hooks.forced_output_length ); |
| 357 | *output_length = test_driver_cipher_hooks.forced_output_length; |
Steven Cooreman | 8b12225 | 2020-09-03 15:30:32 +0200 | [diff] [blame] | 358 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 359 | |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 360 | return( test_driver_cipher_hooks.forced_status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /* |
| 364 | * opaque versions, to do |
| 365 | */ |
| 366 | psa_status_t test_opaque_cipher_encrypt( |
| 367 | const psa_key_attributes_t *attributes, |
| 368 | const uint8_t *key, size_t key_length, |
| 369 | psa_algorithm_t alg, |
| 370 | const uint8_t *input, size_t input_length, |
| 371 | uint8_t *output, size_t output_size, size_t *output_length) |
| 372 | { |
| 373 | (void) attributes; |
| 374 | (void) key; |
| 375 | (void) key_length; |
| 376 | (void) alg; |
| 377 | (void) input; |
| 378 | (void) input_length; |
| 379 | (void) output; |
| 380 | (void) output_size; |
| 381 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 382 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | psa_status_t test_opaque_cipher_decrypt( |
| 386 | const psa_key_attributes_t *attributes, |
| 387 | const uint8_t *key, size_t key_length, |
| 388 | psa_algorithm_t alg, |
| 389 | const uint8_t *input, size_t input_length, |
| 390 | uint8_t *output, size_t output_size, size_t *output_length) |
| 391 | { |
| 392 | (void) attributes; |
| 393 | (void) key; |
| 394 | (void) key_length; |
| 395 | (void) alg; |
| 396 | (void) input; |
| 397 | (void) input_length; |
| 398 | (void) output; |
| 399 | (void) output_size; |
| 400 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 401 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | psa_status_t test_opaque_cipher_encrypt_setup( |
| 405 | test_opaque_cipher_operation_t *operation, |
| 406 | const psa_key_attributes_t *attributes, |
| 407 | const uint8_t *key, size_t key_length, |
| 408 | psa_algorithm_t alg) |
| 409 | { |
| 410 | (void) operation; |
| 411 | (void) attributes; |
| 412 | (void) key; |
| 413 | (void) key_length; |
| 414 | (void) alg; |
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_setup( |
| 419 | test_opaque_cipher_operation_t *operation, |
| 420 | const psa_key_attributes_t *attributes, |
| 421 | const uint8_t *key, size_t key_length, |
| 422 | psa_algorithm_t alg) |
| 423 | { |
| 424 | (void) operation; |
| 425 | (void) attributes; |
| 426 | (void) key; |
| 427 | (void) key_length; |
| 428 | (void) alg; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 429 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | psa_status_t test_opaque_cipher_abort( |
| 433 | test_opaque_cipher_operation_t *operation) |
| 434 | { |
| 435 | (void) operation; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 436 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | psa_status_t test_opaque_cipher_generate_iv( |
| 440 | test_opaque_cipher_operation_t *operation, |
| 441 | uint8_t *iv, |
| 442 | size_t iv_size, |
| 443 | size_t *iv_length) |
| 444 | { |
| 445 | (void) operation; |
| 446 | (void) iv; |
| 447 | (void) iv_size; |
| 448 | (void) iv_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 449 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | psa_status_t test_opaque_cipher_set_iv( |
| 453 | test_opaque_cipher_operation_t *operation, |
| 454 | const uint8_t *iv, |
| 455 | size_t iv_length) |
| 456 | { |
| 457 | (void) operation; |
| 458 | (void) iv; |
| 459 | (void) iv_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 460 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | psa_status_t test_opaque_cipher_update( |
| 464 | test_opaque_cipher_operation_t *operation, |
| 465 | const uint8_t *input, |
| 466 | size_t input_length, |
| 467 | uint8_t *output, |
| 468 | size_t output_size, |
| 469 | size_t *output_length) |
| 470 | { |
| 471 | (void) operation; |
| 472 | (void) input; |
| 473 | (void) input_length; |
| 474 | (void) output; |
| 475 | (void) output_size; |
| 476 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 477 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | psa_status_t test_opaque_cipher_finish( |
| 481 | test_opaque_cipher_operation_t *operation, |
| 482 | uint8_t *output, |
| 483 | size_t output_size, |
| 484 | size_t *output_length) |
| 485 | { |
| 486 | (void) operation; |
| 487 | (void) output; |
| 488 | (void) output_size; |
| 489 | (void) output_length; |
Steven Cooreman | acb5a10 | 2020-09-08 14:06:57 +0200 | [diff] [blame] | 490 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 491 | } |
| 492 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |