Skip to content

Add HKDF key derivation function (RFC 5869) - #46

Merged
dipu-bd merged 1 commit into
masterfrom
ccr-3e9bf447-zsxg7p-hkdf
Dec 3, 2026
Merged

dipu-bd merged 1 commit into
masterfrom
ccr-3e9bf447-zsxg7p-hkdf

Conversation

@dipu-bd

@dipu-bd dipu-bd commented Nov 29, 2026

Copy link
Copy Markdown
Member

Summary

Adds HKDF (RFC 5869), the extract-and-expand key derivation function used for Diffie-Hellman/KEM shared secrets, TLS 1.3 key schedules and AEAD key derivation. It follows the existing PBKDF2 API shape.

// HMAC-SHA256, the common default
final okm = hkdf(sharedSecret, salt: salt, info: utf8.encode('ctx'), keyLength: 32);

// any hash or MAC
final kdf = sha512.hkdf(salt: salt, info: info, keyLength: 64);
kdf.convert(ikm);  // extract + expand
kdf.extract(ikm);  // PRK only
kdf.expand(prk);   // expand an existing pseudorandom key
sha256.hmac.hkdf(...);
  • HKDF (lib/src/algorithms/hkdf.dart) extends KeyDerivatorBase. It exposes extract() and expand() as well as convert(), so it also covers expand-only uses where the input is already a pseudorandom key.
  • Defaults follow the RFC: a missing salt means HashLen zero bytes, info defaults to empty, and keyLength defaults to HashLen.
  • Validation: keyLength must be between 1 and 255 × HashLen, as the RFC requires. Anything else throws ArgumentError.
  • Public API (lib/src/hkdf.dart):
    • .hkdf(...) extensions on MACHash and BlockHashBase
    • a top-level hkdf() defaulting to HMAC-SHA256
    • exported from package:hashlib.
  • README: a row in the key-derivation table and a mention in the feature list.

Test vectors (all from verified sources, nothing invented)

test/fixures/hkdf_vectors.dart is generated from these files, with each source URL and its SHA-256 recorded in the header:

  • RFC 5869 Appendix A, all 7 cases (SHA-1 and SHA-256), as published verbatim by pyca/cryptography (vectors/cryptography_vectors/KDF/rfc-5869-HKDF-SHA{1,256}.txt). The tests check the intermediate PRK, the final OKM, and expand() from the published PRK. (rfc-editor.org and ietf.org are blocked from this environment's network, so I used pyca's verbatim copy. Its first case matches Wycheproof's "RFC 5869" case exactly.)
  • Project Wycheproof (C2SP), 339 cases: hkdf_sha{1,256,384,512}_test.json. They include empty salts, salts longer than the block size, salts that collide, maximal output sizes, and invalid cases with oversized outputs, which must throw.

The vector tests are not tagged vm-only, so they also run on node and dart2wasm. There they exercise HKDF on top of the 32-bit SHA-384/512 code.

There is also a differential test (test/compare/compare_hkdf_test.dart, vm-only) against pointycastle's HKDFKeyDerivator. It covers SHA-1/256/384/512 × 100 random combinations of IKM, salt, info and output lengths up to 600 bytes. All matched.

Verification

  • dart format --output=none --set-exit-if-changed . makes no changes
  • dart analyze --fatal-infos reports no issues
  • dart test -p vm: all tests pass (365 new)
  • dart test -p node: all tests pass
  • dart test -P wasm: all tests pass
  • the integration smoke test runs
  • bash scripts/coverage.sh reports 100% of lib/ covered (5998/5998 lines)

Benchmark (benchmark/hkdf.dart, HKDF-SHA256, dart run in a cloud container, not the reference machine):

Output hashlib PointyCastle
32 B 123,804 op/s 96,461 op/s
256 B 32,227 op/s 28,536 op/s
8160 B 1,185 op/s 1,136 op/s

It is a standalone benchmark, not wired into the aggregator, whose KDF section is built around password-hash security levels. BENCHMARK.md is untouched.

Notes

  • No CHANGELOG entry: the version heading is left to the maintainer. Suggested bullet: "Add HKDF (RFC 5869) with extract/expand, #.hkdf() extensions and a top-level hkdf()".
  • Two pre-existing issues I noticed and did not change:
    • KeyDerivatorBase.verify compares with an early-exit loop, so it isn't constant-time. HKDF inherits it, as PBKDF2 already does.
    • The README cites PBKDF2 as RFC-8081; it should be RFC 8018.

Generated by Claude Code

@codecov-commenter

codecov-commenter commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

Files with missing lines Coverage Δ
lib/src/algorithms/hkdf.dart 100.00% <100.00%> (ø)
lib/src/hkdf.dart 100.00% <100.00%> (ø)
lib/src/rapidhash.dart 100.00% <ø> (ø)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dipu-bd
dipu-bd force-pushed the ccr-3e9bf447-zsxg7p-hkdf branch from e1a258d to 7a983f9 Compare December 3, 2026 15:38
@dipu-bd
dipu-bd merged commit aab9009 into master Dec 3, 2026
18 checks passed
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.

2 participants