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