2015-01-18 19:24:09 -06:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2015-01-18 22:53:48 -06:00
|
|
|
"bytes"
|
2015-01-28 21:16:10 -06:00
|
|
|
"crypto/ecdsa"
|
2015-02-04 20:07:58 -06:00
|
|
|
"crypto/rand"
|
2015-01-21 08:42:12 -06:00
|
|
|
"net"
|
2015-01-18 19:24:09 -06:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-01-20 10:47:46 -06:00
|
|
|
"github.com/obscuren/ecies"
|
2015-01-18 19:24:09 -06:00
|
|
|
)
|
|
|
|
|
2015-01-20 10:47:46 -06:00
|
|
|
func TestPublicKeyEncoding(t *testing.T) {
|
|
|
|
prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
|
|
|
pub0 := &prv0.PublicKey
|
|
|
|
pub0s := crypto.FromECDSAPub(pub0)
|
2015-02-04 20:07:58 -06:00
|
|
|
pub1, err := importPublicKey(pub0s)
|
2015-01-20 10:47:46 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
eciesPub1 := ecies.ImportECDSAPublic(pub1)
|
|
|
|
if eciesPub1 == nil {
|
|
|
|
t.Errorf("invalid ecdsa public key")
|
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
pub1s, err := exportPublicKey(pub1)
|
2015-01-20 10:47:46 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
if len(pub1s) != 64 {
|
|
|
|
t.Errorf("wrong length expect 64, got", len(pub1s))
|
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
pub2, err := importPublicKey(pub1s)
|
2015-01-20 10:47:46 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
pub2s, err := exportPublicKey(pub2)
|
2015-01-20 10:47:46 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(pub1s, pub2s) {
|
|
|
|
t.Errorf("exports dont match")
|
|
|
|
}
|
|
|
|
pub2sEC := crypto.FromECDSAPub(pub2)
|
|
|
|
if !bytes.Equal(pub0s, pub2sEC) {
|
|
|
|
t.Errorf("exports dont match")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSharedSecret(t *testing.T) {
|
|
|
|
prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
|
|
|
pub0 := &prv0.PublicKey
|
|
|
|
prv1, _ := crypto.GenerateKey()
|
|
|
|
pub1 := &prv1.PublicKey
|
|
|
|
|
|
|
|
ss0, err := ecies.ImportECDSA(prv0).GenerateShared(ecies.ImportECDSAPublic(pub1), sskLen, sskLen)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ss1, err := ecies.ImportECDSA(prv1).GenerateShared(ecies.ImportECDSAPublic(pub0), sskLen, sskLen)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Logf("Secret:\n%v %x\n%v %x", len(ss0), ss0, len(ss0), ss1)
|
|
|
|
if !bytes.Equal(ss0, ss1) {
|
|
|
|
t.Errorf("dont match :(")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-18 19:24:09 -06:00
|
|
|
func TestCryptoHandshake(t *testing.T) {
|
2015-02-04 20:07:58 -06:00
|
|
|
testCryptoHandshake(newkey(), newkey(), nil, t)
|
2015-01-28 21:16:10 -06:00
|
|
|
}
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
func TestCryptoHandshakeWithToken(t *testing.T) {
|
|
|
|
sessionToken := make([]byte, shaLen)
|
|
|
|
rand.Read(sessionToken)
|
|
|
|
testCryptoHandshake(newkey(), newkey(), sessionToken, t)
|
2015-01-28 21:16:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func testCryptoHandshake(prv0, prv1 *ecdsa.PrivateKey, sessionToken []byte, t *testing.T) {
|
2015-01-18 19:24:09 -06:00
|
|
|
var err error
|
2015-02-04 20:07:58 -06:00
|
|
|
// pub0 := &prv0.PublicKey
|
2015-01-19 17:42:13 -06:00
|
|
|
pub1 := &prv1.PublicKey
|
2015-01-18 19:24:09 -06:00
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
// pub0s := crypto.FromECDSAPub(pub0)
|
2015-01-26 10:16:23 -06:00
|
|
|
pub1s := crypto.FromECDSAPub(pub1)
|
2015-01-18 19:24:09 -06:00
|
|
|
|
2015-01-19 17:42:13 -06:00
|
|
|
// simulate handshake by feeding output to input
|
2015-01-19 18:23:10 -06:00
|
|
|
// initiator sends handshake 'auth'
|
2015-02-04 20:07:58 -06:00
|
|
|
auth, initNonce, randomPrivKey, err := authMsg(prv0, pub1s, sessionToken)
|
2015-01-20 10:47:46 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
t.Logf("-> %v", hexkey(auth))
|
2015-01-20 10:47:46 -06:00
|
|
|
|
2015-01-19 18:23:10 -06:00
|
|
|
// receiver reads auth and responds with response
|
2015-02-04 20:07:58 -06:00
|
|
|
response, remoteRecNonce, remoteInitNonce, _, remoteRandomPrivKey, remoteInitRandomPubKey, err := authResp(auth, sessionToken, prv1)
|
2015-01-20 10:47:46 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
t.Logf("<- %v\n", hexkey(response))
|
2015-01-20 10:47:46 -06:00
|
|
|
|
2015-01-19 18:23:10 -06:00
|
|
|
// initiator reads receiver's response and the key exchange completes
|
2015-01-26 10:16:23 -06:00
|
|
|
recNonce, remoteRandomPubKey, _, err := completeHandshake(response, prv0)
|
2015-01-20 10:47:46 -06:00
|
|
|
if err != nil {
|
2015-02-04 20:07:58 -06:00
|
|
|
t.Errorf("completeHandshake error: %v", err)
|
2015-01-20 10:47:46 -06:00
|
|
|
}
|
2015-01-18 22:53:48 -06:00
|
|
|
|
2015-01-19 18:23:10 -06:00
|
|
|
// now both parties should have the same session parameters
|
2015-02-04 20:07:58 -06:00
|
|
|
initSessionToken, err := newSession(initNonce, recNonce, randomPrivKey, remoteRandomPubKey)
|
2015-01-20 10:47:46 -06:00
|
|
|
if err != nil {
|
2015-02-04 20:07:58 -06:00
|
|
|
t.Errorf("newSession error: %v", err)
|
2015-01-20 10:47:46 -06:00
|
|
|
}
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
recSessionToken, err := newSession(remoteInitNonce, remoteRecNonce, remoteRandomPrivKey, remoteInitRandomPubKey)
|
2015-01-20 10:47:46 -06:00
|
|
|
if err != nil {
|
2015-02-04 20:07:58 -06:00
|
|
|
t.Errorf("newSession error: %v", err)
|
2015-01-20 10:47:46 -06:00
|
|
|
}
|
2015-01-18 22:53:48 -06:00
|
|
|
|
2015-01-28 21:16:10 -06:00
|
|
|
// fmt.Printf("\nauth (%v) %x\n\nresp (%v) %x\n\n", len(auth), auth, len(response), response)
|
2015-01-21 08:42:12 -06:00
|
|
|
|
|
|
|
// fmt.Printf("\nauth %x\ninitNonce %x\nresponse%x\nremoteRecNonce %x\nremoteInitNonce %x\nremoteRandomPubKey %x\nrecNonce %x\nremoteInitRandomPubKey %x\ninitSessionToken %x\n\n", auth, initNonce, response, remoteRecNonce, remoteInitNonce, remoteRandomPubKey, recNonce, remoteInitRandomPubKey, initSessionToken)
|
2015-01-18 22:53:48 -06:00
|
|
|
|
2015-01-19 18:41:45 -06:00
|
|
|
if !bytes.Equal(initNonce, remoteInitNonce) {
|
|
|
|
t.Errorf("nonces do not match")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(recNonce, remoteRecNonce) {
|
|
|
|
t.Errorf("receiver nonces do not match")
|
|
|
|
}
|
2015-01-19 17:42:13 -06:00
|
|
|
if !bytes.Equal(initSessionToken, recSessionToken) {
|
2015-01-18 22:53:48 -06:00
|
|
|
t.Errorf("session tokens do not match")
|
|
|
|
}
|
2015-01-18 19:24:09 -06:00
|
|
|
}
|
2015-01-21 08:42:12 -06:00
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
func TestHandshake(t *testing.T) {
|
2015-01-21 08:42:12 -06:00
|
|
|
defer testlog(t).detach()
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
prv0, _ := crypto.GenerateKey()
|
|
|
|
prv1, _ := crypto.GenerateKey()
|
|
|
|
pub0s, _ := exportPublicKey(&prv0.PublicKey)
|
|
|
|
pub1s, _ := exportPublicKey(&prv1.PublicKey)
|
|
|
|
rw0, rw1 := net.Pipe()
|
|
|
|
tokens := make(chan []byte)
|
2015-01-21 08:42:12 -06:00
|
|
|
|
|
|
|
go func() {
|
2015-02-04 20:07:58 -06:00
|
|
|
token, err := outboundEncHandshake(rw0, prv0, pub1s, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("outbound side error: %v", err)
|
|
|
|
}
|
|
|
|
tokens <- token
|
2015-01-21 08:42:12 -06:00
|
|
|
}()
|
|
|
|
go func() {
|
2015-02-04 20:07:58 -06:00
|
|
|
token, remotePubkey, err := inboundEncHandshake(rw1, prv1, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("inbound side error: %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(remotePubkey, pub0s) {
|
|
|
|
t.Errorf("inbound side returned wrong remote pubkey\n got: %x\n want: %x", remotePubkey, pub0s)
|
|
|
|
}
|
|
|
|
tokens <- token
|
2015-01-21 08:42:12 -06:00
|
|
|
}()
|
2015-02-04 20:07:58 -06:00
|
|
|
|
|
|
|
t1, t2 := <-tokens, <-tokens
|
|
|
|
if !bytes.Equal(t1, t2) {
|
|
|
|
t.Error("session token mismatch")
|
2015-01-21 08:42:12 -06:00
|
|
|
}
|
|
|
|
}
|