From a8f68cb6a981336869ee1129b316e30024485b7f Mon Sep 17 00:00:00 2001 From: R Date: Fri, 4 Dec 2026 04:45:33 -0300 Subject: [PATCH] secure: constant_time_equal reads 64-bit words, its accumulator a register crypto's constant-time lane on wasip2 failed on crypto#24 (run 47265884663): constant_time_equal read |t| 115.04, 120.80 and 146.54, on secure 44b1557, which webcpp's main still pins. Probes on hosted runners (2026-12-04, wasmtime 50.0.1) placed it on the AMD EPYC 9V74 alone, under wasmtime alone, and in the comparison's own reads, not in the harness: on six 9V74 runners the case read |t| 30 to 150 whatever the layout (the CI's offsets, 64-byte aligned, a cache line split at 1 or 8 bytes), for zeros or a nonzero pattern against random bytes, for a pair that differs only in its last or first byte against random bytes, and with the verdict discarded; its A/A control and a run that writes the classes' bytes without comparing them read 1 to 3. Natively, and on the Xeon 6975P-C, Platinum 8370C and 8573C, EPYC 7763 and 9V45, every variant read below 4. secure d7bbab4 (the accumulator a register through value_barrier, byte by byte) brought the 9V74 down to 5.5 to 10.0 in the probe and 2.4 to 9.9 in dudect_bearssl's case: below the threshold, not by much. Reading 64-bit words instead, the accumulator a register through value_barrier at each word, read 1.4 to 4.2 on three 9V74 runners (nine runs, its A/A control 1.2 to 2.9), and dudect_bearssl's case on this commit read 0.8 to 4.0 in eighteen runs on three 9V74 runners. The comparison now reads eight bytes of each sequence at a time through memcpy, at any alignment, ORs their XOR into a 64-bit register through constant_time::value_barrier, then the bytes past the last whole word one at a time, and folds the 64 bits into one byte before the branchless reduction. A test compares every length from 0 to 40 at every offset from 0 to 7 against each one-bit flip of every byte. --- include/webcpp/secure/constant_time_equal.hpp | 40 +++++++++++----- test/constant_time_equal_test.cpp | 48 +++++++++++++++++-- 2 files changed, 74 insertions(+), 14 deletions(-) diff --git a/include/webcpp/secure/constant_time_equal.hpp b/include/webcpp/secure/constant_time_equal.hpp index 07ffbf6..7079b87 100644 --- a/include/webcpp/secure/constant_time_equal.hpp +++ b/include/webcpp/secure/constant_time_equal.hpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -106,13 +107,16 @@ 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, 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. * @@ -136,14 +140,28 @@ template 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(first_view.first[byte_index]); auto const second_byte = static_cast(second_view.first[byte_index]); accumulated_difference = constant_time::value_barrier( - accumulated_difference | static_cast(first_byte ^ second_byte)); + accumulated_difference | static_cast(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(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; } diff --git a/test/constant_time_equal_test.cpp b/test/constant_time_equal_test.cpp index 1457f94..c9b2ce8 100644 --- a/test/constant_time_equal_test.cpp +++ b/test/constant_time_equal_test.cpp @@ -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 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 +// once per call, from its own header. #include "support/timing_seam.hpp" @@ -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 first_buffer{}; + for (std::size_t i = 0; i < first_buffer.size(); ++i) { + first_buffer[i] = static_cast((i * 37U) + 11U); + } + std::array second_buffer = first_buffer; + std::span const first(first_buffer.data() + offset, length); + std::span 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(1U << bit); + BOOST_TEST(!secure::constant_time_equal(first, second)); + second[byte] ^= static_cast(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 const bytes = base_bytes(); seam::timing_entries = 0; @@ -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()); }