From 4ea3b62bda412c6ff1f4ac4d84f82df01abe18ec Mon Sep 17 00:00:00 2001 From: R Date: Wed, 25 Nov 2026 05:11:52 -0300 Subject: [PATCH 01/12] hash: every keyed function holds webcpp::secure's timing guard and every wipe is secure's erase_secret; hash's copies are gone and hash depends on secure --- AGENTS.md | 19 +- bench/Jamfile | 10 +- bench/collect.py | 40 ++-- bench/timing.cpp | 38 ++-- build.jam | 3 +- doc/backends.adoc | 4 +- doc/changelog.adoc | 1 + doc/digests.adoc | 4 +- doc/generated/choose.adoc | 2 +- doc/guide.json | 2 +- doc/llms.txt | 4 +- doc/macs.adoc | 10 +- doc/overview.adoc | 7 +- doc/password_hashing.adoc | 4 +- doc/performance.adoc | 10 +- doc/use-cases/privacy.adoc | 2 +- example/Jamfile | 17 +- example/blake2b_mac_sodium.cpp | 4 +- example/hmac_sha256.cpp | 8 +- example/hmac_sodium.cpp | 4 +- example/kmac.cpp | 6 +- example/mac_family.cpp | 6 +- example/password_storage.cpp | 4 +- include/webcpp/hash/argon2.hpp | 16 +- include/webcpp/hash/async.hpp | 8 +- include/webcpp/hash/backends/bearssl.hpp | 22 +-- include/webcpp/hash/backends/blake3.hpp | 4 +- include/webcpp/hash/backends/botan.hpp | 14 +- include/webcpp/hash/backends/hash2.hpp | 42 ++-- include/webcpp/hash/backends/openssl.hpp | 10 +- include/webcpp/hash/backends/sodium.hpp | 26 +-- include/webcpp/hash/backends/webcrypto.hpp | 22 +-- include/webcpp/hash/backends/xkcp.hpp | 20 +- include/webcpp/hash/bcrypt.hpp | 18 +- include/webcpp/hash/blake2b_subkey.hpp | 12 +- include/webcpp/hash/blake3.hpp | 20 +- include/webcpp/hash/detail/engines.hpp | 4 +- include/webcpp/hash/detail/timing.hpp | 220 --------------------- include/webcpp/hash/detail/wipe.hpp | 82 -------- include/webcpp/hash/digest.hpp | 16 +- include/webcpp/hash/hkdf.hpp | 28 +-- include/webcpp/hash/hmac.hpp | 4 +- include/webcpp/hash/kdf_standards.hpp | 20 +- include/webcpp/hash/mac.hpp | 17 +- include/webcpp/hash/pbkdf2.hpp | 12 +- include/webcpp/hash/scrypt.hpp | 12 +- meta/include-boundaries.json | 2 - test/Jamfile | 68 +++---- test/blake2b_mac_sodium_test.cpp | 9 +- test/families_bearssl_test.cpp | 9 +- test/hasher_state_test.cpp | 10 +- test/hkdf_bearssl_test.cpp | 9 +- test/hkdf_pieces_bearssl_test.cpp | 6 +- test/hkdf_pieces_botan_test.cpp | 6 +- test/hkdf_pieces_openssl_test.cpp | 6 +- test/hkdf_pieces_sodium_test.cpp | 6 +- test/support/argon2_reference.hpp | 4 +- test/support/timing_coverage.hpp | 54 ++--- test/support/timing_dit.hpp | 16 +- test/support/timing_seam.hpp | 104 +++++++--- test/support/wipe_record.hpp | 25 ++- test/timing_async_test.cpp | 4 +- test/timing_botan_failure_test.cpp | 12 +- test/timing_coverage_bearssl_test.cpp | 6 +- test/timing_coverage_blake3_test.cpp | 6 +- test/timing_coverage_botan_test.cpp | 4 +- test/timing_coverage_hash2_test.cpp | 4 +- test/timing_coverage_openssl_test.cpp | 6 +- test/timing_coverage_sodium_test.cpp | 4 +- test/timing_coverage_xkcp_test.cpp | 4 +- test/timing_during_test.cpp | 18 +- test/timing_keyed_botan_test.cpp | 4 +- test/timing_keyed_openssl_test.cpp | 10 +- test/timing_keyed_test.cpp | 15 +- test/timing_test.cpp | 114 ++++++----- test/timing_unseamed_test.cpp | 17 +- test/wipe_test.cpp | 29 +-- 77 files changed, 613 insertions(+), 836 deletions(-) delete mode 100644 include/webcpp/hash/detail/timing.hpp delete mode 100644 include/webcpp/hash/detail/wipe.hpp diff --git a/AGENTS.md b/AGENTS.md index 590a5d8..7afa199 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -24,12 +24,17 @@ specific to hash. - A test is declared through `hash-run` in `test/Jamfile`, which runs it with exceptions and without. - Every function that computes on a key, a password or a key derivation's secret input holds - `detail::timing_guard` as its first statement (`timing{engaged}` where the arguments decide - whether the call is keyed); unkeyed digests and Boost.Hash2's types by alias hold none. A new - keyed function, or a keyed cell that `doc/capabilities.json` gains (a MAC, a key derivation, a - password hash, on any backend), holds the guard and gets its call in that backend's - `test/timing_coverage__test.cpp`, which fails until it does. `detail/timing.hpp` is a - copy of webcpp::crypto's `detail/timing.hpp` (hash cannot depend on crypto): a change to one is - made to both, and crypto's `test/timing_hash_interop_test.cpp` runs the two together. + `secure::timing_guard` (webcpp::secure's, ``) as its first + statement (`timing{engaged}` where the arguments decide whether the call is keyed); unkeyed + digests and Boost.Hash2's types by alias hold none. A new keyed function, or a keyed cell that + `doc/capabilities.json` gains (a MAC, a key derivation, a password hash, on any backend), holds + the guard and gets its call in that backend's `test/timing_coverage__test.cpp`, which + fails until it does. The seam support (`test/support/timing_seam.hpp`) counts hash's own guards + by their file, under `include/webcpp/hash/`, in `timing_entries`, and every guard in + `timing_all_entries`. +- Every wipe of a secret or of a failed output is `secure::erase_secret` over a span + (``); a test sees each one through secure's seam + `WEBCPP_SECURE_TEST_WIPE_RECORD` (`test/support/wipe_record.hpp`). hash depends on config and + secure, no other webcpp library. - `include/webcpp/hash/detail/canary.h` is a C header: it is in neither the headers-alone list nor `meta/include-boundaries.json`. diff --git a/bench/Jamfile b/bench/Jamfile index 7574060..b225e2e 100644 --- a/bench/Jamfile +++ b/bench/Jamfile @@ -39,11 +39,11 @@ webcpp.run pbkdf2_sha512_key : pbkdf2_sha512.cpp : $(.requirements) ; exe pbkdf2_sha512 : pbkdf2_sha512.cpp : $(.requirements) release ; explicit pbkdf2_sha512 ; -# The cost of detail::timing_guard, the scope every keyed function runs in -# (doc/performance.adoc). timing_check is the correctness test of the measured -# program, run in every lane of test/: the measurement at 10^3 iterations, its -# line's form and the thread's DIT checked. It needs no backend: -# detail/timing.hpp alone. +# The cost of webcpp::secure::timing_guard, the scope every keyed function runs +# in (doc/performance.adoc). timing_check is the correctness test of the +# measured program, run in every lane of test/: the measurement at 10^3 +# iterations, its line's form and the thread's DIT checked. The guard's cost +# needs no backend: alone. webcpp.run timing_check : timing.cpp : speed ; # The same program at variant=release, the one collect.py --timing measures, diff --git a/bench/collect.py b/bench/collect.py index e29923b..c49c183 100755 --- a/bench/collect.py +++ b/bench/collect.py @@ -32,19 +32,20 @@ or a program that does not run, does. `--render-only` rewrites the page from the data. -The timing guard's cost (detail::timing_guard, which every keyed function holds and which sets -64-bit ARM's DIT where the CPU has it) is `timing --measure`'s one line: whether the guard sets DIT -on this machine, and the nanoseconds a guard adds to a call entered from a thread whose DIT is -clear, from one where it is already set, and a disengaged guard's (an unkeyed call of a function -keyed by its arguments), each the median of five runs of 10^7 guards minus an empty loop's, with the -fastest and slowest run. The data keeps it under "timing_guard" with the date, the machine and the -load (every date of the data is UTC's); the page renders it as a paragraph and a table after the -throughput. A line or a "timing_guard" that is not the form this script writes (an older program's, -a key missing or of another type) is refused with a message naming what is wrong, before anything is -written. `--provisional` marks the guard's figures as provisional, measured on a shared, loaded -machine and to be measured again in a final pass on a quiet one, which the page says; measuring the -guard without it clears the mark. `--exe` alone keeps the data's guard when the data is what this -script writes, and otherwise replaces the data, saying the guard was not kept. +The timing guard's cost (webcpp::secure::timing_guard, secure's guard, which every keyed function +holds and which sets 64-bit ARM's DIT where the CPU has it) is `timing --measure`'s one line: +whether the guard sets DIT on this machine, and the nanoseconds a guard adds to a call entered from +a thread whose DIT is clear, from one where it is already set, and a disengaged guard's (an unkeyed +call of a function keyed by its arguments), each the median of five runs of 10^7 guards minus an +empty loop's, with the fastest and slowest run. The data keeps it under "timing_guard" with the +date, the machine and the load (every date of the data is UTC's); the page renders it as a +paragraph and a table after the throughput. A line or a "timing_guard" that is not the form this +script writes (an older program's, a key missing or of another type) is refused with a message +naming what is wrong, before anything is written. `--provisional` marks the guard's figures as +provisional, measured on a shared, loaded machine and to be measured again in a final pass on a +quiet one, which the page says; measuring the guard without it clears the mark. `--exe` alone +keeps the data's guard when the data is what this script writes, and otherwise replaces the data, +saying the guard was not kept. """ import argparse @@ -283,7 +284,8 @@ def measure_timing(exe, provisional): f"collect.py reads: {found} is wrong; build bench/Jamfile's timing again from " "this tree") return { - "measurement": "the nanoseconds detail::timing_guard adds to a call, bench/timing.cpp", + "measurement": "the nanoseconds webcpp::secure::timing_guard adds to a call, " + "bench/timing.cpp", "date": utc_today(), "machine": { "cpu": sysctl("machdep.cpu.brand_string") or platform.processor() or platform.machine(), @@ -330,11 +332,11 @@ def render_timing(timing): "=== The timing guard's cost", "", "Every keyed function (a MAC, a key derivation, a password hash, a keyed mode of", - "BLAKE2b or BLAKE3) runs inside `detail::timing_guard`, which on 64-bit ARM under", - "macOS, iOS, Linux and Android, where the CPU has FEAT_DIT, sets DIT (data-independent", - "timing) for the call and restores the thread's state after it", - "(<>): one `mrs`, and where DIT was clear, two `msr` each", - "followed by an `isb`. Elsewhere (x86, WebAssembly, Windows on ARM64) it executes no", + "BLAKE2b or BLAKE3) runs inside `webcpp::secure::timing_guard`, webcpp::secure's", + "guard, which on 64-bit ARM under macOS, iOS, Linux, Android and Windows, where the", + "CPU has FEAT_DIT, sets DIT (data-independent timing) for the call and restores the", + "thread's state after it (<>): one `mrs`, and where DIT was clear,", + "two `msr` each followed by an `isb`. Elsewhere (x86, WebAssembly) it executes no", "instruction. A function keyed by its arguments, called without a key, holds a", "disengaged guard, which reads its flag and does nothing else.", "", diff --git a/bench/timing.cpp b/bench/timing.cpp index 802c49e..157b12a 100644 --- a/bench/timing.cpp +++ b/bench/timing.cpp @@ -4,10 +4,10 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// The cost of detail::timing_guard, the scope every keyed public function of hash runs in (a MAC, -// a key derivation, a password hash, a keyed mode of BLAKE2b or BLAKE3), which sets 64-bit ARM's -// DIT (data-independent timing) bit where the CPU has it and restores the thread's state after -// it. bench/timing.cpp of webcpp::crypto measures crypto's copy of the guard the same way. +// The cost of webcpp::secure::timing_guard, secure's guard, the scope every keyed public function +// of hash runs in (a MAC, a key derivation, a password hash, a keyed mode of BLAKE2b or BLAKE3), +// which sets 64-bit ARM's DIT (data-independent timing) bit where the CPU has it and restores the +// thread's state after it. // // Run as `timing --measure`, it times, in one process, 10^7 iterations each of an empty loop body, // of a guard entered from a thread whose DIT is clear (it sets DIT and clears it again), of a @@ -26,7 +26,7 @@ // what the library reads. A measurement never fails on its time; bench/collect.py --timing runs // it and writes doc/performance.adoc. -#include +#include #include #include @@ -39,7 +39,7 @@ #include #include -namespace detail = webcpp::hash::detail; +namespace secure = webcpp::secure; namespace { @@ -73,8 +73,8 @@ constexpr std::size_t runs = 5; // Whether the guard sets DIT on this target and CPU. bool guard_sets_dit() noexcept { -#ifdef WEBCPP_HASH_DETAIL_TIMING_COVERED - return detail::cpu_has_dit(); +#ifdef WEBCPP_SECURE_TIMING_COVERED + return secure::cpu_has_dit(); #else return false; #endif @@ -82,9 +82,9 @@ bool guard_sets_dit() noexcept { // Sets or clears this thread's DIT where the guard would, as a program that asks for it does. void force_dit([[maybe_unused]] bool set) noexcept { -#ifdef WEBCPP_HASH_DETAIL_TIMING_COVERED - if (detail::cpu_has_dit()) { - detail::write_dit(set ? detail::dit_bit : 0); +#ifdef WEBCPP_SECURE_TIMING_COVERED + if (secure::cpu_has_dit()) { + secure::detail::write_dit(set ? secure::detail::dit_bit : 0); } #endif } @@ -132,7 +132,7 @@ result measure(std::uint64_t iterations) { std::array set{}; std::array disengaged{}; // A process that starts with DIT set would measure the set part twice. - bool restored = !detail::dit_is_set(); + bool restored = !secure::dit_is_set(); // Written by every iteration of every part, so that no loop is removed or merged. std::uint64_t volatile sink = 0; // The disengaged guard's flag, read at each iteration as a keyed function reads its member. @@ -140,23 +140,23 @@ result measure(std::uint64_t iterations) { for (std::size_t run = 0; run < runs; ++run) { empty[run] = nanoseconds_per_iteration(iterations, [&sink](std::uint64_t i) { sink = i; }); clear[run] = nanoseconds_per_iteration(iterations, [&sink](std::uint64_t i) { - detail::timing_guard const guard; + secure::timing_guard const guard; sink = i; }); - restored = restored && !detail::dit_is_set(); + restored = restored && !secure::dit_is_set(); force_dit(true); set[run] = nanoseconds_per_iteration(iterations, [&sink](std::uint64_t i) { - detail::timing_guard const guard; + secure::timing_guard const guard; sink = i; }); - restored = restored && detail::dit_is_set() == guard_sets_dit(); + restored = restored && secure::dit_is_set() == guard_sets_dit(); force_dit(false); - restored = restored && !detail::dit_is_set(); + restored = restored && !secure::dit_is_set(); disengaged[run] = nanoseconds_per_iteration(iterations, [&sink, &keyed](std::uint64_t i) { - detail::timing_guard const guard{keyed}; + secure::timing_guard const guard{keyed}; sink = i; }); - restored = restored && !detail::dit_is_set(); + restored = restored && !secure::dit_is_set(); } double const loop = summarise(empty, 0).median; return { diff --git a/build.jam b/build.jam index db83a0d..c9e1ad0 100644 --- a/build.jam +++ b/build.jam @@ -16,7 +16,8 @@ import webcpp-target ; project /webcpp/hash ; # tag::hash[] -alias hash : : : : include /boost//headers /webcpp/config//config ; +alias hash : : : : include /boost//headers /webcpp/config//config + /webcpp/secure//secure ; # end::hash[] # tag::openssl[] diff --git a/doc/backends.adoc b/doc/backends.adoc index 219814d..5513cc6 100644 --- a/doc/backends.adoc +++ b/doc/backends.adoc @@ -1929,7 +1929,7 @@ states about its constant time. The backends: [#hash-backends-dit] === Data-independent timing (DIT) -64-bit ARM's DIT (data-independent timing) bit, PSTATE.DIT, makes the instructions Arm lists take a time independent of their data, and Apple requires it of constant-time code (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, enable DIT for constant-time cryptographic operations]). None of the libraries hash calls sets it, so hash does: every function that computes on a key, a password or a key derivation's secret input runs its whole body inside `detail::timing_guard`, on every synchronous backend, which on 64-bit ARM under macOS, iOS, Linux and Android, where the CPU has FEAT_DIT (`hw.optional.arm.FEAT_DIT` on Apple's systems, `HWCAP_DIT` on Linux), sets DIT for the call and returns with the thread's DIT as it found it: clear when it was clear, set when the program set it itself (Apple's `timingsafe_enable_if_supported`) or an outer call did. An error and an exception leave it as found too, and DIT is a thread's own state, so a call never changes another thread's. A program does nothing. Elsewhere the guard executes no instruction: on x86 the counterpart, DOITM, is a register only the kernel writes; on WebAssembly the engine decides; on Windows on ARM64 no documented way to ask whether the CPU has FEAT_DIT was found, and DIT's instructions trap on a CPU without it. +64-bit ARM's DIT (data-independent timing) bit, PSTATE.DIT, makes the instructions Arm lists take a time independent of their data, and Apple requires it of constant-time code (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, enable DIT for constant-time cryptographic operations]). None of the libraries hash calls sets it, so hash does: every function that computes on a key, a password or a key derivation's secret input runs its whole body inside `webcpp::secure::timing_guard`, webcpp::secure's guard, on every synchronous backend, which on 64-bit ARM under macOS, iOS, Linux, Android and Windows, where the CPU has FEAT_DIT (`hw.optional.arm.FEAT_DIT` on Apple's systems, `HWCAP_DIT` on Linux, one read of the register under structured exception handling on Windows), sets DIT for the call and returns with the thread's DIT as it found it: clear when it was clear, set when the program set it itself (Apple's `timingsafe_enable_if_supported`) or an outer call did. An error and an exception leave it as found too, and DIT is a thread's own state, so a call never changes another thread's. A program does nothing. Elsewhere the guard executes no instruction: on x86 the counterpart, DOITM, is a register only the kernel writes; on WebAssembly the engine decides. The functions that set DIT, on every synchronous backend of their cells: @@ -1944,7 +1944,7 @@ The functions that do not: * Boost.Hash2's types by alias, `siphash_32`, `siphash_64`, `hmac`, `hmac_md5_128`, `hmac_sha1_160`, `hmac_sha2_224`, `hmac_sha2_256`, `hmac_sha2_384`, `hmac_sha2_512`, `hmac_sha2_512_224`, `hmac_sha2_512_256`, `hmac_sha3_224`, `hmac_sha3_256`, `hmac_sha3_384`, `hmac_sha3_512`, `hmac_ripemd_128`, `hmac_ripemd_160`, `hmac_blake2b_512` and `hmac_blake2s_256`: they are Boost.Hash2's own classes, and a guard inside them would need a type of hash's in place of the alias. An HMAC with DIT is `mac>`, which runs the same Boost.Hash2 code on `backends::hash2` inside its guard, and a SipHash with DIT is `mac`; a program that calls `hmac` or `siphash_64` directly sets DIT itself. HKDF and PBKDF2 on `backends::hash2` run Boost.Hash2's `hmac` inside their own guard. * The initiating functions of `async.hpp`: they check sizes and post, and the synchronous function they call sets DIT on the thread that computes, a thread of the work executor's pool for a heavy function, the caller's for a cheap one (<>). On `backends::webcrypto` the browser's JavaScript engine computes, and nothing is added. -A coverage test per backend (`test/timing_coverage__test.cpp`) reads this table and fails when a keyed cell of its backend has no call, when a call enters no guard, when the first guard it enters is not its own function's, or when it leaves DIT otherwise than it found it, from a thread with DIT clear and from one that set it; it checks that the unkeyed functions enter none. `test/timing_during_test.cpp` reads DIT while HKDF, PBKDF2 and `mac` compute, through a hash that records it at each update, on the calling thread and on an `async_pbkdf2` pool's. The guard is hash's own copy of webcpp::crypto's, which depends on hash: a change to one is a change to the other, and crypto's test `timing_hash_interop_test` runs both nested, in either order. +A coverage test per backend (`test/timing_coverage__test.cpp`) reads this table and fails when a keyed cell of its backend has no call, when a call enters no guard, when the first guard it enters is not its own function's, or when it leaves DIT otherwise than it found it, from a thread with DIT clear and from one that set it; it checks that the unkeyed functions enter none. `test/timing_during_test.cpp` reads DIT while HKDF, PBKDF2 and `mac` compute, through a hash that records it at each update, on the calling thread and on an `async_pbkdf2` pool's. The guard is secure's, shared with webcpp::crypto and webcpp::codecs: secure's `timing_guard_nesting_test` nests guards as two libraries do. What the guard adds to a call, measured from a release build, is in <>. diff --git a/doc/changelog.adoc b/doc/changelog.adoc index fa1b14f..54c2128 100644 --- a/doc/changelog.adoc +++ b/doc/changelog.adoc @@ -18,6 +18,7 @@ of every version are on webcpp's index page. // tag::unreleased[] * *Breaking change:* Changed the public names: hkdf's input_key_material, pseudorandom_key, application_info, output_key_material and salt, hkdf_max_application_info_size, scrypt_parameters' cost, block_size and parallelism (from n, r, p) and argon2_parameters::memory_kibibytes. * *Breaking change:* Changed HKDF's overloads: `+hkdf(out, {}, {}, {})+`, `+async_hkdf(ex, out, salt, {}, {}, token)+` and `+{vec}+` inputs are ambiguous; pass the vector itself, an empty span or `+byte_pieces{...}+`. +* Changed every keyed function to hold webcpp::secure's timing guard, which also sets DIT on Windows on ARM64, and every wipe to secure's erase_secret; hash depends on secure. * Added the library: Boost.Hash2's algorithms and HMAC under webcpp::hash's names, keccak_256 (Ethereum's Keccak-256 over XKCP), blake3 (keyed, key derivation, extendable output), every result `+[[nodiscard]]+`. * Added HKDF (RFC 5869) and PBKDF2 (RFC 8018) on hash2, argon2id (RFC 9106), scrypt and argon2's Argon2i and Argon2id on sodium; keyed calls set DIT on arm64 Apple and Linux. * Added HKDF over byte pieces (hkdf_extract, hkdf_expand, hkdf and their async forms), with no cap; on the synchronous backends hash makes no buffer, heap or copy of the inputs. diff --git a/doc/digests.adoc b/doc/digests.adoc index c669a4a..09ff98d 100644 --- a/doc/digests.adoc +++ b/doc/digests.adoc @@ -79,7 +79,7 @@ guesses per second on a graphics card. Passwords go through Argon2id (<>). * *A digest is compared as a public value.* Comparing two digests with `==` is fine when both are public, a file's expected digest for example; a tag that -depends on a secret is compared with `webcpp::crypto::constant_time_equal` +depends on a secret is compared with `webcpp::secure::constant_time_equal` (<>). * *Other backends, by name.* `backends::bearssl`, `backends::openssl` and `backends::botan` compute SHA-2 too, never as a default: a call names one @@ -439,7 +439,7 @@ the output given to `digest_of` or `finish` has exactly that size same way on every side, since the library does not guess a padding. * *A key is a secret, the salt and personalisation are not.* For a MAC, use a random key of 16 to 64 bytes from `webcpp::crypto::random_device`, compare -tags with `webcpp::crypto::constant_time_equal`, and prefer +tags with `webcpp::secure::constant_time_equal`, and prefer `mac` (<>); a key of 0 bytes is the unkeyed hash. With a key, `digest_of` and each of `make`, `update` and `finish` of a `hasher` set DIT around the call (<>); without one, none diff --git a/doc/generated/choose.adoc b/doc/generated/choose.adoc index b85bc19..0821415 100644 --- a/doc/generated/choose.adoc +++ b/doc/generated/choose.adoc @@ -25,7 +25,7 @@ | `example/content_address.cpp` | prove a message comes from someone who holds a shared secret key (an API request, a cookie, a webhook) -| <> +| <> | `hash2`: `/webcpp/hash//hash` | none: one backend | `example/hmac_sha256.cpp` diff --git a/doc/guide.json b/doc/guide.json index 5685ab0..2944537 100644 --- a/doc/guide.json +++ b/doc/guide.json @@ -1702,7 +1702,7 @@ "use_cases": [ {"need": "check that a file or message was not changed by accident or on the way", "use": "sha2_256 (or blake3 for large data)", "function": "sha2_256", "backend": "none: one backend", "example": "sha256.cpp", "anchor": "hash-sha2"}, {"need": "name content by what it holds (a file-sharing app, a cache, a store of blobs)", "use": "blake3", "function": "blake3", "backend": "link /third_party//blake3", "example": "content_address.cpp", "anchor": "hash-p2p-content"}, - {"need": "prove a message comes from someone who holds a shared secret key (an API request, a cookie, a webhook)", "use": "hmac_sha2_256, checked with webcpp::crypto::constant_time_equal", "function": "hmac_sha2_256", "backend": "none: one backend", "example": "hmac_sha256.cpp", "anchor": "hash-hmac"}, + {"need": "prove a message comes from someone who holds a shared secret key (an API request, a cookie, a webhook)", "use": "hmac_sha2_256, checked with webcpp::secure::constant_time_equal", "function": "hmac_sha2_256", "backend": "none: one backend", "example": "hmac_sha256.cpp", "anchor": "hash-hmac"}, {"need": "store users' passwords for a login", "use": "argon2id with argon2id_interactive or stronger, a random 16-byte salt per user", "function": "argon2id", "backend": "link /third_party//sodium", "example": "password_storage.cpp", "anchor": "hash-privacy-passwords"}, {"need": "turn a password into an encryption key (a password manager, an encrypted backup)", "use": "argon2id with argon2id_moderate or argon2id_sensitive, then hkdf for each purpose", "function": "argon2id", "backend": "link /third_party//sodium", "example": "argon2id.cpp", "anchor": "hash-argon2id"}, {"need": "check or make the bcrypt strings a system already stores, or read and write OpenSSH's encrypted private keys", "use": "bcrypt and bcrypt_verify at a cost of 10 or more (bcrypt_pbkdf for OpenSSH's keys); argon2id for a new design", "function": "bcrypt", "backend": "name backends::botan and link /third_party//botan, with exceptions", "example": "bcrypt.cpp", "anchor": "hash-bcrypt"}, diff --git a/doc/llms.txt b/doc/llms.txt index d8c99ca..47bb02e 100644 --- a/doc/llms.txt +++ b/doc/llms.txt @@ -4,7 +4,7 @@ Primitives only: protocol steps (BIP39, hash160, EIP-55, Nostr ids, NIP-44) are what a domain library builds from these functions; the guide shows them as examples, never as this library's API. -Timing: every MAC, key derivation and password hash, and BLAKE2b's and BLAKE3's keyed modes, set DIT (data-independent timing) around each call on 64-bit ARM under Apple's systems and Linux where the CPU has FEAT_DIT, on every synchronous backend, and leave the thread's DIT as they found it; unkeyed digests and Boost.Hash2's hmac by alias do not, so an HMAC with DIT is mac>. [Section](https://webcpporg.github.io/libs/hash/#hash-backends-dit) +Timing: every MAC, key derivation and password hash, and BLAKE2b's and BLAKE3's keyed modes, set DIT (data-independent timing) around each call on 64-bit ARM under Apple's systems, Linux and Windows where the CPU has FEAT_DIT, on every synchronous backend, and leave the thread's DIT as they found it; unkeyed digests and Boost.Hash2's hmac by alias do not, so an HMAC with DIT is mac>. [Section](https://webcpporg.github.io/libs/hash/#hash-backends-dit) - [The guide](https://webcpporg.github.io/libs/hash/): every chapter, example and table - [Choose by use case](https://webcpporg.github.io/libs/hash/#hash-choose): a decision table, need to function, backend and example @@ -171,7 +171,7 @@ Fast hashes for hash tables, checksums against accidents and identifiers that no - I need to check that a file or message was not changed by accident or on the way: sha2_256 (or blake3 for large data); backend: none: one backend; example libs/hash/example/sha256.cpp. [Section](https://webcpporg.github.io/libs/hash/#hash-sha2) - I need to name content by what it holds (a file-sharing app, a cache, a store of blobs): blake3; backend: link /third_party//blake3; example libs/hash/example/content_address.cpp. [Section](https://webcpporg.github.io/libs/hash/#hash-p2p-content) -- I need to prove a message comes from someone who holds a shared secret key (an API request, a cookie, a webhook): hmac_sha2_256, checked with webcpp::crypto::constant_time_equal; backend: none: one backend; example libs/hash/example/hmac_sha256.cpp. [Section](https://webcpporg.github.io/libs/hash/#hash-hmac) +- I need to prove a message comes from someone who holds a shared secret key (an API request, a cookie, a webhook): hmac_sha2_256, checked with webcpp::secure::constant_time_equal; backend: none: one backend; example libs/hash/example/hmac_sha256.cpp. [Section](https://webcpporg.github.io/libs/hash/#hash-hmac) - I need to store users' passwords for a login: argon2id with argon2id_interactive or stronger, a random 16-byte salt per user; backend: link /third_party//sodium; example libs/hash/example/password_storage.cpp. [Section](https://webcpporg.github.io/libs/hash/#hash-privacy-passwords) - I need to turn a password into an encryption key (a password manager, an encrypted backup): argon2id with argon2id_moderate or argon2id_sensitive, then hkdf for each purpose; backend: link /third_party//sodium; example libs/hash/example/argon2id.cpp. [Section](https://webcpporg.github.io/libs/hash/#hash-argon2id) - I need to check or make the bcrypt strings a system already stores, or read and write OpenSSH's encrypted private keys: bcrypt and bcrypt_verify at a cost of 10 or more (bcrypt_pbkdf for OpenSSH's keys); argon2id for a new design; backend: name backends::botan and link /third_party//botan, with exceptions; example libs/hash/example/bcrypt.cpp. [Section](https://webcpporg.github.io/libs/hash/#hash-bcrypt) diff --git a/doc/macs.adoc b/doc/macs.adoc index 49a4361..a48a42a 100644 --- a/doc/macs.adoc +++ b/doc/macs.adoc @@ -53,7 +53,7 @@ include::{examples}/hmac_sha256.expected[] *Pitfalls.* -* *Compare tags with `webcpp::crypto::constant_time_equal`, never with `==`.* +* *Compare tags with `webcpp::secure::constant_time_equal`, never with `==`.* The digest's `operator==` is `memcmp`, which returns at the first byte that differs; an attacker who sends guesses and times the answers learns how many leading bytes are right and builds a valid tag one byte at a time, in a few @@ -154,7 +154,7 @@ include::{examples}/kmac.expected[] *Pitfalls.* -* *Compare with `webcpp::crypto::constant_time_equal`.* As for every tag +* *Compare with `webcpp::secure::constant_time_equal`.* As for every tag (<>). * *The key sets the strength.* SP 800-185 (section 8.4.1) forbids a key shorter than the security the application needs: 16 bytes for KMAC128, 32 @@ -197,7 +197,7 @@ include::{examples}/mac_family.expected[] *Pitfalls.* -* *Compare with `webcpp::crypto::constant_time_equal`.* As for every tag +* *Compare with `webcpp::secure::constant_time_equal`.* As for every tag (<>). * *A cut tag is a weaker tag.* Ten bytes, 80 bits, is the shortest `mac` gives; keep the whole digest unless a protocol fixes a shorter tag. @@ -276,7 +276,7 @@ include::{examples}/hmac_sodium.expected[] *Pitfalls.* * *The pitfalls of <> hold:* compare tags with -`webcpp::crypto::constant_time_equal`, and use a random key, not a password. +`webcpp::secure::constant_time_equal`, and use a random key, not a password. * *"HMAC-SHA-512-256" has two meanings.* A program that must agree with libsodium's `crypto_auth_hmacsha512256` names `macs::hmac` and a 32-byte output; one that follows a standard naming HMAC over @@ -309,7 +309,7 @@ include::{examples}/blake2b_mac_sodium.expected[] *Pitfalls.* -* *Compare with `webcpp::crypto::constant_time_equal`.* As for every tag +* *Compare with `webcpp::secure::constant_time_equal`.* As for every tag (<>). * *The length is a parameter, not a cut.* The tag's size goes into the hash: a 16-byte tag is not the first 16 bytes of a 32-byte one, and the receiver diff --git a/doc/overview.adoc b/doc/overview.adoc index 110de45..823f38b 100644 --- a/doc/overview.adoc +++ b/doc/overview.adoc @@ -146,9 +146,10 @@ include::{examples}/hex.hpp[tag=hex,indent=0] hash is not a new implementation of a primitive: every permutation, compression function and hash is Boost.Hash2's or a C library's, and the two constructions webcpp writes over them are listed in <>. It is -not webcpp::crypto either: encryption, signatures, randomness and -constant-time comparison stay there, and `webcpp::crypto::constant_time_equal` -is what compares a MAC. It holds primitives only: what a protocol defines on top +not webcpp::crypto either: encryption, signatures and randomness stay there. +Constant-time comparison is webcpp::secure's, which hash depends on: +`webcpp::secure::constant_time_equal` is what compares a MAC. It holds +primitives only: what a protocol defines on top of them (Bitcoin's BIP39 seeds and hash160, Ethereum's EIP-55 addresses, Nostr's event ids and NIP-44's keys) belongs to domain libraries, webcpp::bitcoin, webcpp::ethereum and webcpp::nostr, which build them from these functions. The diff --git a/doc/password_hashing.adoc b/doc/password_hashing.adoc index df09251..3e5b1ba 100644 --- a/doc/password_hashing.adoc +++ b/doc/password_hashing.adoc @@ -93,7 +93,7 @@ and a browser tab may not get a gigabyte: measure on the target. same memory, passes and salt; a program that raises its costs later must know which costs each stored tag used. hash has no `$argon2id$` string format: the program stores the three values in its own record (<>). -* *Compare the tag with `webcpp::crypto::constant_time_equal`.* +* *Compare the tag with `webcpp::secure::constant_time_equal`.* * *The memory is the backend's.* libsodium allocates the matrix with `mmap` where there is one, else from the C heap; OpenSSL from its own allocator, the C heap; both outside any allocator of the program (<>). @@ -251,7 +251,7 @@ program can spend, time one hash, then trade. *Pitfalls.* * *A random salt per password, the costs stored with the result,* and the -result compared with `webcpp::crypto::constant_time_equal`, as for Argon2id. +result compared with `webcpp::secure::constant_time_equal`, as for Argon2id. * *libsodium's own limit:* N at most 4,294,967,295; hash refuses a larger one with `invalid_parameters` on `backends::sodium`. * *OpenSSL's own limits:* r p below 2^24^ (`invalid_parameters`), and a diff --git a/doc/performance.adoc b/doc/performance.adoc index 1d1543c..a3b6d9f 100644 --- a/doc/performance.adoc +++ b/doc/performance.adoc @@ -124,11 +124,11 @@ the library. === The timing guard's cost Every keyed function (a MAC, a key derivation, a password hash, a keyed mode of -BLAKE2b or BLAKE3) runs inside `detail::timing_guard`, which on 64-bit ARM under -macOS, iOS, Linux and Android, where the CPU has FEAT_DIT, sets DIT (data-independent -timing) for the call and restores the thread's state after it -(<>): one `mrs`, and where DIT was clear, two `msr` each -followed by an `isb`. Elsewhere (x86, WebAssembly, Windows on ARM64) it executes no +BLAKE2b or BLAKE3) runs inside `webcpp::secure::timing_guard`, webcpp::secure's +guard, which on 64-bit ARM under macOS, iOS, Linux, Android and Windows, where the +CPU has FEAT_DIT, sets DIT (data-independent timing) for the call and restores the +thread's state after it (<>): one `mrs`, and where DIT was clear, +two `msr` each followed by an `isb`. Elsewhere (x86, WebAssembly) it executes no instruction. A function keyed by its arguments, called without a key, holds a disengaged guard, which reads its flag and does nothing else. diff --git a/doc/use-cases/privacy.adoc b/doc/use-cases/privacy.adoc index 8ff7bd0..8314752 100644 --- a/doc/use-cases/privacy.adoc +++ b/doc/use-cases/privacy.adoc @@ -44,7 +44,7 @@ one guess tests every user; tables of precomputed hashes of common passwords work at once. * *With a fast hash* (SHA-256, MD5) instead of Argon2id, a graphics card tests billions of guesses per second: most human passwords fall in hours. -* *With `==`* instead of `webcpp::crypto::constant_time_equal`, the time of the +* *With `==`* instead of `webcpp::secure::constant_time_equal`, the time of the comparison tells an attacker at the network how much of a guessed tag is right. * *Without the costs in the record,* the service cannot raise them later: the record must say which memory and passes made its tag, so that a login can diff --git a/example/Jamfile b/example/Jamfile index 1c719b4..4410c3b 100644 --- a/example/Jamfile +++ b/example/Jamfile @@ -40,18 +40,15 @@ webcpp.example whirlpool.cpp : $(.botan) : native emscripten ; webcpp.example skein.cpp : $(.botan) : native emscripten ; # Message authentication: a MAC is compared with -# webcpp::crypto::constant_time_equal, and a hash table's key comes from -# webcpp::crypto::random_device. -webcpp.example hmac_sha256.cpp : /webcpp/crypto//crypto ; +# webcpp::secure::constant_time_equal (secure, which hash depends on), and a +# hash table's key comes from webcpp::crypto::random_device. +webcpp.example hmac_sha256.cpp ; webcpp.example siphash.cpp : /webcpp/crypto//crypto ; -webcpp.example mac_family.cpp : /webcpp/crypto//crypto ; +webcpp.example mac_family.cpp ; webcpp.example siphash_sodium.cpp : /third_party//sodium ; -webcpp.example hmac_sodium.cpp : /third_party//sodium - /webcpp/crypto//crypto ; -webcpp.example blake2b_mac_sodium.cpp : /third_party//sodium - /webcpp/crypto//crypto ; -webcpp.example kmac.cpp : /webcpp/crypto//crypto - /third_party//xkcp ; +webcpp.example hmac_sodium.cpp : /third_party//sodium ; +webcpp.example blake2b_mac_sodium.cpp : /third_party//sodium ; +webcpp.example kmac.cpp : /third_party//xkcp ; webcpp.example macs_openssl.cpp : /webcpp/hash//openssl : native wasip2 wasip3 ; webcpp.example macs_botan.cpp : $(.botan) : native emscripten ; diff --git a/example/blake2b_mac_sodium.cpp b/example/blake2b_mac_sodium.cpp index c9ba138..20abbf3 100644 --- a/example/blake2b_mac_sodium.cpp +++ b/example/blake2b_mac_sodium.cpp @@ -12,8 +12,8 @@ // tag::example[] #include "hex.hpp" -#include #include +#include #include #include @@ -42,7 +42,7 @@ int main() { // The receiver computes the tag again and compares it in constant time. std::array again{}; bool const genuine = hash::mac(again, key, message) && - webcpp::crypto::constant_time_equal(again, tag); + webcpp::secure::constant_time_equal(again, tag); std::cout << "genuine: " << (genuine ? "yes" : "no") << '\n'; // The tag's size is a parameter of the hash: a 16-byte tag is not the 32-byte one cut. diff --git a/example/hmac_sha256.cpp b/example/hmac_sha256.cpp index dc1394e..7ec3211 100644 --- a/example/hmac_sha256.cpp +++ b/example/hmac_sha256.cpp @@ -10,8 +10,8 @@ // tag::example[] #include "hex.hpp" -#include #include +#include #include #include @@ -42,13 +42,13 @@ int main() { // The receiver computes the tag again from the message it got, and compares it with the one // it got. Never with ==: memcmp stops at the first different byte, so an attacker who // measures how long the answer takes learns how many leading bytes of a guess are right, - // and guesses a valid tag one byte at a time. crypto::constant_time_equal reads every byte. - bool const genuine = webcpp::crypto::constant_time_equal(tag_of(key, message), tag); + // and guesses a valid tag one byte at a time. secure::constant_time_equal reads every byte. + bool const genuine = webcpp::secure::constant_time_equal(tag_of(key, message), tag); std::cout << "genuine: " << (genuine ? "yes" : "no") << '\n'; // One byte of the message changed in transit: the tags differ. bool const forged = - webcpp::crypto::constant_time_equal(tag_of(key, "what do ya want for nothing!"), tag); + webcpp::secure::constant_time_equal(tag_of(key, "what do ya want for nothing!"), tag); std::cout << "tampered: " << (forged ? "accepted" : "refused") << '\n'; if (!genuine || forged) { return 1; diff --git a/example/hmac_sodium.cpp b/example/hmac_sodium.cpp index 3c74973..09472ad 100644 --- a/example/hmac_sodium.cpp +++ b/example/hmac_sodium.cpp @@ -12,8 +12,8 @@ // tag::example[] #include "hex.hpp" -#include #include +#include #include #include @@ -42,7 +42,7 @@ int main() { // The receiver computes the tag again and compares it in constant time. std::array again{}; bool const genuine = hash::mac, sodium>(again, key, message) && - webcpp::crypto::constant_time_equal(again, tag); + webcpp::secure::constant_time_equal(again, tag); std::cout << "genuine: " << (genuine ? "yes" : "no") << '\n'; // libsodium's crypto_auth_hmacsha512256 is HMAC-SHA-512 cut to 32 bytes: the same tag, with a diff --git a/example/kmac.cpp b/example/kmac.cpp index 63900d3..8b30fb4 100644 --- a/example/kmac.cpp +++ b/example/kmac.cpp @@ -6,14 +6,14 @@ // KMAC128 with mac, on its default backend, XKCP, against NIST's KMAC sample #2 (the key 0x40 to // 0x5F, the message 00 01 02 03, S = "My Tagged Application"); the receiver's check with -// webcpp::crypto::constant_time_equal; and KMACXOF128 of the same inputs, another tag, NIST's +// webcpp::secure::constant_time_equal; and KMACXOF128 of the same inputs, another tag, NIST's // KMACXOF sample #2. // tag::example[] #include "hex.hpp" -#include #include +#include #include #include @@ -40,7 +40,7 @@ int main() { std::array expected{}; bool const genuine = hash::mac(expected, key, message, {.customisation = application}).has_value() && - webcpp::crypto::constant_time_equal(expected, tag); + webcpp::secure::constant_time_equal(expected, tag); std::cout << "genuine: " << (genuine ? "yes" : "no") << '\n'; // KMACXOF128: the same inputs, another tag. diff --git a/example/mac_family.cpp b/example/mac_family.cpp index 88f7d0b..69fd695 100644 --- a/example/mac_family.cpp +++ b/example/mac_family.cpp @@ -5,14 +5,14 @@ // https://www.boost.org/LICENSE_1_0.txt) // The MAC family: HMAC-SHA-256 with mac, in one call, against RFC 4231's test case 2; the -// receiver's check with webcpp::crypto::constant_time_equal; and a tag cut to 16 bytes, RFC 4231's +// receiver's check with webcpp::secure::constant_time_equal; and a tag cut to 16 bytes, RFC 4231's // test case 5. // tag::example[] #include "hex.hpp" -#include #include +#include #include #include @@ -37,7 +37,7 @@ int main() { // The receiver computes the tag again and compares it in constant time, never with ==. std::array expected{}; bool const genuine = hash::mac(expected, key, message).has_value() && - webcpp::crypto::constant_time_equal(expected, tag); + webcpp::secure::constant_time_equal(expected, tag); std::cout << "genuine: " << (genuine ? "yes" : "no") << '\n'; // A tag cut to 16 bytes is the digest's first 16 (RFC 2104, section 5); shorter than 10 is diff --git a/example/password_storage.cpp b/example/password_storage.cpp index 0bae690..974a09b 100644 --- a/example/password_storage.cpp +++ b/example/password_storage.cpp @@ -12,9 +12,9 @@ // tag::example[] #include "hex.hpp" -#include #include #include +#include #include @@ -61,7 +61,7 @@ boost::system::result check(credential const& stored, std::string_view pas if (!done) { return done.error(); } - return webcpp::crypto::constant_time_equal(candidate, stored.tag); + return webcpp::secure::constant_time_equal(candidate, stored.tag); } } // namespace diff --git a/include/webcpp/hash/argon2.hpp b/include/webcpp/hash/argon2.hpp index a52b1a3..81440ad 100644 --- a/include/webcpp/hash/argon2.hpp +++ b/include/webcpp/hash/argon2.hpp @@ -12,8 +12,8 @@ of RFC 9106, on the backends that have each (Argon2i on libsodium with one lane; every variant and every parameter on OpenSSL; every variant on Botan, at most 128 lanes and 8 GiB). - Every call holds the data-independent-timing guard (`detail::timing_guard`): where the CPU has - DIT, the call sets it and leaves the thread's state as it found it. + Every call holds the data-independent-timing guard (`webcpp::secure::timing_guard`, secure's + guard): where the CPU has DIT, the call sets it and leaves the thread's state as it found it. Every guard runs before any work, and a guard's refusal leaves the output as it was; a backend's failure zeroes it. The backend allocates the memory the costs name. @@ -39,10 +39,10 @@ #include #include -#include -#include #include #include +#include +#include #include @@ -164,7 +164,7 @@ template > std::span password_bytes, std::span salt_bytes, argon2id_parameters const& cost_parameters) { - detail::timing_guard const timing; + secure::timing_guard const timing; auto guarded = detail::argon2id_guards(output_bytes.size(), password_bytes.size(), salt_bytes.size(), cost_parameters); if (!guarded) { @@ -173,7 +173,7 @@ template > auto derive_result = detail::argon2id_engine::derive( output_bytes, password_bytes, salt_bytes, cost_parameters.passes, cost_parameters.memory); if (!derive_result) { - detail::wipe(output_bytes); + secure::erase_secret(output_bytes); } return derive_result; } @@ -296,7 +296,7 @@ template > std::span password, std::span salt, argon2_parameters const& parameters) { - detail::timing_guard const timing; + secure::timing_guard const timing; auto guarded = detail::argon2_guards(output.size(), password.size(), salt.size(), parameters); if (!guarded) { @@ -305,7 +305,7 @@ template > auto derived = detail::argon2_engine::derive(output, password, salt, parameters); if (!derived) { - detail::wipe(output); + secure::erase_secret(output); } return derived; } diff --git a/include/webcpp/hash/async.hpp b/include/webcpp/hash/async.hpp index 8bfa6ba..88a9e2c 100644 --- a/include/webcpp/hash/async.hpp +++ b/include/webcpp/hash/async.hpp @@ -53,7 +53,6 @@ #include #include #include -#include #include #include #include @@ -62,6 +61,7 @@ #include #include #include +#include #include #include @@ -339,11 +339,11 @@ template } auto extracted = hkdf_extract(salt, input_key_material); if (!extracted) { - wipe(pseudorandom_key); + secure::erase_secret(pseudorandom_key); return extracted.error(); } std::ranges::copy(*extracted, pseudorandom_key.begin()); - wipe_object(*extracted); + secure::erase_secret(std::span(&*extracted, 1)); return {}; } @@ -454,7 +454,7 @@ class heavy_state { value_.value = outcome ? *outcome : value_type{}; } if (cancelled_.load(std::memory_order_acquire)) { - wipe(output_.data(), output_.size()); + secure::erase_secret(output_); code = webcpp::asio::error::operation_aborted; value_ = {}; } diff --git a/include/webcpp/hash/backends/bearssl.hpp b/include/webcpp/hash/backends/bearssl.hpp index 56f453c..6560d74 100644 --- a/include/webcpp/hash/backends/bearssl.hpp +++ b/include/webcpp/hash/backends/bearssl.hpp @@ -40,10 +40,10 @@ #include #include #include -#include #include #include #include +#include #include @@ -454,7 +454,7 @@ struct hkdf_engine { const std::size_t produced_count = br_hkdf_produce(&hkdf_context, nonnull(application_info), application_info.size(), output_key_material.data(), output_key_material.size()); - wipe_object(hkdf_context); + secure::erase_secret(std::span(&hkdf_context, 1)); return produced_count == output_key_material.size() ? boost::system::result() : boost::system::result(error::backend_failure); @@ -517,10 +517,10 @@ struct hkdf_pieces_engine { update_all(mac_context, input_key_material); std::size_t const written_count = br_hmac_out(&mac_context, pseudorandom_key.unsafe_value().data()); - wipe_object(mac_context); - wipe_object(key_context); + secure::erase_secret(std::span(&mac_context, 1)); + secure::erase_secret(std::span(&key_context, 1)); if (written_count != pseudorandom_key.unsafe_value().size()) { - wipe_object(pseudorandom_key.unsafe_value()); + secure::erase_secret(std::span(&pseudorandom_key.unsafe_value(), 1)); pseudorandom_key = boost::system::result(error::backend_failure); } return pseudorandom_key; @@ -556,7 +556,7 @@ struct hkdf_pieces_engine { auto const octet_value = static_cast(block_counter); br_hmac_update(&mac_context, &octet_value, 1); computed = br_hmac_out(&mac_context, t_block.data()) == t_block.size(); - wipe_object(mac_context); + secure::erase_secret(std::span(&mac_context, 1)); if (computed) { std::size_t const copied_count = std::min(t_block.size(), output_key_material.size() - written_count); @@ -565,8 +565,8 @@ struct hkdf_pieces_engine { written_count += copied_count; } } - wipe_object(t_block); - wipe_object(key_context); + secure::erase_secret(std::span(&t_block, 1)); + secure::erase_secret(std::span(&key_context, 1)); if (!computed) { return error::backend_failure; } @@ -652,7 +652,7 @@ struct bearssl_digest_engine : full_engine { } /** Wipes the state. @param hashed The state. */ - static void destroy(state& hashed) noexcept { wipe_object(hashed); } + static void destroy(state& hashed) noexcept { secure::erase_secret(std::span(&hashed, 1)); } /** The digest of `input` at once, BearSSL's context wiped. @@ -745,8 +745,8 @@ struct bearssl_hmac_engine : full_engine { br_hmac_init(&mac_context, &key_context, output.size()); br_hmac_update(&mac_context, nonnull(input), input.size()); std::size_t const written_count = br_hmac_out(&mac_context, output.data()); - wipe_object(mac_context); - wipe_object(key_context); + secure::erase_secret(std::span(&mac_context, 1)); + secure::erase_secret(std::span(&key_context, 1)); return written_count == output.size() ? boost::system::result() : boost::system::result(error::backend_failure); } diff --git a/include/webcpp/hash/backends/blake3.hpp b/include/webcpp/hash/backends/blake3.hpp index 56b8690..0a5d936 100644 --- a/include/webcpp/hash/backends/blake3.hpp +++ b/include/webcpp/hash/backends/blake3.hpp @@ -24,7 +24,7 @@ #include #include -#include +#include #include @@ -76,7 +76,7 @@ struct digest_engine : full_engine { } /** Wipes the state. @param hashed The state. */ - static void destroy(state& hashed) noexcept { wipe_object(hashed); } + static void destroy(state& hashed) noexcept { secure::erase_secret(std::span(&hashed, 1)); } /** The output of `input` at once, the state wiped. diff --git a/include/webcpp/hash/backends/botan.hpp b/include/webcpp/hash/backends/botan.hpp index b00b904..e9653b5 100644 --- a/include/webcpp/hash/backends/botan.hpp +++ b/include/webcpp/hash/backends/botan.hpp @@ -66,13 +66,13 @@ #include #include #include -#include #include #include #include #include #include #include +#include #include #include @@ -887,8 +887,8 @@ struct botan_hmac_engine : full_engine { if (computed) { std::copy_n(tag.begin(), output.size(), output.begin()); } - wipe_object(whole); - wipe_object(hashed_key); + secure::erase_secret(whole); + secure::erase_secret(hashed_key); return computed; } }; @@ -1115,7 +1115,7 @@ template } else { derived = used.error(); } - wipe_object(hashed); + secure::erase_secret(hashed); return derived; } @@ -1255,7 +1255,7 @@ struct botan_hkdf_engine { input_key_material, key, {}); }); if (!derived) { - wipe_object(pseudorandom_key_digest); + secure::erase_secret(std::span(&pseudorandom_key_digest, 1)); pseudorandom_key = derived.error(); } return pseudorandom_key; @@ -1380,7 +1380,7 @@ struct hkdf_pieces_engine { }); }); if (!derived) { - wipe_object(pseudorandom_key_digest); + secure::erase_secret(std::span(&pseudorandom_key_digest, 1)); pseudorandom_key = derived.error(); } return pseudorandom_key; @@ -1422,7 +1422,7 @@ struct hkdf_pieces_engine { } }); mac_object.reset(); - wipe_object(t_block); + secure::erase_secret(std::span(&t_block, 1)); return derived; } diff --git a/include/webcpp/hash/backends/hash2.hpp b/include/webcpp/hash/backends/hash2.hpp index 8dcb229..d4afbfb 100644 --- a/include/webcpp/hash/backends/hash2.hpp +++ b/include/webcpp/hash/backends/hash2.hpp @@ -26,7 +26,7 @@ #include #include -#include +#include #include #include @@ -84,7 +84,7 @@ struct hkdf_engine { boost::system::in_place_value, mac_state.result(), }; - wipe_object(mac_state); + secure::erase_secret(std::span(&mac_state, 1)); return pseudorandom_key; } @@ -113,14 +113,14 @@ struct hkdf_engine { const auto octet_value = static_cast(block_counter); mac_state.update(&octet_value, 1); t_block = mac_state.result(); - wipe_object(mac_state); + secure::erase_secret(std::span(&mac_state, 1)); const std::size_t copied_count = std::min(t_block.size(), output_key_material.size() - written_count); std::memcpy(output_key_material.data() + written_count, t_block.data(), copied_count); written_count += copied_count; } - wipe_object(t_block); - wipe_object(keyed_mac); + secure::erase_secret(std::span(&t_block, 1)); + secure::erase_secret(std::span(&keyed_mac, 1)); return {}; } @@ -145,7 +145,7 @@ struct hkdf_engine { std::span(pseudorandom_key_digest.data(), pseudorandom_key_digest.size()), application_info); - wipe_object(pseudorandom_key_digest); + secure::erase_secret(std::span(&pseudorandom_key_digest, 1)); return derive_result; } }; @@ -184,7 +184,7 @@ struct hkdf_pieces_engine { boost::system::in_place_value, mac_state.result(), }; - wipe_object(mac_state); + secure::erase_secret(std::span(&mac_state, 1)); return pseudorandom_key; } @@ -216,14 +216,14 @@ struct hkdf_pieces_engine { const auto octet_value = static_cast(block_counter); mac_state.update(&octet_value, 1); t_block = mac_state.result(); - wipe_object(mac_state); + secure::erase_secret(std::span(&mac_state, 1)); const std::size_t copied_count = std::min(t_block.size(), output_key_material.size() - written_count); std::memcpy(output_key_material.data() + written_count, t_block.data(), copied_count); written_count += copied_count; } - wipe_object(t_block); - wipe_object(keyed_mac); + secure::erase_secret(std::span(&t_block, 1)); + secure::erase_secret(std::span(&keyed_mac, 1)); return {}; } @@ -289,15 +289,15 @@ struct pbkdf2_engine { t_block[byte_index] ^= u_block[byte_index]; } } - wipe_object(mac_state); + secure::erase_secret(std::span(&mac_state, 1)); const std::size_t copied_count = std::min(t_block.size(), output_bytes.size() - written_count); std::memcpy(output_bytes.data() + written_count, t_block.data(), copied_count); written_count += copied_count; } - wipe_object(u_block); - wipe_object(t_block); - wipe_object(keyed_mac); + secure::erase_secret(std::span(&u_block, 1)); + secure::erase_secret(std::span(&t_block, 1)); + secure::erase_secret(std::span(&keyed_mac, 1)); return {}; } }; @@ -342,14 +342,14 @@ struct hash2_digest_engine : full_engine { auto result_bytes = hashed.result(); auto const taken = std::min(output.size(), result_bytes.size()); std::memcpy(output.data(), result_bytes.data(), taken); - wipe_object(result_bytes); + secure::erase_secret(std::span(&result_bytes, 1)); output = output.subspan(taken); } return {}; } /** Wipes the state. @param hashed The state. */ - static void destroy(state& hashed) noexcept { wipe_object(hashed); } + static void destroy(state& hashed) noexcept { secure::erase_secret(std::span(&hashed, 1)); } /** The hash of `input` at once, the state wiped. @@ -506,8 +506,8 @@ struct hash2_hmac_engine : full_engine { keyed_mac.update(input.data(), input.size()); auto tag_bytes = keyed_mac.result(); std::memcpy(output.data(), tag_bytes.data(), output.size()); - wipe_object(tag_bytes); - wipe_object(keyed_mac); + secure::erase_secret(std::span(&tag_bytes, 1)); + secure::erase_secret(std::span(&keyed_mac, 1)); return {}; } }; @@ -611,7 +611,7 @@ struct mac_engine : full_engine { byte = static_cast(tag_value & 0xffU); tag_value >>= 8U; } - wipe_object(keyed_mac); + secure::erase_secret(std::span(&keyed_mac, 1)); return {}; } }; @@ -647,8 +647,8 @@ struct mac_engine : full_engine { keyed_mac.update(input.data(), input.size()); auto tag_bytes = keyed_mac.result(); std::memcpy(output.data(), tag_bytes.data(), output.size()); - wipe_object(tag_bytes); - wipe_object(keyed_mac); + secure::erase_secret(std::span(&tag_bytes, 1)); + secure::erase_secret(std::span(&keyed_mac, 1)); return {}; } }; diff --git a/include/webcpp/hash/backends/openssl.hpp b/include/webcpp/hash/backends/openssl.hpp index 8d84e21..bb773a2 100644 --- a/include/webcpp/hash/backends/openssl.hpp +++ b/include/webcpp/hash/backends/openssl.hpp @@ -78,11 +78,11 @@ #include #include #include -#include #include #include #include #include +#include #include #include @@ -587,7 +587,7 @@ struct hkdf_engine { std::span(pseudorandom_key_digest.data(), pseudorandom_key_digest.size()), ossl_params); if (!derive_result) { - wipe_object(pseudorandom_key_digest); + secure::erase_secret(std::span(&pseudorandom_key_digest, 1)); pseudorandom_key = derive_result.error(); } return pseudorandom_key; @@ -747,7 +747,7 @@ struct hkdf_pieces_engine { EVP_MAC_free(mac_algorithm); auto const outcome = openssl_outcome(computed); if (!outcome) { - wipe_object(pseudorandom_key_digest); + secure::erase_secret(std::span(&pseudorandom_key_digest, 1)); pseudorandom_key = outcome.error(); } return pseudorandom_key; @@ -790,7 +790,7 @@ struct hkdf_pieces_engine { written_count += copied_count; } } - wipe_object(t_block); + secure::erase_secret(std::span(&t_block, 1)); // The frees raise no error: the outcome read after them is the computation's. EVP_MAC_CTX_free(keyed); EVP_MAC_free(mac_algorithm); @@ -1520,7 +1520,7 @@ struct openssl_hmac_engine : full_engine { if (computed) { std::copy_n(tag.begin(), output.size(), output.begin()); } - wipe_object(whole); + secure::erase_secret(whole); return computed; } }; diff --git a/include/webcpp/hash/backends/sodium.hpp b/include/webcpp/hash/backends/sodium.hpp index cb68ba5..e838ed7 100644 --- a/include/webcpp/hash/backends/sodium.hpp +++ b/include/webcpp/hash/backends/sodium.hpp @@ -55,12 +55,12 @@ #include #include #include -#include #include #include #include #include #include +#include #include #include @@ -289,7 +289,7 @@ struct sodium_hkdf_engine { boost::system::result pseudorandom_key{boost::system::in_place_value}; if (!sodium_ready() || Traits::extract(pseudorandom_key.unsafe_value().data(), salt, input_key_material) != 0) { - wipe_object(pseudorandom_key.unsafe_value()); + secure::erase_secret(std::span(&pseudorandom_key.unsafe_value(), 1)); pseudorandom_key = boost::system::result(error::backend_failure); } return pseudorandom_key; @@ -334,7 +334,7 @@ struct sodium_hkdf_engine { auto derive_result = expand( output_key_material, std::span(key_bytes.data(), key_bytes.size()), application_info); - wipe_object(key_bytes); + secure::erase_secret(std::span(&key_bytes, 1)); return derive_result; } }; @@ -458,9 +458,9 @@ struct sodium_hkdf_pieces_engine { bool const computed = sodium_ready() && Init(&mac_state, nonnull(salt), salt.size()) == 0 && update_all(mac_state, input_key_material) && Final(&mac_state, pseudorandom_key.unsafe_value().data()) == 0; - wipe_object(mac_state); + secure::erase_secret(std::span(&mac_state, 1)); if (!computed) { - wipe_object(pseudorandom_key.unsafe_value()); + secure::erase_secret(std::span(&pseudorandom_key.unsafe_value(), 1)); pseudorandom_key = boost::system::result(error::backend_failure); } return pseudorandom_key; @@ -490,7 +490,7 @@ struct sodium_hkdf_pieces_engine { (block_counter == 1 || Update(&mac_state, t_block.data(), t_block.size()) == 0) && update_all(mac_state, application_info) && Update(&mac_state, &octet_value, 1) == 0 && Final(&mac_state, t_block.data()) == 0; - wipe_object(mac_state); + secure::erase_secret(std::span(&mac_state, 1)); if (computed) { std::size_t const copied_count = std::min(t_block.size(), output_key_material.size() - written_count); @@ -499,8 +499,8 @@ struct sodium_hkdf_pieces_engine { written_count += copied_count; } } - wipe_object(t_block); - wipe_object(keyed_state); + secure::erase_secret(std::span(&t_block, 1)); + secure::erase_secret(std::span(&keyed_state, 1)); if (!computed) { return error::backend_failure; } @@ -637,7 +637,7 @@ struct digest_engine : full_engine { } /** Wipes the state. @param hashed The state. */ - static void destroy(state& hashed) noexcept { wipe_object(hashed); } + static void destroy(state& hashed) noexcept { secure::erase_secret(std::span(&hashed, 1)); } /** The digest of `input` at once: `crypto_generichash_blake2b_salt_personal`, whose output size @@ -700,7 +700,7 @@ struct sodium_incremental_engine : full_engine { } /** Wipes the state. @param hashed The state. */ - static void destroy(state& hashed) noexcept { wipe_object(hashed); } + static void destroy(state& hashed) noexcept { secure::erase_secret(std::span(&hashed, 1)); } /** The digest of `input` at once, on a state of this frame, wiped before the return. @@ -957,8 +957,8 @@ struct sodium_hmac_engine : full_engine { if (computed && !whole) { std::copy_n(digest_bytes.begin(), output.size(), output.begin()); } - wipe_object(digest_bytes); - wipe_object(keyed); + secure::erase_secret(digest_bytes); + secure::erase_secret(std::span(&keyed, 1)); if (!computed) { return error::backend_failure; } @@ -1084,7 +1084,7 @@ struct sodium_keyed_blake2b_engine : full_engine { crypto_generichash_blake2b_init(&keyed, key.data(), key.size(), output.size()) == 0 && crypto_generichash_blake2b_update(&keyed, nonnull(input), input.size()) == 0 && crypto_generichash_blake2b_final(&keyed, output.data(), output.size()) == 0; - wipe_object(keyed); + secure::erase_secret(std::span(&keyed, 1)); if (!computed) { return error::backend_failure; } diff --git a/include/webcpp/hash/backends/webcrypto.hpp b/include/webcpp/hash/backends/webcrypto.hpp index 61fd32d..aee7b7f 100644 --- a/include/webcpp/hash/backends/webcrypto.hpp +++ b/include/webcpp/hash/backends/webcrypto.hpp @@ -108,13 +108,13 @@ struct backend_limits { #include #include -#include -#include #include #include #include #include #include +#include +#include #include #include @@ -544,7 +544,7 @@ class webcrypto_operation { private: // The output zeroed, with the completion's error. boost::system::error_code fail(boost::system::error_code code) noexcept { - wipe(output_); + secure::erase_secret(output_); return code; } @@ -590,7 +590,7 @@ boost::system::error_code webcrypto_refusal(Executor const& executor, if (subtle_crypto().isUndefined()) { // The backend's failure: the JavaScript runtime has no WebCrypto. The output is zeroed, as // every backend's failure leaves it. - wipe(output); + secure::erase_secret(output); return error::backend_failure; } return {}; @@ -1082,7 +1082,7 @@ class webcrypto_hmac_hkdf_operation { // The output zeroed and the buffers released, with the completion's error. [[nodiscard]] boost::system::error_code fail(boost::system::error_code code) noexcept { - wipe(output_); + secure::erase_secret(output_); release(); return code; } @@ -1116,7 +1116,7 @@ struct webcrypto_hmac_hkdf_initiation { boost::system::result const& guarded) const { auto refusal = webcrypto_refusal(executor, output, guarded); if (!refusal && !request.gathered) { - wipe(output); + secure::erase_secret(output); refusal = error::backend_failure; } if (refusal) { @@ -1289,7 +1289,7 @@ struct async_engine { static auto hkdf(Executor const& executor, std::span output_key_material, std::span salt, byte_pieces input_key_material, byte_pieces application_info, CompletionToken&& token) { - timing_guard const timing; + secure::timing_guard const timing; auto const guarded = hkdf_pieces_guards(output_key_material.size(), salt.size()); auto request = webcrypto_hmac_hkdf_of(webcrypto_hkdf_steps::extract_and_expand); @@ -1317,7 +1317,7 @@ struct async_engine { std::span salt, std::span input_key_material, CompletionToken&& token) { - timing_guard const timing; + secure::timing_guard const timing; auto guarded = extract_size_guard(pseudorandom_key.size()); if (guarded) { guarded = @@ -1345,7 +1345,7 @@ struct async_engine { static auto hkdf_extract(Executor const& executor, std::span pseudorandom_key, std::span salt, byte_pieces input_key_material, CompletionToken&& token) { - timing_guard const timing; + secure::timing_guard const timing; auto guarded = extract_size_guard(pseudorandom_key.size()); if (guarded) { guarded = hkdf_pieces_salt_guard(salt.size()); @@ -1375,7 +1375,7 @@ struct async_engine { std::span pseudorandom_key, std::span application_info, CompletionToken&& token) { - timing_guard const timing; + secure::timing_guard const timing; auto guarded = expand_guards(output_key_material.size(), pseudorandom_key.size()); if (guarded && application_info.size() > hkdf_max_application_info_size) { guarded = error::input_too_long; @@ -1403,7 +1403,7 @@ struct async_engine { static auto hkdf_expand(Executor const& executor, std::span output_key_material, std::span pseudorandom_key, byte_pieces application_info, CompletionToken&& token) { - timing_guard const timing; + secure::timing_guard const timing; auto const guarded = expand_guards(output_key_material.size(), pseudorandom_key.size()); auto request = webcrypto_hmac_hkdf_of(webcrypto_hkdf_steps::expand); diff --git a/include/webcpp/hash/backends/xkcp.hpp b/include/webcpp/hash/backends/xkcp.hpp index 084bf6b..f3a08de 100644 --- a/include/webcpp/hash/backends/xkcp.hpp +++ b/include/webcpp/hash/backends/xkcp.hpp @@ -31,10 +31,10 @@ #include #include -#include #include #include #include +#include #include @@ -84,12 +84,12 @@ struct digest_engine : full_engine { state& hashed, std::span output) noexcept { auto result_bytes = hashed.result(); std::memcpy(output.data(), result_bytes.data(), output.size()); - wipe_object(result_bytes); + secure::erase_secret(std::span(&result_bytes, 1)); return {}; } /** Wipes the state. @param hashed The state. */ - static void destroy(state& hashed) noexcept { wipe_object(hashed); } + static void destroy(state& hashed) noexcept { secure::erase_secret(std::span(&hashed, 1)); } /** The digest of `input` at once, the state wiped. @@ -171,7 +171,7 @@ struct xkcp_keccak_hash_engine : full_engine { } /** Wipes the state. @param hashed The state. */ - static void destroy(state& hashed) noexcept { wipe_object(hashed); } + static void destroy(state& hashed) noexcept { secure::erase_secret(std::span(&hashed, 1)); } /** The output of `input` at once, the state wiped. @@ -397,7 +397,7 @@ struct xkcp_cshake_engine : full_engine { } /** Wipes the state. @param hashed The state. */ - static void destroy(state& hashed) noexcept { wipe_object(hashed); } + static void destroy(state& hashed) noexcept { secure::erase_secret(std::span(&hashed, 1)); } /** The output of `input` at once, the state wiped. @@ -519,7 +519,7 @@ struct xkcp_tuplehash_engine : full_engine { } /** Wipes the state. @param hashed The state. */ - static void destroy(state& hashed) noexcept { wipe_object(hashed); } + static void destroy(state& hashed) noexcept { secure::erase_secret(std::span(&hashed, 1)); } /** The digest of the tuple of one element, `input`, the state wiped. @@ -621,7 +621,7 @@ struct xkcp_parallelhash_engine : full_engine { } /** Wipes the state. @param hashed The state. */ - static void destroy(state& hashed) noexcept { wipe_object(hashed); } + static void destroy(state& hashed) noexcept { secure::erase_secret(std::span(&hashed, 1)); } /** The digest of `input` at once, the state wiped. @@ -675,7 +675,7 @@ struct xkcp_kmac_engine : full_engine { Functions::squeeze(storage.data(), output.data(), output.size()) == 0 : Functions::final(storage.data(), output.data()) == 0; } - wipe_object(storage); + secure::erase_secret(std::span(&storage, 1)); if (!done) { return error::backend_failure; } @@ -745,7 +745,7 @@ struct xkcp_turboshake_engine : full_engine { } /** Wipes the state. @param hashed The state. */ - static void destroy(state& hashed) noexcept { wipe_object(hashed); } + static void destroy(state& hashed) noexcept { secure::erase_secret(std::span(&hashed, 1)); } /** The output of `input` at once, the state wiped. @@ -854,7 +854,7 @@ struct xkcp_kangarootwelve_engine : full_engine { } /** Wipes the state. @param hashed The state. */ - static void destroy(state& hashed) noexcept { wipe_object(hashed); } + static void destroy(state& hashed) noexcept { secure::erase_secret(std::span(&hashed, 1)); } /** The output of `input` at once, the state wiped. diff --git a/include/webcpp/hash/bcrypt.hpp b/include/webcpp/hash/bcrypt.hpp index b657155..8c0a2b4 100644 --- a/include/webcpp/hash/bcrypt.hpp +++ b/include/webcpp/hash/bcrypt.hpp @@ -16,8 +16,8 @@ another implementation would not. For a new design Argon2id (`argon2id`) is the better choice; bcrypt is here to check and make the strings that systems already store. - Every call holds the data-independent-timing guard (`detail::timing_guard`): where the CPU has - DIT, the call sets it and leaves the thread's state as it found it. + Every call holds the data-independent-timing guard (`webcpp::secure::timing_guard`, secure's + guard): where the CPU has DIT, the call sets it and leaves the thread's state as it found it. Every guard runs before any work, and a guard's refusal leaves the output as it was; a backend's failure zeroes it. The caller gives every input, the salt included: hash draws no random byte. @@ -41,10 +41,10 @@ #include #include -#include -#include #include #include +#include +#include #include @@ -259,7 +259,7 @@ template > std::span password, std::span salt, bcrypt_parameters const& parameters = {}) { - detail::timing_guard const timing; + secure::timing_guard const timing; auto guarded = detail::bcrypt_guards(output.size(), password, salt.size(), parameters); if (!guarded) { return guarded; @@ -267,7 +267,7 @@ template > auto made = detail::bcrypt_engine::generate(output, password, salt, parameters.cost, parameters.version); if (!made) { - detail::wipe(output.data(), output.size()); + secure::erase_secret(output); } return made; } @@ -290,7 +290,7 @@ template > template > [[nodiscard]] boost::system::result bcrypt_verify(std::span password, std::string_view hash) { - detail::timing_guard const timing; + secure::timing_guard const timing; auto const guarded = detail::bcrypt_verify_guards(password, hash); if (!guarded) { return guarded.error(); @@ -317,14 +317,14 @@ template > std::span password, std::span salt, std::uint32_t rounds) { - detail::timing_guard const timing; + secure::timing_guard const timing; auto guarded = detail::bcrypt_pbkdf_guards(output.size(), password.size(), salt.size(), rounds); if (!guarded) { return guarded; } auto derived = detail::bcrypt_pbkdf_engine::derive(output, password, salt, rounds); if (!derived) { - detail::wipe(output); + secure::erase_secret(output); } return derived; } diff --git a/include/webcpp/hash/blake2b_subkey.hpp b/include/webcpp/hash/blake2b_subkey.hpp index 821d6f7..6ed0ef7 100644 --- a/include/webcpp/hash/blake2b_subkey.hpp +++ b/include/webcpp/hash/blake2b_subkey.hpp @@ -14,8 +14,8 @@ zero bytes as the personalisation: the bytes libsodium's `crypto_kdf_derive_from_key` gives, so that a program and a libsodium peer derive the same keys. - Every call holds the data-independent-timing guard (`detail::timing_guard`): where the CPU has - DIT, the call sets it and leaves the thread's state as it found it. + Every call holds the data-independent-timing guard (`webcpp::secure::timing_guard`, secure's + guard): where the CPU has DIT, the call sets it and leaves the thread's state as it found it. Every guard runs before any work, and a guard's refusal leaves the output as it was; a backend's failure zeroes it. @@ -37,10 +37,10 @@ #include #include -#include -#include #include #include +#include +#include #include @@ -87,7 +87,7 @@ template > std::span master_key, std::uint64_t subkey_id, std::span context) { - detail::timing_guard const timing; + secure::timing_guard const timing; using engine = detail::blake2b_subkey_engine; if (output.size() < blake2b_subkey_min_size || output.size() > blake2b_subkey_max_size) { return error::invalid_output_size; @@ -100,7 +100,7 @@ template > } auto derived = engine::derive(output, master_key, subkey_id, context); if (!derived) { - detail::wipe(output); + secure::erase_secret(output); } return derived; } diff --git a/include/webcpp/hash/blake3.hpp b/include/webcpp/hash/blake3.hpp index 79e4116..9a75cce 100644 --- a/include/webcpp/hash/blake3.hpp +++ b/include/webcpp/hash/blake3.hpp @@ -22,7 +22,7 @@ #else #include -#include +#include #include @@ -193,7 +193,7 @@ inline blake3::blake3() noexcept { } inline blake3::blake3(std::uint64_t seed_value) noexcept { - detail::timing_guard const timing{seed_value != 0}; + secure::timing_guard const timing{seed_value != 0}; detail::touch_blake3_canary(); keyed_ = seed_value != 0; if (seed_value == 0) { @@ -211,7 +211,7 @@ inline blake3::blake3(void const* input_data, std::size_t input_size) noexcept : blake3(static_cast(input_data), input_size) {} inline blake3::blake3(unsigned char const* input_data, std::size_t input_size) noexcept { - detail::timing_guard const timing{input_size != 0}; + secure::timing_guard const timing{input_size != 0}; detail::touch_blake3_canary(); keyed_ = input_size != 0; if (input_size == 0) { @@ -231,7 +231,7 @@ inline blake3::blake3(unsigned char const* input_data, std::size_t input_size) n // NOLINTEND(cppcoreguidelines-pro-type-member-init) inline blake3 blake3::keyed(std::span key_bytes) noexcept { - detail::timing_guard const timing; + secure::timing_guard const timing; detail::touch_blake3_canary(); blake3 derived_hasher{uninitialized{}}; derived_hasher.keyed_ = true; @@ -240,7 +240,7 @@ inline blake3 blake3::keyed(std::span key_bytes) noexcep } inline blake3 blake3::derive_key(std::string_view context_string) noexcept { - detail::timing_guard const timing; + secure::timing_guard const timing; detail::touch_blake3_canary(); blake3 derived_hasher{uninitialized{}}; derived_hasher.keyed_ = true; @@ -250,12 +250,12 @@ inline blake3 blake3::derive_key(std::string_view context_string) noexcept { } inline void blake3::update(void const* input_data, std::size_t input_size) noexcept { - detail::timing_guard const timing{keyed_}; + secure::timing_guard const timing{keyed_}; update(static_cast(input_data), input_size); } inline void blake3::update(unsigned char const* input_data, std::size_t input_size) noexcept { - detail::timing_guard const timing{keyed_}; + secure::timing_guard const timing{keyed_}; detail::touch_blake3_canary(); if (input_size == 0) { return; @@ -265,7 +265,7 @@ inline void blake3::update(unsigned char const* input_data, std::size_t input_si } inline blake3::result_type blake3::result() noexcept { - detail::timing_guard const timing{keyed_}; + secure::timing_guard const timing{keyed_}; detail::touch_blake3_canary(); result_type output_bytes; blake3_hasher_finalize_seek(&hasher_, squeezed_, output_bytes.data(), output_bytes.size()); @@ -275,13 +275,13 @@ inline blake3::result_type blake3::result() noexcept { inline void blake3::squeeze(std::span output_bytes, std::uint64_t seek_position) const noexcept { - detail::timing_guard const timing{keyed_}; + secure::timing_guard const timing{keyed_}; detail::touch_blake3_canary(); blake3_hasher_finalize_seek(&hasher_, seek_position, output_bytes.data(), output_bytes.size()); } inline void blake3::reset() noexcept { - detail::timing_guard const timing{keyed_}; + secure::timing_guard const timing{keyed_}; detail::touch_blake3_canary(); blake3_hasher_reset(&hasher_); squeezed_ = 0; diff --git a/include/webcpp/hash/detail/engines.hpp b/include/webcpp/hash/detail/engines.hpp index 9bffc1f..dceedc1 100644 --- a/include/webcpp/hash/detail/engines.hpp +++ b/include/webcpp/hash/detail/engines.hpp @@ -10,8 +10,8 @@ #include #include -#include #include +#include #include #include @@ -318,7 +318,7 @@ template output_key_material, std::span(pseudorandom_key_bytes.data(), pseudorandom_key_bytes.size()), application_info); - wipe_object(pseudorandom_key_bytes); + secure::erase_secret(std::span(&pseudorandom_key_bytes, 1)); return derive_result; } diff --git a/include/webcpp/hash/detail/timing.hpp b/include/webcpp/hash/detail/timing.hpp deleted file mode 100644 index 5ba50e7..0000000 --- a/include/webcpp/hash/detail/timing.hpp +++ /dev/null @@ -1,220 +0,0 @@ -// 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_HASH_DETAIL_TIMING_HPP -#define WEBCPP_HASH_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 hash calls sets it. Every public function that - * computes on a key holds one as its first statement. - * - * This is hash's own copy of the guard of webcpp::crypto, not an include of it: hash depends on - * no other webcpp library, and crypto's detail headers are not hash's to include (config's rule - * on dependencies, and the direction of the dependency). The other copy is - * libs/crypto/include/webcpp/crypto/detail/timing.hpp, and what keeps the two in step is - * libs/crypto/test/timing_hash_interop_test.cpp, which runs both guards nested and checks that - * each leaves the thread's DIT as it found it. A change to one is a change to the other. - * - * The one difference is the constructor's `engaged` flag. Some of hash's functions are keyed only - * by their arguments (a BLAKE2b digest of a call that passes a key, a BLAKE3 object made with a - * key), and an unkeyed call of the same function computes on public data and must not pay for - * DIT: those functions construct the guard with `engaged` false when the call is not keyed. A - * disengaged guard sets nothing, clears nothing and fires no seam, on every target; on a target the - * guard does not cover, `engaged` alone decides whether the seam fires and nothing else happens. - * - * 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_HASH_DETAIL_TIMING_COVERED 1 -#include -#include -#ifdef __APPLE__ -#include -#else -#include -#endif -#endif - -/* - * WEBCPP_HASH_DETAIL_TIMING_ENTERED(where), the seam, is defined by a test alone, before its - * first include, to record the guards a call enters and the function of the first, the outermost - * (test/support/timing_seam.hpp), so that a public function that forgets its guard fails on every - * lane, also where DIT does not apply and also when it calls another guarded function. Then - * timing_guard's constructor takes the std::source_location of the guard's declaration, whose - * function_name() is the function that holds it, and runs the seam with it first, on every - * target. Nothing in a program's build: without the seam the constructor takes no argument, so - * that no function's name or header's path reaches a program, whether the constructor is - * inlined or not. - */ -#ifdef WEBCPP_HASH_DETAIL_TIMING_ENTERED -#include -#endif - -namespace webcpp::hash::detail { - -#ifdef WEBCPP_HASH_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 executes `isb`. - * - * 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. Apple's "Writing ARM64 code for Apple - * platforms" ("Enable DIT for constant-time cryptographic operations") asks for a speculation - * barrier after turning DIT on, so that the timing of the instructions after it reflects the new - * state: `sb` where the CPU has FEAT_SB, else `dsb nsh` then `isb sy`; it asks for none after - * turning DIT off. The `isb` here, after both writes, is not that barrier: which barrier each - * write needs is measured in the final measurement pass of the change data-independent-timing. - * - * @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_HASH_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_HASH_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_HASH_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. 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: -#ifdef WEBCPP_HASH_DETAIL_TIMING_ENTERED - /** - * Where `engaged`, runs the seam, then sets DIT where the CPU has it and it is clear: a - * test's build alone. - * - * @param engaged Whether the call is keyed; false makes the guard do nothing, the seam - * included. - * @param where Where the guard is declared, the default: the seam's argument alone. - */ - explicit timing_guard(bool engaged = true, - std::source_location where = std::source_location::current()) noexcept { - if (!engaged) { - return; - } - WEBCPP_HASH_DETAIL_TIMING_ENTERED(where); - enter(); - } -#else - /** - * Where `engaged`, sets DIT where the CPU has it and it is clear. - * - * @param engaged Whether the call is keyed; false makes the guard do nothing. - */ - explicit timing_guard(bool engaged = true) noexcept { - if (engaged) { - enter(); - } - } -#endif - - /** Clears DIT when this guard set it. */ - ~timing_guard() { -#ifdef WEBCPP_HASH_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: - /** - * Sets DIT where the CPU has it and it is clear, and remembers to clear it. - */ - void enter() noexcept { -#ifdef WEBCPP_HASH_DETAIL_TIMING_COVERED - if (cpu_has_dit() && !dit_is_set()) { - write_dit(dit_bit); - armed_ = true; - } -#endif - } - -#ifdef WEBCPP_HASH_DETAIL_TIMING_COVERED - /** Whether this guard set DIT, and so clears it. */ - bool armed_ = false; -#endif -}; - -} // namespace webcpp::hash::detail - -#endif // WEBCPP_HASH_DETAIL_TIMING_HPP diff --git a/include/webcpp/hash/detail/wipe.hpp b/include/webcpp/hash/detail/wipe.hpp deleted file mode 100644 index 4b72769..0000000 --- a/include/webcpp/hash/detail/wipe.hpp +++ /dev/null @@ -1,82 +0,0 @@ -// 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_HASH_DETAIL_WIPE_HPP -#define WEBCPP_HASH_DETAIL_WIPE_HPP - -#include - -#include -#include -#include -#include -#include - -#ifdef WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD -namespace webcpp::hash::detail::test { - -/** - The test seam through which @ref webcpp::hash::detail::wipe reports each wipe. A program built - with `WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD` defines this function, which each wipe calls once its - bytes are zero, with what it wiped. A program built without the macro neither declares nor - calls it. - - @param wiped_data The first byte wiped, or null when `byte_count` is 0. - @param byte_count How many bytes. -*/ -void wipe_record(void const* wiped_data, std::size_t byte_count) noexcept; - -} // namespace webcpp::hash::detail::test -#endif - -namespace webcpp::hash::detail { - -/** - Zeroes `byte_count` bytes at `wiped_data` with stores the optimiser cannot drop. - - Tip: a `memset` before an object dies is a dead store a compiler may remove; stores through a - volatile pointer, then a signal fence, are kept (spec section 6.1). - - @param wiped_data The first byte, or null when `byte_count` is 0. - @param byte_count How many bytes. -*/ -inline void wipe(void* wiped_data, std::size_t byte_count) noexcept { - auto* const wiped_bytes = static_cast(wiped_data); - for (std::size_t byte_index = 0; byte_index < byte_count; ++byte_index) { - wiped_bytes[byte_index] = 0; - } - std::atomic_signal_fence(std::memory_order_seq_cst); -#ifdef WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD - test::wipe_record(wiped_data, byte_count); -#endif -} - -/** - Zeroes the bytes of a span, as @ref wipe(void*, std::size_t) does: an output a backend's failure - leaves. - - @param wiped_bytes The bytes. -*/ -inline void wipe(std::span wiped_bytes) noexcept { - wipe(wiped_bytes.data(), wiped_bytes.size()); -} - -/** - Zeroes an object's bytes, as @ref wipe(void*, std::size_t) does: a keyed hmac, a block of a key - derivation, a C library's context. - - @tparam T A trivially copyable type, whose bytes are all its state. - @param wiped_object The object. -*/ -template -void wipe_object(T& wiped_object) noexcept { - static_assert(std::is_trivially_copyable_v, "only a plain object's bytes are wiped"); - wipe(&wiped_object, sizeof wiped_object); -} - -} // namespace webcpp::hash::detail - -#endif // WEBCPP_HASH_DETAIL_WIPE_HPP diff --git a/include/webcpp/hash/digest.hpp b/include/webcpp/hash/digest.hpp index 9b75f7a..c59d504 100644 --- a/include/webcpp/hash/digest.hpp +++ b/include/webcpp/hash/digest.hpp @@ -35,10 +35,10 @@ #include #include #include -#include -#include #include #include +#include +#include #include @@ -714,7 +714,7 @@ template > [[nodiscard]] boost::system::result digest_of(std::span output, std::span input, digest_options const& options = {}) { - detail::timing_guard const timing{detail::options_keyed(options)}; + secure::timing_guard const timing{detail::options_keyed(options)}; using engine = detail::digest_engine; auto checked = detail::digest_guards(output.size(), options); if (!checked) { @@ -729,7 +729,7 @@ template > } auto hashed = engine::one_shot(output, input, options); if (!hashed) { - detail::wipe(output); + secure::erase_secret(output); } return hashed; } @@ -790,7 +790,7 @@ class hasher { */ [[nodiscard]] static boost::system::result make( digest_options const& options = {}) noexcept { - detail::timing_guard const timing{detail::options_keyed(options)}; + secure::timing_guard const timing{detail::options_keyed(options)}; auto const checked = traits::check(options); if (!checked) { return checked.error(); @@ -864,7 +864,7 @@ class hasher { either of the last two the hasher is spent (`invalid_state`). */ [[nodiscard]] boost::system::result update(std::span input) noexcept { - detail::timing_guard const timing{keyed_}; + secure::timing_guard const timing{keyed_}; if (phase_ != phase::hashing) { return error::invalid_state; } @@ -886,7 +886,7 @@ class hasher { hasher spent. */ [[nodiscard]] boost::system::result finish(std::span output) noexcept { - detail::timing_guard const timing{keyed_}; + secure::timing_guard const timing{keyed_}; if (phase_ != phase::hashing) { return error::invalid_state; } @@ -900,7 +900,7 @@ class hasher { } auto finished = engine::finish(state_, output); if (!finished) { - detail::wipe(output); + secure::erase_secret(output); } end(); return finished; diff --git a/include/webcpp/hash/hkdf.hpp b/include/webcpp/hash/hkdf.hpp index 3f55482..473cf85 100644 --- a/include/webcpp/hash/hkdf.hpp +++ b/include/webcpp/hash/hkdf.hpp @@ -17,8 +17,8 @@ (`{vec}`, `{array}`), in every pieces position is ambiguous between the two forms: pass `x` itself, which converts to the span, or write `byte_pieces{...}`, or an empty span. - Every call holds the data-independent-timing guard (`detail::timing_guard`): where the CPU has - DIT, the call sets it and leaves the thread's state as it found it. + Every call holds the data-independent-timing guard (`webcpp::secure::timing_guard`, secure's + guard): where the CPU has DIT, the call sets it and leaves the thread's state as it found it. Every guard runs before any work, and a guard's refusal leaves the output as it was; a backend's failure zeroes it. @@ -44,10 +44,10 @@ #include #include #include -#include -#include #include #include +#include +#include // The backends the program links, each under its WEBCPP_HASH_HAS_, so that a call that names // one finds its engines here; the default, backends::hash2, needs none of them. @@ -188,7 +188,7 @@ template template > [[nodiscard]] boost::system::result::type> hkdf_extract( std::span salt, std::span input_key_material) { - detail::timing_guard const timing; + secure::timing_guard const timing; if constexpr (!detail::takes_hmac()) { return error::backend_failure; // unreachable: takes_hmac has stopped the build } else { @@ -219,7 +219,7 @@ template hkdf_expand( std::span output_key_material, std::span pseudorandom_key, std::span application_info) { - detail::timing_guard const timing; + secure::timing_guard const timing; if constexpr (!detail::takes_hmac()) { return error::backend_failure; // unreachable: takes_hmac has stopped the build } else { @@ -245,7 +245,7 @@ template ::expand( output_key_material, pseudorandom_key, application_info); if (!derive_result) { - detail::wipe(output_key_material); + secure::erase_secret(output_key_material); } return derive_result; } @@ -270,7 +270,7 @@ template > std::span salt, std::span input_key_material, std::span application_info) { - detail::timing_guard const timing; + secure::timing_guard const timing; if constexpr (!detail::takes_hmac()) { return error::backend_failure; // unreachable: takes_hmac has stopped the build } else { @@ -283,7 +283,7 @@ template > auto derive_result = detail::hkdf_engine::extract_and_expand( output_key_material, salt, input_key_material, application_info); if (!derive_result) { - detail::wipe(output_key_material); + secure::erase_secret(output_key_material); } return derive_result; } @@ -309,7 +309,7 @@ template > template > [[nodiscard]] boost::system::result::type> hkdf_extract( std::span salt, byte_pieces input_key_material) { - detail::timing_guard const timing; + secure::timing_guard const timing; if constexpr (!detail::takes_hmac()) { return error::backend_failure; // unreachable: takes_hmac has stopped the build } else { @@ -339,7 +339,7 @@ template hkdf_expand( std::span output_key_material, std::span pseudorandom_key, byte_pieces application_info) { - detail::timing_guard const timing; + secure::timing_guard const timing; if constexpr (!detail::takes_hmac()) { return error::backend_failure; // unreachable: takes_hmac has stopped the build } else { @@ -355,7 +355,7 @@ template ::expand( output_key_material, pseudorandom_key, application_info); if (!derive_result) { - detail::wipe(output_key_material); + secure::erase_secret(output_key_material); } return derive_result; } @@ -387,7 +387,7 @@ template salt, byte_pieces input_key_material, byte_pieces application_info) { - detail::timing_guard const timing; + secure::timing_guard const timing; if constexpr (!detail::takes_hmac()) { return error::backend_failure; // unreachable: takes_hmac has stopped the build } else { @@ -399,7 +399,7 @@ template ::extract_and_expand( output_key_material, salt, input_key_material, application_info); if (!derive_result) { - detail::wipe(output_key_material); + secure::erase_secret(output_key_material); } return derive_result; } diff --git a/include/webcpp/hash/hmac.hpp b/include/webcpp/hash/hmac.hpp index 6bf57c8..b9cef13 100644 --- a/include/webcpp/hash/hmac.hpp +++ b/include/webcpp/hash/hmac.hpp @@ -8,7 +8,7 @@ Boost.Hash2's HMAC under webcpp::hash's names: HMAC, RFC 2104, over each of Hash2's cryptographic algorithms. - A program checks a MAC with `webcpp::crypto::constant_time_equal`, never with + A program checks a MAC with `webcpp::secure::constant_time_equal`, never with `digest::operator==`, which does not run in constant time: @code @@ -16,7 +16,7 @@ // a webcpp::hash::hmac_sha2_256::result_type webcpp::hash::hmac_sha2_256 mac(key.data(), key.size()); mac.update(message.data(), message.size()); - bool genuine = webcpp::crypto::constant_time_equal(mac.result(), tag); + bool genuine = webcpp::secure::constant_time_equal(mac.result(), tag); @endcode @see "HMAC", in the guide. diff --git a/include/webcpp/hash/kdf_standards.hpp b/include/webcpp/hash/kdf_standards.hpp index 2df47a6..c3bae2a 100644 --- a/include/webcpp/hash/kdf_standards.hpp +++ b/include/webcpp/hash/kdf_standards.hpp @@ -17,8 +17,8 @@ None of them is for a password (Argon2id is) nor a better choice than HKDF for a new design: they are here to derive the bytes a standard, a smart card or another library derives. - Every call holds the data-independent-timing guard (`detail::timing_guard`): where the CPU has - DIT, the call sets it and leaves the thread's state as it found it. + Every call holds the data-independent-timing guard (`webcpp::secure::timing_guard`, secure's + guard): where the CPU has DIT, the call sets it and leaves the thread's state as it found it. Every guard runs before any work, and a guard's refusal leaves the output as it was; a backend's failure zeroes it. @@ -42,10 +42,10 @@ #include #include -#include -#include #include #include +#include +#include #include @@ -290,7 +290,7 @@ template > [[nodiscard]] boost::system::result kbkdf(std::span output, std::span key, kbkdf_options const& options = {}) { - detail::timing_guard const timing; + secure::timing_guard const timing; if constexpr (!detail::takes_hmac()) { return error::backend_failure; // unreachable: takes_hmac has stopped the build } else { @@ -323,7 +323,7 @@ template > } auto derived = engine::derive(output, key, options); if (!derived) { - detail::wipe(output); + secure::erase_secret(output); } return derived; } @@ -359,7 +359,7 @@ template > [[nodiscard]] boost::system::result sskdf(std::span output, std::span secret, std::span fixed_info = {}) { - detail::timing_guard const timing; + secure::timing_guard const timing; if constexpr (!detail::sskdf_takes()) { return error::backend_failure; // unreachable: sskdf_takes has stopped the build } else { @@ -383,7 +383,7 @@ template > } auto derived = engine::derive(output, secret, fixed_info); if (!derived) { - detail::wipe(output); + secure::erase_secret(output); } return derived; } @@ -421,7 +421,7 @@ template > [[nodiscard]] boost::system::result x963kdf(std::span output, std::span secret, std::span shared_info = {}) { - detail::timing_guard const timing; + secure::timing_guard const timing; if constexpr (!detail::x963kdf_takes()) { return error::backend_failure; // unreachable: x963kdf_takes has stopped the build } else { @@ -444,7 +444,7 @@ template > } auto derived = engine::derive(output, secret, shared_info); if (!derived) { - detail::wipe(output); + secure::erase_secret(output); } return derived; } diff --git a/include/webcpp/hash/mac.hpp b/include/webcpp/hash/mac.hpp index 7bc90bf..7066fba 100644 --- a/include/webcpp/hash/mac.hpp +++ b/include/webcpp/hash/mac.hpp @@ -8,22 +8,23 @@ The MAC family (spec 2.15): `mac`, the tag of a message under a key in one call, over a tag of `macs` and a backend, `default_backend` of the tag by default. - Every call holds the data-independent-timing guard (`detail::timing_guard`): where the CPU has - DIT, `mac` sets it for the call and leaves the thread's state as it found it. + Every call holds the data-independent-timing guard (`webcpp::secure::timing_guard`, secure's + guard): where the CPU has DIT, `mac` sets it for the call and leaves the thread's state as it + found it. The guards are the same on every backend and run before any work: a tag of a size the MAC does not produce is `invalid_output_size`, a key of a size it does not take `invalid_key_size`, a value the MAC accepts and the chosen backend does not `unsupported_by_backend`. A guard's refusal leaves the output as it was; a backend's failure zeroes it. - A program checks a tag with `webcpp::crypto::constant_time_equal`, which runs in constant time, + A program checks a tag with `webcpp::secure::constant_time_equal`, which runs in constant time, never with `==`: @code std::array tag{}; auto done = webcpp::hash::mac>( tag, key, message); - bool genuine = done && webcpp::crypto::constant_time_equal(tag, received_tag); + bool genuine = done && webcpp::secure::constant_time_equal(tag, received_tag); @endcode @see "Message authentication", in the guide. @@ -36,11 +37,11 @@ #include #include #include -#include -#include #include #include #include +#include +#include #include @@ -255,7 +256,7 @@ template > std::span key, std::span input, mac_options const& options = {}) { - detail::timing_guard const timing; + secure::timing_guard const timing; using engine = detail::mac_engine; auto checked = detail::mac_guards(output.size(), key.size(), options); if (!checked) { @@ -266,7 +267,7 @@ template > } auto computed = engine::compute(output, key, input, options); if (!computed) { - detail::wipe(output); + secure::erase_secret(output); } return computed; } diff --git a/include/webcpp/hash/pbkdf2.hpp b/include/webcpp/hash/pbkdf2.hpp index 0bd7f6a..b858df9 100644 --- a/include/webcpp/hash/pbkdf2.hpp +++ b/include/webcpp/hash/pbkdf2.hpp @@ -8,8 +8,8 @@ PBKDF2, RFC 8018: `pbkdf2`, over any Boost.Hash2 hmac whose result is a digest, on the backend the call names or on @ref webcpp::hash::default_kdf_backend. - Every call holds the data-independent-timing guard (`detail::timing_guard`): where the CPU has - DIT, the call sets it and leaves the thread's state as it found it. + Every call holds the data-independent-timing guard (`webcpp::secure::timing_guard`, secure's + guard): where the CPU has DIT, the call sets it and leaves the thread's state as it found it. Every guard runs before any work, and a guard's refusal leaves the output as it was; a backend's failure zeroes it. @@ -33,10 +33,10 @@ #include #include #include -#include -#include #include #include +#include +#include // The backends the program links, each under its WEBCPP_HASH_HAS_, so that a call that names // one finds its engines here; the default, backends::hash2, needs none of them. @@ -128,7 +128,7 @@ template > std::span password_bytes, std::span salt_bytes, std::uint64_t iteration_count) { - detail::timing_guard const timing; + secure::timing_guard const timing; if constexpr (!detail::takes_hmac()) { return error::backend_failure; // unreachable: takes_hmac has stopped the build } else { @@ -140,7 +140,7 @@ template > auto derive_result = detail::pbkdf2_engine::derive( output_bytes, password_bytes, salt_bytes, iteration_count); if (!derive_result) { - detail::wipe(output_bytes); + secure::erase_secret(output_bytes); } return derive_result; } diff --git a/include/webcpp/hash/scrypt.hpp b/include/webcpp/hash/scrypt.hpp index b1c1371..bfd190b 100644 --- a/include/webcpp/hash/scrypt.hpp +++ b/include/webcpp/hash/scrypt.hpp @@ -9,8 +9,8 @@ `crypto_pwhash_scryptsalsa208sha256_ll`, the default), `backends::openssl` (OpenSSL's "SCRYPT", its own memory limit lifted) and `backends::botan` (Botan's "Scrypt", in part). - Every call holds the data-independent-timing guard (`detail::timing_guard`): where the CPU has - DIT, the call sets it and leaves the thread's state as it found it. + Every call holds the data-independent-timing guard (`webcpp::secure::timing_guard`, secure's + guard): where the CPU has DIT, the call sets it and leaves the thread's state as it found it. Every guard runs before any work, and a guard's refusal leaves the output as it was; a backend's failure zeroes it. The backend allocates the 128 r (N + p) bytes the costs name. @@ -37,10 +37,10 @@ #include #include -#include -#include #include #include +#include +#include #include @@ -162,7 +162,7 @@ template > std::span password_bytes, std::span salt_bytes, scrypt_parameters const& cost_parameters) { - detail::timing_guard const timing; + secure::timing_guard const timing; auto guarded = detail::scrypt_guards(output_bytes.size(), password_bytes.size(), salt_bytes.size(), cost_parameters); if (!guarded) { @@ -172,7 +172,7 @@ template > output_bytes, password_bytes, salt_bytes, cost_parameters.cost, cost_parameters.block_size, cost_parameters.parallelism); if (!derive_result) { - detail::wipe(output_bytes); + secure::erase_secret(output_bytes); } return derive_result; } diff --git a/meta/include-boundaries.json b/meta/include-boundaries.json index 83387bd..baae183 100644 --- a/meta/include-boundaries.json +++ b/meta/include-boundaries.json @@ -22,8 +22,6 @@ "include/webcpp/hash/byte_pieces.hpp", "include/webcpp/hash/detail/engines.hpp", "include/webcpp/hash/detail/limits.hpp", - "include/webcpp/hash/detail/timing.hpp", - "include/webcpp/hash/detail/wipe.hpp", "include/webcpp/hash/hkdf.hpp", "include/webcpp/hash/kdf.hpp", "include/webcpp/hash/pbkdf2.hpp" diff --git a/test/Jamfile b/test/Jamfile index d008927..d5770e7 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -66,8 +66,6 @@ webcpp.headers-alone hash : ../include : hash/config.hpp hash/detail/engines.hpp hash/detail/limits.hpp - hash/detail/timing.hpp - hash/detail/wipe.hpp hash/detail/xkcp.hpp hash/digest.hpp hash/error.hpp @@ -212,11 +210,11 @@ hash-run blake3_vectors : blake3_vectors_test.cpp # digests and HMACs on backends::hash2, keccak_256 on backends::xkcp and # BLAKE3's extendable output on backends::blake3 against the Hash2-form types; # every tag's default backend. hasher's state machine, and its wipes reported -# through the seam WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD. +# through the seam WEBCPP_SECURE_TEST_WIPE_RECORD. hash-run families : families_test.cpp : /third_party//xkcp /third_party//blake3 ; hash-run hasher_state : hasher_state_test.cpp - : WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD ; + : WEBCPP_SECURE_TEST_WIPE_RECORD ; # SHAKE128 and SHAKE256 on backends::hash2, a whole extendable output, against # the SHAKE cases of OpenSSL's evpmd_sha.txt, read at run time: FIPS 202's # examples squeezed past several blocks, and NIST's shorter cases. @@ -231,7 +229,7 @@ hash-run hash2_mac_vectors : hash2_mac_vectors_test.cpp # HKDF (RFC 5869) on backends::hash2: the RFC's seven cases and three more # computed by Python's hmac; the guards at their limits; the wipes, each one -# reported through the seam WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD. +# reported through the seam WEBCPP_SECURE_TEST_WIPE_RECORD. hash-run hkdf_hash2 : hkdf_hash2_test.cpp ; hash-run hkdf_guards : hkdf_guards_test.cpp ; @@ -248,22 +246,22 @@ hash-run hkdf_pieces_sliced : hkdf_pieces_test.cpp # crypto_auth_hmacsha512_*) and on backends::bearssl (br_hmac_*, its six # hmacs, Extract and Expand over pieces included), each with the wipe of every # HMAC state, block and pseudorandom key reported through the seam -# WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD, and again in slices of 7 bytes. +# WEBCPP_SECURE_TEST_WIPE_RECORD, and again in slices of 7 bytes. hash-run hkdf_pieces_sodium : hkdf_pieces_sodium_test.cpp - : /third_party//sodium WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD ; + : /third_party//sodium WEBCPP_SECURE_TEST_WIPE_RECORD ; hash-run hkdf_pieces_sodium_sliced : hkdf_pieces_sodium_test.cpp - : /third_party//sodium WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD + : /third_party//sodium WEBCPP_SECURE_TEST_WIPE_RECORD WEBCPP_HASH_DETAIL_TEST_HKDF_PIECES_SLICE=7 ; hash-run hkdf_pieces_sodium_sha512_sliced : hkdf_pieces_sodium_test.cpp - : /third_party//sodium WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD + : /third_party//sodium WEBCPP_SECURE_TEST_WIPE_RECORD WEBCPP_HASH_DETAIL_TEST_HKDF_PIECES_SLICE=7 WEBCPP_HASH_DETAIL_TEST_SLICED_HMAC_SHA2_512 ; hash-run hkdf_pieces_bearssl : hkdf_pieces_bearssl_test.cpp - : /third_party//bearssl WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD ; + : /third_party//bearssl WEBCPP_SECURE_TEST_WIPE_RECORD ; hash-run hkdf_pieces_bearssl_sliced : hkdf_pieces_bearssl_test.cpp - : /third_party//bearssl WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD + : /third_party//bearssl WEBCPP_SECURE_TEST_WIPE_RECORD WEBCPP_HASH_DETAIL_TEST_HKDF_PIECES_SLICE=7 ; -hash-run wipe : wipe_test.cpp : WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD ; +hash-run wipe : wipe_test.cpp : WEBCPP_SECURE_TEST_WIPE_RECORD ; # digests::blake2b on backends::sodium, libsodium's crypto_generichash_blake2b # with key, salt, personalisation and output size: BLAKE2's keyed and unkeyed @@ -283,10 +281,10 @@ hash-run siphash_sodium : siphash_sodium_test.cpp # crypto_generichash_blake2b: BLAKE2's keyed KAT read at run time, Boost.Hash2's # keyed blake2b_512 and digests::blake2b on random cases, every key and tag # size, the guards, and the wipe of libsodium's state, reported through the -# seam WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD. +# seam WEBCPP_SECURE_TEST_WIPE_RECORD. hash-run blake2b_mac_sodium : blake2b_mac_sodium_test.cpp : /third_party//sodium /third_party//blake2_vectors - WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD ; + WEBCPP_SECURE_TEST_WIPE_RECORD ; # blake2b_subkey on backends::sodium, libsodium's # crypto_kdf_blake2b_derive_from_key: kdf.exp read at run time, the @@ -329,9 +327,9 @@ hash-run pbkdf2_guards : pbkdf2_guards_test.cpp ; # HKDF on backends::bearssl: RFC 5869's seven cases through hkdf, the four # other hmacs against backends::hash2, the guards, and the wipe of BearSSL's -# context, reported through the seam WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD. +# context, reported through the seam WEBCPP_SECURE_TEST_WIPE_RECORD. hash-run hkdf_bearssl : hkdf_bearssl_test.cpp - : /third_party//bearssl WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD ; + : /third_party//bearssl WEBCPP_SECURE_TEST_WIPE_RECORD ; # Its read of webcpp::hash's canary survives optimisation: with the library it # links and runs; with another commit's canary it does not link. hash-run hkdf_bearssl_canary : hkdf_bearssl_canary_test.cpp @@ -343,7 +341,7 @@ webcpp.link-fail hkdf_bearssl_canary_other_commit : hkdf_bearssl_canary_test.cpp # through digest_of and hasher, HMAC through mac, against Boost.Hash2 on random # inputs, RFC 4231 and RFC 2202, and the wipe of BearSSL's contexts. hash-run families_bearssl : families_bearssl_test.cpp - : /third_party//bearssl WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD ; + : /third_party//bearssl WEBCPP_SECURE_TEST_WIPE_RECORD ; # HKDF and PBKDF2 on backends::openssl (OpenSSL 3.5.9's EVP_KDF), on the # targets where webcpp builds OpenSSL: the RFCs' cases and backends::hash2's @@ -354,13 +352,13 @@ hash-run hkdf_openssl : hkdf_openssl_test.cpp : /webcpp/hash//openssl : native wasip2 wasip3 ; # HKDF over pieces on backends::openssl, over EVP_MAC "HMAC" (its 15 hmacs): # the equality harness, each block and pseudorandom key wiped through the seam -# WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD, the error queue left empty; again in +# WEBCPP_SECURE_TEST_WIPE_RECORD, the error queue left empty; again in # slices of 7 bytes. hash-run hkdf_pieces_openssl : hkdf_pieces_openssl_test.cpp - : /webcpp/hash//openssl WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD + : /webcpp/hash//openssl WEBCPP_SECURE_TEST_WIPE_RECORD : native wasip2 wasip3 ; hash-run hkdf_pieces_openssl_sliced : hkdf_pieces_openssl_test.cpp - : /webcpp/hash//openssl WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD + : /webcpp/hash//openssl WEBCPP_SECURE_TEST_WIPE_RECORD WEBCPP_HASH_DETAIL_TEST_HKDF_PIECES_SLICE=7 : native wasip2 wasip3 ; hash-run pbkdf2_openssl : pbkdf2_openssl_test.cpp @@ -450,11 +448,11 @@ botan-run kdfs_botan : kdfs_botan_test.cpp ; # HKDF over pieces on backends::botan, over its HMAC (its 14 hmacs): the # equality harness, a salt past Botan's 8,192-byte HMAC key, the digest room, # each block and pseudorandom key wiped through the seam -# WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD; again in slices of 7 bytes. +# WEBCPP_SECURE_TEST_WIPE_RECORD; again in slices of 7 bytes. botan-run hkdf_pieces_botan : hkdf_pieces_botan_test.cpp - : WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD ; + : WEBCPP_SECURE_TEST_WIPE_RECORD ; botan-run hkdf_pieces_botan_sliced : hkdf_pieces_botan_test.cpp - : WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD + : WEBCPP_SECURE_TEST_WIPE_RECORD WEBCPP_HASH_DETAIL_TEST_HKDF_PIECES_SLICE=7 ; # Argon2 on backends::botan: RFC 9106's three cases through the family, # libsodium's table through argon2id, argon2_ctx on random cases with lanes, a @@ -756,10 +754,11 @@ hash-run allocation_openssl : allocation_openssl_test.cpp wasi:-Wl,--wrap=malloc,--wrap=calloc,--wrap=realloc,--wrap=aligned_alloc,--wrap=posix_memalign : native wasip2 wasip3 ; -# 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 for an -# engaged guard and never for a disengaged one. timing_threads adds a second -# thread, natively, with threads. +# webcpp::secure::timing_guard, secure's 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 for an engaged guard and never for a disengaged one, and the +# seam support counts hash's own guards by their file. timing_threads adds a +# second thread, natively, with threads. hash-run timing : timing_test.cpp ; hash-run timing_threads : timing_test.cpp : multi WEBCPP_TEST_TIMING_THREADS : native ; @@ -796,16 +795,17 @@ hash-run timing_during_async : timing_during_test.cpp : native emscripten ; # mac and the keyed modes of BLAKE2b (digest_of, hasher with a key) and of -# BLAKE3 (keyed, derive_key and the seeds that key) hold detail::timing_guard; a -# call that is not keyed holds none; no output changes. +# BLAKE3 (keyed, derive_key and the seeds that key) hold +# webcpp::secure::timing_guard; a call that is not keyed holds none; no output +# changes. hash-run timing_keyed : timing_keyed_test.cpp : $(.hash-tables) /third_party//blake3 /third_party//sodium /third_party//blake3_vectors ; -# The key derivations and password hashes hold detail::timing_guard on every -# backend: timing_keyed covers the default backends'; kbkdf, sskdf and x963kdf, -# and a second cell of the templates, on backends::openssl, here, and bcrypt, -# bcrypt_verify and bcrypt_pbkdf, and a cell of the templates, on +# The key derivations and password hashes hold webcpp::secure::timing_guard on +# every backend: timing_keyed covers the default backends'; kbkdf, sskdf and +# x963kdf, and a second cell of the templates, on backends::openssl, here, and +# bcrypt, bcrypt_verify and bcrypt_pbkdf, and a cell of the templates, on # backends::botan (exceptions on, native and Emscripten). hash-run timing_keyed_openssl : timing_keyed_openssl_test.cpp : /webcpp/hash//openssl : native wasip2 wasip3 ; @@ -820,7 +820,7 @@ botan-run timing_botan_failure : timing_botan_failure_test.cpp # cell of the backend (support/timing_coverage.hpp: macs, key derivations and # password hashes, one per hmac a cell lists, and BLAKE2b with a key, the # blake3 type's keyed modes and bcrypt_verify) enters its own -# detail::timing_guard first; every digest cell without a key, and every +# webcpp::secure::timing_guard first; every digest cell without a key, and every # Boost.Hash2-form type, enters none. A new cell of a family the support # knows gets its call from backend_has_v, which capabilities keeps equal to the # table; a cell of a family or function it does not know fails until its call diff --git a/test/blake2b_mac_sodium_test.cpp b/test/blake2b_mac_sodium_test.cpp index 0d26700..ede8394 100644 --- a/test/blake2b_mac_sodium_test.cpp +++ b/test/blake2b_mac_sodium_test.cpp @@ -12,7 +12,7 @@ // digests::blake2b keyed on backends::sodium at every tag size; every key and tag size of RFC // 7693, those under libsodium's advisory 16-byte minimums included; the family's guards; and the // wipe of libsodium's state after each call, reported through the seam -// WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD. The agreement with backends::openssl on random cases is +// WEBCPP_SECURE_TEST_WIPE_RECORD. The agreement with backends::openssl on random cases is // agreement_families_test's. #include @@ -44,8 +44,8 @@ #include #include -#ifndef WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD -#error "blake2b_mac_sodium_test is built with WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD (test/Jamfile)" +#ifndef WEBCPP_SECURE_TEST_WIPE_RECORD +#error "blake2b_mac_sodium_test is built with WEBCPP_SECURE_TEST_WIPE_RECORD (test/Jamfile)" #endif namespace { @@ -233,8 +233,7 @@ void check_guards() { } // namespace -void webcpp::hash::detail::test::wipe_record(void const* wiped_data, - std::size_t byte_count) noexcept { +void webcpp::secure::test::wipe_record(void const* wiped_data, std::size_t byte_count) noexcept { auto const* const wiped = static_cast(wiped_data); if (!std::all_of(wiped, wiped + byte_count, [](unsigned char byte) { return byte == 0; })) { ++wipes.not_zero; diff --git a/test/families_bearssl_test.cpp b/test/families_bearssl_test.cpp index 70d5351..8518d5e 100644 --- a/test/families_bearssl_test.cpp +++ b/test/families_bearssl_test.cpp @@ -11,7 +11,7 @@ // against RFC 1321's and FIPS 180's printed digests of "abc", an oracle that is not Hash2; HMAC // against RFC 4231 (SHA-2) and RFC 2202 (MD5, SHA-1); SHAKE against FIPS 202's empty-input // outputs; the wipe of BearSSL's contexts after a call, when a hasher finishes and when a hasher -// is dropped unfinished, reported through the seam WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD (spec 6.1); +// is dropped unfinished, reported through the seam WEBCPP_SECURE_TEST_WIPE_RECORD (spec 6.1); // and backends::bearssl never a default (design D34). #include @@ -47,8 +47,8 @@ #include #include -#ifndef WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD -#error "families_bearssl_test is built with WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD (test/Jamfile)" +#ifndef WEBCPP_SECURE_TEST_WIPE_RECORD +#error "families_bearssl_test is built with WEBCPP_SECURE_TEST_WIPE_RECORD (test/Jamfile)" #endif namespace { @@ -389,8 +389,7 @@ static_assert(!hash::backend_has_v, void> } // namespace -void webcpp::hash::detail::test::wipe_record(void const* wiped_data, - std::size_t byte_count) noexcept { +void webcpp::secure::test::wipe_record(void const* wiped_data, std::size_t byte_count) noexcept { auto const* const bytes = static_cast(wiped_data); if (wipes.count < wipes.sizes.size()) { wipes.sizes[wipes.count] = byte_count; diff --git a/test/hasher_state_test.cpp b/test/hasher_state_test.cpp index dc0299d..49f96c8 100644 --- a/test/hasher_state_test.cpp +++ b/test/hasher_state_test.cpp @@ -7,7 +7,7 @@ // hasher's state machine (spec 2.15): make, update, finish once; invalid_state after finish, after // a backend's failure and once moved from; a guard's refusal at finish leaves it hashing; // move-only. On the mock backend, every engine call is counted and every init is matched by one -// destroy, in every path; on backends::hash2, built with WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD, the +// destroy, in every path; on backends::hash2, built with WEBCPP_SECURE_TEST_WIPE_RECORD, the // state's bytes inside the hasher are wiped by finish, by the destructor and in the hasher moved // from. @@ -29,8 +29,8 @@ #include #include -#ifndef WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD -#error "hasher_state_test is built with WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD (test/Jamfile)" +#ifndef WEBCPP_SECURE_TEST_WIPE_RECORD +#error "hasher_state_test is built with WEBCPP_SECURE_TEST_WIPE_RECORD (test/Jamfile)" #endif namespace { @@ -62,7 +62,7 @@ wipe_log wipes; } // namespace -namespace webcpp::hash::detail::test { +namespace webcpp::secure::test { void wipe_record(void const* wiped_data, std::size_t byte_count) noexcept { auto const* const bytes = static_cast(wiped_data); @@ -76,7 +76,7 @@ void wipe_record(void const* wiped_data, std::size_t byte_count) noexcept { } } -} // namespace webcpp::hash::detail::test +} // namespace webcpp::secure::test namespace { diff --git a/test/hkdf_bearssl_test.cpp b/test/hkdf_bearssl_test.cpp index 3d65121..796b85d 100644 --- a/test/hkdf_bearssl_test.cpp +++ b/test/hkdf_bearssl_test.cpp @@ -8,7 +8,7 @@ // over br_sha1_vtable), A.7's absent salt spelled as HashLen zeros too; the four hmacs RFC 5869 // has no case for against backends::hash2; the guards at their limits, which refuse before // BearSSL is reached; and the wipe of BearSSL's context after each call, reported through the -// seam WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD (spec sections 3.4 and 6.1). +// seam WEBCPP_SECURE_TEST_WIPE_RECORD (spec sections 3.4 and 6.1). #include #include @@ -32,8 +32,8 @@ #include #include -#ifndef WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD -#error "hkdf_bearssl_test is built with WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD (test/Jamfile)" +#ifndef WEBCPP_SECURE_TEST_WIPE_RECORD +#error "hkdf_bearssl_test is built with WEBCPP_SECURE_TEST_WIPE_RECORD (test/Jamfile)" #endif namespace hash = webcpp::hash; @@ -161,8 +161,7 @@ void guards() { } // namespace -void webcpp::hash::detail::test::wipe_record(void const* wiped_data, - std::size_t byte_count) noexcept { +void webcpp::secure::test::wipe_record(void const* wiped_data, std::size_t byte_count) noexcept { const auto* const bytes = static_cast(wiped_data); ++wipes.count; wipes.last_size = byte_count; diff --git a/test/hkdf_pieces_bearssl_test.cpp b/test/hkdf_pieces_bearssl_test.cpp index 6fdb8c3..cfde57e 100644 --- a/test/hkdf_pieces_bearssl_test.cpp +++ b/test/hkdf_pieces_bearssl_test.cpp @@ -14,7 +14,7 @@ // refused (test/messages/extract_on_bearssl and expand_on_bearssl): the spec's scenario // hkdf_extract(salt, {a, b}). Each step wipes BearSSL's HMAC // contexts, its key contexts, every block T(i) and the pseudorandom key between the steps, -// reported through the seam WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD; a guard's refusal wipes nothing. +// reported through the seam WEBCPP_SECURE_TEST_WIPE_RECORD; a guard's refusal wipes nothing. // Built again as hkdf_pieces_bearssl_sliced with the test seam // WEBCPP_HASH_DETAIL_TEST_HKDF_PIECES_SLICE, which makes the engine feed every piece to BearSSL in // slices of a few bytes (Review Focus 4), over hmac_sha2_256: the bytes are the same, 1 MiB pieces @@ -46,8 +46,8 @@ #include #include -#ifndef WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD -#error "hkdf_pieces_bearssl_test is built with WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD (test/Jamfile)" +#ifndef WEBCPP_SECURE_TEST_WIPE_RECORD +#error "hkdf_pieces_bearssl_test is built with WEBCPP_SECURE_TEST_WIPE_RECORD (test/Jamfile)" #endif namespace hash = webcpp::hash; diff --git a/test/hkdf_pieces_botan_test.cpp b/test/hkdf_pieces_botan_test.cpp index 7f7fa71..0500ab5 100644 --- a/test/hkdf_pieces_botan_test.cpp +++ b/test/hkdf_pieces_botan_test.cpp @@ -14,7 +14,7 @@ // A salt longer than the 8,192 bytes Botan's HMAC takes as a key is hashed first, as RFC 2104 does, // and gives the same pseudorandom key. Botan's HMAC object is cleared (its clear()) and freed; hash // wipes the digest room of a long salt, each block T(i) and the pseudorandom key between the steps, -// reported through the seam WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD; a guard's refusal wipes nothing. +// reported through the seam WEBCPP_SECURE_TEST_WIPE_RECORD; a guard's refusal wipes nothing. // Built again as hkdf_pieces_botan_sliced with the test seam // WEBCPP_HASH_DETAIL_TEST_HKDF_PIECES_SLICE, which makes the engine feed every piece to Botan in // slices of a few bytes (Review Focus 4), over hmac_sha2_256: the bytes are the same, 1 MiB pieces @@ -44,8 +44,8 @@ #include #include -#ifndef WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD -#error "hkdf_pieces_botan_test is built with WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD (test/Jamfile)" +#ifndef WEBCPP_SECURE_TEST_WIPE_RECORD +#error "hkdf_pieces_botan_test is built with WEBCPP_SECURE_TEST_WIPE_RECORD (test/Jamfile)" #endif namespace hash = webcpp::hash; diff --git a/test/hkdf_pieces_openssl_test.cpp b/test/hkdf_pieces_openssl_test.cpp index 937cc8c..dc587a9 100644 --- a/test/hkdf_pieces_openssl_test.cpp +++ b/test/hkdf_pieces_openssl_test.cpp @@ -13,7 +13,7 @@ // 5869's sizes (support/hkdf_pieces.hpp), over the 15 hmacs of OpenSSL's HMAC cells, every hmac of // hmac.hpp but hmac_ripemd_128. The cells equal those of kdfs::hkdf. OpenSSL's contexts are its // own, freed by EVP_MAC_CTX_free; hash wipes each block T(i) and the pseudorandom key between the -// steps, reported through the seam WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD; a guard's refusal wipes +// steps, reported through the seam WEBCPP_SECURE_TEST_WIPE_RECORD; a guard's refusal wipes // nothing; the thread's error queue is left empty. Built again as hkdf_pieces_openssl_sliced with // the test seam WEBCPP_HASH_DETAIL_TEST_HKDF_PIECES_SLICE, which makes the engine feed every piece // to OpenSSL in slices of a few bytes (Review Focus 4), over hmac_sha2_256: the bytes are the same, @@ -43,8 +43,8 @@ #include #include -#ifndef WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD -#error "hkdf_pieces_openssl_test is built with WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD (test/Jamfile)" +#ifndef WEBCPP_SECURE_TEST_WIPE_RECORD +#error "hkdf_pieces_openssl_test is built with WEBCPP_SECURE_TEST_WIPE_RECORD (test/Jamfile)" #endif namespace hash = webcpp::hash; diff --git a/test/hkdf_pieces_sodium_test.cpp b/test/hkdf_pieces_sodium_test.cpp index eebd0ed..f59a1a9 100644 --- a/test/hkdf_pieces_sodium_test.cpp +++ b/test/hkdf_pieces_sodium_test.cpp @@ -12,7 +12,7 @@ // (support/hkdf_pieces.hpp), over hmac_sha2_256 and hmac_sha2_512, the hmacs of libsodium's HMAC // cells. The cells equal those of kdfs::hkdf. Each step wipes libsodium's HMAC states, every // block T(i) and the pseudorandom key between the steps, reported through the seam -// WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD; a guard's refusal wipes nothing. Built again as +// WEBCPP_SECURE_TEST_WIPE_RECORD; a guard's refusal wipes nothing. Built again as // hkdf_pieces_sodium_sliced with the test seam WEBCPP_HASH_DETAIL_TEST_HKDF_PIECES_SLICE, which // makes the engine feed every piece to libsodium in slices of a few bytes (Review Focus 4), over // hmac_sha2_256: the bytes are the same, 1 MiB pieces included. hkdf_pieces_sodium_sha512_sliced @@ -45,8 +45,8 @@ #include #include -#ifndef WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD -#error "hkdf_pieces_sodium_test is built with WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD (test/Jamfile)" +#ifndef WEBCPP_SECURE_TEST_WIPE_RECORD +#error "hkdf_pieces_sodium_test is built with WEBCPP_SECURE_TEST_WIPE_RECORD (test/Jamfile)" #endif namespace hash = webcpp::hash; diff --git a/test/support/argon2_reference.hpp b/test/support/argon2_reference.hpp index a5114f1..e95f353 100644 --- a/test/support/argon2_reference.hpp +++ b/test/support/argon2_reference.hpp @@ -11,7 +11,7 @@ #ifndef WEBCPP_TEST_HASH_SUPPORT_ARGON2_REFERENCE_HPP #define WEBCPP_TEST_HASH_SUPPORT_ARGON2_REFERENCE_HPP -#include +#include #include #include @@ -61,7 +61,7 @@ inline int argon2_reference(std::span tag, argon2_reference_case c }; // NOLINTEND(cppcoreguidelines-pro-type-const-cast) int const code = argon2_ctx(&context, c.variant); - webcpp::hash::detail::wipe_object(context); + webcpp::secure::erase_secret(std::span(&context, 1)); return code; } diff --git a/test/support/timing_coverage.hpp b/test/support/timing_coverage.hpp index dafb464..1bcbf00 100644 --- a/test/support/timing_coverage.hpp +++ b/test/support/timing_coverage.hpp @@ -4,23 +4,22 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// The coverage of detail::timing_guard, driven by the capability table: every keyed cell of a -// backend in doc/capabilities.json enters its own guard first and leaves the thread's DIT as it -// found it, and every digest cell of the backend, called without a key, enters none (design D1, -// D2). Each backend's timing_coverage__test.cpp builds its calls from the helpers here -// and hands them to check_and_run, which fails on a keyed cell of the table with no call and on a -// call that names no keyed cell; then it walks the backend's digest cells through -// uncover_digests. A test includes this header before any hash header. +// The coverage of webcpp::secure::timing_guard, secure's guard, driven by the capability table: +// every keyed cell of a backend in doc/capabilities.json enters its own guard first and leaves the +// thread's DIT as it found it, and every digest cell of the backend, called without a key, enters +// none (design D1, D2). Each backend's timing_coverage__test.cpp builds its calls from the +// helpers here and hands them to check_and_run, which fails on a keyed cell of the table with no +// call and on a call that names no keyed cell; then it walks the backend's digest cells through +// uncover_digests. A test includes this header before any webcpp header. // -// covered, and the support's shape, are a copy of libs/crypto/test/support/timing_coverage.hpp -// with hash's guard: the duplication is accepted for the reason of detail/timing.hpp's own (hash -// includes nothing of crypto). The keyed cells are the table's (ruling H6): every function of -// the chapters macs, kdfs and password_hashing whose kind is not "type", each backend's cell, one -// per hmac where the cell lists hmacs ("other": a Hash2 hmac the list does not name); plus three -// keyed functions the table holds no cell of, named here with design D1 as the reason: BLAKE2b -// with a key (digest_of and a hasher round) wherever the backend has digests::blake2b, the blake3 -// type's keyed modes on backends::blake3, and bcrypt_verify wherever the backend has bcrypt -// (H12). backends::webcrypto has no coverage test (D6). +// covered and uncovered count hash's own guards, timing_entries (support/timing_seam.hpp), so +// that a guard of another library a call enters is not taken for hash's. The keyed cells are the +// table's (ruling H6): every function of the chapters macs, kdfs and password_hashing whose kind is +// not "type", each backend's cell, one per hmac where the cell lists hmacs ("other": a Hash2 hmac +// the list does not name); plus three keyed functions the table holds no cell of, named here with +// design D1 as the reason: BLAKE2b with a key (digest_of and a hasher round) wherever the backend +// has digests::blake2b, the blake3 type's keyed modes on backends::blake3, and bcrypt_verify +// wherever the backend has bcrypt (H12). backends::webcrypto has no coverage test (D6). #ifndef WEBCPP_TEST_HASH_SUPPORT_TIMING_COVERAGE_HPP #define WEBCPP_TEST_HASH_SUPPORT_TIMING_COVERAGE_HPP @@ -31,7 +30,6 @@ #include #include #include -#include #include #include #include @@ -41,6 +39,7 @@ #include #include #include +#include #include "data_file.hpp" #include "defaults.hpp" @@ -68,9 +67,9 @@ namespace webcpp::test { /** - * Runs `call` and reports an error when it entered no detail::timing_guard, when the first guard - * it entered is not the one of `function` (support/timing_seam.hpp), or when it left the thread's - * DIT other than it found it. + * Runs `call` and reports an error when it entered no webcpp::secure::timing_guard of hash's, when + * the first guard it entered is not the one of `function` (support/timing_seam.hpp), or when it + * left the thread's DIT other than it found it. * * @param name What the call is, `::[ how] on `, as an error names * it. @@ -80,9 +79,9 @@ namespace webcpp::test { */ template void covered(std::string const& name, std::string const& function, Call&& call) { - bool const before = webcpp::hash::detail::dit_is_set(); + bool const before = webcpp::secure::dit_is_set(); require_enters(name, function, std::forward(call)); - if (webcpp::hash::detail::dit_is_set() != before) { + if (webcpp::secure::dit_is_set() != before) { std::string const message = name + (before ? " left DIT clear, though it found it set" : " left DIT set, though it found it clear"); BOOST_ERROR(message.c_str()); @@ -103,10 +102,10 @@ void covered(std::string const& name, Call&& call) { } /** - * Runs `call` and reports an error when it entered a detail::timing_guard: a public function that - * computes on public data alone (a digest without a key, a Hash2 type) holds none (design D2, - * D3). A guard added to one by mistake costs every call, and makes the rule "a guard where a key - * is" harder to check. + * Runs `call` and reports an error when it entered a webcpp::secure::timing_guard of hash's: a + * public function that computes on public data alone (a digest without a key, a Hash2 type) holds + * none (design D2, D3). A guard added to one by mistake costs every call, and makes the rule "a + * guard where a key is" harder to check. * * @param name What the call is, `::[ how] on `, as an error names * it. @@ -118,7 +117,8 @@ void uncovered(std::string_view name, Call&& call) { std::forward(call)(); if (timing_entries != before) { std::string const message = - std::string(name) + " entered a detail::timing_guard it should not hold"; + std::string(name) + + " entered a webcpp::secure::timing_guard of hash's it should not hold"; BOOST_ERROR(message.c_str()); } } diff --git a/test/support/timing_dit.hpp b/test/support/timing_dit.hpp index 7882c63..e357ba6 100644 --- a/test/support/timing_dit.hpp +++ b/test/support/timing_dit.hpp @@ -4,31 +4,31 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// The seam of detail::timing_guard (support/timing_seam.hpp) and a test's control of the thread's -// DIT: force_dit sets or clears it, as a program can, so that a test starts from a known state. -// Like the seam, a test includes this header before any hash header. +// The seam of webcpp::secure::timing_guard (support/timing_seam.hpp) and a test's control of the +// thread's DIT: force_dit sets or clears it, as a program can, so that a test starts from a known +// state. Like the seam, a test includes this header before any webcpp header. #ifndef WEBCPP_TEST_HASH_SUPPORT_TIMING_DIT_HPP #define WEBCPP_TEST_HASH_SUPPORT_TIMING_DIT_HPP #include "timing_seam.hpp" -#include +#include namespace webcpp::test { /** * Sets or clears DIT for this thread, as a program can; only where the CPU has DIT, since the - * instruction traps on a CPU without it. + * instruction traps on a CPU without it, and nothing where the guard does not act. * * @param set Whether to set DIT. */ inline void force_dit([[maybe_unused]] bool set) noexcept { -#ifdef WEBCPP_HASH_DETAIL_TIMING_COVERED - if (!webcpp::hash::detail::cpu_has_dit()) { +#ifdef WEBCPP_SECURE_TIMING_COVERED + if (!webcpp::secure::cpu_has_dit()) { return; } - webcpp::hash::detail::write_dit(set ? webcpp::hash::detail::dit_bit : 0); + webcpp::secure::detail::write_dit(set ? webcpp::secure::detail::dit_bit : 0); #endif } diff --git a/test/support/timing_seam.hpp b/test/support/timing_seam.hpp index a2354d3..5fc75be 100644 --- a/test/support/timing_seam.hpp +++ b/test/support/timing_seam.hpp @@ -4,22 +4,27 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// The seam of detail::timing_guard, recorded: a test includes this header before any hash -// header, so that every guard a call enters raises timing_entries and the first records the -// function that holds it, on every target, also where DIT does not apply. require_enters then -// checks that a call entered a guard, and that the first, the outermost since a guard is its -// function's first statement, is the one of the function the check names: a public function that -// forgot its own guard fails, also when it calls another guarded function. The converse, a call -// that enters no guard, is support/timing_coverage.hpp's uncovered. -// -// This is a copy of libs/crypto/test/support/timing_seam.hpp with hash's macro: the duplication is -// accepted for the reason of detail/timing.hpp's own (hash includes nothing of crypto). +// The seam of webcpp::secure::timing_guard, secure's guard, recorded for hash's tests: a test +// includes this header before any webcpp header, so that the seam is defined before +// is first read and every engaged guard a call enters is +// counted, on every target, also where DIT does not apply. A guard is hash's own when one of +// hash's headers declares it: its source_location's file_name() holds include/webcpp/hash/, with +// `/` or `\` around each word (MSVC writes backslashes, and joins an include directory to a +// header's path with either); the include root keeps a checkout whose own folders read +// webcpp/hash/ from counting a test file's guard. timing_entries counts hash's own guards and +// timing_all_entries every guard: another library's, which a call may enter inside its own, and a +// test's own. require_enters then checks that a call entered a guard of hash's, and that the first +// guard it entered, the outermost since a guard is its function's first statement, is the one of +// the function the check names: a public function that forgot its own guard fails, also when it +// calls another guarded function. The converse, a call that enters no guard of hash's, is +// support/timing_coverage.hpp's uncovered. The recorder is the form of secure's +// (libs/secure/test/support/timing_seam.hpp) in hash's namespace webcpp::test, counting hash's. #ifndef WEBCPP_TEST_HASH_SUPPORT_TIMING_SEAM_HPP #define WEBCPP_TEST_HASH_SUPPORT_TIMING_SEAM_HPP -#ifdef WEBCPP_HASH_DETAIL_TIMING_HPP -#error "support/timing_seam.hpp must be included before any hash header" +#ifdef WEBCPP_SECURE_TIMING_GUARD_HPP +#error "\"support/timing_seam.hpp\" must be included before any webcpp header" #endif #include @@ -32,34 +37,82 @@ namespace webcpp::test { -/** The number of detail::timing_guard objects this thread has constructed. */ +/** Every timing guard this thread entered: hash's, another library's, a test's. */ +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): the seam's counter +inline thread_local std::size_t timing_all_entries = 0; + +/** The timing guards of hash's headers this thread has entered. */ // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): the seam's counter inline thread_local std::size_t timing_entries = 0; /** - * The `function_name()` of the first detail::timing_guard this thread entered since it was last + * The `function_name()` of the first guard, of any file, this thread entered since it was last * reset to null: the function that holds that guard, as the compiler spells it. */ // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): the seam's record inline thread_local char const* timing_first_entry = nullptr; +/** The `function_name()` of the last guard, of any file, this thread entered. */ +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): the seam's record +inline thread_local char const* timing_last_entry = nullptr; + +/** The library whose guards timing_entries counts. */ +inline constexpr std::string_view timing_library = "hash"; + +/** Whether `character` separates a path's components, on any system webcpp builds on. */ +constexpr bool path_separator(char character) noexcept { + return character == '/' || character == '\\'; +} + /** - * Records the entry into a detail::timing_guard: counts it, and keeps its function when it is the - * first since timing_first_entry was reset. + * Whether a path names a file under a library's include directory: the components `include`, + * `webcpp` and `library`, in that order, each followed by a separator, `/` or `\` in any mix, and + * `include` the path's first component or after a separator. + * + * @param path A `file_name()`. + * @param library The library's directory name. + * @return Whether the path is under `include/webcpp//`. + */ +constexpr bool path_in_library(std::string_view path, std::string_view library) noexcept { + constexpr std::string_view include_root = "include"; + constexpr std::string_view root = "webcpp"; + for (std::size_t at = path.find(include_root); at != std::string_view::npos; + at = path.find(include_root, at + 1)) { + std::size_t const root_at = at + include_root.size() + 1; + std::size_t const library_at = root_at + root.size() + 1; + std::size_t const after = library_at + library.size(); + if ((at == 0 || path_separator(path[at - 1])) && after < path.size() && + path_separator(path[root_at - 1]) && path.substr(root_at, root.size()) == root && + path_separator(path[library_at - 1]) && + path.substr(library_at, library.size()) == library && path_separator(path[after])) { + return true; + } + } + return false; +} + +/** + * Records the entry into a webcpp::secure::timing_guard: counts it, as hash's when its file is + * under include/webcpp/hash/, and keeps its function as the first since timing_first_entry was + * reset and as the last. * * @param where Where the guard is declared. */ inline void timing_entered(std::source_location const& where) noexcept { - ++timing_entries; + ++timing_all_entries; + if (path_in_library(where.file_name(), timing_library)) { + ++timing_entries; + } if (timing_first_entry == nullptr) { timing_first_entry = where.function_name(); } + timing_last_entry = where.function_name(); } } // namespace webcpp::test -/** Records the entry into a detail::timing_guard declared at `where`. */ -#define WEBCPP_HASH_DETAIL_TIMING_ENTERED(where) (::webcpp::test::timing_entered(where)) +/** Records the entry into a webcpp::secure::timing_guard declared at `where`. */ +#define WEBCPP_SECURE_TIMING_ENTERED(where) (::webcpp::test::timing_entered(where)) namespace webcpp::test { @@ -240,8 +293,8 @@ inline bool names_function(std::string_view entered, named_function const& named } /** - * Runs `call` and reports an error when it entered no detail::timing_guard, or when the first - * guard it entered is not the one of `function`. + * Runs `call` and reports an error when it entered no webcpp::secure::timing_guard of hash's, or + * when the first guard it entered is not the one of `function`. * * @param name What the call is, as an error names it. * @param function The function whose guard the call must enter first, as `function_named` reads @@ -255,7 +308,8 @@ void require_enters(std::string_view name, std::string_view function, Call&& cal timing_first_entry = nullptr; std::forward(call)(); if (timing_entries == before) { - std::string const message = std::string(name) + " did not enter detail::timing_guard"; + std::string const message = + std::string(name) + " did not enter a webcpp::secure::timing_guard of hash's"; BOOST_ERROR(message.c_str()); } else if (function_named(function).scope.empty()) { std::string const message = @@ -263,7 +317,7 @@ void require_enters(std::string_view name, std::string_view function, Call&& cal BOOST_ERROR(message.c_str()); } else if (!names_function(timing_first_entry, function_named(function))) { std::string const message = std::string(name) + - " entered first the detail::timing_guard of " + + " entered first the webcpp::secure::timing_guard of " + timing_first_entry + ", not its own"; BOOST_ERROR(message.c_str()); } @@ -271,8 +325,8 @@ void require_enters(std::string_view name, std::string_view function, Call&& cal } /** - * Runs `call` and reports an error when it entered no detail::timing_guard, or when the first - * guard it entered is not the one of the function the name begins with. + * Runs `call` and reports an error when it entered no webcpp::secure::timing_guard of hash's, or + * when the first guard it entered is not the one of the function the name begins with. * * @param name What the call is, `::::[ how] on `, which * names the function as `function_named` reads it. diff --git a/test/support/wipe_record.hpp b/test/support/wipe_record.hpp index 2c5c3f2..e93b12f 100644 --- a/test/support/wipe_record.hpp +++ b/test/support/wipe_record.hpp @@ -4,20 +4,20 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// The test seam of detail::wipe, recorded: a program built with -// WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD includes this header once, which defines the seam's function -// detail::test::wipe_record. Each wipe is recorded in wipes, and wiped_sizes() returns the sizes -// of the wipes since its last call, checking that each wipe left only zero bytes; clear_wipes() -// discards them, however many. +// The test seam of webcpp::secure::detail::wipe, which every secure::erase_secret of hash's runs, +// recorded: a program built with WEBCPP_SECURE_TEST_WIPE_RECORD includes this header once, which +// defines the seam's function webcpp::secure::test::wipe_record. Each wipe is recorded in wipes, +// and wiped_sizes() returns the sizes of the wipes since its last call, checking that each wipe +// left only zero bytes; clear_wipes() discards them, however many. #ifndef WEBCPP_TEST_HASH_SUPPORT_WIPE_RECORD_HPP #define WEBCPP_TEST_HASH_SUPPORT_WIPE_RECORD_HPP -#ifndef WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD -#error "support/wipe_record.hpp is for a program built with WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD" +#ifndef WEBCPP_SECURE_TEST_WIPE_RECORD +#error "support/wipe_record.hpp is for a program built with WEBCPP_SECURE_TEST_WIPE_RECORD" #endif -#include +#include #include @@ -39,7 +39,7 @@ struct wipe_log { std::size_t not_zero = 0; }; -/** The seam's record, which detail::wipe reaches through detail::test::wipe_record. */ +/** The seam's record, which secure::detail::wipe reaches through secure::test::wipe_record. */ // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): the seam's record inline wipe_log wipes; @@ -69,11 +69,10 @@ inline void clear_wipes() { } // namespace webcpp::test -// The seam's function, declared by detail/wipe.hpp, defined once by the program that includes -// this header. +// The seam's function, declared by , defined once by the program +// that includes this header. // NOLINTBEGIN(misc-definitions-in-headers) -void webcpp::hash::detail::test::wipe_record(void const* wiped_data, - std::size_t byte_count) noexcept { +void webcpp::secure::test::wipe_record(void const* wiped_data, std::size_t byte_count) noexcept { auto& log = ::webcpp::test::wipes; auto const* const bytes = static_cast(wiped_data); // Checked here, so that the store cannot throw: the seam is noexcept. A wipe past the storage diff --git a/test/timing_async_test.cpp b/test/timing_async_test.cpp index 186163e..6d32e5d 100644 --- a/test/timing_async_test.cpp +++ b/test/timing_async_test.cpp @@ -24,7 +24,6 @@ #include #include -#include #include #include #include @@ -32,6 +31,7 @@ #include #include #include +#include #ifdef WEBCPP_TEST_TIMING_ASYNC_BOTAN #include #include @@ -117,7 +117,7 @@ record read_this_thread() { return { .entries = test::timing_entries, .first = test::timing_first_entry != nullptr ? test::timing_first_entry : "", - .dit_off = !hash::detail::dit_is_set(), + .dit_off = !webcpp::secure::dit_is_set(), }; } diff --git a/test/timing_botan_failure_test.cpp b/test/timing_botan_failure_test.cpp index 28bf89b..3300b9a 100644 --- a/test/timing_botan_failure_test.cpp +++ b/test/timing_botan_failure_test.cpp @@ -7,20 +7,20 @@ // A failure of Botan inside a guard (design D1, ruling H10): through the seam // WEBCPP_HASH_DETAIL_TEST_BOTAN_NAME, which the test's Jamfile defines to "NO-SUCH-HASH", Botan's // create_or_throw throws Lookup_Error inside detail::botan_boundary, after the public function's -// detail::timing_guard is entered; the boundary answers backend_failure, and the guard's scope -// ends by that return. Each of mac, hkdf_extract, hkdf_expand, hkdf and pbkdf2 enters its own +// webcpp::secure::timing_guard is entered; the boundary answers backend_failure, and the guard's +// scope ends by that return. Each of mac, hkdf_extract, hkdf_expand, hkdf and pbkdf2 enters its own // guard first and leaves the thread's DIT as it found it, from a thread with DIT clear and from // one that set DIT itself (where the CPU has DIT). #include "support/timing_dit.hpp" #include -#include #include #include #include #include #include +#include #include #include @@ -52,10 +52,10 @@ bool backend_failed(boost::system::result const& outcome) { // Each call, from DIT set as `set` says: its own guard first, backend_failure, DIT as found. void fails_inside_guard(bool set) { test::force_dit(set); - bool const before = hash::detail::dit_is_set(); + bool const before = webcpp::secure::dit_is_set(); std::string const from = set ? " from DIT set" : " from DIT clear"; auto const restored = [&](std::string const& name) { - if (hash::detail::dit_is_set() != before) { + if (webcpp::secure::dit_is_set() != before) { BOOST_ERROR((name + from + " left DIT other than it found it").c_str()); } }; @@ -90,6 +90,6 @@ void fails_inside_guard(bool set) { int main() { fails_inside_guard(false); fails_inside_guard(true); - BOOST_TEST(!hash::detail::dit_is_set()); + BOOST_TEST(!webcpp::secure::dit_is_set()); return webcpp::test::exit_status(boost::report_errors()); } diff --git a/test/timing_coverage_bearssl_test.cpp b/test/timing_coverage_bearssl_test.cpp index 0526dd4..a536a05 100644 --- a/test/timing_coverage_bearssl_test.cpp +++ b/test/timing_coverage_bearssl_test.cpp @@ -4,9 +4,9 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// Every keyed cell of backends::bearssl enters its own detail::timing_guard first and leaves the -// thread's DIT as it found it, on every target of the backend, also where DIT does not apply, -// each with valid input and with input refused before the backend; every digest cell, called +// Every keyed cell of backends::bearssl enters its own webcpp::secure::timing_guard first and +// leaves the thread's DIT as it found it, on every target of the backend, also where DIT does not +// apply, each with valid input and with input refused before the backend; every digest cell, called // without a key, enters none (support/timing_coverage.hpp). // // What it covers, by family; the list is doc/capabilities.json's, read at run time. A new cell of diff --git a/test/timing_coverage_blake3_test.cpp b/test/timing_coverage_blake3_test.cpp index 277c88d..0c808d0 100644 --- a/test/timing_coverage_blake3_test.cpp +++ b/test/timing_coverage_blake3_test.cpp @@ -5,9 +5,9 @@ // https://www.boost.org/LICENSE_1_0.txt) // The keyed modes of the blake3 type, the one keyed function of backends::blake3, enter their own -// detail::timing_guard first and leave the thread's DIT as it found it, on every target, also -// where DIT does not apply; its digest cell, called through digest_of and a hasher round, and the -// blake3 type in its plain mode enter none (support/timing_coverage.hpp). +// webcpp::secure::timing_guard first and leave the thread's DIT as it found it, on every target, +// also where DIT does not apply; its digest cell, called through digest_of and a hasher round, and +// the blake3 type in its plain mode enter none (support/timing_coverage.hpp). // // What it covers, by family; the list is doc/capabilities.json's, read at run time. A new cell of // a family the support knows gets its call from backend_has_v, which capabilities_test keeps equal diff --git a/test/timing_coverage_botan_test.cpp b/test/timing_coverage_botan_test.cpp index 6902691..1d143ed 100644 --- a/test/timing_coverage_botan_test.cpp +++ b/test/timing_coverage_botan_test.cpp @@ -4,8 +4,8 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// Every keyed cell of backends::botan enters its own detail::timing_guard first and leaves the -// thread's DIT as it found it, on every target of the backend, also where DIT does not apply, +// Every keyed cell of backends::botan enters its own webcpp::secure::timing_guard first and leaves +// the thread's DIT as it found it, on every target of the backend, also where DIT does not apply, // each with valid input and with input refused before the backend; every digest cell, called // without a key, enters none (support/timing_coverage.hpp). // diff --git a/test/timing_coverage_hash2_test.cpp b/test/timing_coverage_hash2_test.cpp index a8a1b9b..c5f3969 100644 --- a/test/timing_coverage_hash2_test.cpp +++ b/test/timing_coverage_hash2_test.cpp @@ -4,8 +4,8 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// Every keyed cell of backends::hash2 enters its own detail::timing_guard first and leaves the -// thread's DIT as it found it, on every target, also where DIT does not apply, each with valid +// Every keyed cell of backends::hash2 enters its own webcpp::secure::timing_guard first 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; every digest cell, called without a key, // enters none; and so does every Boost.Hash2-form type of hmac.hpp and algorithms.hpp, seeded or // not (design D3) (support/timing_coverage.hpp). diff --git a/test/timing_coverage_openssl_test.cpp b/test/timing_coverage_openssl_test.cpp index 8d2daed..f29c5b2 100644 --- a/test/timing_coverage_openssl_test.cpp +++ b/test/timing_coverage_openssl_test.cpp @@ -4,9 +4,9 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// Every keyed cell of backends::openssl enters its own detail::timing_guard first and leaves the -// thread's DIT as it found it, on every target of the backend, also where DIT does not apply, -// each with valid input and with input refused before the backend; every digest cell, called +// Every keyed cell of backends::openssl enters its own webcpp::secure::timing_guard first and +// leaves the thread's DIT as it found it, on every target of the backend, also where DIT does not +// apply, each with valid input and with input refused before the backend; every digest cell, called // without a key, enters none (support/timing_coverage.hpp). // // What it covers, by family; the list is doc/capabilities.json's, read at run time. A new cell of diff --git a/test/timing_coverage_sodium_test.cpp b/test/timing_coverage_sodium_test.cpp index 8c73d9b..c19722c 100644 --- a/test/timing_coverage_sodium_test.cpp +++ b/test/timing_coverage_sodium_test.cpp @@ -4,8 +4,8 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// Every keyed cell of backends::sodium enters its own detail::timing_guard first and leaves the -// thread's DIT as it found it, on every target of the backend, also where DIT does not apply, +// Every keyed cell of backends::sodium enters its own webcpp::secure::timing_guard first and leaves +// the thread's DIT as it found it, on every target of the backend, also where DIT does not apply, // each with valid input and with input refused before the backend; every digest cell, called // without a key, enters none (support/timing_coverage.hpp). // diff --git a/test/timing_coverage_xkcp_test.cpp b/test/timing_coverage_xkcp_test.cpp index 81de519..dadd360 100644 --- a/test/timing_coverage_xkcp_test.cpp +++ b/test/timing_coverage_xkcp_test.cpp @@ -4,8 +4,8 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// Every keyed cell of backends::xkcp enters its own detail::timing_guard first and leaves the -// thread's DIT as it found it, on every target, also where DIT does not apply, each with valid +// Every keyed cell of backends::xkcp enters its own webcpp::secure::timing_guard first 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; every digest cell, called without a key, // enters none; and so does the keccak_256 type, seeded or not (design D3) // (support/timing_coverage.hpp). diff --git a/test/timing_during_test.cpp b/test/timing_during_test.cpp index 8100779..4e9c82e 100644 --- a/test/timing_during_test.cpp +++ b/test/timing_during_test.cpp @@ -6,7 +6,7 @@ // DIT is set while a keyed function computes (design D1; spec scenario "A password derived on Apple // silicon"): a Boost.Hash2-form hash, dit_probe, records at each update whether DIT was set and -// whether a detail::timing_guard had been entered, and runs as the hash under the hmac of +// whether a webcpp::secure::timing_guard had been entered, and runs as the hash under the hmac of // hkdf_extract, hkdf_expand, hkdf (each also over byte pieces) and pbkdf2 on backends::hash2, and // of mac on a stand-in backend whose HMAC-SHA-256 is backends::hash2's over the probe. From a // thread with DIT clear, every update the probe saw ran after a guard was entered and, where the @@ -21,11 +21,11 @@ #include "support/timing_dit.hpp" #include -#include #include #include #include #include +#include #ifdef WEBCPP_TEST_TIMING_DURING_ASYNC #include @@ -58,7 +58,7 @@ struct probe_record { std::size_t updates = 0; /** The updates it ran with DIT clear. */ std::size_t updates_clear = 0; - /** The updates it ran before any detail::timing_guard was entered. */ + /** The updates it ran before any webcpp::secure::timing_guard was entered. */ std::size_t updates_unguarded = 0; }; @@ -75,7 +75,7 @@ void probe_reset() noexcept { /** * SHA-256 in Boost.Hash2's form whose every update records, before it hashes, whether DIT is set - * and whether a detail::timing_guard has been entered on this thread since probe_reset. + * and whether a webcpp::secure::timing_guard has been entered on this thread since probe_reset. */ class dit_probe { public: @@ -108,7 +108,7 @@ class dit_probe { */ void update(void const* data, std::size_t size) { ++probe_seen.updates; - if (!webcpp::hash::detail::dit_is_set()) { + if (!webcpp::secure::dit_is_set()) { ++probe_seen.updates_clear; } if (timing_entries == 0) { @@ -185,7 +185,7 @@ void check_record(std::string const& name, test::probe_record const& seen, bool std::to_string(seen.updates) + " updates ran before any guard") .c_str()); } - std::size_t const expected_clear = hash::detail::cpu_has_dit() ? 0 : seen.updates; + std::size_t const expected_clear = webcpp::secure::cpu_has_dit() ? 0 : seen.updates; if (seen.updates_clear != expected_clear) { BOOST_ERROR((name + ": " + std::to_string(seen.updates_clear) + " of " + std::to_string(seen.updates) + " updates ran with DIT clear") @@ -202,7 +202,7 @@ void during(std::string const& name, Call call) { test::force_dit(false); test::probe_reset(); call(); - check_record(name, test::probe_seen, hash::detail::dit_is_set()); + check_record(name, test::probe_seen, webcpp::secure::dit_is_set()); } void synchronous() { @@ -318,12 +318,12 @@ void on_the_pool() { }; auto const worker = on_pool(pool, [] { - return pool_state{.seen = test::probe_seen, .dit_set = hash::detail::dit_is_set()}; + return pool_state{.seen = test::probe_seen, .dit_set = webcpp::secure::dit_is_set()}; }); check_record("async_pbkdf2 on the pool's thread", worker.seen, worker.dit_set); // The calling thread computed nothing. BOOST_TEST_EQ(test::probe_seen.updates, 0u); - BOOST_TEST(!hash::detail::dit_is_set()); + BOOST_TEST(!webcpp::secure::dit_is_set()); pool.join(); } diff --git a/test/timing_keyed_botan_test.cpp b/test/timing_keyed_botan_test.cpp index ae246ee..24b32ad 100644 --- a/test/timing_keyed_botan_test.cpp +++ b/test/timing_keyed_botan_test.cpp @@ -5,8 +5,8 @@ // https://www.boost.org/LICENSE_1_0.txt) // bcrypt, bcrypt_verify and bcrypt_pbkdf, which only backends::botan serves, and a cell of each -// other key derivation and password hash on it, hold detail::timing_guard (design D1, D4, D6, -// ruling H10): each enters its own guard first, through the seam, also for a call that a guard +// other key derivation and password hash on it, hold webcpp::secure::timing_guard (design D1, D4, +// D6, ruling H10): each enters its own guard first, through the seam, also for a call that a guard // refuses before the backend, and Botan's boundary, which turns its exceptions into errors, holds // none. The functions of the default backends are in timing_keyed_test.cpp. diff --git a/test/timing_keyed_openssl_test.cpp b/test/timing_keyed_openssl_test.cpp index cc56371..9d7b221 100644 --- a/test/timing_keyed_openssl_test.cpp +++ b/test/timing_keyed_openssl_test.cpp @@ -4,11 +4,11 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// The key derivations and the password hashes on backends::openssl hold detail::timing_guard -// (design D1, D4, D6): each enters its own guard first, through the seam, also for a call that a -// guard refuses before the backend. The functions the default backends cover are in -// timing_keyed_test.cpp; here are the three that OpenSSL alone serves (kbkdf, sskdf and x963kdf) -// and, as a second cell of each template, hkdf, pbkdf2, argon2id, argon2 and scrypt. +// The key derivations and the password hashes on backends::openssl hold +// webcpp::secure::timing_guard (design D1, D4, D6): each enters its own guard first, through the +// seam, also for a call that a guard refuses before the backend. The functions the default backends +// cover are in timing_keyed_test.cpp; here are the three that OpenSSL alone serves (kbkdf, sskdf +// and x963kdf) and, as a second cell of each template, hkdf, pbkdf2, argon2id, argon2 and scrypt. #include "support/timing_seam.hpp" diff --git a/test/timing_keyed_test.cpp b/test/timing_keyed_test.cpp index 546a55b..c42e9f1 100644 --- a/test/timing_keyed_test.cpp +++ b/test/timing_keyed_test.cpp @@ -6,8 +6,8 @@ // mac, the keyed modes of BLAKE2b (digest_of, hasher) and of BLAKE3 (the blake3 class), and the // key derivations and password hashes of the default backends (hkdf_extract, hkdf_expand, hkdf, -// pbkdf2, blake2b_subkey, argon2id, argon2, scrypt), hold detail::timing_guard (design D1, D4): -// each enters its own guard first, on every target, through the seam, also for a call that a +// pbkdf2, blake2b_subkey, argon2id, argon2, scrypt), hold webcpp::secure::timing_guard (design D1, +// D4): each enters its own guard first, on every target, through the seam, also for a call that a // guard refuses before the backend; a call that is not keyed enters none (D2); a guard changes no // byte of any output. Where the CPU has DIT, mac leaves the thread's state as it found it. @@ -18,12 +18,12 @@ #include #include #include -#include #include #include #include #include #include +#include #include #include "require.hpp" @@ -49,7 +49,6 @@ namespace { -namespace detail = webcpp::hash::detail; namespace hash = webcpp::hash; namespace test = webcpp::test; namespace digests = webcpp::hash::digests; @@ -97,13 +96,13 @@ void mac_enters() { void mac_leaves_set_state() { std::array tag{}; test::force_dit(true); - BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + BOOST_TEST_EQ(webcpp::secure::dit_is_set(), webcpp::secure::cpu_has_dit()); BOOST_TEST((hash::mac(tag, test_key, test_input)).has_value()); - BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + BOOST_TEST_EQ(webcpp::secure::dit_is_set(), webcpp::secure::cpu_has_dit()); test::force_dit(false); - BOOST_TEST(!detail::dit_is_set()); + BOOST_TEST(!webcpp::secure::dit_is_set()); BOOST_TEST((hash::mac(tag, test_key, test_input)).has_value()); - BOOST_TEST(!detail::dit_is_set()); + BOOST_TEST(!webcpp::secure::dit_is_set()); } // digest_of with a key, and hasher's make, update and finish of a keyed hasher, each enter their diff --git a/test/timing_test.cpp b/test/timing_test.cpp index 448991c..a718026 100644 --- a/test/timing_test.cpp +++ b/test/timing_test.cpp @@ -4,17 +4,18 @@ // 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 engaged guard on every -// target, and never for a disengaged one, and names the function that holds the guard, which -// require_enters reads in the spellings of clang, GCC and MSVC; where the CPU has DIT, an engaged -// guard sets it for its scope and clears it after when it was clear, leaves it set when it was set, -// and nests; a disengaged guard touches nothing. Built with WEBCPP_TEST_TIMING_THREADS -// (timing_threads, native, with threads), a guard on one thread leaves another's DIT clear. A copy -// of libs/crypto/test/timing_test.cpp with hash's names and the engaged flag. +// webcpp::secure::timing_guard, secure's guard, as hash's seam support sees it, with no backend: +// its seam fires once per engaged guard on every target, and never for a disengaged one, and names +// the function that holds the guard, which require_enters reads in the spellings of clang, GCC and +// MSVC; the own-library filter tells hash's guards by their file in the spellings of clang, GCC and +// MSVC and on near misses; where the CPU has DIT, an engaged guard sets it for its scope and clears +// it after when it was clear, leaves it set when it was set, and nests; a disengaged guard touches +// nothing. Built with WEBCPP_TEST_TIMING_THREADS (timing_threads, native, with threads), a guard on +// one thread leaves another's DIT clear. #include "support/timing_dit.hpp" -#include +#include #include #include @@ -30,13 +31,13 @@ namespace { -namespace detail = webcpp::hash::detail; +namespace secure = webcpp::secure; namespace seam_probe { /** Holds a guard, as a public function does, for seam_names_function. */ void guarded_function() { - webcpp::hash::detail::timing_guard const guard; + secure::timing_guard const guard; } /** Calls guarded_function and holds no guard of its own, as a function that forgot its own. */ @@ -46,14 +47,36 @@ void forwarding_function() { } // namespace seam_probe -// Constructing a guard raises the seam's count by exactly one, on every target. +// Constructing a guard raises the seam's count of every guard by exactly one, on every target; the +// guard is this test's own, so hash's count does not move. void seam_fires() { - std::size_t const before = webcpp::test::timing_entries; + std::size_t const before = webcpp::test::timing_all_entries; + std::size_t const hash_before = webcpp::test::timing_entries; { - detail::timing_guard const guard; - BOOST_TEST_EQ(webcpp::test::timing_entries, before + 1); + secure::timing_guard const guard; + BOOST_TEST_EQ(webcpp::test::timing_all_entries, before + 1); } - BOOST_TEST_EQ(webcpp::test::timing_entries, before + 1); + BOOST_TEST_EQ(webcpp::test::timing_all_entries, before + 1); + BOOST_TEST_EQ(webcpp::test::timing_entries, hash_before); +} + +// The own-library filter reads the paths clang, GCC and MSVC give for hash's headers, and no near +// miss: another library's header, a test file, a directory whose name only begins with hash, and +// webcpp/hash/ under a folder that is not an include root. +void path_in_library_spellings() { + using webcpp::test::path_in_library; + constexpr std::string_view library = "hash"; + BOOST_TEST(path_in_library("/w/libs/hash/include/webcpp/hash/mac.hpp", library)); + BOOST_TEST(path_in_library("../include/webcpp/hash/x.hpp", library)); + BOOST_TEST(path_in_library("include/webcpp/hash/x.hpp", library)); + BOOST_TEST(path_in_library(R"(C:\t\a\libs\hash\include\webcpp\hash\x.hpp)", library)); + BOOST_TEST(path_in_library(R"(C:\t\a\include\webcpp/hash\backends\sodium.hpp)", library)); + BOOST_TEST(!path_in_library("/w/libs/hash/test/timing_test.cpp", library)); + BOOST_TEST(!path_in_library("/w/include/webcpp/hashx/x.hpp", library)); + BOOST_TEST(!path_in_library("/w/myinclude/webcpp/hash/x.hpp", library)); + BOOST_TEST(!path_in_library("/w/include/webcpp/secure/timing_guard.hpp", library)); + BOOST_TEST(!path_in_library("/w/include/webcpp/hash", library)); + BOOST_TEST(path_in_library("/w/include/webcpp/secure/timing_guard.hpp", "secure")); } // The seam records the function that holds the first guard a call enters, as the compiler spells @@ -127,64 +150,64 @@ void names_function_spellings() { // From clear: set inside a guard, clear after it. Where the guard does not apply, never set. void guard_sets_and_clears() { webcpp::test::force_dit(false); - BOOST_TEST(!detail::dit_is_set()); + BOOST_TEST(!secure::dit_is_set()); { - detail::timing_guard const guard; - BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + secure::timing_guard const guard; + BOOST_TEST_EQ(secure::dit_is_set(), secure::cpu_has_dit()); } - BOOST_TEST(!detail::dit_is_set()); + BOOST_TEST(!secure::dit_is_set()); } // From set, as a program sets it: still set after a guard. void guard_leaves_set_bit() { webcpp::test::force_dit(true); - BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + BOOST_TEST_EQ(secure::dit_is_set(), secure::cpu_has_dit()); { - detail::timing_guard const guard; - BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + secure::timing_guard const guard; + BOOST_TEST_EQ(secure::dit_is_set(), secure::cpu_has_dit()); } - BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + BOOST_TEST_EQ(secure::dit_is_set(), secure::cpu_has_dit()); webcpp::test::force_dit(false); - BOOST_TEST(!detail::dit_is_set()); + BOOST_TEST(!secure::dit_is_set()); } // From clear: set inside both guards and after the inner one, clear after the outer one. void nested_guards() { webcpp::test::force_dit(false); { - detail::timing_guard const outer; + secure::timing_guard const outer; { - detail::timing_guard const inner; - BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + secure::timing_guard const inner; + BOOST_TEST_EQ(secure::dit_is_set(), secure::cpu_has_dit()); } - BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + BOOST_TEST_EQ(secure::dit_is_set(), secure::cpu_has_dit()); { - detail::timing_guard const quiet{false}; - BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + secure::timing_guard const quiet{false}; + BOOST_TEST_EQ(secure::dit_is_set(), secure::cpu_has_dit()); } - BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + BOOST_TEST_EQ(secure::dit_is_set(), secure::cpu_has_dit()); } - BOOST_TEST(!detail::dit_is_set()); + BOOST_TEST(!secure::dit_is_set()); } // A guard constructed with false fires no seam and touches nothing, whichever state it finds. void disengaged_does_nothing() { - std::size_t const before = webcpp::test::timing_entries; + std::size_t const before = webcpp::test::timing_all_entries; webcpp::test::force_dit(false); { - detail::timing_guard const guard{false}; - BOOST_TEST_EQ(webcpp::test::timing_entries, before); - BOOST_TEST(!detail::dit_is_set()); + secure::timing_guard const guard{false}; + BOOST_TEST_EQ(webcpp::test::timing_all_entries, before); + BOOST_TEST(!secure::dit_is_set()); } - BOOST_TEST(!detail::dit_is_set()); + BOOST_TEST(!secure::dit_is_set()); webcpp::test::force_dit(true); { - detail::timing_guard const guard{false}; - BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + secure::timing_guard const guard{false}; + BOOST_TEST_EQ(secure::dit_is_set(), secure::cpu_has_dit()); } - BOOST_TEST_EQ(detail::dit_is_set(), detail::cpu_has_dit()); + BOOST_TEST_EQ(secure::dit_is_set(), secure::cpu_has_dit()); webcpp::test::force_dit(false); - BOOST_TEST(!detail::dit_is_set()); + BOOST_TEST(!secure::dit_is_set()); } #ifdef WEBCPP_TEST_TIMING_THREADS @@ -197,16 +220,16 @@ void other_thread_untouched() { 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()); + other_reading.set_value(secure::dit_is_set()); }); { - detail::timing_guard const guard; + secure::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()); + BOOST_TEST_EQ(secure::dit_is_set(), secure::cpu_has_dit()); } other.join(); - BOOST_TEST(!detail::dit_is_set()); + BOOST_TEST(!secure::dit_is_set()); } #endif @@ -214,6 +237,7 @@ void other_thread_untouched() { int main() { seam_fires(); + path_in_library_spellings(); seam_names_function(); names_function_spellings(); guard_sets_and_clears(); diff --git a/test/timing_unseamed_test.cpp b/test/timing_unseamed_test.cpp index 2efe0d4..9db71a3 100644 --- a/test/timing_unseamed_test.cpp +++ b/test/timing_unseamed_test.cpp @@ -4,16 +4,16 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// detail::timing_guard in a program's build, without the seam: its constructor takes a bool and no -// std::source_location, on every target, so that no guarded function's name or header's path can -// reach a program. Built with WEBCPP_TEST_TIMING_READ_SELF (natively, at release), the program +// webcpp::secure::timing_guard in a program's build, without the seam: its constructor takes a bool +// and no std::source_location, on every target, so that no guarded function's name or header's path +// can reach a program. Built with WEBCPP_TEST_TIMING_READ_SELF (natively, at release), the program // reads its own executable and finds neither the name of mac, which it calls over HMAC-SHA-256 on // backends::hash2, nor the path of mac.hpp, where mac is declared. A copy of // libs/crypto/test/timing_unseamed_test.cpp over hash's mac. #include -#include #include +#include #include #include @@ -34,16 +34,15 @@ namespace { -namespace detail = webcpp::hash::detail; namespace hash = webcpp::hash; -static_assert(!std::is_constructible_v, +static_assert(!std::is_constructible_v, "without the seam, timing_guard takes no source_location"); -static_assert(!std::is_constructible_v, +static_assert(!std::is_constructible_v, "without the seam, timing_guard takes no source_location"); -static_assert(std::is_nothrow_constructible_v, +static_assert(std::is_nothrow_constructible_v, "without the seam, timing_guard is constructed from a bool, and never throws"); -static_assert(std::is_nothrow_default_constructible_v, +static_assert(std::is_nothrow_default_constructible_v, "without the seam, timing_guard is constructed from nothing, and never throws"); // mac over HMAC-SHA-256 on backends::hash2 runs and answers as it does with the seam. The call is diff --git a/test/wipe_test.cpp b/test/wipe_test.cpp index 8a8c1ce..dd8efdd 100644 --- a/test/wipe_test.cpp +++ b/test/wipe_test.cpp @@ -4,18 +4,19 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// detail::wipe zeroes every byte it is given and none around it; detail::exceeds compares in 64 -// bits on every target. Built with WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD, every wipe is reported -// to this test once its bytes are zero, so that the test checks the whole sequence of wipes HKDF's +// secure::erase_secret, as hash's wipes call it (a span of bytes, a part of a buffer, an object's +// bytes), zeroes every byte it is given and none around it; detail::exceeds compares in 64 bits on +// every target. Built with WEBCPP_SECURE_TEST_WIPE_RECORD, every wipe is reported to this test +// once its bytes are zero, so that the test checks the whole sequence of wipes HKDF's // (also over pieces) and PBKDF2's engines on backends::hash2 do (spec section 6.1): each one's // size, in order, its bytes zero, and the addresses of objects alive at once told apart. #include #include -#include #include #include #include +#include #include @@ -31,12 +32,13 @@ #include #include -#ifndef WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD -#error "wipe_test is built with WEBCPP_HASH_DETAIL_TEST_WIPE_RECORD (test/Jamfile)" +#ifndef WEBCPP_SECURE_TEST_WIPE_RECORD +#error "wipe_test is built with WEBCPP_SECURE_TEST_WIPE_RECORD (test/Jamfile)" #endif namespace detail = webcpp::hash::detail; namespace hash = webcpp::hash; +namespace secure = webcpp::secure; namespace { @@ -53,7 +55,7 @@ struct record { std::size_t not_zero = 0; }; -// The seam's record, which detail::wipe reaches through a function of this test. +// The seam's record, which secure::detail::wipe reaches through a function of this test. // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): the seam's record record wipes; @@ -90,8 +92,7 @@ bool apart(const std::vector& entries, std::size_t index, } // namespace -void webcpp::hash::detail::test::wipe_record(void const* wiped_data, - std::size_t byte_count) noexcept { +void webcpp::secure::test::wipe_record(void const* wiped_data, std::size_t byte_count) noexcept { const auto* const bytes = static_cast(wiped_data); for (std::size_t i = 0; i < byte_count; ++i) { if (bytes[i] != 0) { @@ -110,14 +111,14 @@ namespace { void test_wipe() { std::array bytes{}; bytes.fill(0xa5); - detail::wipe(std::span(bytes.data() + 1, 32)); + secure::erase_secret(std::span(bytes.data() + 1, 32)); BOOST_TEST_EQ(bytes.front(), 0xa5); BOOST_TEST_EQ(bytes.back(), 0xa5); for (std::size_t i = 1; i < 33; ++i) { BOOST_TEST_EQ(bytes[i], 0); } bytes.fill(0xa5); - detail::wipe(bytes.data() + 2, 30); + secure::erase_secret(std::span(bytes).subspan(2, 30)); BOOST_TEST_EQ(bytes[1], 0xa5); BOOST_TEST_EQ(bytes[2], 0); BOOST_TEST_EQ(bytes[31], 0); @@ -127,10 +128,10 @@ void test_wipe() { std::array words; } object{{{1, 2, 3, 4}}}; - detail::wipe_object(object); + secure::erase_secret(std::span(&object, 1)); BOOST_TEST_EQ(object.words[0] | object.words[1] | object.words[2] | object.words[3], 0U); - detail::wipe(nullptr, 0); - detail::wipe(std::span()); + secure::erase_secret(std::span()); + secure::erase_secret(std::span()); BOOST_TEST((taken() == std::vector{32, 30, sizeof(schedule), 0, 0})); } From 47c22690cd6fa61762ddd3b7a41e0d367827f01c Mon Sep 17 00:00:00 2001 From: R Date: Wed, 25 Nov 2026 05:44:02 -0300 Subject: [PATCH 02/12] hash: the changelog's DIT line names secure's guard and erase_secret, within the ten lines of Unreleased --- doc/changelog.adoc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/changelog.adoc b/doc/changelog.adoc index 54c2128..7e47024 100644 --- a/doc/changelog.adoc +++ b/doc/changelog.adoc @@ -18,9 +18,8 @@ of every version are on webcpp's index page. // tag::unreleased[] * *Breaking change:* Changed the public names: hkdf's input_key_material, pseudorandom_key, application_info, output_key_material and salt, hkdf_max_application_info_size, scrypt_parameters' cost, block_size and parallelism (from n, r, p) and argon2_parameters::memory_kibibytes. * *Breaking change:* Changed HKDF's overloads: `+hkdf(out, {}, {}, {})+`, `+async_hkdf(ex, out, salt, {}, {}, token)+` and `+{vec}+` inputs are ambiguous; pass the vector itself, an empty span or `+byte_pieces{...}+`. -* Changed every keyed function to hold webcpp::secure's timing guard, which also sets DIT on Windows on ARM64, and every wipe to secure's erase_secret; hash depends on secure. * Added the library: Boost.Hash2's algorithms and HMAC under webcpp::hash's names, keccak_256 (Ethereum's Keccak-256 over XKCP), blake3 (keyed, key derivation, extendable output), every result `+[[nodiscard]]+`. -* Added HKDF (RFC 5869) and PBKDF2 (RFC 8018) on hash2, argon2id (RFC 9106), scrypt and argon2's Argon2i and Argon2id on sodium; keyed calls set DIT on arm64 Apple and Linux. +* Added HKDF (RFC 5869) and PBKDF2 (RFC 8018) on hash2, argon2id (RFC 9106), scrypt and argon2's Argon2i and Argon2id on sodium; keyed calls hold webcpp::secure's timing guard (DIT on arm64 Apple, Linux and Windows) and wipes are secure's erase_secret: hash depends on secure. * Added HKDF over byte pieces (hkdf_extract, hkdf_expand, hkdf and their async forms), with no cap; on the synchronous backends hash makes no buffer, heap or copy of the inputs. * Added backends::openssl (OpenSSL 3.5.9; native, wasip2, wasip3) and backends::botan (Botan 3.14.1, with exceptions), named in the call: their digests, MACs, key derivations and, on Botan, bcrypt. * Added backends::sodium (HKDF, BLAKE2b with its options, keyed BLAKE2b, SipHash, blake2b_subkey, SHA-2, SHA-3, SHAKE, TurboSHAKE, HMAC) and backends::bearssl (HKDF, MD5, SHA-1, SHA-2, MD5-SHA1, SHAKE, HMAC). From c76cbe9b3b39878664c52f2ee48c93674efbf673 Mon Sep 17 00:00:00 2001 From: R Date: Wed, 25 Nov 2026 05:58:25 -0300 Subject: [PATCH 03/12] hash: the changelog's DIT line within thirty words --- doc/changelog.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changelog.adoc b/doc/changelog.adoc index 7e47024..309e0e7 100644 --- a/doc/changelog.adoc +++ b/doc/changelog.adoc @@ -19,7 +19,7 @@ of every version are on webcpp's index page. * *Breaking change:* Changed the public names: hkdf's input_key_material, pseudorandom_key, application_info, output_key_material and salt, hkdf_max_application_info_size, scrypt_parameters' cost, block_size and parallelism (from n, r, p) and argon2_parameters::memory_kibibytes. * *Breaking change:* Changed HKDF's overloads: `+hkdf(out, {}, {}, {})+`, `+async_hkdf(ex, out, salt, {}, {}, token)+` and `+{vec}+` inputs are ambiguous; pass the vector itself, an empty span or `+byte_pieces{...}+`. * Added the library: Boost.Hash2's algorithms and HMAC under webcpp::hash's names, keccak_256 (Ethereum's Keccak-256 over XKCP), blake3 (keyed, key derivation, extendable output), every result `+[[nodiscard]]+`. -* Added HKDF (RFC 5869) and PBKDF2 (RFC 8018) on hash2, argon2id (RFC 9106), scrypt and argon2's Argon2i and Argon2id on sodium; keyed calls hold webcpp::secure's timing guard (DIT on arm64 Apple, Linux and Windows) and wipes are secure's erase_secret: hash depends on secure. +* Added HKDF (RFC 5869) and PBKDF2 (RFC 8018) on hash2, argon2id (RFC 9106), scrypt and argon2 on sodium; keyed calls hold webcpp::secure's timing guard (DIT on arm64 Apple, Linux, Windows). * Added HKDF over byte pieces (hkdf_extract, hkdf_expand, hkdf and their async forms), with no cap; on the synchronous backends hash makes no buffer, heap or copy of the inputs. * Added backends::openssl (OpenSSL 3.5.9; native, wasip2, wasip3) and backends::botan (Botan 3.14.1, with exceptions), named in the call: their digests, MACs, key derivations and, on Botan, bcrypt. * Added backends::sodium (HKDF, BLAKE2b with its options, keyed BLAKE2b, SipHash, blake2b_subkey, SHA-2, SHA-3, SHAKE, TurboSHAKE, HMAC) and backends::bearssl (HKDF, MD5, SHA-1, SHA-2, MD5-SHA1, SHAKE, HMAC). From d52c320b0e9fb43a36f9c3fc0383860326e8e7c9 Mon Sep 17 00:00:00 2001 From: R Date: Wed, 25 Nov 2026 14:15:44 -0300 Subject: [PATCH 04/12] hash: the DIT section names crypto's timing_nested_hash_test (tools/hash/doc_tables.py --write) --- doc/backends.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/backends.adoc b/doc/backends.adoc index 5513cc6..581f4fd 100644 --- a/doc/backends.adoc +++ b/doc/backends.adoc @@ -1944,7 +1944,7 @@ The functions that do not: * Boost.Hash2's types by alias, `siphash_32`, `siphash_64`, `hmac`, `hmac_md5_128`, `hmac_sha1_160`, `hmac_sha2_224`, `hmac_sha2_256`, `hmac_sha2_384`, `hmac_sha2_512`, `hmac_sha2_512_224`, `hmac_sha2_512_256`, `hmac_sha3_224`, `hmac_sha3_256`, `hmac_sha3_384`, `hmac_sha3_512`, `hmac_ripemd_128`, `hmac_ripemd_160`, `hmac_blake2b_512` and `hmac_blake2s_256`: they are Boost.Hash2's own classes, and a guard inside them would need a type of hash's in place of the alias. An HMAC with DIT is `mac>`, which runs the same Boost.Hash2 code on `backends::hash2` inside its guard, and a SipHash with DIT is `mac`; a program that calls `hmac` or `siphash_64` directly sets DIT itself. HKDF and PBKDF2 on `backends::hash2` run Boost.Hash2's `hmac` inside their own guard. * The initiating functions of `async.hpp`: they check sizes and post, and the synchronous function they call sets DIT on the thread that computes, a thread of the work executor's pool for a heavy function, the caller's for a cheap one (<>). On `backends::webcrypto` the browser's JavaScript engine computes, and nothing is added. -A coverage test per backend (`test/timing_coverage__test.cpp`) reads this table and fails when a keyed cell of its backend has no call, when a call enters no guard, when the first guard it enters is not its own function's, or when it leaves DIT otherwise than it found it, from a thread with DIT clear and from one that set it; it checks that the unkeyed functions enter none. `test/timing_during_test.cpp` reads DIT while HKDF, PBKDF2 and `mac` compute, through a hash that records it at each update, on the calling thread and on an `async_pbkdf2` pool's. The guard is secure's, shared with webcpp::crypto and webcpp::codecs: secure's `timing_guard_nesting_test` nests guards as two libraries do. +A coverage test per backend (`test/timing_coverage__test.cpp`) reads this table and fails when a keyed cell of its backend has no call, when a call enters no guard, when the first guard it enters is not its own function's, or when it leaves DIT otherwise than it found it, from a thread with DIT clear and from one that set it; it checks that the unkeyed functions enter none. `test/timing_during_test.cpp` reads DIT while HKDF, PBKDF2 and `mac` compute, through a hash that records it at each update, on the calling thread and on an `async_pbkdf2` pool's. The guard is secure's, shared with webcpp::crypto and webcpp::codecs: secure's `timing_guard_nesting_test` nests guards as two libraries do, and crypto's `timing_nested_hash_test` runs a keyed call of hash inside a guarded call of crypto. What the guard adds to a call, measured from a release build, is in <>. From 4ae4c46ed8e1fcf51ac60c0a3b44d3b5acd3bf74 Mon Sep 17 00:00:00 2001 From: R Date: Wed, 25 Nov 2026 15:59:25 -0300 Subject: [PATCH 05/12] hash: the guard's sentence of the performance page names ARM64EC among the targets where it executes nothing (collect.py --render-only) --- bench/collect.py | 6 +++--- doc/performance.adoc | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bench/collect.py b/bench/collect.py index c49c183..997028a 100755 --- a/bench/collect.py +++ b/bench/collect.py @@ -336,9 +336,9 @@ def render_timing(timing): "guard, which on 64-bit ARM under macOS, iOS, Linux, Android and Windows, where the", "CPU has FEAT_DIT, sets DIT (data-independent timing) for the call and restores the", "thread's state after it (<>): one `mrs`, and where DIT was clear,", - "two `msr` each followed by an `isb`. Elsewhere (x86, WebAssembly) it executes no", - "instruction. A function keyed by its arguments, called without a key, holds a", - "disengaged guard, which reads its flag and does nothing else.", + "two `msr` each followed by an `isb`. Elsewhere (x86, WebAssembly, ARM64EC) it", + "executes no instruction. A function keyed by its arguments, called without a key,", + "holds a disengaged guard, which reads its flag and does nothing else.", "", ] if timing is None: diff --git a/doc/performance.adoc b/doc/performance.adoc index a3b6d9f..a668293 100644 --- a/doc/performance.adoc +++ b/doc/performance.adoc @@ -128,9 +128,9 @@ BLAKE2b or BLAKE3) runs inside `webcpp::secure::timing_guard`, webcpp::secure's guard, which on 64-bit ARM under macOS, iOS, Linux, Android and Windows, where the CPU has FEAT_DIT, sets DIT (data-independent timing) for the call and restores the thread's state after it (<>): one `mrs`, and where DIT was clear, -two `msr` each followed by an `isb`. Elsewhere (x86, WebAssembly) it executes no -instruction. A function keyed by its arguments, called without a key, holds a -disengaged guard, which reads its flag and does nothing else. +two `msr` each followed by an `isb`. Elsewhere (x86, WebAssembly, ARM64EC) it +executes no instruction. A function keyed by its arguments, called without a key, +holds a disengaged guard, which reads its flag and does nothing else. Measured natively on 2026-11-22 (UTC) on Apple M5 Max (arm64), Darwin 27.0.0, built with variant=release optimization=speed From 5b5efcd96db2004541234140f24ccb46daea3baf Mon Sep 17 00:00:00 2001 From: R Date: Wed, 25 Nov 2026 15:59:25 -0300 Subject: [PATCH 06/12] hash: the changelog names Argon2i/Argon2id on sodium again, and three tests name secure::detail::wipe --- doc/changelog.adoc | 2 +- test/blake2b_mac_sodium_test.cpp | 2 +- test/families_bearssl_test.cpp | 2 +- test/hkdf_bearssl_test.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/changelog.adoc b/doc/changelog.adoc index 309e0e7..2ac8bd3 100644 --- a/doc/changelog.adoc +++ b/doc/changelog.adoc @@ -19,7 +19,7 @@ of every version are on webcpp's index page. * *Breaking change:* Changed the public names: hkdf's input_key_material, pseudorandom_key, application_info, output_key_material and salt, hkdf_max_application_info_size, scrypt_parameters' cost, block_size and parallelism (from n, r, p) and argon2_parameters::memory_kibibytes. * *Breaking change:* Changed HKDF's overloads: `+hkdf(out, {}, {}, {})+`, `+async_hkdf(ex, out, salt, {}, {}, token)+` and `+{vec}+` inputs are ambiguous; pass the vector itself, an empty span or `+byte_pieces{...}+`. * Added the library: Boost.Hash2's algorithms and HMAC under webcpp::hash's names, keccak_256 (Ethereum's Keccak-256 over XKCP), blake3 (keyed, key derivation, extendable output), every result `+[[nodiscard]]+`. -* Added HKDF (RFC 5869) and PBKDF2 (RFC 8018) on hash2, argon2id (RFC 9106), scrypt and argon2 on sodium; keyed calls hold webcpp::secure's timing guard (DIT on arm64 Apple, Linux, Windows). +* Added HKDF (RFC 5869) and PBKDF2 (RFC 8018) on hash2, argon2id (RFC 9106), scrypt and Argon2i/Argon2id on sodium; keyed calls hold webcpp::secure's timing guard (DIT on arm64 Apple, Linux, Windows). * Added HKDF over byte pieces (hkdf_extract, hkdf_expand, hkdf and their async forms), with no cap; on the synchronous backends hash makes no buffer, heap or copy of the inputs. * Added backends::openssl (OpenSSL 3.5.9; native, wasip2, wasip3) and backends::botan (Botan 3.14.1, with exceptions), named in the call: their digests, MACs, key derivations and, on Botan, bcrypt. * Added backends::sodium (HKDF, BLAKE2b with its options, keyed BLAKE2b, SipHash, blake2b_subkey, SHA-2, SHA-3, SHAKE, TurboSHAKE, HMAC) and backends::bearssl (HKDF, MD5, SHA-1, SHA-2, MD5-SHA1, SHAKE, HMAC). diff --git a/test/blake2b_mac_sodium_test.cpp b/test/blake2b_mac_sodium_test.cpp index ede8394..5b12273 100644 --- a/test/blake2b_mac_sodium_test.cpp +++ b/test/blake2b_mac_sodium_test.cpp @@ -76,7 +76,7 @@ struct record { } }; -// The seam's record, which detail::wipe reaches through a function of this test. +// The seam's record, which secure::detail::wipe reaches through a function of this test. // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): the seam's record record wipes; diff --git a/test/families_bearssl_test.cpp b/test/families_bearssl_test.cpp index 8518d5e..14c4c24 100644 --- a/test/families_bearssl_test.cpp +++ b/test/families_bearssl_test.cpp @@ -73,7 +73,7 @@ struct record { } }; -// The seam's record, which detail::wipe reaches through a function of this test. +// The seam's record, which secure::detail::wipe reaches through a function of this test. // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): the seam's record record wipes; diff --git a/test/hkdf_bearssl_test.cpp b/test/hkdf_bearssl_test.cpp index 796b85d..fb853b7 100644 --- a/test/hkdf_bearssl_test.cpp +++ b/test/hkdf_bearssl_test.cpp @@ -51,7 +51,7 @@ struct record { std::size_t not_zero = 0; }; -// The seam's record, which detail::wipe reaches through a function of this test. +// The seam's record, which secure::detail::wipe reaches through a function of this test. // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): the seam's record record wipes; From e71cb982153a0af70d7f7a112c7f3e2989dcc3ea Mon Sep 17 00:00:00 2001 From: R Date: Wed, 25 Nov 2026 22:54:35 -0300 Subject: [PATCH 07/12] hash: the guard's cost is measured again: a guard that found DIT set now reads it again at its end (secure's guard, after Windows on ARM64's catch) --- bench/collect.py | 7 ++++--- bench/timing.cpp | 8 ++++---- doc/measurements.throughput.json | 34 ++++++++++++++++---------------- doc/performance.adoc | 27 +++++++++++++------------ 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/bench/collect.py b/bench/collect.py index 997028a..5d60dc0 100755 --- a/bench/collect.py +++ b/bench/collect.py @@ -335,8 +335,9 @@ def render_timing(timing): "BLAKE2b or BLAKE3) runs inside `webcpp::secure::timing_guard`, webcpp::secure's", "guard, which on 64-bit ARM under macOS, iOS, Linux, Android and Windows, where the", "CPU has FEAT_DIT, sets DIT (data-independent timing) for the call and restores the", - "thread's state after it (<>): one `mrs`, and where DIT was clear,", - "two `msr` each followed by an `isb`. Elsewhere (x86, WebAssembly, ARM64EC) it", + "thread's state after it (<>): one `mrs`; where DIT was clear, two", + "`msr` each followed by an `isb`; where it was set, a second `mrs` on the way out", + "(and an `msr` if something inside cleared it). Elsewhere (x86, WebAssembly, ARM64EC) it", "executes no instruction. A function keyed by its arguments, called without a key,", "holds a disengaged guard, which reads its flag and does nothing else.", "", @@ -366,7 +367,7 @@ def render_timing(timing): ("clear", "clear, as a program that sets nothing has it", "reads DIT, sets it, and clears it on the way out"), ("set", "already set, by the program or by an outer guard", - "reads DIT and leaves it set"), + "reads DIT, and reads it again on the way out"), ("disengaged", "any: a function keyed by its arguments, called without a key", "reads its flag and does nothing else"), ) diff --git a/bench/timing.cpp b/bench/timing.cpp index 157b12a..d2de054 100644 --- a/bench/timing.cpp +++ b/bench/timing.cpp @@ -11,10 +11,10 @@ // // Run as `timing --measure`, it times, in one process, 10^7 iterations each of an empty loop body, // of a guard entered from a thread whose DIT is clear (it sets DIT and clears it again), of a -// guard entered with DIT already set (it reads DIT and leaves it; the program sets DIT for that -// part with the same `msr` as the guard, and clears it after), and of a disengaged guard (what an -// unkeyed call of a function that is keyed by its arguments pays: the flag, read from memory as a -// member is, and a branch), five runs of each, interleaved, and prints one JSON line: +// guard entered with DIT already set (it reads DIT, and again at its end; the program sets DIT +// for that part with the same `msr` as the guard, and clears it after), and of a disengaged guard +// (what an unkeyed call of a function that is keyed by its arguments pays: the flag, read from +// memory as a member is, and a branch), five runs of each, interleaved, and prints one JSON line: // `{"dit": true|false, "clear": , "set": , "disengaged": , ...}`, each cost the // median of its runs minus the empty loop's median, in nanoseconds per guard, then each part's // fastest and slowest run (minus the same median), the iterations, the runs, the target and the diff --git a/doc/measurements.throughput.json b/doc/measurements.throughput.json index f86e098..63d12da 100644 --- a/doc/measurements.throughput.json +++ b/doc/measurements.throughput.json @@ -269,8 +269,8 @@ } ], "timing_guard": { - "measurement": "the nanoseconds detail::timing_guard adds to a call, bench/timing.cpp", - "date": "2026-11-22", + "measurement": "the nanoseconds webcpp::secure::timing_guard adds to a call, bench/timing.cpp", + "date": "2026-11-26", "machine": { "cpu": "Apple M5 Max", "architecture": "arm64", @@ -279,14 +279,14 @@ }, "load_average_before_after": [ [ - 61.44970703125, - 270.64794921875, - 332.67626953125 + 7.77587890625, + 18.5400390625, + 19.75 ], [ - 61.44970703125, - 270.64794921875, - 332.67626953125 + 7.77587890625, + 18.5400390625, + 19.75 ] ], "build": "variant=release optimization=speed", @@ -295,15 +295,15 @@ "iterations": 10000000, "runs": 5, "dit": true, - "clear_ns": 45.418, - "clear_fastest_ns": 44.81, - "clear_slowest_ns": 48.771, - "set_ns": 0.711, - "set_fastest_ns": 0.646, - "set_slowest_ns": 0.755, - "disengaged_ns": -0.006, - "disengaged_fastest_ns": -0.013, - "disengaged_slowest_ns": 0.099, + "clear_ns": 39.509, + "clear_fastest_ns": 39.472, + "clear_slowest_ns": 39.552, + "set_ns": 1.3, + "set_fastest_ns": 1.277, + "set_slowest_ns": 1.394, + "disengaged_ns": -0.0, + "disengaged_fastest_ns": -0.0, + "disengaged_slowest_ns": 0.0, "provisional": true } } diff --git a/doc/performance.adoc b/doc/performance.adoc index a668293..5fb877b 100644 --- a/doc/performance.adoc +++ b/doc/performance.adoc @@ -127,20 +127,21 @@ Every keyed function (a MAC, a key derivation, a password hash, a keyed mode of BLAKE2b or BLAKE3) runs inside `webcpp::secure::timing_guard`, webcpp::secure's guard, which on 64-bit ARM under macOS, iOS, Linux, Android and Windows, where the CPU has FEAT_DIT, sets DIT (data-independent timing) for the call and restores the -thread's state after it (<>): one `mrs`, and where DIT was clear, -two `msr` each followed by an `isb`. Elsewhere (x86, WebAssembly, ARM64EC) it +thread's state after it (<>): one `mrs`; where DIT was clear, two +`msr` each followed by an `isb`; where it was set, a second `mrs` on the way out +(and an `msr` if something inside cleared it). Elsewhere (x86, WebAssembly, ARM64EC) it executes no instruction. A function keyed by its arguments, called without a key, holds a disengaged guard, which reads its flag and does nothing else. -Measured natively on 2026-11-22 (UTC) on Apple M5 Max +Measured natively on 2026-11-26 (UTC) on Apple M5 Max (arm64), Darwin 27.0.0, built with variant=release optimization=speed by Apple LLVM 21.0.0 (clang-2100.4.1.1) (bench/timing.cpp): each row is the median of 5 runs of 10,000,000 guards around a store, with the fastest and slowest run beside it, each minus the median of the same loop without the guard, in nanoseconds per call; a figure that rounds to zero or below, the loop's noise, shows as 0.0. -The guard sets DIT on this machine. The machine's 1, 5 and 15 minute load averages were 61/271/333 -before and 61/271/333 after, on 18 cores. +The guard sets DIT on this machine. The machine's 1, 5 and 15 minute load averages were 8/19/20 +before and 8/19/20 after, on 18 cores. NOTE: these figures are provisional. They were measured on a shared machine under load (the load averages above), and are measured again in a final measurement pass on a quiet machine, once the work in progress has merged; read them as an upper bound. @@ -150,19 +151,19 @@ NOTE: these figures are provisional. They were measured on a shared machine unde | clear, as a program that sets nothing has it | reads DIT, sets it, and clears it on the way out -| 45.4 -| 44.8 -| 48.8 +| 39.5 +| 39.5 +| 39.6 | already set, by the program or by an outer guard -| reads DIT and leaves it set -| 0.7 -| 0.6 -| 0.8 +| reads DIT, and reads it again on the way out +| 1.3 +| 1.3 +| 1.4 | any: a function keyed by its arguments, called without a key | reads its flag and does nothing else | 0.0 | 0.0 -| 0.1 +| 0.0 |=== From dd63e37f937b1594c3e4154dd90b21ff0f47dd3e Mon Sep 17 00:00:00 2001 From: R Date: Thu, 26 Nov 2026 00:24:45 -0300 Subject: [PATCH 08/12] hash: async_cobalt checks under MSVC that each promise's frame is aligned to __STDCPP_DEFAULT_NEW_ALIGNMENT__ (Boost 1.92.0's Cobalt placed it 8 bytes off, and MSVC x64 release faulted on movaps) --- test/async_cobalt_test.cpp | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/async_cobalt_test.cpp b/test/async_cobalt_test.cpp index be0d771..a11a20e 100644 --- a/test/async_cobalt_test.cpp +++ b/test/async_cobalt_test.cpp @@ -14,6 +14,11 @@ // Natively a heavy function, async_pbkdf2 on a thread pool, resumes the task on its own thread; on // Emscripten async_digest on backends::webcrypto completes through Cobalt's executor, an // io_context's. +// Under MSVC each promise's frame is aligned to __STDCPP_DEFAULT_NEW_ALIGNMENT__: Boost 1.92.0's +// Cobalt placed it 8 bytes off a 16-byte boundary, and MSVC's optimized code stores into the frame +// with movaps, so this test died with 0xC0000005 on MSVC x64 release (the CI's Boost carries the +// upstream fix, tools/ci/actions/boost/patches/cobalt-coroutine-align-msvc.patch); without that fix +// the check fails in a debug build too, where nothing faults. #include #include @@ -36,6 +41,7 @@ #include #endif +#include #include #include #include @@ -43,6 +49,7 @@ #include #include +#include #include #include #include @@ -58,6 +65,34 @@ std::span bytes_of(std::string_view text) { return {reinterpret_cast(text.data()), text.size()}; } +// The address of the awaiting coroutine's frame, read without suspending it. +class frame_address { +public: + [[nodiscard]] static bool await_ready() noexcept { return false; } + + bool await_suspend(std::coroutine_handle<> const handle) noexcept { + address_ = handle.address(); + return false; + } + + [[nodiscard]] void* await_resume() const noexcept { return address_; } + +private: + void* address_ = nullptr; +}; + +// Whether a frame starts where an operator new without std::align_val_t must place storage. Only +// MSVC is checked: there Boost's Cobalt aligns the frame to 16 bytes (coroutine_align) since the +// fix the CI's Boost carries; elsewhere Cobalt aligns it to std::max_align_t, as before. +[[nodiscard]] bool frame_aligned([[maybe_unused]] void const* const frame) { +#ifdef BOOST_MSVC + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast): the frame's address + return reinterpret_cast(frame) % __STDCPP_DEFAULT_NEW_ALIGNMENT__ == 0; +#else + return true; +#endif +} + // GCC 14 warns that each promise below, a coroutine, frees its frame with an operator delete that // does not pair with the operator new that made it (-Wmismatched-new-delete); the pair is // Boost.Cobalt's own, from its promise_memory_resource_base, and GCC 15 does not warn @@ -72,6 +107,8 @@ std::span bytes_of(std::string_view text) { cobalt::promise cheap() { // NOLINTNEXTLINE(readability-static-accessed-through-instance) auto const executor = co_await cobalt::this_coro::executor; + void const* const frame = co_await frame_address{}; + BOOST_TEST(frame_aligned(frame)); auto const abc = bytes_of("abc"); auto const key = bytes_of("key"); @@ -119,6 +156,8 @@ cobalt::promise cheap() { #ifdef WEBCPP_HASH_HAS_THREADS // A heavy function: the work on the pool, the task resumed on the thread that runs it. cobalt::promise heavy(webcpp::asio::thread_pool& pool) { + void const* const frame = co_await frame_address{}; + BOOST_TEST(frame_aligned(frame)); auto const caller = std::this_thread::get_id(); auto const password = bytes_of("password"); auto const salt = bytes_of("salt"); From 9f6da5f5d61de8ec3a8571ae97c0228f59ee897a Mon Sep 17 00:00:00 2001 From: R Date: Thu, 26 Nov 2026 00:31:57 -0300 Subject: [PATCH 09/12] hash: async_cobalt's frame_address is ready once it holds the address (clang-tidy's static-accessed-through-instance on a static await_ready) --- test/async_cobalt_test.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/async_cobalt_test.cpp b/test/async_cobalt_test.cpp index a11a20e..30e3da6 100644 --- a/test/async_cobalt_test.cpp +++ b/test/async_cobalt_test.cpp @@ -65,10 +65,11 @@ std::span bytes_of(std::string_view text) { return {reinterpret_cast(text.data()), text.size()}; } -// The address of the awaiting coroutine's frame, read without suspending it. +// The address of the awaiting coroutine's frame, read without suspending it: awaited once, it is +// not ready until await_suspend has read the address. class frame_address { public: - [[nodiscard]] static bool await_ready() noexcept { return false; } + [[nodiscard]] bool await_ready() const noexcept { return address_ != nullptr; } bool await_suspend(std::coroutine_handle<> const handle) noexcept { address_ = handle.address(); From 61d19129f3ecd07fd7f981cb45486fa5d1ad0992 Mon Sep 17 00:00:00 2001 From: R Date: Thu, 26 Nov 2026 01:27:00 -0300 Subject: [PATCH 10/12] hash: the timing seam's path_in_library compares a path's components with a bounds-checked loop in place of string_view::substr, which may throw from a noexcept function (clang-tidy's bugprone-exception-escape) --- test/support/timing_seam.hpp | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/test/support/timing_seam.hpp b/test/support/timing_seam.hpp index 5fc75be..2fb2fdd 100644 --- a/test/support/timing_seam.hpp +++ b/test/support/timing_seam.hpp @@ -64,6 +64,28 @@ constexpr bool path_separator(char character) noexcept { return character == '/' || character == '\\'; } +/** + * Whether `path` holds `name` at `position`, every index checked against the path's size (no + * call that may throw, unlike `substr`). + * + * @param path A `file_name()`. + * @param position Where `name` would start. + * @param name The characters to find there. + * @return Whether the characters of `path` from `position` are those of `name`. + */ +constexpr bool path_holds_at(std::string_view path, std::size_t position, + std::string_view name) noexcept { + if (position > path.size() || name.size() > path.size() - position) { + return false; + } + for (std::size_t index = 0; index < name.size(); ++index) { + if (path[position + index] != name[index]) { + return false; + } + } + return true; +} + /** * Whether a path names a file under a library's include directory: the components `include`, * `webcpp` and `library`, in that order, each followed by a separator, `/` or `\` in any mix, and @@ -82,9 +104,9 @@ constexpr bool path_in_library(std::string_view path, std::string_view library) std::size_t const library_at = root_at + root.size() + 1; std::size_t const after = library_at + library.size(); if ((at == 0 || path_separator(path[at - 1])) && after < path.size() && - path_separator(path[root_at - 1]) && path.substr(root_at, root.size()) == root && - path_separator(path[library_at - 1]) && - path.substr(library_at, library.size()) == library && path_separator(path[after])) { + path_separator(path[root_at - 1]) && path_holds_at(path, root_at, root) && + path_separator(path[library_at - 1]) && path_holds_at(path, library_at, library) && + path_separator(path[after])) { return true; } } From 21c5cd7788678e2a9d78096cda436a68a5958561 Mon Sep 17 00:00:00 2001 From: R Date: Thu, 26 Nov 2026 01:27:06 -0300 Subject: [PATCH 11/12] hash: the DIT section, regenerated by tools/hash/doc_tables.py, says an exception leaves DIT as found while it unwinds the call and that Windows on ARM64 clears it after the catch; the MACs page names every system where mac sets DIT --- doc/backends.adoc | 2 +- doc/macs.adoc | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/backends.adoc b/doc/backends.adoc index 581f4fd..a37b361 100644 --- a/doc/backends.adoc +++ b/doc/backends.adoc @@ -1929,7 +1929,7 @@ states about its constant time. The backends: [#hash-backends-dit] === Data-independent timing (DIT) -64-bit ARM's DIT (data-independent timing) bit, PSTATE.DIT, makes the instructions Arm lists take a time independent of their data, and Apple requires it of constant-time code (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, enable DIT for constant-time cryptographic operations]). None of the libraries hash calls sets it, so hash does: every function that computes on a key, a password or a key derivation's secret input runs its whole body inside `webcpp::secure::timing_guard`, webcpp::secure's guard, on every synchronous backend, which on 64-bit ARM under macOS, iOS, Linux, Android and Windows, where the CPU has FEAT_DIT (`hw.optional.arm.FEAT_DIT` on Apple's systems, `HWCAP_DIT` on Linux, one read of the register under structured exception handling on Windows), sets DIT for the call and returns with the thread's DIT as it found it: clear when it was clear, set when the program set it itself (Apple's `timingsafe_enable_if_supported`) or an outer call did. An error and an exception leave it as found too, and DIT is a thread's own state, so a call never changes another thread's. A program does nothing. Elsewhere the guard executes no instruction: on x86 the counterpart, DOITM, is a register only the kernel writes; on WebAssembly the engine decides. +64-bit ARM's DIT (data-independent timing) bit, PSTATE.DIT, makes the instructions Arm lists take a time independent of their data, and Apple requires it of constant-time code (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, enable DIT for constant-time cryptographic operations]). None of the libraries hash calls sets it, so hash does: every function that computes on a key, a password or a key derivation's secret input runs its whole body inside `webcpp::secure::timing_guard`, webcpp::secure's guard, on every synchronous backend, which on 64-bit ARM under macOS, iOS, Linux, Android and Windows, where the CPU has FEAT_DIT (`hw.optional.arm.FEAT_DIT` on Apple's systems, `HWCAP_DIT` on Linux, one read of the register under structured exception handling on Windows), sets DIT for the call and returns with the thread's DIT as it found it: clear when it was clear, set when the program set it itself (Apple's `timingsafe_enable_if_supported`) or an outer call did. An error leaves it as found too, and so does an exception, while it unwinds the call. After the catch, Windows on ARM64 clears DIT when it resumes the thread: a program there that set DIT itself and catches an exception from a call sets it again after the catch (webcpp::secure's guide, "Exceptions" of its guard). DIT is a thread's own state, so a call never changes another thread's. Otherwise a program does nothing. Elsewhere the guard executes no instruction: on x86 the counterpart, DOITM, is a register only the kernel writes; on WebAssembly the engine decides. The functions that set DIT, on every synchronous backend of their cells: diff --git a/doc/macs.adoc b/doc/macs.adoc index a48a42a..7e8f591 100644 --- a/doc/macs.adoc +++ b/doc/macs.adoc @@ -19,8 +19,9 @@ does not prove to a third party who wrote it (both sides hold the key); that is a signature's job, webcpp::crypto's. `mac` sets DIT (data-independent timing) around each call, on every -synchronous backend, on 64-bit ARM under Apple's systems and Linux where the -CPU has FEAT_DIT, and leaves the thread's DIT as it found it: a program does nothing (<>). Boost.Hash2's types that +synchronous backend, on 64-bit ARM under macOS, iOS, Linux, Android and Windows +where the CPU has FEAT_DIT, and leaves the thread's DIT as it found it: a +program does nothing (<>). Boost.Hash2's types that hash names by alias, `hmac` and its `hmac_*` aliases, `siphash_32` and `siphash_64`, set none (<>): for HMAC with DIT, call `mac>`, and for SipHash with DIT, `mac`. From d06fbd7801202d52dcc99e0ea95d59dc0697e18f Mon Sep 17 00:00:00 2001 From: R Date: Thu, 26 Nov 2026 03:43:46 -0300 Subject: [PATCH 12/12] hash: the guard's rows measured again on an idle machine (clear 37.0 ns, set 1.2 ns); the provisional NOTE of the guard goes --- doc/measurements.throughput.json | 32 ++++++++++++++++---------------- doc/performance.adoc | 16 +++++++--------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/doc/measurements.throughput.json b/doc/measurements.throughput.json index 63d12da..77d86be 100644 --- a/doc/measurements.throughput.json +++ b/doc/measurements.throughput.json @@ -279,14 +279,14 @@ }, "load_average_before_after": [ [ - 7.77587890625, - 18.5400390625, - 19.75 + 1.91015625, + 2.6796875, + 3.17578125 ], [ - 7.77587890625, - 18.5400390625, - 19.75 + 1.9169921875, + 2.66796875, + 3.16845703125 ] ], "build": "variant=release optimization=speed", @@ -295,15 +295,15 @@ "iterations": 10000000, "runs": 5, "dit": true, - "clear_ns": 39.509, - "clear_fastest_ns": 39.472, - "clear_slowest_ns": 39.552, - "set_ns": 1.3, - "set_fastest_ns": 1.277, - "set_slowest_ns": 1.394, - "disengaged_ns": -0.0, - "disengaged_fastest_ns": -0.0, - "disengaged_slowest_ns": 0.0, - "provisional": true + "clear_ns": 37.013, + "clear_fastest_ns": 36.98, + "clear_slowest_ns": 39.605, + "set_ns": 1.211, + "set_fastest_ns": 1.199, + "set_slowest_ns": 1.234, + "disengaged_ns": 0.0, + "disengaged_fastest_ns": 0.0, + "disengaged_slowest_ns": 0.012, + "provisional": false } } diff --git a/doc/performance.adoc b/doc/performance.adoc index 5fb877b..78e5c24 100644 --- a/doc/performance.adoc +++ b/doc/performance.adoc @@ -140,10 +140,8 @@ by Apple LLVM 21.0.0 (clang-2100.4.1.1) (bench/timing.cpp): each row is the medi fastest and slowest run beside it, each minus the median of the same loop without the guard, in nanoseconds per call; a figure that rounds to zero or below, the loop's noise, shows as 0.0. -The guard sets DIT on this machine. The machine's 1, 5 and 15 minute load averages were 8/19/20 -before and 8/19/20 after, on 18 cores. - -NOTE: these figures are provisional. They were measured on a shared machine under load (the load averages above), and are measured again in a final measurement pass on a quiet machine, once the work in progress has merged; read them as an upper bound. +The guard sets DIT on this machine. The machine's 1, 5 and 15 minute load averages were 2/3/3 +before and 2/3/3 after, on 18 cores. [cols="2,4,1,1,1",options="header"] |=== @@ -151,15 +149,15 @@ NOTE: these figures are provisional. They were measured on a shared machine unde | clear, as a program that sets nothing has it | reads DIT, sets it, and clears it on the way out -| 39.5 -| 39.5 +| 37.0 +| 37.0 | 39.6 | already set, by the program or by an outer guard | reads DIT, and reads it again on the way out -| 1.3 -| 1.3 -| 1.4 +| 1.2 +| 1.2 +| 1.2 | any: a function keyed by its arguments, called without a key | reads its flag and does nothing else