Skip to content

Commit ce823c9

Browse files
fjlkaralabe
authored andcommitted
crypto: ensure that VerifySignature rejects malleable signatures (ethereum#15708)
* crypto: ensure that VerifySignature rejects malleable signatures It already rejected them when using libsecp256k1, make sure the nocgo version does the same thing. * crypto: simplify check * crypto: fix build
1 parent 5e1581c commit ce823c9

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

crypto/signature_nocgo.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ func VerifySignature(pubkey, hash, signature []byte) bool {
8787
if err != nil {
8888
return false
8989
}
90+
// Reject malleable signatures. libsecp256k1 does this check but btcec doesn't.
91+
if sig.S.Cmp(secp256k1_halfN) > 0 {
92+
return false
93+
}
9094
return sig.Verify(hash, key)
9195
}
9296

crypto/signature_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ func TestVerifySignature(t *testing.T) {
7575
}
7676
}
7777

78+
// This test checks that VerifySignature rejects malleable signatures with s > N/2.
79+
func TestVerifySignatureMalleable(t *testing.T) {
80+
sig := hexutil.MustDecode("0x638a54215d80a6713c8d523a6adc4e6e73652d859103a36b700851cb0e61b66b8ebfc1a610c57d732ec6e0a8f06a9a7a28df5051ece514702ff9cdff0b11f454")
81+
key := hexutil.MustDecode("0x03ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd3138")
82+
msg := hexutil.MustDecode("0xd301ce462d3e639518f482c7f03821fec1e602018630ce621e1e7851c12343a6")
83+
if VerifySignature(key, msg, sig) {
84+
t.Error("VerifySignature returned true for malleable signature")
85+
}
86+
}
87+
7888
func TestDecompressPubkey(t *testing.T) {
7989
key, err := DecompressPubkey(testpubkeyc)
8090
if err != nil {

0 commit comments

Comments
 (0)