Skip to content

Commit 3e09d17

Browse files
authored
Merge pull request #39 from pilot-protocol/security/verify-length-guard
security: crypto.Verify rejects wrong-length public keys (panic-DoS fix)
2 parents efbd5fb + 7226539 commit 3e09d17

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

crypto/identity.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ func (id *Identity) Sign(message []byte) []byte {
3434
return ed25519.Sign(id.PrivateKey, message)
3535
}
3636

37-
// Verify checks a signature against the public key.
37+
// Verify checks a signature against the public key. A public key that is not
38+
// exactly ed25519.PublicKeySize bytes is rejected rather than passed to
39+
// ed25519.Verify, which panics on a wrong-length key — an attacker-supplied
40+
// key reaches this from unauthenticated message paths.
3841
func Verify(publicKey ed25519.PublicKey, message, signature []byte) bool {
42+
if len(publicKey) != ed25519.PublicKeySize {
43+
return false
44+
}
3945
return ed25519.Verify(publicKey, message, signature)
4046
}
4147

crypto/zz_verify_badkey_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// SPDX-License-Identifier: AGPL-3.0-or-later
2+
3+
package crypto
4+
5+
import (
6+
"crypto/ed25519"
7+
"testing"
8+
)
9+
10+
func TestVerifyRejectsWrongLengthKeyWithoutPanic(t *testing.T) {
11+
id, err := GenerateIdentity()
12+
if err != nil {
13+
t.Fatal(err)
14+
}
15+
msg := []byte("challenge")
16+
sig := id.Sign(msg)
17+
18+
for _, n := range []int{0, 1, 5, 31, 33, 64} {
19+
bad := make([]byte, n)
20+
if Verify(bad, msg, sig) {
21+
t.Fatalf("Verify accepted a %d-byte public key", n)
22+
}
23+
}
24+
25+
if Verify(nil, msg, sig) {
26+
t.Fatal("Verify accepted a nil public key")
27+
}
28+
29+
if !Verify(id.PublicKey, msg, sig) {
30+
t.Fatal("Verify rejected a valid signature")
31+
}
32+
if len(id.PublicKey) != ed25519.PublicKeySize {
33+
t.Fatalf("unexpected key size %d", len(id.PublicKey))
34+
}
35+
}

0 commit comments

Comments
 (0)