Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support AES decryption #203

Merged
merged 38 commits into from Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
a265ba7
Create initial `aes_ctr` module
mbr Oct 3, 2020
9f6ee0f
Add `crypt` convenience function
mbr Oct 3, 2020
a5d1905
Simpify `aes_ctr` API to just `crypt`
mbr Oct 3, 2020
4afe4d3
Optimize AES code, use less copies
mbr Oct 3, 2020
b3ec813
Remove `arrayvec` dependency
mbr Oct 3, 2020
4877a6a
test different aes modes and data sizes
Lireer Oct 3, 2020
852ab62
initial aes reader
Lireer Oct 4, 2020
12260f5
disable crc32 checks when handling aes encrypted data
Lireer Oct 8, 2020
d25d6f5
finalize AesReader validation and most of decryption
Lireer Oct 8, 2020
e69df5c
finalize aes decryption
Lireer Oct 9, 2020
8ffc2d1
cargo fmt and clippy
Lireer Oct 9, 2020
ff23539
differentiate between ae1 and ae2
Lireer Oct 9, 2020
2911282
fix benchmarks
Lireer Oct 9, 2020
0820cc4
fix more clippy warnings
Lireer Oct 10, 2020
354993d
feature gate aes decryption
Lireer Oct 10, 2020
5532fd6
Document aes related modules
Lireer Oct 14, 2020
5f0ae55
Document possible panics
Lireer Oct 14, 2020
ed94e8b
test if using the wrong key size panics
Lireer Oct 14, 2020
48b52a7
move AesMode and AesVendorVersion out of aes-crypto feature
Lireer Oct 14, 2020
8f352c3
add missing documentation
Lireer Oct 14, 2020
75e8f6b
use less feature gates if no further dependencies are needed
Lireer Oct 14, 2020
c5e55c0
bump MSRV to 1.42
Lireer Nov 9, 2020
09ad713
update crypto dependencies
Lireer Nov 9, 2020
46f65d4
add aes-crypto feature to default and update README
Lireer Nov 9, 2020
d7f0a18
Merge remote-tracking branch 'zip-rs/zip/master'
Lireer Jan 25, 2022
bb97711
explain trait guarantee violation of read impl
Lireer Jan 25, 2022
35d8f04
"fix" clippy warnings
Lireer Jan 25, 2022
3a71893
run cargo fmt
Lireer Jan 25, 2022
c17df86
test decryption of aes encrypted files
Lireer Jan 25, 2022
85bb91f
update aes-crypto dependencies
Lireer Jan 26, 2022
2e06844
fix clippy warning and shorten links in tests
Lireer Jan 26, 2022
cfc74a5
use same SHA-1 crate with new name
Lireer Jan 27, 2022
fddad89
deduplicate aes testing code
Lireer Jan 30, 2022
49f7501
add and use AES associated constant
Lireer Jan 30, 2022
3d56021
use hmac reset feature for finalize_reset method
Lireer Jan 30, 2022
8f061f8
fix nightly clippy warning
Lireer Jan 30, 2022
91745d5
use `assert_eq` instead of `debug_assert_eq`
Lireer Jan 30, 2022
c8aece8
fix nightly clippy warnings in examples
Lireer Jan 30, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions Cargo.toml
Expand Up @@ -11,11 +11,16 @@ Library to support the reading and writing of zip files.
edition = "2018"

[dependencies]
flate2 = { version = "1.0.0", default-features = false, optional = true }
time = { version = "0.3", features = ["formatting", "macros" ], optional = true }
aes = { version = "0.6.0", optional = true }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using the latest aes version 0.7.5?
There seems to be breaking changes in the API.

Thank you for the hard work!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried updating aes, pbkdf2, hmac and sha-1 (now sha1) together since they all use the same dependencies. Since this required some small changes to the decryption code, I also put in some more (possibly meaningless) checks.

This increased the performance of AES-128 from 42 MB/s to around 160 - 180 MB/s.

