@stenvault/pqc-wasm
v0.4.1
Published
ML-KEM-768/1024 + ML-DSA-65/87 WASM wrapper — RustCrypto compiled to WebAssembly
Readme
@stenvault/pqc-wasm
Post-quantum cryptography WASM wrapper for StenVault — ML-KEM-768 and ML-KEM-1024 (FIPS 203), ML-DSA-65 and ML-DSA-87 (FIPS 204), compiled from RustCrypto crates to WebAssembly.
Why
Replaces @openforge-sh/liboqs, an unofficial wrapper of the liboqs C library, with a self-maintained Rust→WASM build of the RustCrypto ml-kem and ml-dsa crates.
RustCrypto writes these crates to run in constant time, with ctutils for selections and comparisons. Neither WebAssembly nor a JavaScript engine guarantees that compiled code keeps that property, and this package has not verified it on the final .wasm. npm run test:timing checks total running time statistically; see Security for what it does not cover.
Build
Requires Rust toolchain with wasm32-unknown-unknown target and wasm-pack:
rustup target add wasm32-unknown-unknown
cargo install wasm-pack
# Build both targets
wasm-pack build --target bundler --release --out-dir pkg-bundler
wasm-pack build --target nodejs --release --out-dir pkg-nodejsTest
cargo test # Rust unit tests (native target)
npm test # copies of secrets in the JS layer (fake bindings), then interop, FIPS 203 input checks, Wycheproof vectors and memory wiping against the pkg-nodejs build
npm run test:fips203-scan # every single-byte change of one public key per ML-KEM parameter set, checked against noble
npm run test:timing # dudect-style timing test of decapsulation and signing, not part of npm testThe Kani proofs in proofs/ (CI, on pull requests and main) check that the
key and ciphertext types have the sizes the wrapper converts into, that ML-DSA
verifying keys and signatures one byte short are rejected, and that two
32-byte seed conversions cannot panic. They prove nothing about the
cryptography or its timing. Key and ciphertext decoding is fuzzed instead, by
the cargo-fuzz targets in fuzz/ (ML-KEM-768 and ML-DSA-65). The global
allocator, the crate's only unsafe code, is tested under Miri in CI.
The FIPS 203 tests draw their samples from a seed that is printed in the output.
FIPS203_SEED=<n> npm test replays a run.
API
import {
generateKemKeyPair,
encapsulate,
decapsulate,
generateSignatureKeyPair,
sign,
verify,
} from '@stenvault/pqc-wasm'
// ML-KEM-768
const kp = await generateKemKeyPair()
// kp.publicKey: Uint8Array (1,184 bytes)
// kp.secretKey: Uint8Array (2,400 bytes)
const { ciphertext, sharedSecret } = await encapsulate(kp.publicKey)
const decrypted = await decapsulate(ciphertext, kp.secretKey)
// sharedSecret === decrypted (32 bytes)
// ML-DSA-65
const sigKp = await generateSignatureKeyPair()
// sigKp.publicKey: Uint8Array (1,952 bytes)
// sigKp.secretKey: Uint8Array (32 bytes, FIPS 204 seed)
const signature = await sign(message, sigKp.secretKey)
// signature: Uint8Array (3,309 bytes)
const valid = await verify(message, signature, sigKp.publicKey)Every parameter set is also an object with the same methods, so moving to another security level means passing another object. The top-level functions above are ML-KEM-768 and ML-DSA-65.
import { mlKem768, mlKem1024, mlDsa65, mlDsa87, type Kem, type SignatureScheme } from '@stenvault/pqc-wasm'
const kem: Kem = mlKem1024
const kp = await kem.generateKeyPair()
const { ciphertext, sharedSecret } = await kem.encapsulate(kp.publicKey)
const decrypted = await kem.decapsulate(ciphertext, kp.secretKey)
const dsa: SignatureScheme = mlDsa87
const sigKp = await dsa.generateKeyPair()
const signature = await dsa.sign(message, sigKp.secretKey)
const valid = await dsa.verify(message, signature, sigKp.publicKey)A key belongs to one parameter set: an ML-KEM-768 key passed to mlKem1024
fails its length check, and the same holds for ML-DSA.
ML-KEM input errors
encapsulate and decapsulate reject (the promise throws) on malformed keys.
The lengths are those of the parameter set (see Key sizes); the messages below
are ML-KEM-768's.
| Function | Message | Cause |
|----------|---------|-------|
| encapsulate | invalid encapsulation key length: expected 1184 bytes | wrong length |
| encapsulate | encapsulation key is not canonical | a 12-bit coefficient of the public key, all but its last 32 bytes, is ≥ 3329 (FIPS 203 §7.2) |
| decapsulate | invalid decapsulation key length: expected 2400 bytes | wrong length |
| decapsulate | invalid ciphertext length: expected 1088 bytes | wrong length |
| decapsulate | decapsulation key hash check failed | SHA3-256 of the public key inside secretKey differs from the hash stored after it (FIPS 203 §7.3) |
| decapsulate | decapsulation key embeds a non-canonical encapsulation key | the hash matches, but a 12-bit coefficient of the public key inside secretKey is ≥ 3329 (the §7.2 check, applied to that key); key generation never produces such a key |
A corrupted ciphertext does not throw. decapsulate returns ML-KEM's
implicit-rejection secret, which does not match the sender's.
Architecture
Consumer (StenVault)
↓ imports from "@stenvault/pqc-wasm"
index.browser.ts / index.node.ts ← ensurePqcInit() + re-export
↓
core.ts ← free() in try/finally, param order normalization, each getter read once and returned as is
↓
pkg-bundler/ or pkg-nodejs/ ← wasm-bindgen generated bindings
↓
src/lib.rs ← 12 exported Rust functions, memory wiped after each call
↓
ml-kem + ml-dsa (RustCrypto) ← FIPS 203/204 implementationsThe consumer never calls free(), never calls init(), and never imports from pkg-* directly. The exports field in package.json routes browser to pkg-bundler and node/default to pkg-nodejs automatically.
Key sizes (FIPS)
| Algorithm | Public Key | Secret Key | Ciphertext | Signature | Shared Secret | |-----------|-----------|-----------|-----------|----------|--------------| | ML-KEM-768 | 1,184 B | 2,400 B | 1,088 B | — | 32 B | | ML-KEM-1024 | 1,568 B | 3,168 B | 1,568 B | — | 32 B | | ML-DSA-65 | 1,952 B | 32 B (seed) | — | 3,309 B | — | | ML-DSA-87 | 2,592 B | 32 B (seed) | — | 4,627 B | — |
Security
- Constant time: RustCrypto writes
ml-kemandml-dsato run in constant time, withctutils. Whether the compiled.wasmkeeps that is not verified (see SECURITY.md, Known Limitations).npm run test:timingfinds no timing difference between valid and invalid ciphertexts in decapsulation, or between seeds in signing, in Node 24. - Memory wiping: every heap block is zeroed before it is freed, and each operation then overwrites the part of the stack it used, so no secret key, seed or shared secret is left in the module's memory after a call.
tests/memory.tsscans the memory after every operation. Each secret leaves the module's memory once, as theUint8Arrayreturned to the caller, and the package keeps no other copy (tests/core.ts): that array is the one to overwrite withfill(0)when done. This keeps the copies down but is not a full guarantee: the JavaScript engine's garbage collector and JIT may copy the bytes where no code can reach them. In V8, for example, small arrays such as 32-byte secrets live inside the JavaScript heap, which the garbage collector moves. - FIPS 203 input checks:
encapsulateruns the §7.2 modulus check;decapsulateruns the §7.3 hash check, then the §7.2 check on the public key inside the secret key.ml-kem0.3 runs the same checks; this package runs its own first, so the errors do not depend on the crate. - No audit: Neither RustCrypto nor this wrapper have been independently audited.
- CVE-2026-22705: Timing side-channel in
ml-dsaDecompose function, patched in>= 0.1.0-rc.3(Barrett reduction). - CVE-2026-24850: Signature malleability via duplicate hint indices in
ml-dsa, patched in>= 0.1.0-rc.4. - GHSA-h37v-hp6w-2pp8: Off-by-two in
ml-dsause_hintwhenr0 = 0(FIPS 204 Alg. 40 deviation), patched in>= 0.1.0-rc.5. - RUSTSEC-2026-0385: Non-constant-time mask generation and
u128comparisons inctutils, whichml-dsauses for constant-time selection, patched inctutils >= 0.4.4(this package: 0.3.1). - Cargo.toml requires
ml-dsa >= 0.1.2, the first release that pulls a patchedctutils. - Cargo.toml requires
ml-kem >= 0.3.4, which removed a secret-indexed table lookup from noise sampling and made the conditional subtraction mod q constant-time. - Hedged signing:
signuses FIPS 204's default, hedged variant: each signature mixes in 32 fresh bytes fromcrypto.getRandomValues, so two signatures of the same message differ. That keeps a fault or a side channel from being lined up across repeated signings. With those bytes pinned to zero it gives the deterministic variant, which the tests use to compare against known-answer vectors. If no randomness is available,signthrows instead of signing. - Supply chain:
Cargo.lockcommitted, CI runscargo auditbefore every build.
License
MIT
