diff --git a/include/mbedtls/aead_chacha20_poly1305.h b/include/mbedtls/aead_chacha20_poly1305.h index a1ccf319e..6c8e420b5 100644 --- a/include/mbedtls/aead_chacha20_poly1305.h +++ b/include/mbedtls/aead_chacha20_poly1305.h @@ -124,6 +124,7 @@ int mbedtls_aead_chacha20_poly1305_starts( mbedtls_aead_chacha20_poly1305_contex * \param aad_len The length (in bytes) of the AAD. The length has no * restrictions. * \param aad Buffer containing the AAD. + * This pointer can be NULL if aad_len == 0. * * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned * if \p ctx or \p aad are NULL. @@ -151,7 +152,9 @@ int mbedtls_aead_chacha20_poly1305_update_aad( mbedtls_aead_chacha20_poly1305_co * \param ctx The ChaCha20-Poly1305 context. * \param len The length (in bytes) of the data to encrypt or decrypt. * \param input Buffer containing the data to encrypt or decrypt. + * This pointer can be NULL if len == 0. * \param output Buffer to where the encrypted or decrypted data is written. + * This pointer can be NULL if len == 0. * * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned * if \p ctx, \p input, or \p output are NULL. @@ -195,9 +198,12 @@ int mbedtls_aead_chacha20_poly1305_finish( mbedtls_aead_chacha20_poly1305_contex * parameter does not matter. * \param aad_len The length (in bytes) of the AAD data to process. * \param aad Buffer containing the additional authenticated data (AAD). + * This pointer can be NULL if aad_len == 0. * \param ilen The length (in bytes) of the data to encrypt or decrypt. * \param input Buffer containing the data to encrypt or decrypt. + * This pointer can be NULL if ilen == 0. * \param output Buffer to where the encrypted or decrypted data is written. + * This pointer can be NULL if ilen == 0. * \param mac Buffer to where the computed 128-bit (16 bytes) MAC is written. * * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned diff --git a/include/mbedtls/chacha20.h b/include/mbedtls/chacha20.h index ab87f66b9..ccce12270 100644 --- a/include/mbedtls/chacha20.h +++ b/include/mbedtls/chacha20.h @@ -136,7 +136,9 @@ int mbedtls_chacha20_keystream_block( const mbedtls_chacha20_context *ctx, * \param ctx The ChaCha20 context. * \param size The length (in bytes) to process. This can have any length. * \param input Buffer containing the input data. + * This pointer can be NULL if size == 0. * \param output Buffer containing the output data. + * This pointer can be NULL if size == 0. * * \return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if the ctx, input, or * output pointers are NULL. diff --git a/include/mbedtls/poly1305.h b/include/mbedtls/poly1305.h index 1aa55aeee..ea9364a3c 100644 --- a/include/mbedtls/poly1305.h +++ b/include/mbedtls/poly1305.h @@ -85,6 +85,7 @@ int mbedtls_poly1305_setkey( mbedtls_poly1305_context *ctx, * \param ctx The Poly1305 context. * \param ilen The input length (in bytes). Any value is accepted. * \param input Buffer containing the input data to Process. + * This pointer can be NULL if ilen == 0. * * \return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA is returned if ctx * or input are NULL. diff --git a/library/aead_chacha20_poly1305.c b/library/aead_chacha20_poly1305.c index ab29dfa1b..2dea5c9c5 100644 --- a/library/aead_chacha20_poly1305.c +++ b/library/aead_chacha20_poly1305.c @@ -174,10 +174,15 @@ int mbedtls_aead_chacha20_poly1305_update_aad( mbedtls_aead_chacha20_poly1305_co size_t aad_len, const unsigned char *aad ) { - if ( ( ctx == NULL ) || ( aad == NULL ) ) + if ( ctx == NULL ) { return( MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA ); } + else if ( ( aad_len > 0U ) && ( aad == NULL ) ) + { + /* aad pointer is allowed to be NULL if aad_len == 0 */ + return( MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA ); + } else if ( ctx->state != AEAD_CHACHA20_POLY1305_STATE_AAD ) { return (MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_STATE ); @@ -197,6 +202,11 @@ int mbedtls_aead_chacha20_poly1305_update( mbedtls_aead_chacha20_poly1305_contex { return( MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA ); } + else if ( ( len > 0U ) && ( ( input == NULL ) || ( output == NULL ) ) ) + { + /* input and output pointers are allowed to be NULL if len == 0 */ + return( MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA ); + } else if ( ( ctx->state != AEAD_CHACHA20_POLY1305_STATE_AAD ) && ( ctx->state != AEAD_CHACHA20_POLY1305_STATE_CIPHERTEXT ) ) { diff --git a/library/chacha20.c b/library/chacha20.c index b20c7ad55..351124541 100644 --- a/library/chacha20.c +++ b/library/chacha20.c @@ -291,10 +291,15 @@ int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx, 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 ) ) diff --git a/library/poly1305.c b/library/poly1305.c index 9a61a85ce..f9bdf2c93 100644 --- a/library/poly1305.c +++ b/library/poly1305.c @@ -293,12 +293,17 @@ int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx, size_t queue_free_len; size_t nblocks; - if ( ( ctx == NULL ) || ( input == NULL ) ) + if ( ctx == NULL ) { return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); } + else if ( ( ilen > 0U ) && ( input == NULL ) ) + { + /* input pointer is allowed to be NULL only if ilen == 0 */ + return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); + } - if ( ctx->queue_len > 0U ) + if ( ( remaining > 0U ) && ( ctx->queue_len > 0U ) ) { queue_free_len = ( POLY1305_BLOCK_SIZE_BYTES - ctx->queue_len );