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

@ -141,11 +141,11 @@ func toECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) {
// The priv.D must < N // The priv.D must < N
if priv.D.Cmp(secp256k1N) >= 0 { 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. // The priv.D must not be zero or negative.
if priv.D.Sign() <= 0 { 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) 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 { if err != nil {
return nil, err return nil, err
} else if n != len(buf) { } 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 { if err := checkKeyFileEnd(r); err != nil {
return nil, err return nil, err

@ -36,18 +36,18 @@ import (
"crypto/hmac" "crypto/hmac"
"crypto/subtle" "crypto/subtle"
"encoding/binary" "encoding/binary"
"fmt" "errors"
"hash" "hash"
"io" "io"
"math/big" "math/big"
) )
var ( var (
ErrImport = fmt.Errorf("ecies: failed to import key") ErrImport = errors.New("ecies: failed to import key")
ErrInvalidCurve = fmt.Errorf("ecies: invalid elliptic curve") ErrInvalidCurve = errors.New("ecies: invalid elliptic curve")
ErrInvalidPublicKey = fmt.Errorf("ecies: invalid public key") ErrInvalidPublicKey = errors.New("ecies: invalid public key")
ErrSharedKeyIsPointAtInfinity = fmt.Errorf("ecies: shared key is point at infinity") ErrSharedKeyIsPointAtInfinity = errors.New("ecies: shared key is point at infinity")
ErrSharedKeyTooBig = fmt.Errorf("ecies: shared key params are too big") ErrSharedKeyTooBig = errors.New("ecies: shared key params are too big")
) )
// PublicKey is a representation of an elliptic curve public key. // 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 ( var (
ErrSharedTooLong = fmt.Errorf("ecies: shared secret is too long") ErrSharedTooLong = errors.New("ecies: shared secret is too long")
ErrInvalidMessage = fmt.Errorf("ecies: invalid message") ErrInvalidMessage = errors.New("ecies: invalid message")
) )
// NIST SP 800-56 Concatenation Key Derivation Function (see section 5.8.1). // NIST SP 800-56 Concatenation Key Derivation Function (see section 5.8.1).

@ -35,7 +35,7 @@ import (
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"fmt" "errors"
"math/big" "math/big"
"testing" "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 // cmpParams compares a set of ECIES parameters. We assume, as per the
// docs, that AES is the only supported symmetric encryption algorithm. // docs, that AES is the only supported symmetric encryption algorithm.

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

@ -22,6 +22,7 @@ package crypto
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
"errors"
"fmt" "fmt"
"github.com/ethereum/go-ethereum/common/math" "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) { func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) {
x, y := secp256k1.DecompressPubkey(pubkey) x, y := secp256k1.DecompressPubkey(pubkey)
if x == nil { 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 return &ecdsa.PublicKey{X: x, Y: y, Curve: S256()}, nil
} }

@ -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)) return nil, fmt.Errorf("hash is required to be exactly 32 bytes (%d)", len(hash))
} }
if prv.Curve != btcec.S256() { 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 // ecdsa.PrivateKey -> btcec.PrivateKey
var priv btcec.PrivateKey var priv btcec.PrivateKey
if overflow := priv.Key.SetByteSlice(prv.D.Bytes()); overflow || priv.Key.IsZero() { 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() defer priv.Zero()
sig, err := btc_ecdsa.SignCompact(&priv, hash, false) // ref uncompressed pubkey sig, err := btc_ecdsa.SignCompact(&priv, hash, false) // ref uncompressed pubkey