diff --git a/README.md b/README.md
index 86ba5b9..4cd5c2d 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/src/accel.rs b/src/accel.rs
index 669e533..2050d86 100644
--- a/src/accel.rs
+++ b/src/accel.rs
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License along
// with this program. If not, see .
-//! 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
@@ -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) -> 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) -> 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) {
+ 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) -> bool {
+ false
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index f892383..c7325ab 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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.
//!
@@ -1063,8 +1063,10 @@ pub fn decrypt_zipcrypto(password: &str, check: u8, data: &[u8]) -> Option> 7;
+ x ^= x << 17;
+ x as u32
+ };
+ for len in [0, 1, 2, 3, 15, 16, 17, 255, 256, 4099] {
+ let data: Vec = (0..len).map(|_| next() as u8).collect();
+ let start = [next(), next(), next()];
+ let mut portable_keys = start;
+ let portable: Vec = 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);