Skip to content

fix(keychain): pad DH shared secret to fixed length on Linux#548

Merged
Benehiko merged 1 commit into
mainfrom
fix/keychain-dh-leading-zero
Jun 3, 2026
Merged

fix(keychain): pad DH shared secret to fixed length on Linux#548
Benehiko merged 1 commit into
mainfrom
fix/keychain-dh-leading-zero

Conversation

@Benehiko
Copy link
Copy Markdown
Member

@Benehiko Benehiko commented Jun 2, 2026

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

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>
@Benehiko Benehiko marked this pull request as ready for review June 3, 2026 12:38
Copy link
Copy Markdown

@docker-agent docker-agent Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Benehiko Benehiko requested a review from joe0BAB June 3, 2026 13:00
@Benehiko Benehiko merged commit ea3dafa into main Jun 3, 2026
26 checks passed
@Benehiko Benehiko deleted the fix/keychain-dh-leading-zero branch June 3, 2026 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Intermittent Linux keychain failure: DH shared secret stripped of leading zeros ("secret was transferred or encrypted in an invalid way")

2 participants