MatrixAI / js-encryptedfs

Encrypted Filesystem for TypeScript/JavaScript Applications
https://polykey.com
Apache License 2.0
10 stars 3 forks source link

Decryption yields plaintext with trailing zero padding #7

Closed MeanMangosteen closed 5 years ago

MeanMangosteen commented 5 years ago

When you do a write in efs, and the last block's size does not equal a block size, efs will zero pad to fill the reaming space in the block. Since the original filesize is not stored anywhere, the filesize has been lost when it is encrypted. efs cannot tell the difference between trailing zeros that were part of the plaintext and trailing zeros that were added to fill the block. So as of now, say, when you do a readFileSync() there will exists extraneous zeros in the readBuffer if the original plaintext's last block was not block aligned.

import EFS from '../../lib/EncryptedFS.js';
import fs from 'fs';

const efs = new EFS({genKey: true, keyPass: Buffer.from('very password')});
const writeBuf = Buffer.allocUnsafe(10).fill(0x11);
let readBuf = Buffer.allocUnsafe(20).fill(0xff);
const efsFd = efs.openSync('sandbox/tmp/testTrailingZerosEFS.txt', 'w+');
efs.writeSync(efsFd, writeBuf, 0, writeBuf.length, 0);
efs.readSync(efsFd, readBuf, 0, readBuf.length, 0);
console.log(readBuf.toString('hex'));
// 11111111110000000000

const fd = fs.openSync('sandbox/tmp/testTrailingZeros.txt', 'w+');
fs.writeSync(fd, writeBuf, 0, writeBuf.length, 0);
readBuf = Buffer.allocUnsafe(20).fill(0xff);
fs.readSync(fd, readBuf, 0, readBuf.length, 0);
console.log(readBuf.toString('hex'));
// 1111111111ffffffffff
MeanMangosteen commented 5 years ago

9 was implemented resolving this issue as well. Can be closed.