fix: prevent cipher reuse after final() and reset is_finalized in all init() overrides#948
Merged
fix: prevent cipher reuse after final() and reset is_finalized in all init() overrides#948
Conversation
- Add is_finalized flag to HybridCipher base class - Guard update() and final() in all cipher subclasses - Consolidate duplicate final_called/final_called_ flags into base class - Reset is_finalized on init() for proper re-initialization - Add tests for Buffer concat vs string concat equivalence - Add tests for hex/base64 encoding roundtrips - Add tests for cipher state violations (update after final, double final)
Add is_finalized = false to XSalsa20Cipher, XChaCha20Poly1305Cipher, and ChaCha20Poly1305Cipher init() methods that bypass HybridCipher::init(). Move is_finalized = true inside #else block in XSalsa20Cipher::final() so state isn't corrupted on the error path. Add re-init after final test.
4 tasks
Contributor
🤖 End-to-End Test Results - AndroidStatus: ✅ Passed 📸 Final Test ScreenshotScreenshot automatically captured from End-to-End tests and will expire in 30 days This comment is automatically updated on each test run. |
Contributor
🤖 End-to-End Test Results - iOSStatus: ✅ Passed 📸 Final Test ScreenshotScreenshot automatically captured from End-to-End tests and will expire in 30 days This comment is automatically updated on each test run. |
boorad
added a commit
that referenced
this pull request
Feb 24, 2026
New pages: PQC (ML-DSA/ML-KEM), Argon2, KMAC, Certificate (SPKAC), and Utilities (one-shot hash, timingSafeEqual, primes, introspection). Updated 8 existing pages with missing API sections: SubtleCrypto (deriveBits, deriveKey, wrapKey/unwrapKey, encapsulation), Keys (KeyObject.from, equals, toCryptoKey), Signing (standalone sign/verify), DiffieHellman (diffieHellman function), Ed25519 (Ed448/X448), Hash (crypto.hash one-shot, SHA3), ECDH (convertKey), and reorganized the API index into Core/Key Exchange/Key Derivation/Advanced sections. Annotated 7 pages with behavioral notes from recent fix PRs (#929, #930, #932, #933, #939, #948, #949, #951, #954, #955): cipher single-use warning, generateKeys preservation, PBKDF2 validation, OAEP hash default, randomFill view correctness, RSA-* aliases, and flexible curve names. Added llms.txt index route and fixed llms-full.txt JSX stripping in source.ts to produce clean LLM-friendly output.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
Fixes #945 — cipher behavior change from 0.7+ to 1+ where Buffer concat vs string concat produced different results.
Changes
Cipher reuse prevention (commit 1)
is_finalizedflag to baseHybridCipherclasscheckNotFinalized()guard toupdate()andfinal()across all cipher subclassesfinal_called_/final_calledflags fromChaCha20Poly1305Cipher,XChaCha20Poly1305Cipher, andXSalsa20Poly1305Cipherupdate()orfinal()afterfinal()now throws"Unsupported state or unable to authenticate data", matching Node.js behaviorInit reset fix (commit 2)
is_finalized = falsetoXSalsa20Cipher::init(),XChaCha20Poly1305Cipher::init(), andChaCha20Poly1305Cipher::init()— these overrideinit()without callingHybridCipher::init(), so the flag was never reset on re-initializationis_finalized = trueinside the#else BLSALLOC_SODIUMblock inXSalsa20Cipher::final()so state isn't corrupted on the error pathRoot Cause
The original issue report used the same cipher instance for both Buffer concat and string concat approaches. In 0.x this silently produced wrong results (different ciphertexts). The correct usage requires separate cipher instances, which now produce identical output.