fix(cli): derive identity encryption key with scrypt and a random salt - #635
Open
Kamirus wants to merge 2 commits into
Open
fix(cli): derive identity encryption key with scrypt and a random salt#635Kamirus wants to merge 2 commits into
Kamirus wants to merge 2 commits into
Conversation
The encrypted identity file (identity.pem.encrypted) used an unsalted single-round SHA-256 of the password as the AES-256-CTR key, allowing fast offline brute-force if the file leaks (CodeQL js/insufficient-password-hash). New files are versioned with a MOPSENCv1 magic header and encrypted with AES-256-GCM using a scrypt-derived key (random 16-byte per-file salt, N=2^17). Legacy headerless files still decrypt and are transparently re-encrypted in the new format after a successful decode. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
| const encryptLegacy = (buffer: Buffer, password: string): Buffer => { | ||
| let key = crypto | ||
| .createHash("sha256") | ||
| .update(password) |
…on-kdf # Conflicts: # cli/CHANGELOG.md # cli/pem.ts # cli/tests/pem.test.ts
Contributor
Cursor AI review👀 HUMAN REVIEW REQUESTED — significant intended changes detected Review complete. The diff is fully contained (3 files), and I've verified the crypto correctness, caller compatibility (
Significant Changes Requiring Human Review
VerdictDecision: REQUEST_HUMAN_REVIEW Generated for commit 5a9d633 |
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.
Password-protected identities (
mops user importwith a password) were encrypted with AES-256-CTR under a key derived as an unsalted, single-round SHA-256 of the password, truncated to 32 base64 characters (192 bits of key material). Ifidentity.pem.encryptedleaks, this allows fast offline brute-force — one cheap hash per guess, and rainbow tables apply since there is no salt. Flagged by CodeQL asjs/insufficient-password-hash(high severity).Encrypted identity files are now versioned with a
MOPSENCv1magic header. New-format files derive the key withscrypt(random 16-byte per-file salt, N=2^17, ~100ms per derivation) and encrypt with AES-256-GCM, so tampering and wrong passwords are detected by authentication rather than by failing to parse the decrypted PEM.Compatibility
Headerless files are decrypted with the legacy scheme and transparently re-encrypted in the new format after a successful decode (best-effort — a read-only file keeps working, just unupgraded). Legacy collision with the magic header is not a concern: legacy files start with a random 16-byte IV, so a 9-byte match has probability 2^-72.
No CLI surface changes;
--helpand docs don't describe the on-disk format, so no doc updates beyond the changelog.Verified end-to-end via the real CLI against a scratch
XDG_CONFIG_HOME: import → file starts withMOPSENCv1; correct password → principal; wrong password →Error: Invalid password; a legacy-encrypted file decodes to the same principal and is upgraded in place.🤖 Generated with Claude Code