Skip to content

AES-XTS out of place: AesXts::encrypt and decrypt (breaking: in-place ones become _in_place) - #4283

Merged
reaperhulk merged 1 commit into
claude/out-of-place-specfrom
claude/out-of-place-impl
Nov 6, 2026
Merged

reaperhulk merged 1 commit into
claude/out-of-place-specfrom
claude/out-of-place-impl

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Stacked on #4282 (the contracts). Implements vg_aes_xts_encrypt_unit_to and vg_aes_xts_decrypt_unit_to on x86-64, proven against Spec/Xts/OutOfPlace.lean, and gives AesXts out-of-place encrypt and decrypt. No changes to TCB/ or Spec/.

Breaking change: AesXts's in-place encrypt and decrypt are renamed encrypt_in_place and decrypt_in_place, and the plain names now write to a separate output. This matches AesGcm and ChaCha20Poly1305, whose encrypt is out of place and encrypt_in_place in place.

What changes

  • Rust API (src/cipher/aes/xts.rs). AesXts::encrypt(&self, i, src, dst) and decrypt read the data unit from src and write the result to dst; encrypt_in_place(&self, i, buffer) and decrypt_in_place are the former encrypt and decrypt, unchanged but for their names. A new XtsError::InvalidOutputLength (the variant name AES-GCM and ChaCha20-Poly1305 use) is returned when dst.len() != src.len(); the existing length limits also apply. On x86-64 the backend is chosen by an exhaustive match over the existing Impl enum, covering all ten functions. On the other architectures the Rust code copies src to dst and calls the in-place function. There is no new assembly there.
  • Out-of-place data units (Impl/AesXts/X86_64UnitTo.lean, Proof/AesXts/X86_64/UnitTo/). These are the in-place data-unit functions, except that the whole blocks go through an internal out-of-place whole-block function, vg_aes_xts_{en,de}crypt_to (contract To.xtsToX86_64). The ciphertext-stealing tail is then done in dst.
  • Every CPU-feature variant has an out-of-place counterpart. XtsImpl gains a field to : To.ToImpl, so every variant must supply one, and the data-unit functions call it through UnitImpl.of.
    • The one-pass variants (_aesni_pclmul and _vaes_vpclmul) get new code (Impl/AesXts/X86_64VaesTo.lean, Proof/AesXts/X86_64/VaesTo/). Each block is loaded once from src ([r11 + rcx + d], with rcx = src - dst) and stored once to dst; nothing is copied. The proofs are the in-place ones adapted to that addressing.
    • The scalar, _aesni and _vaes variants copy src to dst and call their in-place functions (To.ToImpl.ofCopy, Impl/AesXts/X86_64To.lean). They are verified but no faster.
  • Constant time. For the one-pass functions, src - dst is kept in a slot of the scratch buffer between entry and the loops. The taint analysis starts from the writable regions' lengths and the scratch buffer's base (VaesTo.τ₀, agree), so it can see that the slot holds a public value.

Tests

  • Wycheproof aes_xts_test.json: every vector out of place, with src and dst at offsets (0,0), (1,3), (7,0), (0,15), (9,9), encrypt then decrypt.
  • NIST CAVP XTS vectors: out of place, plus InvalidOutputLength and the too-short and too-long cases.
  • Run locally with VG_CPU_FEATURES unset, none, aes,ssse3, aes,avx,pclmulqdq,ssse3, aes,avx,avx2,ssse3,vaes and aes,avx,avx2,pclmulqdq,ssse3,vaes,vpclmulqdq.

Benchmarks

New group aes-xts-{bits}-out-of-place in bench/benches/primitives/aes_xts.rs, next to the existing in-place group (which copies, then encrypts in place) and OpenSSL's. Sizes are the usual set plus 1 MiB.

Benchmarks run 42095769221 (x86-64, aes_xts, every VG_CPU_FEATURES configuration; AMD EPYC 9V45, Zen 5, with OpenSSL 3.5.5), XTS-AES-256 (64-byte key). It ran on the commit before the rename, which changes no code a benchmark runs. aws-lc-rs has no XTS, so it has no column.

Default configuration (_vaes_vpclmul):

size copy + in-place out-of-place OpenSSL
64 B 154 ns 154 ns 361 ns
1 KiB 219 ns 217 ns 396 ns
16 KiB 1.36 µs 1.35 µs 962 ns
1 MiB 92.86 µs 78.12 µs 38.96 µs

VG_CPU_FEATURES=aes,avx,pclmulqdq,ssse3 (_aesni_pclmul): 1 MiB 249.73 → 220.03 µs, 16 KiB 3.80 → 3.62 µs.

Out of place saves the copy: about 16% at 1 MiB here, less below. On this CPU OpenSSL is faster from 16 KiB up (2× at 1 MiB), which this PR does not change: that gap is in the XTS loop itself, not the copy (#4291 closes it with AVX-512).

Locally (Intel Xeon @ 2.30GHz with VAES, a different OpenSSL), the same benchmark gave 1 MiB: copy + in-place 105 µs, out-of-place 73.7 µs, OpenSSL 150 µs.

Not covered

  • No new assembly for x86, AArch64, ARM, PPC64LE or RISC-V. Those architectures use the Rust copy fallback.
  • AES-GCM out-of-place decryption is not in this stack.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWwoHb4ZQEEHLgZCj5J

@reaperhulk reaperhulk changed the title AES-XTS out of place: AesXts::encrypt_to and decrypt_to AES-XTS out of place: AesXts::encrypt and decrypt (breaking: in-place ones become _in_place) Nov 5, 2026
@reaperhulk
reaperhulk added this pull request to stack #4288 November 5, 2026 19:53
@reaperhulk
reaperhulk enabled stacked auto-merge November 5, 2026 20:56
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 5, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to invalid changes in the merge commit Nov 5, 2026
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 5, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to invalid changes in the merge commit Nov 5, 2026
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 5, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to invalid changes in the merge commit Nov 5, 2026
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 5, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to invalid changes in the merge commit Nov 5, 2026
…me _in_place)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWwoHb4ZQEEHLgZCj5J
@reaperhulk
reaperhulk force-pushed the claude/out-of-place-impl branch from a7a544d to 3193167 Compare November 6, 2026 08:13
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 6, 2026
Merged via the queue into main with commit 7614141 Nov 6, 2026
35 checks passed
@reaperhulk
reaperhulk deleted the claude/out-of-place-impl branch November 6, 2026 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants