chacha20poly1305 AEAD cipher

Provides RFC 8439 style chacha20poly1305. More...

Detailed Description

Provides RFC 8439 style chacha20poly1305.

This module provides the chacha20poly1305 AEAD symmetric cipher following rfc 8439.

Nonces must be unique per message for a single key. They are allowed to be predictable, e.g. a message counter and are allowed to be visible during transmission.

Files

file  chacha20poly1305.h
 Chacha20poly1305 functions.
 

Data Structures

union  chacha20_ctx_t
 Context of a ChaCha20 multipart operation. More...
 
union  chacha20poly1305_ctx_t
 Chacha20poly1305 state struct. More...
 

Macros

#define CHACHA20POLY1305_CONSTANT_BYTES   (16U)
 Constants length in bytes.
 
#define CHACHA20POLY1305_KEY_BYTES   (32U)
 Key length in bytes.
 
#define CHACHA20POLY1305_COUNTER_BYTES   (4U)
 Counter length in bytes.
 
#define CHACHA20POLY1305_NONCE_BYTES   (12U)
 Nonce length in bytes.
 
#define CHACHA20POLY1305_TAG_BYTES   (16U)
 Tag length in bytes.
 
#define CHACHA20POLY1305_BLOCK_BYTES   (64U)
 Block length in bytes.
 

Functions

void chacha20poly1305_encrypt (uint8_t *cipher, const uint8_t *msg, size_t msglen, const uint8_t *aad, size_t aadlen, const uint8_t *key, const uint8_t *nonce)
 Encrypt a plaintext to ciphertext and append a tag to protect the ciphertext and additional data. More...
 
int chacha20poly1305_decrypt (const uint8_t *cipher, size_t cipherlen, uint8_t *msg, size_t *msglen, const uint8_t *aad, size_t aadlen, const uint8_t *key, const uint8_t *nonce)
 Verify the tag and decrypt a ciphertext to plaintext. More...
 
void chacha20_encrypt_decrypt (const uint8_t *key, const uint8_t *nonce, uint32_t counter, const uint8_t *input, size_t input_length, uint8_t *output)
 Encrypt a plaintext to ciphertext with the ChaCha20 algorithm. More...
 
void chacha20_setup (chacha20_ctx_t *ctx, const uint8_t *key, const uint8_t *nonce, const uint32_t counter)
 Setup a ChaCha20 encrypt or decrypt multipart operation. More...
 
void chacha20_update (chacha20_ctx_t *ctx, const uint8_t *input, uint8_t *output)
 Update a ChaCha20 encrypt or decrypt multipart operation. More...
 
void chacha20_finish (chacha20_ctx_t *ctx, const uint8_t *input, size_t input_length, uint8_t *output)
 Finish a ChaCha20 encrypt or decrypt multipart operation. More...
 

Function Documentation

◆ chacha20_encrypt_decrypt()

void chacha20_encrypt_decrypt ( const uint8_t *  key,
const uint8_t *  nonce,
uint32_t  counter,
const uint8_t *  input,
size_t  input_length,
uint8_t *  output 
)

Encrypt a plaintext to ciphertext with the ChaCha20 algorithm.

Parameters
[in]keyKey to encrypt/decrypt with, must be CHACHA20POLY1305_KEY_BYTES long.
[in]nonceNonce to use. Must be CHACHA20POLY1305_NONCE_BYTES long.
[in]counterInitial counter for the ChaCha20 operation.
[in]inputInput for the encryption/decryption.
[in]input_lengthLength of the input byte array.
[out]outputThe resulting encrypted cipher/decrypted message.

◆ chacha20_finish()

void chacha20_finish ( chacha20_ctx_t ctx,
const uint8_t *  input,
size_t  input_length,
uint8_t *  output 
)

Finish a ChaCha20 encrypt or decrypt multipart operation.

Parameters
[in]ctxContext of the multipart ChaCha20 operation.
[in]inputInput buffer.
[in]input_lengthLength of input buffer. Must be a less than 64B.
[out]outputOutput buffer. Must be at least length of input buffer.

◆ chacha20_setup()

void chacha20_setup ( chacha20_ctx_t ctx,
const uint8_t *  key,
const uint8_t *  nonce,
const uint32_t  counter 
)

Setup a ChaCha20 encrypt or decrypt multipart operation.

Parameters
[out]ctxContext of the multipart ChaCha20 operation.
[in]keyKey to encrypt/decrypt with, must be CHACHA20POLY1305_KEY_BYTES long.
[in]nonceNonce to use. Must be CHACHA20POLY1305_NONCE_BYTES long.
[in]counterInitial counter for the multipart ChaCha20 operation.

◆ chacha20_update()

void chacha20_update ( chacha20_ctx_t ctx,
const uint8_t *  input,
uint8_t *  output 
)

Update a ChaCha20 encrypt or decrypt multipart operation.

Parameters
[in]ctxContext of the multipart ChaCha20 operation.
[in]inputInput buffer containing one block of input data (64B).
[out]outputOutput buffer. Must be at least length of input buffer.

◆ chacha20poly1305_decrypt()

int chacha20poly1305_decrypt ( const uint8_t *  cipher,
size_t  cipherlen,
uint8_t *  msg,
size_t *  msglen,
const uint8_t *  aad,
size_t  aadlen,
const uint8_t *  key,
const uint8_t *  nonce 
)

Verify the tag and decrypt a ciphertext to plaintext.

It is allowed to have cipher == msg

Parameters
[in]cipherresulting ciphertext, is CHACHA20POLY1305_TAG_BYTES longer than the message length
[in]cipherlenlength of the ciphertext
[out]msgmessage to encrypt
[in]msglenresulting length in bytes of the message
[in]aadadditional authenticated data to verify
[in]aadlenlength of the additional authenticated data
[in]keykey to decrypt with, must be CHACHA20POLY1305_KEY_BYTES long
[in]nonceNonce to use. Must be CHACHA20POLY1305_NONCE_BYTES long

◆ chacha20poly1305_encrypt()

void chacha20poly1305_encrypt ( uint8_t *  cipher,
const uint8_t *  msg,
size_t  msglen,
const uint8_t *  aad,
size_t  aadlen,
const uint8_t *  key,
const uint8_t *  nonce 
)

Encrypt a plaintext to ciphertext and append a tag to protect the ciphertext and additional data.

It is allowed to have cipher == msg as long as there is CHACHA20POLY1305_TAG_BYTES space left to hold the authentication tag

Parameters
[out]cipherresulting ciphertext, is CHACHA20POLY1305_TAG_BYTES longer than the message length
[in]msgmessage to encrypt
[in]msglenlength in bytes of the message
[in]aadadditional authenticated data to protect
[in]aadlenlength of the additional authenticated data
[in]keykey to encrypt with, must be CHACHA20POLY1305_KEY_BYTES long
[in]nonceNonce to use. Must be CHACHA20POLY1305_NONCE_BYTES long