Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 6 additions & 4 deletions doc/blocks.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion doc/guard.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc/secure.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 9 additions & 6 deletions include/webcpp/secure/constant_time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -122,7 +125,7 @@ template <detail::mask_word U>
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 {
Expand Down
19 changes: 12 additions & 7 deletions include/webcpp/secure/constant_time_equal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <webcpp/secure/config.hpp>

#include <webcpp/secure/constant_time.hpp>
#include <webcpp/secure/timing_guard.hpp>

#include <concepts>
Expand Down Expand Up @@ -106,10 +107,14 @@ template <class Held>
* 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.
Expand All @@ -131,12 +136,12 @@ template <class First, class Second>
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<std::uint8_t>(first_view.first[byte_index]);
auto const second_byte = static_cast<std::uint8_t>(second_view.first[byte_index]);
accumulated_difference =
accumulated_difference | static_cast<unsigned>(first_byte ^ second_byte);
accumulated_difference = constant_time::value_barrier(
accumulated_difference | static_cast<unsigned>(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.
Expand Down
2 changes: 1 addition & 1 deletion include/webcpp/secure/timing_guard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Loading