Skip to content

feat(crypto): add the Web Cryptography module - #44

Merged
butterflyfish merged 4 commits into
masterfrom
feat/webcrypto
Sep 11, 2026
Merged

butterflyfish merged 4 commits into
masterfrom
feat/webcrypto

Conversation

@butterflyfish

Copy link
Copy Markdown
Contributor

What

modules/rong_crypto installs globalThis.crypto: getRandomValues, randomUUID, and a subtle covering what a worker actually reaches for.

digest SHA-1, SHA-256, SHA-384, SHA-512
sign / verify HMAC with any of the four hashes
encrypt / decrypt AES-GCM (96-bit IV, 128-bit tag), AES-CBC (PKCS#7)
generateKey HMAC, AES-GCM, AES-CBC
importKey / exportKey raw and jwk (kty: "oct")
deriveBits / deriveKey PBKDF2, HKDF

Enabled through rong_modules as the crypto feature, and in all.

Naming

The JavaScript surface is the spec's, down to importKey and getRandomValues, so what a developer already knows about crypto.subtle transfers unchanged and MDN is the documentation. Errors carry the DOMException names the spec observes — NotSupportedError, OperationError, InvalidAccessError, DataError — alongside Rong's machine-readable codes.

The TypeScript profile follows the same rule: AlgorithmIdentifier and BufferSource are the names every Web API signature uses, and the rest of logic-web.d.ts already declares TextEncoder, URL and DOMException unprefixed.

What is not here

Asymmetric algorithms (RSASSA-PKCS1-v1_5, RSA-PSS, RSA-OAEP, ECDSA, ECDH, Ed25519, X25519), AES-CTR, AES-KW, and key wrapping. They are recognized by name and rejected with a NotSupportedError naming the algorithm, so a caller learns what is missing rather than watching a lookup fail.

Tests

Every algorithm is checked against published RFC and NIST vectors — RFC 2202, RFC 4231, RFC 5869, RFC 6070, RFC 7914, and the McGrew-Viega AES-GCM vectors — both in Rust and from JavaScript through the same API a worker sees.

cargo test -p rong_crypto --features quickjs — 18 passed, including the JavaScript conformance suite. tsc on the type profile is clean.

Note

cargo clippy -p rong_modules with no module feature selected fails on a pre-existing dead_code lint for ModuleDescriptor::new. It reproduces on master without this branch and is left alone here.

`globalThis.crypto` with `getRandomValues`, `randomUUID` and a `subtle` that
covers what a worker actually reaches for: SHA-1 through SHA-512 digests, HMAC
sign and verify, AES-GCM and AES-CBC, PBKDF2 and HKDF derivation, and `raw`
and `jwk` key import and export.

The names are the spec's, so what a developer already knows about
`crypto.subtle` transfers unchanged and MDN is the documentation. Errors carry
the `DOMException` names the spec observes -- `NotSupportedError`,
`OperationError`, `InvalidAccessError` -- alongside Rong's own machine-readable
codes.

Asymmetric algorithms, AES-CTR, AES-KW and key wrapping are absent. They are
recognized by name and rejected with a `NotSupportedError` that says which
algorithm it was, so a caller learns what is missing instead of watching a
lookup fail.

Every algorithm is checked from JavaScript against published RFC and NIST
vectors, through the same API a worker sees.
The original suite checked RFC/NIST vectors and happy-path round-trips,
so JWK `use`, HMAC import `length`, HKDF `info`, DataView, deriveBits(0),
and encrypt of a non-AES algorithm never ran.

Reject JWK members of the wrong type and a mismatched `use`, honor HMAC
import length, require HKDF info, accept DataView, return empty bits for
length 0, and throw NotSupportedError for encrypt/decrypt of algorithms
that do not support those operations. Cache CryptoKey.algorithm/usages,
run PBKDF2 off the isolate thread, and treat a non-96-bit AES-GCM IV as
unimplemented rather than malformed.
Typegen discovers JS-facing crates from `#[js_class]` and fails `--check`
when a new module is missing from the policy. Mark crypto as curated like
the other Web globals; the published surface stays in the logic-web profile.
@butterflyfish
butterflyfish merged commit 99b41d6 into master Sep 11, 2026
13 checks passed
@butterflyfish
butterflyfish deleted the feat/webcrypto branch September 11, 2026 10:58
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.

1 participant