Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_<backend>_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, `<webcpp/secure/timing_guard.hpp>`) 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_<backend>_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
(`<webcpp/secure/erase_secret.hpp>`); 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`.
10 changes: 5 additions & 5 deletions bench/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ webcpp.run pbkdf2_sha512_key : pbkdf2_sha512.cpp : $(.requirements) ;
exe pbkdf2_sha512 : pbkdf2_sha512.cpp : $(.requirements) <variant>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: <webcpp/secure/timing_guard.hpp> alone.
webcpp.run timing_check : timing.cpp : <optimization>speed ;

# The same program at variant=release, the one collect.py --timing measures,
Expand Down
47 changes: 25 additions & 22 deletions bench/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -330,13 +332,14 @@ 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",
"(<<hash-backends-dit>>): one `mrs`, and where DIT was clear, two `msr` each",
"followed by an `isb`. Elsewhere (x86, WebAssembly, Windows on ARM64) 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.",
"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 (<<hash-backends-dit>>): 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.",
"",
]
if timing is None:
Expand Down Expand Up @@ -364,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"),
)
Expand Down
46 changes: 23 additions & 23 deletions bench/timing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
// 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
// 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": <ns>, "set": <ns>, "disengaged": <ns>, ...}`, 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
Expand All @@ -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 <webcpp/hash/detail/timing.hpp>
#include <webcpp/secure/timing_guard.hpp>

#include <algorithm>
#include <array>
Expand All @@ -39,7 +39,7 @@
#include <span>
#include <string_view>

namespace detail = webcpp::hash::detail;
namespace secure = webcpp::secure;

namespace {

Expand Down Expand Up @@ -73,18 +73,18 @@ 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
}

// 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
}
Expand Down Expand Up @@ -132,31 +132,31 @@ result measure(std::uint64_t iterations) {
std::array<double, runs> set{};
std::array<double, runs> 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.
bool volatile keyed = false;
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 {
Expand Down
3 changes: 2 additions & 1 deletion build.jam
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import webcpp-target ;
project /webcpp/hash ;

# tag::hash[]
alias hash : : : : <include>include <library>/boost//headers <library>/webcpp/config//config ;
alias hash : : : : <include>include <library>/boost//headers <library>/webcpp/config//config
<library>/webcpp/secure//secure ;
# end::hash[]

# tag::openssl[]
Expand Down
4 changes: 2 additions & 2 deletions doc/backends.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 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:

Expand All @@ -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<macs::hmac<D>>`, which runs the same Boost.Hash2 code on `backends::hash2` inside its guard, and a SipHash with DIT is `mac<macs::siphash_2_4>`; a program that calls `hmac<H>` 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 (<<hash-async>>). On `backends::webcrypto` the browser's JavaScript engine computes, and nothing is added.

A coverage test per backend (`test/timing_coverage_<backend>_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_<backend>_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 <<hash-performance-timing>>.

Expand Down
Loading
Loading