Skip to content

Commit

Permalink
review & clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
agourlay committed May 23, 2024
1 parent 6da1faa commit 6db572c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
8 changes: 5 additions & 3 deletions src/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::io::{self, Error, ErrorKind, Read, Write};
use zeroize::{Zeroize, Zeroizing};

/// The length of the password verifcation value in bytes
const PWD_VERIFY_LENGTH: usize = 2;
pub 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
Expand Down Expand Up @@ -130,14 +130,16 @@ impl<R: Read> AesReader<R> {
/// # Returns
///
/// the verification value and the salt
pub fn get_verification_value_and_salt(mut self) -> io::Result<(Vec<u8>, Vec<u8>)> {
pub fn get_verification_value_and_salt(
mut self,
) -> io::Result<([u8; PWD_VERIFY_LENGTH], Vec<u8>)> {
let salt_length = self.aes_mode.salt_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];
let mut pwd_verification_value = [0; PWD_VERIFY_LENGTH];
self.reader.read_exact(&mut pwd_verification_value)?;
Ok((pwd_verification_value, salt))
}
Expand Down
32 changes: 23 additions & 9 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub(crate) mod zip_archive {
}
}

use crate::aes::PWD_VERIFY_LENGTH;

Check failure on line 85 in src/read.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: ubuntu-latest, stable

unresolved import `crate::aes`

Check failure on line 85 in src/read.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--no-default-features)

unresolved import `crate::aes`
#[cfg(feature = "lzma")]
use crate::read::lzma::LzmaDecoder;
use crate::result::ZipError::{InvalidPassword, UnsupportedArchive};
Expand Down Expand Up @@ -647,33 +648,35 @@ impl<R: Read + Seek> ZipArchive<R> {

/// Returns the verification value and salt for the AES encryption of the file
///
/// It fails if the file is not encrypted or if the file number is invalid.
/// It fails if the file number is invalid.
///
/// # Returns
///
/// - Some with the verification value and the salt
/// - None if the file is not encrypted with AES
#[cfg(feature = "aes-crypto")]
pub fn get_aes_verification_key_and_salt(
&mut self,
file_number: usize,
) -> ZipResult<Option<(AesMode, Vec<u8>, Vec<u8>)>> {
) -> ZipResult<Option<AesInfo>> {
let (_, data) = self
.shared
.files
.get_index(file_number)
.ok_or(ZipError::FileNotFound)?;

if !data.encrypted {
return Err(ZipError::UnsupportedArchive(ZipError::ARCHIVE_NOT_ENCRYPTED));
}
let limit_reader = find_content(data, &mut self.reader)?;
match data.aes_mode {
None => Ok(None),
Some((aes_mode, _, _)) => {
let (key, salt) = AesReader::new(limit_reader, aes_mode, data.compressed_size)
.get_verification_value_and_salt()?;
Ok(Some((aes_mode, key, salt)))
let (verification_value, salt) =
AesReader::new(limit_reader, aes_mode, data.compressed_size)
.get_verification_value_and_salt()?;
let aes_info = AesInfo {
aes_mode,
verification_value,
salt,
};
Ok(Some(aes_info))
}
}
}
Expand Down Expand Up @@ -970,6 +973,17 @@ impl<R: Read + Seek> ZipArchive<R> {
}
}

/// Holds the AES information of a file in the zip archive
#[derive(Debug)]
pub struct AesInfo {
/// The AES encryption mode
pub aes_mode: AesMode,
/// The verification key
pub verification_value: [u8; PWD_VERIFY_LENGTH],
/// The salt
pub salt: Vec<u8>,
}

const fn unsupported_zip_error<T>(detail: &'static str) -> ZipResult<T> {
Err(ZipError::UnsupportedArchive(detail))
}
Expand Down
4 changes: 0 additions & 4 deletions src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ impl ZipError {
/// # ()
/// ```
pub const PASSWORD_REQUIRED: &'static str = "Password required to decrypt file";


/// The text used as an error when the archive is not encrypted
pub const ARCHIVE_NOT_ENCRYPTED: &'static str = "the archive is not encrypted";
}

impl From<ZipError> for io::Error {
Expand Down

0 comments on commit 6db572c

Please sign in to comment.