@@ -2447,6 +2447,9 @@ exit:
2447
2447
/* BEGIN_CASE */
2448
2448
void cipher_operation_init( )
2449
2449
{
2450
+ const uint8_t input[1] = { 0 };
2451
+ unsigned char output[1] = { 0 };
2452
+ size_t output_length;
2450
2453
/* Test each valid way of initializing the object, except for `= {0}`, as
2451
2454
* Clang 5 complains when `-Wmissing-field-initializers` is used, even
2452
2455
* though it's OK by the C standard. We could test for this, but we'd need
@@ -2457,6 +2460,23 @@ void cipher_operation_init( )
2457
2460
2458
2461
memset( &zero, 0, sizeof( zero ) );
2459
2462
2463
+ /* A freshly-initialized cipher operation should not be usable. */
2464
+ TEST_EQUAL( psa_cipher_update( &func,
2465
+ input, sizeof( input ),
2466
+ output, sizeof( output ),
2467
+ &output_length ),
2468
+ PSA_ERROR_BAD_STATE );
2469
+ TEST_EQUAL( psa_cipher_update( &init,
2470
+ input, sizeof( input ),
2471
+ output, sizeof( output ),
2472
+ &output_length ),
2473
+ PSA_ERROR_BAD_STATE );
2474
+ TEST_EQUAL( psa_cipher_update( &zero,
2475
+ input, sizeof( input ),
2476
+ output, sizeof( output ),
2477
+ &output_length ),
2478
+ PSA_ERROR_BAD_STATE );
2479
+
2460
2480
/* A default cipher operation should be abortable without error. */
2461
2481
PSA_ASSERT( psa_cipher_abort( &func ) );
2462
2482
PSA_ASSERT( psa_cipher_abort( &init ) );
@@ -2497,6 +2517,147 @@ exit:
2497
2517
}
2498
2518
/* END_CASE */
2499
2519
2520
+ /* BEGIN_CASE */
2521
+ void cipher_bad_order( )
2522
+ {
2523
+ psa_key_handle_t handle = 0;
2524
+ psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2525
+ psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
2526
+ psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2527
+ psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2528
+ unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2529
+ const uint8_t key[] = {
2530
+ 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2531
+ 0xaa, 0xaa, 0xaa, 0xaa };
2532
+ const uint8_t text[] = {
2533
+ 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2534
+ 0xbb, 0xbb, 0xbb, 0xbb };
2535
+ uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2536
+ size_t length = 0;
2537
+
2538
+ PSA_ASSERT( psa_crypto_init( ) );
2539
+ PSA_ASSERT( psa_allocate_key( &handle ) );
2540
+ psa_key_policy_set_usage( &policy,
2541
+ PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2542
+ alg );
2543
+ PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2544
+ PSA_ASSERT( psa_import_key( handle, key_type,
2545
+ key, sizeof(key) ) );
2546
+
2547
+
2548
+ /* Generate an IV without calling setup beforehand. */
2549
+ TEST_EQUAL( psa_cipher_generate_iv( &operation,
2550
+ buffer, sizeof( buffer ),
2551
+ &length ),
2552
+ PSA_ERROR_BAD_STATE );
2553
+ PSA_ASSERT( psa_cipher_abort( &operation ) );
2554
+
2555
+ /* Generate an IV twice in a row. */
2556
+ PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2557
+ PSA_ASSERT( psa_cipher_generate_iv( &operation,
2558
+ buffer, sizeof( buffer ),
2559
+ &length ) );
2560
+ TEST_EQUAL( psa_cipher_generate_iv( &operation,
2561
+ buffer, sizeof( buffer ),
2562
+ &length ),
2563
+ PSA_ERROR_BAD_STATE );
2564
+ PSA_ASSERT( psa_cipher_abort( &operation ) );
2565
+
2566
+ /* Generate an IV after it's already set. */
2567
+ PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2568
+ PSA_ASSERT( psa_cipher_set_iv( &operation,
2569
+ iv, sizeof( iv ) ) );
2570
+ TEST_EQUAL( psa_cipher_generate_iv( &operation,
2571
+ buffer, sizeof( buffer ),
2572
+ &length ),
2573
+ PSA_ERROR_BAD_STATE );
2574
+ PSA_ASSERT( psa_cipher_abort( &operation ) );
2575
+
2576
+ /* Set an IV without calling setup beforehand. */
2577
+ TEST_EQUAL( psa_cipher_set_iv( &operation,
2578
+ iv, sizeof( iv ) ),
2579
+ PSA_ERROR_BAD_STATE );
2580
+ PSA_ASSERT( psa_cipher_abort( &operation ) );
2581
+
2582
+ /* Set an IV after it's already set. */
2583
+ PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2584
+ PSA_ASSERT( psa_cipher_set_iv( &operation,
2585
+ iv, sizeof( iv ) ) );
2586
+ TEST_EQUAL( psa_cipher_set_iv( &operation,
2587
+ iv, sizeof( iv ) ),
2588
+ PSA_ERROR_BAD_STATE );
2589
+ PSA_ASSERT( psa_cipher_abort( &operation ) );
2590
+
2591
+ /* Set an IV after it's already generated. */
2592
+ PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2593
+ PSA_ASSERT( psa_cipher_generate_iv( &operation,
2594
+ buffer, sizeof( buffer ),
2595
+ &length ) );
2596
+ TEST_EQUAL( psa_cipher_set_iv( &operation,
2597
+ iv, sizeof( iv ) ),
2598
+ PSA_ERROR_BAD_STATE );
2599
+ PSA_ASSERT( psa_cipher_abort( &operation ) );
2600
+
2601
+ /* Call update without calling setup beforehand. */
2602
+ TEST_EQUAL( psa_cipher_update( &operation,
2603
+ text, sizeof( text ),
2604
+ buffer, sizeof( buffer ),
2605
+ &length ),
2606
+ PSA_ERROR_BAD_STATE );
2607
+ PSA_ASSERT( psa_cipher_abort( &operation ) );
2608
+
2609
+ /* Call update without an IV where an IV is required. */
2610
+ TEST_EQUAL( psa_cipher_update( &operation,
2611
+ text, sizeof( text ),
2612
+ buffer, sizeof( buffer ),
2613
+ &length ),
2614
+ PSA_ERROR_BAD_STATE );
2615
+ PSA_ASSERT( psa_cipher_abort( &operation ) );
2616
+
2617
+ /* Call update after finish. */
2618
+ PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2619
+ PSA_ASSERT( psa_cipher_set_iv( &operation,
2620
+ iv, sizeof( iv ) ) );
2621
+ PSA_ASSERT( psa_cipher_finish( &operation,
2622
+ buffer, sizeof( buffer ), &length ) );
2623
+ TEST_EQUAL( psa_cipher_update( &operation,
2624
+ text, sizeof( text ),
2625
+ buffer, sizeof( buffer ),
2626
+ &length ),
2627
+ PSA_ERROR_BAD_STATE );
2628
+ PSA_ASSERT( psa_cipher_abort( &operation ) );
2629
+
2630
+ /* Call finish without calling setup beforehand. */
2631
+ TEST_EQUAL( psa_cipher_finish( &operation,
2632
+ buffer, sizeof( buffer ), &length ),
2633
+ PSA_ERROR_BAD_STATE );
2634
+ PSA_ASSERT( psa_cipher_abort( &operation ) );
2635
+
2636
+ /* Call finish without an IV where an IV is required. */
2637
+ PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2638
+ /* Not calling update means we are encrypting an empty buffer, which is OK
2639
+ * for cipher modes with padding. */
2640
+ TEST_EQUAL( psa_cipher_finish( &operation,
2641
+ buffer, sizeof( buffer ), &length ),
2642
+ PSA_ERROR_BAD_STATE );
2643
+ PSA_ASSERT( psa_cipher_abort( &operation ) );
2644
+
2645
+ /* Call finish twice in a row. */
2646
+ PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2647
+ PSA_ASSERT( psa_cipher_set_iv( &operation,
2648
+ iv, sizeof( iv ) ) );
2649
+ PSA_ASSERT( psa_cipher_finish( &operation,
2650
+ buffer, sizeof( buffer ), &length ) );
2651
+ TEST_EQUAL( psa_cipher_finish( &operation,
2652
+ buffer, sizeof( buffer ), &length ),
2653
+ PSA_ERROR_BAD_STATE );
2654
+ PSA_ASSERT( psa_cipher_abort( &operation ) );
2655
+
2656
+ exit:
2657
+ mbedtls_psa_crypto_free( );
2658
+ }
2659
+ /* END_CASE */
2660
+
2500
2661
/* BEGIN_CASE */
2501
2662
void cipher_encrypt( int alg_arg, int key_type_arg,
2502
2663
data_t *key,
0 commit comments