From 45c0d41aa44a45567599364fc59b0854ef1a2612 Mon Sep 17 00:00:00 2001 From: R Date: Thu, 3 Dec 2026 14:38:33 -0300 Subject: [PATCH 1/2] secure: the page's title fits the site's 60 characters The site's assembly (tools/ci/assemble.py) refuses a library page whose title, with " - webcpp", is longer than 60 characters, so main's Site job (run 47116123227) never deployed: "secure: side-channel and secret-in-memory primitives for C++ - webcpp" is 69. The heading is now "secure: constant time and secrets in memory for C++" (60 with the suffix); the page's first paragraph keeps the longer description. --- doc/secure.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/secure.adoc b/doc/secure.adoc index b6f041d..8d9979c 100644 --- a/doc/secure.adoc +++ b/doc/secure.adoc @@ -4,7 +4,7 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -= secure: side-channel and secret-in-memory primitives for {cpp} += secure: constant time and secrets in memory for {cpp} :toc: left :toclevels: 2 :idprefix: From 2db88726fb44c2b055845614f3de07ec08aa065f Mon Sep 17 00:00:00 2001 From: R Date: Thu, 3 Dec 2026 15:48:02 -0300 Subject: [PATCH 2/2] secure: constant_time_equal accumulates in a register; value_barrier is the asm statement on WebAssembly too crypto's constant-time lane on wasip2 failed on webcpp's main (run 47116123227): constant_time_equal read |t| 89.80, 104.33 and 119.54. A probe on sixteen hosted runners (2026-12-03) built the case as crypto's dudect does, under wasmtime 50.0.1 and natively, in variants. On all four AMD EPYC 9V74 runners and under wasmtime alone, three runs each, it read |t| 70 to 117: - zeros against random bytes (the case): over 10 on all four; - the same with the guard removed: the same; - a pair that differs only in its last byte against a nonzero pair: the same; - an equal random pair against one that differs in its last byte, whose result differs while its values look alike: 1 to 3; - the comparison with its accumulator in a register through an empty asm: 1 to 8. Natively, and on every other runner model (Xeon 6975P-C, 6973P-C, Platinum 8573C, EPYC 7763 and 9V45), every variant read 0 to 4. What leaked was the comparison's volatile accumulator. On wasm it is a store and a load of linear memory at each byte, which wasmtime compiles to the host's, and on that CPU the round trip of a nonzero value took longer than that of zero. The accumulator is now a register, passed through constant_time::value_barrier at each step, so the compiler still cannot know it has settled. On WebAssembly value_barrier is now the empty asm statement it is on GCC and clang: WebAssembly has the statement too (wasi-sdk 34 and Emscripten compile it at every width, as a local). Before, it fell back to the volatile round trip, which MSVC and MemorySanitizer keep. The doc comments and the blocks and guard pages say so. Confirmed on hosted runners, sixteen jobs, five runs of each case: with this change the case read |t| 0.9 to 2.9 under wasmtime on all five AMD EPYC 9V74 runners (25 runs), and 0.3 to 3.7 everywhere else, natively and under wasmtime. The old volatile accumulator, kept in the same probe as a control, still read 52 to 91 on those 9V74 runners under wasmtime. --- doc/blocks.adoc | 10 ++++++---- doc/guard.adoc | 2 +- include/webcpp/secure/constant_time.hpp | 15 +++++++++------ include/webcpp/secure/constant_time_equal.hpp | 19 ++++++++++++------- include/webcpp/secure/timing_guard.hpp | 2 +- 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/doc/blocks.adoc b/doc/blocks.adoc index ee369a9..a84b1f0 100644 --- a/doc/blocks.adoc +++ b/doc/blocks.adoc @@ -82,10 +82,12 @@ table's end selects no entry, whatever the index's width: even `SIZE_MAX` and a *What `value_barrier` is.* It returns its argument, hidden from the optimiser, so that it cannot reason about which values a mask may take and turn the arithmetic that uses it back into a branch. On GCC and clang it is an empty -`asm volatile` statement with the value in a register; the `volatile` keeps the -compiler from merging, hoisting or dropping the statement. On MSVC and on -WebAssembly, which have no such statement, it is a round trip through a -`volatile` local. In a constant expression it is the identity. It holds the +`asm volatile` statement with the value in a register, WebAssembly included, +where the register is a local; the `volatile` keeps the compiler from merging, +hoisting or dropping the statement. On MSVC, which has no such statement, it is +a round trip through a `volatile` local. Not on WebAssembly: there the round +trip is a store and a load of linear memory, and under wasmtime an AMD EPYC 9V74 +took longer for a round trip of a nonzero value than of zero. In a constant expression it is the identity. It holds the value in this one place: it does not stop the compiler from transforming the code around it. diff --git a/doc/guard.adoc b/doc/guard.adoc index 9590227..993799d 100644 --- a/doc/guard.adoc +++ b/doc/guard.adoc @@ -179,7 +179,7 @@ secret safe. Code that branches on a secret leaks through the branch whatever DIT says. * *Not arithmetic on registers alone.* The writes of the register carry a `memory` clobber, which orders them against memory accesses and calls (the -backends' out-of-line C calls, a volatile accumulator), not against arithmetic +backends' out-of-line C calls, the bytes a comparison reads), not against arithmetic on registers, which the compiler may move across them: the same limit as every program that sets DIT. * *Not where it is not covered*, per the table, and not on a CPU without diff --git a/include/webcpp/secure/constant_time.hpp b/include/webcpp/secure/constant_time.hpp index 1e13dda..e32c3ce 100644 --- a/include/webcpp/secure/constant_time.hpp +++ b/include/webcpp/secure/constant_time.hpp @@ -105,11 +105,14 @@ namespace constant_time { * a mask may take and turn the arithmetic that uses it back into a branch. * * On GCC and clang it is an empty `asm volatile` statement with the value in a register - * (BoringSSL's `value_barrier_w`); the `volatile` keeps the compiler from merging, hoisting or - * dropping the statement, which has an output and would otherwise be an ordinary expression; on - * MSVC and on WebAssembly, which have no such statement, and under MemorySanitizer, which would - * report the statement's input when it is a secret marked uninitialised, a round trip through a - * `volatile` local. + * (BoringSSL's `value_barrier_w`), WebAssembly included, where the register is a local; the + * `volatile` keeps the compiler from merging, hoisting or dropping the statement, which has an + * output and would otherwise be an ordinary expression. On MSVC, which has no such statement, + * and under MemorySanitizer, which would report the statement's input when it is a secret marked + * uninitialised, it is a round trip through a `volatile` local. Not on WebAssembly: there the + * round trip is a store and a load of linear memory, which wasmtime compiles to the host's, and + * an AMD EPYC 9V74 took longer for a round trip of a nonzero value than of zero, under wasmtime + * and not natively (constant_time_equal's dudect case on wasip2, 2026-12-03). * In a constant expression it is the identity. The barrier holds the value in this one place: it * does not stop the compiler from transforming the code around it. * @@ -122,7 +125,7 @@ template if (std::is_constant_evaluated()) { return word_value; } -#if (defined(__GNUC__) || defined(__clang__)) && !defined(__wasm__) +#if defined(__GNUC__) || defined(__clang__) if constexpr (detail::memory_sanitizer) { return detail::volatile_round_trip(word_value); } else { diff --git a/include/webcpp/secure/constant_time_equal.hpp b/include/webcpp/secure/constant_time_equal.hpp index bce3d94..07ffbf6 100644 --- a/include/webcpp/secure/constant_time_equal.hpp +++ b/include/webcpp/secure/constant_time_equal.hpp @@ -15,6 +15,7 @@ #include +#include #include #include @@ -106,10 +107,14 @@ template * Whether two byte sequences are equal, in a time that does not depend on their contents. * * Every byte is read, and no branch or index depends on one: the XOR of each pair is - * accumulated with OR in a volatile local, read once after the loop, and reduced to one bit - * without a branch, as BearSSL's tag check does. Sizes are public: sequences of different sizes - * compare unequal without reading their contents. On 64-bit ARM the comparison runs with DIT - * set where the CPU has it, and leaves the thread's DIT as it found it. + * accumulated with OR, each step through `constant_time::value_barrier`, so that the compiler + * cannot know the accumulation settled and stop early, and reduced to one bit without a branch, + * as BearSSL's tag check does. The accumulation stays in a register where the barrier is an + * `asm` statement (GCC and clang, WebAssembly included): a `volatile` local, a store and a load + * of memory at each byte, took longer under wasmtime on an AMD EPYC 9V74 for a nonzero + * difference than for none (|t| 70 to 117 against 1 to 4 elsewhere, 2026-12-03). Sizes are public: + * sequences of different sizes compare unequal without reading their contents. On 64-bit ARM the + * comparison runs with DIT set where the CPU has it, and leaves the thread's DIT as it found it. * * @tparam First The first operand's type. * @tparam Second The second operand's type. @@ -131,12 +136,12 @@ template if (first_view.count != second_view.count) { return false; } - unsigned volatile accumulated_difference = 0; + unsigned accumulated_difference = 0; for (std::size_t byte_index = 0; byte_index < first_view.count; ++byte_index) { auto const first_byte = static_cast(first_view.first[byte_index]); auto const second_byte = static_cast(second_view.first[byte_index]); - accumulated_difference = - accumulated_difference | static_cast(first_byte ^ second_byte); + accumulated_difference = constant_time::value_barrier( + accumulated_difference | static_cast(first_byte ^ second_byte)); } unsigned const total_difference = accumulated_difference; // total_difference is at most 255: minus 1 wraps to the type's maximum only when it is 0. diff --git a/include/webcpp/secure/timing_guard.hpp b/include/webcpp/secure/timing_guard.hpp index d02ef07..f27054e 100644 --- a/include/webcpp/secure/timing_guard.hpp +++ b/include/webcpp/secure/timing_guard.hpp @@ -215,7 +215,7 @@ inline bool probe_dit() noexcept { * runs as well when an exception ends the scope, with DIT as it was in the scope (Windows clears * it only on resuming after the catch, outside the guard). The writes' `memory` clobber orders * them against memory accesses and calls - * (the backends' out-of-line C calls, a volatile accumulator), not against arithmetic on + * (the backends' out-of-line C calls, the bytes a comparison reads), not against arithmetic on * registers alone, which the compiler may move across them: the same limit as every program that * sets DIT. */