Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
16d9db4
hash: public names spelled out (input_key_material, pseudorandom_key,…
Nov 23, 2026
7cfdc49
hash: the lint's findings on the renamed names (trailing commas, a co…
Nov 23, 2026
25fa0b3
hash: restore scrypt's header example and re-wrap the HKDF prose (rev…
Nov 24, 2026
094c2c0
hash: hkdf over byte pieces on backends::hash2, no cap and no heap
Nov 24, 2026
a2cc190
hash: the lint's findings on HKDF over pieces (trailing commas, a con…
Nov 24, 2026
f152c62
hash: HKDF over pieces review fixes (the exact braced-list rule and i…
Nov 24, 2026
25d62aa
hash: the lint's finding on HKDF over pieces (a const salt guard result)
Nov 24, 2026
ae542fb
hash: hkdf over byte pieces on backends::sodium and backends::bearssl
Nov 24, 2026
346740f
hash: one extract_and_expand body for the pieces engines, merged bear…
Nov 24, 2026
b349024
hash: clang-format the pieces engines' shared extract_and_expand calls
Nov 24, 2026
539ab63
hash: hkdf over byte pieces on backends::openssl and backends::botan
Nov 24, 2026
1e2eadb
hash: the pieces engines' Doc Comments keep no colon after a referenc…
Nov 24, 2026
2a00223
hash: the HKDF over pieces harness draws short outputs, its edge leng…
Nov 24, 2026
44bf2b0
hash: the HKDF over pieces harness's edge splits end their lists with…
Nov 24, 2026
f18b2a8
hash: async_hkdf_extract, async_hkdf_expand and async_hkdf over byte …
Nov 24, 2026
ccc01fe
hash: the lint's findings on the asynchronous HKDF forms (trailing co…
Nov 24, 2026
33ceaea
hash: backends::webcrypto's hkdf over byte pieces, on WebCrypto's HMAC
Nov 24, 2026
a8e3a21
hash: the lint's findings on webcrypto's hkdf over pieces (two nodisc…
Nov 24, 2026
5fb37cd
hash: webcrypto's hkdf over pieces fails on a refused buffer, gathers…
Nov 24, 2026
7b60d6e
hash: the lint's findings on webcrypto's hkdf over pieces fix (three …
Nov 24, 2026
b436ede
hash: the webcrypto messages' hkdf calls pass named inputs, not {}, w…
Nov 24, 2026
7885f4e
hash: the reference documents byte_pieces' accessors and WEBCPP_HASH_…
Nov 24, 2026
0e51ffe
hash: the guide documents hkdf over byte pieces and its asynchronous …
Nov 24, 2026
55d2377
hash: the lint's findings on the hkdf_pieces example (two trailing co…
Nov 24, 2026
e953209
hash: the guide and the changelog limit HKDF over pieces' no-copy cla…
Nov 24, 2026
ddaeff1
hash: timing_during_async checks DIT in the asynchronous HKDF functio…
Nov 24, 2026
0273659
hash: the lint's findings on timing_during_async's pieces case (three…
Nov 24, 2026
d0df6a3
hash: merge origin/main (crypto's uniform types) into hkdf-pieces
Nov 24, 2026
dccd138
hash: the end lint's findings (wipe_record's noexcept store, argon2's…
Nov 24, 2026
bdc4e99
hash: the MSVC lanes' findings (sodium's HKDF-pieces update outside i…
Nov 25, 2026
8ca88de
hash: the lint's finding on async_heavy_botan's test_salt (its two-li…
Nov 25, 2026
6e8e646
hash: the final review's findings (one empty piece run deterministica…
Nov 25, 2026
22fba54
hash: the lint's finding on hkdf_pieces.hpp (the empty-piece cases' c…
Nov 25, 2026
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
32 changes: 27 additions & 5 deletions bench/allocation_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
shaped like doc/measurements.throughput.json's: operation, backend, target, implementation and the
counts. backends::botan's records (one per call of each of its entry points, a first call and a
steady one) keep their tag; the table gives one row per operation and target, the range over the
tags. The page's table, between the `tag::table[]` and `end::table[]` lines, is rendered from the
data and never typed; --check fails when the page differs from what the data renders.
tags. An operation recorded as `<name>_pieces`, HKDF's functions over byte pieces, is the table's
"`<name>` over pieces". The page's table, between the `tag::table[]` and `end::table[]` lines, is
rendered from the data and never typed; --check fails when the page differs from what the data
renders.

The table is built only from release builds: the counting programs are bench/Jamfile's
allocation_release, allocation_openssl_release and allocation_botan_release (`b2
Expand Down Expand Up @@ -48,7 +50,23 @@
"the table is built only from release builds: bench/Jamfile's allocation_release, "
"allocation_openssl_release and allocation_botan_release at " + BUILD
)
KEPT = {"argon2id", "scrypt", "sodium_init", "hkdf", "pbkdf2"}
# OpenSSL's operations the table keeps, in its order: HKDF, its three functions over byte pieces
# (operation=<name>_pieces), and PBKDF2.
OPENSSL_OPERATIONS = (
"hkdf",
"hkdf_extract_pieces",
"hkdf_expand_pieces",
"hkdf_pieces",
"pbkdf2",
)
KEPT = {"argon2id", "scrypt", "sodium_init", *OPENSSL_OPERATIONS}


def operation_name(operation):
"""The table's name of a record's operation: `hkdf`, or `hkdf` over pieces for hkdf_pieces."""
if operation.endswith("_pieces"):
return "`%s` over pieces" % operation.removesuffix("_pieces")
return "`%s`" % operation


def build_of(output):
Expand Down Expand Up @@ -218,7 +236,7 @@ def render(data):
if not c["c_allocator_counted"]:
counted = "C library: not counted"
rows.append((c["operation"], c["backend"], target, str(c["operator_new"]), counted))
for op in ("hkdf", "pbkdf2"):
for op in OPENSSL_OPERATIONS:
mine = [
c for c in by_target[target] if c["operation"] == op and c["backend"] == "openssl"
]
Expand Down Expand Up @@ -246,7 +264,11 @@ def at(hmac, call):
"",
]
for row in rows:
lines.append("| `%s` | `%s` | %s | %s | %s" % row)
operation, backend, target, operator_new, detail = row
lines.append(
"| %s | `%s` | %s | %s | %s"
% (operation_name(operation), backend, target, operator_new, detail)
)
lines.append("|===")
return "\n".join(lines) + "\n"

Expand Down
31 changes: 25 additions & 6 deletions doc/allocation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
hash's own code allocates nothing: no function of `webcpp::hash` calls the global
`operator new`, on any target, and none calls the C library's allocator on the targets
where a program can count it. Test `allocation` proves that hash allocates nothing for
Keccak-256, BLAKE3, Boost.Hash2's digests and hmacs, and HKDF and PBKDF2 on
Keccak-256, BLAKE3, Boost.Hash2's digests and hmacs, and HKDF (its three functions over
byte pieces too, with inputs of 4 KiB and 1 MiB in four pieces) and PBKDF2 on
`backends::hash2`, `backends::sodium` and `backends::bearssl`, the failing calls (every
guard's refusal) included. It counts every form of `operator new` and the C library's
`malloc`, `calloc`, `realloc`, `aligned_alloc` and `posix_memalign` on glibc, macOS,
Expand All @@ -23,9 +24,12 @@ counter does not see is `mmap`, which the C libraries below use.

One exception is not hash's own: on Emscripten, a call on `backends::webcrypto` awaits each
of WebCrypto's promises through webcpp::asio's `browser::async_await`, whose bridge to
JavaScript takes a block from the C library per promise awaited (one promise for
`async_digest`, two for `async_hmac`, `async_hkdf` and `async_pbkdf2`, which import a key
first). Test `allocation` counts a bare await's blocks, prints them as
JavaScript takes a block from the C library per promise awaited: one promise for
`async_digest`; two for `async_hmac`, `async_pbkdf2`, the single-buffer `async_hkdf` and
`async_hkdf_extract` (over pieces too), which import a key first; 1 + n for
`async_hkdf_expand` and 3 + n for `async_hkdf` over pieces, which sign once per block of
output, n the output's size in digests rounded up. The pieces are gathered into JavaScript
memory, not webcpp's. Test `allocation` counts a bare await's blocks, prints them as
`record operation=async_await`, and checks that each WebCrypto call makes exactly that many
per promise and nothing more. The table below leaves that block out: it lists the C
libraries of hash's backends, and the block is webcpp::asio's.
Expand Down Expand Up @@ -66,8 +70,8 @@ The table is generated from `measurements.allocation.json` by
`allocation_release`, `allocation_openssl_release` and `allocation_botan_release` of
`bench/Jamfile`, the programs of the three tests built at `variant=release`, and the collector
refuses an output of any other variant. The tests of the lanes keep the lane's variant. The
figures are those of the machine the file records; Botan's rows, collected on 2026-11-21, are
to be re-measured in the final pass, with every other count, before the release.
figures are those of the machine and the day the file records (every row collected on
2026-11-24), to be measured again in the final pass before the release.

// tag::table[]
[cols="1,1,1,1,3",options="header"]
Expand All @@ -78,6 +82,9 @@ to be re-measured in the final pass, with every other count, before the release.
| `argon2id` | `sodium` | native | 0 | C library calls: 3
| `scrypt` | `sodium` | native | 0 | C library calls: 0
| `hkdf` | `openssl` | native | 0 | OpenSSL: 4080 first call (hmac_sha2_256), 30 first call (hmac_sha2_512), 24 steady state
| `hkdf_extract` over pieces | `openssl` | native | 0 | OpenSSL: 10 first call (hmac_sha2_256), 10 first call (hmac_sha2_512), 10 steady state
| `hkdf_expand` over pieces | `openssl` | native | 0 | OpenSSL: 30 first call (hmac_sha2_256), 20 first call (hmac_sha2_512), 20 steady state
| `hkdf` over pieces | `openssl` | native | 0 | OpenSSL: 40 first call (hmac_sha2_256), 30 first call (hmac_sha2_512), 30 steady state
| `pbkdf2` | `openssl` | native | 0 | OpenSSL: 30 first call (hmac_sha2_256), 18 first call (hmac_sha2_512), 18 steady state
| `digest_of` | `botan` | native | 1 to 4 | Botan: 1 to 13 C library calls (operator new's included), over its 25 algorithms; the same in a steady call
| `hasher` | `botan` | native | 1 to 4 | Botan: 1 to 13 C library calls (operator new's included), over its 25 algorithms; the same in a steady call
Expand All @@ -87,6 +94,9 @@ to be re-measured in the final pass, with every other count, before the release.
| `hkdf` | `botan` | native | 17 to 30 | Botan: 25 to 39 C library calls (operator new's included), over its 14 algorithms; the same in a steady call
| `pbkdf2` | `botan` | native | 16 to 29 | Botan: 21 to 34 C library calls (operator new's included), over its 14 algorithms; the same in a steady call
| `kbkdf` | `botan` | native | 18 to 28 | Botan: 21 to 32 C library calls (operator new's included), over its 14 algorithms; the same in a steady call
| `hkdf_extract` over pieces | `botan` | native | 4 to 7 | Botan: 7 to 10 C library calls (operator new's included), over its 14 algorithms; the same in a steady call
| `hkdf_expand` over pieces | `botan` | native | 4 to 7 | Botan: 7 to 11 C library calls (operator new's included), over its 14 algorithms; the same in a steady call
| `hkdf` over pieces | `botan` | native | 8 to 14 | Botan: 14 to 20 C library calls (operator new's included), over its 14 algorithms; the same in a steady call
| `sskdf` | `botan` | native | 4 to 7 | Botan: 5 to 8 C library calls (operator new's included), over its 10 algorithms; the same in a steady call
| `x963kdf` | `botan` | native | 4 | Botan: 5 to 6 C library calls (operator new's included), over its 5 algorithms; the same in a steady call
| `argon2id` | `botan` | native | 6 | Botan: 9 C library calls (operator new's included), of kdfs::argon2id; the same in a steady call
Expand All @@ -99,11 +109,17 @@ to be re-measured in the final pass, with every other count, before the release.
| `argon2id` | `sodium` | wasip2 | 0 | C library calls: 5
| `scrypt` | `sodium` | wasip2 | 0 | C library calls: 1
| `hkdf` | `openssl` | wasip2 | 0 | OpenSSL: 3960 first call (hmac_sha2_256), 30 first call (hmac_sha2_512), 24 steady state
| `hkdf_extract` over pieces | `openssl` | wasip2 | 0 | OpenSSL: 10 first call (hmac_sha2_256), 10 first call (hmac_sha2_512), 10 steady state
| `hkdf_expand` over pieces | `openssl` | wasip2 | 0 | OpenSSL: 30 first call (hmac_sha2_256), 20 first call (hmac_sha2_512), 20 steady state
| `hkdf` over pieces | `openssl` | wasip2 | 0 | OpenSSL: 40 first call (hmac_sha2_256), 30 first call (hmac_sha2_512), 30 steady state
| `pbkdf2` | `openssl` | wasip2 | 0 | OpenSSL: 30 first call (hmac_sha2_256), 18 first call (hmac_sha2_512), 18 steady state
| `sodium_init` | `sodium` | wasip3 | 0 | C library calls: 1
| `argon2id` | `sodium` | wasip3 | 0 | C library calls: 5
| `scrypt` | `sodium` | wasip3 | 0 | C library calls: 1
| `hkdf` | `openssl` | wasip3 | 0 | OpenSSL: 3960 first call (hmac_sha2_256), 30 first call (hmac_sha2_512), 24 steady state
| `hkdf_extract` over pieces | `openssl` | wasip3 | 0 | OpenSSL: 10 first call (hmac_sha2_256), 10 first call (hmac_sha2_512), 10 steady state
| `hkdf_expand` over pieces | `openssl` | wasip3 | 0 | OpenSSL: 30 first call (hmac_sha2_256), 20 first call (hmac_sha2_512), 20 steady state
| `hkdf` over pieces | `openssl` | wasip3 | 0 | OpenSSL: 40 first call (hmac_sha2_256), 30 first call (hmac_sha2_512), 30 steady state
| `pbkdf2` | `openssl` | wasip3 | 0 | OpenSSL: 30 first call (hmac_sha2_256), 18 first call (hmac_sha2_512), 18 steady state
| `sodium_init` | `sodium` | emscripten | 0 | C library calls: 0
| `argon2id` | `sodium` | emscripten | 0 | C library calls: 4
Expand All @@ -116,6 +132,9 @@ to be re-measured in the final pass, with every other count, before the release.
| `hkdf` | `botan` | emscripten | 24 to 44 | Botan: 32 to 53 C library calls (operator new's included), over its 14 algorithms; the same in a steady call
| `pbkdf2` | `botan` | emscripten | 23 to 43 | Botan: 28 to 48 C library calls (operator new's included), over its 14 algorithms; the same in a steady call
| `kbkdf` | `botan` | emscripten | 24 to 43 | Botan: 27 to 47 C library calls (operator new's included), over its 14 algorithms; the same in a steady call
| `hkdf_extract` over pieces | `botan` | emscripten | 4 to 13 | Botan: 7 to 16 C library calls (operator new's included), over its 14 algorithms; the same in a steady call
| `hkdf_expand` over pieces | `botan` | emscripten | 4 to 13 | Botan: 10 to 17 C library calls (operator new's included), over its 14 algorithms; the same in a steady call
| `hkdf` over pieces | `botan` | emscripten | 8 to 26 | Botan: 18 to 32 C library calls (operator new's included), over its 14 algorithms; the same in a steady call
| `sskdf` | `botan` | emscripten | 7 to 10 | Botan: 8 to 11 C library calls (operator new's included), over its 10 algorithms; the same in a steady call
| `x963kdf` | `botan` | emscripten | 7 | Botan: 8 to 9 C library calls (operator new's included), over its 5 algorithms; the same in a steady call
| `argon2id` | `botan` | emscripten | 10 | Botan: 13 C library calls (operator new's included), of kdfs::argon2id; the same in a steady call
Expand Down
89 changes: 69 additions & 20 deletions doc/async.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ The functions come in two kinds:

* *The heavy functions*, `async_argon2id`, `async_argon2`, `async_scrypt`,
`async_pbkdf2`, `async_bcrypt`, `async_bcrypt_pbkdf` and
`async_bcrypt_verify`, run the work on another thread, on an executor the program
gives (the executor of a `webcpp::asio::thread_pool`), and complete on the
event loop. They exist only where the build has threads.
* *The cheap functions*, `async_digest`, `async_hmac` and `async_hkdf`, cost
microseconds: they run at once and complete through the event loop, never
inside the call. They exist on every target, for the algorithms the browser's
WebCrypto has (SHA-1, SHA-256, SHA-384, SHA-512, and HMAC and HKDF over them),
so that a program written once against WebCrypto's family builds and runs
everywhere.
`async_bcrypt_verify`, run the work on another thread, on an executor the
program gives (the executor of a `webcpp::asio::thread_pool`), and complete on
the event loop. They exist only where the build has threads.
* *The cheap functions*, `async_digest`, `async_hmac`, `async_hkdf_extract`,
`async_hkdf_expand` and `async_hkdf` (each HKDF function also over byte
pieces), cost microseconds: they run at once and complete through the event
loop, never inside the call. They exist on every target, for the algorithms
the browser's WebCrypto has (SHA-1, SHA-256, SHA-384, SHA-512, and HMAC and
HKDF over them), so that a program written once against WebCrypto's family
builds and runs everywhere.

Each runs, by default, on the synchronous function's backend
(<<hash-default-backend>>): `backends::webcrypto`, the browser's WebCrypto, is
Expand Down Expand Up @@ -204,12 +205,12 @@ interrupted.
[#hash-async-cheap]
=== The cheap functions through the event loop

`async_digest`, `async_hmac` and `async_hkdf` run the synchronous function at
once, its guards first, and post the completion, with its error code, to the
executor the call gives, whence it reaches the handler's associated executor.
The completion never runs inside the call, so a handler written for the
browser's WebCrypto, which always completes later, behaves the same on every
target:
`async_digest`, `async_hmac`, `async_hkdf_extract`, `async_hkdf_expand` and
`async_hkdf` run the synchronous function at once, its guards first, and post
the completion, with its error code, to the executor the call gives, whence it
reaches the handler's associated executor. The completion never runs inside
the call, so a handler written for the browser's WebCrypto, which always
completes later, behaves the same on every target:

[source]
----
Expand All @@ -227,14 +228,47 @@ include::{examples}/async_hkdf.expected[]
as `async_digest<digests::blake2b>`, stops the build: a post would add nothing
to the synchronous function.

HKDF's three asynchronous functions are those of <<hash-hkdf>>, the
pseudorandom key written into a span of the digest's size:
`async_hkdf_extract(executor, pseudorandom_key, salt, input_key_material,
token)`, `async_hkdf_expand(executor, output_key_material, pseudorandom_key,
application_info, token)` and `async_hkdf(executor, output_key_material, salt,
input_key_material, application_info, token)`. Each also has a form over
`byte_pieces` (<<hash-hkdf-pieces>>), with no limit on the pieces, whose
inputs are read in the call: on a synchronous backend the synchronous function
over pieces runs in the call itself and only its error code is posted, and on
`backends::webcrypto` the salt, the pseudorandom key and the pieces are copied
into JavaScript memory in the call. So a braced list written at the call,
whose array dies at the end of the call's full expression, is safe with every
completion token, a lazy one included: `async_hkdf(executor, key, salt,
{hpke_v1, suite_id, extract_label, ikm}, {length, hpke_v1, suite_id,
expand_label, info}, token)` runs one HPKE LabeledExtract and one
LabeledExpand in one asynchronous call. `hkdf_pieces.cpp`
(<<hash-hkdf-pieces>>) calls `hkdf_extract` and `hkdf_expand` apart instead,
since HPKE's key schedule expands one secret three times.

*Pitfalls.*

* *The storage is the handler's.* The post allocates with the handler's
associated allocator (`bind_allocator`), as every Asio operation does; a
program that counts or pools its allocations binds its own.
* *The same spans rule.* The output and the inputs are the caller's until the
completion, even though the work runs before the call returns: a backend
named in the type may run later (WebCrypto does).
named in the type may run later (WebCrypto does). The forms over pieces read
every input in the call, on every backend: the salt, the pseudorandom key, the
pieces and their list. Only the output stays the caller's until the
completion.
* *A lazy token does not delay the forms over pieces.* With `asio::deferred`,
or the awaitable of `use_awaitable`, an operation normally starts when it is
launched or awaited. The forms over pieces on a synchronous backend compute
and write the output at the call, before the token is used; only the
completion waits. A program that must not derive before the launch calls
them at the launch.
* *A lone `{}` is ambiguous.* As in the synchronous functions, `{}`, or a
braced list of one element that is not exactly a
`std::span<std::uint8_t const>`, in a pieces position matches both forms:
`async_hkdf(executor, okm, salt, {}, {}, token)` does not compile. Pass an
empty span, `x` itself or `+byte_pieces{...}+`.
* *`backends::webcrypto` is for Emscripten.* Named off Emscripten, it stops
the build: "webcpp::hash: backends::webcrypto exists only on Emscripten: name
a synchronous backend, or leave the default." In a synchronous function it
Expand Down Expand Up @@ -295,11 +329,26 @@ Strands and type-erased executors of the `io_context` are accepted; behind
type: pass the inner strand, or the executor of the `io_context`. That the
`io_context` runs on the calling thread is the program's duty: it cannot be
checked.
* *WebCrypto's limits are narrower.* An HKDF info longer than 1,024 bytes
(node's limit, where the other backends take 32,768) completes with
`input_too_long`; PBKDF2's iteration count is at most 2^32 - 1
(`invalid_iterations`); an output of HKDF or PBKDF2 is at most
* *WebCrypto's limits are narrower.* The single-buffer `async_hkdf` runs
WebCrypto's own HKDF, `deriveBits`, and an application_info longer than 1,024
bytes (node's limit, where the other backends take 32,768) completes with
`input_too_long`. The other HKDF functions run RFC 5869's steps on WebCrypto's
HMAC: the single-buffer `async_hkdf_expand` keeps the 32,768 bytes of every
backend (Extract has no application info), and the forms over pieces have no
limit of hash's own, so `async_hkdf` over
`+byte_pieces{input_key_material}+` and `+byte_pieces{info}+` takes a longer
info. PBKDF2's iteration count is at most
2^32 - 1 (`invalid_iterations`); an output of HKDF or PBKDF2 is at most
(2^32 - 1) / 8 bytes. A program that needs more names a synchronous backend.
* *HKDF over HMAC is a promise per block.* `async_hkdf_extract` imports the
salt and signs once; `async_hkdf_expand` imports the pseudorandom key and
signs once per digest of output; `async_hkdf` over pieces does both. The
inputs are gathered into JavaScript memory in the call, as long as the
runtime will allocate a buffer that size (`backend_failure` beyond), and every
buffer of JavaScript that held a secret is zeroed when its promise settles.
The keys WebCrypto imports (`CryptoKey`) cannot be zeroed, since WebCrypto has
no way to, and neither can a tag that arrives after a cancellation, which is
dropped unseen.
* *An empty HMAC key is one zero byte.* WebCrypto refuses an empty key; HMAC
pads every key with zeros to its block (RFC 2104), so the two are the same
key, and webcpp passes one zero byte: the tag is the other backends'.
Expand Down
Loading
Loading