@@ -2593,11 +2593,6 @@ void pk_copy_from_psa_success(data_t *priv_key_data, int key_type_arg,
2593
2593
mbedtls_pk_context pk_priv, pk_priv_copy_public, pk_pub, pk_pub_copy_public;
2594
2594
mbedtls_svc_key_id_t priv_key_id = MBEDTLS_SVC_KEY_ID_INIT;
2595
2595
mbedtls_svc_key_id_t pub_key_id = MBEDTLS_SVC_KEY_ID_INIT;
2596
- unsigned char *in_buf = NULL;
2597
- size_t in_buf_len = MBEDTLS_MD_MAX_SIZE;
2598
- unsigned char out_buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
2599
- unsigned char out_buf2[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
2600
- size_t out_buf_len, out_buf2_len;
2601
2596
2602
2597
mbedtls_pk_init(&pk_priv);
2603
2598
mbedtls_pk_init(&pk_priv_copy_public);
@@ -2620,14 +2615,13 @@ void pk_copy_from_psa_success(data_t *priv_key_data, int key_type_arg,
2620
2615
TEST_EQUAL(mbedtls_pk_copy_from_psa(pub_key_id, &pk_pub), 0);
2621
2616
TEST_EQUAL(mbedtls_pk_copy_public_from_psa(pub_key_id, &pk_pub_copy_public), 0);
2622
2617
2623
- /* Destoy both PSA keys to prove that generated PK contexts are independent
2618
+ /* Destroy both PSA keys to prove that generated PK contexts are independent
2624
2619
* from them. */
2625
2620
priv_key_id = psa_copy_and_destroy(priv_key_id);
2626
2621
pub_key_id = psa_copy_and_destroy(pub_key_id);
2627
2622
2628
- /* Test #1:
2629
- * - check that the generated PK contexts are of the correct type.
2630
- * - [only for RSA] check that the padding mode is correct.
2623
+ /* - Check that the generated PK contexts are of the correct type.
2624
+ * - [Only for RSA] check that the padding mode is correct.
2631
2625
*/
2632
2626
if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
2633
2627
TEST_EQUAL(mbedtls_pk_get_type(&pk_priv), MBEDTLS_PK_ECKEY);
@@ -2648,135 +2642,23 @@ void pk_copy_from_psa_success(data_t *priv_key_data, int key_type_arg,
2648
2642
#endif /* MBEDTLS_RSA_C */
2649
2643
}
2650
2644
2651
- /* Test #2: check that the 2 generated PK contexts form a valid private/public key pair. */
2645
+ /* Check that generated private/public PK contexts form a valid private/public key pair. */
2652
2646
TEST_EQUAL(mbedtls_pk_check_pair(&pk_pub, &pk_priv, mbedtls_test_rnd_std_rand, NULL), 0);
2653
2647
2654
- /* Get the MD alg to be used for the tests below from the provided key policy. */
2655
- mbedtls_md_type_t md_for_test = MBEDTLS_MD_ALG_FOR_TEST; /* Default */
2656
- if ((PSA_ALG_GET_HASH(key_alg) != PSA_ALG_NONE) &&
2657
- (PSA_ALG_GET_HASH(key_alg) != PSA_ALG_ANY_HASH)) {
2658
- md_for_test = mbedtls_md_type_from_psa_alg(key_alg);
2659
- }
2660
- /* Use also the same MD algorithm for PSA sign/verify checks. This is helpful
2661
- * for the cases in which the key policy algorithm is ANY_HASH type. */
2662
- psa_algorithm_t psa_alg_for_test =
2663
- (key_alg & ~PSA_ALG_HASH_MASK) |
2664
- (mbedtls_md_psa_alg_from_type(md_for_test) & PSA_ALG_HASH_MASK);
2665
-
2666
- in_buf_len = mbedtls_md_get_size_from_type(md_for_test);
2667
- TEST_CALLOC(in_buf, in_buf_len);
2668
- memset(in_buf, 0x1, in_buf_len);
2669
-
2670
- /* Test #3: sign/verify with the following pattern:
2671
- * - Sign using the PK context generated from the private key.
2672
- * - Verify from the same PK context used for signature.
2673
- * - Verify with the PK context generated using public key.
2674
- * - Verify using the public PSA key directly.
2675
- */
2676
-
2677
- /* Edge cases: in a build with RSA key support but not RSA padding modes,
2678
- * or with ECDSA verify support but not signature, the signature might be
2679
- * impossible. */
2680
- int pk_can_sign = 0;
2681
- #if defined(MBEDTLS_PKCS1_V15)
2682
- if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(key_alg) || key_alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
2683
- pk_can_sign = 1;
2684
- }
2685
- #endif
2686
- #if defined(MBEDTLS_PKCS1_V21)
2687
- if (PSA_ALG_IS_RSA_PSS(key_alg) || PSA_ALG_IS_RSA_OAEP(key_alg)) {
2688
- pk_can_sign = 1;
2689
- }
2690
- #endif
2691
- #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
2692
- if (PSA_ALG_IS_ECDSA(key_alg) || PSA_ALG_IS_DETERMINISTIC_ECDSA(key_alg)) {
2693
- pk_can_sign = 1;
2694
- }
2695
- #endif
2696
- if (pk_can_sign) {
2697
- TEST_EQUAL(mbedtls_pk_sign(&pk_priv, md_for_test, in_buf, in_buf_len,
2698
- out_buf, sizeof(out_buf), &out_buf_len,
2699
- mbedtls_test_rnd_std_rand, NULL), 0);
2700
-
2701
- TEST_EQUAL(mbedtls_pk_verify(&pk_priv, md_for_test, in_buf, in_buf_len,
2702
- out_buf, out_buf_len), 0);
2703
- TEST_EQUAL(mbedtls_pk_verify(&pk_pub, md_for_test, in_buf, in_buf_len,
2704
- out_buf, out_buf_len), 0);
2705
- }
2706
-
2707
- if (PSA_ALG_IS_HASH_AND_SIGN(key_alg)) {
2708
- #if defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
2709
- /* ECDSA signature requires PK->PSA format conversion. */
2710
- if (PSA_ALG_IS_ECDSA(key_alg)) {
2711
- TEST_EQUAL(mbedtls_ecdsa_der_to_raw(mbedtls_pk_get_bitlen(&pk_pub),
2712
- out_buf, out_buf_len, out_buf,
2713
- sizeof(out_buf), &out_buf_len), 0);
2714
- }
2715
- #endif /* MBEDTLS_PSA_UTIL_HAVE_ECDSA */
2716
- PSA_ASSERT(psa_verify_hash(pub_key_id, psa_alg_for_test, in_buf, in_buf_len,
2717
- out_buf, out_buf_len));
2718
- }
2719
-
2720
- /* Test #4: check sign/verify interoperability also in the opposite direction:
2721
- * sign with PSA and verify with PK. Key's policy must include a valid hash
2722
- * algorithm (not any).
2723
- */
2724
- if (PSA_ALG_IS_HASH_AND_SIGN(key_alg)) {
2725
- PSA_ASSERT(psa_sign_hash(priv_key_id, psa_alg_for_test, in_buf, in_buf_len,
2726
- out_buf, sizeof(out_buf), &out_buf_len));
2727
- #if defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
2728
- /* ECDSA signature requires PSA->PK format conversion */
2729
- if (PSA_ALG_IS_ECDSA(key_alg)) {
2730
- TEST_EQUAL(mbedtls_ecdsa_raw_to_der(mbedtls_pk_get_bitlen(&pk_pub),
2731
- out_buf, out_buf_len, out_buf,
2732
- sizeof(out_buf), &out_buf_len), 0);
2733
- }
2734
- #endif /* MBEDTLS_PSA_UTIL_HAVE_ECDSA */
2735
- TEST_EQUAL(mbedtls_pk_verify(&pk_pub, md_for_test, in_buf, in_buf_len,
2736
- out_buf, out_buf_len), 0);
2737
- }
2738
-
2739
- /* Test #5: in case of RSA key pair try also encryption/decryption. */
2740
- if (PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(key_alg)) {
2741
- /* Encrypt with the public key only PK context. */
2742
- TEST_EQUAL(mbedtls_pk_encrypt(&pk_pub, in_buf, in_buf_len,
2743
- out_buf, &out_buf_len, sizeof(out_buf),
2744
- mbedtls_test_rnd_std_rand, NULL), 0);
2745
-
2746
- /* Decrypt with key pair PK context and compare with original data. */
2747
- TEST_EQUAL(mbedtls_pk_decrypt(&pk_priv, out_buf, out_buf_len,
2748
- out_buf2, &out_buf2_len, sizeof(out_buf2),
2749
- mbedtls_test_rnd_std_rand, NULL), 0);
2750
- TEST_MEMORY_COMPARE(in_buf, in_buf_len, out_buf2, out_buf2_len);
2751
-
2752
- if (PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(key_alg)) {
2753
- /* Decrypt with PSA private key directly and compare with original data. */
2754
- PSA_ASSERT(psa_asymmetric_decrypt(priv_key_id, key_alg, out_buf, out_buf_len,
2755
- NULL, 0,
2756
- out_buf2, sizeof(out_buf2), &out_buf2_len));
2757
- TEST_MEMORY_COMPARE(in_buf, in_buf_len, out_buf2, out_buf2_len);
2758
-
2759
- /* Encrypt with PSA public key directly, decrypt with public key PK context
2760
- * and compare with original data. */
2761
- PSA_ASSERT(psa_asymmetric_encrypt(pub_key_id, key_alg, in_buf, in_buf_len,
2762
- NULL, 0,
2763
- out_buf, sizeof(out_buf), &out_buf_len));
2764
- TEST_EQUAL(mbedtls_pk_decrypt(&pk_priv, out_buf, out_buf_len,
2765
- out_buf2, &out_buf2_len, sizeof(out_buf2),
2766
- mbedtls_test_rnd_std_rand, NULL), 0);
2767
- TEST_MEMORY_COMPARE(in_buf, in_buf_len, out_buf2, out_buf2_len);
2768
- }
2769
- }
2648
+ /* Check consistency between copied PSA keys and generated PK contexts. */
2649
+ TEST_EQUAL(mbedtls_test_key_consistency_psa_pk(priv_key_id, &pk_priv), 1);
2650
+ TEST_EQUAL(mbedtls_test_key_consistency_psa_pk(priv_key_id, &pk_pub), 1);
2651
+ TEST_EQUAL(mbedtls_test_key_consistency_psa_pk(pub_key_id, &pk_priv), 1);
2652
+ TEST_EQUAL(mbedtls_test_key_consistency_psa_pk(pub_key_id, &pk_pub), 1);
2770
2653
2771
2654
/* Test that the keys from mbedtls_pk_copy_public_from_psa() are identical
2772
- * to the public key from mbedtls_pk_copy_from_psa(). */
2655
+ * to the public keys from mbedtls_pk_copy_from_psa(). */
2773
2656
mbedtls_test_set_step(1);
2774
2657
TEST_ASSERT(pk_public_same(&pk_pub, &pk_priv_copy_public));
2775
2658
mbedtls_test_set_step(2);
2776
2659
TEST_ASSERT(pk_public_same(&pk_pub, &pk_pub_copy_public));
2777
2660
2778
2661
exit:
2779
- mbedtls_free(in_buf);
2780
2662
mbedtls_pk_free(&pk_priv);
2781
2663
mbedtls_pk_free(&pk_priv_copy_public);
2782
2664
mbedtls_pk_free(&pk_pub);
0 commit comments