Skip to content

Move to aes 0.9 for hardware AES on ARM and add a keyed Ctr256 - #4

Merged
rjriajul merged 6 commits into
rjriajul:masterfrom
z7vian:aes-0.9-wipe
Nov 6, 2026
Merged

rjriajul merged 6 commits into
rjriajul:masterfrom
z7vian:aes-0.9-wipe

Conversation

@z7vian

@z7vian z7vian commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Rust crates that depend on warpcrypto get software AES on ARM today: --cfg aes_armv8 in .cargo/config.toml only applies when building inside this repo (so the Python wheels have it), not to dependents. aes 0.9 turns on the ARMv8 backend by default (aes 0.9.0 changelog: "Enable ARMv8 backend by default", picked at runtime via cpufeatures), so this moves to it. While there, it adds a keyed CTR type.

Changes

aes 0.9 (aes = ">=0.9.2, <0.9.3")

  • Ported to the cipher 0.5 backend traits. Every public signature and every output is unchanged; the existing known-answer tests in tests/rust.rs pass untouched.
  • The cap is there because aes 0.9.3 raised its MSRV to 1.89 (this crate says 1.85) and allows MSRV bumps in patch releases. A Rust 1.85 build resolves to 0.9.2. I also measured 0.9.4: it needs 1.89, and ctr256 at 512 KiB on x86 dropped from 5920 to 2287 MB/s with this crate's CTR loop (0.9.3 turned VAES on by default; I haven't confirmed that is the cause) (run 42071050073). So the cap can go when rust-version moves past 1.89, but that CTR slowdown needs a look first.
  • .cargo/config.toml is removed: aes 0.9 has no aes_armv8 flag. README updated to match.

Ctr256: AES-256-CTR with the key expanded once, for a stream processed piece by piece. Ctr256::new(&key, &iv), apply_keystream(&mut data), iv(), state(). It gives the same bytes as calling ctr256 on each piece; a test checks this across 18 chunk sizes from 0 to 4096 bytes, most of them not multiples of 16, and three starting counters, including counter carry and wraparound. ctr256 itself is unchanged. It holds an Aes256Enc (encryption key schedule only) and does not implement Clone, so a keystream can't be duplicated by accident. Debug prints nothing secret.

