samuel-lucas6 / Kryptor

A simple, modern, and secure encryption and signing tool that aims to be a better version of age and Minisign.
https://www.kryptor.co.uk
GNU General Public License v3.0
414 stars 33 forks source link

🐛 Bug: Files with a certain length get truncated during decryption #40

Closed samuel-lucas6 closed 2 years ago

samuel-lucas6 commented 2 years ago

Description

I received an email yesterday afternoon reporting an issue where files of a certain length get decrypted to an incorrect size.

Steps to reproduce

  1. Generate a binary file: dd if=/dev/zero of=data bs=1M count=1 iflag=fullblock.
  2. Encrypt the file: kryptor -e -p:test data.bin.
  3. Decrypt the file: kryptor -d -p:test data.bin.kryptor.
  4. Compare the SHA256 hash of the decrypted file with the original file.
  5. Compare the file size of the decrypted file with the original file.

Expected behaviour

The decrypted file should be the same size as the original file and have the same SHA256 hash.

Platform info

samuel-lucas6 commented 2 years ago

After some investigation last night, a mistake with mod (%) usage means Kryptor v3 sometimes removes the last 16,384 byte chunk from decrypted files when the file shouldn't be truncated at all.

Here are the guilty lines of code:

https://github.com/samuel-lucas6/Kryptor/blob/v3.1.0/src/KryptorCLI/FileEncryption/EncryptFile.cs#L68 https://github.com/samuel-lucas6/Kryptor/blob/v3.1.0/src/KryptorCLI/FileEncryption/DecryptFile.cs#L88

It's frustrating and upsetting that I missed this. Rather stupidly, none of my testing used files with a length that would catch this problem. I'm extremely sorry for any damage this has caused. I intend to do a hotfix release later today. As the current code contains breaking changes for v4, I will likely reverse the changes for this release and then add them back again after the release has been published.