Skip to content

Commit 3533096

Browse files
committed
adding isvalid method to ecdsa publickey
1 parent 62b29b0 commit 3533096

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/crypto/ecdsa/ecdsa.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,39 @@ func (pub *PublicKey) Equal(x crypto.PublicKey) bool {
8383
pub.Curve == xx.Curve
8484
}
8585

86+
// IsValid reports where pub is a valid public key on the curve
87+
//
88+
// Valid Elliptic Curve Public Keys have the properties according to
89+
// 'Validation of Elliptic Curve Public Keys' 2003 by Antipa, Brown, Menezes, and
90+
// Vanstone as well as NIST SP 800-56A Section 5.6.2.3.
91+
func (pub *PublicKey) IsValid() bool {
92+
93+
// IsOnCurve will return false if The public Key:
94+
// - is an infinity point, O
95+
// - is on the curve
96+
// If either is false the Public key is invalid
97+
if !pub.Curve.IsOnCurve(pub.X, pub.Y) {
98+
return false
99+
}
100+
101+
// is each coordinate in pub
102+
// >0 and <P
103+
// else invalid
104+
params := pub.Curve.Params()
105+
if pub.X.Sign() < 0 ||
106+
pub.Y.Sign() < 0 ||
107+
pub.X.Cmp(params.P) > 0 ||
108+
pub.Y.Cmp(params.P) > 0 {
109+
return false
110+
}
111+
112+
// The fourth condition only applies to
113+
// curves with cofactors > 1 like Curve25519
114+
// right now there is no interface to determine that
115+
116+
return true
117+
}
118+
86119
// PrivateKey represents an ECDSA private key.
87120
type PrivateKey struct {
88121
PublicKey

0 commit comments

Comments
 (0)