fix(crypto): include CTR IV in keystore MAC (v2) - #1749
Conversation
Authenticate cipherparams.iv in the BLAKE2b MAC so CTR keystream offset cannot be tampered without failing verification (fixes #1721). New encryptToKeyStore writes version 2 with macKey||iv||ciphertext. decryptFromKeystore still accepts v1 (macKey||ciphertext only) for existing wallets.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThe keystore format now uses version 2 for newly written files. Version 2 MACs include the IV. Decryption remains compatible with version 1 keystores. Tests cover encryption, compatibility, tampering, and browser-polyfill behavior. ChangesKeystore MAC authentication
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Practical risk scale for #1721 (for reviewers)Assuming the question is “how likely is an attacker to actually use CTR IV not being in the MAC?” — put it on a scale: Overall practical risk: ~1–2 / 10 (low)
The issue text already says this: low severity in practice for BIP39. What an attacker would need
They do not get the password or a clean copy of the mnemonic from this alone. They get integrity failure that looks like “valid password, bad data” (or a non-mnemonic string). Likelihood of real-world abuse
So: great for a security write-up / “fix the construction”; poor as a primary theft primitive. Scale analogy
Compared to things that are higher risk
Bottom line: Treat #1721 as real but niche — fix for correctness (this PR), don’t rank it as “attackers are probably doing this.” On a 0–10 “will this get used against users” scale: about 1–2. |
Summary
Fixes #1721.
Keystore MAC previously covered only
macKey || ciphertext, not the CTR IV. An attacker who could modifycipherparams.ivcould change the decrypted plaintext without invalidating the MAC (CTR malleability). Low practical impact for BIP39 (checksum usually fails), but the MAC should authenticate everything an attacker can edit.Change
macKey || ciphertextmacKey || iv || ciphertextversion12aes-128-ctr)version: 2with IV bound into the MAC.version >= 2→ verify with IV;version < 2→ legacy MAC (existing wallets keep working).Not in this PR
Compatibility
xchain-cryptoversions should upgrade together before creating new keystores.Test plan
yarn workspace @xchainjs/xchain-crypto test>= 2) is the right switch vs cipher-basedSummary by CodeRabbit