Properly handle errors in the encrypted file reader

This commit is contained in:
timvisee 2018-03-06 18:24:03 +01:00
parent 5dfe94b7ea
commit cce9a28dc3
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2

View file

@ -114,7 +114,7 @@ fn main() {
cipher,
&encrypt_key,
&iv,
);
).unwrap();
// Buffer the encrypted reader, and determine the length
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`
/// 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
/// and encrypted when it is read from the reader.
/// 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,
/// which may output more data than fits in the given buffer while reading.
@ -302,17 +302,22 @@ impl EncryptedFileReaderTagged {
/// This method consumes twice the size of the file in memory while
/// constructing, and constructs a reader that has a size similar to the
/// 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
// TODO: return proper errors from crypter
let crypter = Crypter::new(
cipher,
CrypterMode::Encrypt,
key,
Some(iv),
).unwrap();
)?;
// Construct the encrypted reader
Ok(
EncryptedFileReaderTagged {
file,
cipher,
@ -320,6 +325,7 @@ impl EncryptedFileReaderTagged {
tag: None,
internal_buf: Vec::new(),
}
)
}
/// Calculate the total length of the encrypted file with the appended
@ -383,7 +389,7 @@ impl EncryptedFileReaderTagged {
data.truncate(len);
// 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
let out_len = min(buf.len(), len);