2015-01-19 01:24:09 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2015-01-19 04:53:48 +00:00
|
|
|
"bytes"
|
2015-01-20 16:47:46 +00:00
|
|
|
// "crypto/ecdsa"
|
|
|
|
// "crypto/elliptic"
|
|
|
|
// "crypto/rand"
|
2015-01-19 01:24:09 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-01-20 16:47:46 +00:00
|
|
|
"github.com/obscuren/ecies"
|
2015-01-19 01:24:09 +00:00
|
|
|
)
|
|
|
|
|
2015-01-20 16:47:46 +00:00
|
|
|
func TestPublicKeyEncoding(t *testing.T) {
|
|
|
|
prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
|
|
|
pub0 := &prv0.PublicKey
|
|
|
|
pub0s := crypto.FromECDSAPub(pub0)
|
|
|
|
pub1, err := ImportPublicKey(pub0s)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
eciesPub1 := ecies.ImportECDSAPublic(pub1)
|
|
|
|
if eciesPub1 == nil {
|
|
|
|
t.Errorf("invalid ecdsa public key")
|
|
|
|
}
|
|
|
|
pub1s, err := ExportPublicKey(pub1)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
if len(pub1s) != 64 {
|
|
|
|
t.Errorf("wrong length expect 64, got", len(pub1s))
|
|
|
|
}
|
|
|
|
pub2, err := ImportPublicKey(pub1s)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
pub2s, err := ExportPublicKey(pub2)
|
|
|
|
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-19 01:24:09 +00:00
|
|
|
func TestCryptoHandshake(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var sessionToken []byte
|
2015-01-20 16:47:46 +00:00
|
|
|
prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
2015-01-19 23:42:13 +00:00
|
|
|
pub0 := &prv0.PublicKey
|
|
|
|
prv1, _ := crypto.GenerateKey()
|
|
|
|
pub1 := &prv1.PublicKey
|
2015-01-19 01:24:09 +00:00
|
|
|
|
2015-01-19 23:42:13 +00:00
|
|
|
var initiator, receiver *cryptoId
|
|
|
|
if initiator, err = newCryptoId(&peerId{crypto.FromECDSA(prv0), crypto.FromECDSAPub(pub0)}); err != nil {
|
2015-01-19 01:24:09 +00:00
|
|
|
return
|
|
|
|
}
|
2015-01-19 23:42:13 +00:00
|
|
|
if receiver, err = newCryptoId(&peerId{crypto.FromECDSA(prv1), crypto.FromECDSAPub(pub1)}); err != nil {
|
2015-01-19 01:24:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-19 23:42:13 +00:00
|
|
|
// simulate handshake by feeding output to input
|
2015-01-20 00:23:10 +00:00
|
|
|
// initiator sends handshake 'auth'
|
2015-01-20 16:47:46 +00:00
|
|
|
auth, initNonce, randomPrivKey, _, err := initiator.startHandshake(receiver.pubKeyS, sessionToken)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
2015-01-20 00:23:10 +00:00
|
|
|
// receiver reads auth and responds with response
|
2015-01-20 16:47:46 +00:00
|
|
|
response, remoteRecNonce, remoteInitNonce, remoteRandomPrivKey, remoteInitRandomPubKey, err := receiver.respondToHandshake(auth, crypto.FromECDSAPub(pub0), sessionToken)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
2015-01-20 00:23:10 +00:00
|
|
|
// initiator reads receiver's response and the key exchange completes
|
2015-01-20 16:47:46 +00:00
|
|
|
recNonce, remoteRandomPubKey, _, err := initiator.completeHandshake(response)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
2015-01-19 04:53:48 +00:00
|
|
|
|
2015-01-20 00:23:10 +00:00
|
|
|
// now both parties should have the same session parameters
|
2015-01-20 16:47:46 +00:00
|
|
|
initSessionToken, initSecretRW, err := initiator.newSession(initNonce, recNonce, auth, randomPrivKey, remoteRandomPubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
recSessionToken, recSecretRW, err := receiver.newSession(remoteInitNonce, remoteRecNonce, auth, remoteRandomPrivKey, remoteInitRandomPubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
2015-01-19 04:53:48 +00:00
|
|
|
|
2015-01-20 16:47:46 +00: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-19 04:53:48 +00:00
|
|
|
|
2015-01-20 00:41:45 +00: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 23:42:13 +00:00
|
|
|
if !bytes.Equal(initSessionToken, recSessionToken) {
|
2015-01-19 04:53:48 +00:00
|
|
|
t.Errorf("session tokens do not match")
|
|
|
|
}
|
|
|
|
// aesSecret, macSecret, egressMac, ingressMac
|
2015-01-19 23:42:13 +00:00
|
|
|
if !bytes.Equal(initSecretRW.aesSecret, recSecretRW.aesSecret) {
|
2015-01-19 04:53:48 +00:00
|
|
|
t.Errorf("AES secrets do not match")
|
|
|
|
}
|
2015-01-19 23:42:13 +00:00
|
|
|
if !bytes.Equal(initSecretRW.macSecret, recSecretRW.macSecret) {
|
2015-01-19 04:53:48 +00:00
|
|
|
t.Errorf("macSecrets do not match")
|
|
|
|
}
|
2015-01-19 23:42:13 +00:00
|
|
|
if !bytes.Equal(initSecretRW.egressMac, recSecretRW.egressMac) {
|
2015-01-19 04:53:48 +00:00
|
|
|
t.Errorf("egressMacs do not match")
|
|
|
|
}
|
2015-01-19 23:42:13 +00:00
|
|
|
if !bytes.Equal(initSecretRW.ingressMac, recSecretRW.ingressMac) {
|
2015-01-19 04:53:48 +00:00
|
|
|
t.Errorf("ingressMacs do not match")
|
|
|
|
}
|
2015-01-19 01:24:09 +00:00
|
|
|
|
|
|
|
}
|