aboutsummaryrefslogtreecommitdiff
path: root/src/crypto/rsa/rsa.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-23 17:56:24 +0000
commit5c9bd499e103709a181f7a1a895d221ae6e7ffc8 (patch)
tree38942a3590c3aeec0ee44736f4ffbfd1d3414e24 /src/crypto/rsa/rsa.go
parent24925c7ed9da15fe780d0d2fce21bbaa707e8751 (diff)
downloadgo-5c9bd499e103709a181f7a1a895d221ae6e7ffc8.tar.gz
go-5c9bd499e103709a181f7a1a895d221ae6e7ffc8.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. Fixes #21704 Change-Id: Id5379c96384a11c5afde0614955360e7470bb1c4 Reviewed-on: https://go-review.googlesource.com/c/go/+/223754 Reviewed-by: Katie Hockman <katie@golang.org>
Diffstat (limited to 'src/crypto/rsa/rsa.go')
-rw-r--r--src/crypto/rsa/rsa.go9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/crypto/rsa/rsa.go b/src/crypto/rsa/rsa.go
index d058949242..5a42990640 100644
--- a/src/crypto/rsa/rsa.go
+++ b/src/crypto/rsa/rsa.go
@@ -50,6 +50,15 @@ func (pub *PublicKey) Size() int {
return (pub.N.BitLen() + 7) / 8
}
+// Equal reports whether pub and x have the same value.
+func (pub *PublicKey) Equal(x crypto.PublicKey) bool {
+ xx, ok := x.(*PublicKey)
+ if !ok {
+ return false
+ }
+ return pub.N.Cmp(xx.N) == 0 && pub.E == xx.E
+}
+
// OAEPOptions is an interface for passing options to OAEP decryption using the
// crypto.Decrypter interface.
type OAEPOptions struct {