1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-07-29 11:41:15 +03:00

Add ChaCha20+Poly1305 to the Cipher module

This commit is contained in:
Daniel King
2016-05-17 20:33:28 -03:00
committed by Manuel Pégourié-Gonnard
parent a310c5e42b
commit 8fe4701abe
7 changed files with 391 additions and 25 deletions

View File

@ -33,6 +33,10 @@
#include "mbedtls/cipher_internal.h"
#if defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
#include "mbedtls/aead_chacha20_poly1305.h"
#endif
#if defined(MBEDTLS_AES_C)
#include "mbedtls/aes.h"
#endif
@ -1352,6 +1356,71 @@ static const mbedtls_cipher_info_t chacha20_info = {
};
#endif /* MBEDTLS_CHACHA20_C */
#if defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
static int aead_chacha20_poly1305_setkey_wrap( void *ctx, const unsigned char *key,
unsigned int key_bitlen )
{
if( key_bitlen != 256U )
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
if ( 0 != mbedtls_aead_chacha20_poly1305_setkey( (mbedtls_aead_chacha20_poly1305_context*)ctx, key ) )
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
return( 0 );
}
static void * aead_chacha20_poly1305_ctx_alloc( void )
{
mbedtls_aead_chacha20_poly1305_context *ctx;
ctx = mbedtls_calloc( 1, sizeof( mbedtls_aead_chacha20_poly1305_context ) );
if( ctx == NULL )
return( NULL );
mbedtls_aead_chacha20_poly1305_init( ctx );
return( ctx );
}
static void aead_chacha20_poly1305_ctx_free( void *ctx )
{
mbedtls_aead_chacha20_poly1305_free( (mbedtls_aead_chacha20_poly1305_context *) ctx );
mbedtls_free( ctx );
}
static const mbedtls_cipher_base_t aead_chacha20_poly1305_base_info = {
MBEDTLS_CIPHER_ID_CHACHA20,
NULL,
#if defined(MBEDTLS_CIPHER_MODE_CBC)
NULL,
#endif
#if defined(MBEDTLS_CIPHER_MODE_CFB)
NULL,
#endif
#if defined(MBEDTLS_CIPHER_MODE_CTR)
NULL,
#endif
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
NULL,
#endif
aead_chacha20_poly1305_setkey_wrap,
aead_chacha20_poly1305_setkey_wrap,
aead_chacha20_poly1305_ctx_alloc,
aead_chacha20_poly1305_ctx_free
};
static const mbedtls_cipher_info_t aead_chacha20_poly1305_info = {
MBEDTLS_CIPHER_CHACHA20_POLY1305,
MBEDTLS_MODE_NONE,
256,
"CHACHA20-POLY1305",
12,
0,
64,
&aead_chacha20_poly1305_base_info
};
#endif /* MBEDTLS_AEAD_CHACHA20_POLY1305_C */
#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
static int null_crypt_stream( void *ctx, size_t length,
const unsigned char *input,
@ -1511,6 +1580,10 @@ const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
{ MBEDTLS_CIPHER_CHACHA20, &chacha20_info },
#endif
#if defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
{ MBEDTLS_CIPHER_CHACHA20_POLY1305, &aead_chacha20_poly1305_info },
#endif
#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
{ MBEDTLS_CIPHER_NULL, &null_cipher_info },
#endif /* MBEDTLS_CIPHER_NULL_CIPHER */