Notes/src/services/encryption/data_encryption.ts

120 lines
3.3 KiB
TypeScript
Raw Normal View History

"use strict";
import crypto = require('crypto');
import log = require('../log');
2017-11-10 22:55:19 -05:00
2024-02-17 18:55:41 +02:00
function arraysIdentical(a: any[] | Buffer, b: any[] | Buffer) {
let i = a.length;
if (i !== b.length) return false;
while (i--) {
if (a[i] !== b[i]) return false;
}
return true;
}
function shaArray(content: crypto.BinaryLike) {
2023-05-05 23:41:11 +02:00
// we use this as a simple checksum and don't rely on its security, so SHA-1 is good enough
return crypto.createHash('sha1').update(content).digest();
}
function pad(data: Buffer): Buffer {
if (data.length > 16) {
data = data.slice(0, 16);
}
else if (data.length < 16) {
const zeros = Array(16 - data.length).fill(0);
data = Buffer.concat([data, Buffer.from(zeros)]);
2017-11-15 18:23:19 -05:00
}
return Buffer.from(data);
}
function encrypt(key: Buffer, plainText: Buffer | string) {
2017-11-16 00:22:00 -05:00
if (!key) {
throw new Error("No data key!");
}
const plainTextBuffer = Buffer.from(plainText);
const iv = crypto.randomBytes(16);
2017-11-16 00:22:00 -05:00
const cipher = crypto.createCipheriv('aes-128-cbc', pad(key), pad(iv));
const digest = shaArray(plainTextBuffer).slice(0, 4);
const digestWithPayload = Buffer.concat([digest, plainTextBuffer]);
const encryptedData = Buffer.concat([cipher.update(digestWithPayload), cipher.final()]);
const encryptedDataWithIv = Buffer.concat([iv, encryptedData]);
return encryptedDataWithIv.toString('base64');
}
function decrypt(key: Buffer, cipherText: string | Buffer): Buffer | false | null {
2020-12-21 23:19:03 +01:00
if (cipherText === null) {
return null;
}
2017-11-16 00:22:00 -05:00
if (!key) {
return Buffer.from("[protected]");
}
2019-05-04 16:05:28 +02:00
try {
const cipherTextBufferWithIv = Buffer.from(cipherText.toString(), 'base64');
// old encrypted data can have IV of length 13, see some details here: https://github.com/zadam/trilium/issues/3017
const ivLength = cipherTextBufferWithIv.length % 16 === 0 ? 16 : 13;
2019-05-04 16:05:28 +02:00
const iv = cipherTextBufferWithIv.slice(0, ivLength);
2019-05-04 16:05:28 +02:00
const cipherTextBuffer = cipherTextBufferWithIv.slice(ivLength);
2019-05-04 16:05:28 +02:00
const decipher = crypto.createDecipheriv('aes-128-cbc', pad(key), pad(iv));
2019-05-04 16:05:28 +02:00
const decryptedBytes = Buffer.concat([decipher.update(cipherTextBuffer), decipher.final()]);
2019-05-04 16:05:28 +02:00
const digest = decryptedBytes.slice(0, 4);
const payload = decryptedBytes.slice(4);
2019-05-04 16:05:28 +02:00
const computedDigest = shaArray(payload).slice(0, 4);
2019-05-04 16:05:28 +02:00
if (!arraysIdentical(digest, computedDigest)) {
return false;
}
2019-05-04 16:05:28 +02:00
return payload;
}
catch (e: any) {
2019-05-04 16:05:28 +02:00
// recovery from https://github.com/zadam/trilium/issues/510
if (e.message?.includes("WRONG_FINAL_BLOCK_LENGTH") || e.message?.includes("wrong final block length")) {
2019-05-04 16:05:28 +02:00
log.info("Caught WRONG_FINAL_BLOCK_LENGTH, returning cipherText instead");
return Buffer.from(cipherText);
2019-05-04 16:05:28 +02:00
}
else {
throw e;
}
}
}
function decryptString(dataKey: Buffer, cipherText: string) {
const buffer = decrypt(dataKey, cipherText);
2020-12-21 23:19:03 +01:00
if (buffer === null) {
return null;
} else if (buffer === false) {
log.error(`Could not decrypt string. Buffer: ${buffer}`);
2017-12-30 20:14:05 -05:00
throw new Error("Could not decrypt string.");
}
return buffer.toString('utf-8');
}
export = {
encrypt,
decrypt,
decryptString
2020-12-21 23:19:03 +01:00
};