Skip to content

Commit 144c27b

Browse files
committed
pkwrite: add new internal symbol for the max supported public key DER length
This is also used in pk_psa_sign() to properly size buffers holding the public key. Signed-off-by: Valerio Setti <[email protected]>
1 parent 027796c commit 144c27b

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

library/pkwrite.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,13 @@
109109
#define MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES 0
110110

111111
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
112+
113+
/* Define the maximum available public key DER length based on the supported
114+
* key types (EC and/or RSA). */
115+
#if (MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES > MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES)
116+
#define MBEDTLS_PK_WRITE_PUBKEY_MAX_SIZE MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES
117+
#else
118+
#define MBEDTLS_PK_WRITE_PUBKEY_MAX_SIZE MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES
119+
#endif
120+
112121
#endif /* MBEDTLS_PK_WRITE_H */

tests/suites/test_suite_pk.function

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
#include <test/psa_exercise_key.h>
2525

26+
/* Needed for the definition of MBEDTLS_PK_WRITE_PUBKEY_MAX_SIZE. */
27+
#include "pkwrite.h"
28+
2629
/* Used for properly sizing the key buffer in pk_genkey_ec() */
2730
#include "psa_util_internal.h"
2831

@@ -1828,13 +1831,8 @@ void pk_psa_sign(int psa_type, int bits, int rsa_padding)
18281831
mbedtls_pk_context pk;
18291832
unsigned char hash[32];
18301833
unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
1831-
/* These buffers will be used to contain the key in DER format. Unfortunately
1832-
* when only EC is supported on the PSA side (i.e. no RSA or DH)
1833-
* PSA_EXPORT_PUBLIC_KEY_MAX_SIZE falls to PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(),
1834-
* but this is not enough to contain the DER representation of the public key.
1835-
* Therefore we pick the RSA size to be safe. */
1836-
unsigned char legacy_pub_key[RSA_WRITE_PUBKEY_MAX_SIZE];
1837-
unsigned char opaque_pub_key[RSA_WRITE_PUBKEY_MAX_SIZE];
1834+
unsigned char legacy_pub_key[MBEDTLS_PK_WRITE_PUBKEY_MAX_SIZE];
1835+
unsigned char opaque_pub_key[MBEDTLS_PK_WRITE_PUBKEY_MAX_SIZE];
18381836
size_t sig_len, legacy_pub_key_len, opaque_pub_key_len;
18391837
mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
18401838
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;

0 commit comments

Comments
 (0)