diff --git a/include/mbedtls/cipher.h b/include/mbedtls/cipher.h index 591aa79aa..1ae847d20 100644 --- a/include/mbedtls/cipher.h +++ b/include/mbedtls/cipher.h @@ -193,7 +193,7 @@ enum { /** Maximum length of any IV, in Bytes. */ #define MBEDTLS_MAX_IV_LENGTH 16 /** Maximum block size of any cipher, in Bytes. */ -#define MBEDTLS_MAX_BLOCK_LENGTH 64 +#define MBEDTLS_MAX_BLOCK_LENGTH 16 /** * Base cipher information (opaque struct). diff --git a/library/cipher.c b/library/cipher.c index 2463a6148..cf10094f6 100644 --- a/library/cipher.c +++ b/library/cipher.c @@ -367,6 +367,15 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i } #endif +#if defined(MBEDTLS_CHACHAPOLY_C) + if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 ) + { + *olen = ilen; + return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx, + ilen, input, output ); + } +#endif + if ( 0 == block_size ) { return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT; @@ -378,31 +387,6 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); } - -#if defined(MBEDTLS_CHACHA20_C) - if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ) - { - *olen = ilen; - return mbedtls_chacha20_update( (mbedtls_chacha20_context*) ctx->cipher_ctx, - ilen, input, output ); - } -#endif - - if( input == output && - ( ctx->unprocessed_len != 0 || ilen % mbedtls_cipher_get_block_size( ctx ) ) ) - { - return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); - } - -#if defined(MBEDTLS_CHACHAPOLY_C) - if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 ) - { - *olen = ilen; - return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx, - ilen, input, output ); - } -#endif - #if defined(MBEDTLS_CIPHER_MODE_CBC) if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC ) { diff --git a/library/cipher_wrap.c b/library/cipher_wrap.c index 5c8082850..9110b968c 100644 --- a/library/cipher_wrap.c +++ b/library/cipher_wrap.c @@ -1305,6 +1305,19 @@ static int chacha20_setkey_wrap( void *ctx, const unsigned char *key, return( 0 ); } +static int chacha20_stream_wrap( void *ctx, size_t length, + const unsigned char *input, + unsigned char *output ) +{ + int ret; + + ret = mbedtls_chacha20_update( ctx, length, input, output ); + if( ret == MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA ) + return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); + + return( ret ); +} + static void * chacha20_ctx_alloc( void ) { mbedtls_chacha20_context *ctx; @@ -1337,7 +1350,7 @@ static const mbedtls_cipher_base_t chacha20_base_info = { NULL, #endif #if defined(MBEDTLS_CIPHER_MODE_STREAM) - NULL, + chacha20_stream_wrap, #endif chacha20_setkey_wrap, chacha20_setkey_wrap, @@ -1346,12 +1359,12 @@ static const mbedtls_cipher_base_t chacha20_base_info = { }; static const mbedtls_cipher_info_t chacha20_info = { MBEDTLS_CIPHER_CHACHA20, - MBEDTLS_MODE_NONE, + MBEDTLS_MODE_STREAM, 256, "CHACHA20", 12, 0, - 64, + 1, &chacha20_base_info }; #endif /* MBEDTLS_CHACHA20_C */ @@ -1417,7 +1430,7 @@ static const mbedtls_cipher_info_t chachapoly_info = { "CHACHA20-POLY1305", 12, 0, - 64, + 1, &chachapoly_base_info }; #endif /* MBEDTLS_CHACHAPOLY_C */