-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.js
More file actions
444 lines (382 loc) · 10.7 KB
/
Copy pathencryption.js
File metadata and controls
444 lines (382 loc) · 10.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import process from 'node:process';
import { Buffer } from 'node:buffer';
/**
* AES-GCM Encryption Utilities
* Provides end-to-end encryption capabilities for sensitive data
*/
import crypto from 'crypto';
import logger from './logger.js';
// Encryption configuration
const ALGORITHM = 'aes-256-gcm';
const KEY_LENGTH = 32; // 256 bits
const IV_LENGTH = 12; // 96 bits for GCM
const TAG_LENGTH = 16; // 128 bits
const SALT_LENGTH = 32; // 256 bits for key derivation
// Master encryption key from environment
const MASTER_KEY = process.env.MASTER_ENCRYPTION_KEY
? Buffer.from(process.env.MASTER_ENCRYPTION_KEY, 'base64')
: crypto.randomBytes(KEY_LENGTH);
if (!process.env.MASTER_ENCRYPTION_KEY) {
logger.warn('No MASTER_ENCRYPTION_KEY found in environment. Using randomly generated key.');
logger.warn('Generated key (base64):', MASTER_KEY.toString('base64'));
}
/**
* Generate a cryptographically secure random key
*/
export function generateKey() {
return crypto.randomBytes(KEY_LENGTH);
}
/**
* Derive key from password using PBKDF2
*/
export function deriveKey(password, salt, iterations = 100000) {
if (typeof password === 'string') {
password = Buffer.from(password, 'utf8');
}
return crypto.pbkdf2Sync(password, salt, iterations, KEY_LENGTH, 'sha256');
}
/**
* Generate salt for key derivation
*/
export function generateSalt() {
return crypto.randomBytes(SALT_LENGTH);
}
/**
* Encrypt data using AES-256-GCM
*/
export function encrypt(plaintext, key = MASTER_KEY, additionalData = null) {
try {
// Convert string to buffer if needed
const plaintextBuffer = typeof plaintext === 'string'
? Buffer.from(plaintext, 'utf8')
: plaintext;
// Generate random IV
const iv = crypto.randomBytes(IV_LENGTH);
// Create cipher
const cipher = crypto.createCipher(ALGORITHM, key, { authTagLength: TAG_LENGTH });
cipher.setAutoPadding(false);
// Set IV
const cipherGcm = crypto.createCipheriv(ALGORITHM, key, iv);
// Add additional authenticated data if provided
if (additionalData) {
cipherGcm.setAAD(Buffer.from(additionalData, 'utf8'));
}
// Encrypt the data
const encrypted = Buffer.concat([
cipherGcm.update(plaintextBuffer),
cipherGcm.final()
]);
// Get authentication tag
const authTag = cipherGcm.getAuthTag();
// Return encrypted data with metadata
return {
encrypted: encrypted.toString('base64'),
iv: iv.toString('base64'),
authTag: authTag.toString('base64'),
algorithm: ALGORITHM,
keyLength: KEY_LENGTH,
additionalData: additionalData || null
};
} catch (error) {
logger.error('Encryption failed:', error);
throw new Error('Encryption failed: ' + error.message);
}
}
/**
* Decrypt data using AES-256-GCM
*/
export function decrypt(encryptedData, key = MASTER_KEY) {
try {
const {
encrypted,
iv,
authTag,
algorithm,
additionalData
} = encryptedData;
// Validate algorithm
if (algorithm !== ALGORITHM) {
throw new Error(`Unsupported algorithm: ${algorithm}`);
}
// Convert from base64
const encryptedBuffer = Buffer.from(encrypted, 'base64');
const ivBuffer = Buffer.from(iv, 'base64');
const authTagBuffer = Buffer.from(authTag, 'base64');
// Create decipher
const decipher = crypto.createDecipheriv(algorithm, key, ivBuffer);
// Set authentication tag
decipher.setAuthTag(authTagBuffer);
// Add additional authenticated data if it was used
if (additionalData) {
decipher.setAAD(Buffer.from(additionalData, 'utf8'));
}
// Decrypt the data
const decrypted = Buffer.concat([
decipher.update(encryptedBuffer),
decipher.final()
]);
return decrypted;
} catch (error) {
logger.error('Decryption failed:', error);
throw new Error('Decryption failed: ' + error.message);
}
}
/**
* Encrypt string and return as string
*/
export function encryptString(plaintext, key = MASTER_KEY, additionalData = null) {
const encrypted = encrypt(plaintext, key, additionalData);
return JSON.stringify(encrypted);
}
/**
* Decrypt string from encrypted string
*/
export function decryptString(encryptedString, key = MASTER_KEY) {
try {
const encryptedData = JSON.parse(encryptedString);
const decrypted = decrypt(encryptedData, key);
return decrypted.toString('utf8');
} catch (error) {
logger.error('String decryption failed:', error);
throw new Error('String decryption failed: ' + error.message);
}
}
/**
* Encrypt object (serializes to JSON first)
*/
export function encryptObject(obj, key = MASTER_KEY, additionalData = null) {
try {
const jsonString = JSON.stringify(obj);
return encrypt(jsonString, key, additionalData);
} catch (error) {
logger.error('Object encryption failed:', error);
throw new Error('Object encryption failed: ' + error.message);
}
}
/**
* Decrypt object (deserializes from JSON)
*/
export function decryptObject(encryptedData, key = MASTER_KEY) {
try {
const decrypted = decrypt(encryptedData, key);
return JSON.parse(decrypted.toString('utf8'));
} catch (error) {
logger.error('Object decryption failed:', error);
throw new Error('Object decryption failed: ' + error.message);
}
}
/**
* Generate encryption key pair for users
*/
export function generateUserKeyPair(password, userSalt = null) {
const salt = userSalt || generateSalt();
const key = deriveKey(password, salt);
return {
key: key.toString('base64'),
salt: salt.toString('base64'),
algorithm: ALGORITHM,
iterations: 100000
};
}
/**
* Hybrid encryption: encrypt data with random key, then encrypt key with user key
*/
export function hybridEncrypt(plaintext, userKey, additionalData = null) {
try {
// Generate random data encryption key
const dataKey = generateKey();
// Encrypt the data with the random key
const encryptedData = encrypt(plaintext, dataKey, additionalData);
// Encrypt the data key with the user key
const encryptedDataKey = encrypt(dataKey, userKey);
return {
data: encryptedData,
key: encryptedDataKey,
type: 'hybrid'
};
} catch (error) {
logger.error('Hybrid encryption failed:', error);
throw new Error('Hybrid encryption failed: ' + error.message);
}
}
/**
* Hybrid decryption: decrypt key first, then decrypt data
*/
export function hybridDecrypt(encryptedHybrid, userKey) {
try {
const { data, key } = encryptedHybrid;
// Decrypt the data key
const dataKey = decrypt(key, userKey);
// Decrypt the data with the recovered key
const decryptedData = decrypt(data, dataKey);
return decryptedData;
} catch (error) {
logger.error('Hybrid decryption failed:', error);
throw new Error('Hybrid decryption failed: ' + error.message);
}
}
/**
* Secure hash using SHA-256
*/
export function hash(data, salt = null) {
const hash = crypto.createHash('sha256');
if (salt) {
hash.update(salt);
}
hash.update(typeof data === 'string' ? data : JSON.stringify(data));
return hash.digest('hex');
}
/**
* Generate HMAC signature
*/
export function generateHMAC(data, secret = MASTER_KEY) {
const hmac = crypto.createHmac('sha256', secret);
hmac.update(typeof data === 'string' ? data : JSON.stringify(data));
return hmac.digest('hex');
}
/**
* Verify HMAC signature
*/
export function verifyHMAC(data, signature, secret = MASTER_KEY) {
const expectedSignature = generateHMAC(data, secret);
return crypto.timingSafeEqual(
Buffer.from(signature, 'hex'),
Buffer.from(expectedSignature, 'hex')
);
}
/**
* Encrypt field for database storage
*/
export function encryptField(value, key = MASTER_KEY) {
if (value === null || value === undefined) {
return null;
}
try {
const encrypted = encrypt(String(value), key);
return {
encrypted: encrypted.encrypted,
iv: encrypted.iv,
authTag: encrypted.authTag,
algorithm: encrypted.algorithm
};
} catch (error) {
logger.error('Field encryption failed:', error);
throw new Error('Field encryption failed: ' + error.message);
}
}
/**
* Decrypt field from database
*/
export function decryptField(encryptedField, key = MASTER_KEY) {
if (!encryptedField) {
return null;
}
try {
const decrypted = decrypt(encryptedField, key);
return decrypted.toString('utf8');
} catch (error) {
logger.error('Field decryption failed:', error);
throw new Error('Field decryption failed: ' + error.message);
}
}
/**
* Generate secure random token
*/
export function generateSecureToken(length = 32) {
return crypto.randomBytes(length).toString('hex');
}
/**
* Generate cryptographically secure UUID
*/
export function generateSecureUUID() {
return crypto.randomUUID();
}
/**
* Constant-time string comparison
*/
export function safeCompare(a, b) {
if (typeof a !== 'string' || typeof b !== 'string') {
return false;
}
if (a.length !== b.length) {
return false;
}
return crypto.timingSafeEqual(
Buffer.from(a, 'utf8'),
Buffer.from(b, 'utf8')
);
}
/**
* Encrypt multiple fields for batch operations
*/
export function encryptFields(fields, key = MASTER_KEY) {
const encrypted = {};
for (const [fieldName, value] of Object.entries(fields)) {
encrypted[fieldName] = encryptField(value, key);
}
return encrypted;
}
/**
* Decrypt multiple fields for batch operations
*/
export function decryptFields(encryptedFields, key = MASTER_KEY) {
const decrypted = {};
for (const [fieldName, encryptedValue] of Object.entries(encryptedFields)) {
decrypted[fieldName] = decryptField(encryptedValue, key);
}
return decrypted;
}
/**
* Key rotation utility
*/
export function rotateEncryption(encryptedData, oldKey, newKey) {
try {
// Decrypt with old key
const plaintext = decrypt(encryptedData, oldKey);
// Re-encrypt with new key
return encrypt(plaintext, newKey, encryptedData.additionalData);
} catch (error) {
logger.error('Key rotation failed:', error);
throw new Error('Key rotation failed: ' + error.message);
}
}
/**
* Validate encryption configuration
*/
export function validateConfig() {
return {
algorithm: ALGORITHM,
keyLength: KEY_LENGTH,
ivLength: IV_LENGTH,
tagLength: TAG_LENGTH,
saltLength: SALT_LENGTH,
masterKeyPresent: !!process.env.MASTER_ENCRYPTION_KEY,
isSecure: MASTER_KEY.length === KEY_LENGTH
};
}
export default {
encrypt,
decrypt,
encryptString,
decryptString,
encryptObject,
decryptObject,
encryptField,
decryptField,
encryptFields,
decryptFields,
hybridEncrypt,
hybridDecrypt,
generateKey,
deriveKey,
generateSalt,
generateUserKeyPair,
hash,
generateHMAC,
verifyHMAC,
generateSecureToken,
generateSecureUUID,
safeCompare,
rotateEncryption,
validateConfig
};