Unverified Commit c3f7bb21 authored by bigs's avatar bigs Committed by GitHub

Merge pull request #32 from libp2p/bug/key-equality

Replace bytes.Equal -> subtle.ConstantTimeCompare
parents b5729d89 652a852e
...@@ -115,7 +115,7 @@ func (ePriv *ECDSAPrivateKey) Raw() ([]byte, error) { ...@@ -115,7 +115,7 @@ func (ePriv *ECDSAPrivateKey) Raw() ([]byte, error) {
return x509.MarshalECPrivateKey(ePriv.priv) return x509.MarshalECPrivateKey(ePriv.priv)
} }
// Equals compares to private keys // Equals compares two private keys
func (ePriv *ECDSAPrivateKey) Equals(o Key) bool { func (ePriv *ECDSAPrivateKey) Equals(o Key) bool {
oPriv, ok := o.(*ECDSAPrivateKey) oPriv, ok := o.(*ECDSAPrivateKey)
if !ok { if !ok {
......
...@@ -2,6 +2,7 @@ package crypto ...@@ -2,6 +2,7 @@ package crypto
import ( import (
"bytes" "bytes"
"crypto/subtle"
"errors" "errors"
"fmt" "fmt"
"io" "io"
...@@ -70,7 +71,7 @@ func (k *Ed25519PrivateKey) Equals(o Key) bool { ...@@ -70,7 +71,7 @@ func (k *Ed25519PrivateKey) Equals(o Key) bool {
return false 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. // GetPublic returns an ed25519 public key from a private key.
......
...@@ -4,12 +4,12 @@ ...@@ -4,12 +4,12 @@
package crypto package crypto
import ( import (
"bytes"
"crypto/elliptic" "crypto/elliptic"
"crypto/hmac" "crypto/hmac"
"crypto/rand" "crypto/rand"
"crypto/sha1" "crypto/sha1"
"crypto/sha512" "crypto/sha512"
"crypto/subtle"
"encoding/base64" "encoding/base64"
"errors" "errors"
"fmt" "fmt"
...@@ -364,5 +364,5 @@ func KeyEqual(k1, k2 Key) bool { ...@@ -364,5 +364,5 @@ func KeyEqual(k1, k2 Key) bool {
b1, err1 := k1.Bytes() b1, err1 := k1.Bytes()
b2, err2 := k2.Bytes() b2, err2 := k2.Bytes()
return bytes.Equal(b1, b2) && err1 == err2 return subtle.ConstantTimeCompare(b1, b2) == 1 && err1 == err2
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment