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