Skip to content

Refactor cipher modes onto shared base classes; harden key wiping - #165

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

Refactor cipher modes onto shared base classes; harden key wiping#165
Xor-el merged 1 commit into
masterfrom
feature/modes-refactor

Conversation

@Xor-el

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

Copy link
Copy Markdown
Owner

Consolidate the BlockCipher modes and AEAD modes behind abstract base classes, deduplicating the surface each mode previously reimplemented while keeping every tuned hot path (SIMD/fused kernels, bulk batching, tag-lookahead buffering) in the concrete mode.

Base classes

  • TAbstractBlockCipherMode: base for CBC, CFB, OFB, ECB, SIC and OpenPGP-CFB. Owns the underlying-cipher / algorithm-name / block-size accessors and one overflow-safe bulk bounds check (via TCheck).
  • TAbstractAeadCipher + TAbstractAeadBlockCipher: bases for GCM, EAX, OCB, CCM, GCM-SIV and ChaCha20-Poly1305. Own GetMac, constant-time tag verification, the encrypt-side nonce-reuse guard, MAC-size validation, the unified MAC-check-failed error, the output-size template and a guaranteed key-wiping destructor.
  • TGaloisFieldUtilities: single home for the GF(2^128) x-doubling that CMAC, EAX and OCB each duplicated. MulX moves into TGcmUtilities and ClpGcmSivUtilities is retired.

Security hardening

  • Zeroize key-derived material on teardown for EAX, OCB, CCM, GCM-SIV and ChaCha20-Poly1305 (previously only GCM did). Notably, ChaCha's 256-bit key is now wiped.

Behavioural changes (intentional)

  • GCM-SIV.GetMac now returns the stored tag instead of raising.
  • The MAC-failure message is unified across all AEAD modes.

Tests

  • Add an OpenPGP-CFB test: an independent first-block known-answer vector plus multi-length round-trips.

All cipher outputs remain byte-identical.

Consolidate the BlockCipher modes and AEAD modes behind abstract base
classes, deduplicating the surface each mode previously reimplemented
while keeping every tuned hot path (SIMD/fused kernels, bulk batching,
tag-lookahead buffering) in the concrete mode.

Base classes
- TAbstractBlockCipherMode: base for CBC, CFB, OFB, ECB, SIC and
  OpenPGP-CFB. Owns the underlying-cipher / algorithm-name / block-size
  accessors and one overflow-safe bulk bounds check (via TCheck).
- TAbstractAeadCipher + TAbstractAeadBlockCipher: bases for GCM, EAX,
  OCB, CCM, GCM-SIV and ChaCha20-Poly1305. Own GetMac, constant-time
  tag verification, the encrypt-side nonce-reuse guard, MAC-size
  validation, the unified MAC-check-failed error, the output-size
  template and a guaranteed key-wiping destructor.
- TGaloisFieldUtilities: single home for the GF(2^128) x-doubling that
  CMAC, EAX and OCB each duplicated. MulX moves into TGcmUtilities and
  ClpGcmSivUtilities is retired.

Security hardening
- Zeroize key-derived material on teardown for EAX, OCB, CCM, GCM-SIV
  and ChaCha20-Poly1305 (previously only GCM did). Notably, ChaCha's
  256-bit key is now wiped.

Behavioural changes (intentional)
- GCM-SIV.GetMac now returns the stored tag instead of raising.
- The MAC-failure message is unified across all AEAD modes.

Tests
- Add an OpenPGP-CFB test: an independent first-block known-answer
  vector plus multi-length round-trips.

All cipher outputs remain byte-identical.
@Xor-el
Xor-el merged commit 0520942 into master Jul 27, 2026
28 checks passed
@Xor-el
Xor-el deleted the feature/modes-refactor branch July 27, 2026 18:58
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