diff --git a/src/app/lib/enc.js b/src/app/lib/enc.js index ce608ba9..b889766b 100644 --- a/src/app/lib/enc.js +++ b/src/app/lib/enc.js @@ -1,8 +1,23 @@ /** * data encrypt/decrypt + * + * New format (GCM): 'gcm::::' + * Legacy format: '' (pure hex, no colons — aes-192-cbc) + * + * decrypt/decryptAsync detect the format automatically via the 'gcm:' prefix, + * so old data encrypted with the static IV/salt continues to work without migration. */ -const algorithmDefault = 'aes-192-cbc' +const algorithmDefault = 'aes-256-gcm' + +// Legacy constants — kept only for decrypting old data (aes-192-cbc) +const LEGACY_ALGORITHM = 'aes-192-cbc' +const LEGACY_IV = Buffer.alloc(16, 0) +const LEGACY_SALT = 'salt' +const LEGACY_KEY_LENGTH = 24 +const IV_LENGTH = 12 // 12 bytes is recommended for GCM +const SALT_LENGTH = 16 +const KEY_LENGTH = 32 // aes-256 requires a 32-byte key function scryptAsync (...args) { const crypto = require('crypto') @@ -19,29 +34,42 @@ function scryptAsync (...args) { exports.encrypt = function ( str = '', password, - algorithm = algorithmDefault, - iv = Buffer.alloc(16, 0) + algorithm = algorithmDefault ) { const crypto = require('crypto') - const key = crypto.scryptSync(password, 'salt', 24) - // Use `crypto.randomBytes` to generate a random iv instead of the static iv + const iv = crypto.randomBytes(IV_LENGTH) + const salt = crypto.randomBytes(SALT_LENGTH) + const key = crypto.scryptSync(password, salt, KEY_LENGTH) const cipher = crypto.createCipheriv(algorithm, key, iv) let encrypted = cipher.update(str, 'utf8', 'hex') encrypted += cipher.final('hex') - return encrypted + const authTag = cipher.getAuthTag() + return 'gcm:' + iv.toString('hex') + ':' + salt.toString('hex') + ':' + authTag.toString('hex') + ':' + encrypted } exports.decrypt = function ( encrypted = '', password, - algorithm = algorithmDefault, - iv = Buffer.alloc(16, 0) + algorithm = algorithmDefault ) { const crypto = require('crypto') - // Use the async `crypto.scrypt()` instead. - const key = crypto.scryptSync(password, 'salt', 24) - const decipher = crypto.createDecipheriv(algorithm, key, iv) - // Encrypted using same algorithm, key and iv. + if (encrypted.startsWith('gcm:')) { + // New format: gcm:iv_hex:salt_hex:authtag_hex:ciphertext_hex + const parts = encrypted.split(':') + const iv = Buffer.from(parts[1], 'hex') + const salt = Buffer.from(parts[2], 'hex') + const authTag = Buffer.from(parts[3], 'hex') + const ciphertext = parts[4] + const key = crypto.scryptSync(password, salt, KEY_LENGTH) + const decipher = crypto.createDecipheriv(algorithm, key, iv) + decipher.setAuthTag(authTag) + let decrypted = decipher.update(ciphertext, 'hex', 'utf8') + decrypted += decipher.final('utf8') + return decrypted + } + // Legacy format: aes-192-cbc with static IV and salt + const key = crypto.scryptSync(password, LEGACY_SALT, LEGACY_KEY_LENGTH) + const decipher = crypto.createDecipheriv(LEGACY_ALGORITHM, key, LEGACY_IV) let decrypted = decipher.update(encrypted, 'hex', 'utf8') decrypted += decipher.final('utf8') return decrypted @@ -50,29 +78,42 @@ exports.decrypt = function ( exports.encryptAsync = async function ( str = '', password, - algorithm = algorithmDefault, - iv = Buffer.alloc(16, 0) + algorithm = algorithmDefault ) { const crypto = require('crypto') - const key = await scryptAsync(password, 'salt', 24) - // Use `crypto.randomBytes` to generate a random iv instead of the static iv + const iv = crypto.randomBytes(IV_LENGTH) + const salt = crypto.randomBytes(SALT_LENGTH) + const key = await scryptAsync(password, salt, KEY_LENGTH) const cipher = crypto.createCipheriv(algorithm, key, iv) let encrypted = cipher.update(str, 'utf8', 'hex') encrypted += cipher.final('hex') - return encrypted + const authTag = cipher.getAuthTag() + return 'gcm:' + iv.toString('hex') + ':' + salt.toString('hex') + ':' + authTag.toString('hex') + ':' + encrypted } exports.decryptAsync = async function ( encrypted = '', password, - algorithm = algorithmDefault, - iv = Buffer.alloc(16, 0) + algorithm = algorithmDefault ) { const crypto = require('crypto') - // Use the async `crypto.scrypt()` instead. - const key = await scryptAsync(password, 'salt', 24) - const decipher = crypto.createDecipheriv(algorithm, key, iv) - // Encrypted using same algorithm, key and iv. + if (encrypted.startsWith('gcm:')) { + // New format: gcm:iv_hex:salt_hex:authtag_hex:ciphertext_hex + const parts = encrypted.split(':') + const iv = Buffer.from(parts[1], 'hex') + const salt = Buffer.from(parts[2], 'hex') + const authTag = Buffer.from(parts[3], 'hex') + const ciphertext = parts[4] + const key = await scryptAsync(password, salt, KEY_LENGTH) + const decipher = crypto.createDecipheriv(algorithm, key, iv) + decipher.setAuthTag(authTag) + let decrypted = decipher.update(ciphertext, 'hex', 'utf8') + decrypted += decipher.final('utf8') + return decrypted + } + // Legacy format: aes-192-cbc with static IV and salt + const key = await scryptAsync(password, LEGACY_SALT, LEGACY_KEY_LENGTH) + const decipher = crypto.createDecipheriv(LEGACY_ALGORITHM, key, LEGACY_IV) let decrypted = decipher.update(encrypted, 'hex', 'utf8') decrypted += decipher.final('utf8') return decrypted