mirror of
https://github.com/timvisee/ffsend.git
synced 2025-10-03 09:39:15 +02:00
Properly handle errors in the encrypted file reader
This commit is contained in:
parent
5dfe94b7ea
commit
cce9a28dc3
1 changed files with 20 additions and 14 deletions
34
src/main.rs
34
src/main.rs
|
@ -114,7 +114,7 @@ fn main() {
|
||||||
cipher,
|
cipher,
|
||||||
&encrypt_key,
|
&encrypt_key,
|
||||||
&iv,
|
&iv,
|
||||||
);
|
).unwrap();
|
||||||
|
|
||||||
// Buffer the encrypted reader, and determine the length
|
// Buffer the encrypted reader, and determine the length
|
||||||
let reader_len = reader.len().unwrap();
|
let reader_len = reader.len().unwrap();
|
||||||
|
@ -265,13 +265,13 @@ impl Header for XFileMetadata {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A lazy file reader, that encrypts the file with the given `cipher`
|
/// A lazy file reader, that encrypts the file with the given `cipher`
|
||||||
/// and appends the GCM tag to the end of it.
|
/// and appends the cryptographic tag to the end of it.
|
||||||
///
|
///
|
||||||
/// This reader is lazy because the file data loaded from the system
|
/// This reader is lazy because the file data loaded from the system
|
||||||
/// and encrypted when it is read from the reader.
|
/// and encrypted when it is read from the reader.
|
||||||
/// This greatly reduces memory usage for large files.
|
/// This greatly reduces memory usage for large files.
|
||||||
///
|
///
|
||||||
/// This reader encrypts the file data with an appended GCM tag.
|
/// This reader encrypts the file data with an appended cryptographic tag.
|
||||||
///
|
///
|
||||||
/// The reader uses a small internal buffer as data is encrypted in blocks,
|
/// The reader uses a small internal buffer as data is encrypted in blocks,
|
||||||
/// which may output more data than fits in the given buffer while reading.
|
/// which may output more data than fits in the given buffer while reading.
|
||||||
|
@ -302,24 +302,30 @@ impl EncryptedFileReaderTagged {
|
||||||
/// This method consumes twice the size of the file in memory while
|
/// This method consumes twice the size of the file in memory while
|
||||||
/// constructing, and constructs a reader that has a size similar to the
|
/// constructing, and constructs a reader that has a size similar to the
|
||||||
/// file.
|
/// file.
|
||||||
pub fn new(file: File, cipher: Cipher, key: &[u8], iv: &[u8]) -> Self {
|
///
|
||||||
|
/// It is recommended to wrap this reader in some sort of buffer, such as:
|
||||||
|
/// `std::io::BufReader`
|
||||||
|
pub fn new(file: File, cipher: Cipher, key: &[u8], iv: &[u8])
|
||||||
|
-> Result<Self, io::Error>
|
||||||
|
{
|
||||||
// Build the crypter
|
// Build the crypter
|
||||||
// TODO: return proper errors from crypter
|
|
||||||
let crypter = Crypter::new(
|
let crypter = Crypter::new(
|
||||||
cipher,
|
cipher,
|
||||||
CrypterMode::Encrypt,
|
CrypterMode::Encrypt,
|
||||||
key,
|
key,
|
||||||
Some(iv),
|
Some(iv),
|
||||||
).unwrap();
|
)?;
|
||||||
|
|
||||||
// Construct the encrypted reader
|
// Construct the encrypted reader
|
||||||
EncryptedFileReaderTagged {
|
Ok(
|
||||||
file,
|
EncryptedFileReaderTagged {
|
||||||
cipher,
|
file,
|
||||||
crypter,
|
cipher,
|
||||||
tag: None,
|
crypter,
|
||||||
internal_buf: Vec::new(),
|
tag: None,
|
||||||
}
|
internal_buf: Vec::new(),
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calculate the total length of the encrypted file with the appended
|
/// Calculate the total length of the encrypted file with the appended
|
||||||
|
@ -383,7 +389,7 @@ impl EncryptedFileReaderTagged {
|
||||||
data.truncate(len);
|
data.truncate(len);
|
||||||
|
|
||||||
// Encrypt the data that was read
|
// Encrypt the data that was read
|
||||||
let len = self.crypter.update(&data, &mut encrypted).unwrap();
|
let len = self.crypter.update(&data, &mut encrypted)?;
|
||||||
|
|
||||||
// Calculate how many bytes will be copied to the reader
|
// Calculate how many bytes will be copied to the reader
|
||||||
let out_len = min(buf.len(), len);
|
let out_len = min(buf.len(), len);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue