Skip to content

Commit b05644b

Browse files
Improve ErrInvalidKeyType error messages (#361)
* Improve ErrInvalidKeyType error message * add specific expected type to error message * fix ErrInvalidKey error to ErrInvalidKeyType in rsa and rsapss * format * revert changes from example_test.go remove the comments * fix: udpate the signing names to uppercase
1 parent a49fa5d commit b05644b

File tree

5 files changed

+10
-10
lines changed

5 files changed

+10
-10
lines changed

ecdsa.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (m *SigningMethodECDSA) Verify(signingString string, sig []byte, key interf
6262
case *ecdsa.PublicKey:
6363
ecdsaKey = k
6464
default:
65-
return ErrInvalidKeyType
65+
return newError("ECDSA verify expects *ecsda.PublicKey", ErrInvalidKeyType)
6666
}
6767

6868
if len(sig) != 2*m.KeySize {
@@ -96,7 +96,7 @@ func (m *SigningMethodECDSA) Sign(signingString string, key interface{}) ([]byte
9696
case *ecdsa.PrivateKey:
9797
ecdsaKey = k
9898
default:
99-
return nil, ErrInvalidKeyType
99+
return nil, newError("ECDSA sign expects *ecsda.PrivateKey", ErrInvalidKeyType)
100100
}
101101

102102
// Create the hasher

ed25519.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (m *SigningMethodEd25519) Verify(signingString string, sig []byte, key inte
3838
var ok bool
3939

4040
if ed25519Key, ok = key.(ed25519.PublicKey); !ok {
41-
return ErrInvalidKeyType
41+
return newError("Ed25519 verify expects ed25519.PublicKey", ErrInvalidKeyType)
4242
}
4343

4444
if len(ed25519Key) != ed25519.PublicKeySize {
@@ -60,7 +60,7 @@ func (m *SigningMethodEd25519) Sign(signingString string, key interface{}) ([]by
6060
var ok bool
6161

6262
if ed25519Key, ok = key.(crypto.Signer); !ok {
63-
return nil, ErrInvalidKeyType
63+
return nil, newError("Ed25519 sign expects crypto.Signer", ErrInvalidKeyType)
6464
}
6565

6666
if _, ok := ed25519Key.Public().(ed25519.PublicKey); !ok {

hmac.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (m *SigningMethodHMAC) Verify(signingString string, sig []byte, key interfa
5959
// Verify the key is the right type
6060
keyBytes, ok := key.([]byte)
6161
if !ok {
62-
return ErrInvalidKeyType
62+
return newError("HMAC verify expects []byte", ErrInvalidKeyType)
6363
}
6464

6565
// Can we use the specified hashing method?
@@ -91,7 +91,7 @@ func (m *SigningMethodHMAC) Verify(signingString string, sig []byte, key interfa
9191
func (m *SigningMethodHMAC) Sign(signingString string, key interface{}) ([]byte, error) {
9292
if keyBytes, ok := key.([]byte); ok {
9393
if !m.Hash.Available() {
94-
return nil, ErrHashUnavailable
94+
return nil, newError("HMAC sign expects []byte", ErrInvalidKeyType)
9595
}
9696

9797
hasher := hmac.New(m.Hash.New, keyBytes)

rsa.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (m *SigningMethodRSA) Verify(signingString string, sig []byte, key interfac
5151
var ok bool
5252

5353
if rsaKey, ok = key.(*rsa.PublicKey); !ok {
54-
return ErrInvalidKeyType
54+
return newError("RSA verify expects *rsa.PublicKey", ErrInvalidKeyType)
5555
}
5656

5757
// Create hasher
@@ -73,7 +73,7 @@ func (m *SigningMethodRSA) Sign(signingString string, key interface{}) ([]byte,
7373

7474
// Validate type of key
7575
if rsaKey, ok = key.(*rsa.PrivateKey); !ok {
76-
return nil, ErrInvalidKey
76+
return nil, newError("RSA sign expects *rsa.PrivateKey", ErrInvalidKeyType)
7777
}
7878

7979
// Create the hasher

rsa_pss.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (m *SigningMethodRSAPSS) Verify(signingString string, sig []byte, key inter
8888
case *rsa.PublicKey:
8989
rsaKey = k
9090
default:
91-
return ErrInvalidKey
91+
return newError("RSA-PSS verify expects *rsa.PublicKey", ErrInvalidKeyType)
9292
}
9393

9494
// Create hasher
@@ -115,7 +115,7 @@ func (m *SigningMethodRSAPSS) Sign(signingString string, key interface{}) ([]byt
115115
case *rsa.PrivateKey:
116116
rsaKey = k
117117
default:
118-
return nil, ErrInvalidKeyType
118+
return nil, newError("RSA-PSS sign expects *rsa.PrivateKey", ErrInvalidKeyType)
119119
}
120120

121121
// Create the hasher

0 commit comments

Comments
 (0)