Skip to content

Commit 0edd108

Browse files
committed
minor optimizations
1 parent d838adb commit 0edd108

File tree

3 files changed

+9
-48
lines changed

3 files changed

+9
-48
lines changed

library/pk.c

+3-17
Original file line numberDiff line numberDiff line change
@@ -858,22 +858,6 @@ mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
858858

859859
#if defined(MBEDTLS_USE_PSA_CRYPTO)
860860
#if defined(MBEDTLS_ECP_LIGHT)
861-
int mbedtls_pk_get_public_key(mbedtls_pk_context *pk, unsigned char *buf,
862-
size_t buf_size, size_t *key_len)
863-
{
864-
if ((pk == NULL) || (pk->pk_raw_len == 0)) {
865-
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
866-
}
867-
if (buf_size < pk->pk_raw_len) {
868-
return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
869-
}
870-
871-
memcpy(buf, pk->pk_raw, pk->pk_raw_len);
872-
*key_len = pk->pk_raw_len;
873-
874-
return 0;
875-
}
876-
877861
int mbedtls_pk_get_ec_public_key_props(mbedtls_pk_context *pk,
878862
psa_ecc_family_t *ec_curve, size_t *bits)
879863
{
@@ -935,7 +919,9 @@ int mbedtls_pk_update_keypair_from_public_key(mbedtls_pk_context *pk)
935919
}
936920
/* RSA does not support raw public keys inside the pk_context structure,
937921
* so we quit silently in this case */
938-
if (pk->pk_info->type == MBEDTLS_PK_RSA) {
922+
if ((pk->pk_info->type != MBEDTLS_PK_ECKEY) &&
923+
(pk->pk_info->type != MBEDTLS_PK_ECKEY_DH) &&
924+
(pk->pk_info->type != MBEDTLS_PK_ECDSA)) {
939925
return 0;
940926
}
941927

library/pk_internal.h

-18
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,6 @@
3434
#endif
3535

3636
#if defined(MBEDTLS_ECP_LIGHT) && defined(MBEDTLS_USE_PSA_CRYPTO)
37-
/**
38-
* Return the raw public key content on the provided buffer.
39-
*
40-
* \param pk The PK context that will be used to extract the public key.
41-
* \param buf The output buffer into which the key will be copied
42-
* \param buf_size The size of the output buffer
43-
* \param key_len The effective length of the key copied into the output
44-
* buffer.
45-
*
46-
* \return 0, on success;
47-
* MBEDTLS_ERR_PK_BAD_INPUT_DATA if the provided PK context
48-
* is not valid or if there is no public key stored in it;
49-
* MBEDTLS_ERR_PK_BUFFER_TOO_SMALL if the provided output
50-
* buffer is too small to contain the public key.
51-
*/
52-
int mbedtls_pk_get_public_key(mbedtls_pk_context *pk, unsigned char *buf,
53-
size_t buf_size, size_t *key_len);
54-
5537
/**
5638
* Return EC parameter used in the given PK context.
5739
*

library/pk_wrap.c

+6-13
Original file line numberDiff line numberDiff line change
@@ -726,12 +726,9 @@ static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
726726
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
727727
mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
728728
psa_status_t status;
729-
size_t key_len;
730-
/* This buffer will initially contain the public key and then the signature
731-
* but at different points in time. For all curves except secp224k1, which
732-
* is not currently supported in PSA, the public key is one byte longer
733-
* (header byte + 2 numbers, while the signature is only 2 numbers),
734-
* so use that as the buffer size. */
729+
/* For all curves except secp224k1, which is not currently supported in PSA,
730+
* the public key is one byte longer (header byte + 2 numbers, while the
731+
* signature is only 2 numbers), so use that as the buffer size. */
735732
unsigned char buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
736733
unsigned char *p;
737734
psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
@@ -740,7 +737,8 @@ static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
740737
size_t signature_part_size;
741738
((void) md_alg);
742739

743-
if (mbedtls_pk_get_ec_public_key_props(pk, &curve, &curve_bits) != 0) {
740+
if ((mbedtls_pk_get_ec_public_key_props(pk, &curve, &curve_bits) != 0) ||
741+
(pk->pk_raw_len == 0)) {
744742
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
745743
}
746744
signature_part_size = PSA_BITS_TO_BYTES(curve_bits);
@@ -749,13 +747,8 @@ static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
749747
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
750748
psa_set_key_algorithm(&attributes, psa_sig_md);
751749

752-
ret = mbedtls_pk_get_public_key(pk, buf, sizeof(buf), &key_len);
753-
if (ret != 0) {
754-
goto cleanup;
755-
}
756-
757750
status = psa_import_key(&attributes,
758-
buf, key_len,
751+
pk->pk_raw, pk->pk_raw_len,
759752
&key_id);
760753
if (status != PSA_SUCCESS) {
761754
ret = PSA_PK_TO_MBEDTLS_ERR(status);

0 commit comments

Comments
 (0)