Skip to content

crypto: support non-byte WebCrypto lengths#63988

Open
panva wants to merge 1 commit into
nodejs:mainfrom
panva:webcrypto-non-byte-lengths
Open

crypto: support non-byte WebCrypto lengths#63988
panva wants to merge 1 commit into
nodejs:mainfrom
panva:webcrypto-non-byte-lengths

Conversation

@panva

@panva panva commented Jun 18, 2026

Copy link
Copy Markdown
Member

Align Node.js WebCrypto bit-length handling with the specs by accepting non-byte lengths where the algorithms operate on bit sequences, while preserving byte-multiple rejection where the specs require it.

The WebCrypto spec defines a byte sequence containing a bit sequence by zero-padding to the minimal whole-byte length. HMAC key generation/import/export are specified in bits: HmacKeyGenParams.length, HmacImportParams.length, and HmacKeyAlgorithm.length are key lengths in bits, generated keys use that bit length, imports use the first requested bits, and export returns a byte sequence containing the raw key bits:

The same bit-sequence conversion applies to non-byte deriveBits() output for ECDH and X25519, and to X448 in Secure Curves:

Modern Algorithms specifies cSHAKE and KMAC output lengths in bits. cSHAKE passes outputLength as the NIST SP 800-185 L parameter and returns a byte sequence containing the result. KMAC similarly uses bit-based key lengths and bit-based MAC output lengths:

This adds shared JS and C++ helpers for rounding bit lengths up to bytes and clearing unused low bits in the final byte. It uses them for cSHAKE output, ECDH/X25519/X448 derived bits, HMAC/KMAC key generation/import/derivation, and KMAC sign/verify.

CryptoKey.algorithm.length now preserves the requested non-byte bit length for generated, imported, and derived HMAC/KMAC keys. Raw/JWK export still serializes whole bytes with unused low bits cleared; raw/JWK round-trips preserve non-byte precision only when the unwrap/import algorithm supplies the explicit bit length.

Byte-multiple validation is intentionally kept for algorithms whose specs require it, including HKDF, PBKDF2, Argon2, TurboSHAKE, and KangarooTwelve:

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Jun 18, 2026
@panva panva added crypto Issues and PRs related to the crypto subsystem. webcrypto labels Jun 18, 2026
@panva panva force-pushed the webcrypto-non-byte-lengths branch from 019cc7b to 348cf8c Compare June 18, 2026 16:57
Add shared bit-length helpers for WebCrypto operations that accept bit
sequences whose length is not byte-aligned.

Use the helpers for cSHAKE output, ECDH-derived bits, HMAC/KMAC key
generation/import/derivation, and KMAC sign/verify output. Preserve the
requested bit length in CryptoKey algorithm metadata while storing and
exporting rounded-up byte material with unused low bits cleared.

Keep byte-multiple validation for algorithms whose specs require it.

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
@panva panva force-pushed the webcrypto-non-byte-lengths branch from 348cf8c to f27f79a Compare June 18, 2026 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

crypto Issues and PRs related to the crypto subsystem. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. webcrypto

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants