/** * Unauthenticated encryption with no integrity: compose with `Mac`. * * AES-CTR and AES-CBC with PKCS#7 padding, for protocols that pair them with a * MAC, such as SSH, SRTP, JWE, CMS, and Matrix. Anyone can change the * ciphertext without being detected, so authenticate it with `Mac` * (encrypt-then-MAC), or use `Aead` instead. * * The {@link Cipher} service generates keys, encrypts, and decrypts. The * top-level {@link key}, {@link fromCryptoKey}, and {@link toBytes} functions * build and export keys without a service. Both modes run on `crypto.subtle`. * * Keys are opaque. They hold either key bytes or a `CryptoKey`, display only * their algorithm, and compare by reference. * * **Gotchas** * * - Never reuse an IV with the same key. For AES-CTR a repeated counter block * reveals the XOR of two plaintexts. For AES-CBC the IV must also be * unpredictable. * - AES-CTR decryption never fails, whatever the ciphertext. AES-CBC * decryption fails with one `DecryptionFailed` reason for both bad padding * and a bad length, so callers cannot tell them apart. Verify a MAC before * decrypting, or the error itself becomes a padding oracle. * * @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 TypeId: "~effect/crypto/Cipher/Key"; /** * An unauthenticated cipher algorithm, such as {@link aes256Cbc}. * * **Details** * * `mode` is the WebCrypto algorithm name and `keyLength` is in bytes. * * @stability unstable * @category models * @since 4.0.0 */ export interface Algorithm { readonly name: "aes128Ctr" | "aes256Ctr" | "aes128Cbc" | "aes256Cbc"; readonly mode: "AES-CTR" | "AES-CBC"; readonly keyLength: 16 | 32; } /** * AES-128-CTR, with a 16-byte key. * * **Details** * * The 16-byte `iv` is the initial counter block, and the whole 128-bit block * is the counter, as in OpenSSL, Node.js, Go's `cipher.NewCTR`, and noble. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const aes128Ctr: Algorithm; /** * AES-256-CTR, with a 32-byte key. * * **Details** * * The 16-byte `iv` is the initial counter block, and the whole 128-bit block * is the counter, as in OpenSSL, Node.js, Go's `cipher.NewCTR`, and noble. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const aes256Ctr: Algorithm; /** * AES-128-CBC with PKCS#7 padding, with a 16-byte key. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const aes128Cbc: Algorithm; /** * AES-256-CBC with PKCS#7 padding, with a 32-byte key. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const aes256Cbc: Algorithm; /** * An opaque unauthenticated cipher key for the algorithm `A`. * * **Details** * * The key holds either key bytes or a `CryptoKey` from {@link fromCryptoKey}. * `toString`, `toJSON`, and `inspect` show only the algorithm. Keys compare * by reference and do not implement `Equal`, because comparing key bytes is * not constant time. * * @stability unstable * @category models * @since 4.0.0 */ export interface Key extends Inspectable { readonly [TypeId]: typeof TypeId; readonly algorithm: A; } /** * Checks whether a value is a {@link Key}. * * @stability unstable * @category guards * @since 4.0.0 */ export declare const isKey: (u: unknown) => u is Key; declare const Cipher_base: Context.ServiceClass(algorithm: A) => Effect.Effect, CryptoError.CryptoError>; /** * Encrypts `plaintext`. AES-CTR output has the plaintext's length, and * AES-CBC output is padded to the next multiple of 16 bytes. * * `iv` is 16 bytes: the initial counter block for AES-CTR, and the * initialization vector for AES-CBC. */ readonly encrypt: (key: Key, plaintext: Uint8Array, options: { readonly iv: Uint8Array; }) => Effect.Effect; /** * Decrypts `ciphertext`. AES-CBC fails with a `DecryptionFailed` reason for * bad padding or a bad length; AES-CTR never fails on the ciphertext. */ readonly decrypt: (key: Key, ciphertext: Uint8Array, options: { readonly iv: Uint8Array; }) => Effect.Effect; }>; /** * Service that generates keys, encrypts, and decrypts without * authentication. * * **Example** (Encrypting with AES-CBC) * * ```ts import.meta.vitest * import { Effect, Layer } from "effect" * import { Cipher, SecureRandom } from "effect/crypto" * * const program = Effect.gen(function*() { * const cipher = yield* Cipher.Cipher * const random = yield* SecureRandom.SecureRandom * const key = yield* cipher.generateKey(Cipher.aes256Cbc) * const iv = yield* random.randomBytes(16) * const ciphertext = yield* cipher.encrypt(key, new TextEncoder().encode("secret"), { iv }) * const plaintext = yield* cipher.decrypt(key, ciphertext, { iv }) * return [ciphertext.length, new TextDecoder().decode(plaintext)] * }) * * const layer = Cipher.layerWebCrypto.pipe(Layer.provideMerge(SecureRandom.layerWebCrypto)) * await Effect.runPromise(program.pipe(Effect.provide(layer))) // => [16, "secret"] * ``` * * @stability unstable * @category services * @since 4.0.0 */ export declare class Cipher extends Cipher_base { } /** * Layer that provides `Cipher` on the `WebCrypto` reference. * * **Details** * * Encryption and decryption run on `crypto.subtle`. The layer imports a * byte-backed key on first use and caches the `CryptoKey`. AES-CTR uses the * whole 128-bit block as the counter (`length: 128`), which every runtime * supports. Generated keys come from `SecureRandom`, so a seeded * `SecureRandom` gives reproducible keys. * * @stability unstable * @category layers * @since 4.0.0 */ export declare const layerWebCrypto: Layer.Layer; /** * Builds a key from raw key bytes. * * **Details** * * Copies the bytes, including Node.js `Buffer` inputs. A length other than * the algorithm's key length fails with an `InvalidKey` reason. AES-192 is * not supported. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const key: (algorithm: A, bytes: Uint8Array) => Effect.Effect, CryptoError.CryptoError>; /** * Builds a key from an existing `CryptoKey`, such as a non-extractable one. * * **Details** * * The `CryptoKey` must be a secret key for the algorithm's mode (`AES-CTR` or * `AES-CBC`) and length, with both the `encrypt` and `decrypt` usages, since * a key reaches both operations. Otherwise the constructor fails with an * `InvalidKey` reason. It must come from the `Crypto` object of the * `WebCrypto` reference. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const fromCryptoKey: (algorithm: A, cryptoKey: WebCrypto.CryptoKey) => Effect.Effect, CryptoError.CryptoError>; /** * Exports a key's bytes. * * **Details** * * Returns a fresh `Uint8Array` that shares no memory with the key. 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: Key) => Effect.Effect; export {}; //# sourceMappingURL=Cipher.d.ts.map