1/* BEGIN_HEADER */ 2#include "test/drivers/test_driver.h" 3/* END_HEADER */ 4 5/* BEGIN_DEPENDENCIES 6 * depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_PSA_CRYPTO_DRIVERS:PSA_CRYPTO_DRIVER_TEST 7 * END_DEPENDENCIES 8 */ 9 10/* BEGIN_CASE depends_on:PSA_WANT_ALG_DETERMINISTIC_ECDSA:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR:PSA_WANT_ECC_SECP_R1_256:PSA_WANT_ALG_SHA_256 */ 11void ecdsa_sign_hash( int force_status_arg, 12 data_t *key_input, 13 data_t *data_input, 14 data_t *expected_output, 15 int fake_output, 16 int expected_status_arg ) 17{ 18 psa_status_t force_status = force_status_arg; 19 psa_status_t expected_status = expected_status_arg; 20 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 21 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 22 psa_algorithm_t alg = PSA_ALG_DETERMINISTIC_ECDSA( PSA_ALG_SHA_256 ); 23 uint8_t signature[64]; 24 size_t signature_length = 0xdeadbeef; 25 psa_status_t actual_status; 26 mbedtls_test_driver_signature_sign_hooks = 27 mbedtls_test_driver_signature_hooks_init(); 28 29 PSA_ASSERT( psa_crypto_init( ) ); 30 psa_set_key_type( &attributes, 31 PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_FAMILY_SECP_R1 ) ); 32 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); 33 psa_set_key_algorithm( &attributes, alg ); 34 psa_import_key( &attributes, 35 key_input->x, key_input->len, 36 &key ); 37 38 mbedtls_test_driver_signature_sign_hooks.forced_status = force_status; 39 if( fake_output == 1 ) 40 { 41 mbedtls_test_driver_signature_sign_hooks.forced_output = 42 expected_output->x; 43 mbedtls_test_driver_signature_sign_hooks.forced_output_length = 44 expected_output->len; 45 } 46 47 actual_status = psa_sign_hash( key, alg, 48 data_input->x, data_input->len, 49 signature, sizeof( signature ), 50 &signature_length ); 51 TEST_EQUAL( actual_status, expected_status ); 52 if( expected_status == PSA_SUCCESS ) 53 { 54 ASSERT_COMPARE( signature, signature_length, 55 expected_output->x, expected_output->len ); 56 } 57 TEST_EQUAL( mbedtls_test_driver_signature_sign_hooks.hits, 1 ); 58 59exit: 60 psa_reset_key_attributes( &attributes ); 61 psa_destroy_key( key ); 62 PSA_DONE( ); 63 mbedtls_test_driver_signature_sign_hooks = 64 mbedtls_test_driver_signature_hooks_init(); 65} 66/* END_CASE */ 67 68/* BEGIN_CASE depends_on:PSA_WANT_ALG_ECDSA:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR:PSA_WANT_ECC_SECP_R1_256:PSA_WANT_ALG_SHA_256 */ 69void ecdsa_verify_hash( int force_status_arg, 70 int register_public_key, 71 data_t *key_input, 72 data_t *data_input, 73 data_t *signature_input, 74 int expected_status_arg ) 75{ 76 psa_status_t force_status = force_status_arg; 77 psa_status_t expected_status = expected_status_arg; 78 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 79 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 80 psa_algorithm_t alg = PSA_ALG_ECDSA( PSA_ALG_SHA_256 ); 81 psa_status_t actual_status; 82 mbedtls_test_driver_signature_verify_hooks = 83 mbedtls_test_driver_signature_hooks_init(); 84 85 PSA_ASSERT( psa_crypto_init( ) ); 86 if( register_public_key ) 87 { 88 psa_set_key_type( &attributes, 89 PSA_KEY_TYPE_ECC_PUBLIC_KEY( PSA_ECC_FAMILY_SECP_R1 ) ); 90 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); 91 psa_set_key_algorithm( &attributes, alg ); 92 psa_import_key( &attributes, 93 key_input->x, key_input->len, 94 &key ); 95 } 96 else 97 { 98 psa_set_key_type( &attributes, 99 PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_FAMILY_SECP_R1 ) ); 100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); 101 psa_set_key_algorithm( &attributes, alg ); 102 psa_import_key( &attributes, 103 key_input->x, key_input->len, 104 &key ); 105 } 106 107 mbedtls_test_driver_signature_verify_hooks.forced_status = force_status; 108 109 actual_status = psa_verify_hash( key, alg, 110 data_input->x, data_input->len, 111 signature_input->x, signature_input->len ); 112 TEST_EQUAL( actual_status, expected_status ); 113 TEST_EQUAL( mbedtls_test_driver_signature_verify_hooks.hits, 1 ); 114 115exit: 116 psa_reset_key_attributes( &attributes ); 117 psa_destroy_key( key ); 118 PSA_DONE( ); 119 mbedtls_test_driver_signature_verify_hooks = 120 mbedtls_test_driver_signature_hooks_init(); 121} 122/* END_CASE */ 123 124/* BEGIN_CASE depends_on:PSA_WANT_ALG_DETERMINISTIC_ECDSA:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR:PSA_WANT_ECC_SECP_R1_256:PSA_WANT_ALG_SHA_256 */ 125void ecdsa_sign_message( int force_status_arg, 126 data_t *key_input, 127 data_t *data_input, 128 data_t *expected_output, 129 int fake_output, 130 int expected_status_arg ) 131{ 132 psa_status_t force_status = force_status_arg; 133 psa_status_t expected_status = expected_status_arg; 134 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 135 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 136 psa_algorithm_t alg = PSA_ALG_DETERMINISTIC_ECDSA( PSA_ALG_SHA_256 ); 137 uint8_t signature[64]; 138 size_t signature_length = 0xdeadbeef; 139 psa_status_t actual_status; 140 mbedtls_test_driver_signature_sign_hooks = 141 mbedtls_test_driver_signature_hooks_init(); 142 143 PSA_ASSERT( psa_crypto_init( ) ); 144 psa_set_key_type( &attributes, 145 PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_FAMILY_SECP_R1 ) ); 146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); 147 psa_set_key_algorithm( &attributes, alg ); 148 psa_import_key( &attributes, 149 key_input->x, key_input->len, 150 &key ); 151 152 mbedtls_test_driver_signature_sign_hooks.forced_status = force_status; 153 if( fake_output == 1 ) 154 { 155 mbedtls_test_driver_signature_sign_hooks.forced_output = 156 expected_output->x; 157 mbedtls_test_driver_signature_sign_hooks.forced_output_length = 158 expected_output->len; 159 } 160 161 actual_status = psa_sign_message( key, alg, 162 data_input->x, data_input->len, 163 signature, sizeof( signature ), 164 &signature_length ); 165 TEST_EQUAL( actual_status, expected_status ); 166 if( expected_status == PSA_SUCCESS ) 167 { 168 ASSERT_COMPARE( signature, signature_length, 169 expected_output->x, expected_output->len ); 170 } 171 /* In the builtin algorithm the driver is called twice. */ 172 TEST_EQUAL( mbedtls_test_driver_signature_sign_hooks.hits, 173 force_status == PSA_ERROR_NOT_SUPPORTED ? 2 : 1 ); 174 175exit: 176 psa_reset_key_attributes( &attributes ); 177 psa_destroy_key( key ); 178 PSA_DONE( ); 179 mbedtls_test_driver_signature_sign_hooks = 180 mbedtls_test_driver_signature_hooks_init(); 181} 182/* END_CASE */ 183 184/* BEGIN_CASE depends_on:PSA_WANT_ALG_ECDSA:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR:PSA_WANT_ECC_SECP_R1_256:PSA_WANT_ALG_SHA_256 */ 185void ecdsa_verify_message( int force_status_arg, 186 int register_public_key, 187 data_t *key_input, 188 data_t *data_input, 189 data_t *signature_input, 190 int expected_status_arg ) 191{ 192 psa_status_t force_status = force_status_arg; 193 psa_status_t expected_status = expected_status_arg; 194 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 195 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 196 psa_algorithm_t alg = PSA_ALG_ECDSA( PSA_ALG_SHA_256 ); 197 psa_status_t actual_status; 198 mbedtls_test_driver_signature_verify_hooks = 199 mbedtls_test_driver_signature_hooks_init(); 200 201 PSA_ASSERT( psa_crypto_init( ) ); 202 if( register_public_key ) 203 { 204 psa_set_key_type( &attributes, 205 PSA_KEY_TYPE_ECC_PUBLIC_KEY( PSA_ECC_FAMILY_SECP_R1 ) ); 206 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); 207 psa_set_key_algorithm( &attributes, alg ); 208 psa_import_key( &attributes, 209 key_input->x, key_input->len, 210 &key ); 211 } 212 else 213 { 214 psa_set_key_type( &attributes, 215 PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_FAMILY_SECP_R1 ) ); 216 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); 217 psa_set_key_algorithm( &attributes, alg ); 218 psa_import_key( &attributes, 219 key_input->x, key_input->len, 220 &key ); 221 } 222 223 mbedtls_test_driver_signature_verify_hooks.forced_status = force_status; 224 225 actual_status = psa_verify_message( key, alg, 226 data_input->x, data_input->len, 227 signature_input->x, signature_input->len ); 228 TEST_EQUAL( actual_status, expected_status ); 229 /* In the builtin algorithm the driver is called twice. */ 230 TEST_EQUAL( mbedtls_test_driver_signature_verify_hooks.hits, 231 force_status == PSA_ERROR_NOT_SUPPORTED ? 2 : 1 ); 232 233exit: 234 psa_reset_key_attributes( &attributes ); 235 psa_destroy_key( key ); 236 PSA_DONE( ); 237 mbedtls_test_driver_signature_verify_hooks = 238 mbedtls_test_driver_signature_hooks_init(); 239} 240/* END_CASE */ 241 242/* BEGIN_CASE depends_on:PSA_WANT_ALG_ECDSA:PSA_WANT_ECC_SECP_R1_256 */ 243void generate_key( int force_status_arg, 244 data_t *fake_output, 245 int expected_status_arg ) 246{ 247 psa_status_t force_status = force_status_arg; 248 psa_status_t expected_status = expected_status_arg; 249 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 251 psa_algorithm_t alg = PSA_ALG_ECDSA( PSA_ALG_SHA_256 ); 252 const uint8_t *expected_output = NULL; 253 size_t expected_output_length = 0; 254 psa_status_t actual_status; 255 uint8_t actual_output[PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(256)] = {0}; 256 size_t actual_output_length; 257 mbedtls_test_driver_key_management_hooks = 258 mbedtls_test_driver_key_management_hooks_init(); 259 260 psa_set_key_type( &attributes, 261 PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_FAMILY_SECP_R1 ) ); 262 psa_set_key_bits( &attributes, 256 ); 263 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_EXPORT ); 264 psa_set_key_algorithm( &attributes, alg ); 265 266 if( fake_output->len > 0 ) 267 { 268 expected_output = 269 mbedtls_test_driver_key_management_hooks.forced_output = 270 fake_output->x; 271 272 expected_output_length = 273 mbedtls_test_driver_key_management_hooks.forced_output_length = 274 fake_output->len; 275 } 276 277 mbedtls_test_driver_key_management_hooks.hits = 0; 278 mbedtls_test_driver_key_management_hooks.forced_status = force_status; 279 280 PSA_ASSERT( psa_crypto_init( ) ); 281 282 actual_status = psa_generate_key( &attributes, &key ); 283 TEST_EQUAL( mbedtls_test_driver_key_management_hooks.hits, 1 ); 284 TEST_EQUAL( actual_status, expected_status ); 285 286 if( actual_status == PSA_SUCCESS ) 287 { 288 psa_export_key( key, actual_output, sizeof(actual_output), &actual_output_length ); 289 290 if( fake_output->len > 0 ) 291 { 292 ASSERT_COMPARE( actual_output, actual_output_length, 293 expected_output, expected_output_length ); 294 } 295 else 296 { 297 size_t zeroes = 0; 298 for( size_t i = 0; i < sizeof(actual_output); i++ ) 299 { 300 if( actual_output[i] == 0) 301 zeroes++; 302 } 303 TEST_ASSERT( zeroes != sizeof(actual_output) ); 304 } 305 } 306exit: 307 psa_reset_key_attributes( &attributes ); 308 psa_destroy_key( key ); 309 PSA_DONE( ); 310 mbedtls_test_driver_key_management_hooks = 311 mbedtls_test_driver_key_management_hooks_init(); 312} 313/* END_CASE */ 314 315/* BEGIN_CASE */ 316void validate_key( int force_status_arg, 317 int key_type_arg, 318 data_t *key_input, 319 int expected_status_arg ) 320{ 321 psa_status_t force_status = force_status_arg; 322 psa_status_t expected_status = expected_status_arg; 323 psa_key_type_t key_type = key_type_arg; 324 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 325 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 326 psa_status_t actual_status; 327 mbedtls_test_driver_key_management_hooks = 328 mbedtls_test_driver_key_management_hooks_init(); 329 330 psa_set_key_type( &attributes, 331 key_type ); 332 psa_set_key_bits( &attributes, 0 ); 333 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); 334 335 mbedtls_test_driver_key_management_hooks.forced_status = force_status; 336 337 PSA_ASSERT( psa_crypto_init( ) ); 338 339 actual_status = psa_import_key( &attributes, key_input->x, key_input->len, &key ); 340 TEST_EQUAL( mbedtls_test_driver_key_management_hooks.hits, 1 ); 341 TEST_EQUAL( actual_status, expected_status ); 342exit: 343 psa_reset_key_attributes( &attributes ); 344 psa_destroy_key( key ); 345 PSA_DONE( ); 346 mbedtls_test_driver_key_management_hooks = 347 mbedtls_test_driver_key_management_hooks_init(); 348} 349/* END_CASE */ 350 351/* BEGIN_CASE */ 352void export_key( int force_status_arg, 353 data_t *fake_output, 354 int key_in_type_arg, 355 data_t *key_in, 356 int key_out_type_arg, 357 data_t *expected_output, 358 int expected_status_arg ) 359{ 360 psa_status_t force_status = force_status_arg; 361 psa_status_t expected_status = expected_status_arg; 362 psa_key_handle_t handle = 0; 363 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 364 psa_key_type_t input_key_type = key_in_type_arg; 365 psa_key_type_t output_key_type = key_out_type_arg; 366 const uint8_t *expected_output_ptr = NULL; 367 size_t expected_output_length = 0; 368 psa_status_t actual_status; 369 uint8_t actual_output[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(256)] = {0}; 370 size_t actual_output_length; 371 mbedtls_test_driver_key_management_hooks = 372 mbedtls_test_driver_key_management_hooks_init(); 373 374 psa_set_key_type( &attributes, input_key_type ); 375 psa_set_key_bits( &attributes, 256 ); 376 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); 377 378 PSA_ASSERT( psa_crypto_init( ) ); 379 PSA_ASSERT( psa_import_key( &attributes, key_in->x, key_in->len, &handle ) ); 380 381 if( fake_output->len > 0 ) 382 { 383 expected_output_ptr = 384 mbedtls_test_driver_key_management_hooks.forced_output = 385 fake_output->x; 386 387 expected_output_length = 388 mbedtls_test_driver_key_management_hooks.forced_output_length = 389 fake_output->len; 390 } 391 else 392 { 393 expected_output_ptr = expected_output->x; 394 expected_output_length = expected_output->len; 395 } 396 397 mbedtls_test_driver_key_management_hooks.hits = 0; 398 mbedtls_test_driver_key_management_hooks.forced_status = force_status; 399 400 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( output_key_type ) ) 401 actual_status = psa_export_public_key( handle, actual_output, sizeof(actual_output), &actual_output_length ); 402 else 403 actual_status = psa_export_key( handle, actual_output, sizeof(actual_output), &actual_output_length ); 404 TEST_EQUAL( actual_status, expected_status ); 405 406 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( output_key_type ) && 407 !PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( input_key_type ) ) 408 TEST_EQUAL( mbedtls_test_driver_key_management_hooks.hits, 1 ); 409 410 if( actual_status == PSA_SUCCESS ) 411 { 412 ASSERT_COMPARE( actual_output, actual_output_length, 413 expected_output_ptr, expected_output_length ); 414 } 415exit: 416 psa_reset_key_attributes( &attributes ); 417 psa_destroy_key( handle ); 418 PSA_DONE( ); 419 mbedtls_test_driver_key_management_hooks = 420 mbedtls_test_driver_key_management_hooks_init(); 421} 422/* END_CASE */ 423 424/* BEGIN_CASE */ 425void cipher_encrypt_validation( int alg_arg, 426 int key_type_arg, 427 data_t *key_data, 428 data_t *input ) 429{ 430 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 431 psa_key_type_t key_type = key_type_arg; 432 psa_algorithm_t alg = alg_arg; 433 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); 434 unsigned char *output1 = NULL; 435 size_t output1_buffer_size = 0; 436 size_t output1_length = 0; 437 unsigned char *output2 = NULL; 438 size_t output2_buffer_size = 0; 439 size_t output2_length = 0; 440 size_t function_output_length = 0; 441 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; 442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 443 mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init(); 444 445 PSA_ASSERT( psa_crypto_init( ) ); 446 447 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); 448 psa_set_key_algorithm( &attributes, alg ); 449 psa_set_key_type( &attributes, key_type ); 450 451 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); 452 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + 453 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); 454 ASSERT_ALLOC( output1, output1_buffer_size ); 455 ASSERT_ALLOC( output2, output2_buffer_size ); 456 457 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, 458 &key ) ); 459 460 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, 461 output1_buffer_size, &output1_length ) ); 462 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 463 mbedtls_test_driver_cipher_hooks.hits = 0; 464 465 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); 466 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 467 mbedtls_test_driver_cipher_hooks.hits = 0; 468 469 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); 470 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 471 mbedtls_test_driver_cipher_hooks.hits = 0; 472 473 PSA_ASSERT( psa_cipher_update( &operation, 474 input->x, input->len, 475 output2, output2_buffer_size, 476 &function_output_length ) ); 477 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 478 mbedtls_test_driver_cipher_hooks.hits = 0; 479 480 output2_length += function_output_length; 481 PSA_ASSERT( psa_cipher_finish( &operation, 482 output2 + output2_length, 483 output2_buffer_size - output2_length, 484 &function_output_length ) ); 485 /* Finish will have called abort as well, so expecting two hits here */ 486 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 ); 487 mbedtls_test_driver_cipher_hooks.hits = 0; 488 489 output2_length += function_output_length; 490 491 PSA_ASSERT( psa_cipher_abort( &operation ) ); 492 // driver function should've been called as part of the finish() core routine 493 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 ); 494 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, 495 output2, output2_length ); 496 497exit: 498 psa_cipher_abort( &operation ); 499 mbedtls_free( output1 ); 500 mbedtls_free( output2 ); 501 psa_destroy_key( key ); 502 PSA_DONE( ); 503 mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init(); 504} 505/* END_CASE */ 506 507/* BEGIN_CASE */ 508void cipher_encrypt_multipart( int alg_arg, 509 int key_type_arg, 510 data_t *key_data, 511 data_t *iv, 512 data_t *input, 513 int first_part_size_arg, 514 int output1_length_arg, 515 int output2_length_arg, 516 data_t *expected_output, 517 int mock_output_arg, 518 int force_status_arg, 519 int expected_status_arg ) 520{ 521 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 522 psa_key_type_t key_type = key_type_arg; 523 psa_algorithm_t alg = alg_arg; 524 psa_status_t status; 525 psa_status_t expected_status = expected_status_arg; 526 psa_status_t force_status = force_status_arg; 527 size_t first_part_size = first_part_size_arg; 528 size_t output1_length = output1_length_arg; 529 size_t output2_length = output2_length_arg; 530 unsigned char *output = NULL; 531 size_t output_buffer_size = 0; 532 size_t function_output_length = 0; 533 size_t total_output_length = 0; 534 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; 535 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 536 mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init(); 537 mbedtls_test_driver_cipher_hooks.forced_status = force_status; 538 539 /* Test operation initialization */ 540 mbedtls_psa_cipher_operation_t mbedtls_operation = 541 MBEDTLS_PSA_CIPHER_OPERATION_INIT; 542 543 mbedtls_transparent_test_driver_cipher_operation_t tranparent_operation = 544 MBEDTLS_TRANSPARENT_TEST_DRIVER_CIPHER_OPERATION_INIT; 545 546 mbedtls_opaque_test_driver_cipher_operation_t opaque_operation = 547 MBEDTLS_OPAQUE_TEST_DRIVER_CIPHER_OPERATION_INIT; 548 549 operation.ctx.mbedtls_ctx = mbedtls_operation; 550 operation.ctx.transparent_test_driver_ctx = tranparent_operation; 551 operation.ctx.opaque_test_driver_ctx = opaque_operation; 552 553 PSA_ASSERT( psa_crypto_init( ) ); 554 555 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); 556 psa_set_key_algorithm( &attributes, alg ); 557 psa_set_key_type( &attributes, key_type ); 558 559 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, 560 &key ) ); 561 562 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); 563 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 564 mbedtls_test_driver_cipher_hooks.hits = 0; 565 566 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); 567 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, ( force_status == PSA_SUCCESS ? 1 : 0 ) ); 568 mbedtls_test_driver_cipher_hooks.hits = 0; 569 570 output_buffer_size = ( (size_t) input->len + 571 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) ); 572 ASSERT_ALLOC( output, output_buffer_size ); 573 574 if( mock_output_arg ) 575 { 576 mbedtls_test_driver_cipher_hooks.forced_output = expected_output->x; 577 mbedtls_test_driver_cipher_hooks.forced_output_length = expected_output->len; 578 } 579 580 TEST_ASSERT( first_part_size <= input->len ); 581 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, 582 output, output_buffer_size, 583 &function_output_length ) ); 584 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, ( force_status == PSA_SUCCESS ? 1 : 0 ) ); 585 mbedtls_test_driver_cipher_hooks.hits = 0; 586 587 TEST_ASSERT( function_output_length == output1_length ); 588 total_output_length += function_output_length; 589 590 if( first_part_size < input->len ) 591 { 592 PSA_ASSERT( psa_cipher_update( &operation, 593 input->x + first_part_size, 594 input->len - first_part_size, 595 output + total_output_length, 596 output_buffer_size - total_output_length, 597 &function_output_length ) ); 598 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 599 mbedtls_test_driver_cipher_hooks.hits = 0; 600 601 TEST_ASSERT( function_output_length == output2_length ); 602 total_output_length += function_output_length; 603 } 604 605 if( mock_output_arg ) 606 { 607 mbedtls_test_driver_cipher_hooks.forced_output = NULL; 608 mbedtls_test_driver_cipher_hooks.forced_output_length = 0; 609 } 610 611 status = psa_cipher_finish( &operation, 612 output + total_output_length, 613 output_buffer_size - total_output_length, 614 &function_output_length ); 615 /* Finish will have called abort as well, so expecting two hits here */ 616 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, ( force_status == PSA_SUCCESS ? 2 : 0 ) ); 617 mbedtls_test_driver_cipher_hooks.hits = 0 ; 618 total_output_length += function_output_length; 619 TEST_EQUAL( status, expected_status ); 620 621 if( expected_status == PSA_SUCCESS ) 622 { 623 PSA_ASSERT( psa_cipher_abort( &operation ) ); 624 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 ); 625 626 ASSERT_COMPARE( expected_output->x, expected_output->len, 627 output, total_output_length ); 628 } 629 630exit: 631 psa_cipher_abort( &operation ); 632 mbedtls_free( output ); 633 psa_destroy_key( key ); 634 PSA_DONE( ); 635 mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init(); 636} 637/* END_CASE */ 638 639/* BEGIN_CASE */ 640void cipher_decrypt_multipart( int alg_arg, 641 int key_type_arg, 642 data_t *key_data, 643 data_t *iv, 644 data_t *input, 645 int first_part_size_arg, 646 int output1_length_arg, 647 int output2_length_arg, 648 data_t *expected_output, 649 int mock_output_arg, 650 int force_status_arg, 651 int expected_status_arg ) 652{ 653 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 654 psa_key_type_t key_type = key_type_arg; 655 psa_algorithm_t alg = alg_arg; 656 psa_status_t status; 657 psa_status_t expected_status = expected_status_arg; 658 psa_status_t force_status = force_status_arg; 659 size_t first_part_size = first_part_size_arg; 660 size_t output1_length = output1_length_arg; 661 size_t output2_length = output2_length_arg; 662 unsigned char *output = NULL; 663 size_t output_buffer_size = 0; 664 size_t function_output_length = 0; 665 size_t total_output_length = 0; 666 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; 667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 668 mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init(); 669 mbedtls_test_driver_cipher_hooks.forced_status = force_status; 670 671 /* Test operation initialization */ 672 mbedtls_psa_cipher_operation_t mbedtls_operation = 673 MBEDTLS_PSA_CIPHER_OPERATION_INIT; 674 675 mbedtls_transparent_test_driver_cipher_operation_t tranparent_operation = 676 MBEDTLS_TRANSPARENT_TEST_DRIVER_CIPHER_OPERATION_INIT; 677 678 mbedtls_opaque_test_driver_cipher_operation_t opaque_operation = 679 MBEDTLS_OPAQUE_TEST_DRIVER_CIPHER_OPERATION_INIT; 680 681 operation.ctx.mbedtls_ctx = mbedtls_operation; 682 operation.ctx.transparent_test_driver_ctx = tranparent_operation; 683 operation.ctx.opaque_test_driver_ctx = opaque_operation; 684 685 PSA_ASSERT( psa_crypto_init( ) ); 686 687 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); 688 psa_set_key_algorithm( &attributes, alg ); 689 psa_set_key_type( &attributes, key_type ); 690 691 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, 692 &key ) ); 693 694 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); 695 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 696 mbedtls_test_driver_cipher_hooks.hits = 0; 697 698 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); 699 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, ( force_status == PSA_SUCCESS ? 1 : 0 ) ); 700 mbedtls_test_driver_cipher_hooks.hits = 0; 701 702 output_buffer_size = ( (size_t) input->len + 703 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) ); 704 ASSERT_ALLOC( output, output_buffer_size ); 705 706 if( mock_output_arg ) 707 { 708 mbedtls_test_driver_cipher_hooks.forced_output = expected_output->x; 709 mbedtls_test_driver_cipher_hooks.forced_output_length = expected_output->len; 710 } 711 712 TEST_ASSERT( first_part_size <= input->len ); 713 PSA_ASSERT( psa_cipher_update( &operation, 714 input->x, first_part_size, 715 output, output_buffer_size, 716 &function_output_length ) ); 717 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, ( force_status == PSA_SUCCESS ? 1 : 0 ) ); 718 mbedtls_test_driver_cipher_hooks.hits = 0; 719 720 TEST_ASSERT( function_output_length == output1_length ); 721 total_output_length += function_output_length; 722 723 if( first_part_size < input->len ) 724 { 725 PSA_ASSERT( psa_cipher_update( &operation, 726 input->x + first_part_size, 727 input->len - first_part_size, 728 output + total_output_length, 729 output_buffer_size - total_output_length, 730 &function_output_length ) ); 731 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, ( force_status == PSA_SUCCESS ? 1 : 0 ) ); 732 mbedtls_test_driver_cipher_hooks.hits = 0; 733 734 TEST_ASSERT( function_output_length == output2_length ); 735 total_output_length += function_output_length; 736 } 737 738 if( mock_output_arg ) 739 { 740 mbedtls_test_driver_cipher_hooks.forced_output = NULL; 741 mbedtls_test_driver_cipher_hooks.forced_output_length = 0; 742 } 743 744 status = psa_cipher_finish( &operation, 745 output + total_output_length, 746 output_buffer_size - total_output_length, 747 &function_output_length ); 748 /* Finish will have called abort as well, so expecting two hits here */ 749 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, ( force_status == PSA_SUCCESS ? 2 : 0 ) ); 750 mbedtls_test_driver_cipher_hooks.hits = 0; 751 total_output_length += function_output_length; 752 TEST_EQUAL( status, expected_status ); 753 754 if( expected_status == PSA_SUCCESS ) 755 { 756 PSA_ASSERT( psa_cipher_abort( &operation ) ); 757 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 ); 758 759 ASSERT_COMPARE( expected_output->x, expected_output->len, 760 output, total_output_length ); 761 } 762 763exit: 764 psa_cipher_abort( &operation ); 765 mbedtls_free( output ); 766 psa_destroy_key( key ); 767 PSA_DONE( ); 768 mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init(); 769} 770/* END_CASE */ 771 772/* BEGIN_CASE */ 773void cipher_decrypt( int alg_arg, 774 int key_type_arg, 775 data_t *key_data, 776 data_t *iv, 777 data_t *input_arg, 778 data_t *expected_output, 779 int mock_output_arg, 780 int force_status_arg, 781 int expected_status_arg ) 782{ 783 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 784 psa_status_t status; 785 psa_key_type_t key_type = key_type_arg; 786 psa_algorithm_t alg = alg_arg; 787 psa_status_t expected_status = expected_status_arg; 788 psa_status_t force_status = force_status_arg; 789 unsigned char *input = NULL; 790 size_t input_buffer_size = 0; 791 unsigned char *output = NULL; 792 size_t output_buffer_size = 0; 793 size_t output_length = 0; 794 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 795 mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init(); 796 mbedtls_test_driver_cipher_hooks.forced_status = force_status; 797 798 PSA_ASSERT( psa_crypto_init( ) ); 799 800 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); 801 psa_set_key_algorithm( &attributes, alg ); 802 psa_set_key_type( &attributes, key_type ); 803 804 /* Allocate input buffer and copy the iv and the plaintext */ 805 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); 806 if ( input_buffer_size > 0 ) 807 { 808 ASSERT_ALLOC( input, input_buffer_size ); 809 memcpy( input, iv->x, iv->len ); 810 memcpy( input + iv->len, input_arg->x, input_arg->len ); 811 } 812 813 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); 814 ASSERT_ALLOC( output, output_buffer_size ); 815 816 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, 817 &key ) ); 818 819 if( mock_output_arg ) 820 { 821 mbedtls_test_driver_cipher_hooks.forced_output = expected_output->x; 822 mbedtls_test_driver_cipher_hooks.forced_output_length = expected_output->len; 823 } 824 825 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, 826 output_buffer_size, &output_length ); 827 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 828 mbedtls_test_driver_cipher_hooks.hits = 0; 829 830 TEST_EQUAL( status, expected_status ); 831 832 if( expected_status == PSA_SUCCESS ) 833 { 834 ASSERT_COMPARE( expected_output->x, expected_output->len, 835 output, output_length ); 836 } 837 838exit: 839 mbedtls_free( input ); 840 mbedtls_free( output ); 841 psa_destroy_key( key ); 842 PSA_DONE( ); 843 mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init(); 844} 845/* END_CASE */ 846 847/* BEGIN_CASE */ 848void cipher_entry_points( int alg_arg, int key_type_arg, 849 data_t *key_data, data_t *iv, 850 data_t *input ) 851{ 852 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 853 psa_status_t status; 854 psa_key_type_t key_type = key_type_arg; 855 psa_algorithm_t alg = alg_arg; 856 unsigned char *output = NULL; 857 size_t output_buffer_size = 0; 858 size_t function_output_length = 0; 859 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; 860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 861 mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init(); 862 863 ASSERT_ALLOC( output, input->len + 16 ); 864 output_buffer_size = input->len + 16; 865 866 PSA_ASSERT( psa_crypto_init( ) ); 867 868 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); 869 psa_set_key_algorithm( &attributes, alg ); 870 psa_set_key_type( &attributes, key_type ); 871 872 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, 873 &key ) ); 874 875 /* 876 * Test encrypt failure 877 * First test that if we don't force a driver error, encryption is 878 * successfull, then force driver error. 879 */ 880 status = psa_cipher_encrypt( 881 key, alg, input->x, input->len, 882 output, output_buffer_size, &function_output_length ); 883 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 884 TEST_EQUAL( status, PSA_SUCCESS ); 885 mbedtls_test_driver_cipher_hooks.hits = 0; 886 887 mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR; 888 /* Set the output buffer in a given state. */ 889 for( size_t i = 0; i < output_buffer_size; i++ ) 890 output[i] = 0xa5; 891 892 status = psa_cipher_encrypt( 893 key, alg, input->x, input->len, 894 output, output_buffer_size, &function_output_length ); 895 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 896 TEST_EQUAL( status, PSA_ERROR_GENERIC_ERROR ); 897 /* 898 * Check that the output buffer is still in the same state. 899 * This will fail if the output buffer is used by the core to pass the IV 900 * it generated to the driver (and is not restored). 901 */ 902 for( size_t i = 0; i < output_buffer_size; i++ ) 903 { 904 TEST_EQUAL( output[i], 0xa5 ); 905 } 906 mbedtls_test_driver_cipher_hooks.hits = 0; 907 908 /* Test setup call, encrypt */ 909 mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR; 910 status = psa_cipher_encrypt_setup( &operation, key, alg ); 911 /* When setup fails, it shouldn't call any further entry points */ 912 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 913 TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status ); 914 mbedtls_test_driver_cipher_hooks.hits = 0; 915 status = psa_cipher_set_iv( &operation, iv->x, iv->len ); 916 TEST_EQUAL( status, PSA_ERROR_BAD_STATE ); 917 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 ); 918 919 /* Test setup call failure, decrypt */ 920 status = psa_cipher_decrypt_setup( &operation, key, alg ); 921 /* When setup fails, it shouldn't call any further entry points */ 922 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 923 TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status ); 924 mbedtls_test_driver_cipher_hooks.hits = 0; 925 status = psa_cipher_set_iv( &operation, iv->x, iv->len ); 926 TEST_EQUAL( status, PSA_ERROR_BAD_STATE ); 927 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 ); 928 929 /* Test IV setting failure */ 930 mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS; 931 status = psa_cipher_encrypt_setup( &operation, key, alg ); 932 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 933 TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status ); 934 mbedtls_test_driver_cipher_hooks.hits = 0; 935 936 mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR; 937 status = psa_cipher_set_iv( &operation, iv->x, iv->len ); 938 /* When setting the IV fails, it should call abort too */ 939 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 ); 940 TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status ); 941 /* Failure should prevent further operations from executing on the driver */ 942 mbedtls_test_driver_cipher_hooks.hits = 0; 943 status = psa_cipher_update( &operation, 944 input->x, input->len, 945 output, output_buffer_size, 946 &function_output_length ); 947 TEST_EQUAL( status, PSA_ERROR_BAD_STATE ); 948 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 ); 949 psa_cipher_abort( &operation ); 950 951 /* Test IV generation failure */ 952 mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS; 953 status = psa_cipher_encrypt_setup( &operation, key, alg ); 954 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 955 TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status ); 956 mbedtls_test_driver_cipher_hooks.hits = 0; 957 958 mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR; 959 /* Set the output buffer in a given state. */ 960 for( size_t i = 0; i < 16; i++ ) 961 output[i] = 0xa5; 962 963 status = psa_cipher_generate_iv( &operation, output, 16, &function_output_length ); 964 /* When generating the IV fails, it should call abort too */ 965 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 ); 966 TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status ); 967 /* 968 * Check that the output buffer is still in the same state. 969 * This will fail if the output buffer is used by the core to pass the IV 970 * it generated to the driver (and is not restored). 971 */ 972 for( size_t i = 0; i < 16; i++ ) 973 { 974 TEST_EQUAL( output[i], 0xa5 ); 975 } 976 /* Failure should prevent further operations from executing on the driver */ 977 mbedtls_test_driver_cipher_hooks.hits = 0; 978 status = psa_cipher_update( &operation, 979 input->x, input->len, 980 output, output_buffer_size, 981 &function_output_length ); 982 TEST_EQUAL( status, PSA_ERROR_BAD_STATE ); 983 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 ); 984 psa_cipher_abort( &operation ); 985 986 /* Test update failure */ 987 mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS; 988 status = psa_cipher_encrypt_setup( &operation, key, alg ); 989 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 990 TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status ); 991 mbedtls_test_driver_cipher_hooks.hits = 0; 992 993 status = psa_cipher_set_iv( &operation, iv->x, iv->len ); 994 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 995 TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status ); 996 mbedtls_test_driver_cipher_hooks.hits = 0; 997 998 mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR; 999 status = psa_cipher_update( &operation, 1000 input->x, input->len, 1001 output, output_buffer_size, 1002 &function_output_length ); 1003 /* When the update call fails, it should call abort too */ 1004 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 ); 1005 TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status ); 1006 /* Failure should prevent further operations from executing on the driver */ 1007 mbedtls_test_driver_cipher_hooks.hits = 0; 1008 status = psa_cipher_update( &operation, 1009 input->x, input->len, 1010 output, output_buffer_size, 1011 &function_output_length ); 1012 TEST_EQUAL( status, PSA_ERROR_BAD_STATE ); 1013 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 ); 1014 psa_cipher_abort( &operation ); 1015 1016 /* Test finish failure */ 1017 mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS; 1018 status = psa_cipher_encrypt_setup( &operation, key, alg ); 1019 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 1020 TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status ); 1021 mbedtls_test_driver_cipher_hooks.hits = 0; 1022 1023 status = psa_cipher_set_iv( &operation, iv->x, iv->len ); 1024 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 1025 TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status ); 1026 mbedtls_test_driver_cipher_hooks.hits = 0; 1027 1028 status = psa_cipher_update( &operation, 1029 input->x, input->len, 1030 output, output_buffer_size, 1031 &function_output_length ); 1032 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 ); 1033 TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status ); 1034 mbedtls_test_driver_cipher_hooks.hits = 0; 1035 1036 mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR; 1037 status = psa_cipher_finish( &operation, 1038 output + function_output_length, 1039 output_buffer_size - function_output_length, 1040 &function_output_length ); 1041 /* When the finish call fails, it should call abort too */ 1042 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 ); 1043 TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status ); 1044 /* Failure should prevent further operations from executing on the driver */ 1045 mbedtls_test_driver_cipher_hooks.hits = 0; 1046 status = psa_cipher_update( &operation, 1047 input->x, input->len, 1048 output, output_buffer_size, 1049 &function_output_length ); 1050 TEST_EQUAL( status, PSA_ERROR_BAD_STATE ); 1051 TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 ); 1052 psa_cipher_abort( &operation ); 1053 1054exit: 1055 psa_cipher_abort( &operation ); 1056 mbedtls_free( output ); 1057 psa_destroy_key( key ); 1058 PSA_DONE( ); 1059 mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init(); 1060} 1061/* END_CASE */ 1062 1063/* BEGIN_CASE */ 1064void aead_encrypt( int key_type_arg, data_t *key_data, 1065 int alg_arg, 1066 data_t *nonce, 1067 data_t *additional_data, 1068 data_t *input_data, 1069 data_t *expected_result, 1070 int forced_status_arg ) 1071{ 1072 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 1073 psa_key_type_t key_type = key_type_arg; 1074 psa_algorithm_t alg = alg_arg; 1075 size_t key_bits; 1076 psa_status_t forced_status = forced_status_arg; 1077 unsigned char *output_data = NULL; 1078 size_t output_size = 0; 1079 size_t output_length = 0; 1080 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 1081 psa_status_t status = PSA_ERROR_GENERIC_ERROR; 1082 mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init(); 1083 1084 PSA_ASSERT( psa_crypto_init( ) ); 1085 1086 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); 1087 psa_set_key_algorithm( &attributes, alg ); 1088 psa_set_key_type( &attributes, key_type ); 1089 1090 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, 1091 &key ) ); 1092 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); 1093 key_bits = psa_get_key_bits( &attributes ); 1094 1095 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, 1096 alg ); 1097 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE 1098 * should be exact. */ 1099 TEST_EQUAL( output_size, 1100 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); 1101 TEST_ASSERT( output_size <= 1102 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); 1103 ASSERT_ALLOC( output_data, output_size ); 1104 1105 mbedtls_test_driver_aead_hooks.forced_status = forced_status; 1106 status = psa_aead_encrypt( key, alg, 1107 nonce->x, nonce->len, 1108 additional_data->x, additional_data->len, 1109 input_data->x, input_data->len, 1110 output_data, output_size, 1111 &output_length ); 1112 TEST_EQUAL( mbedtls_test_driver_aead_hooks.hits_encrypt, 1 ); 1113 TEST_EQUAL( mbedtls_test_driver_aead_hooks.driver_status, forced_status ); 1114 1115 TEST_EQUAL( status, ( forced_status == PSA_ERROR_NOT_SUPPORTED ) ? 1116 PSA_SUCCESS : forced_status ); 1117 1118 if( status == PSA_SUCCESS ) 1119 { 1120 ASSERT_COMPARE( expected_result->x, expected_result->len, 1121 output_data, output_length ); 1122 } 1123 1124exit: 1125 psa_destroy_key( key ); 1126 mbedtls_free( output_data ); 1127 PSA_DONE( ); 1128 mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init(); 1129} 1130/* END_CASE */ 1131 1132/* BEGIN_CASE */ 1133void aead_decrypt( int key_type_arg, data_t *key_data, 1134 int alg_arg, 1135 data_t *nonce, 1136 data_t *additional_data, 1137 data_t *input_data, 1138 data_t *expected_data, 1139 int forced_status_arg ) 1140{ 1141 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 1142 psa_key_type_t key_type = key_type_arg; 1143 psa_algorithm_t alg = alg_arg; 1144 size_t key_bits; 1145 psa_status_t forced_status = forced_status_arg; 1146 unsigned char *output_data = NULL; 1147 size_t output_size = 0; 1148 size_t output_length = 0; 1149 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 1150 psa_status_t status = PSA_ERROR_GENERIC_ERROR; 1151 mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init(); 1152 1153 PSA_ASSERT( psa_crypto_init( ) ); 1154 1155 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); 1156 psa_set_key_algorithm( &attributes, alg ); 1157 psa_set_key_type( &attributes, key_type ); 1158 1159 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, 1160 &key ) ); 1161 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); 1162 key_bits = psa_get_key_bits( &attributes ); 1163 1164 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, 1165 alg ); 1166 ASSERT_ALLOC( output_data, output_size ); 1167 1168 mbedtls_test_driver_aead_hooks.forced_status = forced_status; 1169 status = psa_aead_decrypt( key, alg, 1170 nonce->x, nonce->len, 1171 additional_data->x, 1172 additional_data->len, 1173 input_data->x, input_data->len, 1174 output_data, output_size, 1175 &output_length ); 1176 TEST_EQUAL( mbedtls_test_driver_aead_hooks.hits_decrypt, 1 ); 1177 TEST_EQUAL( mbedtls_test_driver_aead_hooks.driver_status, forced_status ); 1178 1179 TEST_EQUAL( status, ( forced_status == PSA_ERROR_NOT_SUPPORTED ) ? 1180 PSA_SUCCESS : forced_status ); 1181 1182 if( status == PSA_SUCCESS ) 1183 { 1184 ASSERT_COMPARE( expected_data->x, expected_data->len, 1185 output_data, output_length ); 1186 } 1187 1188exit: 1189 psa_destroy_key( key ); 1190 mbedtls_free( output_data ); 1191 PSA_DONE( ); 1192 mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init(); 1193} 1194/* END_CASE */ 1195 1196/* BEGIN_CASE */ 1197void mac_sign( int key_type_arg, 1198 data_t *key_data, 1199 int alg_arg, 1200 data_t *input, 1201 data_t *expected_mac, 1202 int forced_status_arg ) 1203{ 1204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 1205 psa_key_type_t key_type = key_type_arg; 1206 psa_algorithm_t alg = alg_arg; 1207 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; 1208 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 1209 uint8_t *actual_mac = NULL; 1210 size_t mac_buffer_size = 1211 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg ); 1212 size_t mac_length = 0; 1213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; 1214 psa_status_t forced_status = forced_status_arg; 1215 mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init(); 1216 1217 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); 1218 /* We expect PSA_MAC_LENGTH to be exact. */ 1219 TEST_ASSERT( expected_mac->len == mac_buffer_size ); 1220 1221 PSA_ASSERT( psa_crypto_init( ) ); 1222 1223 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); 1224 psa_set_key_algorithm( &attributes, alg ); 1225 psa_set_key_type( &attributes, key_type ); 1226 1227 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, 1228 &key ) ); 1229 1230 ASSERT_ALLOC( actual_mac, mac_buffer_size ); 1231 mbedtls_test_driver_mac_hooks.forced_status = forced_status; 1232 1233 /* 1234 * Calculate the MAC, one-shot case. 1235 */ 1236 status = psa_mac_compute( key, alg, 1237 input->x, input->len, 1238 actual_mac, mac_buffer_size, 1239 &mac_length ); 1240 1241 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 ); 1242 if( forced_status == PSA_SUCCESS || 1243 forced_status == PSA_ERROR_NOT_SUPPORTED ) 1244 { 1245 PSA_ASSERT( status ); 1246 } 1247 else 1248 TEST_EQUAL( forced_status, status ); 1249 1250 if( mac_buffer_size > 0 ) 1251 memset( actual_mac, 0, mac_buffer_size ); 1252 mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init(); 1253 mbedtls_test_driver_mac_hooks.forced_status = forced_status; 1254 1255 /* 1256 * Calculate the MAC, multipart case. 1257 */ 1258 status = psa_mac_sign_setup( &operation, key, alg ); 1259 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 ); 1260 1261 if( forced_status == PSA_SUCCESS || 1262 forced_status == PSA_ERROR_NOT_SUPPORTED ) 1263 { 1264 PSA_ASSERT( status ); 1265 } 1266 else 1267 TEST_EQUAL( forced_status, status ); 1268 1269 status = psa_mac_update( &operation, 1270 input->x, input->len ); 1271 if( forced_status == PSA_SUCCESS ) 1272 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 2 ); 1273 else 1274 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 ); 1275 if( forced_status == PSA_SUCCESS || 1276 forced_status == PSA_ERROR_NOT_SUPPORTED ) 1277 { 1278 PSA_ASSERT( status ); 1279 } 1280 else 1281 TEST_EQUAL( PSA_ERROR_BAD_STATE, status ); 1282 1283 status = psa_mac_sign_finish( &operation, 1284 actual_mac, mac_buffer_size, 1285 &mac_length ); 1286 if( forced_status == PSA_SUCCESS ) 1287 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 4 ); 1288 else 1289 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 ); 1290 1291 if( forced_status == PSA_SUCCESS || 1292 forced_status == PSA_ERROR_NOT_SUPPORTED ) 1293 { 1294 PSA_ASSERT( status ); 1295 } 1296 else 1297 TEST_EQUAL( PSA_ERROR_BAD_STATE, status ); 1298 1299 PSA_ASSERT( psa_mac_abort( &operation ) ); 1300 if( forced_status == PSA_SUCCESS ) 1301 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 4 ); 1302 else 1303 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 ); 1304 1305 if( forced_status == PSA_SUCCESS ) 1306 { 1307 ASSERT_COMPARE( expected_mac->x, expected_mac->len, 1308 actual_mac, mac_length ); 1309 } 1310 1311 mbedtls_free( actual_mac ); 1312 actual_mac = NULL; 1313 1314exit: 1315 psa_mac_abort( &operation ); 1316 psa_destroy_key( key ); 1317 PSA_DONE( ); 1318 mbedtls_free( actual_mac ); 1319 mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init(); 1320} 1321/* END_CASE */ 1322 1323/* BEGIN_CASE */ 1324void mac_verify( int key_type_arg, 1325 data_t *key_data, 1326 int alg_arg, 1327 data_t *input, 1328 data_t *expected_mac, 1329 int forced_status_arg ) 1330{ 1331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; 1332 psa_key_type_t key_type = key_type_arg; 1333 psa_algorithm_t alg = alg_arg; 1334 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; 1335 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 1336 psa_status_t status = PSA_ERROR_GENERIC_ERROR; 1337 psa_status_t forced_status = forced_status_arg; 1338 mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init(); 1339 1340 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); 1341 1342 PSA_ASSERT( psa_crypto_init( ) ); 1343 1344 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); 1345 psa_set_key_algorithm( &attributes, alg ); 1346 psa_set_key_type( &attributes, key_type ); 1347 1348 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, 1349 &key ) ); 1350 1351 mbedtls_test_driver_mac_hooks.forced_status = forced_status; 1352 1353 /* 1354 * Verify the MAC, one-shot case. 1355 */ 1356 status = psa_mac_verify( key, alg, 1357 input->x, input->len, 1358 expected_mac->x, expected_mac->len ); 1359 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 ); 1360 if( forced_status == PSA_SUCCESS || 1361 forced_status == PSA_ERROR_NOT_SUPPORTED ) 1362 { 1363 PSA_ASSERT( status ); 1364 } 1365 else 1366 TEST_EQUAL( forced_status, status ); 1367 1368 mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init(); 1369 mbedtls_test_driver_mac_hooks.forced_status = forced_status; 1370 1371 /* 1372 * Verify the MAC, multi-part case. 1373 */ 1374 status = psa_mac_verify_setup( &operation, key, alg ); 1375 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 ); 1376 1377 if( forced_status == PSA_SUCCESS || 1378 forced_status == PSA_ERROR_NOT_SUPPORTED ) 1379 { 1380 PSA_ASSERT( status ); 1381 } 1382 else 1383 TEST_EQUAL( forced_status, status ); 1384 1385 status = psa_mac_update( &operation, 1386 input->x, input->len ); 1387 if( forced_status == PSA_SUCCESS ) 1388 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 2 ); 1389 else 1390 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 ); 1391 1392 if( forced_status == PSA_SUCCESS || 1393 forced_status == PSA_ERROR_NOT_SUPPORTED ) 1394 { 1395 PSA_ASSERT( status ); 1396 } 1397 else 1398 TEST_EQUAL( PSA_ERROR_BAD_STATE, status ); 1399 1400 status = psa_mac_verify_finish( &operation, 1401 expected_mac->x, 1402 expected_mac->len ); 1403 if( forced_status == PSA_SUCCESS ) 1404 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 4 ); 1405 else 1406 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 ); 1407 1408 if( forced_status == PSA_SUCCESS || 1409 forced_status == PSA_ERROR_NOT_SUPPORTED ) 1410 { 1411 PSA_ASSERT( status ); 1412 } 1413 else 1414 TEST_EQUAL( PSA_ERROR_BAD_STATE, status ); 1415 1416 1417 PSA_ASSERT( psa_mac_abort( &operation ) ); 1418 if( forced_status == PSA_SUCCESS ) 1419 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 4 ); 1420 else 1421 TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 ); 1422 1423exit: 1424 psa_mac_abort( &operation ); 1425 psa_destroy_key( key ); 1426 PSA_DONE( ); 1427 mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init(); 1428} 1429/* END_CASE */ 1430 1431/* BEGIN_CASE depends_on:PSA_CRYPTO_DRIVER_TEST:MBEDTLS_PSA_CRYPTO_DRIVERS:MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ 1432void builtin_key_export( int builtin_key_id_arg, 1433 int builtin_key_type_arg, 1434 int builtin_key_bits_arg, 1435 int builtin_key_algorithm_arg, 1436 data_t *expected_output, 1437 int expected_status_arg ) 1438{ 1439 psa_key_id_t builtin_key_id = (psa_key_id_t) builtin_key_id_arg; 1440 psa_key_type_t builtin_key_type = (psa_key_type_t) builtin_key_type_arg; 1441 psa_algorithm_t builtin_key_alg = (psa_algorithm_t) builtin_key_algorithm_arg; 1442 size_t builtin_key_bits = (size_t) builtin_key_bits_arg; 1443 psa_status_t expected_status = expected_status_arg; 1444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 1445 1446 mbedtls_svc_key_id_t key = mbedtls_svc_key_id_make( 0, builtin_key_id ); 1447 uint8_t* output_buffer = NULL; 1448 size_t output_size = 0; 1449 psa_status_t actual_status; 1450 1451 PSA_ASSERT( psa_crypto_init( ) ); 1452 ASSERT_ALLOC( output_buffer, expected_output->len ); 1453 1454 actual_status = psa_export_key( key, output_buffer, expected_output->len, &output_size ); 1455 1456 if( expected_status == PSA_SUCCESS ) 1457 { 1458 PSA_ASSERT( actual_status ); 1459 TEST_EQUAL( output_size, expected_output->len ); 1460 ASSERT_COMPARE( output_buffer, output_size, 1461 expected_output->x, expected_output->len ); 1462 1463 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); 1464 TEST_EQUAL( psa_get_key_bits( &attributes ), builtin_key_bits ); 1465 TEST_EQUAL( psa_get_key_type( &attributes ), builtin_key_type ); 1466 TEST_EQUAL( psa_get_key_algorithm( &attributes ), builtin_key_alg ); 1467 } 1468 else 1469 { 1470 if( actual_status != expected_status ) 1471 fprintf( stderr, "Expected %d but got %d\n", expected_status, actual_status ); 1472 TEST_EQUAL( actual_status, expected_status ); 1473 TEST_EQUAL( output_size, 0 ); 1474 } 1475 1476exit: 1477 mbedtls_free( output_buffer ); 1478 psa_reset_key_attributes( &attributes ); 1479 psa_destroy_key( key ); 1480 PSA_DONE( ); 1481} 1482/* END_CASE */ 1483 1484/* BEGIN_CASE depends_on:PSA_CRYPTO_DRIVER_TEST:MBEDTLS_PSA_CRYPTO_DRIVERS:MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ 1485void builtin_pubkey_export( int builtin_key_id_arg, 1486 int builtin_key_type_arg, 1487 int builtin_key_bits_arg, 1488 int builtin_key_algorithm_arg, 1489 data_t *expected_output, 1490 int expected_status_arg ) 1491{ 1492 psa_key_id_t builtin_key_id = (psa_key_id_t) builtin_key_id_arg; 1493 psa_key_type_t builtin_key_type = (psa_key_type_t) builtin_key_type_arg; 1494 psa_algorithm_t builtin_key_alg = (psa_algorithm_t) builtin_key_algorithm_arg; 1495 size_t builtin_key_bits = (size_t) builtin_key_bits_arg; 1496 psa_status_t expected_status = expected_status_arg; 1497 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 1498 1499 mbedtls_svc_key_id_t key = mbedtls_svc_key_id_make( 0, builtin_key_id ); 1500 uint8_t* output_buffer = NULL; 1501 size_t output_size = 0; 1502 psa_status_t actual_status; 1503 1504 PSA_ASSERT( psa_crypto_init( ) ); 1505 ASSERT_ALLOC( output_buffer, expected_output->len ); 1506 1507 actual_status = psa_export_public_key( key, output_buffer, expected_output->len, &output_size ); 1508 1509 if( expected_status == PSA_SUCCESS ) 1510 { 1511 PSA_ASSERT( actual_status ); 1512 TEST_EQUAL( output_size, expected_output->len ); 1513 ASSERT_COMPARE( output_buffer, output_size, 1514 expected_output->x, expected_output->len ); 1515 1516 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); 1517 TEST_EQUAL( psa_get_key_bits( &attributes ), builtin_key_bits ); 1518 TEST_EQUAL( psa_get_key_type( &attributes ), builtin_key_type ); 1519 TEST_EQUAL( psa_get_key_algorithm( &attributes ), builtin_key_alg ); 1520 } 1521 else 1522 { 1523 TEST_EQUAL( actual_status, expected_status ); 1524 TEST_EQUAL( output_size, 0 ); 1525 } 1526 1527exit: 1528 mbedtls_free( output_buffer ); 1529 psa_reset_key_attributes( &attributes ); 1530 psa_destroy_key( key ); 1531 PSA_DONE( ); 1532} 1533/* END_CASE */ 1534 1535/* BEGIN_CASE */ 1536void hash_compute( int alg_arg, 1537 data_t *input, data_t *hash, 1538 int forced_status_arg, 1539 int expected_status_arg ) 1540{ 1541 psa_algorithm_t alg = alg_arg; 1542 psa_status_t forced_status = forced_status_arg; 1543 psa_status_t expected_status = expected_status_arg; 1544 unsigned char *output = NULL; 1545 size_t output_length; 1546 1547 mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init(); 1548 mbedtls_test_driver_hash_hooks.forced_status = forced_status; 1549 1550 PSA_ASSERT( psa_crypto_init( ) ); 1551 ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) ); 1552 1553 TEST_EQUAL( psa_hash_compute( alg, input->x, input->len, 1554 output, PSA_HASH_LENGTH( alg ), 1555 &output_length ), expected_status ); 1556 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 ); 1557 TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status ); 1558 1559 if( expected_status == PSA_SUCCESS ) 1560 { 1561 ASSERT_COMPARE( output, output_length, hash->x, hash->len ); 1562 } 1563 1564exit: 1565 mbedtls_free( output ); 1566 PSA_DONE( ); 1567 mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init(); 1568} 1569/* END_CASE */ 1570 1571/* BEGIN_CASE */ 1572void hash_multipart_setup( int alg_arg, 1573 data_t *input, data_t *hash, 1574 int forced_status_arg, 1575 int expected_status_arg ) 1576{ 1577 psa_algorithm_t alg = alg_arg; 1578 psa_status_t forced_status = forced_status_arg; 1579 psa_status_t expected_status = expected_status_arg; 1580 unsigned char *output = NULL; 1581 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; 1582 size_t output_length; 1583 1584 mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init(); 1585 ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) ); 1586 1587 PSA_ASSERT( psa_crypto_init( ) ); 1588 1589 mbedtls_test_driver_hash_hooks.forced_status = forced_status; 1590 TEST_EQUAL( psa_hash_setup( &operation, alg ), expected_status ); 1591 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 ); 1592 TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status ); 1593 1594 if( expected_status == PSA_SUCCESS ) 1595 { 1596 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); 1597 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1598 forced_status == PSA_ERROR_NOT_SUPPORTED ? 1 : 2 ); 1599 TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status ); 1600 1601 PSA_ASSERT( psa_hash_finish( &operation, 1602 output, PSA_HASH_LENGTH( alg ), 1603 &output_length ) ); 1604 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1605 forced_status == PSA_ERROR_NOT_SUPPORTED ? 1 : 4 ); 1606 TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status ); 1607 1608 ASSERT_COMPARE( output, output_length, hash->x, hash->len ); 1609 } 1610 1611exit: 1612 psa_hash_abort( &operation ); 1613 mbedtls_free( output ); 1614 PSA_DONE( ); 1615 mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init(); 1616} 1617/* END_CASE */ 1618 1619/* BEGIN_CASE */ 1620void hash_multipart_update( int alg_arg, 1621 data_t *input, data_t *hash, 1622 int forced_status_arg ) 1623{ 1624 psa_algorithm_t alg = alg_arg; 1625 psa_status_t forced_status = forced_status_arg; 1626 unsigned char *output = NULL; 1627 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; 1628 size_t output_length; 1629 1630 mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init(); 1631 ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) ); 1632 1633 PSA_ASSERT( psa_crypto_init( ) ); 1634 1635 /* 1636 * Update inactive operation, the driver shouldn't be called. 1637 */ 1638 TEST_EQUAL( psa_hash_update( &operation, input->x, input->len ), 1639 PSA_ERROR_BAD_STATE ); 1640 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 0 ); 1641 1642 PSA_ASSERT( psa_hash_setup( &operation, alg ) ); 1643 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 ); 1644 TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS ); 1645 1646 mbedtls_test_driver_hash_hooks.forced_status = forced_status; 1647 TEST_EQUAL( psa_hash_update( &operation, input->x, input->len ), 1648 forced_status ); 1649 /* One or two more calls to the driver interface: update or update + abort */ 1650 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1651 forced_status == PSA_SUCCESS ? 2 : 3 ); 1652 TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status ); 1653 1654 if( forced_status == PSA_SUCCESS ) 1655 { 1656 mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init(); 1657 PSA_ASSERT( psa_hash_finish( &operation, 1658 output, PSA_HASH_LENGTH( alg ), 1659 &output_length ) ); 1660 /* Two calls to the driver interface: update + abort */ 1661 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 2 ); 1662 TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS ); 1663 1664 ASSERT_COMPARE( output, output_length, hash->x, hash->len ); 1665 } 1666 1667exit: 1668 psa_hash_abort( &operation ); 1669 mbedtls_free( output ); 1670 PSA_DONE( ); 1671 mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init(); 1672} 1673/* END_CASE */ 1674 1675/* BEGIN_CASE */ 1676void hash_multipart_finish( int alg_arg, 1677 data_t *input, data_t *hash, 1678 int forced_status_arg ) 1679{ 1680 psa_algorithm_t alg = alg_arg; 1681 psa_status_t forced_status = forced_status_arg; 1682 unsigned char *output = NULL; 1683 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; 1684 size_t output_length; 1685 1686 mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init(); 1687 ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) ); 1688 1689 PSA_ASSERT( psa_crypto_init( ) ); 1690 1691 /* 1692 * Finish inactive operation, the driver shouldn't be called. 1693 */ 1694 TEST_EQUAL( psa_hash_finish( &operation, output, PSA_HASH_LENGTH( alg ), 1695 &output_length ), 1696 PSA_ERROR_BAD_STATE ); 1697 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 0 ); 1698 1699 PSA_ASSERT( psa_hash_setup( &operation, alg ) ); 1700 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 ); 1701 TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS ); 1702 1703 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); 1704 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 2 ); 1705 TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS ); 1706 1707 mbedtls_test_driver_hash_hooks.forced_status = forced_status; 1708 TEST_EQUAL( psa_hash_finish( &operation, 1709 output, PSA_HASH_LENGTH( alg ), 1710 &output_length ), 1711 forced_status ); 1712 /* Two more calls to the driver interface: finish + abort */ 1713 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 4 ); 1714 TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status ); 1715 1716 if( forced_status == PSA_SUCCESS ) 1717 ASSERT_COMPARE( output, output_length, hash->x, hash->len ); 1718 1719exit: 1720 psa_hash_abort( &operation ); 1721 mbedtls_free( output ); 1722 PSA_DONE( ); 1723 mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init(); 1724} 1725/* END_CASE */ 1726 1727/* BEGIN_CASE */ 1728void hash_clone( int alg_arg, 1729 data_t *input, data_t *hash, 1730 int forced_status_arg ) 1731{ 1732 psa_algorithm_t alg = alg_arg; 1733 psa_status_t forced_status = forced_status_arg; 1734 unsigned char *output = NULL; 1735 psa_hash_operation_t source_operation = PSA_HASH_OPERATION_INIT; 1736 psa_hash_operation_t target_operation = PSA_HASH_OPERATION_INIT; 1737 size_t output_length; 1738 1739 mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init(); 1740 ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) ); 1741 1742 PSA_ASSERT( psa_crypto_init( ) ); 1743 1744 /* 1745 * Clone inactive operation, the driver shouldn't be called. 1746 */ 1747 TEST_EQUAL( psa_hash_clone( &source_operation, &target_operation ), 1748 PSA_ERROR_BAD_STATE ); 1749 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 0 ); 1750 1751 PSA_ASSERT( psa_hash_setup( &source_operation, alg ) ); 1752 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 ); 1753 TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS ); 1754 1755 mbedtls_test_driver_hash_hooks.forced_status = forced_status; 1756 TEST_EQUAL( psa_hash_clone( &source_operation, &target_operation ), 1757 forced_status ); 1758 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1759 forced_status == PSA_SUCCESS ? 2 : 3 ); 1760 TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status ); 1761 1762 if( forced_status == PSA_SUCCESS ) 1763 { 1764 mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init(); 1765 PSA_ASSERT( psa_hash_update( &target_operation, 1766 input->x, input->len ) ); 1767 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 ); 1768 TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS ); 1769 1770 PSA_ASSERT( psa_hash_finish( &target_operation, 1771 output, PSA_HASH_LENGTH( alg ), 1772 &output_length ) ); 1773 TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 3 ); 1774 TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS ); 1775 1776 ASSERT_COMPARE( output, output_length, hash->x, hash->len ); 1777 } 1778 1779exit: 1780 psa_hash_abort( &source_operation ); 1781 psa_hash_abort( &target_operation ); 1782 mbedtls_free( output ); 1783 PSA_DONE( ); 1784 mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init(); 1785} 1786/* END_CASE */ 1787