Skip to content

File syn_chacha20poly1305.h

FileList > crypto > syn_chacha20poly1305.h

Go to the source code of this file

ChaCha20-Poly1305 AEAD — RFC 8439, pure C99. More...

  • #include <stdbool.h>
  • #include <stddef.h>
  • #include <stdint.h>

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.

Detailed Description

Provides the Authenticated Encryption with Associated Data (AEAD) construction used by WireGuard for both handshake and transport encryption. Also exposes raw ChaCha20 for standalone use.

All state is caller-owned. No heap.

** **

uint8_t key[32], nonce[12], tag[16];
uint8_t plaintext[64], ciphertext[64];

// Encrypt:
syn_aead_encrypt(key, nonce, NULL, 0,
                 plaintext, 64, ciphertext, tag);

// Decrypt:
bool ok = syn_aead_decrypt(key, nonce, NULL, 0,
                           ciphertext, 64, tag, plaintext);

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).


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