Skip to content

Simplify AEAD ProcessBytes dispatch and tidy WipeKeyMaterial - #166

Merged
Xor-el merged 1 commit into
masterfrom
feature/processbytes-refactor
Jul 27, 2026
Merged

Simplify AEAD ProcessBytes dispatch and tidy WipeKeyMaterial#166
Xor-el merged 1 commit into
masterfrom
feature/processbytes-refactor

Conversation

@Xor-el

@Xor-el Xor-el commented Jul 27, 2026

Copy link
Copy Markdown
Owner

Refactor the large, duplicative ProcessBytes methods of the AEAD modes, keeping every hot kernel and the byte-for-byte crypto output unchanged -- only the dispatch scaffolding around them is restructured.

GCM

  • Collapse the near-mirror encrypt/decrypt whole-block dispatch (the 8-4-2-1 tiered staircase plus the repeated two-block advance) into a single RunTieredWholeBlocks(AForEncrypt, ..., AHoldBack) helper. The hold-back (0 on encrypt, the tag size on decrypt) unifies the two threshold families; the per-tier methods are unchanged. 243 -> 110 lines.

EAX

  • Extract the buffer drain / residue-stash / decrypt-stitch boilerplate repeated across the four dispatch paths into DrainInto / StashResidue / StitchDecryptBlock0. The OMAC lookahead sequencing and fused-kernel logic are untouched. 367 -> 314 lines.

ChaCha20-Poly1305

  • Stop rejecting a nil output buffer in DoFinal; rely on the output-length check like the other AEAD modes. In Pascal an empty result array is nil, so the old check wrongly failed a legitimate empty-plaintext decrypt.

All modes

  • Drop the redundant "if F <> nil then" guards around the WipeKeyMaterial fills; the fill helper already no-ops on a nil (or empty) array.

Tests

  • Add a chunked-streaming equivalence harness (chunk sizes across every buffer/tag boundary, both directions, out-of-place and in-place, with per-call Result accounting) that runs each mode under the kernel on/off toggle and the software engines, so every dispatch tier is exercised.

Refactor the large, duplicative ProcessBytes methods of the AEAD modes,
keeping every hot kernel and the byte-for-byte crypto output unchanged --
only the dispatch scaffolding around them is restructured.

GCM
- Collapse the near-mirror encrypt/decrypt whole-block dispatch (the
  8-4-2-1 tiered staircase plus the repeated two-block advance) into a
  single RunTieredWholeBlocks(AForEncrypt, ..., AHoldBack) helper. The
  hold-back (0 on encrypt, the tag size on decrypt) unifies the two
  threshold families; the per-tier methods are unchanged. 243 -> 110 lines.

EAX
- Extract the buffer drain / residue-stash / decrypt-stitch boilerplate
  repeated across the four dispatch paths into DrainInto / StashResidue /
  StitchDecryptBlock0. The OMAC lookahead sequencing and fused-kernel
  logic are untouched. 367 -> 314 lines.

ChaCha20-Poly1305
- Stop rejecting a nil output buffer in DoFinal; rely on the output-length
  check like the other AEAD modes. In Pascal an empty result array is nil,
  so the old check wrongly failed a legitimate empty-plaintext decrypt.

All modes
- Drop the redundant "if F <> nil then" guards around the WipeKeyMaterial
  fills; the fill helper already no-ops on a nil (or empty) array.

Tests
- Add a chunked-streaming equivalence harness (chunk sizes across every
  buffer/tag boundary, both directions, out-of-place and in-place, with
  per-call Result accounting) that runs each mode under the kernel on/off
  toggle and the software engines, so every dispatch tier is exercised.
@Xor-el
Xor-el merged commit dce4185 into master Jul 27, 2026
15 checks passed
@Xor-el
Xor-el deleted the feature/processbytes-refactor branch July 27, 2026 22:30
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.

1 participant