Skip to content

Commit 2833050

Browse files
committed
test_suite_pk: fix guards in pk_psa_sign()
If the public key is exported with mbedtls_pk_write_pubkey_der() it should be re-imported with mbedtls_pk_parse_public_key(). Alternative options (when PK_WRITE is not defined), i.e. mbedtls_ecp_point_write_binary() and mbedtls_rsa_write_pubkey(), export the key in a different format which cannot be parsed by pk_parse module so mbedtls_ecp_point_read_binary() and mbedtls_rsa_parse_pubkey() should be used respectively in this case. Signed-off-by: Valerio Setti <[email protected]>
1 parent ea01efa commit 2833050

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

tests/suites/test_suite_pk.function

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,7 +1875,7 @@ void pk_psa_sign(int psa_type, int bits, int rsa_padding)
18751875
#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
18761876

18771877
/* Export public key from the non-opaque PK context we just created. */
1878-
#if defined(MBEDTLS_PK_WRITE_C)
1878+
#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_PK_WRITE_C)
18791879
ret = mbedtls_pk_write_pubkey_der(&pk, legacy_pub_key, sizeof(legacy_pub_key));
18801880
TEST_ASSERT(ret >= 0);
18811881
legacy_pub_key_len = (size_t) ret;
@@ -1884,7 +1884,7 @@ void pk_psa_sign(int psa_type, int bits, int rsa_padding)
18841884
memmove(legacy_pub_key,
18851885
legacy_pub_key + sizeof(legacy_pub_key) - legacy_pub_key_len,
18861886
legacy_pub_key_len);
1887-
#else /* MBEDTLS_PK_WRITE_C */
1887+
#else /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C */
18881888
#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
18891889
if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(psa_type)) {
18901890
TEST_EQUAL(mbedtls_ecp_point_write_binary(&(mbedtls_pk_ec_ro(pk)->grp),
@@ -1905,7 +1905,7 @@ void pk_psa_sign(int psa_type, int bits, int rsa_padding)
19051905
memmove(legacy_pub_key, end, legacy_pub_key_len);
19061906
}
19071907
#endif /* MBEDTLS_RSA_C */
1908-
#endif /* MBEDTLS_PK_WRITE_C */
1908+
#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C */
19091909

19101910
/* Turn the PK context into an opaque one. */
19111911
TEST_EQUAL(mbedtls_pk_get_psa_attributes(&pk, PSA_KEY_USAGE_SIGN_HASH, &attributes), 0);
@@ -1932,18 +1932,18 @@ void pk_psa_sign(int psa_type, int bits, int rsa_padding)
19321932
}
19331933

19341934
/* Export public key from the opaque PK context. */
1935-
#if defined(MBEDTLS_PK_WRITE_C)
1935+
#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_PK_WRITE_C)
19361936
ret = mbedtls_pk_write_pubkey_der(&pk, opaque_pub_key, sizeof(opaque_pub_key));
19371937
TEST_ASSERT(ret >= 0);
19381938
opaque_pub_key_len = (size_t) ret;
19391939
/* mbedtls_pk_write_pubkey_der() writes backwards in the data buffer. */
19401940
memmove(opaque_pub_key,
19411941
opaque_pub_key + sizeof(opaque_pub_key) - opaque_pub_key_len,
19421942
opaque_pub_key_len);
1943-
#else /* MBEDTLS_PK_WRITE_C */
1943+
#else /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C */
19441944
TEST_EQUAL(psa_export_public_key(key_id, opaque_pub_key, sizeof(opaque_pub_key),
19451945
&opaque_pub_key_len), PSA_SUCCESS);
1946-
#endif /* MBEDTLS_PK_WRITE_C */
1946+
#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C */
19471947

19481948
/* Check that the public keys of opaque and non-opaque PK contexts match. */
19491949
TEST_EQUAL(opaque_pub_key_len, legacy_pub_key_len);
@@ -1955,9 +1955,9 @@ void pk_psa_sign(int psa_type, int bits, int rsa_padding)
19551955

19561956
/* Create a new non-opaque PK context to verify the signature. */
19571957
mbedtls_pk_init(&pk);
1958-
#if defined(MBEDTLS_PK_PARSE_C)
1958+
#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_PK_WRITE_C)
19591959
TEST_EQUAL(mbedtls_pk_parse_public_key(&pk, legacy_pub_key, legacy_pub_key_len), 0);
1960-
#else /* MBEDTLS_PK_PARSE_C */
1960+
#else /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C */
19611961
#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
19621962
if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(psa_type)) {
19631963
TEST_EQUAL(mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)), 0);
@@ -1974,7 +1974,7 @@ void pk_psa_sign(int psa_type, int bits, int rsa_padding)
19741974
legacy_pub_key_len), 0);
19751975
}
19761976
#endif /* MBEDTLS_RSA_C */
1977-
#endif /* MBEDTLS_PK_PARSE_C */
1977+
#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C */
19781978

19791979
#if defined(MBEDTLS_RSA_C)
19801980
if (PSA_KEY_TYPE_IS_RSA(psa_type)) {

0 commit comments

Comments
 (0)