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:
key256-bit key (32 bytes).nonce96-bit nonce (12 bytes).aadAdditional authenticated data (or NULL).aad_lenAAD length.ciphertextData to decrypt.ct_lenCiphertext length.tagExpected 128-bit authentication tag (16 bytes).plaintextOutput 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:
key256-bit key (32 bytes).nonce96-bit nonce (12 bytes).aadAdditional authenticated data (or NULL).aad_lenAAD length.bufIn/out buffer containing ciphertext to decrypt in-place.lenData length in bytes.tagExpected 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:
key256-bit key (32 bytes).nonce96-bit nonce (12 bytes).aadAdditional authenticated data (or NULL).aad_lenAAD length.plaintextData to encrypt.pt_lenPlaintext length.ciphertextOutput ciphertext (same length as plaintext).tagOutput 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:
key256-bit key (32 bytes).nonce96-bit nonce (12 bytes).aadAdditional authenticated data (or NULL).aad_lenAAD length.bufIn/out buffer containing plaintext to encrypt in-place.lenData length in bytes.tagOutput 16-byte authentication tag.
function syn_chacha20_block¶
Generate ChaCha20 keystream block (no XOR).
Produces exactly 64 bytes of keystream for the given counter value.
Parameters:
key256-bit key (32 bytes).nonce96-bit nonce (12 bytes).counterBlock counter.outOutput 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:
key256-bit key (32 bytes).nonce96-bit nonce (12 bytes).counterInitial block counter (usually 0 or 1).inInput data.lenData length in bytes.outOutput 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_keyOne-time Poly1305 key (32 bytes).aadAdditional authenticated data.aad_lenLength ofaad.ctCiphertext.ct_lenLength ofct.tagOutput 16-byte tag.
function chacha20_block_core¶
Compute one ChaCha20 block into out (16 uint32_t).
Parameters:
outOutput buffer (16 words).inInitial 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:
stateInitial state buffer (16 words).key256-bit key.nonce96-bit nonce.counterInitial block counter.
function load32_le¶
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:
ctxPoly1305 context.dataInput data.lenLength in bytes (must be multiple of 16).hibitHigh bit to add (1<<24 for message, 0 for special uses).
function poly1305_finish¶
Finalise Poly1305 and write the 16-byte MAC to mac .
Parameters:
ctxPoly1305 context.macOutput buffer (16 bytes).
function poly1305_init¶
Initialise Poly1305 from a 32-byte one-time key.
Parameters:
ctxContext to initialize.key32-byte key.
function rotl32¶
32-bit left rotation.
Parameters:
xValue to rotate.nBits to shift.
Returns:
Rotated value.
function store32_le¶
function store64_le¶
Store 64-bit little-endian word.
Parameters:
pDestination bytes.vValue to store.
Macro Definition Documentation¶
define QR¶
ChaCha20 Quarter Round.
Parameters:
aIndices into the state vector.
The documentation for this class was generated from the following file src/syntropic/crypto/syn_chacha20poly1305.c