MAC setup: support 0-length HMAC key
Avoid undefined behavior when using a 0-length HMAC key (Asan
complained).
diff --git a/library/psa_crypto.c b/library/psa_crypto.c
index de1f772..a0f2780 100644
--- a/library/psa_crypto.c
+++ b/library/psa_crypto.c
@@ -1435,7 +1435,11 @@
if( status != PSA_SUCCESS )
return( status );
}
- else
+ /* A 0-length key is not commonly used in HMAC when used as a MAC,
+ * but it is permitted. It is common when HMAC is used in HKDF, for
+ * example. Don't call `memcpy` in the 0-length because `key` could be
+ * an invalid pointer which would make the behavior undefined. */
+ else if( key_length != 0 )
memcpy( ipad, key, key_length );
/* ipad contains the key followed by garbage. Xor and fill with 0x36