From 355b03e4d77dec39c2166f1e36837bc6ff12f1f7 Mon Sep 17 00:00:00 2001 From: R Date: Fri, 20 Nov 2026 19:51:22 -0300 Subject: [PATCH 01/22] crypto: detail::timing_guard sets arm64's DIT for its scope and restores the thread's state, with a seam a test counts --- include/webcpp/crypto/ciphers/aead.hpp | 4 + include/webcpp/crypto/detail/timing.hpp | 164 ++++++++++++++++++++++++ include/webcpp/crypto/equal.hpp | 7 +- test/Jamfile | 13 +- test/support/timing_seam.hpp | 54 ++++++++ test/timing_mock_test.cpp | 124 ++++++++++++++++++ test/timing_test.cpp | 149 +++++++++++++++++++++ 7 files changed, 512 insertions(+), 3 deletions(-) create mode 100644 include/webcpp/crypto/detail/timing.hpp create mode 100644 test/support/timing_seam.hpp create mode 100644 test/timing_mock_test.cpp create mode 100644 test/timing_test.cpp diff --git a/include/webcpp/crypto/ciphers/aead.hpp b/include/webcpp/crypto/ciphers/aead.hpp index 942f7fe9..b1293f08 100644 --- a/include/webcpp/crypto/ciphers/aead.hpp +++ b/include/webcpp/crypto/ciphers/aead.hpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -314,6 +315,7 @@ class aead { */ [[nodiscard]] static boost::system::result make( std::span key_bytes) noexcept { + detail::timing_guard const timing; if (key_bytes.size() != key_size) { return error::invalid_key_size; } @@ -389,6 +391,7 @@ class aead { [[nodiscard]] boost::system::result seal( std::span iv_bytes, std::span additional_data_bytes, std::span data_bytes) noexcept { + detail::timing_guard const timing; auto const guard_result = guard_message(iv_bytes, additional_data_bytes, data_bytes.size(), tag_size); if (!guard_result) { @@ -423,6 +426,7 @@ class aead { [[nodiscard]] boost::system::result open( std::span iv_bytes, std::span additional_data_bytes, std::span data_bytes, std::span tag_bytes) noexcept { + detail::timing_guard const timing; auto const guard_result = guard_message(iv_bytes, additional_data_bytes, data_bytes.size(), tag_bytes.size()); if (!guard_result) { diff --git a/include/webcpp/crypto/detail/timing.hpp b/include/webcpp/crypto/detail/timing.hpp new file mode 100644 index 00000000..a2b28287 --- /dev/null +++ b/include/webcpp/crypto/detail/timing.hpp @@ -0,0 +1,164 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +#ifndef WEBCPP_CRYPTO_DETAIL_TIMING_HPP +#define WEBCPP_CRYPTO_DETAIL_TIMING_HPP + +#include + +/* + * timing_guard sets 64-bit ARM's DIT (Data Independent Timing) bit, PSTATE.DIT, for the scope of + * one public operation and restores the thread's state after it. With DIT set, the instructions + * Arm lists take a time independent of their data; Apple requires it of constant-time code on + * Apple silicon, and none of the C libraries crypto calls sets it. Every public function that + * runs a backend's cryptographic code, and equal, holds one as its first statement. + * + * The guard applies on arm64 under macOS, iOS, Linux and Android, where the system says whether + * the CPU has FEAT_DIT. Everywhere else (x86, WebAssembly, where the engine decides, and Windows + * on ARM64, which documents no way to ask) it is an empty class that executes no instruction. + */ +#if defined(__aarch64__) && (defined(__APPLE__) || defined(__linux__)) +/** Defined where timing_guard sets DIT: arm64 under an Apple system or Linux, Android included. */ +#define WEBCPP_CRYPTO_DETAIL_TIMING_COVERED 1 +#include +#include +#ifdef __APPLE__ +#include +#else +#include +#endif +#endif + +/* + * WEBCPP_CRYPTO_DETAIL_TIMING_ENTERED() runs first in timing_guard's constructor, on every target. + * Nothing in a program's build; a test defines it before its first include to count the guards a + * call enters (test/support/timing_seam.hpp), so that a public function that forgets its guard + * fails on every lane, also where DIT does not apply. + */ +#ifndef WEBCPP_CRYPTO_DETAIL_TIMING_ENTERED +/** Marks the entry into a timing_guard: nothing in a program's build. */ +#define WEBCPP_CRYPTO_DETAIL_TIMING_ENTERED() static_cast(0) +#endif + +namespace webcpp::crypto::detail { + +#ifdef WEBCPP_CRYPTO_DETAIL_TIMING_COVERED +/** DIT's bit in the value of the register s3_3_c4_c2_5, bit 24 as in PSTATE. */ +inline constexpr std::uint64_t dit_bit = std::uint64_t{1} << 24U; + +/** + * Writes the DIT register, then waits with `isb` until the write takes effect. + * + * The register is named by its encoding (op0 3, op1 3, CRn 4, CRm 2, op2 5), which every + * assembler takes, with or without an architecture flag. The `isb` is the barrier Apple asks for + * after the write, so that the instructions after it run under the new state. + * + * @param value `dit_bit` to set DIT, 0 to clear it. + */ +inline void write_dit(std::uint64_t value) noexcept { + __asm__ volatile("msr s3_3_c4_c2_5, %0\n\tisb" : : "r"(value) : "memory"); +} +#endif + +/** + * Whether the CPU has DIT, read once per process. + * + * `hw.optional.arm.FEAT_DIT` on Apple's systems, where a value that cannot be read counts as + * absent; `HWCAP_DIT` (bit 24 of `AT_HWCAP`) on Linux. Executing DIT's instructions on a CPU + * without it traps, so nothing reads or writes the register unless this is true. + * + * @return Whether the CPU has FEAT_DIT; `false` on every target the guard does not cover. + */ +[[nodiscard]] inline bool cpu_has_dit() noexcept { +#if defined(WEBCPP_CRYPTO_DETAIL_TIMING_COVERED) && defined(__APPLE__) + static bool const has_dit = [] { + int feature = 0; + std::size_t size = sizeof feature; + return sysctlbyname("hw.optional.arm.FEAT_DIT", &feature, &size, nullptr, 0) == 0 && + feature != 0; + }(); + return has_dit; +#elif defined(WEBCPP_CRYPTO_DETAIL_TIMING_COVERED) + constexpr unsigned long hwcap_dit = 1UL << 24U; // HWCAP_DIT, asm/hwcap.h + static bool const has_dit = (getauxval(AT_HWCAP) & hwcap_dit) != 0; + return has_dit; +#else + return false; +#endif +} + +/** + * Whether DIT is set for the calling thread. + * + * @return PSTATE.DIT, read with `mrs`, where the CPU has DIT on a target the guard covers; + * `false` elsewhere, without executing `mrs`. + */ +[[nodiscard]] inline bool dit_is_set() noexcept { +#ifdef WEBCPP_CRYPTO_DETAIL_TIMING_COVERED + if (!cpu_has_dit()) { + return false; + } + std::uint64_t state = 0; + __asm__ volatile("mrs %0, s3_3_c4_c2_5" : "=r"(state) : : "memory"); + return (state & dit_bit) != 0; +#else + return false; +#endif +} + +/** + * Sets DIT for its scope where the CPU has it, and leaves the thread's DIT as it found it. + * + * Construction finds DIT clear, sets it and remembers to clear it; the destructor clears it then + * only. A guard that finds DIT set does nothing and leaves it set: the program set it itself (as + * Apple's `timingsafe_enable_if_supported` does), or an outer guard did, so that guards nest and + * a thread whose program asked for DIT keeps it after every call. DIT is a thread's own state, + * so a guard never changes another thread's. + */ +class timing_guard { +public: + /** Sets DIT where the CPU has it and it is clear. */ + timing_guard() noexcept { + WEBCPP_CRYPTO_DETAIL_TIMING_ENTERED(); +#ifdef WEBCPP_CRYPTO_DETAIL_TIMING_COVERED + if (cpu_has_dit() && !dit_is_set()) { + write_dit(dit_bit); + armed_ = true; + } +#endif + } + + /** Clears DIT when this guard set it. */ + ~timing_guard() { +#ifdef WEBCPP_CRYPTO_DETAIL_TIMING_COVERED + if (armed_) { + write_dit(0); + } +#endif + } + + /** Not copyable: the state it restores is the scope's. */ + timing_guard(timing_guard const&) = delete; + + /** Not assignable. */ + timing_guard& operator=(timing_guard const&) = delete; + + /** Not movable: the state it restores is the scope's. */ + timing_guard(timing_guard&&) = delete; + + /** Not move-assignable. */ + timing_guard& operator=(timing_guard&&) = delete; + +private: +#ifdef WEBCPP_CRYPTO_DETAIL_TIMING_COVERED + /** Whether this guard set DIT, and so clears it. */ + bool armed_ = false; +#endif +}; + +} // namespace webcpp::crypto::detail + +#endif // WEBCPP_CRYPTO_DETAIL_TIMING_HPP diff --git a/include/webcpp/crypto/equal.hpp b/include/webcpp/crypto/equal.hpp index 1741cac8..75a689c0 100644 --- a/include/webcpp/crypto/equal.hpp +++ b/include/webcpp/crypto/equal.hpp @@ -12,6 +12,8 @@ #include +#include + #include #include @@ -26,7 +28,8 @@ namespace webcpp::crypto { * 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: spans of different sizes - * compare unequal without reading their contents. + * 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. * * @param first_operand The first sequence, a tag or a MAC. * @param second_operand The second. @@ -34,6 +37,7 @@ namespace webcpp::crypto { */ [[nodiscard]] inline bool equal(std::span first_operand, std::span second_operand) noexcept { + detail::timing_guard const timing; if (first_operand.size() != second_operand.size()) { return false; } @@ -62,6 +66,7 @@ namespace webcpp::crypto { template [[nodiscard]] bool equal(boost::hash2::digest const& first_operand, boost::hash2::digest const& second_operand) noexcept { + detail::timing_guard const timing; return equal(std::span(first_operand.data(), N), std::span(second_operand.data(), N)); } diff --git a/test/Jamfile b/test/Jamfile index 87d8eba2..6b25d1c7 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -33,8 +33,8 @@ webcpp.headers-alone crypto : ../include : crypto.hpp crypto/config.hpp crypto/detail/engines.hpp crypto/detail/entropy.hpp crypto/detail/limits.hpp crypto/detail/windows.hpp crypto/detail/wipe.hpp crypto/secret.hpp crypto/supports.hpp crypto/detail/capabilities.hpp crypto/detail/defaults.hpp - crypto/detail/heavy.hpp crypto/ciphers/aead.hpp crypto/ciphers/chacha20_poly1305.hpp - crypto/ciphers/secretbox.hpp crypto/pem.hpp ; + crypto/detail/heavy.hpp crypto/detail/timing.hpp crypto/ciphers/aead.hpp + crypto/ciphers/chacha20_poly1305.hpp crypto/ciphers/secretbox.hpp crypto/pem.hpp ; # The backend BearSSL's header and the default backend's, with BearSSL linked, # on every target. @@ -104,6 +104,15 @@ webcpp.compile-diagnostic heavy_refusal_forgotten : heavy_refusal_forgotten_test crypto-run error : error_test.cpp ; crypto-run equal : equal_test.cpp ; +# detail::timing_guard sets DIT for its scope where the CPU has it and leaves +# the thread's state as it found it; its seam fires on every target; equal +# enters it. timing_threads adds a second thread, natively, with threads. +crypto-run timing : timing_test.cpp ; +crypto-run timing_threads : timing_test.cpp + : multi WEBCPP_TEST_TIMING_THREADS : native ; +# A public operation on a stand-in backend runs it with DIT set, and a call +# refused before the backend still enters the guard and restores the state. +crypto-run timing_mock : timing_mock_test.cpp ; # alone, with no backend linked, compiles, links and runs. crypto-run convenience_header : convenience_header_test.cpp ; crypto-run wipe : wipe_test.cpp ; diff --git a/test/support/timing_seam.hpp b/test/support/timing_seam.hpp new file mode 100644 index 00000000..7d351579 --- /dev/null +++ b/test/support/timing_seam.hpp @@ -0,0 +1,54 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// The seam of detail::timing_guard, counted: a test includes this header before any crypto header, +// so that every guard a call enters raises timing_entries, on every target, also where DIT does +// not apply. + +#ifndef WEBCPP_TEST_CRYPTO_SUPPORT_TIMING_SEAM_HPP +#define WEBCPP_TEST_CRYPTO_SUPPORT_TIMING_SEAM_HPP + +#ifdef WEBCPP_CRYPTO_DETAIL_TIMING_HPP +#error "support/timing_seam.hpp must be included before any crypto header" +#endif + +#include + +#include +#include +#include + +namespace webcpp::test { + +/** The number of detail::timing_guard objects this thread has constructed. */ +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): the seam's counter +inline thread_local std::size_t timing_entries = 0; + +} // namespace webcpp::test + +/** Counts the entry into a detail::timing_guard in webcpp::test::timing_entries. */ +#define WEBCPP_CRYPTO_DETAIL_TIMING_ENTERED() (static_cast(++::webcpp::test::timing_entries)) + +namespace webcpp::test { + +/** + * Runs `call` and reports an error when it entered no detail::timing_guard. + * @param name What the call is, as the error names it. + * @param call The call, with its arguments bound. + */ +template +void require_enters(char const* name, Call&& call) { + std::size_t const before = timing_entries; + std::forward(call)(); + if (timing_entries == before) { + std::string const message = std::string(name) + " did not enter detail::timing_guard"; + BOOST_ERROR(message.c_str()); + } +} + +} // namespace webcpp::test + +#endif // WEBCPP_TEST_CRYPTO_SUPPORT_TIMING_SEAM_HPP diff --git a/test/timing_mock_test.cpp b/test/timing_mock_test.cpp new file mode 100644 index 00000000..59367d02 --- /dev/null +++ b/test/timing_mock_test.cpp @@ -0,0 +1,124 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// detail::timing_guard around a public operation, on a stand-in backend that records whether DIT +// was set when it ran: the backend runs with DIT set where the CPU has it, and the thread finds +// DIT clear again after the call; a call refused before the backend is reached (a key of the +// wrong size) still enters the guard and leaves DIT as it found it. + +#include "support/timing_seam.hpp" + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +namespace webcpp::test { +namespace { + +/** A backend tag for a generic layer's guard alone. */ +struct timing_stand_in {}; + +/** Whether DIT was set when the stand-in's seal last ran. */ +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): what the stand-in records +bool dit_during_seal = false; + +} // namespace +} // namespace webcpp::test + +namespace webcpp::crypto::detail { + +/** The stand-in's ChaCha20-Poly1305: seal records whether DIT is set; every call succeeds. */ +template <> +struct aead_engine { + /** Succeeds. */ + [[nodiscard]] static boost::system::result init( + aead_state& /*state*/, + std::span /*key_bytes*/) noexcept { + return {}; + } + + /** Records whether DIT is set, then succeeds. */ + [[nodiscard]] static boost::system::result seal( + aead_state& /*state*/, + std::span /*iv_bytes*/, + std::span /*additional_data_bytes*/, std::span /*data*/, + boost::hash2::digest<16>& /*tag*/) noexcept { + test::dit_during_seal = dit_is_set(); + return {}; + } + + /** Succeeds. */ + [[nodiscard]] static boost::system::result open( + aead_state& /*state*/, + std::span /*iv_bytes*/, + std::span /*additional_data_bytes*/, std::span /*data*/, + std::array const& /*tag*/) noexcept { + return {}; + } + + /** Nothing to move. */ + static void move( + aead_state& /*to*/, + aead_state& /*from*/) noexcept {} + + /** Nothing to wipe. */ + static void reset( + aead_state& /*state*/) noexcept {} +}; + +} // namespace webcpp::crypto::detail + +namespace { + +namespace crypto = webcpp::crypto; +using aead = + crypto::ciphers::aead; + +// From clear: the backend's seal runs with DIT set where the CPU has it, and DIT is clear after. +void backend_runs_with_dit() { + BOOST_TEST(!crypto::detail::dit_is_set()); + std::array const key{}; + auto made = aead::make(key); + if (!BOOST_TEST(made.has_value())) { + return; + } + BOOST_TEST(!crypto::detail::dit_is_set()); + std::array const iv{}; + std::array data{}; + webcpp::test::dit_during_seal = !crypto::detail::cpu_has_dit(); + webcpp::test::require_enters( + "aead::seal", [&made, &iv, &data] { BOOST_TEST(made->seal(iv, {}, data).has_value()); }); + BOOST_TEST_EQ(webcpp::test::dit_during_seal, crypto::detail::cpu_has_dit()); + BOOST_TEST(!crypto::detail::dit_is_set()); +} + +// A key of 31 bytes is refused before the backend is reached; the guard is entered all the same +// and DIT is clear after. +void early_error_restores_state() { + BOOST_TEST(!crypto::detail::dit_is_set()); + std::array const short_key{}; + webcpp::test::require_enters("aead::make with a 31-byte key", [&short_key] { + BOOST_TEST(aead::make(short_key).error() == crypto::error::invalid_key_size); + }); + BOOST_TEST(!crypto::detail::dit_is_set()); +} + +} // namespace + +int main() { + backend_runs_with_dit(); + early_error_restores_state(); + return webcpp::test::exit_status(boost::report_errors()); +} diff --git a/test/timing_test.cpp b/test/timing_test.cpp new file mode 100644 index 00000000..bc0c3e68 --- /dev/null +++ b/test/timing_test.cpp @@ -0,0 +1,149 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// detail::timing_guard, with no backend: its seam fires once per guard on every target; where +// the CPU has DIT, a guard sets it for its scope and clears it after when it was clear, leaves it +// set when it was set, and nests; equal enters a guard. Built with WEBCPP_TEST_TIMING_THREADS +// (timing_threads, native, with threads), a guard on one thread leaves another's DIT clear. + +#include "support/timing_seam.hpp" + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#ifdef WEBCPP_TEST_TIMING_THREADS +#include +#include +#endif + +namespace { + +namespace detail = webcpp::crypto::detail; + +/** + * Sets or clears DIT for this thread, as a program can, so that a test starts from a known state; + * only where the CPU has DIT, since the instruction traps on a CPU without it. + */ +void force_dit([[maybe_unused]] bool set) { +#ifdef WEBCPP_CRYPTO_DETAIL_TIMING_COVERED + if (!detail::cpu_has_dit()) { + return; + } + std::uint64_t const value = set ? detail::dit_bit : 0; + __asm__ volatile("msr s3_3_c4_c2_5, %0\n\tisb" : : "r"(value) : "memory"); +#endif +} + +// Constructing a guard raises the seam's count by exactly one, on every target. +void seam_fires() { + std::size_t const before = webcpp::test::timing_entries; + { + detail::timing_guard const guard; + BOOST_TEST_EQ(webcpp::test::timing_entries, before + 1); + } + BOOST_TEST_EQ(webcpp::test::timing_entries, before + 1); +} + +// From clear: set inside a guard, clear after it. Where the guard does not apply, never set. +void guard_sets_and_clears() { + force_dit(false); + BOOST_TEST(!detail::dit_is_set()); + { + detail::timing_guard const guard; + BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + } + BOOST_TEST(!detail::dit_is_set()); +} + +// From set, as a program sets it: still set after a guard. +void guard_leaves_set_bit() { + force_dit(true); + BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + { + detail::timing_guard const guard; + BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + } + BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + force_dit(false); + BOOST_TEST(!detail::dit_is_set()); +} + +// From clear: set inside both guards and after the inner one, clear after the outer one. +void nested_guards() { + force_dit(false); + { + detail::timing_guard const outer; + { + detail::timing_guard const inner; + BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + } + BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + } + BOOST_TEST(!detail::dit_is_set()); +} + +// Both overloads of equal enter a guard, and leave DIT as they found it. +void equal_enters() { + force_dit(false); + std::array const first{}; + std::array const second{}; + webcpp::test::require_enters("equal(span, span)", [&first, &second] { + BOOST_TEST(webcpp::crypto::equal(std::span(first), + std::span(second))); + }); + boost::hash2::digest<16> const first_digest{}; + boost::hash2::digest<16> const second_digest{}; + webcpp::test::require_enters("equal(digest, digest)", [&first_digest, &second_digest] { + BOOST_TEST(webcpp::crypto::equal(first_digest, second_digest)); + }); + BOOST_TEST(!detail::dit_is_set()); +} + +#ifdef WEBCPP_TEST_TIMING_THREADS +// While a guard holds DIT on this thread, another thread reads it clear. The other thread is +// started before the guard, so that it cannot inherit the state at its creation (Linux's clone +// copies PSTATE), and reads it only once the guard is entered. +void other_thread_untouched() { + force_dit(false); + std::promise guard_entered; + std::promise other_reading; + std::thread other([entered = guard_entered.get_future(), &other_reading]() mutable { + entered.wait(); + other_reading.set_value(detail::dit_is_set()); + }); + { + detail::timing_guard const guard; + guard_entered.set_value(); + BOOST_TEST(!other_reading.get_future().get()); + BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + } + other.join(); + BOOST_TEST(!detail::dit_is_set()); +} +#endif + +} // namespace + +int main() { + seam_fires(); + guard_sets_and_clears(); + guard_leaves_set_bit(); + nested_guards(); + equal_enters(); +#ifdef WEBCPP_TEST_TIMING_THREADS + other_thread_untouched(); +#endif + return webcpp::test::exit_status(boost::report_errors()); +} From 9af474ce74d90801494724de0c2d1daf90de9b83 Mon Sep 17 00:00:00 2001 From: R Date: Fri, 20 Nov 2026 19:53:49 -0300 Subject: [PATCH 02/22] crypto: the timing test writes DIT through detail::write_dit, and timing_guard says what its barrier orders --- include/webcpp/crypto/detail/timing.hpp | 5 ++++- test/timing_test.cpp | 3 +-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/webcpp/crypto/detail/timing.hpp b/include/webcpp/crypto/detail/timing.hpp index a2b28287..7259b198 100644 --- a/include/webcpp/crypto/detail/timing.hpp +++ b/include/webcpp/crypto/detail/timing.hpp @@ -116,7 +116,10 @@ inline void write_dit(std::uint64_t value) noexcept { * only. A guard that finds DIT set does nothing and leaves it set: the program set it itself (as * Apple's `timingsafe_enable_if_supported` does), or an outer guard did, so that guards nest and * a thread whose program asked for DIT keeps it after every call. DIT is a thread's own state, - * so a guard never changes another thread's. + * so a guard never changes another thread's. The writes' `memory` clobber orders them against + * memory accesses and calls (the backends' out-of-line C calls, equal's volatile accumulator), + * not against arithmetic on registers alone, which the compiler may move across them: the same + * limit as every program that sets DIT. */ class timing_guard { public: diff --git a/test/timing_test.cpp b/test/timing_test.cpp index bc0c3e68..5d925d14 100644 --- a/test/timing_test.cpp +++ b/test/timing_test.cpp @@ -41,8 +41,7 @@ void force_dit([[maybe_unused]] bool set) { if (!detail::cpu_has_dit()) { return; } - std::uint64_t const value = set ? detail::dit_bit : 0; - __asm__ volatile("msr s3_3_c4_c2_5, %0\n\tisb" : : "r"(value) : "memory"); + detail::write_dit(set ? detail::dit_bit : 0); #endif } From 54f442e74459502bf5b858e8c54cbc2c16dd5d32 Mon Sep 17 00:00:00 2001 From: R Date: Fri, 20 Nov 2026 20:06:11 -0300 Subject: [PATCH 03/22] crypto: the ciphers, the boxes and PEM set DIT around each operation on every backend --- include/webcpp/crypto/ciphers/aes.hpp | 6 + include/webcpp/crypto/ciphers/gcm.hpp | 9 + include/webcpp/crypto/hybrid/box.hpp | 4 + include/webcpp/crypto/hybrid/sealed_box.hpp | 3 + include/webcpp/crypto/pem.hpp | 5 + test/Jamfile | 11 + test/support/timing_coverage.hpp | 251 ++++++++++++++++++++ test/timing_coverage_bearssl_test.cpp | 44 ++++ test/timing_coverage_ctaes_test.cpp | 39 +++ test/timing_coverage_openssl_test.cpp | 48 ++++ test/timing_coverage_sodium_test.cpp | 202 ++++++++++++++++ 11 files changed, 622 insertions(+) create mode 100644 test/support/timing_coverage.hpp create mode 100644 test/timing_coverage_bearssl_test.cpp create mode 100644 test/timing_coverage_ctaes_test.cpp create mode 100644 test/timing_coverage_openssl_test.cpp create mode 100644 test/timing_coverage_sodium_test.cpp diff --git a/include/webcpp/crypto/ciphers/aes.hpp b/include/webcpp/crypto/ciphers/aes.hpp index 7553e99f..9d6b8ddb 100644 --- a/include/webcpp/crypto/ciphers/aes.hpp +++ b/include/webcpp/crypto/ciphers/aes.hpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -150,6 +151,7 @@ class basic_aes { */ [[nodiscard]] boost::system::result encrypt( std::span block_bytes) noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -168,6 +170,7 @@ class basic_aes { */ [[nodiscard]] boost::system::result decrypt( std::span block_bytes) noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -229,6 +232,7 @@ class aes128 : public detail::basic_aes<16, Backend> { */ [[nodiscard]] static boost::system::result make( std::span key_bytes) noexcept { + detail::timing_guard const timing; return detail::basic_aes<16, Backend>::template make_as(key_bytes); } @@ -254,6 +258,7 @@ class aes192 : public detail::basic_aes<24, Backend> { */ [[nodiscard]] static boost::system::result make( std::span key_bytes) noexcept { + detail::timing_guard const timing; return detail::basic_aes<24, Backend>::template make_as(key_bytes); } @@ -279,6 +284,7 @@ class aes256 : public detail::basic_aes<32, Backend> { */ [[nodiscard]] static boost::system::result make( std::span key_bytes) noexcept { + detail::timing_guard const timing; return detail::basic_aes<32, Backend>::template make_as(key_bytes); } diff --git a/include/webcpp/crypto/ciphers/gcm.hpp b/include/webcpp/crypto/ciphers/gcm.hpp index d7b613e8..be223699 100644 --- a/include/webcpp/crypto/ciphers/gcm.hpp +++ b/include/webcpp/crypto/ciphers/gcm.hpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -151,6 +152,7 @@ class gcm { */ [[nodiscard]] static boost::system::result make( std::span key_bytes) noexcept { + detail::timing_guard const timing; if (key_bytes.size() != key_size) { return error::invalid_key_size; } @@ -172,6 +174,7 @@ class gcm { * @param moved_source The source, empty afterwards. */ gcm(gcm&& moved_source) noexcept : keyed_(std::exchange(moved_source.keyed_, false)) { + detail::timing_guard const timing; if (keyed_) { moved_source.abandon_message(); engine::move(state_, moved_source.state_); @@ -187,6 +190,7 @@ class gcm { * @return This object. */ gcm& operator=(gcm&& moved_source) noexcept { + detail::timing_guard const timing; if (this != &moved_source) { engine::reset(state_); keyed_ = std::exchange(moved_source.keyed_, false); @@ -223,6 +227,7 @@ class gcm { [[nodiscard]] boost::system::result seal( std::span iv_bytes, std::span additional_data_bytes, std::span data_bytes) noexcept { + detail::timing_guard const timing; auto const guard_result = guard_message(iv_bytes, additional_data_bytes, data_bytes.size(), tag_size); if (!guard_result) { @@ -258,6 +263,7 @@ class gcm { [[nodiscard]] boost::system::result open( std::span iv_bytes, std::span additional_data_bytes, std::span data_bytes, std::span tag_bytes) noexcept { + detail::timing_guard const timing; auto const guard_result = guard_message(iv_bytes, additional_data_bytes, data_bytes.size(), tag_bytes.size()); if (!guard_result) { @@ -293,6 +299,7 @@ class gcm { [[nodiscard]] boost::system::result begin_seal( std::span iv_bytes, std::span additional_data_bytes) noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -322,6 +329,7 @@ class gcm { * pass `max_message_size`, which ends the message; `backend_failure`, which ends it. */ [[nodiscard]] boost::system::result update(std::span data_bytes) noexcept { + detail::timing_guard const timing; if (!keyed_ || !in_message_) { return error::invalid_state; } @@ -345,6 +353,7 @@ class gcm { * too. */ [[nodiscard]] boost::system::result result() noexcept { + detail::timing_guard const timing; if (!keyed_ || !in_message_) { return error::invalid_state; } diff --git a/include/webcpp/crypto/hybrid/box.hpp b/include/webcpp/crypto/hybrid/box.hpp index 7b16c5af..7a8dcbaa 100644 --- a/include/webcpp/crypto/hybrid/box.hpp +++ b/include/webcpp/crypto/hybrid/box.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -145,6 +146,7 @@ class basic_box { [[nodiscard]] static boost::system::result make( std::span secret_key, std::span peer_public_key) noexcept { + detail::timing_guard const timing; if (secret_key.size() != key_size) { return error::invalid_key_size; } @@ -216,6 +218,7 @@ class basic_box { */ [[nodiscard]] boost::system::result seal( std::span iv_bytes, std::span data_bytes) noexcept { + detail::timing_guard const timing; auto const guard_result = guard_message(iv_bytes, data_bytes.size(), tag_size); if (!guard_result) { return guard_result.error(); @@ -247,6 +250,7 @@ class basic_box { [[nodiscard]] boost::system::result open( std::span iv_bytes, std::span data_bytes, std::span tag_bytes) noexcept { + detail::timing_guard const timing; auto const guard_result = guard_message(iv_bytes, data_bytes.size(), tag_bytes.size()); if (!guard_result) { return guard_result.error(); diff --git a/include/webcpp/crypto/hybrid/sealed_box.hpp b/include/webcpp/crypto/hybrid/sealed_box.hpp index c0638f47..357eafc5 100644 --- a/include/webcpp/crypto/hybrid/sealed_box.hpp +++ b/include/webcpp/crypto/hybrid/sealed_box.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -87,6 +88,7 @@ struct basic_sealed_box { [[nodiscard]] static boost::system::result seal( std::span recipient_public_key, std::span message, std::span out) noexcept { + detail::timing_guard const timing; if (out.size() < overhead || out.size() - overhead != message.size()) { return error::invalid_input_size; } @@ -122,6 +124,7 @@ struct basic_sealed_box { std::span recipient_public_key, std::span recipient_secret_key, std::span sealed, std::span out) noexcept { + detail::timing_guard const timing; if (sealed.size() < overhead || out.size() != sealed.size() - overhead) { return error::invalid_input_size; } diff --git a/include/webcpp/crypto/pem.hpp b/include/webcpp/crypto/pem.hpp index 16b7a4c0..1f529283 100644 --- a/include/webcpp/crypto/pem.hpp +++ b/include/webcpp/crypto/pem.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -172,6 +173,7 @@ struct algorithm { */ [[nodiscard]] static boost::system::result armour( std::string_view label, std::span der, std::span out) noexcept { + detail::timing_guard const timing; if (!detail::pem_label_allowed(label)) { return error::invalid_encoding; } @@ -220,6 +222,7 @@ struct algorithm { */ [[nodiscard]] static boost::system::result unarmour( std::string_view label, std::string_view text, std::span out) noexcept { + detail::timing_guard const timing; if (!detail::pem_label_allowed(label)) { return error::invalid_encoding; } @@ -340,6 +343,7 @@ template > [[nodiscard]] boost::system::result armour(std::string_view label, std::span der, std::span out) noexcept { + detail::timing_guard const timing; return algorithm::armour(label, der, out); } @@ -356,6 +360,7 @@ template > [[nodiscard]] boost::system::result unarmour(std::string_view label, std::string_view text, std::span out) noexcept { + detail::timing_guard const timing; return algorithm::unarmour(label, text, out); } diff --git a/test/Jamfile b/test/Jamfile index 6b25d1c7..1ad010f9 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -113,6 +113,17 @@ crypto-run timing_threads : timing_test.cpp # A public operation on a stand-in backend runs it with DIT set, and a call # refused before the backend still enters the guard and restores the state. crypto-run timing_mock : timing_mock_test.cpp ; +# Every public function that runs a backend's code enters the guard and leaves +# the thread's DIT as it found it: one program per backend, wherever the +# backend is built (support/timing_coverage.hpp). +crypto-run timing_coverage_bearssl : timing_coverage_bearssl_test.cpp + : /third_party//bearssl ; +crypto-run timing_coverage_openssl : timing_coverage_openssl_test.cpp + : /webcpp/crypto//openssl : native wasip2 wasip3 ; +crypto-run timing_coverage_ctaes : timing_coverage_ctaes_test.cpp + : /third_party//ctaes ; +crypto-run timing_coverage_sodium : timing_coverage_sodium_test.cpp + : /third_party//sodium ; # alone, with no backend linked, compiles, links and runs. crypto-run convenience_header : convenience_header_test.cpp ; crypto-run wipe : wipe_test.cpp ; diff --git a/test/support/timing_coverage.hpp b/test/support/timing_coverage.hpp new file mode 100644 index 00000000..0c54425a --- /dev/null +++ b/test/support/timing_coverage.hpp @@ -0,0 +1,251 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// The coverage of detail::timing_guard by the families every backend shares: each public function +// of the AES block cipher, gcm and ciphers::aead, called once with valid input and once with +// input refused before the backend is reached, must enter a guard and leave the thread's DIT as +// it found it. Each backend's timing_coverage__test.cpp calls these on the algorithms +// it builds; a test includes support/timing_seam.hpp before any crypto header, then this one. + +#ifndef WEBCPP_TEST_CRYPTO_SUPPORT_TIMING_COVERAGE_HPP +#define WEBCPP_TEST_CRYPTO_SUPPORT_TIMING_COVERAGE_HPP + +#include "timing_seam.hpp" + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +namespace webcpp::test { + +/** + * Runs `call` and reports an error when it entered no detail::timing_guard, or when it left the + * thread's DIT other than it found it. + * + * @param name What the call is, `:: on `, as an error names it. + * @param call The call, with its arguments bound. + */ +template +void covered(std::string const& name, Call&& call) { + bool const before = webcpp::crypto::detail::dit_is_set(); + require_enters(name.c_str(), std::forward(call)); + if (webcpp::crypto::detail::dit_is_set() != before) { + std::string const message = name + " left DIT other than it found it"; + BOOST_ERROR(message.c_str()); + } +} + +/** A key of `Size` bytes whose bytes differ from one another. */ +template +std::array coverage_key() { + std::array key{}; + for (std::size_t i = 0; i < key.size(); ++i) { + key[i] = static_cast((i * 29) + 3); + } + return key; +} + +/** + * The AES block cipher's guarded functions: `make`, a key of the wrong size included, `encrypt` + * and `decrypt`, a partial block included. + * + * @tparam Cipher `aes128`, `aes192` or `aes256`. + * @param family The cipher's name, `aes128`, `aes192` or `aes256`. + * @param backend The backend's name. + */ +template +void cover_aes(std::string const& family, std::string const& backend) { + std::string const on = " on " + backend; + auto const key = coverage_key(); + covered(family + "::make" + on, [&key] { BOOST_TEST(Cipher::make(key).has_value()); }); + covered(family + "::make with a short key" + on, [&key] { + BOOST_TEST(Cipher::make(std::span(key).first(Cipher::key_size - 1)).error() == + webcpp::crypto::error::invalid_key_size); + }); + auto made = Cipher::make(key); + if (!BOOST_TEST(made.has_value())) { + return; + } + std::array data{}; + covered(family + "::encrypt" + on, + [&made, &data] { BOOST_TEST(made->encrypt(data).has_value()); }); + covered(family + "::decrypt" + on, + [&made, &data] { BOOST_TEST(made->decrypt(data).has_value()); }); + covered(family + "::encrypt of a partial block" + on, [&made, &data] { + BOOST_TEST(made->encrypt(std::span(data).first(1)).error() == + webcpp::crypto::error::invalid_input_size); + }); + covered(family + "::decrypt of a partial block" + on, [&made, &data] { + BOOST_TEST(made->decrypt(std::span(data).first(1)).error() == + webcpp::crypto::error::invalid_input_size); + }); +} + +/** + * gcm's guarded functions on a message: `seal` and `open`, an IV of no byte included, and the + * incremental `begin_seal`, `update` and `result`, each refused once too. + * + * @tparam Gcm `gcm>`, `gcm>` or `gcm>`. + * @param made A keyed object, with no message in progress, as it is left. + * @param family The algorithm's name, `aes128_gcm` or `aes256_gcm`. + * @param on ` on `, which ends each name. + */ +template +void cover_gcm_messages(Gcm& made, std::string const& family, std::string const& on) { + std::array const iv{}; + std::array const additional_data{1, 2, 3, 4}; + std::array data{}; + typename Gcm::result_type tag{}; + covered(family + "::seal" + on, [&] { + auto const sealed = made.seal(iv, additional_data, data); + if (BOOST_TEST(sealed.has_value())) { + tag = *sealed; + } + }); + covered(family + "::open" + on, [&] { + BOOST_TEST( + made.open(iv, additional_data, data, std::span(tag.data(), tag.size())).has_value()); + }); + covered(family + "::seal with an empty IV" + on, [&] { + BOOST_TEST(made.seal({}, additional_data, data).error() == + webcpp::crypto::error::invalid_iv_size); + }); + covered(family + "::open with an empty IV" + on, [&] { + BOOST_TEST( + made.open({}, additional_data, data, std::span(tag.data(), tag.size())).error() == + webcpp::crypto::error::invalid_iv_size); + }); + covered(family + "::begin_seal" + on, + [&] { BOOST_TEST(made.begin_seal(iv, additional_data).has_value()); }); + covered(family + "::update" + on, [&] { BOOST_TEST(made.update(data).has_value()); }); + covered(family + "::result" + on, [&] { BOOST_TEST(made.result().has_value()); }); + covered(family + "::begin_seal with an empty IV" + on, [&] { + BOOST_TEST(made.begin_seal({}, additional_data).error() == + webcpp::crypto::error::invalid_iv_size); + }); + covered(family + "::update without a message" + on, + [&] { BOOST_TEST(made.update(data).error() == webcpp::crypto::error::invalid_state); }); + covered(family + "::result without a message" + on, + [&] { BOOST_TEST(made.result().error() == webcpp::crypto::error::invalid_state); }); +} + +/** + * gcm's move constructor and move assignment, which run the backend (ruling R3), of a keyed + * object and of one in the middle of `begin_seal`. Each check does the move alone, so that no + * other guarded function raises the count. + * + * @tparam Gcm `gcm>`, `gcm>` or `gcm>`. + * @param family The algorithm's name, `aes128_gcm` or `aes256_gcm`. + * @param on ` on `, which ends each name. + */ +template +void cover_gcm_moves(std::string const& family, std::string const& on) { + auto const key = coverage_key(); + std::array const iv{}; + auto keyed = Gcm::make(key); + auto other_keyed = Gcm::make(key); + auto target = Gcm::make(key); + auto in_message = Gcm::make(key); + auto in_other_message = Gcm::make(key); + if (!BOOST_TEST(keyed.has_value()) || !BOOST_TEST(other_keyed.has_value()) || + !BOOST_TEST(target.has_value()) || !BOOST_TEST(in_message.has_value()) || + !BOOST_TEST(in_other_message.has_value()) || + !BOOST_TEST(in_message->begin_seal(iv, {}).has_value()) || + !BOOST_TEST(in_other_message->begin_seal(iv, {}).has_value())) { + return; + } + covered(family + "::gcm(gcm&&) of a keyed object" + on, + [&keyed] { [[maybe_unused]] Gcm const moved(std::move(*keyed)); }); + covered(family + "::operator=(gcm&&) of a keyed object" + on, + [&other_keyed, &target] { *target = std::move(*other_keyed); }); + covered(family + "::gcm(gcm&&) during begin_seal" + on, + [&in_message] { [[maybe_unused]] Gcm const moved(std::move(*in_message)); }); + covered(family + "::operator=(gcm&&) during begin_seal" + on, + [&in_other_message, &target] { *target = std::move(*in_other_message); }); +} + +/** + * gcm's guarded functions: `make`, a key of the wrong size included; those on a message + * (`cover_gcm_messages`); and the moves (`cover_gcm_moves`). + * + * @tparam Gcm `gcm>`, `gcm>` or `gcm>`. + * @param family The algorithm's name, `aes128_gcm` or `aes256_gcm`. + * @param backend The backend's name. + */ +template +void cover_gcm(std::string const& family, std::string const& backend) { + std::string const on = " on " + backend; + auto const key = coverage_key(); + covered(family + "::make" + on, [&key] { BOOST_TEST(Gcm::make(key).has_value()); }); + covered(family + "::make with a short key" + on, [&key] { + BOOST_TEST(Gcm::make(std::span(key).first(Gcm::key_size - 1)).error() == + webcpp::crypto::error::invalid_key_size); + }); + auto made = Gcm::make(key); + if (BOOST_TEST(made.has_value())) { + cover_gcm_messages(*made, family, on); + } + cover_gcm_moves(family, on); +} + +/** + * ciphers::aead's guarded functions: `make`, a key of the wrong size included, `seal` and + * `open`, a nonce of the wrong size included. + * + * @tparam Aead `aead`: ChaCha20-Poly1305 and its forms, or a secret box. + * @param family The family's name, as the public alias spells it. + * @param backend The backend's name. + */ +template +void cover_aead(std::string const& family, std::string const& backend) { + std::string const on = " on " + backend; + auto const key = coverage_key(); + covered(family + "::make" + on, [&key] { BOOST_TEST(Aead::make(key).has_value()); }); + covered(family + "::make with a short key" + on, [&key] { + BOOST_TEST(Aead::make(std::span(key).first(Aead::key_size - 1)).error() == + webcpp::crypto::error::invalid_key_size); + }); + auto made = Aead::make(key); + if (!BOOST_TEST(made.has_value())) { + return; + } + std::array const iv{}; + std::array data{}; + typename Aead::result_type tag{}; + covered(family + "::seal" + on, [&] { + auto const sealed = made->seal(iv, {}, data); + if (BOOST_TEST(sealed.has_value())) { + tag = *sealed; + } + }); + covered(family + "::open" + on, [&] { + BOOST_TEST(made->open(iv, {}, data, std::span(tag.data(), tag.size())).has_value()); + }); + covered(family + "::seal with a short nonce" + on, [&] { + BOOST_TEST(made->seal(std::span(iv).first(Aead::iv_size - 1), {}, data).error() == + webcpp::crypto::error::invalid_iv_size); + }); + covered(family + "::open with a short nonce" + on, [&] { + BOOST_TEST(made->open(std::span(iv).first(Aead::iv_size - 1), {}, data, + std::span(tag.data(), tag.size())) + .error() == webcpp::crypto::error::invalid_iv_size); + }); +} + +} // namespace webcpp::test + +#endif // WEBCPP_TEST_CRYPTO_SUPPORT_TIMING_COVERAGE_HPP diff --git a/test/timing_coverage_bearssl_test.cpp b/test/timing_coverage_bearssl_test.cpp new file mode 100644 index 00000000..0f7b122f --- /dev/null +++ b/test/timing_coverage_bearssl_test.cpp @@ -0,0 +1,44 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// Every public function that runs BearSSL's code enters detail::timing_guard and leaves the +// thread's DIT as it found it, on every target, also where DIT does not apply: AES-128-GCM and +// AES-256-GCM, their moves included, and ChaCha20-Poly1305, each with valid input and with input +// refused before the backend (support/timing_coverage.hpp). + +#include "support/timing_seam.hpp" + +#include +#include +#include +#include +#include + +#include "support/timing_coverage.hpp" + +#include + +namespace { + +namespace crypto = webcpp::crypto; +using crypto::backends::bearssl; + +// The ciphers: AES-GCM and ChaCha20-Poly1305. +void cover_ciphers() { + webcpp::test::cover_gcm>>("aes128_gcm", + "bearssl"); + webcpp::test::cover_gcm>>("aes256_gcm", + "bearssl"); + webcpp::test::cover_aead>("chacha20_poly1305", + "bearssl"); +} + +} // namespace + +int main() { + cover_ciphers(); + return webcpp::test::exit_status(boost::report_errors()); +} diff --git a/test/timing_coverage_ctaes_test.cpp b/test/timing_coverage_ctaes_test.cpp new file mode 100644 index 00000000..248a39f6 --- /dev/null +++ b/test/timing_coverage_ctaes_test.cpp @@ -0,0 +1,39 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// Every public function that runs ctaes's code enters detail::timing_guard and leaves the +// thread's DIT as it found it, on every target, also where DIT does not apply: the AES block +// cipher of each key size, with valid input and with input refused before the backend +// (support/timing_coverage.hpp). + +#include "support/timing_seam.hpp" + +#include +#include +#include + +#include "support/timing_coverage.hpp" + +#include + +namespace { + +namespace crypto = webcpp::crypto; +using crypto::backends::ctaes; + +// The ciphers: the AES block cipher. +void cover_ciphers() { + webcpp::test::cover_aes>("aes128", "ctaes"); + webcpp::test::cover_aes>("aes192", "ctaes"); + webcpp::test::cover_aes>("aes256", "ctaes"); +} + +} // namespace + +int main() { + cover_ciphers(); + return webcpp::test::exit_status(boost::report_errors()); +} diff --git a/test/timing_coverage_openssl_test.cpp b/test/timing_coverage_openssl_test.cpp new file mode 100644 index 00000000..4e1b241e --- /dev/null +++ b/test/timing_coverage_openssl_test.cpp @@ -0,0 +1,48 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// Every public function that runs OpenSSL's code enters detail::timing_guard and leaves the +// thread's DIT as it found it, on native, wasip2 and wasip3, also where DIT does not apply: the +// AES block cipher of each key size, AES-128-GCM and AES-256-GCM, their moves included, and +// ChaCha20-Poly1305, each with valid input and with input refused before the backend +// (support/timing_coverage.hpp). + +#include "support/timing_seam.hpp" + +#include +#include +#include +#include +#include + +#include "support/timing_coverage.hpp" + +#include + +namespace { + +namespace crypto = webcpp::crypto; +using crypto::backends::openssl; + +// The ciphers: the AES block cipher, AES-GCM and ChaCha20-Poly1305. +void cover_ciphers() { + webcpp::test::cover_aes>("aes128", "openssl"); + webcpp::test::cover_aes>("aes192", "openssl"); + webcpp::test::cover_aes>("aes256", "openssl"); + webcpp::test::cover_gcm>>("aes128_gcm", + "openssl"); + webcpp::test::cover_gcm>>("aes256_gcm", + "openssl"); + webcpp::test::cover_aead>("chacha20_poly1305", + "openssl"); +} + +} // namespace + +int main() { + cover_ciphers(); + return webcpp::test::exit_status(boost::report_errors()); +} diff --git a/test/timing_coverage_sodium_test.cpp b/test/timing_coverage_sodium_test.cpp new file mode 100644 index 00000000..e77ce3ec --- /dev/null +++ b/test/timing_coverage_sodium_test.cpp @@ -0,0 +1,202 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// Every public function that runs libsodium's code enters detail::timing_guard and leaves the +// thread's DIT as it found it, on every target, also where DIT does not apply, each with valid +// input and with input refused before the backend (support/timing_coverage.hpp). One section per +// part of the library: the ciphers (ChaCha20-Poly1305, its original form, XChaCha20-Poly1305 and +// both secret boxes), the boxes (both boxes and the sealed box), and PEM. + +#include "support/timing_seam.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "support/timing_coverage.hpp" + +#include + +#include +#include +#include +#include +#include +#include + +namespace { + +namespace crypto = webcpp::crypto; +using crypto::backends::sodium; +using webcpp::test::covered; + +// --------------------------------------------------------------------------------------------- +// The ciphers. +// --------------------------------------------------------------------------------------------- + +/** ChaCha20-Poly1305, its original form, XChaCha20-Poly1305 and both secret boxes. */ +void cover_ciphers() { + webcpp::test::cover_aead>("chacha20_poly1305", + "sodium"); + webcpp::test::cover_aead>( + "chacha20_poly1305_original", "sodium"); + webcpp::test::cover_aead>("xchacha20_poly1305", + "sodium"); + webcpp::test::cover_aead>("secretbox", "sodium"); + webcpp::test::cover_aead>( + "secretbox_xchacha20poly1305", "sodium"); +} + +// --------------------------------------------------------------------------------------------- +// The boxes. +// --------------------------------------------------------------------------------------------- + +/** An X25519 key pair whose secret key is `fill` repeated, for the boxes. */ +struct coverage_keypair { + /** The secret key. */ + std::array secret_key{}; + /** Its public key. */ + std::array public_key{}; +}; + +/** The key pair of the secret key of 32 bytes `fill`; its public key, derived outside a check. */ +coverage_keypair make_keypair(std::uint8_t fill) { + coverage_keypair made; + made.secret_key.fill(fill); + auto const derived = crypto::curves::x25519::public_key(made.secret_key); + if (BOOST_TEST(derived.has_value())) { + made.public_key = *derived; + } + return made; +} + +/** + * A box's guarded functions: `make`, a secret key of the wrong size included, `seal` and `open`, + * a nonce of the wrong size included. + * + * @tparam Box `basic_box`. + * @param family The family's name, as the public alias spells it. + */ +template +void cover_box(std::string const& family) { + std::string const on = " on sodium"; + auto const alice = make_keypair(0x41); + auto const bob = make_keypair(0x42); + covered(family + "::make" + on, + [&] { BOOST_TEST(Box::make(alice.secret_key, bob.public_key).has_value()); }); + covered(family + "::make with a short secret key" + on, [&] { + BOOST_TEST(Box::make(std::span(alice.secret_key).first(31), bob.public_key).error() == + crypto::error::invalid_key_size); + }); + auto made = Box::make(alice.secret_key, bob.public_key); + if (!BOOST_TEST(made.has_value())) { + return; + } + std::array const iv{}; + std::array data{}; + typename Box::result_type tag{}; + covered(family + "::seal" + on, [&] { + auto const sealed = made->seal(iv, data); + if (BOOST_TEST(sealed.has_value())) { + tag = *sealed; + } + }); + covered(family + "::open" + on, [&] { + BOOST_TEST(made->open(iv, data, std::span(tag.data(), tag.size())).has_value()); + }); + covered(family + "::seal with a short nonce" + on, [&] { + BOOST_TEST(made->seal(std::span(iv).first(Box::iv_size - 1), data).error() == + crypto::error::invalid_iv_size); + }); + covered(family + "::open with a short nonce" + on, [&] { + BOOST_TEST(made->open(std::span(iv).first(Box::iv_size - 1), data, + std::span(tag.data(), tag.size())) + .error() == crypto::error::invalid_iv_size); + }); +} + +/** The sealed box's `seal` and `open`, an output of the wrong size included. */ +void cover_sealed_box() { + using sealed_box = crypto::hybrid::basic_sealed_box; + auto const bob = make_keypair(0x42); + std::array const message{1, 2, 3, 4, 5, 6, 7, 8}; + std::array sealed{}; + std::array opened{}; + covered("sealed_box::seal on sodium", + [&] { BOOST_TEST(sealed_box::seal(bob.public_key, message, sealed).has_value()); }); + covered("sealed_box::open on sodium", [&] { + BOOST_TEST(sealed_box::open(bob.public_key, bob.secret_key, sealed, opened).has_value()); + }); + covered("sealed_box::seal into a short output on sodium", [&] { + BOOST_TEST(sealed_box::seal(bob.public_key, message, std::span(sealed).first(1)).error() == + crypto::error::invalid_input_size); + }); + covered("sealed_box::open into a short output on sodium", [&] { + BOOST_TEST( + sealed_box::open(bob.public_key, bob.secret_key, sealed, std::span(opened).first(1)) + .error() == crypto::error::invalid_input_size); + }); +} + +/** Both boxes and the sealed box. */ +void cover_boxes() { + cover_box>("box"); + cover_box>( + "box_xchacha20poly1305"); + cover_sealed_box(); +} + +// --------------------------------------------------------------------------------------------- +// PEM. +// --------------------------------------------------------------------------------------------- + +/** `algorithm::armour` and `unarmour`, and the free functions, a refusal of each. */ +void cover_pem() { + namespace pem = crypto::pem; + std::string_view const label = pem::public_key_label; + std::array const der = webcpp::test::coverage_key<44>(); + std::string text(pem::armoured_size(label.size(), der.size()), ' '); + std::array out{}; + covered("pem::algorithm::armour on sodium", + [&] { BOOST_TEST(pem::algorithm::armour(label, der, text).has_value()); }); + covered("pem::algorithm::unarmour on sodium", + [&] { BOOST_TEST(pem::algorithm::unarmour(label, text, out).has_value()); }); + covered("pem::armour on sodium", + [&] { BOOST_TEST(pem::armour(label, der, text).has_value()); }); + covered("pem::unarmour on sodium", + [&] { BOOST_TEST(pem::unarmour(label, text, out).has_value()); }); + covered("pem::algorithm::armour into a short output on sodium", [&] { + BOOST_TEST(pem::algorithm::armour(label, der, std::span(text).first(1)).error() == + crypto::error::invalid_input_size); + }); + covered("pem::algorithm::unarmour of a text without lines on sodium", [&] { + BOOST_TEST(pem::algorithm::unarmour(label, "PEM", out).error() == + crypto::error::invalid_encoding); + }); + covered("pem::armour into a short output on sodium", [&] { + BOOST_TEST(pem::armour(label, der, std::span(text).first(1)).error() == + crypto::error::invalid_input_size); + }); + covered("pem::unarmour of a text without lines on sodium", [&] { + BOOST_TEST(pem::unarmour(label, "PEM", out).error() == + crypto::error::invalid_encoding); + }); +} + +} // namespace + +int main() { + cover_ciphers(); + cover_boxes(); + cover_pem(); + return webcpp::test::exit_status(boost::report_errors()); +} From 8ab4f7e0c6f20ecc18929bdf183ba9b4e1006823 Mon Sep 17 00:00:00 2001 From: R Date: Fri, 20 Nov 2026 20:15:23 -0300 Subject: [PATCH 04/22] crypto: Ed25519, X25519, kx and their formats set DIT around each operation --- include/webcpp/crypto/curves/ed25519.hpp | 12 + .../webcpp/crypto/curves/ed25519/formats.hpp | 3 + include/webcpp/crypto/curves/x25519.hpp | 5 + .../webcpp/crypto/curves/x25519/formats.hpp | 3 + include/webcpp/crypto/curves/x25519/kx.hpp | 5 + test/Jamfile | 3 + test/timing_coverage_sodium_test.cpp | 287 +++++++++++++++++- test/timing_sodium_test.cpp | 90 ++++++ 8 files changed, 407 insertions(+), 1 deletion(-) create mode 100644 test/timing_sodium_test.cpp diff --git a/include/webcpp/crypto/curves/ed25519.hpp b/include/webcpp/crypto/curves/ed25519.hpp index 2517ebc2..4597e49a 100644 --- a/include/webcpp/crypto/curves/ed25519.hpp +++ b/include/webcpp/crypto/curves/ed25519.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include // lint-world: random_device draws generate()'s seed #include @@ -110,6 +111,7 @@ class basic_keypair { */ [[nodiscard]] static boost::system::result make( std::span seed) noexcept { + detail::timing_guard const timing; if (seed.size() != seed_size) { return error::invalid_key_size; } @@ -129,6 +131,7 @@ class basic_keypair { * none; `backend_failure`. */ [[nodiscard]] static boost::system::result generate() noexcept { + detail::timing_guard const timing; secret seed; auto const drawn = random_device().fill(seed.writable()); // lint-world: the seed if (!drawn) { @@ -179,6 +182,7 @@ class basic_keypair { * @return 32 bytes; `invalid_state` moved from. */ [[nodiscard]] boost::system::result> public_key() const noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -195,6 +199,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> sign( std::span message) const noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -213,6 +218,7 @@ class basic_keypair { * @return 32 secret bytes; `invalid_state`; `backend_failure`. */ [[nodiscard]] boost::system::result> x25519_secret() const noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -254,6 +260,7 @@ template verify(std::span public_key, std::span message, std::span signature) noexcept { + detail::timing_guard const timing; static_assert(std::is_same_v, "webcpp::crypto: Ed25519's only verification policy is curves::ed25519::strict."); if (public_key.size() != 32) { @@ -279,6 +286,7 @@ template > [[nodiscard]] boost::system::result> to_x25519_public( std::span ed25519_public_key) noexcept { + detail::timing_guard const timing; if (ed25519_public_key.size() != 32) { return error::invalid_public_key; } @@ -332,6 +340,7 @@ class ph { * @return The signer, ready for the message's pieces; `backend_failure`. */ [[nodiscard]] static boost::system::result begin() noexcept { + detail::timing_guard const timing; ph begun; auto const started = engine::begin(begun.state_); if (!started) { @@ -382,6 +391,7 @@ class ph { * @return Nothing; `invalid_state` once the message is signed or verified, or moved from. */ [[nodiscard]] boost::system::result update(std::span piece) noexcept { + detail::timing_guard const timing; if (!begun_) { return error::invalid_state; } @@ -398,6 +408,7 @@ class ph { */ [[nodiscard]] boost::system::result> sign( basic_keypair const& key) noexcept { + detail::timing_guard const timing; if (!begun_ || !key.keyed_) { return error::invalid_state; } @@ -421,6 +432,7 @@ class ph { [[nodiscard]] boost::system::result verify( std::span public_key, std::span signature) noexcept { + detail::timing_guard const timing; if (!begun_) { return error::invalid_state; } diff --git a/include/webcpp/crypto/curves/ed25519/formats.hpp b/include/webcpp/crypto/curves/ed25519/formats.hpp index de46b540..b1a6175b 100644 --- a/include/webcpp/crypto/curves/ed25519/formats.hpp +++ b/include/webcpp/crypto/curves/ed25519/formats.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -131,6 +132,7 @@ template > template > [[nodiscard]] boost::system::result> decode_spki( std::span spki) noexcept { + detail::timing_guard const timing; if (spki.size() != max_spki_size) { return error::invalid_encoding; } @@ -181,6 +183,7 @@ template > template > [[nodiscard]] boost::system::result> decode_pkcs8( std::span pkcs8) noexcept { + detail::timing_guard const timing; if (pkcs8.size() != max_pkcs8_size) { return error::invalid_encoding; } diff --git a/include/webcpp/crypto/curves/x25519.hpp b/include/webcpp/crypto/curves/x25519.hpp index b0299273..7e42d2a0 100644 --- a/include/webcpp/crypto/curves/x25519.hpp +++ b/include/webcpp/crypto/curves/x25519.hpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -69,6 +70,7 @@ struct algorithm { */ [[nodiscard]] static boost::system::result> public_key( std::span secret_key) noexcept { + detail::timing_guard const timing; if (secret_key.size() != key_size) { return error::invalid_key_size; } @@ -91,6 +93,7 @@ struct algorithm { [[nodiscard]] static boost::system::result> agree( std::span secret_key, std::span peer_public_key) noexcept { + detail::timing_guard const timing; if (secret_key.size() != key_size) { return error::invalid_key_size; } @@ -130,6 +133,7 @@ struct algorithm { template > [[nodiscard]] boost::system::result> public_key( std::span secret_key) noexcept { + detail::timing_guard const timing; return algorithm::public_key(secret_key); } @@ -146,6 +150,7 @@ template > [[nodiscard]] boost::system::result> agree( std::span secret_key, std::span peer_public_key) noexcept { + detail::timing_guard const timing; return algorithm::agree(secret_key, peer_public_key); } diff --git a/include/webcpp/crypto/curves/x25519/formats.hpp b/include/webcpp/crypto/curves/x25519/formats.hpp index e7238fa9..cbee057a 100644 --- a/include/webcpp/crypto/curves/x25519/formats.hpp +++ b/include/webcpp/crypto/curves/x25519/formats.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -110,6 +111,7 @@ template > template > [[nodiscard]] boost::system::result> decode_spki( std::span spki) noexcept { + detail::timing_guard const timing; if (spki.size() != max_spki_size) { return error::invalid_encoding; } @@ -160,6 +162,7 @@ template > template > [[nodiscard]] boost::system::result> decode_pkcs8( std::span pkcs8) noexcept { + detail::timing_guard const timing; if (pkcs8.size() != max_pkcs8_size) { return error::invalid_encoding; } diff --git a/include/webcpp/crypto/curves/x25519/kx.hpp b/include/webcpp/crypto/curves/x25519/kx.hpp index 1431cb26..f869db2c 100644 --- a/include/webcpp/crypto/curves/x25519/kx.hpp +++ b/include/webcpp/crypto/curves/x25519/kx.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include // lint-world: random_device draws generate()'s seed #include @@ -102,6 +103,7 @@ class kx_keypair { */ [[nodiscard]] static boost::system::result make( std::span seed) noexcept { + detail::timing_guard const timing; if (seed.size() != seed_size) { return error::invalid_key_size; } @@ -121,6 +123,7 @@ class kx_keypair { * none; `backend_failure`. */ [[nodiscard]] static boost::system::result generate() noexcept { + detail::timing_guard const timing; secret seed; auto const drawn = random_device().fill(seed.writable()); // lint-world: the seed if (!drawn) { @@ -189,6 +192,7 @@ class kx_keypair { */ [[nodiscard]] boost::system::result client_session_keys( std::span server_public_key) const noexcept { + detail::timing_guard const timing; return session(server_public_key); } @@ -202,6 +206,7 @@ class kx_keypair { */ [[nodiscard]] boost::system::result server_session_keys( std::span client_public_key) const noexcept { + detail::timing_guard const timing; return session(client_public_key); } diff --git a/test/Jamfile b/test/Jamfile index 1ad010f9..127df365 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -124,6 +124,9 @@ crypto-run timing_coverage_ctaes : timing_coverage_ctaes_test.cpp : /third_party//ctaes ; crypto-run timing_coverage_sodium : timing_coverage_sodium_test.cpp : /third_party//sodium ; +# The specification's scenarios on libsodium's Ed25519: a thread whose DIT is +# clear finds it clear after sign, one that set it finds it still set. +crypto-run timing_sodium : timing_sodium_test.cpp : /third_party//sodium ; # alone, with no backend linked, compiles, links and runs. crypto-run convenience_header : convenience_header_test.cpp ; crypto-run wipe : wipe_test.cpp ; diff --git a/test/timing_coverage_sodium_test.cpp b/test/timing_coverage_sodium_test.cpp index e77ce3ec..8c55aff6 100644 --- a/test/timing_coverage_sodium_test.cpp +++ b/test/timing_coverage_sodium_test.cpp @@ -8,18 +8,25 @@ // thread's DIT as it found it, on every target, also where DIT does not apply, each with valid // input and with input refused before the backend (support/timing_coverage.hpp). One section per // part of the library: the ciphers (ChaCha20-Poly1305, its original form, XChaCha20-Poly1305 and -// both secret boxes), the boxes (both boxes and the sealed box), and PEM. +// both secret boxes), the boxes (both boxes and the sealed box), PEM, and the curves (Ed25519 +// and Ed25519ph, X25519, crypto_kx, and the decoders of their key formats; the encoders call no +// backend and hold no guard). #include "support/timing_seam.hpp" #include #include #include +#include +#include #include +#include +#include #include #include #include #include +#include #include #include "support/timing_coverage.hpp" @@ -32,6 +39,7 @@ #include #include #include +#include namespace { @@ -192,11 +200,288 @@ void cover_pem() { }); } +// --------------------------------------------------------------------------------------------- +// The curves. +// --------------------------------------------------------------------------------------------- + +/** + * Ed25519's key pair: `make`, a seed of the wrong size included, `generate` (the system's + * entropy), and `public_key`, `sign` and `x25519_secret`, each of a moved-from key pair too. + */ +void cover_ed25519_keypair() { + using keypair = crypto::curves::ed25519::basic_keypair; + auto const seed = webcpp::test::coverage_key(); + std::array const message{1, 2, 3}; + covered("ed25519::basic_keypair::make on sodium", + [&] { BOOST_TEST(keypair::make(seed).has_value()); }); + covered("ed25519::basic_keypair::make with a short seed on sodium", [&] { + BOOST_TEST(keypair::make(std::span(seed).first(31)).error() == + crypto::error::invalid_key_size); + }); + covered("ed25519::basic_keypair::generate on sodium", + [] { BOOST_TEST(keypair::generate().has_value()); }); + auto made = keypair::make(seed); + if (!BOOST_TEST(made.has_value())) { + return; + } + covered("ed25519::basic_keypair::public_key on sodium", + [&] { BOOST_TEST(made->public_key().has_value()); }); + covered("ed25519::basic_keypair::sign on sodium", + [&] { BOOST_TEST(made->sign(message).has_value()); }); + covered("ed25519::basic_keypair::x25519_secret on sodium", + [&] { BOOST_TEST(made->x25519_secret().has_value()); }); + [[maybe_unused]] keypair const taken(std::move(*made)); + covered("ed25519::basic_keypair::public_key moved from on sodium", [&] { + // NOLINTNEXTLINE(bugprone-use-after-move): the moved-from key pair's refusal + BOOST_TEST(made->public_key().error() == crypto::error::invalid_state); + }); + covered("ed25519::basic_keypair::sign moved from on sodium", [&] { + // NOLINTNEXTLINE(bugprone-use-after-move): the moved-from key pair's refusal + BOOST_TEST(made->sign(message).error() == crypto::error::invalid_state); + }); + covered("ed25519::basic_keypair::x25519_secret moved from on sodium", [&] { + // NOLINTNEXTLINE(bugprone-use-after-move): the moved-from key pair's refusal + BOOST_TEST(made->x25519_secret().error() == crypto::error::invalid_state); + }); +} + +/** Ed25519's free `verify` and `to_x25519_public`, a key of the wrong size included. */ +void cover_ed25519_free() { + namespace ed25519 = crypto::curves::ed25519; + auto const made = ed25519::basic_keypair::make(webcpp::test::coverage_key<32>()); + std::array const message{1, 2, 3}; + if (!BOOST_TEST(made.has_value())) { + return; + } + auto const public_key = made->public_key(); + auto const signature = made->sign(message); + if (!BOOST_TEST(public_key.has_value()) || !BOOST_TEST(signature.has_value())) { + return; + } + covered("ed25519::verify on sodium", [&] { + BOOST_TEST((ed25519::verify(*public_key, message, *signature) + .has_value())); + }); + covered("ed25519::verify with a short key on sodium", [&] { + BOOST_TEST((ed25519::verify(std::span(*public_key).first(31), + message, *signature) + .error() == crypto::error::invalid_public_key)); + }); + covered("ed25519::to_x25519_public on sodium", + [&] { BOOST_TEST(ed25519::to_x25519_public(*public_key).has_value()); }); + covered("ed25519::to_x25519_public with a short key on sodium", [&] { + BOOST_TEST(ed25519::to_x25519_public(std::span(*public_key).first(31)).error() == + crypto::error::invalid_public_key); + }); +} + +/** Ed25519ph's `begin`, `update`, `sign` and `verify` of a message, a valid one. */ +void cover_ed25519ph() { + namespace ed25519 = crypto::curves::ed25519; + using ph = ed25519::ph; + auto const key = ed25519::basic_keypair::make(webcpp::test::coverage_key<32>()); + std::array const piece{1, 2, 3}; + covered("ed25519::ph::begin on sodium", [] { BOOST_TEST(ph::begin().has_value()); }); + auto signer = ph::begin(); + auto verifier = ph::begin(); + if (!BOOST_TEST(key.has_value()) || !BOOST_TEST(signer.has_value()) || + !BOOST_TEST(verifier.has_value()) || !BOOST_TEST(verifier->update(piece).has_value())) { + return; + } + auto const public_key = key->public_key(); + if (!BOOST_TEST(public_key.has_value())) { + return; + } + covered("ed25519::ph::update on sodium", + [&] { BOOST_TEST(signer->update(piece).has_value()); }); + std::array signature{}; + covered("ed25519::ph::sign on sodium", [&] { + auto const signed_message = signer->sign(*key); + if (BOOST_TEST(signed_message.has_value())) { + signature = *signed_message; + } + }); + covered("ed25519::ph::verify on sodium", + [&] { BOOST_TEST(verifier->verify(*public_key, signature).has_value()); }); +} + +/** + * Ed25519ph's `update`, `sign` and `verify` of an ended message, refused before the backend, and + * `verify` with a key of the wrong size. + */ +void cover_ed25519ph_refusals() { + namespace ed25519 = crypto::curves::ed25519; + using ph = ed25519::ph; + auto const key = ed25519::basic_keypair::make(webcpp::test::coverage_key<32>()); + std::array const public_key{}; + std::array const signature{}; + std::array const piece{1, 2, 3}; + auto ended = ph::begin(); + auto short_key = ph::begin(); + if (!BOOST_TEST(key.has_value()) || !BOOST_TEST(ended.has_value()) || + !BOOST_TEST(short_key.has_value()) || !BOOST_TEST(ended->sign(*key).has_value())) { + return; + } + covered("ed25519::ph::update of an ended message on sodium", + [&] { BOOST_TEST(ended->update(piece).error() == crypto::error::invalid_state); }); + covered("ed25519::ph::sign of an ended message on sodium", + [&] { BOOST_TEST(ended->sign(*key).error() == crypto::error::invalid_state); }); + covered("ed25519::ph::verify of an ended message on sodium", [&] { + BOOST_TEST(ended->verify(public_key, signature).error() == crypto::error::invalid_state); + }); + covered("ed25519::ph::verify with a short key on sodium", [&] { + BOOST_TEST(short_key->verify(std::span(public_key).first(31), signature).error() == + crypto::error::invalid_public_key); + }); +} + +/** Ed25519's `decode_spki` and `decode_pkcs8`, an encoding of the wrong size of each included. */ +void cover_ed25519_formats() { + namespace ed25519 = crypto::curves::ed25519; + auto const key = ed25519::basic_keypair::make(webcpp::test::coverage_key<32>()); + if (!BOOST_TEST(key.has_value())) { + return; + } + auto const public_key = key->public_key(); + std::array spki{}; + std::array pkcs8{}; + if (!BOOST_TEST(public_key.has_value()) || + !BOOST_TEST(ed25519::encode_spki(*public_key, spki).has_value()) || + !BOOST_TEST(ed25519::encode_pkcs8(*key, pkcs8).has_value())) { + return; + } + covered("ed25519::decode_spki on sodium", + [&] { BOOST_TEST(ed25519::decode_spki(spki).has_value()); }); + covered("ed25519::decode_pkcs8 on sodium", + [&] { BOOST_TEST(ed25519::decode_pkcs8(pkcs8).has_value()); }); + covered("ed25519::decode_spki of a short encoding on sodium", [&] { + BOOST_TEST(ed25519::decode_spki(std::span(spki).first(43)).error() == + crypto::error::invalid_encoding); + }); + covered("ed25519::decode_pkcs8 of a short encoding on sodium", [&] { + BOOST_TEST(ed25519::decode_pkcs8(std::span(pkcs8).first(47)).error() == + crypto::error::invalid_encoding); + }); +} + +/** + * X25519's `algorithm::public_key` and `agree`, and the free functions, a key of the + * wrong size of each included. + */ +void cover_x25519() { + namespace x25519 = crypto::curves::x25519; + using algorithm = x25519::algorithm; + auto const alice = make_keypair(0x41); + auto const bob = make_keypair(0x42); + auto const short_key = std::span(alice.secret_key).first(31); + covered("x25519::algorithm::public_key on sodium", + [&] { BOOST_TEST(algorithm::public_key(alice.secret_key).has_value()); }); + covered("x25519::algorithm::agree on sodium", + [&] { BOOST_TEST(algorithm::agree(alice.secret_key, bob.public_key).has_value()); }); + covered("x25519::public_key on sodium", + [&] { BOOST_TEST(x25519::public_key(alice.secret_key).has_value()); }); + covered("x25519::agree on sodium", [&] { + BOOST_TEST(x25519::agree(alice.secret_key, bob.public_key).has_value()); + }); + covered("x25519::algorithm::public_key with a short key on sodium", [&] { + BOOST_TEST(algorithm::public_key(short_key).error() == crypto::error::invalid_key_size); + }); + covered("x25519::algorithm::agree with a short key on sodium", [&] { + BOOST_TEST(algorithm::agree(short_key, bob.public_key).error() == + crypto::error::invalid_key_size); + }); + covered("x25519::public_key with a short key on sodium", [&] { + BOOST_TEST(x25519::public_key(short_key).error() == + crypto::error::invalid_key_size); + }); + covered("x25519::agree with a short key on sodium", [&] { + BOOST_TEST(x25519::agree(short_key, bob.public_key).error() == + crypto::error::invalid_key_size); + }); +} + +/** X25519's `decode_spki` and `decode_pkcs8`, an encoding of the wrong size of each included. */ +void cover_x25519_formats() { + namespace x25519 = crypto::curves::x25519; + auto const alice = make_keypair(0x41); + std::array spki{}; + std::array pkcs8{}; + if (!BOOST_TEST(x25519::encode_spki(alice.public_key, spki).has_value()) || + !BOOST_TEST(x25519::encode_pkcs8(alice.secret_key, pkcs8).has_value())) { + return; + } + covered("x25519::decode_spki on sodium", + [&] { BOOST_TEST(x25519::decode_spki(spki).has_value()); }); + covered("x25519::decode_pkcs8 on sodium", + [&] { BOOST_TEST(x25519::decode_pkcs8(pkcs8).has_value()); }); + covered("x25519::decode_spki of a short encoding on sodium", [&] { + BOOST_TEST(x25519::decode_spki(std::span(spki).first(43)).error() == + crypto::error::invalid_encoding); + }); + covered("x25519::decode_pkcs8 of a short encoding on sodium", [&] { + BOOST_TEST(x25519::decode_pkcs8(std::span(pkcs8).first(47)).error() == + crypto::error::invalid_encoding); + }); +} + +/** + * crypto_kx's key pair: `make`, a seed of the wrong size included, `generate` (the system's + * entropy), and `client_session_keys` and `server_session_keys`, a peer key of the wrong size + * included. `public_key` copies bytes and holds no guard (ruling R4). + */ +void cover_kx() { + using kx_keypair = crypto::curves::x25519::kx_keypair; + auto const seed = webcpp::test::coverage_key(); + covered("x25519::kx_keypair::make on sodium", + [&] { BOOST_TEST(kx_keypair::make(seed).has_value()); }); + covered("x25519::kx_keypair::make with a short seed on sodium", [&] { + BOOST_TEST(kx_keypair::make(std::span(seed).first(31)).error() == + crypto::error::invalid_key_size); + }); + covered("x25519::kx_keypair::generate on sodium", + [] { BOOST_TEST(kx_keypair::generate().has_value()); }); + auto const client = kx_keypair::make(seed); + auto const server = kx_keypair::generate(); + if (!BOOST_TEST(client.has_value()) || !BOOST_TEST(server.has_value())) { + return; + } + auto const client_public = client->public_key(); + auto const server_public = server->public_key(); + if (!BOOST_TEST(client_public.has_value()) || !BOOST_TEST(server_public.has_value())) { + return; + } + covered("x25519::kx_keypair::client_session_keys on sodium", + [&] { BOOST_TEST(client->client_session_keys(*server_public).has_value()); }); + covered("x25519::kx_keypair::server_session_keys on sodium", + [&] { BOOST_TEST(server->server_session_keys(*client_public).has_value()); }); + covered("x25519::kx_keypair::client_session_keys with a short key on sodium", [&] { + BOOST_TEST(client->client_session_keys(std::span(*server_public).first(31)).error() == + crypto::error::invalid_public_key); + }); + covered("x25519::kx_keypair::server_session_keys with a short key on sodium", [&] { + BOOST_TEST(server->server_session_keys(std::span(*client_public).first(31)).error() == + crypto::error::invalid_public_key); + }); +} + +/** Ed25519, Ed25519ph, X25519, crypto_kx, and the decoders of the key formats. */ +void cover_curves() { + cover_ed25519_keypair(); + cover_ed25519_free(); + cover_ed25519ph(); + cover_ed25519ph_refusals(); + cover_ed25519_formats(); + cover_x25519(); + cover_x25519_formats(); + cover_kx(); +} + } // namespace int main() { cover_ciphers(); cover_boxes(); cover_pem(); + cover_curves(); return webcpp::test::exit_status(boost::report_errors()); } diff --git a/test/timing_sodium_test.cpp b/test/timing_sodium_test.cpp new file mode 100644 index 00000000..82e99efb --- /dev/null +++ b/test/timing_sodium_test.cpp @@ -0,0 +1,90 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// The specification's two scenarios of DIT on libsodium's Ed25519: a thread whose DIT is clear +// signs RFC 8032's TEST 1 and finds it clear again, with the RFC's signature; a thread that set +// DIT itself, as a program can, finds it still set after signing. Where the CPU has no DIT, or +// the guard does not apply, both reduce to DIT never being read set. + +#include +#include +#include +#include + +#include "support/hex.hpp" + +#include + +#include +#include +#include +#include + +namespace { + +namespace detail = webcpp::crypto::detail; +using keypair = webcpp::crypto::curves::ed25519::basic_keypair; + +/** RFC 8032 section 7.1, TEST 1: the secret key (the seed); the message is empty. */ +constexpr std::string_view rfc8032_seed = + "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60"; +/** RFC 8032 section 7.1, TEST 1: the signature. */ +constexpr std::string_view rfc8032_signature = + "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9" + "b46bd25bf5f0595bbe24655141438e7a100b"; + +/** + * Sets or clears DIT for this thread, as a program can, so that a test starts from a known state; + * only where the CPU has DIT, since the instruction traps on a CPU without it. + */ +void force_dit([[maybe_unused]] bool set) { +#ifdef WEBCPP_CRYPTO_DETAIL_TIMING_COVERED + if (!detail::cpu_has_dit()) { + return; + } + detail::write_dit(set ? detail::dit_bit : 0); +#endif +} + +/** Signs TEST 1's empty message with TEST 1's key pair and checks the RFC's signature. */ +void sign_test_1() { + auto const seed = webcpp::test::from_hex(rfc8032_seed); + auto const expected = webcpp::test::from_hex(rfc8032_signature); + auto const made = keypair::make(seed); + if (!BOOST_TEST(made.has_value())) { + return; + } + auto const signature = made->sign(std::span()); + if (BOOST_TEST(signature.has_value())) { + BOOST_TEST(std::ranges::equal(*signature, expected)); + } +} + +// A program that never set DIT signs: DIT is clear again when sign returns. +void sign_clears_state() { + force_dit(false); + BOOST_TEST(!detail::dit_is_set()); + sign_test_1(); + BOOST_TEST(!detail::dit_is_set()); +} + +// A program that set DIT itself signs: DIT is still set when sign returns. +void sign_leaves_set_state() { + force_dit(true); + BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + sign_test_1(); + BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + force_dit(false); + BOOST_TEST(!detail::dit_is_set()); +} + +} // namespace + +int main() { + sign_clears_state(); + sign_leaves_set_state(); + return webcpp::test::exit_status(boost::report_errors()); +} From e677df1be674af3599f2a69a868d67013c764359 Mon Sep 17 00:00:00 2001 From: R Date: Fri, 20 Nov 2026 20:28:45 -0300 Subject: [PATCH 05/22] crypto: secp256k1 and its groups set DIT around each operation --- include/webcpp/crypto/curves/secp256k1.hpp | 24 + .../crypto/curves/secp256k1/ellswift.hpp | 5 + .../crypto/curves/secp256k1/formats.hpp | 5 + .../webcpp/crypto/curves/secp256k1/musig.hpp | 18 + .../curves/secp256k1/silent_payments.hpp | 6 + .../webcpp/crypto/curves/secp256k1/tweak.hpp | 8 + test/Jamfile | 2 + test/timing_coverage_secp256k1_test.cpp | 913 ++++++++++++++++++ 8 files changed, 981 insertions(+) create mode 100644 test/timing_coverage_secp256k1_test.cpp diff --git a/include/webcpp/crypto/curves/secp256k1.hpp b/include/webcpp/crypto/curves/secp256k1.hpp index d9ba1c7e..a792b333 100644 --- a/include/webcpp/crypto/curves/secp256k1.hpp +++ b/include/webcpp/crypto/curves/secp256k1.hpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include // lint-world: the context's randomisation, its purpose @@ -485,6 +486,7 @@ class public_key { */ [[nodiscard]] friend bool operator==(public_key const& left_key, public_key const& right_key) noexcept { + detail::timing_guard const timing; return compare(left_key, right_key) == 0; } @@ -497,6 +499,7 @@ class public_key { */ [[nodiscard]] friend std::strong_ordering operator<=>(public_key const& left_key, public_key const& right_key) noexcept { + detail::timing_guard const timing; return compare(left_key, right_key) <=> 0; } @@ -565,6 +568,7 @@ class basic_keypair { [[nodiscard]] static boost::system::result make( std::span secret_bytes, std::pmr::memory_resource* memory_resource = std::pmr::get_default_resource()) { + detail::timing_guard const timing; if (secret_bytes.size() != 32) { return error::invalid_key_size; } @@ -641,6 +645,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> xonly_public_key() const noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -658,6 +663,7 @@ class basic_keypair { * @return 33 bytes; `invalid_state` moved from. */ [[nodiscard]] boost::system::result> public_key() const noexcept { + detail::timing_guard const timing; return encoded_public_key<33>(webcpp::crypto::detail::secp256k1_compressed); } @@ -668,6 +674,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> public_key_uncompressed() const noexcept { + detail::timing_guard const timing; return encoded_public_key<65>(webcpp::crypto::detail::secp256k1_uncompressed); } @@ -681,6 +688,7 @@ class basic_keypair { [[nodiscard]] boost::system::result> sign_schnorr( std::span message_bytes, std::span aux_random_bytes) const noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -706,6 +714,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> sign_ecdsa( std::span message_hash) const noexcept { + detail::timing_guard const timing; return signed_ecdsa(message_hash, nullptr); } @@ -721,6 +730,7 @@ class basic_keypair { [[nodiscard]] boost::system::result> sign_ecdsa( std::span message_hash, std::span extra_entropy) const noexcept { + detail::timing_guard const timing; webcpp::crypto::detail::secp256k1_secret nonce_data; std::ranges::copy(extra_entropy, nonce_data.bytes.begin()); return signed_ecdsa(message_hash, nonce_data.bytes.data()); @@ -735,6 +745,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> sign_recoverable(std::span message_hash) const noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -764,6 +775,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> ecdh( std::span public_key_bytes) const noexcept { + detail::timing_guard const timing; return agree<32>(public_key_bytes, nullptr); } @@ -779,6 +791,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> ecdh_x( std::span public_key_bytes) const noexcept { + detail::timing_guard const timing; return agree<32>(public_key_bytes, webcpp::crypto::detail::webcpp_crypto_detail_secp256k1_copy_x); } @@ -791,6 +804,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> ecdh_point( std::span public_key_bytes) const noexcept { + detail::timing_guard const timing; return agree<65>(public_key_bytes, webcpp::crypto::detail::webcpp_crypto_detail_secp256k1_copy_point); } @@ -811,6 +825,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result tweak_xonly( std::span tweak_bytes) noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -984,6 +999,7 @@ namespace webcpp::crypto::curves::secp256k1 { template > [[nodiscard]] boost::system::result parse_public_key( std::span encoded_key) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); @@ -1008,6 +1024,7 @@ template > [[nodiscard]] std::array> serialize_public_key( public_key const& parsed_key) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); @@ -1038,6 +1055,7 @@ template > [[nodiscard]] boost::system::result recover( std::span recoverable_signature, std::span message_hash) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); // libsecp256k1 checks the id with ARG_CHECK, whose callback aborts: refused first here. @@ -1073,6 +1091,7 @@ template > [[nodiscard]] boost::system::result verify_schnorr( std::span signature_bytes, std::span message_bytes, std::span xonly_key_bytes) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); @@ -1103,6 +1122,7 @@ template > std::span signature_bytes, std::span message_hash, std::span public_key_bytes) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); @@ -1135,6 +1155,7 @@ template > [[nodiscard]] boost::system::result to_der( std::span compact_signature, std::span output_bytes) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); if (output_bytes.size() < max_der_size) { return error::invalid_input_size; @@ -1166,6 +1187,7 @@ template > template > [[nodiscard]] boost::system::result> from_der( std::span der_signature) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); if (der_signature.empty()) { return error::invalid_signature; @@ -1197,6 +1219,7 @@ template > template > [[nodiscard]] boost::system::result> normalize_ecdsa( std::span compact_signature) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); @@ -1228,6 +1251,7 @@ template > [[nodiscard]] boost::system::result tweak_xonly( std::span xonly_key_bytes, std::span tweak_bytes) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); diff --git a/include/webcpp/crypto/curves/secp256k1/ellswift.hpp b/include/webcpp/crypto/curves/secp256k1/ellswift.hpp index 284a1c2d..72cd1c52 100644 --- a/include/webcpp/crypto/curves/secp256k1/ellswift.hpp +++ b/include/webcpp/crypto/curves/secp256k1/ellswift.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -73,6 +74,7 @@ enum class ellswift_party { template > [[nodiscard]] std::array ellswift_encode( public_key const& parsed_key, std::span randomness) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -95,6 +97,7 @@ template > template > [[nodiscard]] public_key ellswift_decode( std::span encoded_key) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -120,6 +123,7 @@ template > template [[nodiscard]] boost::system::result> ellswift_create( basic_keypair const& key_pair, std::span randomness) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -161,6 +165,7 @@ template std::span initiator_encoding, std::span responder_encoding, ellswift_party our_party) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); diff --git a/include/webcpp/crypto/curves/secp256k1/formats.hpp b/include/webcpp/crypto/curves/secp256k1/formats.hpp index e3587029..dd2609f0 100644 --- a/include/webcpp/crypto/curves/secp256k1/formats.hpp +++ b/include/webcpp/crypto/curves/secp256k1/formats.hpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -97,6 +98,7 @@ template > [[nodiscard]] boost::system::result encode_spki( public_key const& parsed_key, std::span output_bytes, point_form encoded_form = point_form::compressed) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); if (output_bytes.size() < max_spki_size) { @@ -131,6 +133,7 @@ template > template > [[nodiscard]] boost::system::result decode_spki( std::span spki_bytes) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); @@ -168,6 +171,7 @@ template > template > [[nodiscard]] boost::system::result encode_pkcs8( basic_keypair const& key_pair, std::span output_bytes) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); using access = webcpp::crypto::detail::secp256k1_access; @@ -210,6 +214,7 @@ template > [[nodiscard]] boost::system::result> decode_pkcs8( std::span pkcs8_bytes, std::pmr::memory_resource* memory_resource = std::pmr::get_default_resource()) { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); diff --git a/include/webcpp/crypto/curves/secp256k1/musig.hpp b/include/webcpp/crypto/curves/secp256k1/musig.hpp index e66e27bd..0778a9cb 100644 --- a/include/webcpp/crypto/curves/secp256k1/musig.hpp +++ b/include/webcpp/crypto/curves/secp256k1/musig.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include // lint-world: the nonce's session randomness @@ -85,6 +86,7 @@ class aggregate_key { * @return 32 bytes, the tweaks included. */ [[nodiscard]] std::array xonly_public_key() const noexcept { + detail::timing_guard const timing; webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); public_key full_key = full_public_key(); @@ -105,6 +107,7 @@ class aggregate_key { * @return The key, the tweaks included. */ [[nodiscard]] public_key full_public_key() const noexcept { + detail::timing_guard const timing; webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); using access = webcpp::crypto::detail::secp256k1_access; @@ -127,6 +130,7 @@ class aggregate_key { */ [[nodiscard]] boost::system::result tweak_add( std::span tweak_bytes) noexcept { + detail::timing_guard const timing; return tweaked(tweak_bytes, secp256k1_musig_pubkey_ec_tweak_add); } @@ -140,6 +144,7 @@ class aggregate_key { */ [[nodiscard]] boost::system::result tweak_xonly_add( std::span tweak_bytes) noexcept { + detail::timing_guard const timing; return tweaked(tweak_bytes, secp256k1_musig_pubkey_xonly_tweak_add); } @@ -310,6 +315,7 @@ struct nonce_pair { template > [[nodiscard]] boost::system::result key_aggregate( std::span signer_keys, workspace pointer_space) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -358,6 +364,7 @@ template basic_keypair const& signer, aggregate_key const& signers_key, std::span message_hash = {}, std::span extra_input = {}) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -412,6 +419,7 @@ template template > [[nodiscard]] boost::system::result nonce_aggregate( std::span signer_nonces, workspace pointer_space) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -452,6 +460,7 @@ template > [[nodiscard]] boost::system::result nonce_process( aggregate_nonce const& nonce_sum, std::span message_bytes, aggregate_key const& signers_key) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -482,6 +491,7 @@ template [[nodiscard]] boost::system::result partial_sign( basic_keypair const& signer, secret_nonce signer_nonce, aggregate_key const& signers_key, session const& signing_session) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -529,6 +539,7 @@ template > partial_signature const& signature_part, public_nonce const& signer_nonce, public_key const& signer_key, aggregate_key const& signers_key, session const& signing_session) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -559,6 +570,7 @@ template > [[nodiscard]] boost::system::result> partial_signature_aggregate( session const& signing_session, std::span signature_parts, workspace pointer_space) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -598,6 +610,7 @@ template > template > [[nodiscard]] boost::system::result parse_public_nonce( std::span encoded_nonce) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -624,6 +637,7 @@ template > template > [[nodiscard]] std::array serialize_public_nonce( public_nonce const& parsed_nonce) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -648,6 +662,7 @@ template > template > [[nodiscard]] boost::system::result parse_aggregate_nonce( std::span encoded_nonce) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -674,6 +689,7 @@ template > template > [[nodiscard]] std::array serialize_aggregate_nonce( aggregate_nonce const& parsed_nonce) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -697,6 +713,7 @@ template > template > [[nodiscard]] boost::system::result parse_partial_signature( std::span encoded_signature) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -724,6 +741,7 @@ template > template > [[nodiscard]] std::array serialize_partial_signature( partial_signature const& parsed_signature) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); diff --git a/include/webcpp/crypto/curves/secp256k1/silent_payments.hpp b/include/webcpp/crypto/curves/secp256k1/silent_payments.hpp index 87269d68..d9c5a560 100644 --- a/include/webcpp/crypto/curves/secp256k1/silent_payments.hpp +++ b/include/webcpp/crypto/curves/secp256k1/silent_payments.hpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -299,6 +300,7 @@ template std::span> const* const> taproot_inputs, std::span> const* const> plain_inputs, std::span> output_keys, workspace pointer_space) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); @@ -390,6 +392,7 @@ template template [[nodiscard]] boost::system::result