Repository navigation
Add v2 post-quantum hybrid HPKE (MLKEM1024-P384) scheme - #267
Conversation
r0mdau
left a comment
There was a problem hiding this comment.
Thanks for this. The crypto looks right to me: stdlib crypto/hpke with MLKEM1024-P384, the key ID bound into the HPKE info, a fresh data key per session with counter nonces, the private seed verified against the public key, and old binaries failing closed on v2 files.
Before this merges it is interesting to see:
- The v1 regression fixed, where a value shaped like
EJ[1:...]is left in plaintext. reencrypt --to-public-keyrejecting a duplicate_public_key. Today it reports success and keeps the old recipient.- Copied encrypted values handled at encrypt time. Encrypt accepts them and decrypt fails, for a copy within a file and for a value whose session field is missing.
- The merge-conflict claim corrected. Two branches that each add a secret do conflict on the session line, though keeping both lines resolves it.
|
We found these issues in our review too and are already working on fixes. No action needed from Javier or his agent on this. |
r0mdau
left a comment
There was a problem hiding this comment.
LG for the fixes.
One question: encrypt and decrypt still accept a duplicate _public_key and use the last one. That matches main for v1, so I assume it is intentional. Is it worth rejecting for v2 files?
|
Since it's already built I don't know if there's necessarily any value in changing it at this point, but we could have kept the XChaCha20-Poly1305 piece as it was since it's a lateral move as far as security goes compared to AES-256. I'm updating the draft guidance now to prevent additional false-positives of this low-priority work (they both have the same bits of security, but AES-256 is the only CNSA 2.0 approved because AES is the main approval source). So far looks good to me, just waiting on a final reporting from my robot about it. I'm somewhat suspicious about the decryption speeds and want to make sure something is accounting for all the 'why' parts of it |
… skipped fields - Require canonical positive decimal schema versions in parseBoxedEnvelope (rejecting EJ[02:...], EJ[+2:...], EJ[01:...], EJ[0:...], EJ[-1:...]) and align IsHybridBoxedMessage with loadHybridBoxedMessage (including rejecting embedded CR/LF in base64). - Clarify the intentional v1/v2 separation exception in legacyMessageParser and ejson_v1_compat_test.go, and test that non-canonical EJ[02:...] is sealed in v1. - Reject duplicate top-level _public_key fields on v2 encrypt/decrypt/reencrypt (ErrPublicKeyDuplicate) while keeping pure-v1 last-wins compatibility on encrypt/decrypt. - Unify ExtractSessions across readers and writers so both reject malformed (ErrSessionInvalid) and duplicate (ErrSessionDuplicate) session records, and reject trailing non-whitespace JSON tokens, whitespace-padded public keys, and newline-padded base64 in public keys/sessions. - Add json.SkippedHybridSessionRefs mirroring Walker's underscore scoping: Encrypt pruning preserves sessions still referenced under _-prefixed keys, and Reencrypt refuses (naming the key path and session ID) before dropping any session still referenced under a _-prefixed key.
|
Addressed all open review comments in 2efda9b and added property-based testing with
|
Build on PR Shopify#250's hybrid scheme exploration, updated to Go 1.26+ stdlib crypto/hpke and per-edit-session encapsulation: - Schema v2 (EJ[2:<sid>:<n>:<ct>]) using RFC 9180 HPKE Base Mode (MLKEM1024-P384 KEM 0x0051, HKDF-SHA384, Export-Only AEAD 0xFFFF) exporting a 32-byte AES-256-GCM session data key with counted 96-bit big-endian nonces and AAD = "EJ[2:<sid>:<n>:". - Key ID derived as "ejv2-" + lowercase hex SHA-384(pubkey) (101 chars), keeping keydir filenames, Secret Manager secret IDs (<=255), and Kubernetes Secret keys (<=253) under length limits while keeping v2 names distinct from v1 64-hex keys. Private key stays a compact 32-byte RFC 9180 seed (64 hex chars), verified against _public_key. - One HPKE encapsulation per edit session stored in top-level _ejson_v2_session_<sid> (sid = SHA-384(enc)[:16] hex), preserving offline public-key encryption without merge conflicts and pruning unreferenced sessions. - Strict v1/v2 separation per file, duplicate (sid, n) nonce rejection, `ejson keygen --pqc` / `--scheme v2`, `ejson reencrypt [--to-public-key]`, and cross-language known-answer test vectors in testdata/v2_vectors.json.
Restore the legacy v1 skip boundary and always seal decrypted values for the target recipient. Preserve v1 application fields on rotation, and reject ambiguous recipients and migration collisions before writing. Validate v2 session records before pruning and in the final output. Reject missing sessions, repeated session/counter pairs and ciphertext shorter than the authentication tag. Exercise the production writer with known-answer vectors and regression tests for byte-preserving refusal.
… skipped fields - Require canonical positive decimal schema versions in parseBoxedEnvelope (rejecting EJ[02:...], EJ[+2:...], EJ[01:...], EJ[0:...], EJ[-1:...]) and align IsHybridBoxedMessage with loadHybridBoxedMessage (including rejecting embedded CR/LF in base64). - Clarify the intentional v1/v2 separation exception in legacyMessageParser and ejson_v1_compat_test.go, and test that non-canonical EJ[02:...] is sealed in v1. - Reject duplicate top-level _public_key fields on v2 encrypt/decrypt/reencrypt (ErrPublicKeyDuplicate) while keeping pure-v1 last-wins compatibility on encrypt/decrypt. - Unify ExtractSessions across readers and writers so both reject malformed (ErrSessionInvalid) and duplicate (ErrSessionDuplicate) session records, and reject trailing non-whitespace JSON tokens, whitespace-padded public keys, and newline-padded base64 in public keys/sessions. - Add json.SkippedHybridSessionRefs mirroring Walker's underscore scoping: Encrypt pruning preserves sessions still referenced under _-prefixed keys, and Reencrypt refuses (naming the key path and session ID) before dropping any session still referenced under a _-prefixed key.
…invariants Add pgregory.net/rapid property-based and stateful tests covering: - Canonical boxed envelope parsing, mutual exclusion, key derivation/session parsing, and AEAD round-trip/tamper/duplicate-nonce checks (crypto/hybrid_prop_test.go). - Format-preserving metadata edits, Walker vs SkippedHybridSessionRefs exact partition/path/order parity, and fail-closed duplicate/trailing-garbage checks (json/metadata_prop_test.go). - Nested document round-trips (including raw multiline literals and v2<->v1<->v2 rotation), v1 skip/seal/separation boundary, multi-session state machine (rename-to-underscore pruning, backup promotion, reencrypt refusal, adversarial mutations), and concurrent branch merges (ejson_v2_prop_test.go).
Summary
Adds an opt-in
v2post-quantum hybrid scheme alongside existingv1(EJ[1:...]) using Go 1.26+ standard librarycrypto/hpke(zero new external dependencies):MLKEM1024-P384(KEM0x0051,draft-ietf-hpke-pq),HKDF-SHA384(0x0002), andExportOnly(0xFFFF), exporting a 32-byteAES-256-GCMdata key per edit session._public_key:"v2:<base64 of 1,665-byte MLKEM1024-P384 public key>"(preserves offline encrypt-to-public-key; existing v1 binaries fail closed)."ejv2-" + hex(SHA-384(pubkey_bytes))(101 chars — fits filesystemNAME_MAX255, GCP Secret Manager secret IDs 255, and Kubernetes Secret keys 253, and stays distinct from v1 64-hex keys). Derived from_public_keyrather than stored separately.<keydir>/<key_id>, derived & verified against_public_keyon decrypt.ejson encryptrun that encrypts ≥1 plaintext value performs one HPKE encapsulation and writes"_ejson_v2_session_<sid>": "<base64(enc)>"(sid = hex(SHA-384(enc)[:16])), spliced in place without reformatting or reordering keys (json/metadata.go).ejson encryptprunes unreferenced session fields.EJ[2:<sid>:<n>:<base64(AES-256-GCM(k, nonce=be96(n), pt, aad="EJ[2:<sid>:<n>:"))>].DecryptrefusesEJ[1:...]in a v2 file,EJ[2:...]in a v1 file, missing session fields, mismatched seeds, and duplicate(sid, n)pairs (nonce reuse).ejson keygen --pqc/--scheme v2(-wwrites<keydir>/ejv2-<sha384>with mode0400) andejson reencrypt [--to-public-key <pub>]for whole-file v1→v2 migration and session compaction.testdata/v2_vectors.jsonfor cross-language readers.What changed vs. PR #250
v3→v2(EJ[2:...]) per@thepwagner's review on add v3 X25519 + ML-KEM-768 scheme #250.crypto/hpkeMLKEM1024P384()+HKDFSHA384()+ExportOnly()+AES-256-GCM._ejson_v2_session_<sid>).ejv2-+ full SHA-384 (101 chars), with 64-hex seed private keys.reencrypt --to-public-key, duplicate-nonce rejection, andtestdata/v2_vectors.json.Verification
Run with Go
1.27.4:go vet ./...: passgo test -race -cover ./...: all 4 packages pass (ejson82.3%,cmd/ejson17.2%,crypto70.0%,json81.3%)go test -bench=BenchmarkCore1700Values -benchmem -cpu=1 -run='^$' .(1,700 secrets, single core):EJ[1:)EJ[2:+ 1 session)