Skip to content

Commit

Permalink
Use const instead of let, where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Jul 23, 2020
1 parent 90f3ded commit fe3de6e
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 159 deletions.
73 changes: 37 additions & 36 deletions lib/backend/fipsrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = class FIPSCrypto extends Backend
async aes256ctr(plaintext, key, nonce)
{
let ciphertext;
let cipher = crypto.createCipheriv('aes-256-ctr', key, nonce);
const cipher = crypto.createCipheriv('aes-256-ctr', key, nonce);
ciphertext = cipher.update(plaintext);
cipher.final();
return ciphertext;
Expand All @@ -63,12 +63,12 @@ module.exports = class FIPSCrypto extends Backend
if (!Buffer.isBuffer(plaintext)) {
plaintext = await Util.toBuffer(plaintext);
}
let hkdfSalt = await Util.randomBytes(SALT_SIZE);
let encKey = await Util.HKDF(key, hkdfSalt, 'AES-256-CTR');
let macKey = await Util.HKDF(key, hkdfSalt, 'HMAC-SHA-384');
let ctrNonce = await Util.randomBytes(NONCE_SIZE);
const hkdfSalt = await Util.randomBytes(SALT_SIZE);
const encKey = await Util.HKDF(key, hkdfSalt, 'AES-256-CTR');
const macKey = await Util.HKDF(key, hkdfSalt, 'HMAC-SHA-384');
const ctrNonce = await Util.randomBytes(NONCE_SIZE);

let ciphertext = await this.aes256ctr(plaintext, encKey, ctrNonce);
const ciphertext = await this.aes256ctr(plaintext, encKey, ctrNonce);
await sodium.sodium_memzero(encKey);

let mac;
Expand Down Expand Up @@ -122,23 +122,23 @@ module.exports = class FIPSCrypto extends Backend
async decrypt(ciphertext, key, aad = '')
{
if (!sodium) sodium = await SodiumPlus.auto();
let header = ciphertext.slice(0, 5);
const header = ciphertext.slice(0, 5);
if (!await Util.hashEquals(MAGIC_HEADER, header)) {
throw new CryptoOperationException('Invalid ciphertext header.');
}
let decoded = await Util.toBuffer(base64url.parse(ciphertext.slice(5)));
let hkdfSalt = decoded.slice(0, SALT_SIZE);
let ctrNonce = decoded.slice(
const decoded = await Util.toBuffer(base64url.parse(ciphertext.slice(5)));
const hkdfSalt = decoded.slice(0, SALT_SIZE);
const ctrNonce = decoded.slice(
SALT_SIZE,
SALT_SIZE + NONCE_SIZE
);
let mac = decoded.slice(
const mac = decoded.slice(
SALT_SIZE + NONCE_SIZE,
SALT_SIZE + NONCE_SIZE + MAC_SIZE
);
let cipher = decoded.slice(SALT_SIZE + NONCE_SIZE + MAC_SIZE);
const cipher = decoded.slice(SALT_SIZE + NONCE_SIZE + MAC_SIZE);

let macKey = await Util.HKDF(key, hkdfSalt, 'HMAC-SHA-384');
const macKey = await Util.HKDF(key, hkdfSalt, 'HMAC-SHA-384');
let recalc;
if (aad.length > 0) {
recalc = await Util.hmac(
Expand Down Expand Up @@ -172,9 +172,9 @@ module.exports = class FIPSCrypto extends Backend
await sodium.sodium_memzero(macKey);
throw new CryptoOperationException('Invalid MAC');
}
let encKey = await Util.HKDF(key, hkdfSalt, 'AES-256-CTR');
const encKey = await Util.HKDF(key, hkdfSalt, 'AES-256-CTR');

let plaintext = await this.aes256ctr(cipher, encKey, ctrNonce);
const plaintext = await this.aes256ctr(cipher, encKey, ctrNonce);
await sodium.sodium_memzero(encKey);
return plaintext.toString('binary');
}
Expand Down Expand Up @@ -248,7 +248,7 @@ module.exports = class FIPSCrypto extends Backend
*/
async getIndexTypeColumn(tableName, fieldName, indexName)
{
let hash = await Util.hmac(
const hash = await Util.hmac(
'sha384',
Util.pack([
await Util.toBuffer(fieldName),
Expand Down Expand Up @@ -302,13 +302,13 @@ module.exports = class FIPSCrypto extends Backend
chunkSize = 8192
) {
if (!sodium) sodium = await SodiumPlus.auto();
let header = Buffer.alloc(5, 0);
let storedMAC = Buffer.alloc(48, 0);
let salt = Buffer.alloc(16, 0); // pbkdf2
let hkdfSalt = Buffer.alloc(32, 0); // HKDF
const header = Buffer.alloc(5, 0);
const storedMAC = Buffer.alloc(48, 0);
const salt = Buffer.alloc(16, 0); // pbkdf2
const hkdfSalt = Buffer.alloc(32, 0); // HKDF
let ctrNonce = Buffer.alloc(16, 0);

let inputFileSize = (await fs.fstat(inputFP)).size;
const inputFileSize = (await fs.fstat(inputFP)).size;
if (inputFileSize < 5) {
throw new CryptoOperationException('Input file is empty');
}
Expand All @@ -321,9 +321,9 @@ module.exports = class FIPSCrypto extends Backend
await fs.read(inputFP, hkdfSalt, 0, 32, 69);
await fs.read(inputFP, ctrNonce, 0, 16, 101);

let encKey = await Util.HKDF(key, hkdfSalt, 'AES-256-CTR');
let macKey = await Util.HKDF(key, hkdfSalt, 'HMAC-SHA-384');
let hmac = crypto.createHmac('sha384', macKey);
const encKey = await Util.HKDF(key, hkdfSalt, 'AES-256-CTR');
const macKey = await Util.HKDF(key, hkdfSalt, 'HMAC-SHA-384');
const hmac = crypto.createHmac('sha384', macKey);
hmac.update(MAGIC_HEADER);
hmac.update(salt);
hmac.update(hkdfSalt);
Expand All @@ -336,17 +336,17 @@ module.exports = class FIPSCrypto extends Backend
cHmac.update(hkdfSalt);
cHmac.update(ctrNonce);

let ctrIncrease = (chunkSize + 15) >>> 4;
const ctrIncrease = (chunkSize + 15) >>> 4;
let outPos = 0;
let inPos = 117;
let toRead = chunkSize;
let plaintext;
let ciphertext = Buffer.alloc(chunkSize, 0);
const ciphertext = Buffer.alloc(chunkSize, 0);

// First, validate the HMAC of the ciphertext. We're storing the MAC of each chunk
// in memory, as well.
let thisChunkMac;
let chunkMacs = [];
const chunkMacs = [];
do {
toRead = (inPos + chunkSize > inputFileSize)
? (inputFileSize - inPos)
Expand All @@ -365,7 +365,8 @@ module.exports = class FIPSCrypto extends Backend
outPos += toRead;
inPos += toRead;
} while (inPos < inputFileSize);
let calcMAC = hmac.digest();

const calcMAC = hmac.digest();
if (!await Util.hashEquals(calcMAC, storedMAC)) {
throw new CryptoOperationException('Invalid authentication tag');
}
Expand Down Expand Up @@ -434,11 +435,11 @@ module.exports = class FIPSCrypto extends Backend
salt = Constants.DUMMY_SALT
) {
if (!sodium) sodium = await SodiumPlus.auto();
let hkdfSalt = await Util.randomBytes(SALT_SIZE);
const hkdfSalt = await Util.randomBytes(SALT_SIZE);
let ctrNonce = await Util.randomBytes(NONCE_SIZE);

let encKey = await Util.HKDF(key, hkdfSalt, 'AES-256-CTR');
let macKey = await Util.HKDF(key, hkdfSalt, 'HMAC-SHA-384');
const encKey = await Util.HKDF(key, hkdfSalt, 'AES-256-CTR');
const macKey = await Util.HKDF(key, hkdfSalt, 'HMAC-SHA-384');

await fs.write(outputFP, await Util.toBuffer(MAGIC_HEADER), 0, 5);
// Empty space for MAC
Expand All @@ -448,20 +449,20 @@ module.exports = class FIPSCrypto extends Backend
await fs.write(outputFP, ctrNonce, 0, 16, 101);

// Init MAC state
let hmac = crypto.createHmac('sha384', macKey);
const hmac = crypto.createHmac('sha384', macKey);
await sodium.sodium_memzero(macKey);
hmac.update(MAGIC_HEADER);
hmac.update(salt);
hmac.update(hkdfSalt);
hmac.update(ctrNonce);

// We want to increase our CTR value by the number of blocks we used previously
let ctrIncrease = (chunkSize + 15) >>> 4;
let inputFileSize = (await fs.fstat(inputFP)).size;
const ctrIncrease = (chunkSize + 15) >>> 4;
const inputFileSize = (await fs.fstat(inputFP)).size;
let outPos = 117;
let inPos = 0;
let toRead = chunkSize;
let plaintext = Buffer.alloc(chunkSize, 0);
const plaintext = Buffer.alloc(chunkSize, 0);
let ciphertext;

do {
Expand All @@ -484,7 +485,7 @@ module.exports = class FIPSCrypto extends Backend
} while (inPos < inputFileSize);
await sodium.sodium_memzero(encKey);

let storedMAC = hmac.digest();
const storedMAC = hmac.digest();

// Write the MAC at the beginning of the file.
await fs.write(outputFP, storedMAC, 0, 48, 5);
Expand Down
Loading

0 comments on commit fe3de6e

Please sign in to comment.