@@ -339,6 +339,70 @@ void test_crypto_key_handles(void)
339
339
TEST_ASSERT_EQUAL (PSA_ERROR_EMPTY_SLOT, psa_open_key (PSA_KEY_LIFETIME_PERSISTENT, id, &key_handle));
340
340
}
341
341
342
+ void test_crypto_hash_clone (void )
343
+ {
344
+ psa_algorithm_t alg = PSA_ALG_SHA_256;
345
+ unsigned char hash[PSA_HASH_MAX_SIZE];
346
+ size_t hash_len;
347
+ psa_hash_operation_t source;
348
+ psa_hash_operation_t target;
349
+ /* SHA-256 hash of an empty string */
350
+ static const unsigned char expected_hash[] = {
351
+ 0xe3 , 0xb0 , 0xc4 , 0x42 , 0x98 , 0xfc , 0x1c , 0x14 , 0x9a , 0xfb , 0xf4 , 0xc8 ,
352
+ 0x99 , 0x6f , 0xb9 , 0x24 , 0x27 , 0xae , 0x41 , 0xe4 , 0x64 , 0x9b , 0x93 , 0x4c ,
353
+ 0xa4 , 0x95 , 0x99 , 0x1b , 0x78 , 0x52 , 0xb8 , 0x55
354
+ };
355
+
356
+ source = psa_hash_operation_init ();
357
+ target = psa_hash_operation_init ();
358
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_setup (&source, alg));
359
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_finish (&source, hash, sizeof (hash), &hash_len));
360
+ /* should fail because psa_hash_finish has been called on source */
361
+ TEST_ASSERT_EQUAL (PSA_ERROR_BAD_STATE, psa_hash_clone (&source, &target));
362
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_abort (&source));
363
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_abort (&target));
364
+
365
+ source = psa_hash_operation_init ();
366
+ target = psa_hash_operation_init ();
367
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_setup (&source, alg));
368
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_verify (&source, expected_hash, sizeof (expected_hash)));
369
+ /* should fail because psa_hash_verify has been called on source */
370
+ TEST_ASSERT_EQUAL (PSA_ERROR_BAD_STATE, psa_hash_clone (&source, &target));
371
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_abort (&source));
372
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_abort (&target));
373
+
374
+ source = psa_hash_operation_init ();
375
+ target = psa_hash_operation_init ();
376
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_setup (&source, alg));
377
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_abort (&source));
378
+ /* should fail because psa_hash_abort has been called on source */
379
+ TEST_ASSERT_EQUAL (PSA_ERROR_BAD_STATE, psa_hash_clone (&source, &target));
380
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_abort (&target));
381
+
382
+ source = psa_hash_operation_init ();
383
+ target = psa_hash_operation_init ();
384
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_setup (&source, alg));
385
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_setup (&target, alg));
386
+ /* should fail because psa_hash_setup has been called on target */
387
+ TEST_ASSERT_EQUAL (PSA_ERROR_BAD_STATE, psa_hash_clone (&source, &target));
388
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_abort (&source));
389
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_abort (&target));
390
+
391
+ source = psa_hash_operation_init ();
392
+ target = psa_hash_operation_init ();
393
+ /* should fail because psa_hash_setup has not been called on source */
394
+ TEST_ASSERT_EQUAL (PSA_ERROR_BAD_STATE, psa_hash_clone (&source, &target));
395
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_abort (&source));
396
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_abort (&target));
397
+
398
+ source = psa_hash_operation_init ();
399
+ target = psa_hash_operation_init ();
400
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_setup (&source, alg));
401
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_clone (&source, &target));
402
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_abort (&source));
403
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_hash_abort (&target));
404
+ }
405
+
342
406
utest::v1::status_t case_setup_handler (const Case *const source, const size_t index_of_case)
343
407
{
344
408
psa_status_t status = psa_crypto_init ();
@@ -372,6 +436,7 @@ Case cases[] = {
372
436
Case (" mbed-crypto asymmetric sign/verify" , case_setup_handler, test_crypto_asymmetric_sign_verify, case_teardown_handler),
373
437
Case (" mbed-crypto key derivation" , case_setup_handler, test_crypto_key_derivation, case_teardown_handler),
374
438
Case (" mbed-crypto key handles" , case_setup_handler, test_crypto_key_handles, case_teardown_handler),
439
+ Case (" mbed-crypto hash clone" , case_setup_handler, test_crypto_hash_clone, case_teardown_handler),
375
440
};
376
441
377
442
Specification specification (test_setup, cases);
0 commit comments