/** * Digital signatures: Ed25519, ECDSA on P-256, P-384 and P-521, RSA-PSS, and * RSASSA-PKCS1-v1_5. * * The {@link Signature} service generates key pairs, signs, verifies, and * computes a private key's public key. The top-level {@link privateKey}, * {@link publicKey}, {@link privateKeyFromCryptoKey}, * {@link publicKeyFromCryptoKey}, and {@link toBytes} functions build and * export keys without a service. * * The hash is part of the algorithm. ECDSA P-256, P-384 and P-521 sign with * SHA-256, SHA-384 and SHA-512, as JOSE ES256, ES384 and ES512 do. RSA * algorithms come from {@link rsaPss} and {@link rsaPkcs1v15}, with pre-made * values such as {@link rsaPssSha256}. * * Keys are opaque. They hold either bytes or a `CryptoKey`, display only their * algorithm, and compare by reference. The bytes are: * * - Ed25519: the 32-byte seed and the 32-byte public key; * - ECDSA: PKCS#8 for private keys, and the uncompressed SEC1 point for public * keys; * - RSA: PKCS#8 and SPKI DER. * * Only Ed25519 private keys and Ed25519 and ECDSA public keys have a raw * constructor. Other keys come from `Signature.generateKeyPair`, * `CryptoKey`s, or key formats. * * **Gotchas** * * - Ed25519 needs Chrome 137, Firefox 129, or Safari 17. There is no * fallback for older browsers. * - Ed25519 keys are generated from `SecureRandom` bytes, so a seeded * `SecureRandom` reproduces them. ECDSA and RSA key generation, and ECDSA * and RSA-PSS signatures, use WebCrypto's own randomness and cannot be * seeded. Test them with round trips such as `verify(sign(data))`. * * @stability unstable * @since 4.0.0 */ import * as Context from "../Context.ts"; import * as Effect from "../Effect.ts"; import type { Inspectable } from "../Inspectable.ts"; import * as Layer from "../Layer.ts"; import * as CryptoError from "./CryptoError.ts"; import * as Digest from "./Digest.ts"; import * as SecureRandom from "./SecureRandom.ts"; import * as WebCrypto from "./WebCrypto.ts"; declare const PrivateKeyTypeId: "~effect/crypto/Signature/PrivateKey"; declare const PublicKeyTypeId: "~effect/crypto/Signature/PublicKey"; /** * An elliptic curve for ECDSA. * * @stability unstable * @category models * @since 4.0.0 */ export type Curve = "P-256" | "P-384" | "P-521"; /** * Ed25519 (RFC 8032), with 64-byte signatures. * * @stability unstable * @category models * @since 4.0.0 */ export interface Ed25519 { readonly _tag: "Ed25519"; readonly name: "ed25519"; } /** * ECDSA on a NIST curve, with the curve's hash and raw `r‖s` signatures. * * @stability unstable * @category models * @since 4.0.0 */ export interface Ecdsa { readonly _tag: "Ecdsa"; readonly name: { readonly "P-256": "ecdsaP256"; readonly "P-384": "ecdsaP384"; readonly "P-521": "ecdsaP521"; }[C]; readonly namedCurve: C; readonly hash: Digest.Algorithm; } /** * RSA-PSS (RFC 8017), from {@link rsaPss}. * * **Details** * * `modulusLength` is in bits and is only read by key generation: an imported * key's `algorithm.modulusLength` is its real size. `saltLength` is in bytes. * * @stability unstable * @category models * @since 4.0.0 */ export interface RsaPss { readonly _tag: "RsaPss"; readonly name: string; readonly hash: Digest.Algorithm; readonly modulusLength: number; readonly saltLength: number; } /** * RSASSA-PKCS1-v1_5 (RFC 8017), from {@link rsaPkcs1v15}. * * **Details** * * `modulusLength` is in bits and is only read by key generation: an imported * key's `algorithm.modulusLength` is its real size. * * @stability unstable * @category models * @since 4.0.0 */ export interface RsaPkcs1v15 { readonly _tag: "RsaPkcs1v15"; readonly name: string; readonly hash: Digest.Algorithm; readonly modulusLength: number; } /** * A signature algorithm, such as {@link ed25519} or {@link ecdsaP256}. * * @stability unstable * @category models * @since 4.0.0 */ export type Algorithm = Ed25519 | Ecdsa | RsaPss | RsaPkcs1v15; /** * Ed25519 (RFC 8032). * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const ed25519: Ed25519; /** * ECDSA on P-256 with SHA-256 (JOSE ES256), with 64-byte signatures. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const ecdsaP256: Ecdsa<"P-256">; /** * ECDSA on P-384 with SHA-384 (JOSE ES384), with 96-byte signatures. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const ecdsaP384: Ecdsa<"P-384">; /** * ECDSA on P-521 with SHA-512 (JOSE ES512), with 132-byte signatures. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const ecdsaP521: Ecdsa<"P-521">; /** * Builds an RSA-PSS algorithm for a hash and a modulus length in bits. * * **Details** * * MGF1 uses the same hash. The salt length defaults to the hash's output * length. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const rsaPss: (options: { readonly hash: Digest.Algorithm; readonly modulusLength: number; readonly saltLength?: number | undefined; }) => RsaPss; /** * Builds an RSASSA-PKCS1-v1_5 algorithm for a hash and a modulus length in * bits. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const rsaPkcs1v15: (options: { readonly hash: Digest.Algorithm; readonly modulusLength: number; }) => RsaPkcs1v15; /** * RSA-PSS with SHA-256 (JOSE PS256), generating 3072-bit keys. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const rsaPssSha256: RsaPss; /** * RSA-PSS with SHA-384 (JOSE PS384), generating 3072-bit keys. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const rsaPssSha384: RsaPss; /** * RSA-PSS with SHA-512 (JOSE PS512), generating 3072-bit keys. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const rsaPssSha512: RsaPss; /** * RSASSA-PKCS1-v1_5 with SHA-256 (JOSE RS256), generating 3072-bit keys. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const rsaPkcs1v15Sha256: RsaPkcs1v15; /** * RSASSA-PKCS1-v1_5 with SHA-384 (JOSE RS384), generating 3072-bit keys. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const rsaPkcs1v15Sha384: RsaPkcs1v15; /** * RSASSA-PKCS1-v1_5 with SHA-512 (JOSE RS512), generating 3072-bit keys. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const rsaPkcs1v15Sha512: RsaPkcs1v15; /** * An opaque signing key for the algorithm `A`. * * **Details** * * The key holds either bytes or a `CryptoKey` from * {@link privateKeyFromCryptoKey}. `toString`, `toJSON`, and `inspect` show * only the algorithm. Keys compare by reference and do not implement `Equal`. * * @stability unstable * @category models * @since 4.0.0 */ export interface PrivateKey extends Inspectable { readonly [PrivateKeyTypeId]: typeof PrivateKeyTypeId; readonly algorithm: A; } /** * An opaque verification key for the algorithm `A`. * * **Details** * * The key holds either bytes or a `CryptoKey` from * {@link publicKeyFromCryptoKey}. Keys compare by reference and do not * implement `Equal`. * * @stability unstable * @category models * @since 4.0.0 */ export interface PublicKey extends Inspectable { readonly [PublicKeyTypeId]: typeof PublicKeyTypeId; readonly algorithm: A; } /** * A private key and its public key. * * @stability unstable * @category models * @since 4.0.0 */ export interface KeyPair { readonly privateKey: PrivateKey; readonly publicKey: PublicKey; } /** * Checks whether a value is a {@link PrivateKey}. * * @stability unstable * @category guards * @since 4.0.0 */ export declare const isPrivateKey: (u: unknown) => u is PrivateKey; /** * Checks whether a value is a {@link PublicKey}. * * @stability unstable * @category guards * @since 4.0.0 */ export declare const isPublicKey: (u: unknown) => u is PublicKey; declare const Signature_base: Context.ServiceClass(algorithm: A) => Effect.Effect, CryptoError.CryptoError>; /** * Signs `data`. ECDSA signatures are raw `r‖s`, each padded to the curve's * coordinate size. */ readonly sign: (privateKey: PrivateKey, data: Uint8Array) => Effect.Effect; /** * Checks `signature` against `data`. Any problem with the signature bytes, * such as a wrong length or an out-of-range `r` or `s`, returns `false`. * Only problems with the key or the layer fail. */ readonly verify: (publicKey: PublicKey, signature: Uint8Array, data: Uint8Array) => Effect.Effect; /** * Computes the public key of a private key. A non-extractable `CryptoKey` * fails with a `NotExtractable` reason, since its public members cannot be * read. */ readonly publicKeyOf: (privateKey: PrivateKey) => Effect.Effect, CryptoError.CryptoError>; }>; /** * Service that generates key pairs, signs, verifies, and computes public keys. * * **Example** (Signing and verifying with Ed25519) * * ```ts import.meta.vitest * import { Effect, Layer } from "effect" * import { SecureRandom, Signature } from "effect/crypto" * * const program = Effect.gen(function*() { * const signature = yield* Signature.Signature * const { privateKey, publicKey } = yield* signature.generateKeyPair(Signature.ed25519) * const data = new TextEncoder().encode("message") * const signed = yield* signature.sign(privateKey, data) * return [signed.length, yield* signature.verify(publicKey, signed, data)] * }) * * const layer = Signature.layerWebCrypto.pipe(Layer.provide(SecureRandom.layerWebCrypto)) * await Effect.runPromise(program.pipe(Effect.provide(layer))) // => [64, true] * ``` * * @stability unstable * @category services * @since 4.0.0 */ export declare class Signature extends Signature_base { } /** * Layer that provides `Signature` on the `WebCrypto` reference. * * **Details** * * Every algorithm runs on `crypto.subtle`. The layer imports a byte-backed * key on first use and caches the `CryptoKey`. Ed25519 private keys come from * `SecureRandom`, so a seeded `SecureRandom` reproduces them. * * @stability unstable * @category layers * @since 4.0.0 */ export declare const layerWebCrypto: Layer.Layer; /** * Builds an Ed25519 private key from its 32-byte seed. * * **Details** * * The bytes are copied. Another length fails with an `InvalidKey` reason, * including the 64-byte `seed‖public key` form libsodium and TweetNaCl use: * pass its first 32 bytes. ECDSA and RSA private keys have no raw form, so * passing them is a type error. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const privateKey: (algorithm: A, bytes: Uint8Array) => Effect.Effect, CryptoError.CryptoError>; /** * Builds a public key from its raw form: 32 bytes for Ed25519, or an * uncompressed SEC1 point for ECDSA. * * **Details** * * The bytes are copied. A wrong length, or a compressed point, fails with an * `InvalidKey` reason. Only uncompressed points are taken because Safari and * Firefox before 146 reject compressed ones in raw import, though they accept * them inside SPKI: decode a compressed point with `KeyFormat.spki`. Whether a * point is on the curve is checked when the key is first used. RSA public keys have * no raw form, so passing them is a type error. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const publicKey: (algorithm: A, bytes: Uint8Array) => Effect.Effect, CryptoError.CryptoError>; /** * Builds a private key from an existing `CryptoKey`, such as a * non-extractable one. * * **Details** * * The `CryptoKey` must be a private key of the algorithm's WebCrypto name, * curve or hash, with the `sign` usage. Otherwise the constructor fails with * an `InvalidKey` reason. The modulus length of an RSA algorithm is not * checked. The key must come from the `Crypto` object of the `WebCrypto` * reference. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const privateKeyFromCryptoKey: (algorithm: A, cryptoKey: WebCrypto.CryptoKey) => Effect.Effect, CryptoError.CryptoError>; /** * Builds a public key from an existing `CryptoKey`. * * **Details** * * The `CryptoKey` must be a public key of the algorithm's WebCrypto name, * curve or hash, with the `verify` usage. Otherwise the constructor fails * with an `InvalidKey` reason. The key must come from the `Crypto` object of * the `WebCrypto` reference. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const publicKeyFromCryptoKey: (algorithm: A, cryptoKey: WebCrypto.CryptoKey) => Effect.Effect, CryptoError.CryptoError>; /** * Exports a key's stored bytes. * * **Details** * * Returns a fresh `Uint8Array`: the seed or raw public key for Ed25519, * PKCS#8 or the uncompressed point for ECDSA, and PKCS#8 or SPKI DER for * RSA. A `CryptoKey`-backed key is exported on the `WebCrypto` reference, and * fails with a `NotExtractable` reason when the `CryptoKey` is not * extractable. * * @stability unstable * @category converting * @since 4.0.0 */ export declare const toBytes: (key: PrivateKey | PublicKey) => Effect.Effect; export {}; //# sourceMappingURL=Signature.d.ts.map