gossamer/crypto
Cross-runtime bindings for the Web Cryptography API. The top-level
Crypto
interface (random_uuid) lives here, along with types shared across
gossamer/crypto/key, gossamer/crypto/subtle, and
gossamer/crypto/jwk. For cryptographically strong random bytes,
use gleam_crypto.strong_random_bytes.
Types
AES cipher modes supported by subtle.
pub type AesAlgorithm {
Cbc
Ctr
Gcm
Kw
}
Constructors
-
CbcAES with Cipher Block Chaining — symmetric encryption.
-
CtrAES with Counter mode — symmetric encryption.
-
GcmAES with Galois/Counter Mode — authenticated symmetric encryption.
-
KwAES Key Wrap — symmetric encryption of another key.
Errors raised by subtle operations.
pub type CryptoError {
KeyUsageMismatch(usage: KeyUsage)
KeyNotExtractable
AlgorithmNotSupported
InvalidAccess
OperationFailed
DataMalformed
QuotaExceeded
InvalidSyntax
}
Constructors
-
KeyUsageMismatch(usage: KeyUsage)The key’s
usagesdon’t include the capability required for this operation (e.g., callingencryptwith a key whose usages don’t includeEncrypt). Theusagepayload names the missing capability. -
KeyNotExtractableThe key’s
extractableflag isFalse, but the operation (export_key,export_key_jwk,wrap_key,wrap_key_jwk) requires it. -
AlgorithmNotSupportedThe runtime does not support the requested algorithm or algorithm/operation combination. Corresponds to the
NotSupportedErrorDOMException. -
InvalidAccessThe key is not appropriate for the requested operation (e.g., using an asymmetric key in a symmetric operation). Corresponds to the
InvalidAccessErrorDOMException. -
OperationFailedThe cryptographic operation failed for an operation-specific reason — authenticated decryption failed, signature verification produced an invalid result, etc. Corresponds to the
OperationErrorDOMException. -
DataMalformedThe input data is malformed for the operation (e.g., an imported key has the wrong structure). Corresponds to the
DataErrorDOMException. -
QuotaExceededThe input exceeds runtime-imposed size limits. Corresponds to the
QuotaExceededErrorDOMException. -
InvalidSyntaxA required parameter is missing or invalid for the algorithm (e.g., an empty
usageslist when generating or importing a symmetric key, or an out-of-range numeric parameter). Corresponds to theSyntaxErrorDOMException and theTypeErrorrejections from invalid numeric parameters.
Elliptic curve algorithms supported by subtle.
pub type EcAlgorithm {
Dh
Dsa
}
Constructors
-
DhElliptic Curve Diffie-Hellman — key agreement.
-
DsaElliptic Curve Digital Signature Algorithm — signing.
Cryptographic hash algorithms supported by subtle.
pub type HashAlgorithm {
Sha1
Sha256
Sha384
Sha512
}
Constructors
-
Sha1SHA-1 — included for legacy compatibility; avoid for new signatures.
-
Sha256SHA-2 with 256-bit output.
-
Sha384SHA-2 with 384-bit output.
-
Sha512SHA-2 with 512-bit output.
The algorithm and parameters bound to a CryptoKey, exposed
via the algorithm field on
crypto/key.Info.
pub type KeyAlgorithm {
Aes(name: AesAlgorithm, length: Int)
Ec(name: EcAlgorithm, named_curve: NamedCurve)
Ed25519
Hkdf
Hmac(hash: HashAlgorithm, length: Int)
Pbkdf2
Rsa(
name: RsaAlgorithm,
modulus_length: Int,
public_exponent: BitArray,
hash: HashAlgorithm,
)
X25519
}
Constructors
-
Aes(name: AesAlgorithm, length: Int)An AES key.
lengthis the key size in bits (128, 192, or 256). -
Ec(name: EcAlgorithm, named_curve: NamedCurve)An elliptic-curve key bound to
named_curve. -
Ed25519An Ed25519 signing key.
-
HkdfAn HKDF base key for key derivation.
-
Hmac(hash: HashAlgorithm, length: Int)An HMAC key bound to
hashwithlengthbits of key material. -
Pbkdf2A PBKDF2 base key for password-based derivation.
-
Rsa( name: RsaAlgorithm, modulus_length: Int, public_exponent: BitArray, hash: HashAlgorithm, )An RSA key with
modulus_lengthbits and the givenhash.public_exponentis the raw bytes of the exponent (commonly<<1, 0, 1>>for 65537). -
X25519An X25519 key-agreement key.
Whether a CryptoKey is public, private, or secret (symmetric).
pub type KeyKind {
Private
Public
Secret
}
Constructors
-
PrivateThe private half of an asymmetric key pair.
-
PublicThe public half of an asymmetric key pair.
-
SecretA symmetric key (e.g., AES, HMAC).
An allowed use for a CryptoKey. A key can only be used with
operations matching one of its declared usages.
pub type KeyUsage {
Decrypt
DeriveBits
DeriveKey
Encrypt
Sign
UnwrapKey
Verify
WrapKey
}
Constructors
-
DecryptThe key may be used to decrypt ciphertext.
-
DeriveBitsThe key may be used to derive raw byte material.
-
DeriveKeyThe key may be used to derive another
CryptoKey. -
EncryptThe key may be used to encrypt plaintext.
-
SignThe key may be used to produce a signature.
-
UnwrapKeyThe key may be used to decrypt a wrapped key.
-
VerifyThe key may be used to verify a signature.
-
WrapKeyThe key may be used to encrypt another key for transport.
A named elliptic curve used by ECDH and ECDSA operations in
subtle.
pub type NamedCurve {
P256
P384
P521
}
Constructors
-
P256NIST P-256 (secp256r1).
-
P384NIST P-384 (secp384r1).
-
P521NIST P-521 (secp521r1).
RSA algorithms supported by subtle.
pub type RsaAlgorithm {
Oaep
Pss
SsaPkcs1V15
}
Constructors
-
OaepRSA with Optimal Asymmetric Encryption Padding — asymmetric encryption.
-
PssRSA with Probabilistic Signature Scheme — asymmetric signing.
-
SsaPkcs1V15RSA with PKCS #1 v1.5 padding — legacy asymmetric signing.