- Added a common strict DER using BoringSSL's CBS parsers, used by RSA,
EC and OKP (Ed25519/X25519) import
- HKDF/PBKDF2 `extractable: true` on import is a SyntaxError
- JwkSecret is now lexicographic sorted
- generateKey HMAC rejects lengths over 2^16 bits
generateKey and importKey checked Ed25519's usages and then returned
NotSupportedError. The new crypto/OKP.zig implements it: generate with
ED25519_keypair; import and export raw, spki and pkcs8 (RFC 8410) and
jwk (RFC 8037: kty OKP, crv, alg Ed25519 or EdDSA, x matching d); and
pure Ed25519 sign and verify. As the spec asks, verify is false for a
public key or R that is an invalid point or a small-order element,
which libcrypto doesn't check.
The usage-set and JWK member helpers shared with EC and RSA move to
common.zig.
ECDSA and ECDH keys could be generated and imported from spki / pkcs8,
but not exported in any format, nor imported as raw or jwk, and an
imported key's curve wasn't checked against the requested namedCurve.
Export: spki and pkcs8 through the DER helper RSA used (moved to
common.exportDer), raw as the uncompressed public point (0x04 || x ||
y, public keys only), and jwk with crv, x, y and, for a private key, d,
each of the curve's size, in lexicographic order and without alg, as
browsers return it.
Import: raw takes an uncompressed or compressed SEC 1 point; jwk follows
the "jwk" steps of ECDSA's and ECDH's "import key": kty "EC", use "sig"
(ECDSA) or "enc" (ECDH) when usages are requested, key_ops, ext, crv
equal to the requested curve and, for ECDSA, an alg naming it (ES256,
ES384, ES512); x, y and d must be of the curve's size. For every format,
a point off the curve, a d that doesn't match x and y, or a key on
another curve than namedCurve is DataError.
The curves' facts (name, NID, size, JWK alg) move into one table.
sign and verify handle ECDSA keys (P-256, P-384, P-521). The data is
hashed with the hash of the operation's EcdsaParams, not the key's, and
signed with ECDSA_do_sign; the signature is r followed by s, each a
big-endian integer of the curve order's size (64, 96 and 132 bytes), the
IEEE P1363 form WebCrypto uses rather than DER. verify takes that form
back: a signature of another length, such as one with extra padding, or
one that doesn't match is false, and the error it leaves on the
libcrypto queue is cleared. The sizes come from the key's own group,
not its namedCurve.
EcdsaParams' hash is read like RSA-PSS's saltLength: kept as a js.Value
in the Sign dictionary and converted only when the algorithm is ECDSA,
so other algorithms ignore a stray one. A missing hash, or an object
hash without a name, is a TypeError, and an unknown hash
NotSupportedError, both before the key checks. pssSaltLength becomes
signParams, which reads either member.
The list of hashes and canonicalHash move from RSA.zig to common.zig,
for EC.zig to use too.
RSA-PSS joins RSA-OAEP and RSASSA-PKCS1-v1_5 as a Scheme: sign / verify
usages, JWK use "sig" and alg PS1, PS256, PS384, PS512. Generation and
spki/pkcs8/jwk import and export go through the shared code.
sign and verify read RsaPssParams' saltLength, a required [EnforceRange]
unsigned long, when the algorithm is RSA-PSS (TypeError otherwise, before
the key checks); other algorithms ignore the member. The signature uses
PSS padding with the key's hash for the message and for MGF1, and that
salt length. A salt too long for the key is OperationError when signing
and false when verifying. Lengths past c_int are capped, since negative
ones are libcrypto's special values.
RSA.zig now serves RSA-OAEP and RSASSA-PKCS1-v1_5. What differs between
them is a Scheme: the usages of each half of a pair (sign / verify for
RSASSA-PKCS1-v1_5), the JWK `use` ("sig") and the JWK `alg` of each hash
(RS1, RS256, RS384, RS512). Generation, spki/pkcs8/jwk import and export
go through it, so they follow the RSASSA-PKCS1-v1_5 "import key" steps
the same way RSA-OAEP's do.
sign and verify handle RSA keys: the algorithm must be one that signs
(else NotSupportedError), the key's own, and the key must have the
usage (else InvalidAccessError); then a private key signs and a public
key verifies, with the key's hash and PKCS #1 v1.5 padding
(EVP_DigestSign / EVP_DigestVerify). A signature that doesn't match,
whatever its length, resolves to false; the error it leaves on the
libcrypto queue is cleared.
RSA-OAEP encrypt/decrypt and wrapKey/unwrapKey now check the key's
algorithm name too, so an RSASSA-PKCS1-v1_5 key can't encrypt or wrap.
RSA-PSS is still NotSupportedError.
wrapKey exports the key (a JWK as the UTF-8 of JSON.stringify through
V8) and encrypts it with RSA-OAEP or AES-CBC/CTR/GCM, or wraps it with
AES-KW (RFC 3394, BoringSSL's AES_wrap_key). unwrapKey decrypts or
unwraps, parses a JWK with V8's JSON.parse and the importKey binding
conversion, and imports the result. The wrapping key's algorithm and
wrapKey/unwrapKey usage are checked first, as in the spec.
exportKey and encrypt/decrypt are split into exportAs / cryptBytes and
the promise resolution; the usage bit becomes a parameter of AES.crypt
and RSA.crypt (canEncrypt/canDecrypt go away).
The "raw" export no longer hands out the private half of an asymmetric
key (InvalidAccessError) or an empty buffer for an EC public key
(NotSupportedError until the raw point is implemented), which wrapKey
would otherwise wrap.
exportKey: spki for public keys, pkcs8 for private keys (the other
half's format is InvalidAccessError), and jwk with the public members
plus d, p, q, dp, dq, qi for a private key, in the lexicographic order
browsers return a JsonWebKey in.
importKey("jwk") follows the RSA-OAEP "import key" steps: kty, use,
key_ops, ext and alg are checked against the requested key, members must
be minimal Base64urlUInts, and the key is built with RSA_new_public_key /
RSA_new_private_key so an inconsistent key is DataError.
KeyData.Jwk: kty is optional (a missing kty is the algorithm's
DataError, not a binding TypeError) and key_ops is read as a sequence.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rejects modulusLength > 4096 (8192 takes 5-10 seconds on my mid-upper CPU...if
anyone asks for it, maybe it becomes a runtime flag)
cryptOp is driven by the requested algorithm rather than the key type
RSA crypt label support
Builds on the existing RSA parameter validation (RSA.validate and the
generateKey-rsa-validation test) and implements RSA-OAEP for real, backed
by the vendored BoringSSL:
- generateKey produces an RSA key pair (modulusLength + publicExponent)
- importKey accepts spki (public) and pkcs8 (private) DER, and rejects an
unsupported OAEP hash at import time
- encrypt / decrypt go through EVP_PKEY with OAEP padding, using the
digest stored on the key
- CryptoKey.algorithm reports modulusLength for RSA keys
RSASSA-PKCS1-v1_5 and RSA-PSS still pass validation but remain
NotSupported for key generation. The key material lives as an EVP_PKEY in
CryptoKey, mirroring the EC.zig asymmetric-key pattern.
Adds two tests to crypto.html: a generate/encrypt/decrypt round-trip and
an import (spki + pkcs8) round-trip. `make test` and `zig fmt --check` pass.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
I threw Claude at WPT's /WebCryptoAPI/ which represents a significant number of
failing cases.
There were a few very low-hanging fruit, like enabling CryptoKey on Worker and
supporting `{name: string}` in addition to just `string` for some functions.
Some of these "fixes" just handle the error case / validation, which tends to
represent a greater number of WPT cases, e.g. some of the importKey algorithms
aren't fully supported, but they still validate the input and return the correct
errors.
To that end, while there should be considerably more passing cases (~42K), some
of these changes merely unlocked more failing cases, so our total case count
should go up.
Claude's summary:
1. digest {name} object fix (16→80)
2. Symmetric importKey/exportKey/generateKey (AES, HMAC; raw + jwk) + CryptoKey
accessors + DataError
3. EC/OKP importKey usage validation (failure-path)
4. PBKDF2 + HKDF deriveBits, then deriveKey
5. ECDH generateKey + import (spki/pkcs8) + deriveBits/deriveKey
6. AES encrypt/decrypt (CBC/CTR/GCM)
I've been thinking the implementation here is messy (ever since we added support for it) and thought it would be better to separate each algorithm to their respective files in order to maintain in a long run. `digest` is also refactored to prefer libcrypto instead of std.