Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ It runs on the `zlib-rs` backend, a Rust port of zlib, for speed: it inflates
Every field is read through bounds-checked helpers, so a truncated or hostile
archive yields `Err`, never a panic. Parsing has no `unsafe`. The crate's only
`unsafe` is in `src/accel.rs`, which runs the processor's AES instructions
(ARMv8 AES, x86 AES-NI) and, for the WinZip AES key derivation, its SHA-1
instructions (ARMv8 SHA1, x86 SHA extensions), after checking for them at run
time; without them the portable code runs, and the tests hold both to the same
reference.
(ARMv8 AES, x86 AES-NI), its SHA-1 instructions for the WinZip AES key
derivation (ARMv8 SHA1, x86 SHA extensions) and its CRC-32 instructions for
ZipCrypto (ARMv8 CRC32), after checking for them at run time; without them the
portable code runs, and the tests hold both to the same reference.
`zlib-rs` uses `unsafe` internally for its SIMD paths too.

- **The central directory is the only source of sizes and offsets.** Local
Expand Down
54 changes: 53 additions & 1 deletion src/accel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <https://www.gnu.org/licenses/>.

//! The processor's own AES and SHA-1 instructions, where it has them.
//! The processor's own AES, SHA-1 and CRC-32 instructions, where it has them.
//!
//! This is the crate's only `unsafe`: calling an instruction the compiler
//! cannot assume the processor has, and reading and writing whole 16-byte
Expand Down Expand Up @@ -450,3 +450,55 @@ mod sha {
None
}
}

/// Decrypts ZipCrypto `data` from the cipher state `keys`, appending the
/// plain bytes to `out` and leaving `keys` where the data ends, using the
/// processor's CRC-32 instructions for the two CRC steps of each byte.
/// `false`, with nothing changed, when it has none.
pub(crate) fn zipcrypto_decrypt(keys: &mut [u32; 3], data: &[u8], out: &mut Vec<u8>) -> bool {
crc::zipcrypto_decrypt(keys, data, out)
}

#[cfg(target_arch = "aarch64")]
mod crc {
use std::arch::aarch64::__crc32b;

pub(super) fn zipcrypto_decrypt(keys: &mut [u32; 3], data: &[u8], out: &mut Vec<u8>) -> bool {
if !std::arch::is_aarch64_feature_detected!("crc") {
return false;
}
// SAFETY: the processor has the CRC-32 instructions (checked above)
unsafe { run(keys, data, out) };
true
}

/// `CRC32B` is one byte of the reflected CRC-32 that ZIP uses, without
/// the inversions at either end: exactly ZipCrypto's key update. Each
/// byte's keystream depends on the byte before it, so this is one long
/// chain, and the instruction is shorter than a table lookup.
#[target_feature(enable = "crc")]
fn run(keys: &mut [u32; 3], data: &[u8], out: &mut Vec<u8>) {
let [mut k0, mut k1, mut k2] = *keys;
out.reserve(data.len());
for &cipher in data {
let t = k2 | 2;
let plain = cipher ^ (t.wrapping_mul(t ^ 1) >> 8) as u8;
k0 = __crc32b(k0, plain);
k1 = k1
.wrapping_add(k0 & 0xff)
.wrapping_mul(134_775_813)
.wrapping_add(1);
k2 = __crc32b(k2, (k1 >> 24) as u8);
out.push(plain);
}
*keys = [k0, k1, k2];
}
}

#[cfg(not(target_arch = "aarch64"))]
mod crc {
// x86's CRC32 instruction is CRC-32C, a different polynomial.
pub(super) fn zipcrypto_decrypt(_: &mut [u32; 3], _: &[u8], _: &mut Vec<u8>) -> bool {
false
}
}
35 changes: 32 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
//! Every field is read through bounds-checked helpers, so a truncated or
//! hostile archive yields `Err`, never a panic or an out-of-bounds read.
//! Parsing has no `unsafe`. The only `unsafe` in the crate is in `accel`,
//! which runs the processor's own AES and SHA-1 instructions where it has them, checked
//! which runs the processor's own AES, SHA-1 and CRC-32 instructions where it has them, checked
//! for at run time, with the portable code as the fallback. Inflation is done
//! by `flate2` on its `zlib-rs` backend, which also uses `unsafe`, for speed.
//!
Expand Down Expand Up @@ -1063,8 +1063,10 @@ pub fn decrypt_zipcrypto(password: &str, check: u8, data: &[u8]) -> Option<Vec<u
return None;
}
let mut out = Vec::with_capacity(payload.len());
for b in payload {
out.push(crypto_step(&mut keys, *b));
if !accel::zipcrypto_decrypt(&mut keys, payload, &mut out) {
for b in payload {
out.push(crypto_step(&mut keys, *b));
}
}
Some(out)
}
Expand Down Expand Up @@ -1999,6 +2001,33 @@ mod tests {
}
}

#[test]
fn zipcrypto_decryption_agrees_with_and_without_crc_instructions() {
let mut x = 0x2545_f491_4f6c_dd1du64;
let mut next = || {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
x as u32
};
for len in [0, 1, 2, 3, 15, 16, 17, 255, 256, 4099] {
let data: Vec<u8> = (0..len).map(|_| next() as u8).collect();
let start = [next(), next(), next()];
let mut portable_keys = start;
let portable: Vec<u8> = data
.iter()
.map(|&b| crypto_step(&mut portable_keys, b))
.collect();
let mut fast_keys = start;
let mut fast = vec![7u8; 3]; // appends after what is there
if accel::zipcrypto_decrypt(&mut fast_keys, &data, &mut fast) {
assert_eq!(fast[..3], [7; 3]);
assert_eq!(fast[3..], portable[..], "{len} bytes");
assert_eq!(fast_keys, portable_keys, "{len} bytes");
}
}
}

#[test]
fn aes_key_derivation_matches_the_reference() {
let long = "x".repeat(100);
Expand Down
Loading