DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
sha3.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 sha3.h
6 * @brief Keccak-f[1600] sponge: SHA3-256, SHA3-512, SHAKE128, SHAKE256 (FIPS 202).
7 *
8 * The symmetric primitives ML-KEM (FIPS 203) is built on: G = SHA3-512, H = SHA3-256, the matrix
9 * XOF = SHAKE128, and the noise PRF = SHAKE256. Zero-heap, endian-independent (the sponge state is
10 * addressed as a little-endian byte string regardless of host byte order), no external dependency.
11 *
12 * One-shot helpers cover fixed-length digests and arbitrary SHAKE output. For an incremental XOF
13 * (ML-KEM samples the public matrix by squeezing three bytes at a time) absorb once with
14 * shake128_absorb() then pull with keccak_squeeze() as many times as needed.
15 *
16 * @author Douglas Quigg (dstroy0)
17 * @date 2026
18 */
19
20#ifndef DETERMINISTICESPASYNCWEBSERVER_PQC_SHA3_H
21#define DETERMINISTICESPASYNCWEBSERVER_PQC_SHA3_H
22
23#include "ServerConfig.h"
24
25#if DETWS_ENABLE_PQC_KEX
26
27#include <stddef.h>
28#include <stdint.h>
29
30/// Sponge rates (block size in octets = 1600/8 - 2*capacity/8) for the modes we use.
31#define KECCAK_RATE_SHA3_256 136
32#define KECCAK_RATE_SHA3_512 72
33#define KECCAK_RATE_SHAKE128 168
34#define KECCAK_RATE_SHAKE256 136
35
36/// A Keccak sponge in the squeeze phase; `out_pos` is how many octets of the current block are spent.
37struct KeccakCtx
38{
39 uint64_t st[25];
40 uint32_t rate;
41 uint32_t out_pos;
42};
43
44/// Absorb the whole message with domain-separation byte @p domain (0x06 SHA3, 0x1F SHAKE) and pad,
45/// leaving @p c ready to squeeze. Handles any input length (multi-block).
46void keccak_absorb(KeccakCtx *c, uint32_t rate, const uint8_t *in, size_t inlen, uint8_t domain);
47
48/// Squeeze @p outlen octets, permuting between blocks. May be called repeatedly for XOF use.
49void keccak_squeeze(KeccakCtx *c, uint8_t *out, size_t outlen);
50
51/// SHA3-256 one-shot: 32-octet digest of @p in.
52void sha3_256(uint8_t out[32], const uint8_t *in, size_t inlen);
53
54/// SHA3-512 one-shot: 64-octet digest of @p in.
55void sha3_512(uint8_t out[64], const uint8_t *in, size_t inlen);
56
57/// SHAKE128 one-shot: @p outlen octets from @p in.
58void shake128(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen);
59
60/// SHAKE256 one-shot: @p outlen octets from @p in.
61void shake256(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen);
62
63/// Begin an incremental SHAKE128 XOF over @p in; pull output with keccak_squeeze(@p c, ...).
64void shake128_absorb(KeccakCtx *c, const uint8_t *in, size_t inlen);
65
66#endif // DETWS_ENABLE_PQC_KEX
67
68#endif // DETERMINISTICESPASYNCWEBSERVER_PQC_SHA3_H
User-facing configuration for DeterministicESPAsyncWebServer.