Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491/** Optional config-file encryption and decryption. Mirrors upstream `conf` 15.1.0's wire format exactly: a 16-byte initialization vector, a `:` separator, then the ciphertext, with `aes-256-gcm` appending its 16-byte authentication tag. Keys derive through PBKDF2-SHA512 over the initialization vector, with a legacy salt fallback for files written by older `conf` releases. The feature is obscurity, not security: the key lives beside the app. @module */
import { createCipheriv, createDecipheriv, pbkdf2Sync, randomBytes,} from 'node:crypto';import { tagged, type Logger,} from '@monochromatic-dev/module-logger/ts';
import { caughtValueText, } from '@monochromatic-dev/module-caught-value/ts';import { DecryptionFailedError, InvalidAuthenticationTagError,} from './errors.ts';import { bytesToString, concatBytes, stringToBytes,} from './bytes.ts';
//region Types
/** Encryption algorithm identifier accepted by the store. @example ```ts const algorithm: EncryptionAlgorithm = 'aes-256-cbc'; ``` */export type EncryptionAlgorithm = 'aes-256-cbc' | 'aes-256-gcm' | 'aes-256-ctr';
/** Key material accepted by the store's `encryptionKey` option. @example ```ts const key: EncryptionKey = 'correct horse battery staple'; ``` */export type EncryptionKey = string | Uint8Array | NodeJS.TypedArray | DataView;
//endregion Types
//region Constants
/** Algorithm used when `encryptionKey` is set without an explicit algorithm. @example ```ts DEFAULT_ENCRYPTION_ALGORITHM; // 'aes-256-cbc' ``` */export const DEFAULT_ENCRYPTION_ALGORITHM: EncryptionAlgorithm = 'aes-256-cbc';
/** Every algorithm the store accepts. @example ```ts SUPPORTED_ENCRYPTION_ALGORITHMS.has('aes-256-gcm'); // => true ``` */export const SUPPORTED_ENCRYPTION_ALGORITHMS: ReadonlySet<string> = new Set<string>([ 'aes-256-cbc', 'aes-256-gcm', 'aes-256-ctr',],);
/** Byte length of the random initialization vector written before the separator. @example ```ts INITIALIZATION_VECTOR_LENGTH; // 16 ``` */const INITIALIZATION_VECTOR_LENGTH = 16;
/** Code point of the `:` byte separating the initialization vector from the ciphertext. @example ```ts SEPARATOR_CODE_POINT; // 58 ``` */const SEPARATOR_CODE_POINT = 58;
/** Byte length of the `aes-256-gcm` authentication tag appended after the ciphertext. @example ```ts AUTHENTICATION_TAG_LENGTH; // 16 ``` */const AUTHENTICATION_TAG_LENGTH = 16;
/** PBKDF2 round count used to derive the file key. @example ```ts PBKDF2_ITERATIONS; // 10_000 ``` */const PBKDF2_ITERATIONS = 10_000;
/** Byte length of the derived cipher key. @example ```ts DERIVED_KEY_LENGTH; // 32 ``` */const DERIVED_KEY_LENGTH = 32;
/** PBKDF2 digest backing key derivation. @example ```ts PBKDF2_DIGEST; // 'sha512' ``` */const PBKDF2_DIGEST = 'sha512';
//endregion Constants
//region Algorithm checks
/** Reports whether a value names a supported encryption algorithm. @param value - Candidate from options or an external caller. @returns `true` when `value` is one of {@link SUPPORTED_ENCRYPTION_ALGORITHMS}. @example ```ts isSupportedEncryptionAlgorithm('aes-256-cbc'); // => true isSupportedEncryptionAlgorithm('rot13'); // => false ``` */export function isSupportedEncryptionAlgorithm(value: unknown,): value is EncryptionAlgorithm { return ((typeof value) === 'string') && SUPPORTED_ENCRYPTION_ALGORITHMS.has(value);}
//endregion Algorithm checks
//region Key derivation
/** Derives the per-file cipher key from the user key and a salt. @param encryptionKey - User-supplied key material. @param salt - Salt bytes; the initialization vector in current files. @returns Derived 32-byte key for the configured algorithm. @example ```ts deriveKey({ encryptionKey: 'k', salt: initializationVector, }); ``` */export function deriveKey({ encryptionKey, salt,}: { readonly encryptionKey: EncryptionKey; readonly salt: Uint8Array | string;},): Buffer { // oxlint-disable-next-line no-restricted-syntax/no-sync -- Structurally synchronous boundary: the store's get/set contract writes before returning, so key derivation must finish inside the write call; see package/module/conf-fork/DECISION.sync-api.md. return pbkdf2Sync( encryptionKey, salt, PBKDF2_ITERATIONS, DERIVED_KEY_LENGTH, PBKDF2_DIGEST, );}
//endregion Key derivation
//region Encryption
/** Encrypts serialized store text into the config-file byte layout. @param serialized - Serialized store text to protect. @param encryptionKey - User-supplied key material. @param encryptionAlgorithm - Algorithm to apply. @returns Initialization vector, separator, ciphertext, and (for `aes-256-gcm`) authentication tag as one buffer. @example ```ts encryptSerializedStore({ serialized: '{"theme":"dark"}', encryptionKey: 'k', encryptionAlgorithm: 'aes-256-cbc', }); // => Uint8Array ``` */export function encryptSerializedStore({ serialized, encryptionKey, encryptionAlgorithm,}: { readonly serialized: string; readonly encryptionKey: EncryptionKey; readonly encryptionAlgorithm: EncryptionAlgorithm;},): Uint8Array { /** Random salt and nonce for this write. */ const initializationVector = randomBytes(INITIALIZATION_VECTOR_LENGTH,); /** Derived per-file cipher key. */ const key = deriveKey({ encryptionKey, salt: initializationVector, },); /** Framed parts joined below in wire order. */ const parts = [ initializationVector, stringToBytes(':',), ]; if (encryptionAlgorithm === 'aes-256-gcm') { /** GCM cipher whose authentication tag is appended after the ciphertext. */ const cipher = createCipheriv( 'aes-256-gcm', key, initializationVector, ); parts.push( concatBytes([ cipher.update(stringToBytes(serialized,),), cipher.final(), ],), cipher.getAuthTag(), ); return concatBytes(parts,); } /** Block or stream cipher for the unauthenticated algorithms. */ const cipher = createCipheriv( encryptionAlgorithm, key, initializationVector, ); parts.push( concatBytes([ cipher.update(stringToBytes(serialized,),), cipher.final(), ],), ); return concatBytes(parts,);}
//endregion Encryption
//region Decryption
/** Decrypts one payload with a given salt, or reports why it failed. @param data - Full file bytes including framing. @param encryptionKey - User-supplied key material. @param encryptionAlgorithm - Algorithm the file was written with. @param salt - Salt candidate to try. @returns Decrypted serialized store text. @throws InvalidAuthenticationTagError when an `aes-256-gcm` payload is too short for its authentication tag. @example ```ts decryptWithSalt({ data: fileBytes, encryptionKey: 'k', encryptionAlgorithm: 'aes-256-cbc', salt: initializationVector, }); ``` */export function decryptWithSalt({ data, encryptionKey, encryptionAlgorithm, salt,}: { readonly data: Uint8Array; readonly encryptionKey: EncryptionKey; readonly encryptionAlgorithm: EncryptionAlgorithm; readonly salt: Uint8Array | string;},): string { /** Initialization vector framing the payload. */ const initializationVector = data.slice( 0, INITIALIZATION_VECTOR_LENGTH, ); /** Ciphertext region following the separator byte. */ const payload = data.slice(INITIALIZATION_VECTOR_LENGTH + 1,); if (encryptionAlgorithm === 'aes-256-gcm') { /** Index where the trailing authentication tag begins. */ const authenticationTagStart = payload.length - AUTHENTICATION_TAG_LENGTH; if (authenticationTagStart < 0) throw new InvalidAuthenticationTagError(); /** GCM decipher authenticated against the trailing tag. */ const decipher = createDecipheriv( 'aes-256-gcm', deriveKey({ encryptionKey, salt, },), initializationVector, ); decipher.setAuthTag(payload.slice(authenticationTagStart,),); return bytesToString( concatBytes([ decipher.update(payload.slice( 0, authenticationTagStart, ),), decipher.final(), ],), ); } /** Block or stream decipher for the unauthenticated algorithms. */ const decipher = createDecipheriv( encryptionAlgorithm, deriveKey({ encryptionKey, salt, },), initializationVector, ); return bytesToString( concatBytes([ decipher.update(payload,), decipher.final(), ],), );}
/** Decrypts config-file bytes back into serialized store text. Mirrors upstream `conf` 15.1.0: files without framing pass through untouched under `aes-256-cbc`, current-salt then legacy-salt decryption are attempted, and only then does the plaintext-or-fail fallback apply. @param data - Full file bytes as read from disk. @param encryptionKey - User-supplied key material. @param encryptionAlgorithm - Algorithm the file was written with. @param logger - Logger for the expected salt-scheme fallback diagnostics. @returns Serialized store text. @throws DecryptionFailedError when no plaintext passthrough applies and every decryption attempt failed. @example ```ts decryptConfigData({ data: fileBytes, encryptionKey: 'k', encryptionAlgorithm: 'aes-256-cbc', }); // => '{"theme":"dark"}' ``` */export function decryptConfigData({ data, encryptionKey, encryptionAlgorithm, logger,}: { readonly data: Uint8Array; readonly encryptionKey: EncryptionKey; readonly encryptionAlgorithm: EncryptionAlgorithm; readonly logger?: Logger;},): string { /** Logger wrapped with this function's name so fallback diagnostics name their origin. */ const log = logger ?? tagged({ tag: decryptConfigData.name, },); /** Byte where the initialization vector ends and the separator lives. */ const separatorByte = data[INITIALIZATION_VECTOR_LENGTH]; if (separatorByte !== SEPARATOR_CODE_POINT) { if (encryptionAlgorithm === 'aes-256-cbc') return bytesToString(data,); throw new DecryptionFailedError(); } /** Initialization vector framing the payload. */ const initializationVector = data.slice( 0, INITIALIZATION_VECTOR_LENGTH, ); try { return decryptWithSalt({ data, encryptionKey, encryptionAlgorithm, salt: initializationVector, },); } catch (currentSaltError) { log.debug(`current-salt decryption failed (${caughtValueText(currentSaltError,)}); trying legacy salt`,); try { return decryptWithSalt({ data, encryptionKey, encryptionAlgorithm, salt: bytesToString(initializationVector,), },); } catch (legacySaltError) { log.debug(`legacy-salt decryption failed (${caughtValueText(legacySaltError,)}); falling back`,); } } if (encryptionAlgorithm === 'aes-256-cbc') return bytesToString(data,); throw new DecryptionFailedError();}
//endregion Decryption