Skip to content

Commit ebc7dc9

Browse files
authored
crypto: validate hash length in no cgo Sign (#33104)
- Replace hardcoded DigestLength - Add hash length validation
1 parent d2a5dba commit ebc7dc9

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

crypto/signature_nocgo.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func sigToPub(hash, sig []byte) (*secp256k1.PublicKey, error) {
4343
if len(sig) != SignatureLength {
4444
return nil, errors.New("invalid signature")
4545
}
46+
if len(hash) != DigestLength {
47+
return nil, fmt.Errorf("hash is required to be exactly %d bytes (%d)", DigestLength, len(hash))
48+
}
4649
// Convert to secp256k1 input format with 'recovery id' v at the beginning.
4750
btcsig := make([]byte, SignatureLength)
4851
btcsig[0] = sig[RecoveryIDOffset] + 27
@@ -76,8 +79,8 @@ func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
7679
//
7780
// The produced signature is in the [R || S || V] format where V is 0 or 1.
7881
func Sign(hash []byte, prv *ecdsa.PrivateKey) ([]byte, error) {
79-
if len(hash) != 32 {
80-
return nil, fmt.Errorf("hash is required to be exactly 32 bytes (%d)", len(hash))
82+
if len(hash) != DigestLength {
83+
return nil, fmt.Errorf("hash is required to be exactly %d bytes (%d)", DigestLength, len(hash))
8184
}
8285
if prv.Curve != S256() {
8386
return nil, errors.New("private key curve is not secp256k1")

0 commit comments

Comments
 (0)