mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-29 08:04:24 +01:00
Check return values from lower modules
The cast to void was motivated by the assumption that the functions only return non-zero when passed bad arguments, but that might not be true of alternative implementation, for example on hardware failure.
This commit is contained in:
parent
1729789075
commit
f4f01b6b7a
@ -54,18 +54,19 @@
|
|||||||
*
|
*
|
||||||
* \param ctx The ChaCha20-Poly1305 context.
|
* \param ctx The ChaCha20-Poly1305 context.
|
||||||
*/
|
*/
|
||||||
static void chachapoly_pad_aad( mbedtls_chachapoly_context *ctx )
|
static int chachapoly_pad_aad( mbedtls_chachapoly_context *ctx )
|
||||||
{
|
{
|
||||||
uint32_t partial_block_len = (uint32_t) ( ctx->aad_len % 16U );
|
uint32_t partial_block_len = (uint32_t) ( ctx->aad_len % 16U );
|
||||||
unsigned char zeroes[15];
|
unsigned char zeroes[15];
|
||||||
|
|
||||||
if( partial_block_len > 0U )
|
if( partial_block_len == 0U )
|
||||||
{
|
return( 0 );
|
||||||
memset( zeroes, 0, sizeof( zeroes ) );
|
|
||||||
(void) mbedtls_poly1305_update( &ctx->poly1305_ctx,
|
memset( zeroes, 0, sizeof( zeroes ) );
|
||||||
zeroes,
|
|
||||||
16U - partial_block_len );
|
return( mbedtls_poly1305_update( &ctx->poly1305_ctx,
|
||||||
}
|
zeroes,
|
||||||
|
16U - partial_block_len ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -78,13 +79,13 @@ static void chachapoly_pad_ciphertext( mbedtls_chachapoly_context *ctx )
|
|||||||
uint32_t partial_block_len = (uint32_t) ( ctx->ciphertext_len % 16U );
|
uint32_t partial_block_len = (uint32_t) ( ctx->ciphertext_len % 16U );
|
||||||
unsigned char zeroes[15];
|
unsigned char zeroes[15];
|
||||||
|
|
||||||
if( partial_block_len > 0U )
|
if( partial_block_len == 0U )
|
||||||
{
|
return( 0 );
|
||||||
memset( zeroes, 0, sizeof( zeroes ) );
|
|
||||||
(void) mbedtls_poly1305_update( &ctx->poly1305_ctx,
|
memset( zeroes, 0, sizeof( zeroes ) );
|
||||||
zeroes,
|
return( mbedtls_poly1305_update( &ctx->poly1305_ctx,
|
||||||
16U - partial_block_len );
|
zeroes,
|
||||||
}
|
16U - partial_block_len ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx )
|
void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx )
|
||||||
@ -199,6 +200,8 @@ int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
|
|||||||
const unsigned char *input,
|
const unsigned char *input,
|
||||||
unsigned char *output )
|
unsigned char *output )
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
if( ctx == NULL )
|
if( ctx == NULL )
|
||||||
{
|
{
|
||||||
return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
|
return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
|
||||||
@ -218,24 +221,32 @@ int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
|
|||||||
{
|
{
|
||||||
ctx->state = CHACHAPOLY_STATE_CIPHERTEXT;
|
ctx->state = CHACHAPOLY_STATE_CIPHERTEXT;
|
||||||
|
|
||||||
chachapoly_pad_aad( ctx );
|
ret = chachapoly_pad_aad( ctx );
|
||||||
|
if( ret != 0 )
|
||||||
|
return( ret );
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->ciphertext_len += len;
|
ctx->ciphertext_len += len;
|
||||||
|
|
||||||
if( ctx->mode == MBEDTLS_CHACHAPOLY_ENCRYPT )
|
if( ctx->mode == MBEDTLS_CHACHAPOLY_ENCRYPT )
|
||||||
{
|
{
|
||||||
/* Note: the following functions return an error only if one or more of
|
ret = mbedtls_chacha20_update( &ctx->chacha20_ctx, len, input, output );
|
||||||
* the input pointers are NULL. Since we have checked their
|
if( ret != 0 )
|
||||||
* validity above, we can safety ignore the return value.
|
return( ret );
|
||||||
*/
|
|
||||||
(void) mbedtls_chacha20_update( &ctx->chacha20_ctx, len, input, output );
|
ret = mbedtls_poly1305_update( &ctx->poly1305_ctx, output, len );
|
||||||
(void) mbedtls_poly1305_update( &ctx->poly1305_ctx, output, len );
|
if( ret != 0 )
|
||||||
|
return( ret );
|
||||||
}
|
}
|
||||||
else /* DECRYPT */
|
else /* DECRYPT */
|
||||||
{
|
{
|
||||||
(void) mbedtls_poly1305_update( &ctx->poly1305_ctx, input, len );
|
ret = mbedtls_poly1305_update( &ctx->poly1305_ctx, input, len );
|
||||||
(void) mbedtls_chacha20_update( &ctx->chacha20_ctx, len, input, output );
|
if( ret != 0 )
|
||||||
|
return( ret );
|
||||||
|
|
||||||
|
ret = mbedtls_chacha20_update( &ctx->chacha20_ctx, len, input, output );
|
||||||
|
if( ret != 0 )
|
||||||
|
return( ret );
|
||||||
}
|
}
|
||||||
|
|
||||||
return( 0 );
|
return( 0 );
|
||||||
@ -244,6 +255,7 @@ int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
|
|||||||
int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
|
int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
|
||||||
unsigned char mac[16] )
|
unsigned char mac[16] )
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
unsigned char len_block[16];
|
unsigned char len_block[16];
|
||||||
|
|
||||||
if( ( ctx == NULL ) || ( mac == NULL ) )
|
if( ( ctx == NULL ) || ( mac == NULL ) )
|
||||||
@ -257,11 +269,15 @@ int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
|
|||||||
|
|
||||||
if( ctx->state == CHACHAPOLY_STATE_AAD )
|
if( ctx->state == CHACHAPOLY_STATE_AAD )
|
||||||
{
|
{
|
||||||
chachapoly_pad_aad( ctx );
|
ret = chachapoly_pad_aad( ctx );
|
||||||
|
if( ret != 0 )
|
||||||
|
return( ret );
|
||||||
}
|
}
|
||||||
else if( ctx->state == CHACHAPOLY_STATE_CIPHERTEXT )
|
else if( ctx->state == CHACHAPOLY_STATE_CIPHERTEXT )
|
||||||
{
|
{
|
||||||
chachapoly_pad_ciphertext( ctx );
|
ret = chachapoly_pad_ciphertext( ctx );
|
||||||
|
if( ret != 0 )
|
||||||
|
return( ret );
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->state = CHACHAPOLY_STATE_FINISHED;
|
ctx->state = CHACHAPOLY_STATE_FINISHED;
|
||||||
@ -286,10 +302,13 @@ int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
|
|||||||
len_block[14] = (unsigned char)( ctx->ciphertext_len >> 48 );
|
len_block[14] = (unsigned char)( ctx->ciphertext_len >> 48 );
|
||||||
len_block[15] = (unsigned char)( ctx->ciphertext_len >> 56 );
|
len_block[15] = (unsigned char)( ctx->ciphertext_len >> 56 );
|
||||||
|
|
||||||
(void) mbedtls_poly1305_update( &ctx->poly1305_ctx, len_block, 16U );
|
ret = mbedtls_poly1305_update( &ctx->poly1305_ctx, len_block, 16U );
|
||||||
(void) mbedtls_poly1305_finish( &ctx->poly1305_ctx, mac );
|
if( ret != 0 )
|
||||||
|
return( ret );
|
||||||
|
|
||||||
return( 0 );
|
ret = mbedtls_poly1305_finish( &ctx->poly1305_ctx, mac );
|
||||||
|
|
||||||
|
return( ret );
|
||||||
}
|
}
|
||||||
|
|
||||||
int mbedtls_chachapoly_crypt_and_tag( mbedtls_chachapoly_context *ctx,
|
int mbedtls_chachapoly_crypt_and_tag( mbedtls_chachapoly_context *ctx,
|
||||||
|
Loading…
Reference in New Issue
Block a user