Skip to content

hash: HKDF over byte pieces, no cap and no heap - #14

Merged
pinhopro merged 33 commits into
mainfrom
hash/hkdf-pieces
Nov 25, 2026
Merged

pinhopro merged 33 commits into
mainfrom
hash/hkdf-pieces

Conversation

@pinhopro

Copy link
Copy Markdown
Contributor

HKDF over byte pieces: byte_pieces (its own header, <webcpp/hash/byte_pieces.hpp>) and the pieces forms of hkdf, hkdf_extract and hkdf_expand, with no cap on the input keying material, the application info or the number of pieces, on backends::hash2, sodium, bearssl (Extract and Expand included), openssl and botan; no heap of hash's own on the synchronous backends. The asynchronous forms async_hkdf, async_hkdf_extract and async_hkdf_expand over pieces run on the synchronous backends and on backends::webcrypto (gathered into JavaScript memory in the initiating call). Every HMAC state, T(i) and PRK is wiped on success and on failure; a piece longer than a backend's update length is fed in slices.

Breaking change: a lone {} in a pieces position, or a one-element braced list of something other than std::span<std::uint8_t const>, is now ambiguous; pass the value itself, an empty span or byte_pieces{...}.

Also: the MSVC lanes' findings, the end lint's findings (incl. pre-existing argon2/xkcp member initializers), timing coverage of the pieces forms, the guide, changelog, generated tables and allocation measurements (release builds). origin/main merged.

Change: webcpporg/proposals (hkdf-pieces). Verified locally on every lane before merge (clang, gcc-15, wasip2, wasip3, Emscripten under node with WebCrypto, Linux and macOS lint, MSVC 14.3 and 14.5 on x64 and arm64).

R added 30 commits November 23, 2026 20:35
… application_info, scrypt's cost, block_size and parallelism, argon2's memory_kibibytes)
…nst, a Jam comment's width, the changelog's form)
…st, a split check, a fluent chain, a Doc Comment's colon, a concise #ifdef)
…ts compile tests, a slice of at least one byte at compile time, one salt guard, a reflowed comment)
libsodium's crypto_auth_hmacsha256_* and crypto_auth_hmacsha512_* (the keyed state copied per block) and BearSSL's br_hmac_* (a key context made once, an HMAC context per block) run hkdf_extract, hkdf_expand and hkdf over pieces wherever the backend has the HMAC; BearSSL gains Extract and Expand over pieces while its single-buffer ones stay refused. Every state, block and pseudorandom key is wiped. Tests: the equality harness on every hmac of the cells, again in 7-byte slices, the wipe sequences, the spec's BearSSL Extract scenario and its refused single-buffer twin, and zero allocations on both backends.
…ssl timing bullets, sha512 sliced twin on sodium
OpenSSL's EVP_MAC "HMAC" (the context keyed by the pseudorandom key duplicated by EVP_MAC_CTX_dup for each block, the error queue left as found, not_enough_memory on an allocation's failure) and Botan's MessageAuthenticationCode "HMAC(<h>)" (re-keyed for each block by clear and set_key, a salt past 8,192 bytes hashed first, every exception caught at the boundary) run hkdf_extract, hkdf_expand and hkdf over pieces over the hmacs of their kdfs::hkdf cells. Each block and the pseudorandom key are wiped; the libraries' contexts are freed and cleared by them. Tests: the equality harness over every hmac of both cells, again in 7-byte slices, the wipe sequences, a long salt on Botan; the pieces forms in OpenSSL's out-of-memory, error queue, failure and limits tests and Botan's failure test; the libraries' own allocations of the three forms printed.
…e, and the OpenSSL error queue and failure tests call the pieces forms in a function of their own
…ths deterministic, and the pieces tests share one wipe record

The random cases of support/hkdf_pieces.hpp drew outputs of up to 255 digests, about 128 blocks per
case, which made hkdf_pieces_botan take minutes in a debug build. Most random cases now draw 1 byte
to 4 digests (every 100th the whole range), and the edge lengths 1, HashLen - 1, HashLen,
HashLen + 1, 255 HashLen - 1 and 255 HashLen are cases of their own with random and empty pieces.
The wipe record of the pieces tests moves to support/wipe_record.hpp. allocation_openssl and
allocation assert the error of each refusal, and the reflowed comments are rewrapped.
async_hkdf, async_hkdf_extract and async_hkdf_expand over byte_pieces, and
the single-buffer async_hkdf_extract and async_hkdf_expand, run RFC 5869 on
WebCrypto's HMAC (importKey, then one sign per step or block) as a composed
operation whose state is emscripten::vals: the pieces are gathered into
JavaScript memory in the initiating call, every buffer that held a secret is
zeroed once read and at the completion, and the single-buffer
async_hkdf_expand keeps hkdf_max_application_info_size (ruling R3). The
capability table gains the five webcrypto cells; hkdf_guards reuses
hkdf_extract_guards, and hkdf_extract_into says why it holds no
timing_guard.
…ards, an unused result, parentheses, a split complexity)
… nothing after one, and zeroes the last tag before the handler