Key schedule wiping without aes's zeroize feature
In aes 0.9 the zeroize feature wipes the whole cipher on every drop with zeroize_flat_type, one volatile byte store at a time (aes 0.9.2 src/lib.rs:434-442; zeroize 1.9 src/lib.rs:748-764). Aes256 is 960 bytes (the union is sized for the software fallback's key schedule), so every call that sets up a key pays for 960 single-byte stores. That added about 120 ns per call on x86 and about 330 ns on ARM, which made short inputs slower than on master (numbers below, column A).

This PR drops that feature and wraps every cipher the crate creates (the per-call functions, pack_message/unpack_message, the Python bindings and Ctr256) in a small private Wiped<C>(ManuallyDrop<C>). Its Drop zeroes the same 960 bytes with volatile machine-word stores, then a compiler_fence(SeqCst). The helper functions take &Wiped<_>, so a cipher that isn't wiped doesn't compile. For comparison, aes 0.8.4 with zeroize wiped only the active variant: 30 × 16-byte volatile stores on AES-NI (ni.rs:191-196, :265-270) or 120 × u64 on the software path (soft.rs:118-119). This wipes every byte the 0.9 feature wiped.

The unsafe block, with its reasoning:

impl<C> Drop for Wiped<C> {
    fn drop(&mut self) {
        const { assert!(align_of::<C>() >= align_of::<usize>()) };
        let words = size_of::<C>() / size_of::<usize>();
        let tail = size_of::<C>() % size_of::<usize>();
        let p = (&mut *self.0 as *mut C).cast::<usize>();
        // SAFETY: `p` points to the `size_of::<C>()` bytes of `self.0` and is aligned for `usize`
        // (asserted above); `words` words and then `tail` bytes cover exactly those bytes.
        // `self.0` is never read or dropped after this, so the zeros need not be a valid `C`.
        // Not running `C`'s destructor is allowed in safe Rust (as with `mem::forget`), so it
        // can't make this unsound. It also loses nothing: `Aes256`/`Aes256Enc` hold only `Copy`
        // unions of plain bytes, with no heap memory or handles, and their `Drop` only wipes, and
        // only when some crate in the build turns on `aes`'s `zeroize` feature.
        unsafe {
            for i in 0..words {
                p.add(i).write_volatile(0);
            }
            let p = p.add(words).cast::<u8>();
            for i in 0..tail {
                p.add(i).write_volatile(0);
            }
        }
        compiler_fence(Ordering::SeqCst);
    }
}

Miri runs tests/rust.rs clean with this in place (with --cfg aes_backend="soft", since Miri can't run the AES intrinsics; the wiped struct is the same 960 bytes): miri job in run 42082454811.

Alternative without unsafe (option A): drop the last commit and keep features = ["zeroize"]. Everything else stays the same; the cost is the byte-wise wipe on short inputs. In the run below, x86 IGE at 16 B goes from 122 MB/s on master to 79, ctr256 at 1 KiB from 3122 to 2472. On ARM against the --cfg aes_armv8 build that the wheels use, IGE at 1 KiB goes from 1043 to 780 and ctr256 at 1 KiB from 2534 to 1417. At 128 KiB and up A and B are the same. That branch is at z7vian/WarpCrypto@aes-0.9. If you'd rather not carry unsafe code, take that one.

Not changed: no #[pyfunction] signature or Python behaviour changes (src/python.rs only swaps how the cipher is built), no public Rust API removed or changed, and no version bump. Ctr256 is a new type, so this fits a minor release (2.2.0).

Benchmarks

Run 42072506583: GitHub ubuntu-latest (AMD EPYC 7763) and ubuntu-24.04-arm (CPU part 0xd49, aes pmull flags), Rust 1.99.1, 5 interleaved passes, median MB/s. Each variant is built as a separate crate that depends on warpcrypto by path, so this repo's .cargo/config.toml doesn't apply. That is how other crates see it; the "+ --cfg aes_armv8" column is how the Python wheels were built. A = aes 0.9 with aes's zeroize; B = this PR. (The bench built B at the commit before a comment-only change to the SAFETY note.)

ARM

op size master master + --cfg aes_armv8 A B (this PR)
ige256_encrypt 16 B 19 76 29 83
ige256_encrypt 1 KiB 47 1043 780 1062
ige256_encrypt 128 KiB 48 1307 1307 1310
ige256_encrypt 512 KiB 48 1306 1310 1307
ige256_decrypt 16 B 20 74 29 83
ige256_decrypt 1 KiB 51 1039 781 1067
ige256_decrypt 128 KiB 52 1308 1307 1309
ige256_decrypt 512 KiB 52 1301 1308 1300
ctr256 16 B 19 74 30 85
ctr256 1 KiB 174 2534 1417 2658
ctr256 128 KiB 190 5222 4857 4858
ctr256 512 KiB 190 5220 4913 4871

x86

op size master A B (this PR)
ige256_encrypt 16 B 122 79 234
ige256_encrypt 1 KiB 707 672 737
ige256_encrypt 128 KiB 764 763 764
ige256_encrypt 512 KiB 763 763 763
ige256_decrypt 16 B 121 77 228
ige256_decrypt 1 KiB 706 666 737
ige256_decrypt 128 KiB 764 763 764
ige256_decrypt 512 KiB 763 763 763
ctr256 16 B 118 73 192
ctr256 1 KiB 3122 2472 3666
ctr256 128 KiB 4925 4902 4943
ctr256 512 KiB 4891 4908 4905

One regression: on ARM, ctr256 at 128 KiB and up is about 7% slower than the --cfg aes_armv8 build (5222 → 4858 MB/s at 128 KiB). It shows up in A as well, so it comes from aes 0.9.2's ARM backend, not from the wipe. It affects large CTR buffers on ARM in the Python wheels, i.e. file and CDN downloads in Python callers on ARM machines. IGE and everything else on ARM is the same or faster; Rust dependents on ARM go from software AES to 4–27× faster.

Ctr256 vs calling ctr256 per chunk (same stream fed in fixed-size chunks, B, MB/s):

chunk x86 ctr256 x86 Ctr256 ARM ctr256 ARM Ctr256
64 B 722 2234 317 3369
256 B 2068 4533 1098 4582
1 KiB 3621 4839 2642 4899

CI

The repo's workflow run on my fork (renamed copy, fail-fast: false, plus the Miri job): run 42082454811. The Rust jobs (1.85 and stable on Linux x86, Linux ARM, macOS and Windows), clippy, cargo publish --dry-run, Miri and the Python matrix pass, except build (ubuntu-latest, 3.9): it stops at the "Set up Python 3.9" step with The version '3.9' with architecture 'x64' was not found for Ubuntu 26.04., before any build step runs. build (macos-latest, 3.12) was cancelled after 15 minutes without getting a runner and passed on re-run (attempt 2).

@rjriajul
rjriajul merged commit 193f4d6 into rjriajul:master Nov 6, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants