From 188c71e38253c2741494b57352e5a15158838ed9 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 29 Oct 2018 19:26:02 +0100 Subject: [PATCH] Private EC key format: change to raw secret value (export) Change the import/export format of private elliptic curve keys from RFC 5915 to the raw secret value. This commit updates the export code. --- library/psa_crypto.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index f77df3051..eac1eb4d5 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -869,6 +869,21 @@ static psa_status_t psa_internal_export_key( psa_key_slot_t key, *data_length = slot->data.raw.bytes; return( PSA_SUCCESS ); } +#if defined(MBEDTLS_ECP_C) + if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key ) + { + size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) ); + if( bytes > data_size ) + return( PSA_ERROR_BUFFER_TOO_SMALL ); + status = mbedtls_to_psa_error( + mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) ); + if( status != PSA_SUCCESS ) + return( status ); + memset( data + bytes, 0, data_size - bytes ); + *data_length = bytes; + return( PSA_SUCCESS ); + } +#endif else { #if defined(MBEDTLS_PK_WRITE_C)