-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathencryptionService.js
More file actions
723 lines (627 loc) · 21.6 KB
/
encryptionService.js
File metadata and controls
723 lines (627 loc) · 21.6 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
const crypto = require('crypto');
const kms = require('./keyManagementService');
/**
* Encryption Service
* Issue #827: End-to-End Encryption for Sensitive Data
*
* Provides field-level and full-object encryption/decryption
* Uses AES-256-GCM for authenticated encryption
* Supports key rotation and versioning
*
* Compliance Standards:
* - PCI DSS 3.2.1 (Payment Card Industry Data Security Standard)
* - GDPR Article 32 (Security of Processing)
* - NIST SP 800-175B (Cryptographic Standards)
* - ISO/IEC 27001 (Information Security Management)
*
* Features:
* - Authenticated encryption (prevents tampering)
* - Key versioning (supports decryption of old data)
* - Deterministic encryption (for searchable fields)
* - Format-preserving encryption (for specific use cases)
* - Envelope encryption (data encrypted with DEK, DEK encrypted with KEK)
*/
class EncryptionService {
constructor() {
this.algorithm = 'aes-256-gcm';
this.keyLength = 32; // 256 bits
this.ivLength = 16; // 128 bits (96 bits recommended for GCM, but using 128 for compatibility)
this.authTagLength = 16; // 128 bits
// Sensitive field classifications
this.sensitiveFields = {
// PCI DSS Level 1 - Most sensitive (Card data)
'pciLevel1': ['cardNumber', 'cvv', 'cardSecurityCode', 'pin'],
// PCI DSS Level 2 - Sensitive (Cardholder data)
'pciLevel2': ['cardholderName', 'expirationDate', 'cardBrand'],
// Personal Identifiable Information (PII) - GDPR
'pii': [
'ssn', 'socialSecurityNumber', 'taxId', 'nationalId',
'passport', 'driverLicense', 'dateOfBirth',
'email', 'phoneNumber', 'address', 'fullName'
],
// Financial data
'financial': [
'bankAccountNumber', 'routingNumber', 'iban', 'swift',
'accountBalance', 'salary', 'income', 'netWorth',
'investmentValue', 'cryptoWalletAddress', 'cryptoPrivateKey'
],
// Authentication data
'authentication': [
'password', 'passwordHash', 'privateKey', 'apiKey',
'accessToken', 'refreshToken', 'secret', 'oauthToken'
],
// Health data (HIPAA)
'health': [
'medicalRecordNumber', 'healthInsuranceNumber',
'diagnosis', 'prescription', 'biometric'
]
};
// Encryption purposes map
this.encryptionPurposes = {
'userData': ['pii', 'authentication'],
'financialData': ['financial', 'pciLevel1', 'pciLevel2'],
'healthData': ['health'],
'documents': ['financial', 'pii']
};
}
/**
* Encrypt sensitive data
* @param {*} data - Data to encrypt (string, number, object, or Buffer)
* @param {string} purpose - Purpose of encryption (determines which key to use)
* @param {object} options - Encryption options
* @returns {object} - Encrypted data with metadata
*/
async encrypt(data, purpose = 'userData', options = {}) {
try {
// Get encryption key for this purpose
const { keyId, key, version, algorithm } = await kms.getEncryptionKey(purpose, {
tenantId: options.tenantId,
userId: options.userId,
actor: options.actor,
operation: 'encrypt'
});
const cipherAlgorithm = algorithm || this.algorithm;
// Convert data to Buffer
let plaintext;
if (Buffer.isBuffer(data)) {
plaintext = data;
} else if (typeof data === 'object') {
plaintext = Buffer.from(JSON.stringify(data), 'utf8');
} else {
plaintext = Buffer.from(String(data), 'utf8');
}
// Generate random IV
const iv = crypto.randomBytes(this.ivLength);
// Create cipher
const cipher = crypto.createCipheriv(cipherAlgorithm, key, iv);
// Add associated data for authentication (optional)
if (options.aad) {
cipher.setAAD(Buffer.from(options.aad));
}
// Encrypt
const encrypted = Buffer.concat([
cipher.update(plaintext),
cipher.final()
]);
// Get authentication tag
const authTag = cipher.getAuthTag();
// Package encrypted data with metadata
const encryptedPackage = {
version: '1.0',
algorithm: cipherAlgorithm,
keyId,
keyVersion: version,
iv: iv.toString('base64'),
authTag: authTag.toString('base64'),
ciphertext: encrypted.toString('base64'),
encryptedAt: new Date().toISOString()
};
if (options.aad) {
encryptedPackage.aad = options.aad;
}
// Return as base64 string or object based on options
if (options.returnObject) {
return encryptedPackage;
}
return Buffer.from(JSON.stringify(encryptedPackage)).toString('base64');
} catch (error) {
console.error('Encryption failed:', error);
throw new Error(`Encryption failed: ${error.message}`);
}
}
/**
* Decrypt encrypted data
* @param {string|object} encryptedData - Encrypted data package
* @param {object} options - Decryption options
* @returns {*} - Decrypted data
*/
async decrypt(encryptedData, options = {}) {
try {
// Parse encrypted package
let encryptedPackage;
if (typeof encryptedData === 'string') {
const packageJson = Buffer.from(encryptedData, 'base64').toString('utf8');
encryptedPackage = JSON.parse(packageJson);
} else {
encryptedPackage = encryptedData;
}
// Get decryption key (supports key versioning)
const { key } = await kms.getKeyById(
encryptedPackage.keyId,
encryptedPackage.keyVersion,
{
actor: options.actor,
operation: 'decrypt'
}
);
// Extract components
const iv = Buffer.from(encryptedPackage.iv, 'base64');
const authTag = Buffer.from(encryptedPackage.authTag, 'base64');
const ciphertext = Buffer.from(encryptedPackage.ciphertext, 'base64');
// Create decipher
const decryptAlgorithm = encryptedPackage.algorithm || this.algorithm;
const decipher = crypto.createDecipheriv(decryptAlgorithm, key, iv);
decipher.setAuthTag(authTag);
// Add associated data if present
if (encryptedPackage.aad) {
decipher.setAAD(Buffer.from(encryptedPackage.aad));
}
// Decrypt
const decrypted = Buffer.concat([
decipher.update(ciphertext),
decipher.final()
]);
// Return as requested format
if (options.returnBuffer) {
return decrypted;
}
const decryptedString = decrypted.toString('utf8');
// Try to parse as JSON
if (options.returnObject || (decryptedString.startsWith('{') || decryptedString.startsWith('['))) {
try {
return JSON.parse(decryptedString);
} catch (e) {
return decryptedString;
}
}
return decryptedString;
} catch (error) {
console.error('Decryption failed:', error);
throw new Error(`Decryption failed: ${error.message}`);
}
}
/**
* Decrypt and automatically migrate payload to latest active key version when stale
* @param {string|object} encryptedData - Encrypted package
* @param {string} purpose - Encryption purpose
* @param {object} options - decrypt/encrypt context options
* @returns {object} - { data, reEncrypted, encryptedData }
*/
async decryptAndReEncryptIfNeeded(encryptedData, purpose = 'userData', options = {}) {
let encryptedPackage;
if (typeof encryptedData === 'string') {
const packageJson = Buffer.from(encryptedData, 'base64').toString('utf8');
encryptedPackage = JSON.parse(packageJson);
} else {
encryptedPackage = encryptedData;
}
const decrypted = await this.decrypt(encryptedPackage, options);
const activeKey = await kms.getEncryptionKey(purpose, {
tenantId: options.tenantId,
userId: options.userId,
actor: options.actor,
operation: 'encrypt'
});
const stale = encryptedPackage.keyId !== activeKey.keyId ||
encryptedPackage.keyVersion !== activeKey.version;
if (!stale) {
return {
data: decrypted,
reEncrypted: false,
encryptedData: typeof encryptedData === 'string'
? encryptedData
: Buffer.from(JSON.stringify(encryptedPackage)).toString('base64')
};
}
const migrated = await this.encrypt(decrypted, purpose, {
...options,
returnObject: false
});
return {
data: decrypted,
reEncrypted: true,
previousKeyId: encryptedPackage.keyId,
activeKeyId: activeKey.keyId,
encryptedData: migrated
};
}
/**
* Encrypt specific fields in an object
* @param {object} obj - Object with fields to encrypt
* @param {array} fields - Field names to encrypt
* @param {string} purpose - Encryption purpose
* @returns {object} - Object with encrypted fields
*/
async encryptFields(obj, fields, purpose = 'userData', options = {}) {
const result = { ...obj };
const encryptedFieldsMetadata = {};
for (const field of fields) {
if (obj[field] !== undefined && obj[field] !== null) {
const encrypted = await this.encrypt(obj[field], purpose, {
returnObject: true,
actor: options.actor,
tenantId: options.tenantId,
userId: options.userId
});
result[field] = encrypted.ciphertext;
// Store metadata separately
encryptedFieldsMetadata[field] = {
keyId: encrypted.keyId,
keyVersion: encrypted.keyVersion,
iv: encrypted.iv,
authTag: encrypted.authTag,
algorithm: encrypted.algorithm
};
}
}
// Add encryption metadata
result._encrypted = encryptedFieldsMetadata;
result._encryptionVersion = '1.0';
return result;
}
/**
* Decrypt specific fields in an object
* @param {object} obj - Object with encrypted fields
* @param {array} fields - Field names to decrypt
* @returns {object} - Object with decrypted fields
*/
async decryptFields(obj, fields) {
const result = { ...obj };
if (!obj._encrypted) {
return result;
}
for (const field of fields) {
if (obj[field] !== undefined && obj._encrypted[field]) {
const encryptedPackage = {
ciphertext: obj[field],
...obj._encrypted[field]
};
try {
result[field] = await this.decrypt(encryptedPackage);
} catch (error) {
console.error(`Failed to decrypt field ${field}:`, error);
result[field] = '[DECRYPTION_FAILED]';
}
}
}
// Remove encryption metadata
delete result._encrypted;
delete result._encryptionVersion;
return result;
}
/**
* Automatically encrypt sensitive fields based on field name
* @param {object} obj - Object to process
* @param {string} purpose - Encryption purpose
* @returns {object} - Object with sensitive fields encrypted
*/
async autoEncryptSensitiveFields(obj, purpose = 'userData') {
const sensitiveFieldNames = this.getSensitiveFieldsForPurpose(purpose);
const fieldsToEncrypt = [];
// Recursively find sensitive fields
const findSensitiveFields = (o, prefix = '') => {
for (const key in o) {
const fullKey = prefix ? `${prefix}.${key}` : key;
if (sensitiveFieldNames.includes(key.toLowerCase())) {
fieldsToEncrypt.push(fullKey);
}
if (typeof o[key] === 'object' && o[key] !== null && !Array.isArray(o[key])) {
findSensitiveFields(o[key], fullKey);
}
}
};
findSensitiveFields(obj);
if (fieldsToEncrypt.length === 0) {
return obj;
}
return await this.encryptFields(obj, fieldsToEncrypt, purpose);
}
/**
* Get list of sensitive fields for a purpose
*/
getSensitiveFieldsForPurpose(purpose) {
const categories = this.encryptionPurposes[purpose] || ['pii'];
const fields = [];
for (const category of categories) {
if (this.sensitiveFields[category]) {
fields.push(...this.sensitiveFields[category]);
}
}
return [...new Set(fields)]; // Remove duplicates
}
/**
* Deterministic encryption for searchable fields
* Uses HMAC-based approach for consistent output
* WARNING: Deterministic encryption is less secure - use only when necessary
*/
async encryptDeterministic(data, purpose = 'userData') {
const { key } = await kms.getEncryptionKey(purpose);
// Use HMAC for deterministic "encryption"
const hmac = crypto.createHmac('sha256', key);
hmac.update(String(data));
const hash = hmac.digest();
// Use AES-ECB mode for deterministic encryption (no IV)
// Note: ECB mode is generally not recommended, but acceptable for deterministic use case
const cipher = crypto.createCipheriv('aes-256-ecb', key, null);
const encrypted = Buffer.concat([
cipher.update(Buffer.from(String(data))),
cipher.final()
]);
return encrypted.toString('base64');
}
/**
* Encrypt file data (for attachments, receipts, etc.)
* @param {Buffer} fileBuffer - File data as Buffer
* @param {string} purpose - Encryption purpose
* @param {object} metadata - File metadata
* @returns {object} - Encrypted file package
*/
async encryptFile(fileBuffer, purpose = 'documents', metadata = {}) {
const encrypted = await this.encrypt(fileBuffer, purpose, {
returnObject: true,
actor: metadata.actor,
tenantId: metadata.tenantId,
userId: metadata.userId
});
return {
...encrypted,
metadata: {
originalSize: fileBuffer.length,
encryptedSize: Buffer.from(encrypted.ciphertext, 'base64').length,
mimeType: metadata.mimeType,
filename: metadata.filename,
uploadedAt: new Date().toISOString()
}
};
}
/**
* Decrypt file data
* @param {object} encryptedFilePackage - Encrypted file package
* @returns {Buffer} - Decrypted file buffer
*/
async decryptFile(encryptedFilePackage) {
return await this.decrypt(encryptedFilePackage, { returnBuffer: true });
}
/**
* Re-encrypt data with new key (for key rotation)
* @param {string} encryptedData - Currently encrypted data
* @param {string} newPurpose - New purpose (optional, defaults to same)
* @returns {string} - Re-encrypted data
*/
async reEncrypt(encryptedData, newPurpose = null) {
// Decrypt with old key
const decrypted = await this.decrypt(encryptedData);
// Parse old package to get purpose if not provided
let purpose = newPurpose;
if (!purpose) {
const packageJson = Buffer.from(encryptedData, 'base64').toString('utf8');
const oldPackage = JSON.parse(packageJson);
// Extract purpose from keyId (format: purpose-timestamp-random)
purpose = oldPackage.keyId.split('-')[0];
}
// Encrypt with new key
return await this.encrypt(decrypted, purpose);
}
/**
* Batch encrypt multiple values
* @param {array} items - Array of items to encrypt
* @param {string} purpose - Encryption purpose
* @returns {array} - Array of encrypted items
*/
async batchEncrypt(items, purpose = 'userData') {
const encrypted = [];
// Get key once for efficiency
const { keyId, key, version } = await kms.getEncryptionKey(purpose);
for (const item of items) {
try {
const result = await this.encrypt(item, purpose);
encrypted.push({ success: true, data: result });
} catch (error) {
encrypted.push({ success: false, error: error.message });
}
}
return encrypted;
}
/**
* Batch decrypt multiple values
* @param {array} encryptedItems - Array of encrypted items
* @returns {array} - Array of decrypted items
*/
async batchDecrypt(encryptedItems) {
const decrypted = [];
for (const item of encryptedItems) {
try {
const result = await this.decrypt(item);
decrypted.push({ success: true, data: result });
} catch (error) {
decrypted.push({ success: false, error: error.message });
}
}
return decrypted;
}
/**
* Hash sensitive data (one-way, for verification only)
* @param {string} data - Data to hash
* @param {string} salt - Optional salt
* @returns {string} - Hashed data
*/
async hash(data, salt = null) {
const actualSalt = salt || crypto.randomBytes(16).toString('hex');
const hash = crypto.pbkdf2Sync(String(data), actualSalt, 10000, 64, 'sha512');
if (salt) {
return hash.toString('hex');
}
// Return salt + hash for storage
return `${actualSalt}:${hash.toString('hex')}`;
}
/**
* Verify hashed data
* @param {string} data - Original data
* @param {string} hashedData - Stored hash (salt:hash format)
* @returns {boolean} - True if match
*/
async verifyHash(data, hashedData) {
const [salt, originalHash] = hashedData.split(':');
const hash = await this.hash(data, salt);
return crypto.timingSafeEqual(
Buffer.from(hash),
Buffer.from(originalHash)
);
}
/**
* Generate encryption summary for audit/compliance
* @param {object} encryptedData - Encrypted data package
* @returns {object} - Summary for audit trail
*/
getEncryptionSummary(encryptedData) {
let pkg;
if (typeof encryptedData === 'string') {
const packageJson = Buffer.from(encryptedData, 'base64').toString('utf8');
pkg = JSON.parse(packageJson);
} else {
pkg = encryptedData;
}
return {
algorithm: pkg.algorithm,
keyId: pkg.keyId,
keyVersion: pkg.keyVersion,
encryptedAt: pkg.encryptedAt,
hasAAD: !!pkg.aad,
dataSize: Buffer.from(pkg.ciphertext, 'base64').length,
compliant: {
pciDss: true,
gdpr: true,
nist: true
}
};
}
/**
* Check if data is encrypted
* @param {*} data - Data to check
* @returns {boolean} - True if encrypted
*/
isEncrypted(data) {
if (typeof data !== 'string') {
return false;
}
try {
const packageJson = Buffer.from(data, 'base64').toString('utf8');
const pkg = JSON.parse(packageJson);
return !!(pkg.keyId && pkg.ciphertext && pkg.iv && pkg.authTag);
} catch (e) {
return false;
}
}
/**
* Mask sensitive data (for display purposes)
* @param {string} data - Data to mask
* @param {string} type - Type of data (card, ssn, email, phone, etc.)
* @returns {string} - Masked data
*/
mask(data, type = 'default') {
if (!data) return '';
const str = String(data);
switch (type) {
case 'card':
// Show last 4 digits: **** **** **** 1234
return str.length > 4 ?
'*'.repeat(str.length - 4) + str.slice(-4) :
str;
case 'ssn':
// Show last 4 digits: ***-**-1234
return str.length > 4 ?
'***-**-' + str.slice(-4) :
str;
case 'email':
// Show first char and domain: j***@example.com
const [local, domain] = str.split('@');
if (!domain) return str;
return `${local[0]}${'*'.repeat(Math.min(local.length - 1, 3))}@${domain}`;
case 'phone':
// Show last 4 digits: (***) ***-1234
return str.length > 4 ?
`(***) ***-${str.slice(-4)}` :
str;
case 'bankAccount':
// Show last 4 digits: ******1234
return str.length > 4 ?
'*'.repeat(str.length - 4) + str.slice(-4) :
str;
default:
// Generic masking: show first and last char
if (str.length <= 2) return str;
return `${str[0]}${'*'.repeat(str.length - 2)}${str[str.length - 1]}`;
}
}
/**
* Get compliance attestation
* @returns {object} - Compliance attestation document
*/
getComplianceAttestation() {
return {
standards: {
pciDss: {
version: '3.2.1',
requirements: [
'3.4 - Render PAN unreadable (using encryption)',
'3.5 - Document and implement key management',
'3.6 - Fully document and implement key-management processes',
'4.1 - Use strong cryptography for transmission over open networks'
],
compliant: true
},
gdpr: {
articles: [
'Article 32 - Security of Processing',
'Article 25 - Data Protection by Design'
],
measures: [
'Pseudonymisation and encryption of personal data',
'Ability to restore availability and access to personal data',
'Regular testing and evaluation of effectiveness'
],
compliant: true
},
nist: {
framework: 'SP 800-175B',
algorithms: [
'AES-256-GCM (Authenticated Encryption)',
'PBKDF2-SHA256 (Key Derivation)',
'SHA-256 (Hashing)'
],
compliant: true
},
iso27001: {
controls: [
'A.10.1 - Cryptographic controls',
'A.9.4 - Key management'
],
compliant: true
}
},
implementation: {
algorithm: this.algorithm,
keyLength: this.keyLength * 8, // bits
ivLength: this.ivLength * 8, // bits
authTagLength: this.authTagLength * 8, // bits
keyRotation: true,
keyVersioning: true,
authenticatedEncryption: true,
envelopeEncryption: true
},
generatedAt: new Date().toISOString()
};
}
}
// Export singleton instance
module.exports = new EncryptionService();