Skip to content

File syn_chacha20poly1305.c

FileList > crypto > syn_chacha20poly1305.c

Go to the source code of this file

ChaCha20-Poly1305 AEAD — RFC 8439. More...

  • #include "../util/syn_pack.h"
  • #include "syn_chacha20poly1305.h"
  • #include <string.h>

Classes

Type Name
struct Poly1305_Ctx
Poly1305 context.

Public Functions

Type Name
bool syn_aead_decrypt (const uint8_t key, const uint8_t nonce, const uint8_t * aad, size_t aad_len, const uint8_t * ciphertext, size_t ct_len, const uint8_t tag, uint8_t * plaintext)
Decrypt and verify (AEAD).
bool syn_aead_decrypt_inplace (const uint8_t key, const uint8_t nonce, const uint8_t * aad, size_t aad_len, uint8_t * buf, size_t len, const uint8_t tag)
Zero-copy in-place AEAD decryption & tag verification.
void syn_aead_encrypt (const uint8_t key, const uint8_t nonce, const uint8_t * aad, size_t aad_len, const uint8_t * plaintext, size_t pt_len, uint8_t * ciphertext, uint8_t tag)
Encrypt and authenticate (AEAD).
void syn_aead_encrypt_inplace (const uint8_t key, const uint8_t nonce, const uint8_t * aad, size_t aad_len, uint8_t * buf, size_t len, uint8_t tag)
Zero-copy in-place AEAD encryption.
void syn_chacha20_block (const uint8_t key, const uint8_t nonce, uint32_t counter, uint8_t out)
Generate ChaCha20 keystream block (no XOR).
void syn_chacha20_xor (const uint8_t key, const uint8_t nonce, uint32_t counter, const uint8_t * in, size_t len, uint8_t * out)
XOR data with ChaCha20 keystream.

Public Static Functions

Type Name
void aead_mac (const uint8_t poly_key, const uint8_t * aad, size_t aad_len, const uint8_t * ct, size_t ct_len, uint8_t tag)
Compute Poly1305 MAC over (aad || pad || ct || pad || len(aad) || len(ct)).
void chacha20_block_core (uint32_t out, const uint32_t in)
Compute one ChaCha20 block into out (16 uint32_t).
void chacha20_init (uint32_t state, const uint8_t key, const uint8_t nonce, uint32_t counter)
Set up the ChaCha20 initial state.
uint32_t load32_le (const uint8_t * p)
void poly1305_blocks (Poly1305_Ctx * ctx, const uint8_t * data, size_t len, uint32_t hibit)
Absorb full 16-byte blocks into Poly1305 accumulator.
void poly1305_finish (Poly1305_Ctx * ctx, uint8_t mac)
Finalise Poly1305 and write the 16-byte MAC to mac .
void poly1305_init (Poly1305_Ctx * ctx, const uint8_t key)
Initialise Poly1305 from a 32-byte one-time key.
uint32_t rotl32 (uint32_t x, unsigned n)
32-bit left rotation.
void store32_le (uint8_t * p, uint32_t v)
void store64_le (uint8_t * p, uint64_t v)
Store 64-bit little-endian word.

Macros

Type Name
define QR (a, b, c, d) /* multi line expression */
ChaCha20 Quarter Round.

Detailed Description

ChaCha20: add-rotate-xor stream cipher (pure 32-bit integer ops). Poly1305: one-time MAC using 130-bit arithmetic with 5 × 26-bit limbs. AEAD: combined construction per RFC 8439 §2.8.

Public Functions Documentation

function syn_aead_decrypt

Decrypt and verify (AEAD).

bool syn_aead_decrypt (
    const uint8_t key,
    const uint8_t nonce,
    const uint8_t * aad,
    size_t aad_len,
    const uint8_t * ciphertext,
    size_t ct_len,
    const uint8_t tag,
    uint8_t * plaintext
) 

Parameters:

  • key 256-bit key (32 bytes).
  • nonce 96-bit nonce (12 bytes).
  • aad Additional authenticated data (or NULL).
  • aad_len AAD length.
  • ciphertext Data to decrypt.
  • ct_len Ciphertext length.
  • tag Expected 128-bit authentication tag (16 bytes).
  • plaintext Output plaintext (same length as ciphertext).

Returns:

true if tag is valid and decryption succeeded.


function syn_aead_decrypt_inplace

Zero-copy in-place AEAD decryption & tag verification.

bool syn_aead_decrypt_inplace (
    const uint8_t key,
    const uint8_t nonce,
    const uint8_t * aad,
    size_t aad_len,
    uint8_t * buf,
    size_t len,
    const uint8_t tag
) 

Parameters:

  • key 256-bit key (32 bytes).
  • nonce 96-bit nonce (12 bytes).
  • aad Additional authenticated data (or NULL).
  • aad_len AAD length.
  • buf In/out buffer containing ciphertext to decrypt in-place.
  • len Data length in bytes.
  • tag Expected 16-byte authentication tag.

Returns:

true if tag matches and decryption succeeded, false otherwise.


function syn_aead_encrypt

Encrypt and authenticate (AEAD).

void syn_aead_encrypt (
    const uint8_t key,
    const uint8_t nonce,
    const uint8_t * aad,
    size_t aad_len,
    const uint8_t * plaintext,
    size_t pt_len,
    uint8_t * ciphertext,
    uint8_t tag
) 

