fix(keychain): pad DH shared secret to fixed length on Linux#548
Merged
Conversation
The Secret Service session encryption mode (dh-ietf1024-sha256-aes128-cbc-pkcs7) derived the HKDF AES key from sharedSecret.Bytes(), which returns the minimal big-endian encoding and strips leading zero bytes. When the Diffie-Hellman shared secret had one or more leading zero bytes (~1/256 of sessions per byte), our HKDF input was shorter than the 128-byte zero-padded value the keyring peer uses, so the two sides derived different AES keys. The keyring then rejected CreateItem with "the secret was transferred or encrypted in an invalid way", surfacing as intermittent CI failures on the Linux keychain tests. Encode the shared secret into a fixed-length buffer sized to the group prime (128 bytes for the 1024-bit Second Oakley Group) with big.Int.FillBytes so both sides agree on the HKDF input. Also surface the decrypt error in GetSecret instead of silently returning (nil, nil), and add a deterministic regression test that pins the zero-padded encoding. Fixes #547 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Assessment: 🟢 APPROVE
The fix is correct and well-implemented. big.Int.FillBytes precisely addresses the intermittent DH shared-secret mismatch by left-padding to the group prime size (128 bytes). The regression test with myPrivate=1 is a clean, deterministic vector that proves the fix and documents the failure mode. Surfacing the decrypt error in GetSecret is also a straightforward improvement over silently swallowing it.
joe0BAB
approved these changes
Jun 3, 2026
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.
The Secret Service session encryption mode
(dh-ietf1024-sha256-aes128-cbc-pkcs7) derived the HKDF AES key from sharedSecret.Bytes(), which returns the minimal big-endian encoding and strips leading zero bytes. When the Diffie-Hellman shared secret had one or more leading zero bytes (~1/256 of sessions per byte), our HKDF input was shorter than the 128-byte zero-padded value the keyring peer uses, so the two sides derived different AES keys. The keyring then rejected CreateItem with "the secret was transferred or encrypted in an invalid way", surfacing as intermittent CI failures on the Linux keychain tests.
Encode the shared secret into a fixed-length buffer sized to the group prime (128 bytes for the 1024-bit Second Oakley Group) with big.Int.FillBytes so both sides agree on the HKDF input. Also surface the decrypt error in GetSecret instead of silently returning (nil, nil), and add a deterministic regression test that pins the zero-padded encoding.
Fixes #547