Skip to content

feat: add HKDF-SHA256 and HKDF-SHA512 (extract, expand, derive) - #152

Merged
WingZer0o merged 2 commits into
mainfrom
feat/hkdf
Nov 26, 2026
Merged

WingZer0o merged 2 commits into
mainfrom
feat/hkdf

Conversation

@WingZer0o

@WingZer0o WingZer0o commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

New kdf module with the CASHKDF trait and CASHkdfSha256 / CASHkdfSha512: extract(salt, ikm) returns the PRK, expand(prk, info, length) derives a key from it, and derive does both. Separate extract and expand let one PRK yield several keys with distinct info, as the cas-p2p-chat key schedule needs.

An empty salt means RFC 5869's "not provided". A PRK shorter than HashLen is InvalidKey; a length of 0 or above 255 * HashLen is InvalidParameters. No new CasError variant. The ikm / prk arguments and the output buffer are zeroized.

Tests:

  • Known answers come from the Wycheproof hkdf_sha256 / hkdf_sha512 files, downloaded at test time like the other algorithms (169 cases, none skipped, including the EmptySalt cases). derive and extract + expand are both checked against every case.
  • tests/kdf.rs covers one PRK expanded into several keys, the output-length limits and short PRKs. Its inputs come from OsRng, so CodeQL's hard-coded-cryptographic-value alerts no longer apply.

🤖 Generated with Claude Code

New `kdf` module with the `CASHKDF` trait and `CASHkdfSha256` /
`CASHkdfSha512`: `extract(salt, ikm)` returns the PRK, `expand(prk, info,
length)` derives a key from it, and `derive` does both. Separate extract
and expand let one PRK yield several keys with distinct `info`, as the
cas-p2p-chat key schedule needs.

An empty salt means RFC 5869's "not provided". A PRK shorter than HashLen
is `InvalidKey`; a length of 0 or above 255 * HashLen is
`InvalidParameters`. No new `CasError` variant. The `ikm` / `prk`
arguments and the output buffer are zeroized.

Tested with RFC 5869 Appendix A cases 1-3 (committed, cross-checked with
Python's hmac) and Wycheproof hkdf_sha256 / hkdf_sha512 (169 cases, none
skipped).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread tests/kdf.rs Fixed
Comment thread tests/kdf.rs Fixed
…HKDF tests

CodeQL (rust/hard-coded-cryptographic-value) flagged the literal salts in
tests/kdf.rs. Draw every salt, IKM and PRK from OsRng instead, and drop
the empty-salt-equals-zero-salt test, whose point is a literal zero salt;
the Wycheproof EmptySalt cases cover it.

The known answers now come only from the downloaded Wycheproof
hkdf_sha256 / hkdf_sha512 files, like the other algorithms, so
tests/data/hkdf/rfc5869_sha256.txt is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@WingZer0o
WingZer0o merged commit d635cc2 into main Nov 26, 2026
9 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 26, 2026
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