From 52b9018cf7d8a905fbad9752560a224fc75f6784 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 29 Oct 2018 19:26:27 +0100 Subject: [PATCH] psa_export_key: for raw-byte keys, zero the end of the output buffer Skip all writing to the target buffer if its size is 0, since in this case the pointer might be invalid and this would cause the calls to memcpy and memset to have undefined behavior. --- library/psa_crypto.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index eac1eb4d5..87f9147a6 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -864,8 +864,12 @@ static psa_status_t psa_internal_export_key( psa_key_slot_t key, { if( slot->data.raw.bytes > data_size ) return( PSA_ERROR_BUFFER_TOO_SMALL ); - if( slot->data.raw.bytes != 0 ) + if( data_size != 0 ) + { memcpy( data, slot->data.raw.data, slot->data.raw.bytes ); + memset( data + slot->data.raw.bytes, 0, + data_size - slot->data.raw.bytes ); + } *data_length = slot->data.raw.bytes; return( PSA_SUCCESS ); }