test_driver_cipher.c (13754B)
1 /* 2 * Test driver for cipher functions. 3 * Currently only supports multi-part operations using AES-CTR. 4 */ 5 /* Copyright The Mbed TLS Contributors 6 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 7 */ 8 9 #include <test/helpers.h> 10 11 #if defined(PSA_CRYPTO_DRIVER_TEST) 12 #include "psa/crypto.h" 13 #include "psa_crypto_cipher.h" 14 #include "psa_crypto_core.h" 15 #include "mbedtls/cipher.h" 16 17 #include "test/drivers/cipher.h" 18 19 #include "test/random.h" 20 21 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) 22 #if MBEDTLS_VERSION_MAJOR < 4 23 #include "libtestdriver1/library/psa_crypto_cipher.h" 24 #else 25 #include "libtestdriver1/tf-psa-crypto/drivers/builtin/src/psa_crypto_cipher.h" 26 #endif 27 #endif 28 29 #include <string.h> 30 31 mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks = 32 MBEDTLS_TEST_DRIVER_CIPHER_INIT; 33 34 psa_status_t mbedtls_test_transparent_cipher_encrypt( 35 const psa_key_attributes_t *attributes, 36 const uint8_t *key_buffer, 37 size_t key_buffer_size, 38 psa_algorithm_t alg, 39 const uint8_t *iv, 40 size_t iv_length, 41 const uint8_t *input, 42 size_t input_length, 43 uint8_t *output, 44 size_t output_size, 45 size_t *output_length) 46 { 47 mbedtls_test_driver_cipher_hooks.hits++; 48 mbedtls_test_driver_cipher_hooks.hits_encrypt++; 49 50 if (mbedtls_test_driver_cipher_hooks.forced_output != NULL) { 51 if (output_size < mbedtls_test_driver_cipher_hooks.forced_output_length) { 52 return PSA_ERROR_BUFFER_TOO_SMALL; 53 } 54 55 memcpy(output, 56 mbedtls_test_driver_cipher_hooks.forced_output, 57 mbedtls_test_driver_cipher_hooks.forced_output_length); 58 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length; 59 60 return mbedtls_test_driver_cipher_hooks.forced_status; 61 } 62 63 if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) { 64 return mbedtls_test_driver_cipher_hooks.forced_status; 65 } 66 if (mbedtls_test_driver_cipher_hooks.forced_status_encrypt != PSA_SUCCESS) { 67 return mbedtls_test_driver_cipher_hooks.forced_status_encrypt; 68 } 69 70 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ 71 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) 72 return libtestdriver1_mbedtls_psa_cipher_encrypt( 73 (const libtestdriver1_psa_key_attributes_t *) attributes, 74 key_buffer, key_buffer_size, 75 alg, iv, iv_length, input, input_length, 76 output, output_size, output_length); 77 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) 78 return mbedtls_psa_cipher_encrypt( 79 attributes, key_buffer, key_buffer_size, 80 alg, iv, iv_length, input, input_length, 81 output, output_size, output_length); 82 #endif 83 84 return PSA_ERROR_NOT_SUPPORTED; 85 } 86 87 psa_status_t mbedtls_test_transparent_cipher_decrypt( 88 const psa_key_attributes_t *attributes, 89 const uint8_t *key_buffer, 90 size_t key_buffer_size, 91 psa_algorithm_t alg, 92 const uint8_t *input, 93 size_t input_length, 94 uint8_t *output, 95 size_t output_size, 96 size_t *output_length) 97 { 98 mbedtls_test_driver_cipher_hooks.hits++; 99 100 if (mbedtls_test_driver_cipher_hooks.forced_output != NULL) { 101 if (output_size < mbedtls_test_driver_cipher_hooks.forced_output_length) { 102 return PSA_ERROR_BUFFER_TOO_SMALL; 103 } 104 105 memcpy(output, 106 mbedtls_test_driver_cipher_hooks.forced_output, 107 mbedtls_test_driver_cipher_hooks.forced_output_length); 108 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length; 109 110 return mbedtls_test_driver_cipher_hooks.forced_status; 111 } 112 113 if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) { 114 return mbedtls_test_driver_cipher_hooks.forced_status; 115 } 116 117 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ 118 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) 119 return libtestdriver1_mbedtls_psa_cipher_decrypt( 120 (const libtestdriver1_psa_key_attributes_t *) attributes, 121 key_buffer, key_buffer_size, 122 alg, input, input_length, 123 output, output_size, output_length); 124 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) 125 return mbedtls_psa_cipher_decrypt( 126 attributes, key_buffer, key_buffer_size, 127 alg, input, input_length, 128 output, output_size, output_length); 129 #endif 130 131 return PSA_ERROR_NOT_SUPPORTED; 132 } 133 134 psa_status_t mbedtls_test_transparent_cipher_encrypt_setup( 135 mbedtls_transparent_test_driver_cipher_operation_t *operation, 136 const psa_key_attributes_t *attributes, 137 const uint8_t *key, size_t key_length, 138 psa_algorithm_t alg) 139 { 140 mbedtls_test_driver_cipher_hooks.hits++; 141 142 if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) { 143 return mbedtls_test_driver_cipher_hooks.forced_status; 144 } 145 146 if (!MBEDTLS_TEST_OBJECT_IS_ALL_ZERO(operation)) { 147 return PSA_ERROR_TEST_DETECTED_BAD_INITIALIZATION; 148 } 149 150 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ 151 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) 152 return libtestdriver1_mbedtls_psa_cipher_encrypt_setup( 153 operation, 154 (const libtestdriver1_psa_key_attributes_t *) attributes, 155 key, key_length, alg); 156 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) 157 return mbedtls_psa_cipher_encrypt_setup( 158 operation, attributes, key, key_length, alg); 159 #endif 160 161 return PSA_ERROR_NOT_SUPPORTED; 162 } 163 164 psa_status_t mbedtls_test_transparent_cipher_decrypt_setup( 165 mbedtls_transparent_test_driver_cipher_operation_t *operation, 166 const psa_key_attributes_t *attributes, 167 const uint8_t *key, size_t key_length, 168 psa_algorithm_t alg) 169 { 170 mbedtls_test_driver_cipher_hooks.hits++; 171 172 if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) { 173 return mbedtls_test_driver_cipher_hooks.forced_status; 174 } 175 176 if (!MBEDTLS_TEST_OBJECT_IS_ALL_ZERO(operation)) { 177 return PSA_ERROR_TEST_DETECTED_BAD_INITIALIZATION; 178 } 179 180 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ 181 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) 182 return libtestdriver1_mbedtls_psa_cipher_decrypt_setup( 183 operation, 184 (const libtestdriver1_psa_key_attributes_t *) attributes, 185 key, key_length, alg); 186 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) 187 return mbedtls_psa_cipher_decrypt_setup( 188 operation, attributes, key, key_length, alg); 189 #endif 190 191 return PSA_ERROR_NOT_SUPPORTED; 192 } 193 194 psa_status_t mbedtls_test_transparent_cipher_abort( 195 mbedtls_transparent_test_driver_cipher_operation_t *operation) 196 { 197 mbedtls_test_driver_cipher_hooks.hits++; 198 199 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ 200 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) 201 libtestdriver1_mbedtls_psa_cipher_abort(operation); 202 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) 203 mbedtls_psa_cipher_abort(operation); 204 #endif 205 206 /* Wiping the entire struct here, instead of member-by-member. This is 207 * useful for the test suite, since it gives a chance of catching memory 208 * corruption errors should the core not have allocated (enough) memory for 209 * our context struct. */ 210 memset(operation, 0, sizeof(*operation)); 211 212 return mbedtls_test_driver_cipher_hooks.forced_status; 213 } 214 215 psa_status_t mbedtls_test_transparent_cipher_set_iv( 216 mbedtls_transparent_test_driver_cipher_operation_t *operation, 217 const uint8_t *iv, 218 size_t iv_length) 219 { 220 mbedtls_test_driver_cipher_hooks.hits++; 221 mbedtls_test_driver_cipher_hooks.hits_set_iv++; 222 223 if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) { 224 return mbedtls_test_driver_cipher_hooks.forced_status; 225 } 226 if (mbedtls_test_driver_cipher_hooks.forced_status_set_iv != PSA_SUCCESS) { 227 return mbedtls_test_driver_cipher_hooks.forced_status_set_iv; 228 } 229 230 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ 231 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) 232 return libtestdriver1_mbedtls_psa_cipher_set_iv( 233 operation, iv, iv_length); 234 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) 235 return mbedtls_psa_cipher_set_iv(operation, iv, iv_length); 236 #endif 237 238 return PSA_ERROR_NOT_SUPPORTED; 239 } 240 241 psa_status_t mbedtls_test_transparent_cipher_update( 242 mbedtls_transparent_test_driver_cipher_operation_t *operation, 243 const uint8_t *input, 244 size_t input_length, 245 uint8_t *output, 246 size_t output_size, 247 size_t *output_length) 248 { 249 mbedtls_test_driver_cipher_hooks.hits++; 250 251 if (mbedtls_test_driver_cipher_hooks.forced_output != NULL) { 252 if (output_size < mbedtls_test_driver_cipher_hooks.forced_output_length) { 253 return PSA_ERROR_BUFFER_TOO_SMALL; 254 } 255 256 memcpy(output, 257 mbedtls_test_driver_cipher_hooks.forced_output, 258 mbedtls_test_driver_cipher_hooks.forced_output_length); 259 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length; 260 261 return mbedtls_test_driver_cipher_hooks.forced_status; 262 } 263 264 if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) { 265 return mbedtls_test_driver_cipher_hooks.forced_status; 266 } 267 268 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ 269 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) 270 return libtestdriver1_mbedtls_psa_cipher_update( 271 operation, input, input_length, 272 output, output_size, output_length); 273 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) 274 return mbedtls_psa_cipher_update( 275 operation, input, input_length, 276 output, output_size, output_length); 277 #endif 278 279 return PSA_ERROR_NOT_SUPPORTED; 280 } 281 282 psa_status_t mbedtls_test_transparent_cipher_finish( 283 mbedtls_transparent_test_driver_cipher_operation_t *operation, 284 uint8_t *output, 285 size_t output_size, 286 size_t *output_length) 287 { 288 mbedtls_test_driver_cipher_hooks.hits++; 289 290 if (mbedtls_test_driver_cipher_hooks.forced_output != NULL) { 291 if (output_size < mbedtls_test_driver_cipher_hooks.forced_output_length) { 292 return PSA_ERROR_BUFFER_TOO_SMALL; 293 } 294 295 memcpy(output, 296 mbedtls_test_driver_cipher_hooks.forced_output, 297 mbedtls_test_driver_cipher_hooks.forced_output_length); 298 *output_length = mbedtls_test_driver_cipher_hooks.forced_output_length; 299 300 return mbedtls_test_driver_cipher_hooks.forced_status; 301 } 302 303 if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) { 304 return mbedtls_test_driver_cipher_hooks.forced_status; 305 } 306 307 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ 308 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER) 309 return libtestdriver1_mbedtls_psa_cipher_finish( 310 operation, output, output_size, output_length); 311 #elif defined(MBEDTLS_PSA_BUILTIN_CIPHER) 312 return mbedtls_psa_cipher_finish( 313 operation, output, output_size, output_length); 314 #endif 315 316 return PSA_ERROR_NOT_SUPPORTED; 317 } 318 319 /* 320 * opaque versions, to do 321 */ 322 psa_status_t mbedtls_test_opaque_cipher_encrypt( 323 const psa_key_attributes_t *attributes, 324 const uint8_t *key, size_t key_length, 325 psa_algorithm_t alg, 326 const uint8_t *iv, size_t iv_length, 327 const uint8_t *input, size_t input_length, 328 uint8_t *output, size_t output_size, size_t *output_length) 329 { 330 (void) attributes; 331 (void) key; 332 (void) key_length; 333 (void) alg; 334 (void) iv; 335 (void) iv_length; 336 (void) input; 337 (void) input_length; 338 (void) output; 339 (void) output_size; 340 (void) output_length; 341 return PSA_ERROR_NOT_SUPPORTED; 342 } 343 344 psa_status_t mbedtls_test_opaque_cipher_decrypt( 345 const psa_key_attributes_t *attributes, 346 const uint8_t *key, size_t key_length, 347 psa_algorithm_t alg, 348 const uint8_t *input, size_t input_length, 349 uint8_t *output, size_t output_size, size_t *output_length) 350 { 351 (void) attributes; 352 (void) key; 353 (void) key_length; 354 (void) alg; 355 (void) input; 356 (void) input_length; 357 (void) output; 358 (void) output_size; 359 (void) output_length; 360 return PSA_ERROR_NOT_SUPPORTED; 361 } 362 363 psa_status_t mbedtls_test_opaque_cipher_encrypt_setup( 364 mbedtls_opaque_test_driver_cipher_operation_t *operation, 365 const psa_key_attributes_t *attributes, 366 const uint8_t *key, size_t key_length, 367 psa_algorithm_t alg) 368 { 369 (void) operation; 370 (void) attributes; 371 (void) key; 372 (void) key_length; 373 (void) alg; 374 return PSA_ERROR_NOT_SUPPORTED; 375 } 376 377 psa_status_t mbedtls_test_opaque_cipher_decrypt_setup( 378 mbedtls_opaque_test_driver_cipher_operation_t *operation, 379 const psa_key_attributes_t *attributes, 380 const uint8_t *key, size_t key_length, 381 psa_algorithm_t alg) 382 { 383 (void) operation; 384 (void) attributes; 385 (void) key; 386 (void) key_length; 387 (void) alg; 388 return PSA_ERROR_NOT_SUPPORTED; 389 } 390 391 psa_status_t mbedtls_test_opaque_cipher_abort( 392 mbedtls_opaque_test_driver_cipher_operation_t *operation) 393 { 394 (void) operation; 395 return PSA_ERROR_NOT_SUPPORTED; 396 } 397 398 psa_status_t mbedtls_test_opaque_cipher_set_iv( 399 mbedtls_opaque_test_driver_cipher_operation_t *operation, 400 const uint8_t *iv, 401 size_t iv_length) 402 { 403 (void) operation; 404 (void) iv; 405 (void) iv_length; 406 return PSA_ERROR_NOT_SUPPORTED; 407 } 408 409 psa_status_t mbedtls_test_opaque_cipher_update( 410 mbedtls_opaque_test_driver_cipher_operation_t *operation, 411 const uint8_t *input, 412 size_t input_length, 413 uint8_t *output, 414 size_t output_size, 415 size_t *output_length) 416 { 417 (void) operation; 418 (void) input; 419 (void) input_length; 420 (void) output; 421 (void) output_size; 422 (void) output_length; 423 return PSA_ERROR_NOT_SUPPORTED; 424 } 425 426 psa_status_t mbedtls_test_opaque_cipher_finish( 427 mbedtls_opaque_test_driver_cipher_operation_t *operation, 428 uint8_t *output, 429 size_t output_size, 430 size_t *output_length) 431 { 432 (void) operation; 433 (void) output; 434 (void) output_size; 435 (void) output_length; 436 return PSA_ERROR_NOT_SUPPORTED; 437 } 438 #endif /* PSA_CRYPTO_DRIVER_TEST */