1
0
mirror of https://github.com/ARMmbed/mbedtls.git synced 2025-06-27 07:37:05 +08:00

Add support for chunked plaintext/cyphertext input.

Signed-off-by: Mateusz Starzyk <mateusz.starzyk@mobica.com>
This commit is contained in:
Mateusz Starzyk 2021-07-07 13:41:30 +02:00
parent 2ad7d8e1ff
commit 6a15bcf61b

View File

@ -333,48 +333,55 @@ int mbedtls_ccm_update( mbedtls_ccm_context *ctx,
{ {
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
unsigned char i; unsigned char i;
size_t len_left, olen; size_t use_len, offset, olen;
const unsigned char *src;
unsigned char *dst;
if( output_size < input_len ) if( output_size < input_len )
return( MBEDTLS_ERR_CCM_BAD_INPUT ); return( MBEDTLS_ERR_CCM_BAD_INPUT );
CCM_VALIDATE_RET( output_length != NULL ); CCM_VALIDATE_RET( output_length != NULL );
*output_len = input_len; *output_len = input_len;
/* if( ctx->processed == 0 )
* Authenticate and {en,de}crypt the message.
*
* The only difference between encryption and decryption is
* the respective order of authentication and {en,de}cryption.
*/
len_left = input_len;
src = input;
dst = output;
while( len_left > 0 )
{
size_t use_len = len_left > 16 ? 16 : len_left;
if( ctx->mode == CCM_ENCRYPT )
{ {
memset( ctx->b, 0, 16 ); memset( ctx->b, 0, 16 );
memcpy( ctx->b, src, use_len );
UPDATE_CBC_MAC;
} }
mbedtls_ccm_crypt( ctx, 0, use_len, src, dst ); while ( input_len > 0 )
{
offset = ctx->processed % 16;
use_len = 16 - offset;
if( use_len > input_len )
use_len = input_len;
ctx->processed += use_len;
memcpy( ctx->b + offset, input, use_len );
if( use_len + offset == 16 || ctx->processed == ctx->plaintext_len )
{
if( ctx->mode == CCM_ENCRYPT )
{
UPDATE_CBC_MAC;
ret = mbedtls_ccm_crypt( ctx, 0, use_len, ctx->b, output );
if( ret != 0 )
return ret;
memset( ctx->b, 0, 16 );
}
if( ctx->mode == CCM_DECRYPT ) if( ctx->mode == CCM_DECRYPT )
{ {
ret = mbedtls_ccm_crypt( ctx, 0, use_len, ctx->b, output );
if( ret != 0 )
return ret;
memset( ctx->b, 0, 16 ); memset( ctx->b, 0, 16 );
memcpy( ctx->b, dst, use_len ); memcpy( ctx->b, output, use_len );
UPDATE_CBC_MAC; UPDATE_CBC_MAC;
memset( ctx->b, 0, 16 );
} }
dst += use_len; input_len -= use_len;
src += use_len; input += use_len;
len_left -= use_len; output += use_len;
/* /*
* Increment counter. * Increment counter.
@ -384,8 +391,9 @@ int mbedtls_ccm_update( mbedtls_ccm_context *ctx,
if( ++(ctx->ctr)[15-i] != 0 ) if( ++(ctx->ctr)[15-i] != 0 )
break; break;
} }
}
return (0); return 0;
} }
int mbedtls_ccm_finish( mbedtls_ccm_context *ctx, int mbedtls_ccm_finish( mbedtls_ccm_context *ctx,