Merge pull request #46 from libp2p/feat/cherry-pick-stabalize

cherry-pick key optimizations from stabilize
This commit is contained in:
Steven Allen 2019-08-01 17:30:46 -07:00 committed by GitHub
commit 33faefebfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 83 additions and 25 deletions

View File

@ -119,7 +119,7 @@ func (ePriv *ECDSAPrivateKey) Raw() ([]byte, error) {
func (ePriv *ECDSAPrivateKey) Equals(o Key) bool {
oPriv, ok := o.(*ECDSAPrivateKey)
if !ok {
return false
return basicEquals(ePriv, o)
}
return ePriv.priv.D.Cmp(oPriv.priv.D) == 0
@ -163,7 +163,7 @@ func (ePub ECDSAPublicKey) Raw() ([]byte, error) {
func (ePub *ECDSAPublicKey) Equals(o Key) bool {
oPub, ok := o.(*ECDSAPublicKey)
if !ok {
return false
return basicEquals(ePub, o)
}
return ePub.pub.X != nil && ePub.pub.Y != nil && oPub.pub.X != nil && oPub.pub.Y != nil &&

View File

@ -363,7 +363,21 @@ func KeyEqual(k1, k2 Key) bool {
return true
}
b1, err1 := k1.Bytes()
b2, err2 := k2.Bytes()
return subtle.ConstantTimeCompare(b1, b2) == 1 && err1 == err2
return k1.Equals(k2)
}
func basicEquals(k1, k2 Key) bool {
if k1.Type() != k2.Type() {
return false
}
a, err := k1.Raw()
if err != nil {
return false
}
b, err := k2.Raw()
if err != nil {
return false
}
return subtle.ConstantTimeCompare(a, b) == 1
}

View File

@ -101,18 +101,19 @@ func testKeyEncoding(t *testing.T, sk PrivKey) {
}
func testKeyEquals(t *testing.T, k Key) {
kb, err := k.Bytes()
if err != nil {
t.Fatal(err)
}
// kb, err := k.Raw()
// if err != nil {
// t.Fatal(err)
// }
if !KeyEqual(k, k) {
t.Fatal("Key not equal to itself.")
}
if !KeyEqual(k, testkey(kb)) {
t.Fatal("Key not equal to key with same bytes.")
}
// bad test, relies on deep internals..
// if !KeyEqual(k, testkey(kb)) {
// t.Fatal("Key not equal to key with same bytes.")
// }
sk, pk, err := test.RandTestKeyPair(RSA, 512)
if err != nil {
@ -143,7 +144,20 @@ func (pk testkey) Raw() ([]byte, error) {
}
func (pk testkey) Equals(k Key) bool {
return KeyEqual(pk, k)
if pk.Type() != k.Type() {
return false
}
a, err := pk.Raw()
if err != nil {
return false
}
b, err := k.Raw()
if err != nil {
return false
}
return bytes.Equal(a, b)
}
func TestUnknownCurveErrors(t *testing.T) {

View File

@ -5,7 +5,7 @@ package crypto
import (
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
openssl "github.com/spacemonkeygo/openssl"
openssl "github.com/libp2p/go-openssl"
)
// define these as separate types so we can add more key types later and reuse
@ -61,7 +61,12 @@ func (pk *opensslPublicKey) Raw() ([]byte, error) {
// Equals checks whether this key is equal to another
func (pk *opensslPublicKey) Equals(k Key) bool {
return KeyEqual(pk, k)
k0, ok := k.(*RsaPublicKey)
if !ok {
return basicEquals(pk, k)
}
return pk.key.Equal(k0.opensslPublicKey.key)
}
// Sign returns a signature of the input data
@ -94,5 +99,10 @@ func (sk *opensslPrivateKey) Raw() ([]byte, error) {
// Equals checks whether this key is equal to another
func (sk *opensslPrivateKey) Equals(k Key) bool {
return KeyEqual(sk, k)
k0, ok := k.(*RsaPrivateKey)
if !ok {
return basicEquals(sk, k)
}
return sk.key.Equal(k0.opensslPrivateKey.key)
}

View File

@ -63,7 +63,13 @@ func (pk *RsaPublicKey) Raw() ([]byte, error) {
// Equals checks whether this key is equal to another
func (pk *RsaPublicKey) Equals(k Key) bool {
return KeyEqual(pk, k)
// make sure this is an rsa public key
other, ok := (k).(*RsaPublicKey)
if !ok {
return basicEquals(pk, k)
}
return pk.k.N.Cmp(other.k.N) == 0 && pk.k.E == other.k.E
}
// Sign returns a signature of the input data
@ -93,7 +99,17 @@ func (sk *RsaPrivateKey) Raw() ([]byte, error) {
// Equals checks whether this key is equal to another
func (sk *RsaPrivateKey) Equals(k Key) bool {
return KeyEqual(sk, k)
// make sure this is an rsa public key
other, ok := (k).(*RsaPrivateKey)
if !ok {
return basicEquals(sk, k)
}
a := sk.sk
b := other.sk
// Don't care about constant time. We're only comparing the public half.
return a.PublicKey.N.Cmp(b.PublicKey.N) == 0 && a.PublicKey.E == b.PublicKey.E
}
// UnmarshalRsaPrivateKey returns a private key from the input x509 bytes

View File

@ -6,7 +6,7 @@ import (
"errors"
"io"
openssl "github.com/spacemonkeygo/openssl"
openssl "github.com/libp2p/go-openssl"
)
// RsaPrivateKey is an rsa private key

View File

@ -66,10 +66,10 @@ func (k *Secp256k1PrivateKey) Raw() ([]byte, error) {
func (k *Secp256k1PrivateKey) Equals(o Key) bool {
sk, ok := o.(*Secp256k1PrivateKey)
if !ok {
return false
return basicEquals(k, o)
}
return k.D.Cmp(sk.D) == 0
return k.GetPublic().Equals(sk.GetPublic())
}
// Sign returns a signature from input data
@ -107,7 +107,7 @@ func (k *Secp256k1PublicKey) Raw() ([]byte, error) {
func (k *Secp256k1PublicKey) Equals(o Key) bool {
sk, ok := o.(*Secp256k1PublicKey)
if !ok {
return false
return basicEquals(k, o)
}
return (*btcec.PublicKey)(k).IsEqual((*btcec.PublicKey)(sk))

2
go.mod
View File

@ -8,12 +8,12 @@ require (
github.com/jbenet/goprocess v0.1.3
github.com/libp2p/go-flow-metrics v0.0.1
github.com/libp2p/go-msgio v0.0.4
github.com/libp2p/go-openssl v0.0.2
github.com/minio/sha256-simd v0.1.0
github.com/mr-tron/base58 v1.1.2
github.com/multiformats/go-multiaddr v0.0.4
github.com/multiformats/go-multihash v0.0.5
github.com/smola/gocompat v0.2.0
github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a
go.opencensus.io v0.21.0
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443
)

8
go.sum
View File

@ -67,7 +67,11 @@ github.com/libp2p/go-buffer-pool v0.0.1/go.mod h1:xtyIz9PMobb13WaxR6Zo1Pd1zXJKYg
github.com/libp2p/go-flow-metrics v0.0.1 h1:0gxuFd2GuK7IIP5pKljLwps6TvcuYgvG7Atqi3INF5s=
github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8=
github.com/libp2p/go-msgio v0.0.4 h1:agEFehY3zWJFUHK6SEMR7UYmk2z6kC3oeCM7ybLhguA=
github.com/libp2p/go-msgio v0.0.4 h1:agEFehY3zWJFUHK6SEMR7UYmk2z6kC3oeCM7ybLhguA=
github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ=
github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ=
github.com/libp2p/go-openssl v0.0.2 h1:9pP2d3Ubaxkv7ZisLjx9BFwgOGnQdQYnfcH29HNY3ls=
github.com/libp2p/go-openssl v0.0.2/go.mod h1:v8Zw2ijCSWBQi8Pq5GAixw6DbFfa9u6VIYDXnvOXkc0=
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329 h1:2gxZ0XQIU/5z3Z3bUBu+FXuk2pFbkN6tcwi/pjyaDic=
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
@ -106,8 +110,6 @@ github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/smola/gocompat v0.2.0 h1:6b1oIMlUXIpz//VKEDzPVBK8KG7beVwmHIUEBIs/Pns=
github.com/smola/gocompat v0.2.0/go.mod h1:1B0MlxbmoZNo3h8guHp8HztB3BSYR5itql9qtVc0ypY=
github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a h1:/eS3yfGjQKG+9kayBkj0ip1BGhq6zJ3eaVksphxAaek=
github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a/go.mod h1:7AyxJNCJ7SBZ1MfVQCWD6Uqo2oubI2Eq2y2eqf+A5r0=
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 h1:RC6RW7j+1+HkWaX/Yh71Ee5ZHaHYt7ZP4sQgUrm6cDU=
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w0SWMsp6j9O/dk4/ZpIhL+3CkG8ofA2vuv7k+ltqUMc=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
@ -154,6 +156,8 @@ golang.org/x/sys v0.0.0-20190219092855-153ac476189d h1:Z0Ahzd7HltpJtjAHHxX8QFP3j
golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb h1:fgwFCsaw9buMuxNd6+DQfAuSFqbNiQZpcgJQAgJsK6k=
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635 h1:2eB4G6bDQDeP69ZXbOKC00S2Kf6TIiRS+DzfKsKeQU0=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=