/** * Key agreement: X25519, and ECDH on P-256, P-384 and P-521. * * The {@link KeyAgreement} service generates key pairs, derives a shared * secret from a private key and a peer's public key, 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. * * Keys carry their algorithm in their type, so deriving a secret from keys on * different curves is a type error. The shared secret is plain bytes: pass it * through `Kdf` before using it as a key. * * Keys are opaque. They hold either bytes or a `CryptoKey`, display only their * algorithm, and compare by reference. The bytes are: * * - X25519: the 32-byte private key and the 32-byte public key; * - ECDH: PKCS#8 for private keys, and the uncompressed SEC1 point for public * keys. * * Only X25519 private keys and X25519 and ECDH public keys have a raw * constructor. ECDH private keys come from * `KeyAgreement.generateKeyPair`, `CryptoKey`s, or key formats. * * **Gotchas** * * - X25519 needs Chrome 133, Firefox 130, or Safari 17. There is no fallback * for older browsers. * - X25519 private keys are generated from `SecureRandom` bytes, so a seeded * `SecureRandom` reproduces them. ECDH key generation uses WebCrypto's own * randomness and cannot be seeded. * * @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 SecureRandom from "./SecureRandom.ts"; import * as WebCrypto from "./WebCrypto.ts"; declare const PrivateKeyTypeId: "~effect/crypto/KeyAgreement/PrivateKey"; declare const PublicKeyTypeId: "~effect/crypto/KeyAgreement/PublicKey"; /** * An elliptic curve for ECDH. * * @stability unstable * @category models * @since 4.0.0 */ export type Curve = "P-256" | "P-384" | "P-521"; /** * X25519 (RFC 7748), with 32-byte shared secrets. * * @stability unstable * @category models * @since 4.0.0 */ export interface X25519 { readonly _tag: "X25519"; readonly name: "x25519"; } /** * ECDH on a NIST curve, with shared secrets of the curve's coordinate size. * * @stability unstable * @category models * @since 4.0.0 */ export interface Ecdh { readonly _tag: "Ecdh"; readonly name: { readonly "P-256": "ecdhP256"; readonly "P-384": "ecdhP384"; readonly "P-521": "ecdhP521"; }[C]; readonly namedCurve: C; } /** * A key agreement algorithm, such as {@link x25519} or {@link ecdhP256}. * * @stability unstable * @category models * @since 4.0.0 */ export type Algorithm = X25519 | Ecdh; /** * X25519 (RFC 7748). * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const x25519: X25519; /** * ECDH on P-256, with 32-byte shared secrets. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const ecdhP256: Ecdh<"P-256">; /** * ECDH on P-384, with 48-byte shared secrets. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const ecdhP384: Ecdh<"P-384">; /** * ECDH on P-521, with 66-byte shared secrets. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const ecdhP521: Ecdh<"P-521">; /** * An opaque key agreement private 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 key agreement public 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 KeyAgreement_base: Context.ServiceClass(algorithm: A) => Effect.Effect, CryptoError.CryptoError>; /** * Computes the secret shared by a private key and a peer's public key on * the same curve. A peer key that is not on the curve, or a low-order * X25519 key that gives an all-zero secret, fails with an `InvalidKey` * reason. */ readonly deriveSharedSecret: (privateKey: PrivateKey, publicKey: PublicKey) => 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, derives shared secrets, and computes * public keys. * * **Example** (Agreeing on a secret with X25519) * * ```ts import.meta.vitest * import { Effect, Layer } from "effect" * import { KeyAgreement, SecureRandom } from "effect/crypto" * * const program = Effect.gen(function*() { * const keyAgreement = yield* KeyAgreement.KeyAgreement * const alice = yield* keyAgreement.generateKeyPair(KeyAgreement.x25519) * const bob = yield* keyAgreement.generateKeyPair(KeyAgreement.x25519) * const aliceSecret = yield* keyAgreement.deriveSharedSecret(alice.privateKey, bob.publicKey) * const bobSecret = yield* keyAgreement.deriveSharedSecret(bob.privateKey, alice.publicKey) * return aliceSecret.every((byte, i) => byte === bobSecret[i]) * }) * * const layer = KeyAgreement.layerWebCrypto.pipe(Layer.provide(SecureRandom.layerWebCrypto)) * await Effect.runPromise(program.pipe(Effect.provide(layer))) // => true * ``` * * @stability unstable * @category services * @since 4.0.0 */ export declare class KeyAgreement extends KeyAgreement_base { } /** * Layer that provides `KeyAgreement` 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`. X25519 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 X25519 private key from its 32 bytes. * * **Details** * * The bytes are copied. Another length fails with an `InvalidKey` reason, * including the 64-byte `private key‖public key` form libsodium uses: pass * its first 32 bytes. ECDH 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 X25519, or an * uncompressed SEC1 point for ECDH. * * **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. * * @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 and * curve, with the `deriveBits` 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 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 and * curve. Public keys need no usages. 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 raw key for X25519, and PKCS#8 or the * uncompressed point for ECDH. 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=KeyAgreement.d.ts.map