Skip to content

Refactor XChaCha20Poly1305 to delegate via injected ChaCha engine#93

Merged
Xor-el merged 1 commit into
masterfrom
enhancement/xchacha
May 11, 2026
Merged

Refactor XChaCha20Poly1305 to delegate via injected ChaCha engine#93
Xor-el merged 1 commit into
masterfrom
enhancement/xchacha

Conversation

@Xor-el
Copy link
Copy Markdown
Owner

@Xor-el Xor-el commented May 11, 2026

Reworks TXChaCha20Poly1305 to share the parent's nonce/key validation, MAC state machine, and reuse-detection by injecting the underlying ChaCha engine instead of overriding Init with a parallel implementation. Adds round-trip, tamper, and nonce-validation tests for both TXChaCha20Engine and TXChaCha20Poly1305.

Design

TChaCha20Poly1305 gains a protected constructor accepting (IMac, IChaCha7539Engine, ANonceBytes). The 12-byte nonce check is replaced with a configurable FNonceBytes, and the previously hard-coded NonceSize constant is removed. The internal FChaCha20 field becomes IChaCha7539Engine (interfaced, so the explicit destructor is no longer needed). Error messages (SInvalidParameters, SNonceMustBe96, SCannotReuseNonce, SMacCheckFailed) become format strings keyed by AlgorithmName and nonce width.

TXChaCha20Poly1305 collapses to a thin subclass:

  • Two constructors (default and (IMac)-taking), both delegating to the base via inherited Create(APoly1305, TXChaCha20Engine.Create() as IXChaCha20Engine, 24).
  • GetAlgorithmName override.
  • The custom Init, the cached FMasterKey, and the destructor are removed — the parent now handles nonce-length validation (192-bit), reuse detection, MAC sizing, and key state, with HChaCha20 derivation happening inside the injected TXChaCha20Engine.

IChaCha7539Engine is extended with ProcessBlocks2 / ProcessBlocks4 so future SIMD fast paths can be invoked through the interface rather than the concrete class.

Test changes

XChaCha20Poly1305Tests adds:

  • TestDeterministicRoundTrip2048 — 2048-byte plaintext encrypted then decrypted with a deterministic LCG-generated key/nonce/AAD/payload, asserting the round-trip recovers the input.
  • TestTamperedTagMacFailure — flips the last tag byte of a known-vector ciphertext and asserts EInvalidCipherTextCryptoLibException with the new format-string message (mac check in XChaCha20Poly1305 failed).
  • TestRejectNonce12Byte — passing a 96-bit nonce raises EArgumentCryptoLibException with Nonce must be 192 bits (verifies the format-string path picks up FNonceBytes correctly).
  • TestReuseNonceEncryptionRejected — a second encrypt-init under the same (key, nonce) raises with cannot reuse nonce for XChaCha20Poly1305 encryption.

XChaCha20Tests adds:

  • TestRoundTrip1024 — 1024-byte plaintext encrypt/decrypt round-trip, with an explicit check that ciphertext differs from plaintext (catches a no-op keystream regression).
  • TestRejectShortNonce64Bits and TestRejectShortNonce96Bits — 8-byte and 12-byte nonces must raise EArgumentCryptoLibException.

@Xor-el Xor-el merged commit 0daa733 into master May 11, 2026
33 checks passed
@Xor-el Xor-el deleted the enhancement/xchacha branch May 11, 2026 14:43
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