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/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: 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. */