Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #32 from libp2p/bug/key-equality
Browse files Browse the repository at this point in the history
Replace bytes.Equal -> subtle.ConstantTimeCompare
  • Loading branch information
bigs committed Jul 12, 2019
2 parents b5729d8 + 652a852 commit c3f7bb2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crypto/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (ePriv *ECDSAPrivateKey) Raw() ([]byte, error) {
return x509.MarshalECPrivateKey(ePriv.priv)
}

// Equals compares to private keys
// Equals compares two private keys
func (ePriv *ECDSAPrivateKey) Equals(o Key) bool {
oPriv, ok := o.(*ECDSAPrivateKey)
if !ok {
Expand Down
3 changes: 2 additions & 1 deletion crypto/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package crypto

import (
"bytes"
"crypto/subtle"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (k *Ed25519PrivateKey) Equals(o Key) bool {
return false
}

return bytes.Equal(k.k, edk.k)
return subtle.ConstantTimeCompare(k.k, edk.k) == 1
}

// GetPublic returns an ed25519 public key from a private key.
Expand Down
4 changes: 2 additions & 2 deletions crypto/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
package crypto

import (
"bytes"
"crypto/elliptic"
"crypto/hmac"
"crypto/rand"
"crypto/sha1"
"crypto/sha512"
"crypto/subtle"
"encoding/base64"
"errors"
"fmt"
Expand Down Expand Up @@ -364,5 +364,5 @@ func KeyEqual(k1, k2 Key) bool {

b1, err1 := k1.Bytes()
b2, err2 := k2.Bytes()
return bytes.Equal(b1, b2) && err1 == err2
return subtle.ConstantTimeCompare(b1, b2) == 1 && err1 == err2
}

0 comments on commit c3f7bb2

Please sign in to comment.