mirror of
https://github.com/cuberite/polarssl.git
synced 2025-09-07 06:05:03 -04:00
Merge pull request #7536 from valeriosetti/issue7480-backport
Backport: Fix test gap in PK write: private (opaque) -> public
This commit is contained in:
commit
d7570a2a3b
@ -646,6 +646,7 @@ int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
|
||||
psa_key_type_t key_type;
|
||||
size_t bits;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
psa_status_t status;
|
||||
|
||||
/* export the private key material in the format PSA wants */
|
||||
if (mbedtls_pk_get_type(pk) != MBEDTLS_PK_ECKEY) {
|
||||
@ -668,7 +669,9 @@ int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
|
||||
psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(hash_alg));
|
||||
|
||||
/* import private key into PSA */
|
||||
if (PSA_SUCCESS != psa_import_key(&attributes, d, d_len, key)) {
|
||||
status = psa_import_key(&attributes, d, d_len, key);
|
||||
mbedtls_platform_zeroize(d, sizeof(d));
|
||||
if (status != PSA_SUCCESS) {
|
||||
return MBEDTLS_ERR_PK_HW_ACCEL_FAILED;
|
||||
}
|
||||
|
||||
|
@ -888,6 +888,10 @@ ec_prv.pk8param.pem: ec_prv.pk8param.der
|
||||
$(OPENSSL) pkey -in $< -inform DER -out $@
|
||||
all_final += ec_prv.pk8param.pem
|
||||
|
||||
ec_pub.pem: ec_prv.sec1.der
|
||||
$(OPENSSL) pkey -in $< -inform DER -outform PEM -pubout -out $@
|
||||
all_final += ec_pub.pem
|
||||
|
||||
################################################################
|
||||
#### Convert PEM keys to DER format
|
||||
################################################################
|
||||
|
Binary file not shown.
@ -93,3 +93,15 @@ pk_write_key_check:"data_files/ec_bp512_prv.pem":TEST_PEM
|
||||
Private key write check EC Brainpool 512 bits (DER)
|
||||
depends_on:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_BP512R1_ENABLED
|
||||
pk_write_key_check:"data_files/ec_bp512_prv.der":TEST_DER
|
||||
|
||||
Derive public key EC 192 bits
|
||||
depends_on:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED
|
||||
pk_write_public_from_private:"data_files/ec_prv.sec1.der":"data_files/ec_pub.der"
|
||||
|
||||
Derive public key EC 521 bits
|
||||
depends_on:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP521R1_ENABLED
|
||||
pk_write_public_from_private:"data_files/ec_521_prv.der":"data_files/ec_521_pub.der"
|
||||
|
||||
Derive public key EC Brainpool 512 bits
|
||||
depends_on:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_BP512R1_ENABLED
|
||||
pk_write_public_from_private:"data_files/ec_bp512_prv.der":"data_files/ec_bp512_pub.der"
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "mbedtls/pk.h"
|
||||
#include "mbedtls/pem.h"
|
||||
#include "mbedtls/oid.h"
|
||||
#include "psa/crypto_sizes.h"
|
||||
|
||||
typedef enum {
|
||||
TEST_PEM,
|
||||
@ -123,3 +124,55 @@ void pk_write_key_check(char *key_file, int is_der)
|
||||
goto exit; /* make the compiler happy */
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void pk_write_public_from_private(char *priv_key_file, char *pub_key_file)
|
||||
{
|
||||
mbedtls_pk_context priv_key;
|
||||
uint8_t *derived_key_raw = NULL;
|
||||
size_t derived_key_len = 0;
|
||||
uint8_t *pub_key_raw = NULL;
|
||||
size_t pub_key_len = 0;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
mbedtls_svc_key_id_t opaque_key_id = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
mbedtls_pk_init(&priv_key);
|
||||
USE_PSA_INIT();
|
||||
|
||||
TEST_EQUAL(mbedtls_pk_parse_keyfile(&priv_key, priv_key_file, NULL), 0);
|
||||
TEST_EQUAL(mbedtls_pk_load_file(pub_key_file, &pub_key_raw,
|
||||
&pub_key_len), 0);
|
||||
|
||||
derived_key_len = pub_key_len;
|
||||
ASSERT_ALLOC(derived_key_raw, derived_key_len);
|
||||
|
||||
TEST_EQUAL(mbedtls_pk_write_pubkey_der(&priv_key, derived_key_raw,
|
||||
derived_key_len), pub_key_len);
|
||||
|
||||
ASSERT_COMPARE(derived_key_raw, derived_key_len,
|
||||
pub_key_raw, pub_key_len);
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
mbedtls_platform_zeroize(derived_key_raw, sizeof(derived_key_raw));
|
||||
|
||||
TEST_EQUAL(mbedtls_pk_wrap_as_opaque(&priv_key, &opaque_key_id,
|
||||
PSA_ALG_NONE), 0);
|
||||
|
||||
TEST_EQUAL(mbedtls_pk_write_pubkey_der(&priv_key, derived_key_raw,
|
||||
derived_key_len), pub_key_len);
|
||||
|
||||
ASSERT_COMPARE(derived_key_raw, derived_key_len,
|
||||
pub_key_raw, pub_key_len);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
exit:
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_destroy_key(opaque_key_id);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
mbedtls_free(derived_key_raw);
|
||||
mbedtls_free(pub_key_raw);
|
||||
mbedtls_pk_free(&priv_key);
|
||||
USE_PSA_DONE();
|
||||
}
|
||||
/* END_CASE */
|
||||
|
Loading…
x
Reference in New Issue
Block a user