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 depends_on:PSA_WANT_ALG_ECDSA:PSA_WANT_ECC_SECP_R1_256 */
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 depends_on:PSA_WANT_ALG_ECDSA:PSA_WANT_ECC_SECP_R1_256 */
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    /* Test setup call, encrypt */
876    mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
877    status = psa_cipher_encrypt_setup( &operation, key, alg );
878    /* When setup fails, it shouldn't call any further entry points */
879    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
880    TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
881    mbedtls_test_driver_cipher_hooks.hits = 0;
882    status = psa_cipher_set_iv( &operation, iv->x, iv->len );
883    TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
884    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
885
886    /* Test setup call failure, decrypt */
887    status = psa_cipher_decrypt_setup( &operation, key, alg );
888    /* When setup fails, it shouldn't call any further entry points */
889    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
890    TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
891    mbedtls_test_driver_cipher_hooks.hits = 0;
892    status = psa_cipher_set_iv( &operation, iv->x, iv->len );
893    TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
894    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
895
896    /* Test IV setting failure */
897    mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS;
898    status = psa_cipher_encrypt_setup( &operation, key, alg );
899    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
900    TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
901    mbedtls_test_driver_cipher_hooks.hits = 0;
902
903    mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
904    status = psa_cipher_set_iv( &operation, iv->x, iv->len );
905    /* When setting the IV fails, it should call abort too */
906    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 );
907    TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
908    /* Failure should prevent further operations from executing on the driver */
909    mbedtls_test_driver_cipher_hooks.hits = 0;
910    status = psa_cipher_update( &operation,
911                                input->x, input->len,
912                                output, output_buffer_size,
913                                &function_output_length );
914    TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
915    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
916    psa_cipher_abort( &operation );
917
918    /* Test IV generation failure */
919    mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS;
920    status = psa_cipher_encrypt_setup( &operation, key, alg );
921    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
922    TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
923    mbedtls_test_driver_cipher_hooks.hits = 0;
924
925    mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
926    status = psa_cipher_generate_iv( &operation, output, 16, &function_output_length );
927    /* When generating the IV fails, it should call abort too */
928    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 );
929    TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
930    /* Failure should prevent further operations from executing on the driver */
931    mbedtls_test_driver_cipher_hooks.hits = 0;
932    status = psa_cipher_update( &operation,
933                                input->x, input->len,
934                                output, output_buffer_size,
935                                &function_output_length );
936    TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
937    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
938    psa_cipher_abort( &operation );
939
940    /* Test update failure */
941    mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS;
942    status = psa_cipher_encrypt_setup( &operation, key, alg );
943    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
944    TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
945    mbedtls_test_driver_cipher_hooks.hits = 0;
946
947    status = psa_cipher_set_iv( &operation, iv->x, iv->len );
948    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
949    TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
950    mbedtls_test_driver_cipher_hooks.hits = 0;
951
952    mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
953    status = psa_cipher_update( &operation,
954                                input->x, input->len,
955                                output, output_buffer_size,
956                                &function_output_length );
957    /* When the update call fails, it should call abort too */
958    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 );
959    TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
960    /* Failure should prevent further operations from executing on the driver */
961    mbedtls_test_driver_cipher_hooks.hits = 0;
962    status = psa_cipher_update( &operation,
963                                input->x, input->len,
964                                output, output_buffer_size,
965                                &function_output_length );
966    TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
967    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
968    psa_cipher_abort( &operation );
969
970    /* Test finish failure */
971    mbedtls_test_driver_cipher_hooks.forced_status = PSA_SUCCESS;
972    status = psa_cipher_encrypt_setup( &operation, key, alg );
973    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
974    TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
975    mbedtls_test_driver_cipher_hooks.hits = 0;
976
977    status = psa_cipher_set_iv( &operation, iv->x, iv->len );
978    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
979    TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
980    mbedtls_test_driver_cipher_hooks.hits = 0;
981
982    status = psa_cipher_update( &operation,
983                                input->x, input->len,
984                                output, output_buffer_size,
985                                &function_output_length );
986    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 1 );
987    TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
988    mbedtls_test_driver_cipher_hooks.hits = 0;
989
990    mbedtls_test_driver_cipher_hooks.forced_status = PSA_ERROR_GENERIC_ERROR;
991    status = psa_cipher_finish( &operation,
992                                output + function_output_length,
993                                output_buffer_size - function_output_length,
994                                &function_output_length );
995    /* When the finish call fails, it should call abort too */
996    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 2 );
997    TEST_EQUAL( status, mbedtls_test_driver_cipher_hooks.forced_status );
998    /* Failure should prevent further operations from executing on the driver */
999    mbedtls_test_driver_cipher_hooks.hits = 0;
1000    status = psa_cipher_update( &operation,
1001                                input->x, input->len,
1002                                output, output_buffer_size,
1003                                &function_output_length );
1004    TEST_EQUAL( status, PSA_ERROR_BAD_STATE );
1005    TEST_EQUAL( mbedtls_test_driver_cipher_hooks.hits, 0 );
1006    psa_cipher_abort( &operation );
1007
1008exit:
1009    psa_cipher_abort( &operation );
1010    mbedtls_free( output );
1011    psa_destroy_key( key );
1012    PSA_DONE( );
1013    mbedtls_test_driver_cipher_hooks = mbedtls_test_driver_cipher_hooks_init();
1014}
1015/* END_CASE */
1016
1017/* BEGIN_CASE */
1018void aead_encrypt( int key_type_arg, data_t *key_data,
1019                   int alg_arg,
1020                   data_t *nonce,
1021                   data_t *additional_data,
1022                   data_t *input_data,
1023                   data_t *expected_result,
1024                   int forced_status_arg )
1025{
1026    mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
1027    psa_key_type_t key_type = key_type_arg;
1028    psa_algorithm_t alg = alg_arg;
1029    size_t key_bits;
1030    psa_status_t forced_status = forced_status_arg;
1031    unsigned char *output_data = NULL;
1032    size_t output_size = 0;
1033    size_t output_length = 0;
1034    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1035    psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1036    mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
1037
1038    PSA_ASSERT( psa_crypto_init( ) );
1039
1040    psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT  );
1041    psa_set_key_algorithm( &attributes, alg );
1042    psa_set_key_type( &attributes, key_type );
1043
1044    PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1045                                &key ) );
1046    PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
1047    key_bits = psa_get_key_bits( &attributes );
1048
1049    output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
1050                                                         alg );
1051    /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
1052     * should be exact. */
1053    TEST_EQUAL( output_size,
1054                PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
1055    TEST_ASSERT( output_size <=
1056                 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
1057    ASSERT_ALLOC( output_data, output_size );
1058
1059    mbedtls_test_driver_aead_hooks.forced_status = forced_status;
1060    status = psa_aead_encrypt( key, alg,
1061                               nonce->x, nonce->len,
1062                               additional_data->x, additional_data->len,
1063                               input_data->x, input_data->len,
1064                               output_data, output_size,
1065                               &output_length );
1066    TEST_EQUAL( mbedtls_test_driver_aead_hooks.hits_encrypt, 1 );
1067    TEST_EQUAL( mbedtls_test_driver_aead_hooks.driver_status, forced_status );
1068
1069    TEST_EQUAL( status, ( forced_status == PSA_ERROR_NOT_SUPPORTED ) ?
1070                        PSA_SUCCESS : forced_status );
1071
1072    if( status == PSA_SUCCESS )
1073    {
1074        ASSERT_COMPARE( expected_result->x, expected_result->len,
1075                        output_data, output_length );
1076    }
1077
1078exit:
1079    psa_destroy_key( key );
1080    mbedtls_free( output_data );
1081    PSA_DONE( );
1082    mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
1083}
1084/* END_CASE */
1085
1086/* BEGIN_CASE */
1087void aead_decrypt( int key_type_arg, data_t *key_data,
1088                   int alg_arg,
1089                   data_t *nonce,
1090                   data_t *additional_data,
1091                   data_t *input_data,
1092                   data_t *expected_data,
1093                   int forced_status_arg )
1094{
1095    mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
1096    psa_key_type_t key_type = key_type_arg;
1097    psa_algorithm_t alg = alg_arg;
1098    size_t key_bits;
1099    psa_status_t forced_status = forced_status_arg;
1100    unsigned char *output_data = NULL;
1101    size_t output_size = 0;
1102    size_t output_length = 0;
1103    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1104    psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1105    mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
1106
1107    PSA_ASSERT( psa_crypto_init( ) );
1108
1109    psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT  );
1110    psa_set_key_algorithm( &attributes, alg );
1111    psa_set_key_type( &attributes, key_type );
1112
1113    PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1114                                &key ) );
1115    PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
1116    key_bits = psa_get_key_bits( &attributes );
1117
1118    output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
1119                                                         alg );
1120    ASSERT_ALLOC( output_data, output_size );
1121
1122    mbedtls_test_driver_aead_hooks.forced_status = forced_status;
1123    status = psa_aead_decrypt( key, alg,
1124                               nonce->x, nonce->len,
1125                               additional_data->x,
1126                               additional_data->len,
1127                               input_data->x, input_data->len,
1128                               output_data, output_size,
1129                               &output_length );
1130    TEST_EQUAL( mbedtls_test_driver_aead_hooks.hits_decrypt, 1 );
1131    TEST_EQUAL( mbedtls_test_driver_aead_hooks.driver_status, forced_status );
1132
1133    TEST_EQUAL( status, ( forced_status == PSA_ERROR_NOT_SUPPORTED ) ?
1134                        PSA_SUCCESS : forced_status );
1135
1136    if( status == PSA_SUCCESS )
1137    {
1138        ASSERT_COMPARE( expected_data->x, expected_data->len,
1139                        output_data, output_length );
1140    }
1141
1142exit:
1143    psa_destroy_key( key );
1144    mbedtls_free( output_data );
1145    PSA_DONE( );
1146    mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init();
1147}
1148/* END_CASE */
1149
1150/* BEGIN_CASE */
1151void mac_sign( int key_type_arg,
1152               data_t *key_data,
1153               int alg_arg,
1154               data_t *input,
1155               data_t *expected_mac,
1156               int forced_status_arg )
1157{
1158    mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
1159    psa_key_type_t key_type = key_type_arg;
1160    psa_algorithm_t alg = alg_arg;
1161    psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1162    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1163    uint8_t *actual_mac = NULL;
1164    size_t mac_buffer_size =
1165        PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
1166    size_t mac_length = 0;
1167    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1168    psa_status_t forced_status = forced_status_arg;
1169    mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
1170
1171    TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1172    /* We expect PSA_MAC_LENGTH to be exact. */
1173    TEST_ASSERT( expected_mac->len == mac_buffer_size );
1174
1175    PSA_ASSERT( psa_crypto_init( ) );
1176
1177    psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
1178    psa_set_key_algorithm( &attributes, alg );
1179    psa_set_key_type( &attributes, key_type );
1180
1181    PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1182                                &key ) );
1183
1184    ASSERT_ALLOC( actual_mac, mac_buffer_size );
1185    mbedtls_test_driver_mac_hooks.forced_status = forced_status;
1186
1187    /*
1188     * Calculate the MAC, one-shot case.
1189     */
1190    status = psa_mac_compute( key, alg,
1191                              input->x, input->len,
1192                              actual_mac, mac_buffer_size,
1193                              &mac_length );
1194
1195    TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
1196    if( forced_status == PSA_SUCCESS ||
1197        forced_status == PSA_ERROR_NOT_SUPPORTED )
1198    {
1199        PSA_ASSERT( status );
1200    }
1201    else
1202        TEST_EQUAL( forced_status, status );
1203
1204    if( mac_buffer_size > 0 )
1205        memset( actual_mac, 0, mac_buffer_size );
1206    mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
1207    mbedtls_test_driver_mac_hooks.forced_status = forced_status;
1208
1209    /*
1210     * Calculate the MAC, multipart case.
1211     */
1212    status = psa_mac_sign_setup( &operation, key, alg );
1213    TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
1214
1215    if( forced_status == PSA_SUCCESS ||
1216        forced_status == PSA_ERROR_NOT_SUPPORTED )
1217    {
1218        PSA_ASSERT( status );
1219    }
1220    else
1221        TEST_EQUAL( forced_status, status );
1222
1223    status = psa_mac_update( &operation,
1224                             input->x, input->len );
1225    if( forced_status == PSA_SUCCESS )
1226        TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 2 );
1227    else
1228        TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
1229    if( forced_status == PSA_SUCCESS ||
1230        forced_status == PSA_ERROR_NOT_SUPPORTED )
1231    {
1232        PSA_ASSERT( status );
1233    }
1234    else
1235        TEST_EQUAL( PSA_ERROR_BAD_STATE, status );
1236
1237    status = psa_mac_sign_finish( &operation,
1238                                  actual_mac, mac_buffer_size,
1239                                  &mac_length );
1240    if( forced_status == PSA_SUCCESS )
1241        TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 4 );
1242    else
1243        TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
1244
1245    if( forced_status == PSA_SUCCESS ||
1246        forced_status == PSA_ERROR_NOT_SUPPORTED )
1247    {
1248        PSA_ASSERT( status );
1249    }
1250    else
1251        TEST_EQUAL( PSA_ERROR_BAD_STATE, status );
1252
1253    PSA_ASSERT( psa_mac_abort( &operation ) );
1254    if( forced_status == PSA_SUCCESS )
1255        TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 4 );
1256    else
1257        TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
1258
1259    if( forced_status == PSA_SUCCESS )
1260    {
1261        ASSERT_COMPARE( expected_mac->x, expected_mac->len,
1262                        actual_mac, mac_length );
1263    }
1264
1265    mbedtls_free( actual_mac );
1266    actual_mac = NULL;
1267
1268exit:
1269    psa_mac_abort( &operation );
1270    psa_destroy_key( key );
1271    PSA_DONE( );
1272    mbedtls_free( actual_mac );
1273    mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
1274}
1275/* END_CASE */
1276
1277/* BEGIN_CASE */
1278void mac_verify( int key_type_arg,
1279                 data_t *key_data,
1280                 int alg_arg,
1281                 data_t *input,
1282                 data_t *expected_mac,
1283                 int forced_status_arg )
1284{
1285    mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
1286    psa_key_type_t key_type = key_type_arg;
1287    psa_algorithm_t alg = alg_arg;
1288    psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1289    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1290    psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1291    psa_status_t forced_status = forced_status_arg;
1292    mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
1293
1294    TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1295
1296    PSA_ASSERT( psa_crypto_init( ) );
1297
1298    psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
1299    psa_set_key_algorithm( &attributes, alg );
1300    psa_set_key_type( &attributes, key_type );
1301
1302    PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1303                                &key ) );
1304
1305    mbedtls_test_driver_mac_hooks.forced_status = forced_status;
1306
1307    /*
1308     * Verify the MAC, one-shot case.
1309     */
1310    status = psa_mac_verify( key, alg,
1311                             input->x, input->len,
1312                             expected_mac->x, expected_mac->len );
1313    TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
1314    if( forced_status == PSA_SUCCESS ||
1315        forced_status == PSA_ERROR_NOT_SUPPORTED )
1316    {
1317        PSA_ASSERT( status );
1318    }
1319    else
1320        TEST_EQUAL( forced_status, status );
1321
1322    mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
1323    mbedtls_test_driver_mac_hooks.forced_status = forced_status;
1324
1325    /*
1326     * Verify the MAC, multi-part case.
1327     */
1328    status = psa_mac_verify_setup( &operation, key, alg );
1329    TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
1330
1331    if( forced_status == PSA_SUCCESS ||
1332        forced_status == PSA_ERROR_NOT_SUPPORTED )
1333    {
1334        PSA_ASSERT( status );
1335    }
1336    else
1337        TEST_EQUAL( forced_status, status );
1338
1339    status = psa_mac_update( &operation,
1340                             input->x, input->len );
1341    if( forced_status == PSA_SUCCESS )
1342        TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 2 );
1343    else
1344        TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
1345
1346    if( forced_status == PSA_SUCCESS ||
1347        forced_status == PSA_ERROR_NOT_SUPPORTED )
1348    {
1349        PSA_ASSERT( status );
1350    }
1351    else
1352        TEST_EQUAL( PSA_ERROR_BAD_STATE, status );
1353
1354    status = psa_mac_verify_finish( &operation,
1355                                    expected_mac->x,
1356                                    expected_mac->len );
1357    if( forced_status == PSA_SUCCESS )
1358        TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 4 );
1359    else
1360        TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
1361
1362    if( forced_status == PSA_SUCCESS ||
1363        forced_status == PSA_ERROR_NOT_SUPPORTED )
1364    {
1365        PSA_ASSERT( status );
1366    }
1367    else
1368        TEST_EQUAL( PSA_ERROR_BAD_STATE, status );
1369
1370
1371    PSA_ASSERT( psa_mac_abort( &operation ) );
1372    if( forced_status == PSA_SUCCESS )
1373        TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 4 );
1374    else
1375        TEST_EQUAL( mbedtls_test_driver_mac_hooks.hits, 1 );
1376
1377exit:
1378    psa_mac_abort( &operation );
1379    psa_destroy_key( key );
1380    PSA_DONE( );
1381    mbedtls_test_driver_mac_hooks = mbedtls_test_driver_mac_hooks_init();
1382}
1383/* END_CASE */
1384
1385/* BEGIN_CASE depends_on:PSA_CRYPTO_DRIVER_TEST:MBEDTLS_PSA_CRYPTO_DRIVERS:MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
1386void builtin_key_export( int builtin_key_id_arg,
1387                         int builtin_key_type_arg,
1388                         int builtin_key_bits_arg,
1389                         int builtin_key_algorithm_arg,
1390                         data_t *expected_output,
1391                         int expected_status_arg )
1392{
1393    psa_key_id_t builtin_key_id = (psa_key_id_t) builtin_key_id_arg;
1394    psa_key_type_t builtin_key_type = (psa_key_type_t) builtin_key_type_arg;
1395    psa_algorithm_t builtin_key_alg = (psa_algorithm_t) builtin_key_algorithm_arg;
1396    size_t builtin_key_bits = (size_t) builtin_key_bits_arg;
1397    psa_status_t expected_status = expected_status_arg;
1398    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1399
1400    mbedtls_svc_key_id_t key = mbedtls_svc_key_id_make( 0, builtin_key_id );
1401    uint8_t* output_buffer = NULL;
1402    size_t output_size = 0;
1403    psa_status_t actual_status;
1404
1405    PSA_ASSERT( psa_crypto_init( ) );
1406    ASSERT_ALLOC( output_buffer, expected_output->len );
1407
1408    actual_status = psa_export_key( key, output_buffer, expected_output->len, &output_size );
1409
1410    if( expected_status == PSA_SUCCESS )
1411    {
1412        PSA_ASSERT( actual_status );
1413        TEST_EQUAL( output_size, expected_output->len );
1414        ASSERT_COMPARE( output_buffer, output_size,
1415                        expected_output->x, expected_output->len );
1416
1417        PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
1418        TEST_EQUAL( psa_get_key_bits( &attributes ), builtin_key_bits );
1419        TEST_EQUAL( psa_get_key_type( &attributes ), builtin_key_type );
1420        TEST_EQUAL( psa_get_key_algorithm( &attributes ), builtin_key_alg );
1421    }
1422    else
1423    {
1424        if( actual_status != expected_status )
1425            fprintf( stderr, "Expected %d but got %d\n", expected_status, actual_status );
1426        TEST_EQUAL( actual_status, expected_status );
1427        TEST_EQUAL( output_size, 0 );
1428    }
1429
1430exit:
1431    mbedtls_free( output_buffer );
1432    psa_reset_key_attributes( &attributes );
1433    psa_destroy_key( key );
1434    PSA_DONE( );
1435}
1436/* END_CASE */
1437
1438/* BEGIN_CASE depends_on:PSA_CRYPTO_DRIVER_TEST:MBEDTLS_PSA_CRYPTO_DRIVERS:MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
1439void builtin_pubkey_export( int builtin_key_id_arg,
1440                            int builtin_key_type_arg,
1441                            int builtin_key_bits_arg,
1442                            int builtin_key_algorithm_arg,
1443                            data_t *expected_output,
1444                            int expected_status_arg )
1445{
1446    psa_key_id_t builtin_key_id = (psa_key_id_t) builtin_key_id_arg;
1447    psa_key_type_t builtin_key_type = (psa_key_type_t) builtin_key_type_arg;
1448    psa_algorithm_t builtin_key_alg = (psa_algorithm_t) builtin_key_algorithm_arg;
1449    size_t builtin_key_bits = (size_t) builtin_key_bits_arg;
1450    psa_status_t expected_status = expected_status_arg;
1451    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1452
1453    mbedtls_svc_key_id_t key = mbedtls_svc_key_id_make( 0, builtin_key_id );
1454    uint8_t* output_buffer = NULL;
1455    size_t output_size = 0;
1456    psa_status_t actual_status;
1457
1458    PSA_ASSERT( psa_crypto_init( ) );
1459    ASSERT_ALLOC( output_buffer, expected_output->len );
1460
1461    actual_status = psa_export_public_key( key, output_buffer, expected_output->len, &output_size );
1462
1463    if( expected_status == PSA_SUCCESS )
1464    {
1465        PSA_ASSERT( actual_status );
1466        TEST_EQUAL( output_size, expected_output->len );
1467        ASSERT_COMPARE( output_buffer, output_size,
1468                        expected_output->x, expected_output->len );
1469
1470        PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
1471        TEST_EQUAL( psa_get_key_bits( &attributes ), builtin_key_bits );
1472        TEST_EQUAL( psa_get_key_type( &attributes ), builtin_key_type );
1473        TEST_EQUAL( psa_get_key_algorithm( &attributes ), builtin_key_alg );
1474    }
1475    else
1476    {
1477        TEST_EQUAL( actual_status, expected_status );
1478        TEST_EQUAL( output_size, 0 );
1479    }
1480
1481exit:
1482    mbedtls_free( output_buffer );
1483    psa_reset_key_attributes( &attributes );
1484    psa_destroy_key( key );
1485    PSA_DONE( );
1486}
1487/* END_CASE */
1488
1489/* BEGIN_CASE */
1490void hash_compute( int alg_arg,
1491                   data_t *input, data_t *hash,
1492                   int forced_status_arg,
1493                   int expected_status_arg )
1494{
1495    psa_algorithm_t alg = alg_arg;
1496    psa_status_t forced_status = forced_status_arg;
1497    psa_status_t expected_status = expected_status_arg;
1498    unsigned char *output = NULL;
1499    size_t output_length;
1500
1501    mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
1502    mbedtls_test_driver_hash_hooks.forced_status = forced_status;
1503
1504    PSA_ASSERT( psa_crypto_init( ) );
1505    ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) );
1506
1507    TEST_EQUAL( psa_hash_compute( alg, input->x, input->len,
1508                                  output, PSA_HASH_LENGTH( alg ),
1509                                  &output_length ), expected_status );
1510    TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 );
1511    TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status );
1512
1513    if( expected_status == PSA_SUCCESS )
1514    {
1515        ASSERT_COMPARE( output, output_length, hash->x, hash->len );
1516    }
1517
1518exit:
1519    mbedtls_free( output );
1520    PSA_DONE( );
1521    mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
1522}
1523/* END_CASE */
1524
1525/* BEGIN_CASE */
1526void hash_multipart_setup( int alg_arg,
1527                           data_t *input, data_t *hash,
1528                           int forced_status_arg,
1529                           int expected_status_arg )
1530{
1531    psa_algorithm_t alg = alg_arg;
1532    psa_status_t forced_status = forced_status_arg;
1533    psa_status_t expected_status = expected_status_arg;
1534    unsigned char *output = NULL;
1535    psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
1536    size_t output_length;
1537
1538    mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
1539    ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) );
1540
1541    PSA_ASSERT( psa_crypto_init( ) );
1542
1543    mbedtls_test_driver_hash_hooks.forced_status = forced_status;
1544    TEST_EQUAL( psa_hash_setup( &operation, alg ), expected_status );
1545    TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 );
1546    TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status );
1547
1548    if( expected_status == PSA_SUCCESS )
1549    {
1550        PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
1551        TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits,
1552                    forced_status == PSA_ERROR_NOT_SUPPORTED ? 1 : 2 );
1553        TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status );
1554
1555        PSA_ASSERT( psa_hash_finish( &operation,
1556                                     output, PSA_HASH_LENGTH( alg ),
1557                                     &output_length ) );
1558        TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits,
1559                    forced_status == PSA_ERROR_NOT_SUPPORTED ? 1 : 4 );
1560        TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status );
1561
1562        ASSERT_COMPARE( output, output_length, hash->x, hash->len );
1563    }
1564
1565exit:
1566    psa_hash_abort( &operation );
1567    mbedtls_free( output );
1568    PSA_DONE( );
1569    mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
1570}
1571/* END_CASE */
1572
1573/* BEGIN_CASE */
1574void hash_multipart_update( int alg_arg,
1575                            data_t *input, data_t *hash,
1576                            int forced_status_arg )
1577{
1578    psa_algorithm_t alg = alg_arg;
1579    psa_status_t forced_status = forced_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    /*
1590     * Update inactive operation, the driver shouldn't be called.
1591     */
1592    TEST_EQUAL( psa_hash_update( &operation, input->x, input->len ),
1593                PSA_ERROR_BAD_STATE );
1594    TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 0 );
1595
1596    PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1597    TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 );
1598    TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS );
1599
1600    mbedtls_test_driver_hash_hooks.forced_status = forced_status;
1601    TEST_EQUAL( psa_hash_update( &operation, input->x, input->len ),
1602                forced_status );
1603    /* One or two more calls to the driver interface: update or update + abort */
1604    TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits,
1605                forced_status == PSA_SUCCESS ? 2 : 3 );
1606    TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status );
1607
1608    if( forced_status == PSA_SUCCESS )
1609    {
1610        mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
1611        PSA_ASSERT( psa_hash_finish( &operation,
1612                                     output, PSA_HASH_LENGTH( alg ),
1613                                     &output_length ) );
1614        /* Two calls to the driver interface: update + abort */
1615        TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 2 );
1616        TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS );
1617
1618        ASSERT_COMPARE( output, output_length, hash->x, hash->len );
1619    }
1620
1621exit:
1622    psa_hash_abort( &operation );
1623    mbedtls_free( output );
1624    PSA_DONE( );
1625    mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
1626}
1627/* END_CASE */
1628
1629/* BEGIN_CASE */
1630void hash_multipart_finish( int alg_arg,
1631                            data_t *input, data_t *hash,
1632                            int forced_status_arg )
1633{
1634    psa_algorithm_t alg = alg_arg;
1635    psa_status_t forced_status = forced_status_arg;
1636    unsigned char *output = NULL;
1637    psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
1638    size_t output_length;
1639
1640    mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
1641    ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) );
1642
1643    PSA_ASSERT( psa_crypto_init( ) );
1644
1645    /*
1646     * Finish inactive operation, the driver shouldn't be called.
1647     */
1648    TEST_EQUAL( psa_hash_finish( &operation, output, PSA_HASH_LENGTH( alg ),
1649                                 &output_length ),
1650                PSA_ERROR_BAD_STATE );
1651    TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 0 );
1652
1653    PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1654    TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 );
1655    TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS );
1656
1657    PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
1658    TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 2 );
1659    TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS );
1660
1661    mbedtls_test_driver_hash_hooks.forced_status = forced_status;
1662    TEST_EQUAL( psa_hash_finish( &operation,
1663                                 output, PSA_HASH_LENGTH( alg ),
1664                                 &output_length ),
1665                forced_status );
1666    /* Two more calls to the driver interface: finish + abort */
1667    TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 4 );
1668    TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status );
1669
1670    if( forced_status == PSA_SUCCESS )
1671        ASSERT_COMPARE( output, output_length, hash->x, hash->len );
1672
1673exit:
1674    psa_hash_abort( &operation );
1675    mbedtls_free( output );
1676    PSA_DONE( );
1677    mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
1678}
1679/* END_CASE */
1680
1681/* BEGIN_CASE */
1682void hash_clone( int alg_arg,
1683                 data_t *input, data_t *hash,
1684                 int forced_status_arg )
1685{
1686    psa_algorithm_t alg = alg_arg;
1687    psa_status_t forced_status = forced_status_arg;
1688    unsigned char *output = NULL;
1689    psa_hash_operation_t source_operation = PSA_HASH_OPERATION_INIT;
1690    psa_hash_operation_t target_operation = PSA_HASH_OPERATION_INIT;
1691    size_t output_length;
1692
1693    mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
1694    ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) );
1695
1696    PSA_ASSERT( psa_crypto_init( ) );
1697
1698    /*
1699     * Clone inactive operation, the driver shouldn't be called.
1700     */
1701    TEST_EQUAL( psa_hash_clone( &source_operation, &target_operation ),
1702                PSA_ERROR_BAD_STATE );
1703    TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 0 );
1704
1705    PSA_ASSERT( psa_hash_setup( &source_operation, alg ) );
1706    TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 );
1707    TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS );
1708
1709    mbedtls_test_driver_hash_hooks.forced_status = forced_status;
1710    TEST_EQUAL( psa_hash_clone( &source_operation, &target_operation ),
1711                forced_status );
1712    TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits,
1713                forced_status == PSA_SUCCESS ? 2 : 3 );
1714    TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, forced_status );
1715
1716    if( forced_status == PSA_SUCCESS )
1717    {
1718        mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
1719        PSA_ASSERT( psa_hash_update( &target_operation,
1720                                     input->x, input->len ) );
1721        TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 1 );
1722        TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS );
1723
1724        PSA_ASSERT( psa_hash_finish( &target_operation,
1725                                     output, PSA_HASH_LENGTH( alg ),
1726                                     &output_length ) );
1727        TEST_EQUAL( mbedtls_test_driver_hash_hooks.hits, 3 );
1728        TEST_EQUAL( mbedtls_test_driver_hash_hooks.driver_status, PSA_SUCCESS );
1729
1730        ASSERT_COMPARE( output, output_length, hash->x, hash->len );
1731    }
1732
1733exit:
1734    psa_hash_abort( &source_operation );
1735    psa_hash_abort( &target_operation );
1736    mbedtls_free( output );
1737    PSA_DONE( );
1738    mbedtls_test_driver_hash_hooks = mbedtls_test_driver_hash_hooks_init();
1739}
1740/* END_CASE */
1741