@venizia/ignis-helpers 0.4.7-2 → 0.4.7-4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/core.d.ts +3 -0
- package/dist/esm/core.d.ts.map +1 -1
- package/dist/esm/core.js +6 -1
- package/dist/esm/core.js.map +1 -1
- package/dist/esm/modules/crypto/algorithms/abstract.algorithm.d.ts +22 -0
- package/dist/esm/modules/crypto/algorithms/abstract.algorithm.d.ts.map +1 -0
- package/dist/esm/modules/crypto/algorithms/abstract.algorithm.js +10 -0
- package/dist/esm/modules/crypto/algorithms/abstract.algorithm.js.map +1 -0
- package/dist/esm/modules/crypto/algorithms/aes-legacy.algorithm.d.ts +7 -6
- package/dist/esm/modules/crypto/algorithms/aes-legacy.algorithm.d.ts.map +1 -1
- package/dist/esm/modules/crypto/algorithms/aes-legacy.algorithm.js +32 -38
- package/dist/esm/modules/crypto/algorithms/aes-legacy.algorithm.js.map +1 -1
- package/dist/esm/modules/crypto/algorithms/aes.algorithm.d.ts +9 -8
- package/dist/esm/modules/crypto/algorithms/aes.algorithm.d.ts.map +1 -1
- package/dist/esm/modules/crypto/algorithms/aes.algorithm.js +46 -41
- package/dist/esm/modules/crypto/algorithms/aes.algorithm.js.map +1 -1
- package/dist/esm/modules/crypto/algorithms/aes.engine.d.ts +32 -0
- package/dist/esm/modules/crypto/algorithms/aes.engine.d.ts.map +1 -0
- package/dist/esm/modules/crypto/algorithms/aes.engine.js +76 -0
- package/dist/esm/modules/crypto/algorithms/aes.engine.js.map +1 -0
- package/dist/esm/modules/crypto/algorithms/base.algorithm.d.ts +10 -17
- package/dist/esm/modules/crypto/algorithms/base.algorithm.d.ts.map +1 -1
- package/dist/esm/modules/crypto/algorithms/base.algorithm.js +51 -12
- package/dist/esm/modules/crypto/algorithms/base.algorithm.js.map +1 -1
- package/dist/esm/modules/crypto/algorithms/credential.algorithm.d.ts +45 -0
- package/dist/esm/modules/crypto/algorithms/credential.algorithm.d.ts.map +1 -0
- package/dist/esm/modules/crypto/algorithms/credential.algorithm.js +116 -0
- package/dist/esm/modules/crypto/algorithms/credential.algorithm.js.map +1 -0
- package/dist/esm/modules/crypto/algorithms/ecdh.algorithm.d.ts +6 -2
- package/dist/esm/modules/crypto/algorithms/ecdh.algorithm.d.ts.map +1 -1
- package/dist/esm/modules/crypto/algorithms/ecdh.algorithm.js +18 -5
- package/dist/esm/modules/crypto/algorithms/ecdh.algorithm.js.map +1 -1
- package/dist/esm/modules/crypto/algorithms/hash.algorithm.d.ts.map +1 -1
- package/dist/esm/modules/crypto/algorithms/hash.algorithm.js +2 -3
- package/dist/esm/modules/crypto/algorithms/hash.algorithm.js.map +1 -1
- package/dist/esm/modules/crypto/algorithms/index.d.ts +1 -0
- package/dist/esm/modules/crypto/algorithms/index.d.ts.map +1 -1
- package/dist/esm/modules/crypto/algorithms/index.js +1 -0
- package/dist/esm/modules/crypto/algorithms/index.js.map +1 -1
- package/dist/esm/modules/crypto/algorithms/rsa.algorithm.d.ts +8 -7
- package/dist/esm/modules/crypto/algorithms/rsa.algorithm.d.ts.map +1 -1
- package/dist/esm/modules/crypto/algorithms/rsa.algorithm.js +22 -25
- package/dist/esm/modules/crypto/algorithms/rsa.algorithm.js.map +1 -1
- package/dist/esm/modules/crypto/common/constants.d.ts +9 -0
- package/dist/esm/modules/crypto/common/constants.d.ts.map +1 -1
- package/dist/esm/modules/crypto/common/constants.js +10 -0
- package/dist/esm/modules/crypto/common/constants.js.map +1 -1
- package/dist/esm/modules/crypto/common/types.d.ts +3 -3
- package/dist/esm/modules/crypto/common/types.d.ts.map +1 -1
- package/dist/esm/modules/crypto/common/types.js.map +1 -1
- package/dist/esm/modules/uid/uuid/sha1.d.ts +1 -1
- package/dist/esm/modules/uid/uuid/sha1.js +1 -1
- package/dist/esm/modules/uid/uuid/sha1.js.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aes.algorithm.js","source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,MAAM,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,QAAQ,CAC5E,cAAc,CACf,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,OAAO,EAAE,CAAC;gBACZ,MAAM,KAAK,CAAC;YACd,CAAC;YAED,OAAO,OAAO,CAAC;QACjB,CAAC;IACH,CAAC;IAED,WAAW,CAAC,IAAkD;QAC5D,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;QAEtC,IAAI,CAAC,YAAY,IAAI,aAAa,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC,EAAE,CAAC;YACpE,OAAO,EAAE,CAAC;QACZ,CAAC;QAED,MAAM,MAAM,GAAG,EAAE,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC;QAC7C,MAAM,WAAW,GAAG,MAAM,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC;QAC9C,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,CAAC,CAAC;QACjE,OAAO,SAAS,CAAC;IACnB,CAAC;CACF","sourcesContent":["import { AESAlgorithms, type AESAlgorithmType } from './aes.constants';\nimport { ObjectUtility } from '@venizia/ignis-inversion';\nimport C from 'node:crypto';\nimport fs from 'node:fs';\nimport { getError } from '@/modules/error';\nimport { BaseCryptoAlgorithm } from './base.algorithm';\n\nconst DEFAULT_LENGTH = 16;\nconst CIPHERTEXT_VERSION = 0x01;\nconst DEFAULT_KEY_ID = '0';\n\ninterface IAESDecryptOptions {\n inputEncoding?: BufferEncoding;\n outputEncoding?: BufferEncoding;\n doThrow?: boolean;\n /** Per-deployment PBKDF2 salt override. Must be supplied identically on encrypt and decrypt; omit for the shipped default. */\n kdfSalt?: string;\n /** PBKDF2 iteration count override. Must be supplied identically on encrypt and decrypt; omit for the shipped default. */\n kdfIterations?: number;\n}\n\n/** `iv` is encrypt-only: the envelope carries the IV, so decrypt reads it from there and a supplied one would be silently ignored. */\ninterface IAESExtraOptions extends IAESDecryptOptions {\n iv?: Buffer;\n}\n\nexport interface IAESKeyringEntry {\n id: string;\n secret: string;\n}\n\nexport type TAESSecret = string | IAESKeyringEntry[];\n\nexport { AESAlgorithms, type AESAlgorithmType };\n\nexport class AES extends BaseCryptoAlgorithm<\n AESAlgorithmType,\n string,\n string,\n TAESSecret,\n string,\n string,\n IAESExtraOptions\n> {\n constructor(opts: { algorithm: AESAlgorithmType }) {\n super({ scope: AES.name, ...opts });\n }\n\n static withAlgorithm(algorithm: AESAlgorithmType) {\n return new AES({ algorithm });\n }\n\n protected resolveEncryptKey(opts: {\n secret: TAESSecret;\n kdfSalt?: string;\n kdfIterations?: number;\n }): { id: string; key: Buffer } {\n const { secret, kdfSalt, kdfIterations } = opts;\n const entry = Array.isArray(secret) ? secret[0] : { id: DEFAULT_KEY_ID, secret };\n\n if (!entry || ObjectUtility.isEmpty({ value: entry.secret })) {\n throw getError({ message: '[AES][resolveEncryptKey] Missing secret or empty keyring' });\n }\n\n const key = this.normalizeSecretKey({\n secret: entry.secret,\n length: this.getAlgorithmKeySize(),\n kdfSalt,\n kdfIterations,\n });\n return { id: entry.id, key };\n }\n\n protected resolveDecryptKey(opts: {\n secret: TAESSecret;\n id: string;\n kdfSalt?: string;\n kdfIterations?: number;\n }): Buffer {\n const { secret, id, kdfSalt, kdfIterations } = opts;\n const length = this.getAlgorithmKeySize();\n\n if (!Array.isArray(secret)) {\n return this.normalizeSecretKey({ secret, length, kdfSalt, kdfIterations });\n }\n\n const entry = secret.find((e) => e.id === id);\n if (!entry) {\n throw getError({\n message: `[AES][resolveDecryptKey] No key in keyring matches ciphertext key id \"${id}\"`,\n });\n }\n\n // Without this the empty secret derives a key from '' and fails as an opaque OpenSSL error, hiding a plain configuration mistake.\n if (ObjectUtility.isEmpty({ value: entry.secret })) {\n throw getError({\n message: `[AES][resolveDecryptKey] Keyring entry for key id \"${id}\" has an empty secret`,\n });\n }\n\n return this.normalizeSecretKey({ secret: entry.secret, length, kdfSalt, kdfIterations });\n }\n\n encrypt(opts: { message: string; secret: TAESSecret; opts?: IAESExtraOptions }) {\n const { message, secret } = opts;\n const {\n iv = C.randomBytes(DEFAULT_LENGTH),\n inputEncoding = 'utf-8',\n outputEncoding = 'base64',\n doThrow = true,\n kdfSalt,\n kdfIterations,\n } = opts.opts ?? {};\n\n try {\n const { id, key } = this.resolveEncryptKey({ secret, kdfSalt, kdfIterations });\n const idBuffer = Buffer.from(id, 'utf-8');\n if (idBuffer.length > 0xff) {\n throw getError({ message: '[AES][encrypt] Key id too long (max 255 bytes)' });\n }\n\n const cipher = C.createCipheriv(this.algorithm, key, iv);\n\n // Envelope: [version(1)][idLen(1)][id(idLen)][iv(16)][authTag(16, gcm only)][cipher]\n const header = Buffer.from([CIPHERTEXT_VERSION, idBuffer.length]);\n const parts = [header, idBuffer, iv];\n\n const cipherText = cipher.update(message, inputEncoding);\n const cipherFinal = cipher.final();\n\n switch (this.algorithm) {\n case 'aes-256-cbc': {\n break;\n }\n case 'aes-256-gcm': {\n parts.push((cipher as C.CipherGCM).getAuthTag());\n break;\n }\n }\n parts.push(cipherText);\n parts.push(cipherFinal);\n\n return Buffer.concat(parts).toString(outputEncoding);\n } catch (error) {\n if (doThrow) {\n throw error;\n }\n\n return message;\n }\n }\n\n encryptFile(opts: { absolutePath: string; secret: TAESSecret }): string {\n const { absolutePath, secret } = opts;\n\n if (!absolutePath || ObjectUtility.isEmpty({ value: absolutePath })) {\n return '';\n }\n\n const buffer = fs.readFileSync(absolutePath);\n const fileContent = buffer?.toString('utf-8');\n const encrypted = this.encrypt({ message: fileContent, secret });\n return encrypted;\n }\n\n decrypt(opts: { message: string; secret: TAESSecret; opts?: IAESDecryptOptions }) {\n const { message, secret } = opts;\n const {\n inputEncoding = 'base64',\n outputEncoding = 'utf-8',\n doThrow = true,\n kdfSalt,\n kdfIterations,\n } = opts.opts ?? {};\n\n try {\n const raw = Buffer.from(message, inputEncoding);\n\n const version = raw[0];\n if (version !== CIPHERTEXT_VERSION) {\n throw getError({ message: `[AES][decrypt] Unsupported ciphertext version: ${version}` });\n }\n\n const idLength = raw[1];\n let cursor = 2;\n\n const id = raw.subarray(cursor, cursor + idLength).toString('utf-8');\n cursor += idLength;\n\n const iv = raw.subarray(cursor, cursor + DEFAULT_LENGTH);\n cursor += DEFAULT_LENGTH;\n\n const key = this.resolveDecryptKey({ secret, id, kdfSalt, kdfIterations });\n const decipher = C.createDecipheriv(this.algorithm, key, iv);\n\n switch (this.algorithm) {\n case 'aes-256-cbc': {\n break;\n }\n case 'aes-256-gcm': {\n const authTag = raw.subarray(cursor, cursor + DEFAULT_LENGTH);\n cursor += DEFAULT_LENGTH;\n (decipher as C.DecipherGCM).setAuthTag(authTag);\n break;\n }\n }\n\n const cipherText = raw.subarray(cursor);\n return Buffer.concat([decipher.update(cipherText), decipher.final()]).toString(\n outputEncoding,\n );\n } catch (error) {\n if (doThrow) {\n throw error;\n }\n\n return message;\n }\n }\n\n decryptFile(opts: { absolutePath: string; secret: TAESSecret }) {\n const { absolutePath, secret } = opts;\n\n if (!absolutePath || ObjectUtility.isEmpty({ value: absolutePath })) {\n return '';\n }\n\n const buffer = fs.readFileSync(absolutePath);\n const fileContent = buffer?.toString('utf-8');\n const decrypted = this.decrypt({ message: fileContent, secret });\n return decrypted;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"aes.algorithm.js","sourceRoot":"","sources":["../../../../../src/modules/crypto/algorithms/aes.algorithm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAyB,MAAM,iBAAiB,CAAC;AACvE,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;A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AAC,IAAkD;QAClE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;QAEtC,IAAI,CAAC,YAAY,IAAI,aAAa,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC,EAAE,CAAC;YACpE,OAAO,EAAE,CAAC;QACZ,CAAC;QAED,MAAM,MAAM,GAAG,EAAE,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC;QAC7C,MAAM,WAAW,GAAG,MAAM,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC;QAC9C,OAAO,IAAI,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,CAAC,CAAC;IACxD,CAAC;CACF","sourcesContent":["import { AESAlgorithms, type AESAlgorithmType } from './aes.constants';\nimport { ObjectUtility } from '@venizia/ignis-inversion';\nimport fs from 'node:fs';\nimport { getError } from '@/modules/error';\nimport { AesEngine } from './aes.engine';\nimport { BaseCryptoAlgorithm } from './base.algorithm';\n\nconst DEFAULT_LENGTH = 16;\nconst CIPHERTEXT_VERSION = 0x01;\nconst DEFAULT_KEY_ID = '0';\n\ninterface IAESDecryptOptions {\n inputEncoding?: BufferEncoding;\n outputEncoding?: BufferEncoding;\n doThrow?: boolean;\n /** Per-deployment PBKDF2 salt override. Must be supplied identically on encrypt and decrypt; omit for the shipped default. */\n kdfSalt?: string;\n /** PBKDF2 iteration count override. Must be supplied identically on encrypt and decrypt; omit for the shipped default. */\n kdfIterations?: number;\n}\n\n/** `iv` is encrypt-only: the envelope carries the IV, so decrypt reads it from there and a supplied one would be silently ignored. */\ninterface IAESExtraOptions extends IAESDecryptOptions {\n iv?: Buffer;\n}\n\nexport interface IAESKeyringEntry {\n id: string;\n secret: string;\n}\n\nexport type TAESSecret = string | IAESKeyringEntry[];\n\nexport { AESAlgorithms, type AESAlgorithmType };\n\n/** `encrypt` and `decrypt` are async: key derivation and the cipher run over `crypto.subtle`. */\nexport class AES extends BaseCryptoAlgorithm<\n AESAlgorithmType,\n string,\n string,\n TAESSecret,\n Promise<string>,\n Promise<string>,\n IAESExtraOptions\n> {\n constructor(opts: { algorithm: AESAlgorithmType }) {\n super({ scope: AES.name, ...opts });\n }\n\n static withAlgorithm(algorithm: AESAlgorithmType) {\n return new AES({ algorithm });\n }\n\n protected async resolveEncryptKey(opts: {\n secret: TAESSecret;\n kdfSalt?: string;\n kdfIterations?: number;\n }): Promise<{ id: string; key: Buffer }> {\n const { secret, kdfSalt, kdfIterations } = opts;\n const entry = Array.isArray(secret) ? secret[0] : { id: DEFAULT_KEY_ID, secret };\n\n if (!entry || ObjectUtility.isEmpty({ value: entry.secret })) {\n throw getError({ message: '[AES][resolveEncryptKey] Missing secret or empty keyring' });\n }\n\n const key = await this.normalizeSecretKey({\n secret: entry.secret,\n length: this.getAlgorithmKeySize(),\n kdfSalt,\n kdfIterations,\n });\n return { id: entry.id, key };\n }\n\n protected async resolveDecryptKey(opts: {\n secret: TAESSecret;\n id: string;\n kdfSalt?: string;\n kdfIterations?: number;\n }): Promise<Buffer> {\n const { secret, id, kdfSalt, kdfIterations } = opts;\n const length = this.getAlgorithmKeySize();\n\n if (!Array.isArray(secret)) {\n return this.normalizeSecretKey({ secret, length, kdfSalt, kdfIterations });\n }\n\n const entry = secret.find((e) => e.id === id);\n if (!entry) {\n throw getError({\n message: `[AES][resolveDecryptKey] No key in keyring matches ciphertext key id \"${id}\"`,\n });\n }\n\n // Without this the empty secret derives a key from '' and fails as an opaque OpenSSL error, hiding a plain configuration mistake.\n if (ObjectUtility.isEmpty({ value: entry.secret })) {\n throw getError({\n message: `[AES][resolveDecryptKey] Keyring entry for key id \"${id}\" has an empty secret`,\n });\n }\n\n return this.normalizeSecretKey({\n secret: entry.secret,\n length,\n kdfSalt,\n kdfIterations,\n });\n }\n\n async encrypt(opts: {\n message: string;\n secret: TAESSecret;\n opts?: IAESExtraOptions;\n }): Promise<string> {\n const { message, secret } = opts;\n const {\n iv = Buffer.from(crypto.getRandomValues(new Uint8Array(DEFAULT_LENGTH))),\n inputEncoding = 'utf-8',\n outputEncoding = 'base64',\n doThrow = true,\n kdfSalt,\n kdfIterations,\n } = opts.opts ?? {};\n\n try {\n const { id, key } = await this.resolveEncryptKey({ secret, kdfSalt, kdfIterations });\n const idBuffer = Buffer.from(id, 'utf-8');\n if (idBuffer.length > 0xff) {\n throw getError({ message: '[AES][encrypt] Key id too long (max 255 bytes)' });\n }\n\n // `decrypt` reads exactly DEFAULT_LENGTH bytes of IV from the envelope, so any other length would write a ciphertext nothing can read.\n if (iv.length !== DEFAULT_LENGTH) {\n throw getError({\n message: `[AES][encrypt] The IV must be ${DEFAULT_LENGTH} bytes, got ${iv.length}`,\n });\n }\n\n const { cipherText, authTag } = await AesEngine.seal({\n algorithm: this.algorithm,\n key,\n iv,\n plain: Buffer.from(message, inputEncoding),\n });\n\n // Envelope: [version(1)][idLen(1)][id(idLen)][iv(16)][authTag(16, gcm only)][cipher]\n const header = Buffer.from([CIPHERTEXT_VERSION, idBuffer.length]);\n const parts: Uint8Array[] = [header, idBuffer, iv];\n\n if (authTag !== null) {\n parts.push(authTag);\n }\n parts.push(cipherText);\n\n return Buffer.concat(parts).toString(outputEncoding);\n } catch (error) {\n if (doThrow) {\n throw error;\n }\n\n return message;\n }\n }\n\n async encryptFile(opts: { absolutePath: string; secret: TAESSecret }): Promise<string> {\n const { absolutePath, secret } = opts;\n\n if (!absolutePath || ObjectUtility.isEmpty({ value: absolutePath })) {\n return '';\n }\n\n const buffer = fs.readFileSync(absolutePath);\n const fileContent = buffer?.toString('utf-8');\n return this.encrypt({ message: fileContent, secret });\n }\n\n async decrypt(opts: {\n message: string;\n secret: TAESSecret;\n opts?: IAESDecryptOptions;\n }): Promise<string> {\n const { message, secret } = opts;\n const {\n inputEncoding = 'base64',\n outputEncoding = 'utf-8',\n doThrow = true,\n kdfSalt,\n kdfIterations,\n } = opts.opts ?? {};\n\n try {\n const raw = Buffer.from(message, inputEncoding);\n\n const version = raw[0];\n if (version !== CIPHERTEXT_VERSION) {\n throw getError({ message: `[AES][decrypt] Unsupported ciphertext version: ${version}` });\n }\n\n const idLength = raw[1];\n let cursor = 2;\n\n const id = raw.subarray(cursor, cursor + idLength).toString('utf-8');\n cursor += idLength;\n\n const iv = raw.subarray(cursor, cursor + DEFAULT_LENGTH);\n cursor += DEFAULT_LENGTH;\n\n const key = await this.resolveDecryptKey({ secret, id, kdfSalt, kdfIterations });\n\n let authTag: Uint8Array | null = null;\n if (this.algorithm === AESAlgorithms.AES_256_GCM) {\n authTag = raw.subarray(cursor, cursor + DEFAULT_LENGTH);\n cursor += DEFAULT_LENGTH;\n }\n\n const plain = await AesEngine.open({\n algorithm: this.algorithm,\n key,\n iv,\n cipherText: raw.subarray(cursor),\n authTag,\n });\n return Buffer.from(plain).toString(outputEncoding);\n } catch (error) {\n if (doThrow) {\n throw error;\n }\n\n return message;\n }\n }\n\n async decryptFile(opts: { absolutePath: string; secret: TAESSecret }): Promise<string> {\n const { absolutePath, secret } = opts;\n\n if (!absolutePath || ObjectUtility.isEmpty({ value: absolutePath })) {\n return '';\n }\n\n const buffer = fs.readFileSync(absolutePath);\n const fileContent = buffer?.toString('utf-8');\n return this.decrypt({ message: fileContent, secret });\n }\n}\n"]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type AESAlgorithmType } from './aes.constants.js';
|
|
2
|
+
/**
|
|
3
|
+
* AES-CBC and AES-GCM over `crypto.subtle`, shared by `AES` and `LegacyAES`. It keeps the layout of the
|
|
4
|
+
* synchronous implementation it replaced, so what is already stored still reads: CBC pads with PKCS#7,
|
|
5
|
+
* and GCM takes a 16-byte IV and a 16-byte tag. Web Crypto returns GCM output as `cipherText || tag`;
|
|
6
|
+
* the stored envelopes put the tag BEFORE the cipher text, so `seal` and `open` split and join them here
|
|
7
|
+
* and the callers deal in the two parts.
|
|
8
|
+
*/
|
|
9
|
+
export declare class AesEngine {
|
|
10
|
+
static seal(opts: {
|
|
11
|
+
algorithm: AESAlgorithmType;
|
|
12
|
+
key: Uint8Array;
|
|
13
|
+
iv: Uint8Array;
|
|
14
|
+
plain: Uint8Array;
|
|
15
|
+
}): Promise<{
|
|
16
|
+
cipherText: Uint8Array;
|
|
17
|
+
authTag: Uint8Array | null;
|
|
18
|
+
}>;
|
|
19
|
+
/** Throws when the tag does not authenticate: a GCM tag that is missing or not 16 bytes is refused before any decryption. */
|
|
20
|
+
static open(opts: {
|
|
21
|
+
algorithm: AESAlgorithmType;
|
|
22
|
+
key: Uint8Array;
|
|
23
|
+
iv: Uint8Array;
|
|
24
|
+
cipherText: Uint8Array;
|
|
25
|
+
authTag: Uint8Array | null;
|
|
26
|
+
}): Promise<Uint8Array>;
|
|
27
|
+
/** The key must be exactly the size the algorithm names: Web Crypto would otherwise run a 16 or 24 byte key as AES-128 or AES-192. */
|
|
28
|
+
private static importKey;
|
|
29
|
+
private static getName;
|
|
30
|
+
private static getParams;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=aes.engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aes.engine.d.ts","sourceRoot":"","sources":["../../../../../src/modules/crypto/algorithms/aes.engine.ts"],"names":[],"mappings":"AACA,OAAO,EAAiB,KAAK,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAavE;;;;;;GAMG;AACH,qBAAa,SAAS;WACP,IAAI,CAAC,IAAI,EAAE;QACtB,SAAS,EAAE,gBAAgB,CAAC;QAC5B,GAAG,EAAE,UAAU,CAAC;QAChB,EAAE,EAAE,UAAU,CAAC;QACf,KAAK,EAAE,UAAU,CAAC;KACnB,GAAG,OAAO,CAAC;QAAE,UAAU,EAAE,UAAU,CAAC;QAAC,OAAO,EAAE,UAAU,GAAG,IAAI,CAAA;KAAE,CAAC;IAoBnE,6HAA6H;WAChH,IAAI,CAAC,IAAI,EAAE;QACtB,SAAS,EAAE,gBAAgB,CAAC;QAC5B,GAAG,EAAE,UAAU,CAAC;QAChB,EAAE,EAAE,UAAU,CAAC;QACf,UAAU,EAAE,UAAU,CAAC;QACvB,OAAO,EAAE,UAAU,GAAG,IAAI,CAAC;KAC5B,GAAG,OAAO,CAAC,UAAU,CAAC;IAyBvB,sIAAsI;mBACjH,SAAS;IAuB9B,OAAO,CAAC,MAAM,CAAC,OAAO;IAItB,OAAO,CAAC,MAAM,CAAC,SAAS;CAoBzB"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { getError } from '../../error/index.js';
|
|
2
|
+
import { AESAlgorithms } from './aes.constants.js';
|
|
3
|
+
const IV_BYTES = 16;
|
|
4
|
+
const TAG_BYTES = 16;
|
|
5
|
+
const TAG_BITS = TAG_BYTES * 8;
|
|
6
|
+
/**
|
|
7
|
+
* AES-CBC and AES-GCM over `crypto.subtle`, shared by `AES` and `LegacyAES`. It keeps the layout of the
|
|
8
|
+
* synchronous implementation it replaced, so what is already stored still reads: CBC pads with PKCS#7,
|
|
9
|
+
* and GCM takes a 16-byte IV and a 16-byte tag. Web Crypto returns GCM output as `cipherText || tag`;
|
|
10
|
+
* the stored envelopes put the tag BEFORE the cipher text, so `seal` and `open` split and join them here
|
|
11
|
+
* and the callers deal in the two parts.
|
|
12
|
+
*/
|
|
13
|
+
export class AesEngine {
|
|
14
|
+
static async seal(opts) {
|
|
15
|
+
const { algorithm, key, iv, plain } = opts;
|
|
16
|
+
const cryptoKey = await AesEngine.importKey({ algorithm, key, usage: 'encrypt' });
|
|
17
|
+
const encrypted = await crypto.subtle.encrypt(AesEngine.getParams({ algorithm, iv }), cryptoKey, plain);
|
|
18
|
+
const sealed = new Uint8Array(encrypted);
|
|
19
|
+
if (algorithm === AESAlgorithms.AES_256_GCM) {
|
|
20
|
+
return {
|
|
21
|
+
cipherText: sealed.subarray(0, sealed.length - TAG_BYTES),
|
|
22
|
+
authTag: sealed.subarray(sealed.length - TAG_BYTES),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return { cipherText: sealed, authTag: null };
|
|
26
|
+
}
|
|
27
|
+
/** Throws when the tag does not authenticate: a GCM tag that is missing or not 16 bytes is refused before any decryption. */
|
|
28
|
+
static async open(opts) {
|
|
29
|
+
const { algorithm, key, iv, cipherText, authTag } = opts;
|
|
30
|
+
const cryptoKey = await AesEngine.importKey({ algorithm, key, usage: 'decrypt' });
|
|
31
|
+
let input = cipherText;
|
|
32
|
+
if (algorithm === AESAlgorithms.AES_256_GCM) {
|
|
33
|
+
if (authTag === null || authTag.length !== TAG_BYTES) {
|
|
34
|
+
throw getError({
|
|
35
|
+
message: `[AesEngine][open] The GCM auth tag must be ${TAG_BYTES} bytes, got ${authTag?.length ?? 0}`,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
input = new Uint8Array(cipherText.length + TAG_BYTES);
|
|
39
|
+
input.set(cipherText, 0);
|
|
40
|
+
input.set(authTag, cipherText.length);
|
|
41
|
+
}
|
|
42
|
+
const decrypted = await crypto.subtle.decrypt(AesEngine.getParams({ algorithm, iv }), cryptoKey, input);
|
|
43
|
+
return new Uint8Array(decrypted);
|
|
44
|
+
}
|
|
45
|
+
/** The key must be exactly the size the algorithm names: Web Crypto would otherwise run a 16 or 24 byte key as AES-128 or AES-192. */
|
|
46
|
+
static async importKey(opts) {
|
|
47
|
+
const { algorithm, key, usage } = opts;
|
|
48
|
+
const expected = Number(algorithm.split('-')[1]) / 8;
|
|
49
|
+
if (key.length !== expected) {
|
|
50
|
+
throw getError({
|
|
51
|
+
message: `[AesEngine][importKey] Invalid key length ${key.length} bytes for ${algorithm}, expected ${expected}`,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return crypto.subtle.importKey('raw', key, { name: AesEngine.getName({ algorithm }) }, false, [usage]);
|
|
55
|
+
}
|
|
56
|
+
static getName(opts) {
|
|
57
|
+
return opts.algorithm === AESAlgorithms.AES_256_GCM ? 'AES-GCM' : 'AES-CBC';
|
|
58
|
+
}
|
|
59
|
+
static getParams(opts) {
|
|
60
|
+
const { algorithm, iv } = opts;
|
|
61
|
+
if (algorithm === AESAlgorithms.AES_256_GCM) {
|
|
62
|
+
// GCM takes an IV of any length from 1 byte (12 is the usual one). `LegacyAES` read such data before, so it still does.
|
|
63
|
+
if (iv.length === 0) {
|
|
64
|
+
throw getError({ message: '[AesEngine][getParams] The GCM IV must not be empty' });
|
|
65
|
+
}
|
|
66
|
+
return { name: 'AES-GCM', iv: iv, tagLength: TAG_BITS };
|
|
67
|
+
}
|
|
68
|
+
if (iv.length !== IV_BYTES) {
|
|
69
|
+
throw getError({
|
|
70
|
+
message: `[AesEngine][getParams] Invalid IV length ${iv.length} bytes, expected ${IV_BYTES}`,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return { name: 'AES-CBC', iv: iv };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=aes.engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aes.engine.js","sourceRoot":"","sources":["../../../../../src/modules/crypto/algorithms/aes.engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAyB,MAAM,iBAAiB,CAAC;AAEvE,MAAM,QAAQ,GAAG,EAAE,CAAC;AACpB,MAAM,SAAS,GAAG,EAAE,CAAC;AACrB,MAAM,QAAQ,GAAG,SAAS,GAAG,CAAC,CAAC;AAS/B;;;;;;GAMG;AACH,MAAM,OAAO,SAAS;IACpB,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAKjB;QACC,MAAM,EAAE,SAAS,EAAE,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC;QAC3C,MAAM,SAAS,GAAG,MAAM,SAAS,CAAC,SAAS,CAAC,EAAE,SAAS,EAAE,GAAG,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;QAClF,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,OAAO,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C,WAAW,EAAE,CAAC;YAC5C,wHAAwH;YACxH,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACpB,MAAM,QAAQ,CAAC,EAAE,OAAO,EAAE,qDAAqD,EAAE,CAAC,CAAC;YACrF,CAAC;YAED,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,EAAY,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC;QACpE,CAAC;QAED,IAAI,EAAE,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;YAC3B,MAAM,QAAQ,CAAC;gBACb,OAAO,EAAE,4CAA4C,EAAE,CAAC,MAAM,oBAAoB,QAAQ,EAAE;aAC7F,CAAC,CAAC;QACL,CAAC;QAED,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,EAAY,EAAE,CAAC;IAC/C,CAAC;CACF","sourcesContent":["import { getError } from '@/modules/error';\nimport { AESAlgorithms, type AESAlgorithmType } from './aes.constants';\n\nconst IV_BYTES = 16;\nconst TAG_BYTES = 16;\nconst TAG_BITS = TAG_BYTES * 8;\n\n/** What `crypto.subtle` takes. Every array here is backed by an `ArrayBuffer`; none is shared memory. */\ntype TBytes = Uint8Array<ArrayBuffer>;\n\ntype TCipherParams =\n | { name: 'AES-CBC'; iv: TBytes }\n | { name: 'AES-GCM'; iv: TBytes; tagLength: number };\n\n/**\n * AES-CBC and AES-GCM over `crypto.subtle`, shared by `AES` and `LegacyAES`. It keeps the layout of the\n * synchronous implementation it replaced, so what is already stored still reads: CBC pads with PKCS#7,\n * and GCM takes a 16-byte IV and a 16-byte tag. Web Crypto returns GCM output as `cipherText || tag`;\n * the stored envelopes put the tag BEFORE the cipher text, so `seal` and `open` split and join them here\n * and the callers deal in the two parts.\n */\nexport class AesEngine {\n static async seal(opts: {\n algorithm: AESAlgorithmType;\n key: Uint8Array;\n iv: Uint8Array;\n plain: Uint8Array;\n }): Promise<{ cipherText: Uint8Array; authTag: Uint8Array | null }> {\n const { algorithm, key, iv, plain } = opts;\n const cryptoKey = await AesEngine.importKey({ algorithm, key, usage: 'encrypt' });\n const encrypted = await crypto.subtle.encrypt(\n AesEngine.getParams({ algorithm, iv }),\n cryptoKey,\n plain as TBytes,\n );\n const sealed = new Uint8Array(encrypted);\n\n if (algorithm === AESAlgorithms.AES_256_GCM) {\n return {\n cipherText: sealed.subarray(0, sealed.length - TAG_BYTES),\n authTag: sealed.subarray(sealed.length - TAG_BYTES),\n };\n }\n\n return { cipherText: sealed, authTag: null };\n }\n\n /** Throws when the tag does not authenticate: a GCM tag that is missing or not 16 bytes is refused before any decryption. */\n static async open(opts: {\n algorithm: AESAlgorithmType;\n key: Uint8Array;\n iv: Uint8Array;\n cipherText: Uint8Array;\n authTag: Uint8Array | null;\n }): Promise<Uint8Array> {\n const { algorithm, key, iv, cipherText, authTag } = opts;\n const cryptoKey = await AesEngine.importKey({ algorithm, key, usage: 'decrypt' });\n let input = cipherText;\n\n if (algorithm === AESAlgorithms.AES_256_GCM) {\n if (authTag === null || authTag.length !== TAG_BYTES) {\n throw getError({\n message: `[AesEngine][open] The GCM auth tag must be ${TAG_BYTES} bytes, got ${authTag?.length ?? 0}`,\n });\n }\n\n input = new Uint8Array(cipherText.length + TAG_BYTES);\n input.set(cipherText, 0);\n input.set(authTag, cipherText.length);\n }\n\n const decrypted = await crypto.subtle.decrypt(\n AesEngine.getParams({ algorithm, iv }),\n cryptoKey,\n input as TBytes,\n );\n return new Uint8Array(decrypted);\n }\n\n /** The key must be exactly the size the algorithm names: Web Crypto would otherwise run a 16 or 24 byte key as AES-128 or AES-192. */\n private static async importKey(opts: {\n algorithm: AESAlgorithmType;\n key: Uint8Array;\n usage: 'encrypt' | 'decrypt';\n }): Promise<CryptoKey> {\n const { algorithm, key, usage } = opts;\n const expected = Number(algorithm.split('-')[1]) / 8;\n\n if (key.length !== expected) {\n throw getError({\n message: `[AesEngine][importKey] Invalid key length ${key.length} bytes for ${algorithm}, expected ${expected}`,\n });\n }\n\n return crypto.subtle.importKey(\n 'raw',\n key as TBytes,\n { name: AesEngine.getName({ algorithm }) },\n false,\n [usage],\n );\n }\n\n private static getName(opts: { algorithm: AESAlgorithmType }): 'AES-CBC' | 'AES-GCM' {\n return opts.algorithm === AESAlgorithms.AES_256_GCM ? 'AES-GCM' : 'AES-CBC';\n }\n\n private static getParams(opts: { algorithm: AESAlgorithmType; iv: Uint8Array }): TCipherParams {\n const { algorithm, iv } = opts;\n\n if (algorithm === AESAlgorithms.AES_256_GCM) {\n // GCM takes an IV of any length from 1 byte (12 is the usual one). `LegacyAES` read such data before, so it still does.\n if (iv.length === 0) {\n throw getError({ message: '[AesEngine][getParams] The GCM IV must not be empty' });\n }\n\n return { name: 'AES-GCM', iv: iv as TBytes, tagLength: TAG_BITS };\n }\n\n if (iv.length !== IV_BYTES) {\n throw getError({\n message: `[AesEngine][getParams] Invalid IV length ${iv.length} bytes, expected ${IV_BYTES}`,\n });\n }\n\n return { name: 'AES-CBC', iv: iv as TBytes };\n }\n}\n"]}
|
|
@@ -1,18 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export declare abstract class AbstractCryptoAlgorithm<AlgorithmType extends string, EncryptInputType = unknown, DecryptInputType = unknown, SecretKeyType = unknown, EncryptReturnType = unknown, DecryptReturnType = unknown, ExtraOptions = unknown> extends BaseHelper implements ICryptoAlgorithm<AlgorithmType, EncryptInputType, DecryptInputType, SecretKeyType, EncryptReturnType, DecryptReturnType, ExtraOptions> {
|
|
4
|
-
algorithm: AlgorithmType;
|
|
5
|
-
abstract encrypt(opts: {
|
|
6
|
-
message: EncryptInputType;
|
|
7
|
-
secret: SecretKeyType;
|
|
8
|
-
opts?: ExtraOptions;
|
|
9
|
-
}): EncryptReturnType;
|
|
10
|
-
abstract decrypt(opts: {
|
|
11
|
-
message: DecryptInputType;
|
|
12
|
-
secret: SecretKeyType;
|
|
13
|
-
opts?: ExtraOptions;
|
|
14
|
-
}): DecryptReturnType;
|
|
15
|
-
}
|
|
1
|
+
import { AbstractCryptoAlgorithm } from './abstract.algorithm.js';
|
|
2
|
+
export { AbstractCryptoAlgorithm } from './abstract.algorithm.js';
|
|
16
3
|
export declare abstract class BaseCryptoAlgorithm<AlgorithmType extends string, EncryptInputType = unknown, DecryptInputType = unknown, SecretKeyType = unknown, EncryptReturnType = unknown, DecryptReturnType = unknown, ExtraOptions = unknown> extends AbstractCryptoAlgorithm<AlgorithmType, EncryptInputType, DecryptInputType, SecretKeyType, EncryptReturnType, DecryptReturnType, ExtraOptions> {
|
|
17
4
|
constructor(opts: {
|
|
18
5
|
scope: string;
|
|
@@ -21,13 +8,19 @@ export declare abstract class BaseCryptoAlgorithm<AlgorithmType extends string,
|
|
|
21
8
|
validateAlgorithmName(opts: {
|
|
22
9
|
algorithm: AlgorithmType;
|
|
23
10
|
}): void;
|
|
24
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* Omit `kdfSalt`/`kdfIterations` for the shipped default (byte-identical to every prior release). Supplying either requires supplying the same value again on decrypt.
|
|
13
|
+
* Derived with PBKDF2 over `crypto.subtle`, once per secret: a second call, even one that starts while the first runs, shares its result.
|
|
14
|
+
*/
|
|
25
15
|
normalizeSecretKey(opts: {
|
|
26
16
|
secret: string;
|
|
27
17
|
length: number;
|
|
28
18
|
kdfSalt?: string;
|
|
29
19
|
kdfIterations?: number;
|
|
30
|
-
}): Buffer
|
|
20
|
+
}): Promise<Buffer>;
|
|
21
|
+
/** Runs `derive` once per `cacheKey`. A failed derivation leaves nothing behind, so the next call tries again. */
|
|
22
|
+
private deriveOnce;
|
|
23
|
+
private derivePbkdf2;
|
|
31
24
|
/** Rejects a `kdfSalt` short enough to look configured while giving up most of the protection a salt exists for (NIST SP 800-132: 128-bit minimum). */
|
|
32
25
|
private validateKdfSalt;
|
|
33
26
|
getAlgorithmKeySize(): number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.algorithm.d.ts","sourceRoot":"","sources":["../../../../../src/modules/crypto/algorithms/base.algorithm.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"base.algorithm.d.ts","sourceRoot":"","sources":["../../../../../src/modules/crypto/algorithms/base.algorithm.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAE/D,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAU/D,8BAAsB,mBAAmB,CACvC,aAAa,SAAS,MAAM,EAC5B,gBAAgB,GAAG,OAAO,EAC1B,gBAAgB,GAAG,OAAO,EAC1B,aAAa,GAAG,OAAO,EACvB,iBAAiB,GAAG,OAAO,EAC3B,iBAAiB,GAAG,OAAO,EAC3B,YAAY,GAAG,OAAO,CACtB,SAAQ,uBAAuB,CAC/B,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,aAAa,EACb,iBAAiB,EACjB,iBAAiB,EACjB,YAAY,CACb;gBACa,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,aAAa,CAAA;KAAE;IAU7D,qBAAqB,CAAC,IAAI,EAAE;QAAE,SAAS,EAAE,aAAa,CAAA;KAAE;IAUxD;;;OAGG;IACG,kBAAkB,CAAC,IAAI,EAAE;QAC7B,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,aAAa,CAAC,EAAE,MAAM,CAAC;KACxB,GAAG,OAAO,CAAC,MAAM,CAAC;IAwBnB,kHAAkH;IAClH,OAAO,CAAC,UAAU;YAmBJ,YAAY;IA4B1B,uJAAuJ;IACvJ,OAAO,CAAC,eAAe;IAWvB,mBAAmB;CAIpB"}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import C from 'node:crypto';
|
|
2
|
-
import { BaseHelper } from '../../base.js';
|
|
3
1
|
import { getError } from '../../error/index.js';
|
|
4
2
|
import { ParseUtility } from '../../../utilities/index.js';
|
|
5
3
|
import { DEFAULT_CIPHER_BITS, DEFAULT_KDF_DIGEST, DEFAULT_KDF_ITERATIONS, DEFAULT_KDF_SALT, MINIMUM_KDF_SALT_BYTES, } from '../common/index.js';
|
|
4
|
+
import { AbstractCryptoAlgorithm } from './abstract.algorithm.js';
|
|
5
|
+
export { AbstractCryptoAlgorithm } from './abstract.algorithm.js';
|
|
6
|
+
const MAXIMUM_KDF_ITERATIONS = 2 ** 31 - 1;
|
|
7
|
+
/** The Web Crypto name of each digest `DEFAULT_KDF_DIGEST` may hold. */
|
|
8
|
+
const KDF_HASH_NAMES = { sha256: 'SHA-256' };
|
|
9
|
+
/** One entry per derivation. It holds the promise, so callers that arrive while it runs share it. */
|
|
6
10
|
const DERIVED_KEY_CACHE = new Map();
|
|
7
|
-
export class AbstractCryptoAlgorithm extends BaseHelper {
|
|
8
|
-
}
|
|
9
11
|
export class BaseCryptoAlgorithm extends AbstractCryptoAlgorithm {
|
|
10
12
|
constructor(opts) {
|
|
11
13
|
super({
|
|
@@ -23,22 +25,59 @@ export class BaseCryptoAlgorithm extends AbstractCryptoAlgorithm {
|
|
|
23
25
|
});
|
|
24
26
|
}
|
|
25
27
|
}
|
|
26
|
-
/**
|
|
27
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Omit `kdfSalt`/`kdfIterations` for the shipped default (byte-identical to every prior release). Supplying either requires supplying the same value again on decrypt.
|
|
30
|
+
* Derived with PBKDF2 over `crypto.subtle`, once per secret: a second call, even one that starts while the first runs, shares its result.
|
|
31
|
+
*/
|
|
32
|
+
async normalizeSecretKey(opts) {
|
|
28
33
|
const { secret, length, kdfSalt, kdfIterations } = opts;
|
|
29
34
|
if (kdfSalt !== undefined) {
|
|
30
35
|
this.validateKdfSalt({ kdfSalt });
|
|
31
36
|
}
|
|
32
37
|
const salt = kdfSalt ?? DEFAULT_KDF_SALT;
|
|
33
38
|
const iterations = kdfIterations ?? DEFAULT_KDF_ITERATIONS;
|
|
39
|
+
// `pbkdf2Sync` refused anything but an integer from 1 up to 2^31 - 1; Web Crypto would round a fraction down and take up to 2^32 - 1.
|
|
40
|
+
if (!Number.isInteger(iterations) || iterations < 1 || iterations > MAXIMUM_KDF_ITERATIONS) {
|
|
41
|
+
throw getError({
|
|
42
|
+
message: `[BaseCryptoAlgorithm][normalizeSecretKey] kdfIterations must be an integer from 1 to ${MAXIMUM_KDF_ITERATIONS}, got ${iterations}`,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
34
45
|
const cacheKey = `${DEFAULT_KDF_DIGEST}:${iterations}:${length}:${salt}:${secret}`;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
46
|
+
return this.deriveOnce({
|
|
47
|
+
cacheKey,
|
|
48
|
+
derive: () => this.derivePbkdf2({ secret, salt, iterations, length }),
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/** Runs `derive` once per `cacheKey`. A failed derivation leaves nothing behind, so the next call tries again. */
|
|
52
|
+
deriveOnce(opts) {
|
|
53
|
+
const { cacheKey, derive } = opts;
|
|
54
|
+
const pending = DERIVED_KEY_CACHE.get(cacheKey);
|
|
55
|
+
if (pending) {
|
|
56
|
+
return pending;
|
|
57
|
+
}
|
|
58
|
+
const derived = derive();
|
|
59
|
+
DERIVED_KEY_CACHE.set(cacheKey, derived);
|
|
60
|
+
derived.catch(() => {
|
|
61
|
+
if (DERIVED_KEY_CACHE.get(cacheKey) === derived) {
|
|
62
|
+
DERIVED_KEY_CACHE.delete(cacheKey);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
return derived;
|
|
66
|
+
}
|
|
67
|
+
async derivePbkdf2(opts) {
|
|
68
|
+
const { secret, salt, iterations, length } = opts;
|
|
69
|
+
const hash = KDF_HASH_NAMES[DEFAULT_KDF_DIGEST];
|
|
70
|
+
if (hash === undefined) {
|
|
71
|
+
throw getError({
|
|
72
|
+
message: `[BaseCryptoAlgorithm][derivePbkdf2] No Web Crypto name for the digest "${DEFAULT_KDF_DIGEST}"`,
|
|
73
|
+
});
|
|
38
74
|
}
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
75
|
+
const encoder = new TextEncoder();
|
|
76
|
+
const material = await crypto.subtle.importKey('raw', encoder.encode(secret), 'PBKDF2', false, [
|
|
77
|
+
'deriveBits',
|
|
78
|
+
]);
|
|
79
|
+
const bits = await crypto.subtle.deriveBits({ name: 'PBKDF2', hash, salt: encoder.encode(salt), iterations }, material, length * 8);
|
|
80
|
+
return Buffer.from(bits);
|
|
42
81
|
}
|
|
43
82
|
/** Rejects a `kdfSalt` short enough to look configured while giving up most of the protection a salt exists for (NIST SP 800-132: 128-bit minimum). */
|
|
44
83
|
validateKdfSalt(opts) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.algorithm.js","sourceRoot":"","sources":["../../../../../src/modules/crypto/algorithms/base.algorithm.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"base.algorithm.js","sourceRoot":"","sources":["../../../../../src/modules/crypto/algorithms/base.algorithm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,sBAAsB,EACtB,gBAAgB,EAChB,sBAAsB,GACvB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAE/D,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAE/D,MAAM,sBAAsB,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;AAE3C,wEAAwE;AACxE,MAAM,cAAc,GAA2B,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;AAErE,qGAAqG;AACrG,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAA2B,CAAC;AAE7D,MAAM,OAAgB,mBAQpB,SAAQ,uBAQT;IACC,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,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE;YAC7F,YAAY;SACb,CAAC,CAAC;QACH,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,UAAU,CACzC,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,EAChE,QAAQ,EACR,MAAM,GAAG,CAAC,CACX,CAAC;QAEF,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC3B,CAAC;IAED,uJAAuJ;IAC/I,eAAe,CAAC,IAAyB;QAC/C,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;QACzB,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAEvD,IAAI,UAAU,GAAG,sBAAsB,EAAE,CAAC;YACxC,MAAM,QAAQ,CAAC;gBACb,OAAO,EAAE,mEAAmE,sBAAsB,eAAe,UAAU,EAAE;aAC9H,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED,mBAAmB;QACjB,MAAM,CAAC,GAAG,YAAY,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,SAAS,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,mBAAmB,EAAE,CAAC,CAAC;QAChG,OAAO,YAAY,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC9C,CAAC;CACF","sourcesContent":["import { getError } from '@/modules/error';\nimport { ParseUtility } from '@/utilities';\nimport {\n DEFAULT_CIPHER_BITS,\n DEFAULT_KDF_DIGEST,\n DEFAULT_KDF_ITERATIONS,\n DEFAULT_KDF_SALT,\n MINIMUM_KDF_SALT_BYTES,\n} from '../common';\nimport { AbstractCryptoAlgorithm } from './abstract.algorithm';\n\nexport { AbstractCryptoAlgorithm } from './abstract.algorithm';\n\nconst MAXIMUM_KDF_ITERATIONS = 2 ** 31 - 1;\n\n/** The Web Crypto name of each digest `DEFAULT_KDF_DIGEST` may hold. */\nconst KDF_HASH_NAMES: Record<string, string> = { sha256: 'SHA-256' };\n\n/** One entry per derivation. It holds the promise, so callers that arrive while it runs share it. */\nconst DERIVED_KEY_CACHE = new Map<string, Promise<Buffer>>();\n\nexport abstract class BaseCryptoAlgorithm<\n AlgorithmType extends string,\n EncryptInputType = unknown,\n DecryptInputType = unknown,\n SecretKeyType = unknown,\n EncryptReturnType = unknown,\n DecryptReturnType = unknown,\n ExtraOptions = unknown,\n> extends AbstractCryptoAlgorithm<\n AlgorithmType,\n EncryptInputType,\n DecryptInputType,\n SecretKeyType,\n EncryptReturnType,\n DecryptReturnType,\n ExtraOptions\n> {\n constructor(opts: { scope: string; algorithm: AlgorithmType }) {\n super({\n scope: opts.scope ?? opts.algorithm ?? BaseCryptoAlgorithm.name,\n identifier: opts.algorithm,\n });\n this.validateAlgorithmName({ algorithm: opts.algorithm });\n\n this.algorithm = opts.algorithm;\n }\n\n validateAlgorithmName(opts: { algorithm: AlgorithmType }) {\n const { algorithm } = opts;\n\n if (!algorithm) {\n throw getError({\n message: `[validateAlgorithmName] Invalid algorithm name | algorithm: ${algorithm}`,\n });\n }\n }\n\n /**\n * Omit `kdfSalt`/`kdfIterations` for the shipped default (byte-identical to every prior release). Supplying either requires supplying the same value again on decrypt.\n * Derived with PBKDF2 over `crypto.subtle`, once per secret: a second call, even one that starts while the first runs, shares its result.\n */\n async normalizeSecretKey(opts: {\n secret: string;\n length: number;\n kdfSalt?: string;\n kdfIterations?: number;\n }): Promise<Buffer> {\n const { secret, length, kdfSalt, kdfIterations } = opts;\n\n if (kdfSalt !== undefined) {\n this.validateKdfSalt({ kdfSalt });\n }\n\n const salt = kdfSalt ?? DEFAULT_KDF_SALT;\n const iterations = kdfIterations ?? DEFAULT_KDF_ITERATIONS;\n\n // `pbkdf2Sync` refused anything but an integer from 1 up to 2^31 - 1; Web Crypto would round a fraction down and take up to 2^32 - 1.\n if (!Number.isInteger(iterations) || iterations < 1 || iterations > MAXIMUM_KDF_ITERATIONS) {\n throw getError({\n message: `[BaseCryptoAlgorithm][normalizeSecretKey] kdfIterations must be an integer from 1 to ${MAXIMUM_KDF_ITERATIONS}, got ${iterations}`,\n });\n }\n const cacheKey = `${DEFAULT_KDF_DIGEST}:${iterations}:${length}:${salt}:${secret}`;\n\n return this.deriveOnce({\n cacheKey,\n derive: () => this.derivePbkdf2({ secret, salt, iterations, length }),\n });\n }\n\n /** Runs `derive` once per `cacheKey`. A failed derivation leaves nothing behind, so the next call tries again. */\n private deriveOnce(opts: { cacheKey: string; derive: () => Promise<Buffer> }): Promise<Buffer> {\n const { cacheKey, derive } = opts;\n const pending = DERIVED_KEY_CACHE.get(cacheKey);\n\n if (pending) {\n return pending;\n }\n\n const derived = derive();\n DERIVED_KEY_CACHE.set(cacheKey, derived);\n derived.catch(() => {\n if (DERIVED_KEY_CACHE.get(cacheKey) === derived) {\n DERIVED_KEY_CACHE.delete(cacheKey);\n }\n });\n\n return derived;\n }\n\n private async derivePbkdf2(opts: {\n secret: string;\n salt: string;\n iterations: number;\n length: number;\n }): Promise<Buffer> {\n const { secret, salt, iterations, length } = opts;\n const hash = KDF_HASH_NAMES[DEFAULT_KDF_DIGEST];\n\n if (hash === undefined) {\n throw getError({\n message: `[BaseCryptoAlgorithm][derivePbkdf2] No Web Crypto name for the digest \"${DEFAULT_KDF_DIGEST}\"`,\n });\n }\n\n const encoder = new TextEncoder();\n const material = await crypto.subtle.importKey('raw', encoder.encode(secret), 'PBKDF2', false, [\n 'deriveBits',\n ]);\n const bits = await crypto.subtle.deriveBits(\n { name: 'PBKDF2', hash, salt: encoder.encode(salt), iterations },\n material,\n length * 8,\n );\n\n return Buffer.from(bits);\n }\n\n /** Rejects a `kdfSalt` short enough to look configured while giving up most of the protection a salt exists for (NIST SP 800-132: 128-bit minimum). */\n private validateKdfSalt(opts: { kdfSalt: string }): void {\n const { kdfSalt } = opts;\n const byteLength = Buffer.byteLength(kdfSalt, 'utf-8');\n\n if (byteLength < MINIMUM_KDF_SALT_BYTES) {\n throw getError({\n message: `[BaseCryptoAlgorithm][validateKdfSalt] kdfSalt must be at least ${MINIMUM_KDF_SALT_BYTES} bytes, got ${byteLength}`,\n });\n }\n }\n\n getAlgorithmKeySize() {\n const b = ParseUtility.toInt({ input: this.algorithm?.split('-')?.[1] ?? DEFAULT_CIPHER_BITS });\n return ParseUtility.toInt({ input: b / 8 });\n }\n}\n"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type TCredentialAlgorithm } from '../common/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Storing and checking a credential (a password) over `Bun.password`. `hash` writes an argon2id hash with
|
|
4
|
+
* Bun's default parameters unless `algorithm: 'bcrypt'` asks for bcrypt (`$2b$`, 60 characters, the stored
|
|
5
|
+
* format of the `bcrypt` package). `verify` reads every existing bcrypt and argon2 hash, whichever
|
|
6
|
+
* algorithm `hash` wrote, so a stored password is never rehashed or migrated. Needs Bun: it throws on
|
|
7
|
+
* another runtime.
|
|
8
|
+
*
|
|
9
|
+
* Passwords longer than 72 bytes are where Bun and the `bcrypt` package differ, and it only matters for
|
|
10
|
+
* bcrypt. The package keeps only the first 72 bytes; Bun keeps all of them, by hashing a longer password
|
|
11
|
+
* before bcrypt sees it. Two things follow, and they are the whole truth about "the same format":
|
|
12
|
+
*
|
|
13
|
+
* - A hash `hash` writes for a password longer than 72 bytes is read by Bun only. The `bcrypt` package
|
|
14
|
+
* cannot verify it, so rolling back to that package, or another service reading the same column, locks
|
|
15
|
+
* those users out. Up to 72 bytes the two read each other's hashes.
|
|
16
|
+
* - A hash the `bcrypt` package wrote for a longer password fails a plain `Bun.password.verify` with the full
|
|
17
|
+
* password. `verify` therefore retries a bcrypt hash with the first 72 bytes when the password is longer.
|
|
18
|
+
* That retry is for the legacy hashes. A hash `hash` wrote never needs it, because Bun reads its own hash on
|
|
19
|
+
* the first attempt. The retry has one price: a hash of a password of exactly 72 bytes also accepts that
|
|
20
|
+
* password followed by anything, the way bcrypt itself always has.
|
|
21
|
+
*/
|
|
22
|
+
export declare class CredentialHelper {
|
|
23
|
+
/**
|
|
24
|
+
* A hash of `password`: argon2id by default (Bun's default parameters), bcrypt with `algorithm: 'bcrypt'`.
|
|
25
|
+
* For bcrypt, `cost` (4 to 31, default 10) is the work factor, `2 ** cost` rounds. `cost` with argon2id
|
|
26
|
+
* is refused rather than ignored: a caller who tuned a cost is told the default changed.
|
|
27
|
+
*/
|
|
28
|
+
static hash(opts: {
|
|
29
|
+
password: string;
|
|
30
|
+
algorithm?: TCredentialAlgorithm;
|
|
31
|
+
cost?: number;
|
|
32
|
+
}): Promise<string>;
|
|
33
|
+
/**
|
|
34
|
+
* Whether `password` matches `hash`. Nothing to match is `false`: an empty or non-string password, an empty
|
|
35
|
+
* or non-string hash. A stored value that is not a password hash is a server fault (500), so a corrupt row is
|
|
36
|
+
* never answered like a wrong password, and the runtime's own message stays out of it.
|
|
37
|
+
*/
|
|
38
|
+
static verify(opts: {
|
|
39
|
+
password: string;
|
|
40
|
+
hash: string;
|
|
41
|
+
}): Promise<boolean>;
|
|
42
|
+
private static verifyHash;
|
|
43
|
+
private static assertBun;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=credential.algorithm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential.algorithm.d.ts","sourceRoot":"","sources":["../../../../../src/modules/crypto/algorithms/credential.algorithm.ts"],"names":[],"mappings":"AAEA,OAAO,EAGL,KAAK,oBAAoB,EAC1B,MAAM,WAAW,CAAC;AAWnB;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,gBAAgB;IAC3B;;;;OAIG;WACU,IAAI,CAAC,IAAI,EAAE;QACtB,QAAQ,EAAE,MAAM,CAAC;QACjB,SAAS,CAAC,EAAE,oBAAoB,CAAC;QACjC,IAAI,CAAC,EAAE,MAAM,CAAC;KACf,GAAG,OAAO,CAAC,MAAM,CAAC;IAsCnB;;;;OAIG;WACU,MAAM,CAAC,IAAI,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,OAAO,CAAC;mBA+B1D,UAAU;IAmB/B,OAAO,CAAC,MAAM,CAAC,SAAS;CAQzB"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { HTTP } from '../../../common/constants/index.js';
|
|
2
|
+
import { getError } from '../../error/index.js';
|
|
3
|
+
import { CredentialAlgorithms, DEFAULT_CREDENTIAL_ALGORITHM, } from '../common/index.js';
|
|
4
|
+
/** The bcrypt versions the `bcrypt` package writes. */
|
|
5
|
+
const BCRYPT_PREFIX = /^\$2[aby]\$/;
|
|
6
|
+
/** bcrypt reads only the first 72 bytes of a password. */
|
|
7
|
+
const BCRYPT_MAX_PASSWORD_BYTES = 72;
|
|
8
|
+
const MIN_COST = 4;
|
|
9
|
+
const MAX_COST = 31;
|
|
10
|
+
const DEFAULT_COST = 10;
|
|
11
|
+
/**
|
|
12
|
+
* Storing and checking a credential (a password) over `Bun.password`. `hash` writes an argon2id hash with
|
|
13
|
+
* Bun's default parameters unless `algorithm: 'bcrypt'` asks for bcrypt (`$2b$`, 60 characters, the stored
|
|
14
|
+
* format of the `bcrypt` package). `verify` reads every existing bcrypt and argon2 hash, whichever
|
|
15
|
+
* algorithm `hash` wrote, so a stored password is never rehashed or migrated. Needs Bun: it throws on
|
|
16
|
+
* another runtime.
|
|
17
|
+
*
|
|
18
|
+
* Passwords longer than 72 bytes are where Bun and the `bcrypt` package differ, and it only matters for
|
|
19
|
+
* bcrypt. The package keeps only the first 72 bytes; Bun keeps all of them, by hashing a longer password
|
|
20
|
+
* before bcrypt sees it. Two things follow, and they are the whole truth about "the same format":
|
|
21
|
+
*
|
|
22
|
+
* - A hash `hash` writes for a password longer than 72 bytes is read by Bun only. The `bcrypt` package
|
|
23
|
+
* cannot verify it, so rolling back to that package, or another service reading the same column, locks
|
|
24
|
+
* those users out. Up to 72 bytes the two read each other's hashes.
|
|
25
|
+
* - A hash the `bcrypt` package wrote for a longer password fails a plain `Bun.password.verify` with the full
|
|
26
|
+
* password. `verify` therefore retries a bcrypt hash with the first 72 bytes when the password is longer.
|
|
27
|
+
* That retry is for the legacy hashes. A hash `hash` wrote never needs it, because Bun reads its own hash on
|
|
28
|
+
* the first attempt. The retry has one price: a hash of a password of exactly 72 bytes also accepts that
|
|
29
|
+
* password followed by anything, the way bcrypt itself always has.
|
|
30
|
+
*/
|
|
31
|
+
export class CredentialHelper {
|
|
32
|
+
/**
|
|
33
|
+
* A hash of `password`: argon2id by default (Bun's default parameters), bcrypt with `algorithm: 'bcrypt'`.
|
|
34
|
+
* For bcrypt, `cost` (4 to 31, default 10) is the work factor, `2 ** cost` rounds. `cost` with argon2id
|
|
35
|
+
* is refused rather than ignored: a caller who tuned a cost is told the default changed.
|
|
36
|
+
*/
|
|
37
|
+
static async hash(opts) {
|
|
38
|
+
const { password, algorithm = DEFAULT_CREDENTIAL_ALGORITHM, cost } = opts;
|
|
39
|
+
CredentialHelper.assertBun({ caller: 'hash' });
|
|
40
|
+
if (typeof password !== 'string' || password.length === 0) {
|
|
41
|
+
throw getError({
|
|
42
|
+
message: '[CredentialHelper.hash] Invalid password | Expected a non-empty string',
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (typeof algorithm !== 'string' || !CredentialAlgorithms.isValid(algorithm)) {
|
|
46
|
+
throw getError({
|
|
47
|
+
message: `[CredentialHelper.hash] Invalid algorithm | Expected one of ${[...CredentialAlgorithms.SCHEME_SET].join(', ')} | Got: ${String(algorithm)}`,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (algorithm === CredentialAlgorithms.ARGON2ID) {
|
|
51
|
+
if (cost !== undefined && cost !== null) {
|
|
52
|
+
throw getError({
|
|
53
|
+
message: `[CredentialHelper.hash] Invalid cost | cost applies to bcrypt only | Pass algorithm: 'bcrypt' to keep a bcrypt cost, or drop cost for argon2id`,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return Bun.password.hash(password, { algorithm: 'argon2id' });
|
|
57
|
+
}
|
|
58
|
+
const bcryptCost = cost ?? DEFAULT_COST;
|
|
59
|
+
if (!Number.isInteger(bcryptCost) || bcryptCost < MIN_COST || bcryptCost > MAX_COST) {
|
|
60
|
+
throw getError({
|
|
61
|
+
message: `[CredentialHelper.hash] Invalid cost | Expected an integer from ${MIN_COST} to ${MAX_COST} | Got: ${bcryptCost}`,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return Bun.password.hash(password, { algorithm: 'bcrypt', cost: bcryptCost });
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Whether `password` matches `hash`. Nothing to match is `false`: an empty or non-string password, an empty
|
|
68
|
+
* or non-string hash. A stored value that is not a password hash is a server fault (500), so a corrupt row is
|
|
69
|
+
* never answered like a wrong password, and the runtime's own message stays out of it.
|
|
70
|
+
*/
|
|
71
|
+
static async verify(opts) {
|
|
72
|
+
const { password, hash } = opts;
|
|
73
|
+
CredentialHelper.assertBun({ caller: 'verify' });
|
|
74
|
+
if (typeof password !== 'string' || password.length === 0) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
if (typeof hash !== 'string' || hash.length === 0) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
const matched = await CredentialHelper.verifyHash({ password, hash });
|
|
81
|
+
if (matched || !BCRYPT_PREFIX.test(hash)) {
|
|
82
|
+
return matched;
|
|
83
|
+
}
|
|
84
|
+
const bytes = Buffer.from(password, 'utf8');
|
|
85
|
+
if (bytes.length <= BCRYPT_MAX_PASSWORD_BYTES) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
return CredentialHelper.verifyHash({
|
|
89
|
+
password: bytes.subarray(0, BCRYPT_MAX_PASSWORD_BYTES),
|
|
90
|
+
hash,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
static async verifyHash(opts) {
|
|
94
|
+
const { password, hash } = opts;
|
|
95
|
+
try {
|
|
96
|
+
const matched = await Bun.password.verify(password, hash);
|
|
97
|
+
return matched;
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
throw getError({
|
|
101
|
+
statusCode: HTTP.ResultCodes.RS_5.InternalServerError,
|
|
102
|
+
message: '[CredentialHelper.verify] Invalid hash | The stored value is not a password hash',
|
|
103
|
+
cause: error,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
static assertBun(opts) {
|
|
108
|
+
if (typeof Bun === 'undefined') {
|
|
109
|
+
throw getError({
|
|
110
|
+
statusCode: HTTP.ResultCodes.RS_5.InternalServerError,
|
|
111
|
+
message: `[CredentialHelper.${opts.caller}] Unsupported runtime | CredentialHelper needs Bun.password`,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=credential.algorithm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential.algorithm.js","sourceRoot":"","sources":["../../../../../src/modules/crypto/algorithms/credential.algorithm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EACL,oBAAoB,EACpB,4BAA4B,GAE7B,MAAM,WAAW,CAAC;AAEnB,uDAAuD;AACvD,MAAM,aAAa,GAAG,aAAa,CAAC;AAEpC,0DAA0D;AAC1D,MAAM,yBAAyB,GAAG,EAAE,CAAC;AACrC,MAAM,QAAQ,GAAG,CAAC,CAAC;AACnB,MAAM,QAAQ,GAAG,EAAE,CAAC;AACpB,MAAM,YAAY,GAAG,EAAE,CAAC;AAExB;;;;;;;;;;;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AM,CAAC,KAAK,CAAC,UAAU,CAAC,IAG/B;QACC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC;QAEhC,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,MAAM,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;YAE1D,OAAO,OAAO,CAAC;QACjB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,QAAQ,CAAC;gBACb,UAAU,EAAE,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,mBAAmB;gBACrD,OAAO,EAAE,kFAAkF;gBAC3F,KAAK,EAAE,KAAK;aACb,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAEO,MAAM,CAAC,SAAS,CAAC,IAAwB;QAC/C,IAAI,OAAO,GAAG,KAAK,WAAW,EAAE,CAAC;YAC/B,MAAM,QAAQ,CAAC;gBACb,UAAU,EAAE,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,mBAAmB;gBACrD,OAAO,EAAE,qBAAqB,IAAI,CAAC,MAAM,6DAA6D;aACvG,CAAC,CAAC;QACL,CAAC;IACH,CAAC;CACF","sourcesContent":["import { HTTP } from '@/common/constants';\nimport { getError } from '@/modules/error';\nimport {\n CredentialAlgorithms,\n DEFAULT_CREDENTIAL_ALGORITHM,\n type TCredentialAlgorithm,\n} from '../common';\n\n/** The bcrypt versions the `bcrypt` package writes. */\nconst BCRYPT_PREFIX = /^\\$2[aby]\\$/;\n\n/** bcrypt reads only the first 72 bytes of a password. */\nconst BCRYPT_MAX_PASSWORD_BYTES = 72;\nconst MIN_COST = 4;\nconst MAX_COST = 31;\nconst DEFAULT_COST = 10;\n\n/**\n * Storing and checking a credential (a password) over `Bun.password`. `hash` writes an argon2id hash with\n * Bun's default parameters unless `algorithm: 'bcrypt'` asks for bcrypt (`$2b$`, 60 characters, the stored\n * format of the `bcrypt` package). `verify` reads every existing bcrypt and argon2 hash, whichever\n * algorithm `hash` wrote, so a stored password is never rehashed or migrated. Needs Bun: it throws on\n * another runtime.\n *\n * Passwords longer than 72 bytes are where Bun and the `bcrypt` package differ, and it only matters for\n * bcrypt. The package keeps only the first 72 bytes; Bun keeps all of them, by hashing a longer password\n * before bcrypt sees it. Two things follow, and they are the whole truth about \"the same format\":\n *\n * - A hash `hash` writes for a password longer than 72 bytes is read by Bun only. The `bcrypt` package\n * cannot verify it, so rolling back to that package, or another service reading the same column, locks\n * those users out. Up to 72 bytes the two read each other's hashes.\n * - A hash the `bcrypt` package wrote for a longer password fails a plain `Bun.password.verify` with the full\n * password. `verify` therefore retries a bcrypt hash with the first 72 bytes when the password is longer.\n * That retry is for the legacy hashes. A hash `hash` wrote never needs it, because Bun reads its own hash on\n * the first attempt. The retry has one price: a hash of a password of exactly 72 bytes also accepts that\n * password followed by anything, the way bcrypt itself always has.\n */\nexport class CredentialHelper {\n /**\n * A hash of `password`: argon2id by default (Bun's default parameters), bcrypt with `algorithm: 'bcrypt'`.\n * For bcrypt, `cost` (4 to 31, default 10) is the work factor, `2 ** cost` rounds. `cost` with argon2id\n * is refused rather than ignored: a caller who tuned a cost is told the default changed.\n */\n static async hash(opts: {\n password: string;\n algorithm?: TCredentialAlgorithm;\n cost?: number;\n }): Promise<string> {\n const { password, algorithm = DEFAULT_CREDENTIAL_ALGORITHM, cost } = opts;\n\n CredentialHelper.assertBun({ caller: 'hash' });\n\n if (typeof password !== 'string' || password.length === 0) {\n throw getError({\n message: '[CredentialHelper.hash] Invalid password | Expected a non-empty string',\n });\n }\n\n if (typeof algorithm !== 'string' || !CredentialAlgorithms.isValid(algorithm)) {\n throw getError({\n message: `[CredentialHelper.hash] Invalid algorithm | Expected one of ${[...CredentialAlgorithms.SCHEME_SET].join(', ')} | Got: ${String(algorithm)}`,\n });\n }\n\n if (algorithm === CredentialAlgorithms.ARGON2ID) {\n if (cost !== undefined && cost !== null) {\n throw getError({\n message: `[CredentialHelper.hash] Invalid cost | cost applies to bcrypt only | Pass algorithm: 'bcrypt' to keep a bcrypt cost, or drop cost for argon2id`,\n });\n }\n\n return Bun.password.hash(password, { algorithm: 'argon2id' });\n }\n\n const bcryptCost = cost ?? DEFAULT_COST;\n\n if (!Number.isInteger(bcryptCost) || bcryptCost < MIN_COST || bcryptCost > MAX_COST) {\n throw getError({\n message: `[CredentialHelper.hash] Invalid cost | Expected an integer from ${MIN_COST} to ${MAX_COST} | Got: ${bcryptCost}`,\n });\n }\n\n return Bun.password.hash(password, { algorithm: 'bcrypt', cost: bcryptCost });\n }\n\n /**\n * Whether `password` matches `hash`. Nothing to match is `false`: an empty or non-string password, an empty\n * or non-string hash. A stored value that is not a password hash is a server fault (500), so a corrupt row is\n * never answered like a wrong password, and the runtime's own message stays out of it.\n */\n static async verify(opts: { password: string; hash: string }): Promise<boolean> {\n const { password, hash } = opts;\n\n CredentialHelper.assertBun({ caller: 'verify' });\n\n if (typeof password !== 'string' || password.length === 0) {\n return false;\n }\n\n if (typeof hash !== 'string' || hash.length === 0) {\n return false;\n }\n\n const matched = await CredentialHelper.verifyHash({ password, hash });\n\n if (matched || !BCRYPT_PREFIX.test(hash)) {\n return matched;\n }\n\n const bytes = Buffer.from(password, 'utf8');\n\n if (bytes.length <= BCRYPT_MAX_PASSWORD_BYTES) {\n return false;\n }\n\n return CredentialHelper.verifyHash({\n password: bytes.subarray(0, BCRYPT_MAX_PASSWORD_BYTES),\n hash,\n });\n }\n\n private static async verifyHash(opts: {\n password: string | Uint8Array;\n hash: string;\n }): Promise<boolean> {\n const { password, hash } = opts;\n\n try {\n const matched = await Bun.password.verify(password, hash);\n\n return matched;\n } catch (error) {\n throw getError({\n statusCode: HTTP.ResultCodes.RS_5.InternalServerError,\n message: '[CredentialHelper.verify] Invalid hash | The stored value is not a password hash',\n cause: error,\n });\n }\n }\n\n private static assertBun(opts: { caller: string }): void {\n if (typeof Bun === 'undefined') {\n throw getError({\n statusCode: HTTP.ResultCodes.RS_5.InternalServerError,\n message: `[CredentialHelper.${opts.caller}] Unsupported runtime | CredentialHelper needs Bun.password`,\n });\n }\n }\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AbstractCryptoAlgorithm } from './
|
|
1
|
+
import { AbstractCryptoAlgorithm } from './abstract.algorithm.js';
|
|
2
2
|
export type ECDHAlgorithmType = 'ecdh-p256';
|
|
3
3
|
export interface IECDHEncryptedPayload {
|
|
4
4
|
iv: string;
|
|
@@ -7,7 +7,11 @@ export interface IECDHEncryptedPayload {
|
|
|
7
7
|
export interface IECDHExtraOptions {
|
|
8
8
|
additionalData?: string;
|
|
9
9
|
}
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* ECDH P-256 key exchange with HKDF-derived AES-256-GCM session encryption. It runs in a browser or a Worker as
|
|
12
|
+
* well as on the server: `crypto.subtle`, `btoa` and `atob` only, no `Buffer` and no `node:*`, so it rides
|
|
13
|
+
* `@venizia/ignis-helpers/core`. Server and client both use this class and speak one wire format.
|
|
14
|
+
*/
|
|
11
15
|
export declare class ECDH extends AbstractCryptoAlgorithm<ECDHAlgorithmType, string, IECDHEncryptedPayload, CryptoKey, Promise<IECDHEncryptedPayload>, Promise<string>, IECDHExtraOptions> {
|
|
12
16
|
private static readonly encoder;
|
|
13
17
|
private static readonly decoder;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ecdh.algorithm.d.ts","sourceRoot":"","sources":["../../../../../src/modules/crypto/algorithms/ecdh.algorithm.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"ecdh.algorithm.d.ts","sourceRoot":"","sources":["../../../../../src/modules/crypto/algorithms/ecdh.algorithm.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAW/D,MAAM,MAAM,iBAAiB,GAAG,WAAW,CAAC;AAE5C,MAAM,WAAW,qBAAqB;IACpC,EAAE,EAAE,MAAM,CA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