A runtime refusing a JavaScript buffer while the pieces are gathered (new
Uint8Array throwing a RangeError or an out of memory) is caught in
JavaScript and completes with backend_failure, the output and the buffers
already made zeroed, instead of ending the program; once an input is not
gathered (refused, or over webcrypto_max_gathered), async_hkdf gathers no
application info. The last tag is zeroed before the completion handler
runs. A test seam lowers webcrypto_max_gathered (async_webcrypto_gathered).
The tests add the empty list, one empty piece and {empty, a, empty} as
each list, RFC 5869's cases over the four hmacs with hash2 as the
reference, and the refused buffers.
…hich the pieces forms make ambiguous

A lone {} in the input keying material and application info positions is ambiguous between the single-buffer and the pieces forms (rulings R9, R11), so webcrypto_synchronous, async_hkdf_not_in_webcrypto and webcrypto_off_emscripten stopped at the overload ambiguity before their message. Each now passes a named array, as sodium_hmac and bearssl_hmac do.
…forms

kdfs.adoc's HKDF over pieces with HPKE's labeled steps (example/hkdf_pieces.cpp), async.adoc's three asynchronous HKDF functions and the pieces read in the call, the WebCrypto limits and promises, allocation.adoc's pieces rows from allocation_release (allocation_collect.py renders them by their function), the changelog's lines, and timing_during over the three pieces forms.
…im to the synchronous backends

WebCrypto's asynchronous forms gather every input into JavaScript buffers in the call; kdfs.adoc and the changelog now say so, async.adoc names the salt and the pseudorandom key among the inputs read in the call and describes the HPKE call accurately, and the long lines are rewrapped to the pages' 78 columns.
… and xkcp's member initializers, byte_pieces' braced-list NOLINT)

- test/support/wipe_record.hpp: the noexcept seam stores with operator[] after its bounds check,
  not with at(), which could throw (bugprone-exception-escape). A wipe past the record's storage
  is counted, and wiped_sizes() now fails the test on it instead of truncating silently;
  clear_wipes() discards the record however many wipes it counted, for the work before a checked
  step (the four hkdf_pieces backend tests, which discarded through wiped_sizes()).
- argon2.hpp: argon2_parameters' passes and memory_kibibytes, and argon2id_parameters' passes and
  memory, get {} default member initializers, as every designatable aggregate member does
  (cppcoreguidelines-pro-type-member-init).
- backends/xkcp.hpp: the KangarooTwelve state's customisation gets {} as well.
- example/password_storage.cpp: credential's members get {}.
- byte_pieces.hpp: the initializer_list constructor states with a NOLINT why it is not explicit,
  as the range constructor already does: a braced list {a, b} converts to its pieces by design.
…ts lambda, test_salt and test_password at file scope, /bigobj for timing_coverage_botan)

- backends/sodium.hpp: sodium_hkdf_pieces_engine::update_all's lambda calls update_slice, a
  static member, which calls libsodium's _update: MSVC 14.3 rejects a call of the template
  parameter `Update` inside a lambda (C2102, "'&' requires l-value", every TU of the sodium
  HKDF-pieces engine).
- async_heavy_botan, scrypt and argon2id tests: their file-scope password and salt are
  test_password and test_salt, which no parameter of the library hides (botan_pbkdf2_engine's
  `salt` made MSVC C4459 an error at /WX in async_heavy_botan).
- test/Jamfile: timing_coverage_botan compiles with /bigobj on MSVC; its HKDF-over-pieces cells
  pass the section limit of an MSVC 14.3 x64 object (C1128).
R added 3 commits November 24, 2026 22:48
…lly on every synchronous engine, the cap wording of jamfile.adoc and async.adoc, guide.json's application info label, byte_pieces' NOLINT on its constructor's line so MrDocs sees its comment)
@pinhopro
pinhopro merged commit 569b51b into main Nov 25, 2026
1 of 3 checks passed
@pinhopro
pinhopro deleted the hash/hkdf-pieces branch November 25, 2026 06:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant