Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
2b433e5
crypto: timing_hash_interop goes with hash's copy of the guard
Nov 25, 2026
b69e8d4
crypto: every public operation holds webcpp::secure's timing guard, a…
Nov 25, 2026
1566153
crypto: timing_unseamed keeps its licence notice, and no unused names…
Nov 25, 2026
89f76e3
crypto: the guard's sentence of the performance page names ARM64EC am…
Nov 25, 2026
2467d20
crypto: the MSVC compile-fail case secret_of_no_bytes moves to secure…
Nov 25, 2026
78cb909
crypto: keypair.hpp's list item keeps its indent, timing_coverage say…
Nov 25, 2026
04d56e7
crypto: the examples erase their secrets with webcpp::secure::erase_s…
Nov 25, 2026
7940e70
crypto: timing_unseamed reaches the cipher by unsafe_value, since res…
Nov 25, 2026
4c748a9
crypto: timing_unseamed binds the cipher to a name (lint fix)
Nov 25, 2026
89918ec
crypto: throwing_resource_restores_state checks DIT during the unwind…
Nov 26, 2026
94ee3d6
crypto: dudect_sodium's Ed25519 key pair is named signer, not pair, w…
Nov 26, 2026
c34a87c
crypto: the guard's cost is measured again: a guard that found DIT se…
Nov 26, 2026
6e22d1d
crypto: the memory tests' counting_resource has a private destructor …
Nov 26, 2026
d25b0e6
crypto: the timing seam's path_in_library compares a path's component…
Nov 26, 2026
99de4f7
crypto: the OpenSSL memory adapter keeps DIT set across its catches w…
Nov 26, 2026
ea1c823
crypto: the DIT section says an exception leaves DIT as found while i…
Nov 26, 2026
3170ff8
crypto: the guard's rows measured again on an idle machine (clear 37.…
Nov 26, 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
20 changes: 16 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ specific to crypto.
- **webcpp's own library** (`"port-of": null`) over C backends: BearSSL, OpenSSL, ctaes,
libsecp256k1 and libsodium (the superproject's `/third_party//sodium`, shared with
webcpp::hash). The algorithms are the backends'; crypto is the interface shaped as Boost.Hash2's.
crypto depends on config, hash and secure: the timing guard, `erase_secret`,
`constant_time_equal`, `secret<N>` and `declassify` are webcpp::secure's, with no copy and no
alias in crypto.
- **No oracle.** No original runs beside crypto: the evidence is the published vectors (the GCM
specification's cases, Wycheproof, FIPS 197, RFC 8439 and RFC 8032, BIP340 and BIP341), the
backends' own suites in the lane `c-suites`, and the agreement tests, which compare two backends
Expand Down Expand Up @@ -43,11 +46,20 @@ specific to crypto.
`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.
- A new public function that calls a backend holds `detail::timing_guard` as its
first statement (it sets arm64's DIT for the call, `detail/timing.hpp`) and joins the coverage
- 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
test of its backend, `test/timing_coverage_<backend>_test.cpp`, which checks through the seam
that each call entered its own guard. A function that only copies or encodes bytes, with no
backend call, holds none.
that each call entered its own guard. The seam support (`test/support/timing_seam.hpp`) counts
crypto's own guards by their file, under `include/webcpp/crypto/`, in `timing_entries`, and
every guard, hash's and secure's included, in `timing_all_entries`. A function that only copies
or encodes bytes, with no backend call, holds none.
- Every wipe of a key, a state or a failed output is `secure::erase_secret` over a span
(`<webcpp/secure/erase_secret.hpp>`; an object that is not an array as
`std::span(&object, 1)`); a verdict webcpp branches on is marked with `secure::declassify(x)`,
which the constant-time lane's `WEBCPP_SECURE_CT_VALGRIND` and `WEBCPP_SECURE_CT_MSAN` turn into
the checker's mark; a returned secret is a `secure::secret<N>`, a comparison of secrets
`secure::constant_time_equal`, each always written qualified.
- The constant-time lane's P-256 paths (`test/constant_time/paths.hpp`): BearSSL's ECDSA signing
branches on its own verdicts, listed for memcheck in `bearssl_verdicts.supp`, which a later
P-curve's signing joins; MemorySanitizer counts BearSSL's window selections public (masks of the
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ target [webcpp](https://github.com/webcpporg/webcpp) has: native (Linux, macOS,
MSVC), wasip2, wasip3 and Emscripten.

- `webcpp::crypto`: a `random_device` that reads the operating system's or the browser's
generator, `constant_time_equal` for tags and MACs, `erase_secret`, which zeroes a buffer of
the program's with stores the optimiser keeps, and `error`, every failure's value.
generator, and `error`, every failure's value. The constant-time comparison
(`constant_time_equal`), the erasure of a buffer (`erase_secret`) and the self-wiping
`secret<N>` crypto returns are [webcpp::secure](https://github.com/webcpporg/secure)'s, which
crypto depends on.
- `webcpp::crypto::ciphers`: AES-GCM (`gcm`, `aes128_gcm`, `aes256_gcm`) with keys of 16, 24 and
32 bytes, the AES block cipher (`aes128`, `aes192`, `aes256`) and the ChaCha20-Poly1305 AEADs.
- `webcpp::crypto::secret_key_encryption`: NaCl's and libsodium's secret boxes (`secretbox`,
Expand Down
2 changes: 1 addition & 1 deletion bench/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ webcpp.run throughput_check : throughput.cpp : $(.requirements) ;
exe throughput : throughput.cpp : $(.requirements) <variant>release ;
explicit throughput ;

# The guard's cost needs no backend: detail/timing.hpp alone.
# The guard's cost needs no backend: <webcpp/secure/timing_guard.hpp> alone.
webcpp.run timing_check : timing.cpp ;

# The same program at variant=release, the one collect.py --timing measures,
Expand Down
33 changes: 18 additions & 15 deletions bench/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@
script reads (an older bench/timing.cpp's, without the fastest and slowest runs, among them), are
refused with a message naming what is wrong, before the page is written.

The timing guard's cost (detail::timing_guard, which every public operation holds and which sets
64-bit ARM's DIT where the CPU has it) is `timing --measure`'s one line: whether the guard sets DIT
on this machine, and the nanoseconds a guard adds to a call entered from a thread whose DIT is
clear and from one where it is already set, each the median of five runs of 10^7 guards minus an
empty loop's, with the fastest and slowest run. The data keeps it under "timing_guard" with the
date, the machine and the load; the page renders it as a paragraph and a two-row table under the
throughput. Every date is UTC's, as the constant-time lane's are.
The timing guard's cost (webcpp::secure::timing_guard, secure's guard, which every public
operation holds and which sets 64-bit ARM's DIT where the CPU has it) is `timing --measure`'s one
line: whether the guard sets DIT on this machine, and the nanoseconds a guard adds to a call
entered from a thread whose DIT is clear and from one where it is already set, each the median of
five runs of 10^7 guards minus an empty loop's, with the fastest and slowest run. The data keeps
it under "timing_guard" with the date, the machine and the load; the page renders it as a
paragraph and a two-row table under the throughput. Every date is UTC's, as the constant-time
lane's are.

libsodium's x86-64 assembly (HAVE_AMD64_ASM, design D7) is recorded beside the table as not
measured yet, with its reason, until an x86-64 machine runs a scratch build of it by hand.
Expand Down Expand Up @@ -350,7 +351,8 @@ def measure_timing(exe):
loads.append(os.getloadavg())
result = program_line("native", [exe, "--measure"], output, TIMING_LINE_SHAPE)
return {
"measurement": "the nanoseconds detail::timing_guard adds to a call, bench/timing.cpp",
"measurement": "the nanoseconds webcpp::secure::timing_guard adds to a call, "
"bench/timing.cpp",
"date": utc_today(),
"machine": machine(),
"load_average_before_after": [list(loads[0]), list(loads[1])],
Expand Down Expand Up @@ -417,12 +419,13 @@ def render_timing(data, timing, titles, backends):
"[#crypto-performance-timing]",
"==== The timing guard's cost",
"",
"Every public operation runs inside `detail::timing_guard`, which on 64-bit ARM under",
"macOS, iOS, Linux and Android, where the CPU has FEAT_DIT, sets DIT (data-independent",
"timing) for the call and restores the thread's state after it",
"(<<crypto-backends-dit>>): one `mrs`, and where DIT was clear, two `msr`",
"each followed by an `isb`. Elsewhere (x86, WebAssembly, Windows on ARM64) it executes",
"no instruction.",
"Every public operation runs inside `webcpp::secure::timing_guard`, webcpp::secure's",
"guard, which on 64-bit ARM under macOS, iOS, Linux, Android and Windows, where the",
"CPU has FEAT_DIT, sets DIT (data-independent timing) for the call and restores the",
"thread's state after it (<<crypto-backends-dit>>): one `mrs`; where DIT was clear, two",
"`msr` each followed by an `isb`; where it was set, a second `mrs` on the way out",
"(and an `msr` if something inside cleared it). Elsewhere (x86, WebAssembly, ARM64EC) it",
"executes no instruction.",
"",
]
if timing is None:
Expand Down Expand Up @@ -464,7 +467,7 @@ def render_timing(data, timing, titles, backends):
f"| {fmt_ns(timing.get('clear_slowest_ns'))}",
"",
"| already set, by the program or by an outer guard",
"| reads DIT and leaves it set",
"| reads DIT, and reads it again on the way out",
f"| {fmt_ns(timing['set_ns'])}",
f"| {fmt_ns(timing.get('set_fastest_ns'))}",
f"| {fmt_ns(timing.get('set_slowest_ns'))}",
Expand Down
50 changes: 25 additions & 25 deletions bench/timing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
// accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)

// The cost of detail::timing_guard, the scope every public operation of crypto runs in, which sets
// 64-bit ARM's DIT (data-independent timing) bit where the CPU has it and restores the thread's
// state after it.
// The cost of webcpp::secure::timing_guard, secure's guard, the scope every public operation of
// crypto runs in, which sets 64-bit ARM's DIT (data-independent timing) bit where the CPU has it
// and restores the thread's state after it.
//
// Run as `timing --measure`, it times, in one process, 10^7 iterations each of an empty loop body,
// of a guard entered from a thread whose DIT is clear (it sets DIT and clears it again), and of a
// guard entered with DIT already set (it reads DIT and leaves it; the program sets DIT for that
// part with the same `msr` as the guard, and clears it after), five runs of each, interleaved, and
// prints one JSON line: `{"dit": true|false, "clear": <ns>, "set": <ns>, ...}`, each cost the
// median of its runs minus the empty loop's median, in nanoseconds per guard, then each part's
// fastest and slowest run (minus the same median), the iterations, the runs, the target and the
// compiler. "dit" says whether the guard sets DIT here (a target it covers, on a CPU that has it);
// where it does not, the guard executes no instruction and both costs are the loop's noise. Both
// modes fail when the process starts with DIT set, or when the thread's DIT is not as the guard
// must leave it after a part. Run with no argument, it is the correctness test: the same
// guard entered with DIT already set (it reads DIT, and again at its end; the program sets DIT
// for that part with the same `msr` as the guard, and clears it after), five runs of each,
// interleaved, and prints one JSON line: `{"dit": true|false, "clear": <ns>, "set": <ns>, ...}`,
// each cost the median of its runs minus the empty loop's median, in nanoseconds per guard, then
// each part's fastest and slowest run (minus the same median), the iterations, the runs, the
// target and the compiler. "dit" says whether the guard sets DIT here (a target it covers, on a CPU
// that has it); where it does not, the guard executes no instruction and both costs are the loop's
// noise. Both modes fail when the process starts with DIT set, or when the thread's DIT is not as
// the guard must leave it after a part. Run with no argument, it is the correctness test: the same
// measurement at 10^3 iterations, whose line must also have that form, and whose "dit" must be
// what the library reads. A measurement never fails on its time; bench/collect.py --timing runs
// it and writes doc/performance.adoc.

#include <webcpp/crypto/detail/timing.hpp>
#include <webcpp/secure/timing_guard.hpp>

#include <algorithm>
#include <array>
Expand All @@ -36,7 +36,7 @@
#include <span>
#include <string_view>

namespace detail = webcpp::crypto::detail;
namespace secure = webcpp::secure;

namespace {

Expand Down Expand Up @@ -70,18 +70,18 @@ constexpr std::size_t runs = 5;

// Whether the guard sets DIT on this target and CPU.
bool guard_sets_dit() noexcept {
#ifdef WEBCPP_CRYPTO_DETAIL_TIMING_COVERED
return detail::cpu_has_dit();
#ifdef WEBCPP_SECURE_TIMING_COVERED
return secure::cpu_has_dit();
#else
return false;
#endif
}

// Sets or clears this thread's DIT where the guard would, as a program that asks for it does.
void force_dit([[maybe_unused]] bool set) noexcept {
#ifdef WEBCPP_CRYPTO_DETAIL_TIMING_COVERED
if (detail::cpu_has_dit()) {
detail::write_dit(set ? detail::dit_bit : 0);
#ifdef WEBCPP_SECURE_TIMING_COVERED
if (secure::cpu_has_dit()) {
secure::detail::write_dit(set ? secure::detail::dit_bit : 0);
}
#endif
}
Expand Down Expand Up @@ -127,24 +127,24 @@ result measure(std::uint64_t iterations) {
std::array<double, runs> clear{};
std::array<double, runs> set{};
// A process that starts with DIT set would measure the set part twice.
bool restored = !detail::dit_is_set();
bool restored = !secure::dit_is_set();
// Written by every iteration of every part, so that no loop is removed or merged.
std::uint64_t volatile sink = 0;
for (std::size_t run = 0; run < runs; ++run) {
empty[run] = nanoseconds_per_iteration(iterations, [&sink](std::uint64_t i) { sink = i; });
clear[run] = nanoseconds_per_iteration(iterations, [&sink](std::uint64_t i) {
detail::timing_guard const guard;
secure::timing_guard const guard;
sink = i;
});
restored = restored && !detail::dit_is_set();
restored = restored && !secure::dit_is_set();
force_dit(true);
set[run] = nanoseconds_per_iteration(iterations, [&sink](std::uint64_t i) {
detail::timing_guard const guard;
secure::timing_guard const guard;
sink = i;
});
restored = restored && detail::dit_is_set() == guard_sets_dit();
restored = restored && secure::dit_is_set() == guard_sets_dit();
force_dit(false);
restored = restored && !detail::dit_is_set();
restored = restored && !secure::dit_is_set();
}
double const loop = summarise(empty, 0).median;
return {.clear = summarise(clear, loop), .set = summarise(set, loop), .restored = restored};
Expand Down
5 changes: 4 additions & 1 deletion build.jam
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ project /webcpp/crypto ;

# Header-only, on webcpp's configuration, with webcpp::hash's header-only
# alias, whose algorithms name a signature's prehash (Ed25519ph's SHA-512;
# design D1). random_device reads Windows' generator through bcrypt
# design D1), and webcpp::secure's, whose timing guard, erase_secret,
# constant_time_equal, secret<N> and declassify crypto's code holds and
# returns. random_device reads Windows' generator through bcrypt
# (BCryptGenRandom), a system library.
# tag::crypto[]
alias crypto : : : : <include>include <library>/boost//headers
<library>/webcpp/config//config <library>/webcpp/hash//hash
<library>/webcpp/secure//secure
<target-os>windows:<find-shared-library>bcrypt ;
# end::crypto[]

Expand Down
8 changes: 4 additions & 4 deletions doc/async.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ its own, and the handler would run off the program's loop.

| X25519 (<<crypto-x25519>>)
| `async_from_secret<Keypair>(executor, secret_key, token)`, `async_generate<Keypair>(executor, token)`, `async_agree(executor, key_pair, peer_public_key, token)`, the peer's key a `curves::x25519::public_key`
| `void(error_code, Keypair)` for `async_from_secret` and `async_generate`, `void(error_code, secret<32>)` for `async_agree`
| `void(error_code, Keypair)` for `async_from_secret` and `async_generate`, `void(error_code, secure::secret<32>)` for `async_agree`

| P-256 (<<crypto-p256>>)
| `async_from_secret<Keypair>(executor, scalar, token)`, `async_generate<Keypair>(executor, token)`, `async_sign(executor, key_pair, crypto::ecdsa_sha256, input, token)`, `async_verify(executor, crypto::ecdsa_sha256, signature, input, public_key, token)`, `async_agree(executor, key_pair, peer_public_key, token)`
| `void(error_code, Keypair)` for `async_from_secret` and `async_generate`, `void(error_code, signature)` for `async_sign`, `void(error_code)` for `async_verify`, `void(error_code, secret<32>)` for `async_agree`
| `void(error_code, Keypair)` for `async_from_secret` and `async_generate`, `void(error_code, signature)` for `async_sign`, `void(error_code)` for `async_verify`, `void(error_code, secure::secret<32>)` for `async_agree`
|===

Each function has its member form, which the free function calls, with the
Expand Down Expand Up @@ -178,7 +178,7 @@ include::{examples}/async_sign.expected[]

*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 `secret<32>`
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
Expand Down Expand Up @@ -279,7 +279,7 @@ executor: a handler not bound to the loop's completes on the pool, and so
waits for one of its threads.
* After the work starts, it is not interrupted. A `terminal` or `partial`
cancellation lets it run to its end, destroys its value (which wipes a
`secret<N>` or a key pair) and completes with `operation_aborted`. A `total`
`secure::secret<N>` or a key pair) and completes with `operation_aborted`. A `total`
one is then ignored, since the value's destruction is a side effect already
under way.
* The signal is emitted on the handler's executor, as Asio's slots require:
Expand Down
Loading
Loading