aboutsummaryrefslogtreecommitdiff
path: root/src/crypto/ecdsa/ecdsa.go
diff options
context:
space:
mode:
authorFilippo Valsorda <filippo@golang.org>2020-03-17 20:34:51 -0400
committerFilippo Valsorda <filippo@golang.org>2020-03-26 15:00:36 +0000
commitb5f2c0f50297fa5cd14af668ddd7fd923626cf8c (patch)
tree58af599ee0724097371cb06a1e5d9ba8214cc2c5 /src/crypto/ecdsa/ecdsa.go
parentf9c5ef8d8f94b364c758930f64b9305c52200b5b (diff)
downloadgo-b5f2c0f50297fa5cd14af668ddd7fd923626cf8c.tar.gz
go-b5f2c0f50297fa5cd14af668ddd7fd923626cf8c.zip
crypto/rsa,crypto/ecdsa,crypto/ed25519: implement PublicKey.Equal
This makes all modern public keys in the standard library implement a common interface (below) that can be used by applications for better type safety and allows for checking that public (and private keys via Public()) are equivalent. interface { Equal(crypto.PublicKey) bool } Equality for ECDSA keys is complicated, we take a strict interpretation that works for all secure applications (the ones not using the unfortunate non-constant time CurveParams implementation) and fails closed otherwise. Tests in separate files to make them x_tests and avoid an import loop with crypto/x509. Re-landing of CL 223754. Dropped the test that was assuming named curves are not implemented by CurveParams, because it's not true for all curves, and anyway is not a property we need to test. There is still a test to check that different curves make keys not Equal. Fixes #21704 Fixes #38035 Reviewed-on: https://go-review.googlesource.com/c/go/+/223754 Reviewed-by: Katie Hockman <katie@golang.org> Change-Id: I736759b145bfb4f7f8eecd78c324315d5a05385c Reviewed-on: https://go-review.googlesource.com/c/go/+/225460 Run-TryBot: Filippo Valsorda <filippo@golang.org> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/crypto/ecdsa/ecdsa.go')
-rw-r--r--src/crypto/ecdsa/ecdsa.go18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/crypto/ecdsa/ecdsa.go b/src/crypto/ecdsa/ecdsa.go
index 744182aac2..189399d126 100644
--- a/src/crypto/ecdsa/ecdsa.go
+++ b/src/crypto/ecdsa/ecdsa.go
@@ -62,6 +62,24 @@ type PublicKey struct {
X, Y *big.Int
}
+// Equal reports whether pub and x have the same value.
+//
+// Two keys are only considered to have the same value if they have the same Curve value.
+// Note that for example elliptic.P256() and elliptic.P256().Params() are different
+// values, as the latter is a generic not constant time implementation.
+func (pub *PublicKey) Equal(x crypto.PublicKey) bool {
+ xx, ok := x.(*PublicKey)
+ if !ok {
+ return false
+ }
+ return pub.X.Cmp(xx.X) == 0 && pub.Y.Cmp(xx.Y) == 0 &&
+ // Standard library Curve implementations are singletons, so this check
+ // will work for those. Other Curves might be equivalent even if not
+ // singletons, but there is no definitive way to check for that, and
+ // better to err on the side of safety.
+ pub.Curve == xx.Curve
+}
+
// PrivateKey represents an ECDSA private key.
type PrivateKey struct {
PublicKey