diff --git a/crypto/key.go b/crypto/key.go index 32c6488ff59..8fcc13e7de2 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -13,7 +13,6 @@ import ( "crypto/sha256" "crypto/sha512" "hash" - "math/big" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto" @@ -97,26 +96,16 @@ func GenerateEKeyPair(curveName string) ([]byte, GenSharedKey, error) { return nil, nil, err } - var pubKey bytes.Buffer - pubKey.Write(x.Bytes()) - pubKey.Write(y.Bytes()) + pubKey := elliptic.Marshal(curve, x, y) + u.PErr("GenerateEKeyPair %d\n", len(pubKey)) done := func(theirPub []byte) ([]byte, error) { // Verify and unpack node's public key. - curveSize := curve.Params().BitSize - - if len(theirPub) != (curveSize / 4) { - u.PErr("Malformed public key: %v", theirPub) - return nil, fmt.Errorf("Malformed public key: %v != %v", len(theirPub), (curveSize / 4)) + x, y := elliptic.Unmarshal(curve, theirPub) + if x == nil { + return nil, fmt.Errorf("Malformed public key: %d %v", len(theirPub), theirPub) } - bound := (curveSize / 8) - x := big.NewInt(0) - y := big.NewInt(0) - - x.SetBytes(theirPub[0:bound]) - y.SetBytes(theirPub[bound : bound*2]) - if !curve.IsOnCurve(x, y) { return nil, errors.New("Invalid public key.") } @@ -127,7 +116,7 @@ func GenerateEKeyPair(curveName string) ([]byte, GenSharedKey, error) { return secret.Bytes(), nil } - return pubKey.Bytes(), done, nil + return pubKey, done, nil } // Generates a set of keys for each party by stretching the shared key. diff --git a/crypto/spipe/handshake.go b/crypto/spipe/handshake.go index f42f13aaa63..a29d3c1d2b8 100644 --- a/crypto/spipe/handshake.go +++ b/crypto/spipe/handshake.go @@ -119,7 +119,7 @@ func (s *SecurePipe) handshake() error { } // u.POut("Selected %s %s %s\n", exchange, cipherType, hashType) - epubkey, done, err := ci.GenerateEKeyPair(exchange) // Generate EphemeralPubKey + epubkey, genSharedKey, err := ci.GenerateEKeyPair(exchange) // Generate EphemeralPubKey var handshake bytes.Buffer // Gather corpus to sign. handshake.Write(encoded) @@ -173,7 +173,7 @@ func (s *SecurePipe) handshake() error { return errors.New("Bad signature!") } - secret, err := done(exchangeResp.GetEpubkey()) + secret, err := genSharedKey(exchangeResp.GetEpubkey()) if err != nil { return err }