DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
mlkem.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 mlkem.h
6 * @brief ML-KEM-768 encapsulation (FIPS 203), responder side only.
7 *
8 * The post-quantum half of the mlkem768x25519-sha256 (SSH) and X25519MLKEM768 (TLS 1.3) hybrid key
9 * exchanges. This server is always the KEM *responder*: the peer sends its ML-KEM encapsulation key,
10 * we run Encaps to produce a ciphertext + shared secret. Only Encaps is implemented - no KeyGen or
11 * Decaps - so none of the constant-time Fujisaki-Okamoto re-encryption/compare surface is present.
12 *
13 * Encaps is the FIPS 203 "internal" (derandomized) form: the 32-octet message @p m is supplied by the
14 * caller (drawn from the platform RNG in production, fixed in known-answer tests). Deterministic given
15 * (@p ek, @p m), which is exactly what the ACVP encapsulation test vectors pin.
16 *
17 * Arithmetic is a software NTT over q=3329 with Montgomery reduction (the twiddle factors are fixed
18 * constants premultiplied into Montgomery form, so each butterfly is two int16 multiplies and a
19 * shift - no division, and the hardware MPI, which targets RSA/DH-sized operands, would only add
20 * marshaling overhead). Zero heap; peak stack ~7 KB.
21 *
22 * @author Douglas Quigg (dstroy0)
23 * @date 2026
24 */
25
26#ifndef DETERMINISTICESPASYNCWEBSERVER_PQC_MLKEM_H
27#define DETERMINISTICESPASYNCWEBSERVER_PQC_MLKEM_H
28
29#include "ServerConfig.h"
30
31#if DETWS_ENABLE_PQC_KEX
32
33#include <stddef.h>
34#include <stdint.h>
35
36#define MLKEM768_EK_BYTES 1184 ///< encapsulation key (public key): 384*k + 32
37#define MLKEM768_CT_BYTES 1088 ///< ciphertext: 32*(du*k + dv) = 32*(30+4)
38#define MLKEM768_SS_BYTES 32 ///< shared secret
39#define MLKEM768_MSG_BYTES 32 ///< the random message m fed to Encaps
40
41/**
42 * @brief ML-KEM-768 Encaps (FIPS 203, derandomized): (ct, ss) from an encapsulation key and message.
43 *
44 * Validates @p ek (FIPS 203 modulus check: every decoded coefficient must be < q); on a malformed key
45 * it writes nothing and returns false. Otherwise derives K = ss and encrypts @p m under @p ek.
46 *
47 * @param[in] ek peer encapsulation key (MLKEM768_EK_BYTES).
48 * @param[in] m 32-octet message (the encapsulation randomness).
49 * @param[out] ct ciphertext (MLKEM768_CT_BYTES).
50 * @param[out] ss 32-octet shared secret.
51 * @return true on success, false if @p ek fails the modulus check.
52 */
53bool mlkem768_encaps(const uint8_t ek[MLKEM768_EK_BYTES], const uint8_t m[MLKEM768_MSG_BYTES],
54 uint8_t ct[MLKEM768_CT_BYTES], uint8_t ss[MLKEM768_SS_BYTES]);
55
56#endif // DETWS_ENABLE_PQC_KEX
57
58#endif // DETERMINISTICESPASYNCWEBSERVER_PQC_MLKEM_H
User-facing configuration for DeterministicESPAsyncWebServer.