/** * Abstract class representing a CBC mode cipher. * Implements the Cipher interface. */ export declare abstract class AbstractCbcCipher implements Cipher { /** * Encrypts the given plaintext using the specified encryption algorithm. * @param enc - The encryption algorithm to use. * @param plaintext - The plaintext data to encrypt. * @param cek - The content encryption key. * @param iv - The initialization vector. * @param aad - Additional authenticated data. * @returns A promise that resolves to the encryption result. * @throws Will throw an error if the encryption algorithm is invalid. */ encrypt: ({ enc, plaintext, cek, iv, aad, }: EncryptParams) => Promise; /** * Decrypts the given ciphertext using the specified decryption algorithm. * @param enc - The decryption algorithm to use. * @param cek - The content encryption key. * @param ciphertext - The ciphertext data to decrypt. * @param iv - The initialization vector. * @param tag - The authentication tag. * @param aad - Additional authenticated data. * @returns A promise that resolves to the decrypted data as a Uint8Array. */ decrypt: ({ enc, cek, ciphertext, iv, tag, aad, }: DecryptParams) => Promise; /** * Returns the byte length of the initialization vector (IV) required for CBC encryption. * The IV length required for CBC mode (AES-CBC) is always 16 bytes (128 bits) per the JWE specification. * @param enc - The encryption algorithm identifier (not used, included for interface compatibility). * @returns The IV byte length required for CBC mode (always 16). */ getIvByteLength: (_enc: Enc) => number; /** * Returns the required byte length of the content encryption key (CEK) for the given CBC encryption algorithm. * For CBC mode, the CEK is twice the key length since it's used for both encryption and MAC operations. * @param enc - The encryption algorithm identifier. * @returns The required CEK byte length for the specified CBC algorithm. */ getCekByteLength: (enc: Enc) => number; /** * Abstract method for the internal encryption process. * @param args - The arguments required for encryption. * @returns A promise that resolves to the encrypted data as a Uint8Array. */ encryptInternal: (_: CbcEncryptInternalParams) => Promise; /** * Abstract method for the internal decryption process. * @param args - The arguments required for decryption. * @returns A promise that resolves to the decrypted data as a Uint8Array. */ decryptInternal: (_: CbcDecryptInternalParams) => Promise; /** * Abstract method for generating a tag. * @param args - The arguments required for tag generation. * @returns A promise that resolves to the generated tag as a Uint8Array. */ generateTag: (_: GenerateTagParams) => Promise; } /** * Abstract class representing a GCM mode cipher. * Implements the Cipher interface. */ export declare abstract class AbstractGcmCipher implements Cipher { /** * Encrypts the given plaintext using the specified encryption algorithm. * @param enc - The encryption algorithm to use. * @param plaintext - The plaintext data to encrypt. * @param cek - The content encryption key. * @param iv - The initialization vector. * @param aad - Additional authenticated data. * @returns A promise that resolves to the encryption result. * @throws Will throw an error if the encryption algorithm is invalid. */ encrypt: ({ enc, plaintext, cek, iv, aad, }: EncryptParams) => Promise; /** * Decrypts the given ciphertext using the specified decryption algorithm. * @param enc - The decryption algorithm to use. * @param cek - The content encryption key. * @param ciphertext - The ciphertext data to decrypt. * @param iv - The initialization vector. * @param tag - The authentication tag. * @param aad - Additional authenticated data. * @returns A promise that resolves to the decrypted data as a Uint8Array. */ decrypt: ({ enc, cek, ciphertext, iv, tag, aad, }: DecryptParams) => Promise; /** * Returns the required byte length of the initialization vector (IV) for the specified GCM encryption algorithm. * Per the JWE specification, the IV for AES-GCM must always be 12 bytes (96 bits). * @param _enc - The encryption algorithm identifier (not used, included for interface compatibility). * @returns The IV byte length required for GCM mode (always 12). */ getIvByteLength: (_enc: Enc) => number; /** * Returns the required byte length of the content encryption key (CEK) for the given GCM encryption algorithm. * For GCM mode, the CEK length matches the key length. * @param enc - The encryption algorithm identifier. * @returns The required CEK byte length for the specified GCM algorithm. */ getCekByteLength: (enc: Enc) => number; /** * Abstract method for the internal encryption process. * @param args - The arguments required for encryption. * @returns A promise that resolves to the encrypted data as a Uint8Array. */ encryptInternal: (_: GcmEncryptInternalParams) => Promise; /** * Abstract method for the internal GCM decryption process. * @param args - The arguments required for GCM decryption. * @returns A promise that resolves to the decrypted data as a Uint8Array. */ decryptInternal: (_: GcmDecryptInternalParams) => Promise; } /** * A cipher implementation that supports both CBC and GCM modes of AES encryption. * Implements the Cipher interface. */ export declare class AesCipher implements Cipher { /** The CBC cipher instance */ private cbc; /** The GCM cipher instance */ private gcm; /** * Constructs an AesCipher instance. * @param params - The parameters required to construct the cipher */ constructor({ cbc, gcm }: AesCipherConstructorParams); /** * Encrypts data using either CBC or GCM mode based on the specified algorithm. * @param params - The encryption parameters * @returns A promise that resolves to the encryption result * @throws Will throw an error if the encryption algorithm is invalid */ encrypt: (params: EncryptParams) => Promise; /** * Decrypts data using either CBC or GCM mode based on the specified algorithm. * @param params - The decryption parameters * @returns A promise that resolves to the decrypted data * @throws Will throw an error if the decryption algorithm is invalid */ decrypt: (params: DecryptParams) => Promise; /** * Returns the required byte length of the initialization vector (IV) * for the specified encryption algorithm. * Delegates to CBC or GCM implementation depending on the mode. * * @param enc - The encryption algorithm identifier. * @returns The IV byte length required for the specified algorithm. * @throws Will throw an error if the encryption mode is invalid. */ getIvByteLength: (enc: Enc) => number; /** * Returns the required byte length of the content encryption key (CEK) * for the specified encryption algorithm. * Delegates to CBC or GCM implementation depending on the mode. * * @param enc - The encryption algorithm identifier. * @returns The CEK byte length required for the specified algorithm. * @throws Will throw an error if the encryption mode is invalid. */ getCekByteLength: (enc: Enc) => number; } /** * Parameters required to construct an AesCipher instance. * @property {AbstractCbcCipher} cbc - The CBC cipher instance. * @property {AbstractGcmCipher} gcm - The GCM cipher instance. */ export declare interface AesCipherConstructorParams { cbc: AbstractCbcCipher; gcm: AbstractGcmCipher; } /** * Parameters required for the internal CBC decryption process. */ export declare type CbcDecryptInternalParams = { /** The raw encryption key as a Uint8Array. */ encRawKey: Uint8Array; /** The initialization vector as a Uint8Array. */ iv: Uint8Array; /** The ciphertext data to be decrypted as a Uint8Array. */ ciphertext: Uint8Array; }; /** * Type representing CBC mode encryption algorithms with HMAC. */ export declare type CbcEnc = (typeof cbcEncArray)[number]; /** * Array of supported CBC mode encryption algorithms with HMAC. */ export declare const cbcEncArray: readonly ["A128CBC-HS256", "A192CBC-HS384", "A256CBC-HS512"]; /** * Parameters required for the internal CBC encryption process. */ export declare type CbcEncryptInternalParams = { /** The raw encryption key as a Uint8Array. */ encRawKey: Uint8Array; /** The initialization vector as a Uint8Array. */ iv: Uint8Array; /** The plaintext data to be encrypted as a Uint8Array. */ plaintext: Uint8Array; }; /** * Returns the required byte length of the content encryption key (CEK) for CBC encryption. * For CBC mode, the CEK is twice the key length since it's used for both encryption and MAC operations. * @param enc - The encryption algorithm identifier. * @returns The required CEK byte length for the specified CBC algorithm. */ export declare const cbcGetCekByteLength: (enc: Enc) => number; /** * Verifies the length of the content encryption key (CEK) for CBC encryption. * @param cek - The content encryption key as a Uint8Array. * @param keyBitLength - The expected length of the key in bits. * @throws Will throw an error if the length of the CEK is not as expected. */ export declare const cbcVerifyCekLength: (cek: Uint8Array, keyBitLength: number) => void; /** * Verifies the length of the initialization vector (IV) for CBC encryption. * @param iv - The initialization vector as a Uint8Array. * @throws Will throw an error if the length of the IV is not 16 bytes. */ export declare const cbcVerifyIvLength: (iv: Uint8Array) => void; /** * Verifies the length of the authentication tag for CBC encryption. * @param tag - The authentication tag as a Uint8Array. * @param keyBitLength - The expected length of the key in bits. * @throws Will throw an error if the length of the tag is not as expected. */ export declare const cbcVerifyTagLength: (tag: Uint8Array, keyBitLength: number) => void; /** * Interface for cryptographic cipher operations. */ export declare interface Cipher { /** * Encrypts data using the specified algorithm and parameters. * @param params - The encryption parameters. * @returns A promise that resolves to the encryption result. */ encrypt: ({ enc, plaintext, cek, iv, aad, }: EncryptParams) => Promise; /** * Decrypts data using the specified algorithm and parameters. * @param params - The decryption parameters. * @returns A promise that resolves to the decrypted data. */ decrypt: ({ enc, cek, ciphertext, iv, tag, aad, }: DecryptParams) => Promise; /** * Returns the byte length of the initialization vector (IV) required for the specified encryption algorithm. * @param enc - The encryption algorithm identifier. * @returns The IV byte length for the specified algorithm. */ getIvByteLength: (enc: Enc) => number; /** * Returns the required byte length of the content encryption key (CEK) for the given encryption algorithm. * This value depends on the selected algorithm—for CBC modes, the CEK is typically twice the underlying key size due to combined encryption/MAC. * @param enc - The encryption algorithm identifier. * @returns The required CEK byte length for the specified algorithm. */ getCekByteLength: (enc: Enc) => number; } /** * Decodes the given Uint8Array into EncryptionData. * @param {Uint8Array} encoded - The encoded data. * @returns {EncryptionData} The decoded data. */ export declare const decodeEncryptionData: (encoded: Uint8Array) => EncryptionData; /** * Parameters required for decryption operation. * @property {string} enc - The encryption algorithm used. * @property {Uint8Array} cek - The content encryption key. * @property {Uint8Array} ciphertext - The data to be decrypted. * @property {Uint8Array} iv - The initialization vector. * @property {Uint8Array} tag - The authentication tag. * @property {Uint8Array} aad - Additional authenticated data. */ export declare interface DecryptParams { enc: string; cek: Uint8Array; ciphertext: Uint8Array; iv: Uint8Array; tag: Uint8Array; aad: Uint8Array; } /** * Divides the content encryption key (CEK) into encryption and MAC raw keys. * * @param {DivideCekArgs} args - The arguments for dividing the CEK. * @returns {DivideCekResult} The divided encryption and MAC raw keys. * @throws {Error} If the CEK length is not twice the keyBytes. */ export declare const divideCek: ({ cek, keyBitLength, }: DivideCekArgs) => DivideCekResult; /** * Arguments for the divideCek function. */ declare type DivideCekArgs = { /** * The content encryption key (CEK) as a Uint8Array. */ cek: Uint8Array; /** * The length of the key in bits. * This value is used to determine how to split the content encryption key (CEK) * into encryption and MAC raw keys. */ keyBitLength: number; }; /** * Result of the divideCek function. */ declare type DivideCekResult = { /** * The encryption raw key as a Uint8Array. */ encRawKey: Uint8Array; /** * The MAC raw key as a Uint8Array. */ macRawKey: Uint8Array; }; /** * Type representing all supported encryption algorithms. */ export declare type Enc = (typeof encArray)[number]; /** * Array of all supported encryption algorithms. */ export declare const encArray: readonly ["A128CBC-HS256", "A192CBC-HS384", "A256CBC-HS512", "A128GCM", "A192GCM", "A256GCM"]; /** * Encodes the given EncryptionData into a single Uint8Array. * @param {EncryptionData} data - The data to be encoded. * @returns {Uint8Array} The encoded data. */ export declare const encodeEncryptionData: ({ ciphertext, iv, tag, aad, }: EncryptionData) => Uint8Array; /** * Represents the data required for encryption and decryption operations. * @property {Uint8Array} ciphertext - The encrypted data. * @property {Uint8Array} iv - The initialization vector. * @property {Uint8Array} tag - The authentication tag. * @property {Uint8Array} aad - The additional authenticated data. */ export declare type EncryptionData = { ciphertext: Uint8Array; iv: Uint8Array; tag: Uint8Array; aad: Uint8Array; }; /** * Parameters required for encryption operation. * @property {Enc} enc - The encryption algorithm to use. * @property {Uint8Array} plaintext - The data to be encrypted. * @property {Uint8Array} cek - The content encryption key. * @property {Uint8Array} iv - The initialization vector. * @property {Uint8Array} aad - Additional authenticated data. */ export declare interface EncryptParams { enc: Enc; plaintext: Uint8Array; cek: Uint8Array; iv: Uint8Array; aad: Uint8Array; } /** * Result of an encryption operation. * @property {Uint8Array} ciphertext - The encrypted data. * @property {Uint8Array} tag - The authentication tag. */ export declare interface EncryptResult { ciphertext: Uint8Array; tag: Uint8Array; } /** * Parameters required for the internal GCM decryption process. */ export declare type GcmDecryptInternalParams = { /** * The raw encryption key as a Uint8Array. */ encRawKey: Uint8Array; /** * The ciphertext data to be decrypted as a Uint8Array. */ ciphertext: Uint8Array; /** * The authentication tag as a Uint8Array. */ tag: Uint8Array; /** * The initialization vector as a Uint8Array. */ iv: Uint8Array; /** * Additional authenticated data as a Uint8Array. */ aad: Uint8Array; }; /** * Type representing GCM mode encryption algorithms. */ export declare type GcmEnc = (typeof gcmEncArray)[number]; /** * Array of supported GCM mode encryption algorithms. */ export declare const gcmEncArray: readonly ["A128GCM", "A192GCM", "A256GCM"]; /** * Parameters required for the internal GCM encryption process. */ export declare type GcmEncryptInternalParams = { /** * The raw encryption key as a Uint8Array. */ encRawKey: Uint8Array; /** * The plaintext data to be encrypted as a Uint8Array. */ plaintext: Uint8Array; /** * The initialization vector as a Uint8Array. */ iv: Uint8Array; /** * Additional authenticated data as a Uint8Array. */ aad: Uint8Array; }; /** * Result of the internal GCM encryption process. */ export declare type GcmEncryptInternalResult = { /** * The encrypted data as a Uint8Array. */ ciphertext: Uint8Array; /** * The authentication tag as a Uint8Array. */ tag: Uint8Array; }; /** * Returns the required byte length of the content encryption key (CEK) for GCM encryption. * For GCM mode, the CEK length matches the key length. * @param enc - The encryption algorithm identifier. * @returns The required CEK byte length for the specified GCM algorithm. */ export declare const gcmGetCekByteLength: (enc: Enc) => number; /** * Verifies the length of the content encryption key (CEK) for GCM encryption. * @param cek - The content encryption key as a Uint8Array. * @param keyBitLength - The expected length of the key in bits. * @throws Will throw an error if the length of the CEK is not as expected. */ export declare const gcmVerifyCekLength: (cek: Uint8Array, keyBitLength: number) => void; /** * Verifies the length of the initialization vector (IV) for GCM encryption. * @param iv - The initialization vector as a Uint8Array. * @throws Will throw an error if the length of the IV is not 12 bytes. */ export declare const gcmVerifyIvLength: (iv: Uint8Array) => void; /** * Verifies the length of the authentication tag for GCM encryption. * GCM requires a 16-byte (128-bit) authentication tag. * @param {Uint8Array} tag - The authentication tag to verify * @throws {Error} Will throw an error if the tag length is not 16 bytes * @example * const tag = new Uint8Array(16); * gcmVerifyTagLength(tag); // No error * * const invalidTag = new Uint8Array(12); * gcmVerifyTagLength(invalidTag); // Throws error */ export declare const gcmVerifyTagLength: (tag: Uint8Array) => void; /** * Generates MAC data by concatenating AAD, IV, ciphertext, and AAD length in bits. * @param {GenerateMacDataArgs} args - The arguments for generating MAC data. * @returns {Uint8Array} - The generated MAC data. */ export declare const generateMacData: ({ aad, iv, ciphertext, }: GenerateMacDataArgs) => Uint8Array; /** * Arguments for generating MAC data. * @typedef {Object} GenerateMacDataArgs@typedef {Object} GenerateMacDataArgs * @property {Uint8Array} aad - Additional authenticated data. * @property {Uint8Array} iv - Initialization vector. * @property {Uint8Array} ciphertext - Ciphertext. */ declare type GenerateMacDataArgs = { aad: Uint8Array; iv: Uint8Array; ciphertext: Uint8Array; }; /** * Parameters required for generating a tag. */ export declare type GenerateTagParams = { /** The raw MAC key as a Uint8Array. */ macRawKey: Uint8Array; /** The MAC data as a Uint8Array. */ macData: Uint8Array; /** The length of the key in bits. */ keyBitLength: number; }; /** * Checks if a given string is a valid CBC mode encryption algorithm with HMAC. * @param enc - The string to check. * @returns True if the string is a valid CBC mode encryption algorithm with HMAC, false otherwise. */ export declare const isCbcEnc: (enc: string) => enc is CbcEnc; /** * Checks if a given string is a valid encryption algorithm. * @param value - The string to check. * @returns True if the string is a valid encryption algorithm, false otherwise. */ export declare const isEnc: (value: string) => value is Enc; /** * Checks if a given string is a valid GCM mode encryption algorithm. * @param enc - The string to check. * @returns True if the string is a valid GCM mode encryption algorithm, false otherwise. */ export declare const isGcmEnc: (enc: string) => enc is GcmEnc; /** * Extracts the key length in bits from an encryption algorithm identifier. * The identifier is expected to follow the format where the key length * is represented by the three digits following the first character. * * @example * parseKeyBitLength('A128GCM') // returns 128 * parseKeyBitLength('A256GCM') // returns 256 * * @param enc - The encryption algorithm identifier string * @returns The key length in bits as a number * @throws Will throw an error if the input format is invalid */ export declare const parseKeyBitLength: (enc: Enc) => number; /** * Compares two Uint8Array values in a timing-safe manner. * @param a - The first Uint8Array to compare. * @param b - The second Uint8Array to compare. * @returns true if the values are equal, false otherwise. */ export declare const timingSafeEqual: (a: Uint8Array, b: Uint8Array) => boolean; export { }