Skip to content
This repository has been archived by the owner on Jul 15, 2018. It is now read-only.

Commit

Permalink
make PrivateKey + Signature comparisons use constant time comparisons
Browse files Browse the repository at this point in the history
Fixes #43

Avoid susceptibility to timing/side channel attacks by ensuring
that private key and signature comparisons use
`subtle.ConstantTimeCompare`
instead of
`bytes.Equal`
  • Loading branch information
odeke-em committed Oct 26, 2017
1 parent 0418d32 commit 3df2ca1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 11 additions & 3 deletions priv_key.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package crypto

import (
"bytes"
"crypto/subtle"

secp256k1 "github.com/btcsuite/btcd/btcec"
"github.com/tendermint/ed25519"
Expand Down Expand Up @@ -57,7 +57,11 @@ func (privKey PrivKeyEd25519) PubKey() PubKey {

func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
if otherEd, ok := other.Unwrap().(PrivKeyEd25519); ok {
return bytes.Equal(privKey[:], otherEd[:])
// It is essential that we constant time compare
// private keys and signatures instead of bytes.Equal,
// to avoid susceptibility to timing/side channel attacks.
// See Issue https://github.com/tendermint/go-crypto/issues/43
return subtle.ConstantTimeCompare(privKey[:], otherEd[:]) == 0
} else {
return false
}
Expand Down Expand Up @@ -144,7 +148,11 @@ func (privKey PrivKeySecp256k1) PubKey() PubKey {

func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
if otherSecp, ok := other.Unwrap().(PrivKeySecp256k1); ok {
return bytes.Equal(privKey[:], otherSecp[:])
// It is essential that we constant time compare
// private keys and signatures instead of bytes.Equal,
// to avoid susceptibility to timing/side channel attacks.
// See Issue https://github.com/tendermint/go-crypto/issues/43
return subtle.ConstantTimeCompare(privKey[:], otherSecp[:]) == 0
} else {
return false
}
Expand Down
14 changes: 11 additions & 3 deletions signature.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package crypto

import (
"bytes"
"crypto/subtle"
"fmt"

"github.com/tendermint/go-wire"
Expand Down Expand Up @@ -46,7 +46,11 @@ func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fing

func (sig SignatureEd25519) Equals(other Signature) bool {
if otherEd, ok := other.Unwrap().(SignatureEd25519); ok {
return bytes.Equal(sig[:], otherEd[:])
// It is essential that we constant time compare
// private keys and signatures instead of bytes.Equal,
// to avoid susceptibility to timing/side channel attacks.
// See Issue https://github.com/tendermint/go-crypto/issues/43
return subtle.ConstantTimeCompare(sig[:], otherEd[:]) == 0
} else {
return false
}
Expand Down Expand Up @@ -82,7 +86,11 @@ func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fi

func (sig SignatureSecp256k1) Equals(other Signature) bool {
if otherEd, ok := other.Unwrap().(SignatureSecp256k1); ok {
return bytes.Equal(sig[:], otherEd[:])
// It is essential that we constant time compare
// private keys and signatures instead of bytes.Equal,
// to avoid susceptibility to timing/side channel attacks.
// See Issue https://github.com/tendermint/go-crypto/issues/43
return subtle.ConstantTimeCompare(sig[:], otherEd[:]) == 0
} else {
return false
}
Expand Down

0 comments on commit 3df2ca1

Please sign in to comment.