From 353e45361d9dc733f1ef5102eda0f0b28864848a Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Thu, 15 Nov 2018 09:53:57 +0000 Subject: [PATCH] Don't call memcpy() with 0-length arguments The standard prohibits calling memcpy() with NULL pointer arguments, even if the size argument is 0. The TLS-1.2 PRF generator setup function previously called memcpy() with the label and salt as the source, even if they were of length 0, as exercised by the derive_key_policy test case in the PSA crypto test suite. This commit adds guards around the memcpy() calls so that they are only executed of salt or label have positive length, respectively. --- library/psa_crypto.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 5ee43e4d6..f0de86124 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -3708,9 +3708,17 @@ static psa_status_t psa_generator_tls12_prf_setup( /* Write `label + seed' at the end of the `A(i) + seed` buffer, * leaving the initial `hash_length` bytes unspecified for now. */ - memcpy( tls12_prf->Ai_with_seed + hash_length, label, label_length ); - memcpy( tls12_prf->Ai_with_seed + hash_length + label_length, - salt, salt_length ); + if( label_length != 0 ) + { + memcpy( tls12_prf->Ai_with_seed + hash_length, + label, label_length ); + } + + if( salt_length != 0 ) + { + memcpy( tls12_prf->Ai_with_seed + hash_length + label_length, + salt, salt_length ); + } /* The first block gets generated when * psa_generator_read() is called. */