crypto: replace noarg fmt.Errorf with errors.New (#27333)

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
Delweng 2023-05-24 20:45:51 +08:00 committed by GitHub
parent b0095eeb20
commit 21c87e0f1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 18 deletions

View File

@ -141,11 +141,11 @@ func toECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) {
// The priv.D must < N
if priv.D.Cmp(secp256k1N) >= 0 {
return nil, fmt.Errorf("invalid private key, >=N")
return nil, errors.New("invalid private key, >=N")
}
// The priv.D must not be zero or negative.
if priv.D.Sign() <= 0 {
return nil, fmt.Errorf("invalid private key, zero or negative")
return nil, errors.New("invalid private key, zero or negative")
}
priv.PublicKey.X, priv.PublicKey.Y = priv.PublicKey.Curve.ScalarBaseMult(d)
@ -204,7 +204,7 @@ func LoadECDSA(file string) (*ecdsa.PrivateKey, error) {
if err != nil {
return nil, err
} else if n != len(buf) {
return nil, fmt.Errorf("key file too short, want 64 hex characters")
return nil, errors.New("key file too short, want 64 hex characters")
}
if err := checkKeyFileEnd(r); err != nil {
return nil, err

View File

@ -36,18 +36,18 @@ import (
"crypto/hmac"
"crypto/subtle"
"encoding/binary"
"fmt"
"errors"
"hash"
"io"
"math/big"
)
var (
ErrImport = fmt.Errorf("ecies: failed to import key")
ErrInvalidCurve = fmt.Errorf("ecies: invalid elliptic curve")
ErrInvalidPublicKey = fmt.Errorf("ecies: invalid public key")
ErrSharedKeyIsPointAtInfinity = fmt.Errorf("ecies: shared key is point at infinity")
ErrSharedKeyTooBig = fmt.Errorf("ecies: shared key params are too big")
ErrImport = errors.New("ecies: failed to import key")
ErrInvalidCurve = errors.New("ecies: invalid elliptic curve")
ErrInvalidPublicKey = errors.New("ecies: invalid public key")
ErrSharedKeyIsPointAtInfinity = errors.New("ecies: shared key is point at infinity")
ErrSharedKeyTooBig = errors.New("ecies: shared key params are too big")
)
// PublicKey is a representation of an elliptic curve public key.
@ -138,8 +138,8 @@ func (prv *PrivateKey) GenerateShared(pub *PublicKey, skLen, macLen int) (sk []b
}
var (
ErrSharedTooLong = fmt.Errorf("ecies: shared secret is too long")
ErrInvalidMessage = fmt.Errorf("ecies: invalid message")
ErrSharedTooLong = errors.New("ecies: shared secret is too long")
ErrInvalidMessage = errors.New("ecies: invalid message")
)
// NIST SP 800-56 Concatenation Key Derivation Function (see section 5.8.1).

View File

@ -35,7 +35,7 @@ import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"errors"
"math/big"
"testing"
@ -62,7 +62,7 @@ func TestKDF(t *testing.T) {
}
}
var ErrBadSharedKeys = fmt.Errorf("ecies: shared keys don't match")
var ErrBadSharedKeys = errors.New("ecies: shared keys don't match")
// cmpParams compares a set of ECIES parameters. We assume, as per the
// docs, that AES is the only supported symmetric encryption algorithm.

View File

@ -39,6 +39,7 @@ import (
"crypto/elliptic"
"crypto/sha256"
"crypto/sha512"
"errors"
"fmt"
"hash"
@ -47,8 +48,8 @@ import (
var (
DefaultCurve = ethcrypto.S256()
ErrUnsupportedECDHAlgorithm = fmt.Errorf("ecies: unsupported ECDH algorithm")
ErrUnsupportedECIESParameters = fmt.Errorf("ecies: unsupported ECIES parameters")
ErrUnsupportedECDHAlgorithm = errors.New("ecies: unsupported ECDH algorithm")
ErrUnsupportedECIESParameters = errors.New("ecies: unsupported ECIES parameters")
ErrInvalidKeyLen = fmt.Errorf("ecies: invalid key size (> %d) in ECIESParams", maxKeyLen)
)

View File

@ -22,6 +22,7 @@ package crypto
import (
"crypto/ecdsa"
"crypto/elliptic"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common/math"
@ -72,7 +73,7 @@ func VerifySignature(pubkey, digestHash, signature []byte) bool {
func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) {
x, y := secp256k1.DecompressPubkey(pubkey)
if x == nil {
return nil, fmt.Errorf("invalid public key")
return nil, errors.New("invalid public key")
}
return &ecdsa.PublicKey{X: x, Y: y, Curve: S256()}, nil
}

View File

@ -74,12 +74,12 @@ func Sign(hash []byte, prv *ecdsa.PrivateKey) ([]byte, error) {
return nil, fmt.Errorf("hash is required to be exactly 32 bytes (%d)", len(hash))
}
if prv.Curve != btcec.S256() {
return nil, fmt.Errorf("private key curve is not secp256k1")
return nil, errors.New("private key curve is not secp256k1")
}
// ecdsa.PrivateKey -> btcec.PrivateKey
var priv btcec.PrivateKey
if overflow := priv.Key.SetByteSlice(prv.D.Bytes()); overflow || priv.Key.IsZero() {
return nil, fmt.Errorf("invalid private key")
return nil, errors.New("invalid private key")
}
defer priv.Zero()
sig, err := btc_ecdsa.SignCompact(&priv, hash, false) // ref uncompressed pubkey