Update AES library (API has changed)

This commit is contained in:
Chris Hennick 2023-04-23 16:00:27 -07:00
parent 0a57febc0f
commit 491c512d6c
No known key found for this signature in database
GPG key ID: 25653935CC8B6C74
3 changed files with 13 additions and 9 deletions

View file

@ -12,7 +12,7 @@ Library to support the reading and writing of zip files.
edition = "2021" edition = "2021"
[dependencies] [dependencies]
aes = { version = "0.7.5", optional = true } aes = { version = "0.8.2", optional = true }
byteorder = "1.4.3" byteorder = "1.4.3"
bzip2 = { version = "0.4.3", optional = true } bzip2 = { version = "0.4.3", optional = true }
constant_time_eq = { version = "0.2.5", optional = true } constant_time_eq = { version = "0.2.5", optional = true }

View file

@ -9,7 +9,7 @@ use crate::types::AesMode;
use constant_time_eq::constant_time_eq; use constant_time_eq::constant_time_eq;
use hmac::{Hmac, Mac}; use hmac::{Hmac, Mac};
use sha1::Sha1; use sha1::Sha1;
use std::io::{self, Read}; use std::io::{self, Error, ErrorKind, Read};
/// The length of the password verifcation value in bytes /// The length of the password verifcation value in bytes
const PWD_VERIFY_LENGTH: usize = 2; const PWD_VERIFY_LENGTH: usize = 2;
@ -84,7 +84,8 @@ impl<R: Read> AesReader<R> {
let mut derived_key: Vec<u8> = vec![0; derived_key_len]; let mut derived_key: Vec<u8> = vec![0; derived_key_len];
// use PBKDF2 with HMAC-Sha1 to derive the key // use PBKDF2 with HMAC-Sha1 to derive the key
pbkdf2::pbkdf2::<Hmac<Sha1>>(password, &salt, ITERATION_COUNT, &mut derived_key); pbkdf2::pbkdf2::<Hmac<Sha1>>(password, &salt, ITERATION_COUNT, &mut derived_key)
.map_err(|e| Error::new(ErrorKind::InvalidInput, e))?;
let decrypt_key = &derived_key[0..key_length]; let decrypt_key = &derived_key[0..key_length];
let hmac_key = &derived_key[key_length..key_length * 2]; let hmac_key = &derived_key[key_length..key_length * 2];
let pwd_verify = &derived_key[derived_key_len - 2..]; let pwd_verify = &derived_key[derived_key_len - 2..];
@ -165,8 +166,8 @@ impl<R: Read> Read for AesReaderValid<R> {
// use constant time comparison to mitigate timing attacks // use constant time comparison to mitigate timing attacks
if !constant_time_eq(computed_auth_code, &read_auth_code) { if !constant_time_eq(computed_auth_code, &read_auth_code) {
return Err( return Err(
io::Error::new( Error::new(
io::ErrorKind::InvalidData, ErrorKind::InvalidData,
"Invalid authentication code, this could be due to an invalid password or errors in the data" "Invalid authentication code, this could be due to an invalid password or errors in the data"
) )
); );

View file

@ -4,10 +4,12 @@
//! different byte order (little endian) than NIST (big endian). //! different byte order (little endian) than NIST (big endian).
//! See [AesCtrZipKeyStream](./struct.AesCtrZipKeyStream.html) for more information. //! See [AesCtrZipKeyStream](./struct.AesCtrZipKeyStream.html) for more information.
use aes::cipher;
use aes::cipher::{BlockCipher, BlockEncrypt};
use aes::cipher::generic_array::GenericArray; use aes::cipher::generic_array::GenericArray;
use aes::{BlockEncrypt, NewBlockCipher};
use byteorder::WriteBytesExt; use byteorder::WriteBytesExt;
use std::{any, fmt}; use std::{any, fmt};
use cipher::KeyInit;
/// Internal block size of an AES cipher. /// Internal block size of an AES cipher.
const AES_BLOCK_SIZE: usize = 16; const AES_BLOCK_SIZE: usize = 16;
@ -27,7 +29,7 @@ pub trait AesKind {
/// Key type. /// Key type.
type Key: AsRef<[u8]>; type Key: AsRef<[u8]>;
/// Cipher used to decrypt. /// Cipher used to decrypt.
type Cipher; type Cipher: KeyInit;
} }
impl AesKind for Aes128 { impl AesKind for Aes128 {
@ -82,7 +84,7 @@ where
impl<C> AesCtrZipKeyStream<C> impl<C> AesCtrZipKeyStream<C>
where where
C: AesKind, C: AesKind,
C::Cipher: NewBlockCipher, C::Cipher: BlockCipher,
{ {
/// Creates a new zip variant AES-CTR key stream. /// Creates a new zip variant AES-CTR key stream.
/// ///
@ -151,13 +153,14 @@ fn xor(dest: &mut [u8], src: &[u8]) {
mod tests { mod tests {
use super::{Aes128, Aes192, Aes256, AesCipher, AesCtrZipKeyStream, AesKind}; use super::{Aes128, Aes192, Aes256, AesCipher, AesCtrZipKeyStream, AesKind};
use aes::{BlockEncrypt, NewBlockCipher}; use aes::{BlockEncrypt, NewBlockCipher};
use aes::cipher::{BlockCipher, BlockEncrypt};
/// Checks whether `crypt_in_place` produces the correct plaintext after one use and yields the /// Checks whether `crypt_in_place` produces the correct plaintext after one use and yields the
/// cipertext again after applying it again. /// cipertext again after applying it again.
fn roundtrip<Aes>(key: &[u8], ciphertext: &mut [u8], expected_plaintext: &[u8]) fn roundtrip<Aes>(key: &[u8], ciphertext: &mut [u8], expected_plaintext: &[u8])
where where
Aes: AesKind, Aes: AesKind,
Aes::Cipher: NewBlockCipher + BlockEncrypt, Aes::Cipher: BlockCipher + BlockEncrypt,
{ {
let mut key_stream = AesCtrZipKeyStream::<Aes>::new(key); let mut key_stream = AesCtrZipKeyStream::<Aes>::new(key);