import type { WasmSource } from '../wasm-source.js'; import { AuthenticationError } from '../errors.js'; export { AuthenticationError }; /** * Load and initialise the ChaCha20 WASM module from `source`. * Must be called before constructing any ChaCha20 class. * @param source WASM binary, gzip+base64 string, URL, ArrayBuffer, Uint8Array, * pre-compiled WebAssembly.Module, Response, or Promise */ export declare function chacha20Init(source: WasmSource): Promise; export type { WasmSource }; export { isInitialized } from '../init.js'; /** * Raw ChaCha20 stream cipher (RFC 8439 §2.4). * * Holds exclusive access to the `chacha20` WASM module from construction * until `dispose()`. Constructing a second ChaCha20 or any other chacha20 * user while this instance is live throws. Call `dispose()` when done. */ export declare class ChaCha20 { private readonly x; private _tok; constructor(); /** * Load key and nonce into WASM state and set the block counter to 1. * Must be called before each message (RFC 8439 §2.4). * @param key 32 bytes * @param nonce 12 bytes, must be unique per (key, message) */ beginEncrypt(key: Uint8Array, nonce: Uint8Array): void; /** * XOR `chunk` with the next keystream block(s). Counter advances automatically. * @param chunk Plaintext chunk, must not exceed WASM CHUNK_SIZE * @returns Ciphertext of the same length */ encryptChunk(chunk: Uint8Array): Uint8Array; /** * Alias for `beginEncrypt`, ChaCha20 is a stream cipher (symmetric). * @param key 32 bytes * @param nonce 12 bytes, must match the value used to encrypt */ beginDecrypt(key: Uint8Array, nonce: Uint8Array): void; /** * Alias for `encryptChunk`, ChaCha20 is a stream cipher (symmetric). * @param chunk Ciphertext chunk * @returns Plaintext of the same length */ decryptChunk(chunk: Uint8Array): Uint8Array; /** Wipe WASM state and release exclusive module access. Idempotent. */ dispose(): void; } /** * Poly1305 one-time MAC (RFC 8439 §2.5). * * Atomic (stateless) class: each `mac()` call is independent. * The key must never be reused across messages. For most use cases, prefer * `ChaCha20Poly1305` or `XChaCha20Poly1305` which manage the one-time key * automatically. */ export declare class Poly1305 { private readonly x; constructor(); /** * Compute a 16-byte Poly1305 MAC for `msg` using `key`. * @param key 32-byte one-time key, must not be reused across messages * @param msg Message to authenticate * @returns 16-byte Poly1305 tag */ mac(key: Uint8Array, msg: Uint8Array): Uint8Array; /** Wipe WASM MAC state. */ dispose(): void; } /** * ChaCha20-Poly1305 AEAD (RFC 8439 §2.8). * * `encrypt()` returns ciphertext || tag(16) as a single Uint8Array. * `decrypt()` accepts the same combined format and splits internally. * * Single-use encrypt guard: `encrypt()` may only be called once per instance. * Create a new instance for each encryption to prevent nonce reuse. * * `decrypt()` uses constant-time tag comparison, XOR-accumulate pattern, * no early return on mismatch. Plaintext is never returned on failure. */ export declare class ChaCha20Poly1305 { private readonly x; private _used; constructor(); /** * Encrypt and authenticate `plaintext` with ChaCha20-Poly1305 (RFC 8439 §2.8). * * **Single-use guard:** `encrypt()` may only be called once per instance. * Any throw, including validation errors, permanently locks this instance. * Always create a new `ChaCha20Poly1305` per message. * @param key 32 bytes * @param nonce 12 bytes, must be unique per (key, message) * @param plaintext Data to encrypt * @param aad Additional authenticated data (optional) * @returns Ciphertext || 16-byte Poly1305 tag */ encrypt(key: Uint8Array, nonce: Uint8Array, plaintext: Uint8Array, aad?: Uint8Array): Uint8Array; /** * Verify and decrypt a ChaCha20-Poly1305 ciphertext (RFC 8439 §2.8). * Throws `AuthenticationError` if the tag does not match. * @param key 32 bytes * @param nonce 12 bytes, must match the value used to encrypt * @param ciphertext Ciphertext || 16-byte tag (combined format from `encrypt`) * @param aad Additional authenticated data (optional) * @returns Plaintext */ decrypt(key: Uint8Array, nonce: Uint8Array, ciphertext: Uint8Array, // ciphertext || tag(16) combined aad?: Uint8Array): Uint8Array; /** Wipe WASM cipher and MAC state. */ dispose(): void; } /** * XChaCha20-Poly1305 AEAD (IETF draft-irtf-cfrg-xchacha). * * Recommended authenticated encryption primitive for most use cases. * Uses a 24-byte nonce, safe for random generation via crypto.getRandomValues. * * Single-use encrypt guard: `encrypt()` may only be called once per instance. * Create a new instance for each encryption to prevent nonce reuse. * * `decrypt()` constant-time guarantee is inherited from the inner AEAD path. */ export declare class XChaCha20Poly1305 { private readonly x; private _used; constructor(); /** * Encrypt and authenticate `plaintext` with XChaCha20-Poly1305 * (draft-irtf-cfrg-xchacha). Recommended for general-purpose AEAD. * * **Single-use guard:** `encrypt()` may only be called once per instance. * Any throw, including validation errors, permanently locks this instance. * Always create a new `XChaCha20Poly1305` per message to prevent nonce reuse. * @param key 32 bytes * @param nonce 24 bytes, safe to generate randomly via `randomBytes(24)` * @param plaintext Data to encrypt * @param aad Additional authenticated data (optional) * @returns Ciphertext || 16-byte Poly1305 tag */ encrypt(key: Uint8Array, nonce: Uint8Array, plaintext: Uint8Array, aad?: Uint8Array): Uint8Array; /** * Verify and decrypt an XChaCha20-Poly1305 ciphertext. * Throws `AuthenticationError` if the tag does not match. * @param key 32 bytes * @param nonce 24 bytes, must match the value used to encrypt * @param ciphertext Ciphertext || 16-byte tag (combined format from `encrypt`) * @param aad Additional authenticated data (optional) * @returns Plaintext */ decrypt(key: Uint8Array, nonce: Uint8Array, ciphertext: Uint8Array, aad?: Uint8Array): Uint8Array; /** Wipe WASM cipher and MAC state. */ dispose(): void; } export { XChaCha20Cipher } from './cipher-suite.js'; export { ChaCha20Generator } from './generator.js';