Allow some parameters to be NULL if the length is 0.
This change permits users of the ChaCha20/Poly1305 algorithms
(and the AEAD construction thereof) to pass NULL pointers for
data that they do not need, and avoids the need to provide a valid
buffer for data that is not used.
diff --git a/library/chacha20.c b/library/chacha20.c
index b20c7ad..3511245 100644
--- a/library/chacha20.c
+++ b/library/chacha20.c
@@ -291,10 +291,15 @@
size_t offset = 0U;
size_t i;
- if ( ( ctx == NULL ) || ( input == NULL ) || ( output == NULL ) )
+ if ( ctx == NULL )
{
return( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
}
+ else if ( ( size > 0U ) && ( ( input == NULL ) || ( output == NULL ) ) )
+ {
+ /* input and output pointers are allowed to be NULL only if size == 0 */
+ return( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
+ }
/* Use leftover keystream bytes, if available */
while ( ( size > 0U ) && ( ctx->keystream_bytes_used < CHACHA20_BLOCK_SIZE_BYTES ) )