Skip to content

Commit 08271c8

Browse files
committed
fix: also guard X25519 ECDH against a nil/empty receiver keypair
Extend the ECDH nil-guard to the receiver: a nil *KeyPair or a zero-value keypair (nil underlying private key) now returns an error instead of panicking, matching the guard already applied to the peer key. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W7WcBfhKfPFyj2Gp4H6s7R
1 parent e2e614c commit 08271c8

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

multikey/x25519/x25519.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,12 @@ func (k *KeyPair) KeyDID() did.DID { return k.Public().KeyDID() }
113113

114114
// ECDH performs X25519 Diffie-Hellman between this private key and the peer's
115115
// public key, returning the raw shared secret. It returns an error rather than
116-
// panicking when peer is nil or carries no public key, since peer keys may come
117-
// from external input.
116+
// panicking when either the keypair or the peer is nil or carries no key, since
117+
// peer keys may come from external input.
118118
func (k *KeyPair) ECDH(peer *PublicKey) ([]byte, error) {
119+
if k == nil || k.priv == nil {
120+
return nil, fmt.Errorf("nil private key")
121+
}
119122
if peer == nil || peer.pub == nil {
120123
return nil, fmt.Errorf("nil peer public key")
121124
}

multikey/x25519/x25519_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,13 @@ func TestECDH(t *testing.T) {
156156
_, err = alice.ECDH(&x25519.PublicKey{})
157157
require.Error(t, err)
158158
})
159+
160+
t.Run("returns an error for a nil or empty keypair instead of panicking", func(t *testing.T) {
161+
var nilKeyPair *x25519.KeyPair
162+
_, err := nilKeyPair.ECDH(alice.Public())
163+
require.Error(t, err)
164+
// A zero-value keypair (nil underlying key) is also rejected.
165+
_, err = (&x25519.KeyPair{}).ECDH(alice.Public())
166+
require.Error(t, err)
167+
})
159168
}

0 commit comments

Comments
 (0)