Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5aa8bf2
crypto: error::counter_overflow (22), a stream cipher's counter exhau…
Dec 5, 2026
23a6f9c
crypto: ciphers::chacha20, RFC 8439's raw stream cipher, its contract…
Dec 5, 2026
c25aec8
crypto: the stream cipher's lint: no redundant typename, the stand-in…
Dec 5, 2026
3361621
crypto: ChaCha20 on BearSSL and OpenSSL, the three backends' agreement
Dec 5, 2026
28b2c0c
crypto: ChaCha20's asynchronous forms, member and free, and its refus…
Dec 5, 2026
eb2c15e
crypto: NIP-44 version 2's end-to-end vectors through ChaCha20 on the…
Dec 5, 2026
863d9f1
crypto: ChaCha20 in the constant-time lane (BearSSL and libsodium und…
Dec 5, 2026
76a3a2d
crypto: NIP-44's test support checks one section per function
Dec 5, 2026
f7da651
crypto: authenticators::poly1305, RFC 8439's one-time authenticator, …
Dec 5, 2026
53845ff
crypto: the review round of the stream cipher's tasks: async_from_key…
Dec 5, 2026
19a3453
crypto: Poly1305's review round: AGENTS.md's trait list reflowed, the…
Dec 5, 2026
6cba875
crypto: Poly1305 on OpenSSL, the backends' agreement, RFC 8439's AEAD…
Dec 5, 2026
3e1f57d
crypto: Poly1305's composition and asynchronous tests: the lint's fin…
Dec 5, 2026
a7687a5
crypto: the agreement's sizes read no name that <windows.h> defines a…
Dec 5, 2026
e156b84
crypto: the stream cipher's asynchronous fallbacks say what they need
Dec 5, 2026
262aaa5
crypto: Poly1305 in the constant-time lane (libsodium under memcheck …
Dec 5, 2026
56a1d6e
crypto: the review round of Poly1305's tasks: the free async_verify's…
Dec 5, 2026
d350c48
crypto: the stand-in of makes_empty_v reads its inner result with uns…
Dec 5, 2026
37fa228
crypto: the guide's ChaCha20 section and Poly1305 page, the examples,…
Dec 5, 2026
a514b3c
crypto: the guide's review round: OpenSSL's errors in ChaCha20's guar…
Dec 5, 2026
7576ecc
Merge remote-tracking branch 'origin/main' into crypto/primitives
Dec 5, 2026
280006d
crypto: the final review's round: ChaCha20's refusal alone on a backe…
Dec 5, 2026
dd6372a
crypto: the examples' Jamfile comments wrap at 80 columns
Dec 5, 2026
c638dbf
crypto: MSVC's lint: max() parenthesised against windows.h's macro, P…
Dec 5, 2026
9d66d47
crypto: the counter's end keeps its parentheses against windows.h's m…
Dec 5, 2026
2453484
Merge remote-tracking branch 'origin/main' into crypto/primitives
Dec 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ specific to crypto.

## Cautions

- An algorithm type does every guard; a backend's header supplies only the eight traits of
- An algorithm type does every guard; a backend's header supplies only the eleven traits of
`detail/engines.hpp` (`block_state`, `block_engine`, `gcm_state`, `gcm_engine`, `aead_state`,
`aead_engine`, `secretbox_state`, `secretbox_engine`), the two of `detail/p_curves.hpp` per
P-curve (`p_curve_state`, `p_curve_engine`) and, for the other curves, `public_key_encryption`
and `key_exchange`, the functions of its own header.
`aead_engine`, `secretbox_state`, `secretbox_engine`, `stream_state`, `stream_engine`,
`authenticator_engine`), the two of `detail/p_curves.hpp` per P-curve (`p_curve_state`,
`p_curve_engine`) and, for the other curves, `public_key_encryption` and `key_exchange`, the
functions of its own header.
- A new public function that calls a backend holds `secure::timing_guard` as its first statement
(`secure::timing_guard const timing_scope;`, webcpp::secure's,
`<webcpp/secure/timing_guard.hpp>`, which sets arm64's DIT for the call) and joins the coverage
Expand Down
72 changes: 71 additions & 1 deletion bench/operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#ifndef WEBCPP_CRYPTO_BENCH_OPERATIONS_HPP
#define WEBCPP_CRYPTO_BENCH_OPERATIONS_HPP

#include <webcpp/crypto/authenticators/poly1305.hpp>
#include <webcpp/crypto/ciphers/aes.hpp>
#include <webcpp/crypto/ciphers/chacha20.hpp>
#include <webcpp/crypto/ciphers/chacha20_poly1305.hpp>
#include <webcpp/crypto/ciphers/gcm.hpp>
#include <webcpp/crypto/curves/ed25519.hpp>
Expand Down Expand Up @@ -47,7 +49,7 @@

namespace webcpp::crypto::bench {

// The bytes an AEAD seals at once.
// The bytes an AEAD seals, a stream cipher's keystream covers and an authenticator tags at once.
inline constexpr std::size_t message_size = std::size_t{16} * 1024;

// Ends the program, naming the operation the library refused.
Expand Down Expand Up @@ -154,6 +156,66 @@ class aead_seal {
Aead aead_;
};

// The keystream of 16 KiB XORed in place, from block counter 1, as RFC 8439's AEAD encrypts.
template <class Cipher>
class keystream {
public:
static constexpr std::string_view operation = "keystream of 16 KiB";

explicit keystream(std::uint8_t seed)
: nonce_(filled<Cipher::nonce_size>(static_cast<std::uint8_t>(seed + 2U))),
data_(filled<message_size>(seed)),
cipher_(take(Cipher::from_key(filled<Cipher::key_size>(seed)), "from_key")) {}

std::uint8_t run() {
if (!cipher_.apply_keystream(nonce_, data_, 1)) {
refused("apply_keystream");
}
return data_[0];
}

bool check() {
auto const plain = data_;
static_cast<void>(run());
bool const changed = data_ != plain;
static_cast<void>(run());
return changed && data_ == plain;
}

private:
std::array<std::uint8_t, Cipher::nonce_size> nonce_{};
std::array<std::uint8_t, message_size> data_{};
Cipher cipher_;
};

// The tag of 16 KiB under a one-time key; one key for every repetition, a measurement, never a
// use.
template <class Authenticator>
class one_time_tag {
public:
static constexpr std::string_view operation = "tag of 16 KiB";

explicit one_time_tag(std::uint8_t seed)
: key_(filled<Authenticator::key_size>(static_cast<std::uint8_t>(seed + 4U))),
message_(filled<message_size>(seed)) {}

std::uint8_t run() {
return take(Authenticator::authenticate(key_, message_), "authenticate").data()[0];
}

bool check() {
auto const tag = take(Authenticator::authenticate(key_, message_), "authenticate");
auto forged = tag;
forged.data()[0] ^= 1U;
return Authenticator::verify(key_, message_, tag).has_value() &&
!Authenticator::verify(key_, message_, forged).has_value();
}

private:
std::array<std::uint8_t, Authenticator::key_size> key_{};
std::array<std::uint8_t, message_size> message_{};
};

// AES-256-GCM.
template <class Backend>
using gcm = aead_seal<ciphers::gcm<ciphers::aes256<Backend>>>;
Expand All @@ -174,6 +236,14 @@ using chacha20_poly1305_original = aead_seal<ciphers::chacha20_poly1305_original
template <class Backend>
using xchacha20_poly1305 = aead_seal<ciphers::xchacha20_poly1305<Backend>>;

// ChaCha20, the raw stream cipher (RFC 8439).
template <class Backend>
using chacha20 = keystream<ciphers::chacha20<Backend>>;

// Poly1305, the one-time authenticator (RFC 8439).
template <class Backend>
using poly1305 = one_time_tag<authenticators::poly1305<Backend>>;

#ifdef WEBCPP_CRYPTO_HAS_SECP256K1
// A BIP340 signature of a 32-byte message on secp256k1.
template <class Backend>
Expand Down
24 changes: 17 additions & 7 deletions bench/throughput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
// The throughput of each family's fixed operation (bench/operations.hpp) on each backend this build
// links: AES-256-GCM on BearSSL and OpenSSL, the AES block cipher on ctaes and OpenSSL, BIP340
// signing on libsecp256k1, P-256's ECDSA signing, its verification and its ECDH's x on BearSSL,
// Ed25519 signing and X25519 agreement on libsodium, and the ChaCha20-Poly1305 family on libsodium,
// BearSSL and OpenSSL (OpenSSL natively and on WASI only). A family's further operation, beside its
// fixed one, is named after it, as crypto.json's `sized_operations` names it (P-256's
// `verify_ecdsa` and `ecdh_x`).
// Ed25519 signing and X25519 agreement on libsodium, the ChaCha20-Poly1305 family and the raw
// ChaCha20's keystream on libsodium, BearSSL and OpenSSL, and Poly1305's tag on libsodium and
// OpenSSL (OpenSSL natively and on WASI only). A family's further operation, beside its fixed one,
// is named after it, as crypto.json's `sized_operations` names it (P-256's `verify_ecdsa` and
// `ecdh_x`).
//
// Run with a family and a backend, `throughput <family> <backend> [<operation>] [--runs N]`, it
// repeats the operation, the fixed one when none is named, enough times for a run to last at least
Expand All @@ -19,9 +20,9 @@
// operation and their median. `--list` prints the cells this build has, one "<family> <backend>"
// per line, with " <operation>" after it for a further operation. Run with no argument, it is the
// correctness test: each cell's operation is checked once (an open of what it sealed, a
// verification of what it signed, both sides of an agreement), and the program exits 0 only when
// every check holds. A measurement never fails on its time; bench/collect.py runs it and writes
// doc/performance.adoc.
// verification of what it signed, both sides of an agreement, a keystream applied twice, a tag
// verified and a forged one refused), and the program exits 0 only when every check holds. A
// measurement never fails on its time; bench/collect.py runs it and writes doc/performance.adoc.

#include <webcpp/crypto/backends/bearssl.hpp>
#include <webcpp/crypto/backends/ctaes.hpp>
Expand Down Expand Up @@ -181,6 +182,15 @@ constexpr auto cells = std::to_array<cell>({
#endif
cell_of<bench::chacha20_poly1305_original, backends::sodium>("chacha20_poly1305_original"),
cell_of<bench::xchacha20_poly1305, backends::sodium>("xchacha20_poly1305"),
cell_of<bench::chacha20, backends::sodium>("chacha20"),
cell_of<bench::chacha20, backends::bearssl>("chacha20"),
#ifdef WEBCPP_CRYPTO_HAS_OPENSSL
cell_of<bench::chacha20, backends::openssl>("chacha20"),
#endif
cell_of<bench::poly1305, backends::sodium>("poly1305"),
#ifdef WEBCPP_CRYPTO_HAS_OPENSSL
cell_of<bench::poly1305, backends::openssl>("poly1305"),
#endif
});

int usage() {
Expand Down
22 changes: 22 additions & 0 deletions build.jam
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,28 @@ rule xchacha20_poly1305-default-linked ( properties * )
}
}
alias xchacha20_poly1305-default : : : : <conditional>@xchacha20_poly1305-default-linked ;
rule chacha20-default-linked ( properties * )
{
switch [ webcpp-target.target-of $(properties) ]
{
case native : return <library>/third_party//sodium ;
case wasip2 : return <library>/third_party//sodium ;
case wasip3 : return <library>/third_party//sodium ;
case emscripten : return <library>/third_party//sodium ;
}
}
alias chacha20-default : : : : <conditional>@chacha20-default-linked ;
rule poly1305-default-linked ( properties * )
{
switch [ webcpp-target.target-of $(properties) ]
{
case native : return <library>/third_party//sodium ;
case wasip2 : return <library>/third_party//sodium ;
case wasip3 : return <library>/third_party//sodium ;
case emscripten : return <library>/third_party//sodium ;
}
}
alias poly1305-default : : : : <conditional>@poly1305-default-linked ;
rule secretbox_xsalsa20poly1305-default-linked ( properties * )
{
switch [ webcpp-target.target-of $(properties) ]
Expand Down
45 changes: 31 additions & 14 deletions doc/async.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ its own, and the handler would run off the program's loop.
| `async_from_key<Secretbox>(executor, key, token)` for a secret box (a crypto_box is made by `from_keys`), `secret_key_encryption::async_seal(executor, box, nonce, data, token)`, `secret_key_encryption::async_open(executor, box, nonce, data, tag, token)`
| `void(error_code, Secretbox)` for `async_from_key`, `void(error_code, tag)` for `async_seal`, `void(error_code)` for `async_open`

| ChaCha20, the raw stream cipher (<<crypto-chacha20>>), on a native backend
| `async_from_key<C>(executor, key, token)`, `unauthenticated_encryption::async_apply_keystream(executor, cipher, nonce, data, initial_counter, token)`
| `void(error_code, C)` for `async_from_key`, `void(error_code)` for `async_apply_keystream`

| Poly1305 (<<crypto-poly1305>>), on a native backend
| `single_use_key_authentication::async_authenticate<A>(executor, key, message, token)`, `single_use_key_authentication::async_verify<A>(executor, key, message, tag, token)`
| `void(error_code, tag)` for `async_authenticate`, `void(error_code)` for `async_verify`

| Ed25519 (<<crypto-ed25519>>)
| `async_from_secret<Keypair>(executor, seed, token)`, `async_generate<Keypair>(executor, token)`, `async_sign(executor, key_pair, crypto::signing_schemes::eddsa, message, token)`, `async_verify(executor, crypto::signing_schemes::eddsa, crypto::verification_policies::canonical, signature, message, public_key, token)`; on a native backend the same under `crypto::signing_schemes::eddsa_prehashed` with `+crypto::signing_schemes::prehashed{digest}+`, the message's SHA-512 (<<crypto-ed25519>>), which the browser's backend refuses at compile time
| `void(error_code, Keypair)` for `async_from_secret` and `async_generate`, `void(error_code, signature)` for `async_sign`, `void(error_code)` for `async_verify`
Expand All @@ -71,7 +79,10 @@ same arguments but the object: `Keypair::async_from_secret(executor, bytes, toke
`public_key.async_verify(executor, scheme, policy, signature, input, token)`,
`key_pair.async_agree(executor, peer, token)` on X25519 and P-256, and every
AEAD's and secret box's `C::async_from_key`, `aead.async_seal` and
`aead.async_open`, and a crypto_box's `box.async_seal` and `box.async_open`.
`aead.async_open`, a crypto_box's `box.async_seal` and `box.async_open`,
ChaCha20's `C::async_from_key` and `cipher.async_apply_keystream`, and
Poly1305's static `A::async_authenticate` and `A::async_verify`, which take the
free forms' arguments, as an authenticator has no object.
Both forms share
one initiation, which runs the synchronous member, so the four forms of an
operation give the same bytes, errors and refusals
Expand Down Expand Up @@ -104,7 +115,8 @@ P-256's own types, the key pair and the validated `public_key`, and
The AEADs and boxes WebCrypto lacks (ChaCha20-Poly1305 and its forms, the secret
boxes, crypto_box) have their asynchronous seal, open and from_key on a native
backend alone, so that a program sealing in an event loop writes one shape for
every AEAD. On a backend that completes by itself, as the browser's will
every AEAD, and so do the raw ChaCha20 and Poly1305, which WebCrypto lacks too.
On a backend that completes by itself, as the browser's will
(<<crypto-async-webcrypto>>), no form of them runs, synchronous or asynchronous:
only AES-GCM seals and opens there, and any other stops the build with a message
that says so and names a native backend. The other algorithms of crypto that
Expand All @@ -115,6 +127,10 @@ as these, and are called inside a handler.

include::generated/backends-async_gcm.adoc[]

include::generated/backends-async_stream_cipher.adoc[]

include::generated/backends-async_single_use_key_authenticator.adoc[]

include::generated/backends-async_ed25519.adoc[]

include::generated/backends-async_x25519.adoc[]
Expand Down Expand Up @@ -189,20 +205,21 @@ HPKE: on the browser's backend they stop the build.

*The completion's value on an error.* A value that is a keyed object or a
secret has no meaning when the call failed, and the completion gives the
empty object a move leaves: a `gcm` or a key pair without a key, a `secure::secret<32>`
or a signature of zeros, a tag of zeros. A program that reads the object by
mistake gets `invalid_state` from every member of a `gcm` or a key pair, and
zeros from a secret, never a key. Check the error code first. An operation
without a value, `async_open` and `async_verify`, completes with the code
alone.
empty object a move leaves: a `gcm`, a ChaCha20 or a key pair without a key, a
`secure::secret<32>` or a signature of zeros, a tag of zeros. A program that
reads the object by mistake gets `invalid_state` from every member of a `gcm`,
a ChaCha20 or a key pair, and zeros from a secret, never a key. Check the error
code first. An operation without a value, `async_open`, `async_verify` and
`async_apply_keystream`, completes with the code alone.

*What the guards say.* The completion's code is the synchronous function's,
unchanged: `invalid_key_size`, `invalid_nonce_size`, `message_too_long`,
`authentication_failed`, `invalid_public_key`, `weak_public_key`,
`invalid_signature`, `entropy_unavailable`, `backend_failure`
(<<crypto-errors>>). A refusal is posted like a success, and the data a guard
refuses is untouched; a failed `async_seal` or `async_open` zeroes it, as
`seal` and `open` do.
unchanged: `invalid_key_size`, `invalid_nonce_size`, `invalid_tag_size`,
`message_too_long`, `counter_overflow`, `authentication_failed`,
`invalid_public_key`, `weak_public_key`, `invalid_signature`,
`entropy_unavailable`, `backend_failure` (<<crypto-errors>>). A refusal is
posted like a success, and the data a guard refuses is untouched; a failed
`async_seal`, `async_open` or `async_apply_keystream` zeroes it, as `seal`,
`open` and `apply_keystream` do.

*Pitfalls.*

Expand Down
Loading
Loading