-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.js
More file actions
354 lines (296 loc) · 9.71 KB
/
crypto.js
File metadata and controls
354 lines (296 loc) · 9.71 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
require('dotenv').config();
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
// Security constants
const KEY_FILE = path.join(__dirname, '.encryption_key');
const ALGORITHM = 'aes-256-gcm'; // Authenticated encryption
const IV_LENGTH = 16;
const AUTH_TAG_LENGTH = 16;
const SALT_LENGTH = 32;
const KEY_LENGTH = 32;
const PBKDF2_ITERATIONS = 100000; // Strong key derivation
// Secure file permissions (600 on Unix)
const SECURE_FILE_MODE = 0o600;
/**
* Securely generates cryptographically random bytes
*/
function secureRandomBytes(length) {
return crypto.randomBytes(length);
}
/**
* Derives a key from a password using PBKDF2
*/
function deriveKey(password, salt) {
return crypto.pbkdf2Sync(password, salt, PBKDF2_ITERATIONS, KEY_LENGTH, 'sha512');
}
/**
* Generates a secure encryption key with proper entropy validation
*/
function generateSecureKey() {
const key = secureRandomBytes(KEY_LENGTH);
// Validate entropy (basic check)
const uniqueBytes = new Set(key).size;
if (uniqueBytes < KEY_LENGTH / 2) {
// Retry if entropy is too low
return generateSecureKey();
}
return key;
}
/**
* Securely writes data to file with restricted permissions
*/
function secureWriteFile(filePath, data) {
// Write to temp file first, then rename (atomic operation)
const tempPath = `${filePath}.tmp.${Date.now()}`;
try {
fs.writeFileSync(tempPath, data, { mode: SECURE_FILE_MODE });
fs.renameSync(tempPath, filePath);
// Set permissions explicitly (for existing files)
if (process.platform !== 'win32') {
fs.chmodSync(filePath, SECURE_FILE_MODE);
}
} catch (error) {
// Clean up temp file if it exists
try { fs.unlinkSync(tempPath); } catch {}
throw error;
}
}
/**
* Generate or load encryption key with enhanced security
*/
function getEncryptionKey() {
// Priority 1: Environment variable (for production deployments)
if (process.env.ENCRYPTION_KEY) {
const envKey = process.env.ENCRYPTION_KEY;
// Validate key format and length
if (!/^[a-fA-F0-9]{64}$/.test(envKey)) {
console.error('❌ ENCRYPTION_KEY must be 64 hex characters (256-bit key)');
process.exit(1);
}
return Buffer.from(envKey, 'hex');
}
// Priority 2: Load from secure key file
if (fs.existsSync(KEY_FILE)) {
try {
const keyData = fs.readFileSync(KEY_FILE, 'utf8').trim();
// Validate stored key
if (!/^[a-fA-F0-9]{64}$/.test(keyData)) {
console.error('❌ Corrupted key file detected. Regenerating...');
fs.unlinkSync(KEY_FILE);
} else {
return Buffer.from(keyData, 'hex');
}
} catch (error) {
console.error('❌ Failed to read key file:', error.message);
}
}
// Generate new key
const newKey = generateSecureKey();
try {
secureWriteFile(KEY_FILE, newKey.toString('hex'));
console.log('🔐 Generated new 256-bit encryption key');
console.log('⚠️ IMPORTANT: Back up your .encryption_key file securely!');
} catch (error) {
console.error('❌ Failed to save encryption key:', error.message);
console.log('⚠️ Using in-memory key (data will be lost on restart)');
}
return newKey;
}
const ENCRYPTION_KEY = getEncryptionKey();
/**
* Encrypts text using AES-256-GCM (authenticated encryption)
* Format: version:salt:iv:authTag:ciphertext
* @param {string} text - Plain text to encrypt
* @returns {string} - Encrypted string with all components
*/
function encrypt(text) {
if (typeof text !== 'string') {
throw new Error('Input must be a string');
}
const version = '2'; // Encryption version for future upgrades
const iv = secureRandomBytes(IV_LENGTH);
const salt = secureRandomBytes(SALT_LENGTH);
// Derive unique key for this encryption using salt
const derivedKey = deriveKey(ENCRYPTION_KEY, salt);
const cipher = crypto.createCipheriv(ALGORITHM, derivedKey, iv, {
authTagLength: AUTH_TAG_LENGTH
});
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
const authTag = cipher.getAuthTag();
// Format: version:salt:iv:authTag:ciphertext
return [
version,
salt.toString('hex'),
iv.toString('hex'),
authTag.toString('hex'),
encrypted
].join(':');
}
/**
* Decrypts text encrypted with AES-256-GCM
* @param {string} encryptedText - Encrypted string from encrypt()
* @returns {string} - Decrypted plain text
*/
function decrypt(encryptedText) {
if (typeof encryptedText !== 'string') {
throw new Error('Input must be a string');
}
const parts = encryptedText.split(':');
// Support for v1 (legacy CBC format)
if (parts.length === 2) {
return decryptLegacy(encryptedText);
}
// v2 GCM format
if (parts.length !== 5) {
throw new Error('Invalid encrypted data format');
}
const [version, saltHex, ivHex, authTagHex, ciphertext] = parts;
if (version !== '2') {
throw new Error(`Unsupported encryption version: ${version}`);
}
const salt = Buffer.from(saltHex, 'hex');
const iv = Buffer.from(ivHex, 'hex');
const authTag = Buffer.from(authTagHex, 'hex');
// Derive the same key using salt
const derivedKey = deriveKey(ENCRYPTION_KEY, salt);
const decipher = crypto.createDecipheriv(ALGORITHM, derivedKey, iv, {
authTagLength: AUTH_TAG_LENGTH
});
decipher.setAuthTag(authTag);
let decrypted = decipher.update(ciphertext, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
/**
* Decrypts legacy v1 CBC format (backwards compatibility)
*/
function decryptLegacy(encryptedText) {
const parts = encryptedText.split(':');
const iv = Buffer.from(parts[0], 'hex');
const ciphertext = parts[1];
const decipher = crypto.createDecipheriv('aes-256-cbc', ENCRYPTION_KEY, iv);
let decrypted = decipher.update(ciphertext, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
/**
* Generates a secure random token
* @param {number} length - Token length in bytes
* @returns {string} - Hex-encoded token
*/
function generateToken(length = 32) {
return secureRandomBytes(length).toString('hex');
}
/**
* Creates a secure hash of data using SHA-512
* @param {string} data - Data to hash
* @param {string} salt - Optional salt (generated if not provided)
* @returns {object} - { hash, salt }
*/
function secureHash(data, salt = null) {
const useSalt = salt || secureRandomBytes(SALT_LENGTH).toString('hex');
const hash = crypto
.createHmac('sha512', useSalt)
.update(data)
.digest('hex');
return { hash, salt: useSalt };
}
/**
* Verifies data against a hash
* @param {string} data - Data to verify
* @param {string} hash - Expected hash
* @param {string} salt - Salt used in hashing
* @returns {boolean} - True if valid
*/
function verifyHash(data, hash, salt) {
const computed = secureHash(data, salt);
return crypto.timingSafeEqual(
Buffer.from(computed.hash, 'hex'),
Buffer.from(hash, 'hex')
);
}
/**
* Securely compares two strings in constant time
* @param {string} a - First string
* @param {string} b - Second string
* @returns {boolean} - True if equal
*/
function secureCompare(a, b) {
if (typeof a !== 'string' || typeof b !== 'string') {
return false;
}
const bufA = Buffer.from(a);
const bufB = Buffer.from(b);
if (bufA.length !== bufB.length) {
// Prevent timing attack by still doing comparison
crypto.timingSafeEqual(bufA, bufA);
return false;
}
return crypto.timingSafeEqual(bufA, bufB);
}
/**
* Encrypts an object (serializes to JSON first)
* @param {object} obj - Object to encrypt
* @returns {string} - Encrypted string
*/
function encryptObject(obj) {
return encrypt(JSON.stringify(obj));
}
/**
* Decrypts to an object
* @param {string} encryptedText - Encrypted string
* @returns {object} - Decrypted object
*/
function decryptObject(encryptedText) {
return JSON.parse(decrypt(encryptedText));
}
/**
* Migrates old v1 encrypted data to v2 format
* @param {string} oldEncrypted - v1 encrypted data
* @returns {string} - v2 encrypted data
*/
function migrateEncryption(oldEncrypted) {
try {
const decrypted = decrypt(oldEncrypted);
return encrypt(decrypted);
} catch (error) {
throw new Error('Failed to migrate encryption: ' + error.message);
}
}
/**
* Validates that the encryption system is working correctly
* @returns {boolean} - True if validation passes
*/
function validateEncryption() {
const testData = 'encryption-validation-test-' + Date.now();
try {
const encrypted = encrypt(testData);
const decrypted = decrypt(encrypted);
if (decrypted !== testData) {
console.error('❌ Encryption validation failed: data mismatch');
return false;
}
console.log('✅ Encryption system validated successfully');
return true;
} catch (error) {
console.error('❌ Encryption validation failed:', error.message);
return false;
}
}
// Validate on load
validateEncryption();
module.exports = {
encrypt,
decrypt,
encryptObject,
decryptObject,
generateToken,
secureHash,
verifyHash,
secureCompare,
secureRandomBytes,
migrateEncryption,
validateEncryption
};