fix(ext/crypto): pad JWK to multiple of 8 bytes for AES-KW wrap#35475
Conversation
`SubtleCrypto.wrapKey("jwk", key, wrappingKey, "AES-KW")` threw a
TypeError because the UTF-8 JSON serialization of the JWK is generally
not a multiple of 8 bytes, which AES-KW requires.
Pad with ASCII spaces to the next multiple of 8 — the de-facto behavior
of Node.js, Chrome, and Firefox. JSON.parse on unwrap accepts the
trailing whitespace, so the unwrap path needs no change.
Fixes #35416.
|
|
|
Second unrelated infra flake on the same CI run: |
Summary
SubtleCrypto.wrapKey("jwk", key, wrappingKey, "AES-KW")threwTypeError: Data must be multiple of 8 bytesbecause the UTF-8 JSON serialization of a JWK is generally not aligned to 8 bytes, which AES-KW requires.Pad the JWK JSON with ASCII spaces (
0x20) to the next multiple of 8 before passing to AES-KW. This matches the de-facto behavior of Node.js, Chrome, and Firefox (the spec is silent here, but every implementation pads). The unwrap path needs no change —JSON.parseaccepts the trailing whitespace produced after AES-KW unwrap.Repro from the issue now matches Node.js output:
Fixes #35416.
Test plan
cargo test -p unit_tests --test unit -- webcrypto—webcrypto_testandwebcrypto_mldsa_testpasscargo test -p deno_crypto— all crate tests passcargo clippy -p deno_crypto— cleantestWrapUnwrapJwkAesKwcovering all four HMAC hashes (SHA-1/256/384/512); verifies wrapped length is a multiple of 8 and that the unwrapped key produces the same MAC as the original