-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathencryption.ts
More file actions
386 lines (350 loc) · 12.7 KB
/
Copy pathencryption.ts
File metadata and controls
386 lines (350 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import logger from "./logger";
import crypto from "crypto";
import { env } from "../config/env";
const ALGORITHM = "aes-256-gcm" as const;
const IV_LENGTH = 12; // 96-bit IV — recommended for GCM
const AUTH_TAG_LENGTH = 16; // 128-bit auth tag
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface EncryptedPayload {
iv: string; // hex
authTag: string; // hex
ciphertext: string; // hex
}
// ---------------------------------------------------------------------------
// Key derivation (HKDF-SHA-256)
// ---------------------------------------------------------------------------
/**
* Derives a 32-byte AES key from raw key material using HKDF-SHA-256.
* Deterministic — same inputs always produce the same key.
*
* @param keyMaterial Raw key material (env var value or per-user secret)
* @param info Context label that domain-separates derived keys
*/
export function deriveKey(
keyMaterial: string,
info = "pii-encryption",
): Buffer {
const ikm = Buffer.from(keyMaterial, "utf8");
// HKDF extract
const prk = crypto
.createHmac("sha256", "mobile-money-hkdf-salt")
.update(ikm)
.digest();
// HKDF expand (single block — 32 bytes is exactly one SHA-256 output)
const infoBuffer = Buffer.from(info, "utf8");
const t = crypto
.createHmac("sha256", prk)
.update(Buffer.concat([infoBuffer, Buffer.from([1])]))
.digest();
return t.subarray(0, 32);
}
/**
* Derives a per-user AES-256 key by mixing the master key with the user ID.
* Isolates breach impact — compromising one user's key doesn't affect others.
*/
export function deriveUserKey(userId: string): Buffer {
const masterKey = process.env.DB_ENCRYPTION_KEY || env.DB_ENCRYPTION_KEY;
return deriveKey(masterKey, `pii-user-${userId}`);
}
// ---------------------------------------------------------------------------
// Core AES-256-GCM primitives
// ---------------------------------------------------------------------------
/**
* Encrypts plaintext with AES-256-GCM.
* A fresh random IV is generated for every call — IVs are NEVER reused.
*
* @param plaintext The string to encrypt
* @param key 32-byte Buffer (use deriveKey / deriveUserKey)
*/
export function encryptAES(plaintext: string, key: Buffer): EncryptedPayload {
if (key.length !== 32) {
throw new Error("Encryption key must be exactly 32 bytes for AES-256-GCM");
}
const iv = crypto.randomBytes(IV_LENGTH);
const cipher = crypto.createCipheriv(ALGORITHM, key, iv, {
authTagLength: AUTH_TAG_LENGTH,
});
const ciphertext = Buffer.concat([
cipher.update(plaintext, "utf8"),
cipher.final(),
]);
const authTag = cipher.getAuthTag();
return {
iv: iv.toString("hex"),
authTag: authTag.toString("hex"),
ciphertext: ciphertext.toString("hex"),
};
}
/**
* Decrypts an EncryptedPayload produced by `encryptAES()`.
* Throws a clear, catchable error on authentication failure — never silently returns garbage.
*
* @param payload The EncryptedPayload to decrypt
* @param key The same 32-byte Buffer used during encryption
*/
export function decryptAES(payload: EncryptedPayload, key: Buffer): string {
if (key.length !== 32) {
throw new Error("Decryption key must be exactly 32 bytes for AES-256-GCM");
}
const iv = Buffer.from(payload.iv, "hex");
const authTag = Buffer.from(payload.authTag, "hex");
const ciphertext = Buffer.from(payload.ciphertext, "hex");
const decipher = crypto.createDecipheriv(ALGORITHM, key, iv, {
authTagLength: AUTH_TAG_LENGTH,
});
decipher.setAuthTag(authTag);
try {
const plaintext = Buffer.concat([
decipher.update(ciphertext),
decipher.final(),
]);
return plaintext.toString("utf8");
} catch {
// Never log the key or ciphertext — only surface a safe message
throw new Error(
"PII decryption failed: authentication tag mismatch. The data may be corrupt or the wrong key was used.",
);
}
}
// ---------------------------------------------------------------------------
// Serialisation helpers — store EncryptedPayload as a single TEXT column
// Format: <iv_hex>:<authTag_hex>:<ciphertext_hex>
// ---------------------------------------------------------------------------
export function serializePayload(payload: EncryptedPayload): string {
return `${payload.iv}:${payload.authTag}:${payload.ciphertext}`;
}
export function deserializePayload(raw: string): EncryptedPayload {
const parts = raw.split(":");
if (parts.length !== 3) {
throw new Error(
"Invalid encrypted payload format — expected iv:authTag:ciphertext",
);
}
const [iv, authTag, ciphertext] = parts;
return { iv, authTag, ciphertext };
}
// ---------------------------------------------------------------------------
// Dynamic key rotation & registry helpers
// ---------------------------------------------------------------------------
/**
* Dynamically resolves all configured encryption keys from process.env and env.ts.
* Supports:
* - DB_ENCRYPTION_KEY (fallback/legacy key)
* - DB_ENCRYPTION_KEY_XXX (dynamic version keys, e.g. DB_ENCRYPTION_KEY_V1)
* - DB_ENCRYPTION_KEYS (JSON string mapping versions to keys, e.g. {"v1": "key1", "v2": "key2"})
*/
export function getEncryptionKeys(): Map<string, string> {
const keys = new Map<string, string>();
// 1. Default/legacy key
const currentKey = process.env.DB_ENCRYPTION_KEY || env.DB_ENCRYPTION_KEY;
if (currentKey) {
keys.set("legacy", currentKey);
}
// 2. Fallback keys (DB_ENCRYPTION_KEYS_FALLBACK)
if (process.env.DB_ENCRYPTION_KEYS_FALLBACK) {
const fallbacks = process.env.DB_ENCRYPTION_KEYS_FALLBACK.split(",");
fallbacks.forEach((fb, idx) => {
const trimmed = fb.trim();
if (trimmed) {
keys.set(`fallback_${idx}`, trimmed);
}
});
}
// 3. Parse DB_ENCRYPTION_KEYS JSON if configured
if (process.env.DB_ENCRYPTION_KEYS) {
try {
const parsed = JSON.parse(process.env.DB_ENCRYPTION_KEYS);
for (const [ver, val] of Object.entries(parsed)) {
keys.set(ver.toLowerCase(), val as string);
}
} catch (err) {
logger.error(err, "Failed to parse DB_ENCRYPTION_KEYS JSON:");
}
}
// 4. Scan process.env for DB_ENCRYPTION_KEY_XXX
for (const [key, val] of Object.entries(process.env)) {
if (
key.startsWith("DB_ENCRYPTION_KEY_") &&
val &&
key !== "DB_ENCRYPTION_KEY_VERSION" &&
key !== "DB_ENCRYPTION_KEYS_FALLBACK"
) {
const version = key.replace("DB_ENCRYPTION_KEY_", "").toLowerCase();
keys.set(version, val);
}
}
return keys;
}
// ---------------------------------------------------------------------------
// Convenience field helpers (global key with rotation)
// ---------------------------------------------------------------------------
/**
* Encrypt a PII field with the global key.
* Respects ACTIVE_ENCRYPTION_KEY_VERSION for key/salt rotation.
* Returns null/undefined/empty as-is.
*/
export function encryptField(
value: string | null | undefined,
): string | null | undefined {
if (value == null || value === "") return value;
const keys = getEncryptionKeys();
const activeVersion = (
process.env.ACTIVE_ENCRYPTION_KEY_VERSION || ""
).toLowerCase();
// If we have an active version and a corresponding key is registered:
if (activeVersion && activeVersion !== "legacy" && keys.has(activeVersion)) {
const keyMaterial = keys.get(activeVersion)!;
const key = deriveKey(keyMaterial);
const encrypted = encryptAES(value, key);
return `${activeVersion}:${serializePayload(encrypted)}`;
}
// Fallback to legacy/default encryption
const keyMaterial = keys.get("legacy") || env.DB_ENCRYPTION_KEY;
const key = deriveKey(keyMaterial);
return serializePayload(encryptAES(value, key));
}
/**
* Decrypt a PII field with the global key.
* Detects version prefixes dynamically to select the appropriate decryption key.
* Returns null/undefined/empty as-is.
*/
export function decryptField(
raw: string | null | undefined,
): string | null | undefined {
if (raw == null || raw === "") return raw;
const parts = raw.split(":");
const keysMap = getEncryptionKeys();
// A versioned payload has format: version:iv:authTag:ciphertext (parts.length >= 4)
if (parts.length >= 4 && keysMap.has(parts[0].toLowerCase())) {
const version = parts[0].toLowerCase();
const keyMaterial = keysMap.get(version)!;
const key = deriveKey(keyMaterial);
try {
const payload = deserializePayload(parts.slice(1).join(":"));
return decryptAES(payload, key);
} catch (err) {
console.warn(
`[Encryption] Decryption failed for versioned payload '${version}'. Error: ${
err instanceof Error ? err.message : err
}`,
);
throw err;
}
}
// Fallback / multi-key trial decryption
const keys = Array.from(keysMap.values()).map((k) => deriveKey(k));
for (let i = 0; i < keys.length; i++) {
try {
return decryptAES(deserializePayload(raw), keys[i]);
} catch (err) {
if (i === keys.length - 1) {
if (raw.includes(":")) {
throw err;
}
}
}
}
return raw;
}
// ---------------------------------------------------------------------------
// Convenience field helpers (per-user key)
// ---------------------------------------------------------------------------
/** Encrypt a PII field with a per-user derived key. */
export function encryptFieldForUser(
value: string | null | undefined,
userId: string,
): string | null | undefined {
if (value == null || value === "") return value;
const key = deriveUserKey(userId);
return serializePayload(encryptAES(value, key));
}
/** Decrypt a PII field with a per-user derived key. */
export function decryptFieldForUser(
raw: string | null | undefined,
userId: string,
): string | null | undefined {
if (raw == null || raw === "") return raw;
const payload = deserializePayload(raw);
const keysMap = getEncryptionKeys();
const keys = Array.from(keysMap.values()).map((k) =>
deriveKey(k, `pii-user-${userId}`),
);
for (let i = 0; i < keys.length; i++) {
try {
return decryptAES(payload, keys[i]);
} catch (err) {
if (i === keys.length - 1) {
throw err;
}
}
}
return raw;
}
// ---------------------------------------------------------------------------
// Legacy shim — keeps existing callers (phone_number, email, 2FA) working.
// Deterministic mode preserved for indexed phone-number lookups.
// ---------------------------------------------------------------------------
const DETERMINISTIC_IV = Buffer.alloc(IV_LENGTH, 0);
/**
* @deprecated Use encryptField / encryptFieldForUser for new PII fields.
* Kept for backward-compat with phone_number, email, two_factor_secret callers.
*/
export function encrypt(
text: string | null | undefined,
deterministic = false,
): string | null | undefined {
if (text == null || text === "") return text;
const masterKey = process.env.DB_ENCRYPTION_KEY || env.DB_ENCRYPTION_KEY;
const key = deriveKey(masterKey);
const iv = deterministic ? DETERMINISTIC_IV : crypto.randomBytes(IV_LENGTH);
const cipher = crypto.createCipheriv(ALGORITHM, key, iv, {
authTagLength: AUTH_TAG_LENGTH,
});
const ciphertext = Buffer.concat([
cipher.update(text, "utf8"),
cipher.final(),
]).toString("hex");
const authTag = cipher.getAuthTag().toString("hex");
return `${iv.toString("hex")}:${authTag}:${ciphertext}`;
}
/**
* @deprecated Use decryptField / decryptFieldForUser for new PII fields.
* Kept for backward-compat with phone_number, email, two_factor_secret callers.
*/
export function decrypt(
encryptedData: string | null | undefined,
): string | null | undefined {
if (
encryptedData == null ||
encryptedData === "" ||
!encryptedData.includes(":")
)
return encryptedData;
const parts = encryptedData.split(":");
if (parts.length !== 3) return encryptedData;
const [ivHex, authTagHex, ciphertextHex] = parts;
const iv = Buffer.from(ivHex, "hex");
const authTag = Buffer.from(authTagHex, "hex");
const ciphertext = Buffer.from(ciphertextHex, "hex");
const keysMap = getEncryptionKeys();
const keys = Array.from(keysMap.values()).map((k) => deriveKey(k));
for (let i = 0; i < keys.length; i++) {
try {
const decipher = crypto.createDecipheriv(ALGORITHM, keys[i], iv, {
authTagLength: AUTH_TAG_LENGTH,
});
decipher.setAuthTag(authTag);
return Buffer.concat([
decipher.update(ciphertext),
decipher.final(),
]).toString("utf8");
} catch (err) {
if (i === keys.length - 1) {
throw new Error("PII decryption failed: authentication tag mismatch.");
}
}
}
return encryptedData;
}