github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/p2p/crypto_test.go (about)

     1  package p2p
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/ecdsa"
     6  	"crypto/rand"
     7  	"net"
     8  	"testing"
     9  
    10  	"github.com/jonasnick/go-ethereum/crypto"
    11  	"github.com/obscuren/ecies"
    12  )
    13  
    14  func TestPublicKeyEncoding(t *testing.T) {
    15  	prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
    16  	pub0 := &prv0.PublicKey
    17  	pub0s := crypto.FromECDSAPub(pub0)
    18  	pub1, err := importPublicKey(pub0s)
    19  	if err != nil {
    20  		t.Errorf("%v", err)
    21  	}
    22  	eciesPub1 := ecies.ImportECDSAPublic(pub1)
    23  	if eciesPub1 == nil {
    24  		t.Errorf("invalid ecdsa public key")
    25  	}
    26  	pub1s, err := exportPublicKey(pub1)
    27  	if err != nil {
    28  		t.Errorf("%v", err)
    29  	}
    30  	if len(pub1s) != 64 {
    31  		t.Errorf("wrong length expect 64, got", len(pub1s))
    32  	}
    33  	pub2, err := importPublicKey(pub1s)
    34  	if err != nil {
    35  		t.Errorf("%v", err)
    36  	}
    37  	pub2s, err := exportPublicKey(pub2)
    38  	if err != nil {
    39  		t.Errorf("%v", err)
    40  	}
    41  	if !bytes.Equal(pub1s, pub2s) {
    42  		t.Errorf("exports dont match")
    43  	}
    44  	pub2sEC := crypto.FromECDSAPub(pub2)
    45  	if !bytes.Equal(pub0s, pub2sEC) {
    46  		t.Errorf("exports dont match")
    47  	}
    48  }
    49  
    50  func TestSharedSecret(t *testing.T) {
    51  	prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
    52  	pub0 := &prv0.PublicKey
    53  	prv1, _ := crypto.GenerateKey()
    54  	pub1 := &prv1.PublicKey
    55  
    56  	ss0, err := ecies.ImportECDSA(prv0).GenerateShared(ecies.ImportECDSAPublic(pub1), sskLen, sskLen)
    57  	if err != nil {
    58  		return
    59  	}
    60  	ss1, err := ecies.ImportECDSA(prv1).GenerateShared(ecies.ImportECDSAPublic(pub0), sskLen, sskLen)
    61  	if err != nil {
    62  		return
    63  	}
    64  	t.Logf("Secret:\n%v %x\n%v %x", len(ss0), ss0, len(ss0), ss1)
    65  	if !bytes.Equal(ss0, ss1) {
    66  		t.Errorf("dont match :(")
    67  	}
    68  }
    69  
    70  func TestCryptoHandshake(t *testing.T) {
    71  	testCryptoHandshake(newkey(), newkey(), nil, t)
    72  }
    73  
    74  func TestCryptoHandshakeWithToken(t *testing.T) {
    75  	sessionToken := make([]byte, shaLen)
    76  	rand.Read(sessionToken)
    77  	testCryptoHandshake(newkey(), newkey(), sessionToken, t)
    78  }
    79  
    80  func testCryptoHandshake(prv0, prv1 *ecdsa.PrivateKey, sessionToken []byte, t *testing.T) {
    81  	var err error
    82  	// pub0 := &prv0.PublicKey
    83  	pub1 := &prv1.PublicKey
    84  
    85  	// pub0s := crypto.FromECDSAPub(pub0)
    86  	pub1s := crypto.FromECDSAPub(pub1)
    87  
    88  	// simulate handshake by feeding output to input
    89  	// initiator sends handshake 'auth'
    90  	auth, initNonce, randomPrivKey, err := authMsg(prv0, pub1s, sessionToken)
    91  	if err != nil {
    92  		t.Errorf("%v", err)
    93  	}
    94  	t.Logf("-> %v", hexkey(auth))
    95  
    96  	// receiver reads auth and responds with response
    97  	response, remoteRecNonce, remoteInitNonce, _, remoteRandomPrivKey, remoteInitRandomPubKey, err := authResp(auth, sessionToken, prv1)
    98  	if err != nil {
    99  		t.Errorf("%v", err)
   100  	}
   101  	t.Logf("<- %v\n", hexkey(response))
   102  
   103  	// initiator reads receiver's response and the key exchange completes
   104  	recNonce, remoteRandomPubKey, _, err := completeHandshake(response, prv0)
   105  	if err != nil {
   106  		t.Errorf("completeHandshake error: %v", err)
   107  	}
   108  
   109  	// now both parties should have the same session parameters
   110  	initSessionToken, err := newSession(initNonce, recNonce, randomPrivKey, remoteRandomPubKey)
   111  	if err != nil {
   112  		t.Errorf("newSession error: %v", err)
   113  	}
   114  
   115  	recSessionToken, err := newSession(remoteInitNonce, remoteRecNonce, remoteRandomPrivKey, remoteInitRandomPubKey)
   116  	if err != nil {
   117  		t.Errorf("newSession error: %v", err)
   118  	}
   119  
   120  	// fmt.Printf("\nauth (%v) %x\n\nresp (%v) %x\n\n", len(auth), auth, len(response), response)
   121  
   122  	// 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)
   123  
   124  	if !bytes.Equal(initNonce, remoteInitNonce) {
   125  		t.Errorf("nonces do not match")
   126  	}
   127  	if !bytes.Equal(recNonce, remoteRecNonce) {
   128  		t.Errorf("receiver nonces do not match")
   129  	}
   130  	if !bytes.Equal(initSessionToken, recSessionToken) {
   131  		t.Errorf("session tokens do not match")
   132  	}
   133  }
   134  
   135  func TestHandshake(t *testing.T) {
   136  	defer testlog(t).detach()
   137  
   138  	prv0, _ := crypto.GenerateKey()
   139  	prv1, _ := crypto.GenerateKey()
   140  	pub0s, _ := exportPublicKey(&prv0.PublicKey)
   141  	pub1s, _ := exportPublicKey(&prv1.PublicKey)
   142  	rw0, rw1 := net.Pipe()
   143  	tokens := make(chan []byte)
   144  
   145  	go func() {
   146  		token, err := outboundEncHandshake(rw0, prv0, pub1s, nil)
   147  		if err != nil {
   148  			t.Errorf("outbound side error: %v", err)
   149  		}
   150  		tokens <- token
   151  	}()
   152  	go func() {
   153  		token, remotePubkey, err := inboundEncHandshake(rw1, prv1, nil)
   154  		if err != nil {
   155  			t.Errorf("inbound side error: %v", err)
   156  		}
   157  		if !bytes.Equal(remotePubkey, pub0s) {
   158  			t.Errorf("inbound side returned wrong remote pubkey\n  got:  %x\n  want: %x", remotePubkey, pub0s)
   159  		}
   160  		tokens <- token
   161  	}()
   162  
   163  	t1, t2 := <-tokens, <-tokens
   164  	if !bytes.Equal(t1, t2) {
   165  		t.Error("session token mismatch")
   166  	}
   167  }