Slightly simplify pk_derive_public_key()

- add a comment explain potentially surprising parameters
- avoid nesting #if guards: I find the linear structure #if #elif #else
makes the three cases clearer.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
This commit is contained in:
Manuel Pégourié-Gonnard 2023-07-24 12:06:22 +02:00
parent 2585852231
commit d5b4372012

View file

@ -519,24 +519,32 @@ static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_pk_context *p
/* /*
* Helper function for deriving a public key from its private counterpart. * Helper function for deriving a public key from its private counterpart.
*
* Note: the private key information is always available from pk,
* however for convenience the serialized version is also passed,
* as it's available at each calling site, and useful in some configs
* (as otherwise we're have to re-serialize it from the pk context).
*/ */
static int pk_derive_public_key(mbedtls_pk_context *pk, static int pk_derive_public_key(mbedtls_pk_context *pk,
const unsigned char *d, size_t d_len, const unsigned char *d, size_t d_len,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{ {
int ret; #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
#if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status; psa_status_t status;
(void) f_rng; (void) f_rng;
(void) p_rng; (void) p_rng;
#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
(void) d; (void) d;
(void) d_len; (void) d_len;
status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw), status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
&pk->pub_raw_len); &pk->pub_raw_len);
ret = psa_pk_status_to_mbedtls(status); return psa_pk_status_to_mbedtls(status);
#else /* MBEDTLS_PK_USE_PSA_EC_DATA */ #elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
int ret;
psa_status_t status;
(void) f_rng;
(void) p_rng;
mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx; mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
size_t key_len; size_t key_len;
@ -563,16 +571,14 @@ static int pk_derive_public_key(mbedtls_pk_context *pk,
} else if (destruction_status != PSA_SUCCESS) { } else if (destruction_status != PSA_SUCCESS) {
return psa_pk_status_to_mbedtls(destruction_status); return psa_pk_status_to_mbedtls(destruction_status);
} }
ret = mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len); return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, key_buf, key_len);
#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
#else /* MBEDTLS_USE_PSA_CRYPTO */ #else /* MBEDTLS_USE_PSA_CRYPTO */
mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx; mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
(void) d; (void) d;
(void) d_len; (void) d_len;
ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng); return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
#endif /* MBEDTLS_USE_PSA_CRYPTO */ #endif /* MBEDTLS_USE_PSA_CRYPTO */
return ret;
} }
#if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES) #if defined(MBEDTLS_PK_HAVE_RFC8410_CURVES)