byteorder = "1.3"
bzip2 = { version = "0.4", optional = true }
constant_time_eq = { version = "0.1.5", optional = true }
crc32fast = "1.1.1"
flate2 = { version = "1.0.0", default-features = false, optional = true }
hmac = {version = "0.10.1", optional = true }
pbkdf2 = {version = "0.6.0", optional = true }
sha-1 = {version = "0.9.2", optional = true }
time = { version = "0.3", features = ["formatting", "macros" ], optional = true }
zstd = { version = "0.10", optional = true }

[dev-dependencies]
Expand All @@ -24,11 +29,12 @@ getrandom = "0.2"
walkdir = "2"

[features]
aes-crypto = [ "aes", "constant_time_eq", "hmac", "pbkdf2", "sha-1" ]
deflate = ["flate2/rust_backend"]
deflate-miniz = ["flate2/default"]
deflate-zlib = ["flate2/zlib"]
unreserved = []
default = ["bzip2", "deflate", "time", "zstd"]
default = ["aes-crypto", "bzip2", "deflate", "time", "zstd"]

[[bench]]
name = "read_entry"
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -44,6 +44,7 @@ zip = { version = "0.5", default-features = false }

The features available are:

* `aes-crypto`: Enables decryption of files which were encrypted with AES. Supports AE-1 and AE-2 methods.
* `deflate`: Enables the deflate compression algorithm, which is the default for zip files.
* `bzip2`: Enables the BZip2 compression algorithm.
* `time`: Enables features using the [time](https://github.com/rust-lang-deprecated/time) crate.
Expand Down
177 changes: 177 additions & 0 deletions src/aes.rs
@@ -0,0 +1,177 @@
//! Implementation of the AES decryption for zip files.
//!
//! This was implemented according to the [WinZip specification](https://www.winzip.com/win/en/aes_info.html).
//! Note that using CRC with AES depends on the used encryption specification, AE-1 or AE-2.
//! If the file is marked as encrypted with AE-2 the CRC field is ignored, even if it isn't set to 0.

use crate::aes_ctr;
use crate::types::AesMode;
use constant_time_eq::constant_time_eq;
use hmac::{Hmac, Mac, NewMac};
use sha1::Sha1;
use std::io::{self, Read};

/// The length of the password verifcation value in bytes
const PWD_VERIFY_LENGTH: usize = 2;
/// The length of the authentication code in bytes
const AUTH_CODE_LENGTH: usize = 10;
/// The number of iterations used with PBKDF2
const ITERATION_COUNT: u32 = 1000;

/// Create a AesCipher depending on the used `AesMode` and the given `key`.
///
/// # Panics
///
/// This panics if `key` doesn't have the correct size for the chosen aes mode.
fn cipher_from_mode(aes_mode: AesMode, key: &[u8]) -> Box<dyn aes_ctr::AesCipher> {
match aes_mode {
AesMode::Aes128 => Box::new(aes_ctr::AesCtrZipKeyStream::<aes_ctr::Aes128>::new(key))
as Box<dyn aes_ctr::AesCipher>,
AesMode::Aes192 => Box::new(aes_ctr::AesCtrZipKeyStream::<aes_ctr::Aes192>::new(key))
as Box<dyn aes_ctr::AesCipher>,
AesMode::Aes256 => Box::new(aes_ctr::AesCtrZipKeyStream::<aes_ctr::Aes256>::new(key))
as Box<dyn aes_ctr::AesCipher>,
}
}

// An aes encrypted file starts with a salt, whose length depends on the used aes mode
// followed by a 2 byte password verification value
// then the variable length encrypted data
// and lastly a 10 byte authentication code
pub struct AesReader<R> {
reader: R,
aes_mode: AesMode,
data_length: u64,
}

impl<R: Read> AesReader<R> {
pub fn new(reader: R, aes_mode: AesMode, compressed_size: u64) -> AesReader<R> {
let data_length = compressed_size
- (PWD_VERIFY_LENGTH + AUTH_CODE_LENGTH + aes_mode.salt_length()) as u64;

Self {
reader,
aes_mode,
data_length,
}
}

/// Read the AES header bytes and validate the password.
///
/// Even if the validation succeeds, there is still a 1 in 65536 chance that an incorrect
/// password was provided.
/// It isn't possible to check the authentication code in this step. This will be done after
/// reading and decrypting the file.
///
/// # Returns
///
/// If the password verification failed `Ok(None)` will be returned to match the validate
/// method of ZipCryptoReader.
pub fn validate(mut self, password: &[u8]) -> io::Result<Option<AesReaderValid<R>>> {
let salt_length = self.aes_mode.salt_length();
let key_length = self.aes_mode.key_length();

let mut salt = vec![0; salt_length];
self.reader.read_exact(&mut salt)?;

// next are 2 bytes used for password verification
let mut pwd_verification_value = vec![0; PWD_VERIFY_LENGTH];
self.reader.read_exact(&mut pwd_verification_value)?;

// derive a key from the password and salt
// the length depends on the aes key length
let derived_key_len = 2 * key_length + PWD_VERIFY_LENGTH;
let mut derived_key: Vec<u8> = vec![0; derived_key_len];

// use PBKDF2 with HMAC-Sha1 to derive the key
pbkdf2::pbkdf2::<Hmac<Sha1>>(password, &salt, ITERATION_COUNT, &mut derived_key);
let decrypt_key = &derived_key[0..key_length];
let hmac_key = &derived_key[key_length..key_length * 2];
let pwd_verify = &derived_key[derived_key_len - 2..];

// the last 2 bytes should equal the password verification value
if pwd_verification_value != pwd_verify {
// wrong password
return Ok(None);
}

let cipher = cipher_from_mode(self.aes_mode, decrypt_key);
let hmac = Hmac::<Sha1>::new_varkey(hmac_key).unwrap();

Ok(Some(AesReaderValid {
reader: self.reader,
data_remaining: self.data_length,
cipher,
hmac,
}))
}
}

/// A reader for aes encrypted files, which has already passed the first password check.
///
/// There is a 1 in 65536 chance that an invalid password passes that check.
/// After the data has been read and decrypted an HMAC will be checked and provide a final means
/// to check if either the password is invalid or if the data has been changed.
pub struct AesReaderValid<R: Read> {
reader: R,
data_remaining: u64,
cipher: Box<dyn aes_ctr::AesCipher>,
hmac: Hmac<Sha1>,
}

impl<R: Read> Read for AesReaderValid<R> {
/// This implementation does not fulfill all requirements set in the trait documentation.
///
/// ```txt
/// "If an error is returned then it must be guaranteed that no bytes were read."
/// ```
///
/// Whether this applies to errors that occur while reading the encrypted data depends on the
/// underlying reader. If the error occurs while verifying the HMAC, the reader might become
/// practically unusable, since its position after the error is not known.
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if self.data_remaining == 0 {
return Ok(0);
}

// get the number of bytes to read, compare as u64 to make sure we can read more than
// 2^32 bytes even on 32 bit systems.
let bytes_to_read = self.data_remaining.min(buf.len() as u64) as usize;
let read = self.reader.read(&mut buf[0..bytes_to_read])?;
self.data_remaining -= read as u64;

// Update the hmac with the encrypted data
self.hmac.update(&buf[0..read]);

// decrypt the data
self.cipher.crypt_in_place(&mut buf[0..read]);

// if there is no data left to read, check the integrity of the data
if self.data_remaining == 0 {
// Zip uses HMAC-Sha1-80, which only uses the first half of the hash
// see https://www.winzip.com/win/en/aes_info.html#auth-faq
let mut read_auth_code = [0; AUTH_CODE_LENGTH];
self.reader.read_exact(&mut read_auth_code)?;
let computed_auth_code = &self.hmac.finalize_reset().into_bytes()[0..AUTH_CODE_LENGTH];

// use constant time comparison to mitigate timing attacks
if !constant_time_eq(computed_auth_code, &read_auth_code) {
return Err(
io::Error::new(
io::ErrorKind::InvalidData,
"Invalid authentication code, this could be due to an invalid password or errors in the data"
)
);
}
}

Ok(read)
}
}

impl<R: Read> AesReaderValid<R> {
/// Consumes this decoder, returning the underlying reader.
pub fn into_inner(self) -> R {
self.reader
}
}