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
40 changes: 29 additions & 11 deletions include/webcpp/secure/constant_time_equal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <ranges>
#include <span>
#include <type_traits>
Expand Down Expand Up @@ -106,13 +107,16 @@ 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, 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:
* Every byte is read, and no branch or index depends on one: the sequences are read eight bytes at
* a time as 64-bit words (the bytes past the last whole word one at a time), the XOR of each pair
* is accumulated with OR, each step through `constant_time::value_barrier`, so that the compiler
* cannot know the accumulation settled and stop early, and the accumulation is folded 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). Under wasmtime,
* on hosted runners' AMD EPYC 9V74, the time depended on the bytes compared (dudect's case of 16
* bytes): with a `volatile` accumulator, a store and a load of memory at each byte, |t| 70 to 117
* against 1 to 4 elsewhere (2026-12-03); with a register accumulator read byte by byte, 2 to 10;
* read by words, 1 to 4 (2026-12-04). Natively every form read below 4. 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.
*
Expand All @@ -136,14 +140,28 @@ template <class First, class Second>
if (first_view.count != second_view.count) {
return false;
}
unsigned accumulated_difference = 0;
for (std::size_t byte_index = 0; byte_index < first_view.count; ++byte_index) {
constexpr std::size_t word_size = sizeof(std::uint64_t);
std::uint64_t accumulated_difference = 0;
std::size_t byte_index = 0;
for (; first_view.count - byte_index >= word_size; byte_index += word_size) {
std::uint64_t first_word = 0;
std::uint64_t second_word = 0;
std::memcpy(&first_word, first_view.first + byte_index, word_size);
std::memcpy(&second_word, second_view.first + byte_index, word_size);
accumulated_difference =
constant_time::value_barrier(accumulated_difference | (first_word ^ second_word));
}
for (; 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 = constant_time::value_barrier(
accumulated_difference | static_cast<unsigned>(first_byte ^ second_byte));
accumulated_difference | static_cast<std::uint64_t>(first_byte ^ second_byte));
}
unsigned const total_difference = accumulated_difference;
// Every byte's difference folded into the low byte: nonzero exactly when one of them is.
accumulated_difference |= accumulated_difference >> 32U;
accumulated_difference |= accumulated_difference >> 16U;
accumulated_difference |= accumulated_difference >> 8U;
auto const total_difference = static_cast<unsigned>(accumulated_difference & 0xFFU);
// total_difference is at most 255: minus 1 wraps to the type's maximum only when it is 0.
return (((total_difference - 1U) >> 8U) & 1U) != 0;
}
Expand Down
48 changes: 45 additions & 3 deletions test/constant_time_equal_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
// https://www.boost.org/LICENSE_1_0.txt)

// constant_time_equal compares any two byte sequences (spans, arrays of bytes, secrets, string
// views, Boost.Hash2 digests), tells them apart by any bit, calls sequences of different sizes
// unequal without reading them, and enters the guard of <webcpp/secure/timing_guard.hpp> once
// per call, from its own header.
// views, Boost.Hash2 digests), tells them apart by any bit at every length and alignment, calls
// sequences of different sizes unequal without reading them, and enters the guard of
// <webcpp/secure/timing_guard.hpp> once per call, from its own header.

#include "support/timing_seam.hpp"

Expand Down Expand Up @@ -193,6 +193,47 @@ void sizes() {
BOOST_TEST(!secure::constant_time_equal(sixteen, fifteen));
}

/**
* Checks `length` bytes at `offset` of a 48-byte buffer against a copy, then against each one-bit
* flip of every byte, then with the bytes just outside the range changed.
*/
void check_range(std::size_t length, std::size_t offset) {
std::array<std::uint8_t, 48> first_buffer{};
for (std::size_t i = 0; i < first_buffer.size(); ++i) {
first_buffer[i] = static_cast<std::uint8_t>((i * 37U) + 11U);
}
std::array<std::uint8_t, 48> second_buffer = first_buffer;
std::span<std::uint8_t const> const first(first_buffer.data() + offset, length);
std::span<std::uint8_t> const second(second_buffer.data() + offset, length);
BOOST_TEST(secure::constant_time_equal(first, second));
for (std::size_t byte = 0; byte < length; ++byte) {
for (unsigned bit = 0; bit < 8; ++bit) {
second[byte] ^= static_cast<std::uint8_t>(1U << bit);
BOOST_TEST(!secure::constant_time_equal(first, second));
second[byte] ^= static_cast<std::uint8_t>(1U << bit);
}
}
// Bytes outside the compared range are not read as part of it.
second_buffer[offset + length] ^= 0xFFU;
if (offset > 0) {
second_buffer[offset - 1] ^= 0xFFU;
}
BOOST_TEST(secure::constant_time_equal(first, second));
}

/**
* Every length from 0 to 40 at every offset from 0 to 7: the comparison reads whole 64-bit words
* first and the bytes past the last one after, and folds the words' differences into one byte, so
* that a flip in any byte of a word or of the tail, at any alignment, is told apart.
*/
void lengths_and_offsets() {
for (std::size_t length = 0; length <= 40; ++length) {
for (std::size_t offset = 0; offset < 8; ++offset) {
check_range(length, offset);
}
}
}

void guard_through_the_seam() {
std::array<std::uint8_t, 32> const bytes = base_bytes();
seam::timing_entries = 0;
Expand Down Expand Up @@ -254,6 +295,7 @@ int main() {
mixed_pairs();
bytes_by_value();
sizes();
lengths_and_offsets();
guard_through_the_seam();
return webcpp::test::exit_status(boost::report_errors());
}
Loading