Parameters:

  • key 256-bit key (32 bytes).
  • nonce 96-bit nonce (12 bytes).
  • aad Additional authenticated data (or NULL).
  • aad_len AAD length.
  • plaintext Data to encrypt.
  • pt_len Plaintext length.
  • ciphertext Output ciphertext (same length as plaintext).
  • tag Output 128-bit authentication tag (16 bytes).

function syn_aead_encrypt_inplace

Zero-copy in-place AEAD encryption.

void syn_aead_encrypt_inplace (
    const uint8_t key,
    const uint8_t nonce,
    const uint8_t * aad,
    size_t aad_len,
    uint8_t * buf,
    size_t len,
    uint8_t tag
) 

Parameters:

  • key 256-bit key (32 bytes).
  • nonce 96-bit nonce (12 bytes).
  • aad Additional authenticated data (or NULL).
  • aad_len AAD length.
  • buf In/out buffer containing plaintext to encrypt in-place.
  • len Data length in bytes.
  • tag Output 16-byte authentication tag.

function syn_chacha20_block

Generate ChaCha20 keystream block (no XOR).

void syn_chacha20_block (
    const uint8_t key,
    const uint8_t nonce,
    uint32_t counter,
    uint8_t out
) 

Produces exactly 64 bytes of keystream for the given counter value.

Parameters:

  • key 256-bit key (32 bytes).
  • nonce 96-bit nonce (12 bytes).
  • counter Block counter.
  • out Output buffer (exactly 64 bytes).

function syn_chacha20_xor

XOR data with ChaCha20 keystream.

void syn_chacha20_xor (
    const uint8_t key,
    const uint8_t nonce,
    uint32_t counter,
    const uint8_t * in,
    size_t len,
    uint8_t * out
) 

Encrypts (or decrypts — same operation) by XOR-ing with the ChaCha20 keystream starting at the given block counter.

Parameters:

  • key 256-bit key (32 bytes).
  • nonce 96-bit nonce (12 bytes).
  • counter Initial block counter (usually 0 or 1).
  • in Input data.
  • len Data length in bytes.
  • out Output buffer (may alias in).

Public Static Functions Documentation

function aead_mac

Compute Poly1305 MAC over (aad || pad || ct || pad || len(aad) || len(ct)).

static void aead_mac (
    const uint8_t poly_key,
    const uint8_t * aad,
    size_t aad_len,
    const uint8_t * ct,
    size_t ct_len,
    uint8_t tag
) 

Parameters:

  • poly_key One-time Poly1305 key (32 bytes).
  • aad Additional authenticated data.
  • aad_len Length of aad.
  • ct Ciphertext.
  • ct_len Length of ct.
  • tag Output 16-byte tag.

function chacha20_block_core

Compute one ChaCha20 block into out (16 uint32_t).

static void chacha20_block_core (
    uint32_t out,
    const uint32_t in
) 

Parameters:

  • out Output buffer (16 words).
  • in Initial state (16 words).

function chacha20_init

Set up the ChaCha20 initial state.

static void chacha20_init (
    uint32_t state,
    const uint8_t key,
    const uint8_t nonce,
    uint32_t counter
) 

Parameters:

  • state Initial state buffer (16 words).
  • key 256-bit key.
  • nonce 96-bit nonce.
  • counter Initial block counter.

function load32_le

static inline uint32_t load32_le (
    const uint8_t * p
) 

function poly1305_blocks

Absorb full 16-byte blocks into Poly1305 accumulator.

static void poly1305_blocks (
    Poly1305_Ctx * ctx,
    const uint8_t * data,
    size_t len,
    uint32_t hibit
) 

Parameters:

  • ctx Poly1305 context.
  • data Input data.
  • len Length in bytes (must be multiple of 16).
  • hibit High bit to add (1<<24 for message, 0 for special uses).

function poly1305_finish

Finalise Poly1305 and write the 16-byte MAC to mac .

static void poly1305_finish (
    Poly1305_Ctx * ctx,
    uint8_t mac
) 

Parameters:

  • ctx Poly1305 context.
  • mac Output buffer (16 bytes).

function poly1305_init

Initialise Poly1305 from a 32-byte one-time key.

static void poly1305_init (
    Poly1305_Ctx * ctx,
    const uint8_t key
) 

Parameters:

  • ctx Context to initialize.
  • key 32-byte key.

function rotl32

32-bit left rotation.

static inline uint32_t rotl32 (
    uint32_t x,
    unsigned n
) 

Parameters:

  • x Value to rotate.
  • n Bits to shift.

Returns:

Rotated value.


function store32_le

static inline void store32_le (
    uint8_t * p,
    uint32_t v
) 

function store64_le

Store 64-bit little-endian word.

static inline void store64_le (
    uint8_t * p,
    uint64_t v
) 

Parameters:

  • p Destination bytes.
  • v Value to store.

Macro Definition Documentation

define QR

ChaCha20 Quarter Round.

#define QR (
    a,
    b,
    c,
    d
) `/* multi line expression */`

Parameters:

  • a Indices into the state vector.


The documentation for this class was generated from the following file src/syntropic/crypto/syn_chacha20poly1305.c