DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ssh_chacha20.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file ssh_chacha20.h
6 * @brief ChaCha20 stream cipher (D. J. Bernstein; RFC 8439).
7 *
8 * The 20-round ARX permutation used by the chacha20-poly1305@openssh.com cipher. Two views of the
9 * same core are exposed:
10 *
11 * - ssh_chacha20_xor(): the original ChaCha layout OpenSSH uses - a 64-bit little-endian block
12 * counter (state words 12-13) and a 64-bit nonce/IV (words 14-15). This is what the SSH AEAD
13 * drives; the counter increments per 64-byte block.
14 * - ssh_chacha20_block_ietf(): the RFC 8439 layout (32-bit counter in word 12, 96-bit nonce in
15 * words 13-15), exposed so the core can be checked against the published RFC 8439 Section 2.3.2
16 * block test vector.
17 *
18 * Pure ARX (add-rotate-xor): naturally constant-time, no tables, ~64-byte state. No heap.
19 *
20 * @author Douglas Quigg (dstroy0)
21 * @date 2026
22 */
23
24#ifndef DETERMINISTICESPASYNCWEBSERVER_SSH_CHACHA20_H
25#define DETERMINISTICESPASYNCWEBSERVER_SSH_CHACHA20_H
26
27#include <stddef.h>
28#include <stdint.h>
29
30#define SSH_CHACHA20_KEY_LEN 32
31#define SSH_CHACHA20_BLOCK_LEN 64
32
33/**
34 * @brief XOR @p len bytes of @p in with the ChaCha20 keystream into @p out (OpenSSH layout).
35 * @param key 32-byte key.
36 * @param iv 8-byte nonce (OpenSSH uses the packet sequence number, big-endian).
37 * @param counter initial 64-bit block counter (0 for the Poly1305-key / length blocks, 1 for
38 * the packet payload); it increments per 64-byte block.
39 * @param in plaintext (or ciphertext when decrypting); may be nullptr to emit raw keystream.
40 * @param out output buffer (@p len bytes); may alias @p in.
41 */
42void ssh_chacha20_xor(const uint8_t key[SSH_CHACHA20_KEY_LEN], const uint8_t iv[8], uint64_t counter, const uint8_t *in,
43 uint8_t *out, size_t len);
44
45/**
46 * @brief One 64-byte ChaCha20 keystream block in the RFC 8439 layout (for the KAT).
47 * @param key 32-byte key.
48 * @param counter 32-bit block counter (word 12).
49 * @param nonce 12-byte nonce (words 13-15).
50 * @param out 64-byte keystream block.
51 */
52void ssh_chacha20_block_ietf(const uint8_t key[SSH_CHACHA20_KEY_LEN], uint32_t counter, const uint8_t nonce[12],
53 uint8_t out[SSH_CHACHA20_BLOCK_LEN]);
54
55#endif // DETERMINISTICESPASYNCWEBSERVER_SSH_CHACHA20_H
void ssh_chacha20_block_ietf(const uint8_t key[SSH_CHACHA20_KEY_LEN], uint32_t counter, const uint8_t nonce[12], uint8_t out[SSH_CHACHA20_BLOCK_LEN])
One 64-byte ChaCha20 keystream block in the RFC 8439 layout (for the KAT).
#define SSH_CHACHA20_BLOCK_LEN
#define SSH_CHACHA20_KEY_LEN
void ssh_chacha20_xor(const uint8_t key[SSH_CHACHA20_KEY_LEN], const uint8_t iv[8], uint64_t counter, const uint8_t *in, uint8_t *out, size_t len)
XOR len bytes of in with the ChaCha20 keystream into out (OpenSSH layout).