Browse Source

Merge pull request #53 from tendermint/constant-time-equals

Constant time equals
pull/1782/head
Ethan Buchman 7 years ago
committed by GitHub
parent
commit
43cc4fb645
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 5 deletions
  1. +7
    -3
      priv_key.go
  2. +2
    -2
      signature.go
  3. +24
    -0
      signature_test.go

+ 7
- 3
priv_key.go View File

@ -1,7 +1,7 @@
package crypto package crypto
import ( import (
"bytes"
"crypto/subtle"
secp256k1 "github.com/btcsuite/btcd/btcec" secp256k1 "github.com/btcsuite/btcd/btcec"
"github.com/tendermint/ed25519" "github.com/tendermint/ed25519"
@ -69,9 +69,11 @@ func (privKey PrivKeyEd25519) PubKey() PubKey {
return PubKeyEd25519(pubBytes).Wrap() return PubKeyEd25519(pubBytes).Wrap()
} }
// Equals - you probably don't need to use this.
// Runs in constant time based on length of the keys.
func (privKey PrivKeyEd25519) Equals(other PrivKey) bool { func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
if otherEd, ok := other.Unwrap().(PrivKeyEd25519); ok { if otherEd, ok := other.Unwrap().(PrivKeyEd25519); ok {
return bytes.Equal(privKey[:], otherEd[:])
return subtle.ConstantTimeCompare(privKey[:], otherEd[:]) == 1
} else { } else {
return false return false
} }
@ -156,9 +158,11 @@ func (privKey PrivKeySecp256k1) PubKey() PubKey {
return pub.Wrap() return pub.Wrap()
} }
// Equals - you probably don't need to use this.
// Runs in constant time based on length of the keys.
func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool { func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
if otherSecp, ok := other.Unwrap().(PrivKeySecp256k1); ok { if otherSecp, ok := other.Unwrap().(PrivKeySecp256k1); ok {
return bytes.Equal(privKey[:], otherSecp[:])
return subtle.ConstantTimeCompare(privKey[:], otherSecp[:]) == 1
} else { } else {
return false return false
} }


+ 2
- 2
signature.go View File

@ -87,8 +87,8 @@ func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 }
func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) } func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
func (sig SignatureSecp256k1) Equals(other Signature) bool { func (sig SignatureSecp256k1) Equals(other Signature) bool {
if otherEd, ok := other.Unwrap().(SignatureSecp256k1); ok {
return bytes.Equal(sig[:], otherEd[:])
if otherSecp, ok := other.Unwrap().(SignatureSecp256k1); ok {
return bytes.Equal(sig[:], otherSecp[:])
} else { } else {
return false return false
} }


+ 24
- 0
signature_test.go View File

@ -141,3 +141,27 @@ func TestWrapping(t *testing.T) {
} }
} }
func TestPrivKeyEquality(t *testing.T) {
{
privKey := GenPrivKeySecp256k1().Wrap()
privKey2 := GenPrivKeySecp256k1().Wrap()
assert.False(t, privKey.Equals(privKey2))
assert.False(t, privKey2.Equals(privKey))
privKeyCopy := privKey // copy
assert.True(t, privKey.Equals(privKeyCopy))
assert.True(t, privKeyCopy.Equals(privKey))
}
{
privKey := GenPrivKeyEd25519().Wrap()
privKey2 := GenPrivKeyEd25519().Wrap()
assert.False(t, privKey.Equals(privKey2))
assert.False(t, privKey2.Equals(privKey))
privKeyCopy := privKey // copy
assert.True(t, privKey.Equals(privKeyCopy))
assert.True(t, privKeyCopy.Equals(privKey))
}
}

Loading…
Cancel
Save