aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/crypto_test.go
diff options
context:
space:
mode:
authorzelig <viktor.tron@gmail.com>2015-01-21 00:47:46 +0800
committerFelix Lange <fjl@twurst.com>2015-02-06 07:00:35 +0800
commit364b7832811c19106456b3b30a34c7097a0b526e (patch)
treef3143c8bdfdb3a2eedab9bb4a00b3df0116602e6 /p2p/crypto_test.go
parent58fc2c679b3d3b987aeefc98aa22db5f02717638 (diff)
downloadgo-tangerine-364b7832811c19106456b3b30a34c7097a0b526e.tar
go-tangerine-364b7832811c19106456b3b30a34c7097a0b526e.tar.gz
go-tangerine-364b7832811c19106456b3b30a34c7097a0b526e.tar.bz2
go-tangerine-364b7832811c19106456b3b30a34c7097a0b526e.tar.lz
go-tangerine-364b7832811c19106456b3b30a34c7097a0b526e.tar.xz
go-tangerine-364b7832811c19106456b3b30a34c7097a0b526e.tar.zst
go-tangerine-364b7832811c19106456b3b30a34c7097a0b526e.zip
changes that fix it all:
- set proper public key serialisation length in pubLen = 64 - reset all sizes and offsets - rename from DER to S (we are not using DER encoding) - add remoteInitRandomPubKey as return value to respondToHandshake - add ImportPublicKey with error return to read both EC golang.elliptic style 65 byte encoding and 64 byte one - add ExportPublicKey falling back to go-ethereum/crypto.FromECDSAPub() chopping off the first byte - add Import - Export tests - all tests pass
Diffstat (limited to 'p2p/crypto_test.go')
-rw-r--r--p2p/crypto_test.go92
1 files changed, 85 insertions, 7 deletions
diff --git a/p2p/crypto_test.go b/p2p/crypto_test.go
index 89baca084..8000efaf1 100644
--- a/p2p/crypto_test.go
+++ b/p2p/crypto_test.go
@@ -2,16 +2,76 @@ package p2p
import (
"bytes"
+ // "crypto/ecdsa"
+ // "crypto/elliptic"
+ // "crypto/rand"
"fmt"
"testing"
"github.com/ethereum/go-ethereum/crypto"
+ "github.com/obscuren/ecies"
)
+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 :(")
+ }
+}
+
func TestCryptoHandshake(t *testing.T) {
var err error
var sessionToken []byte
- prv0, _ := crypto.GenerateKey()
+ prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
pub0 := &prv0.PublicKey
prv1, _ := crypto.GenerateKey()
pub1 := &prv1.PublicKey
@@ -26,17 +86,35 @@ func TestCryptoHandshake(t *testing.T) {
// simulate handshake by feeding output to input
// initiator sends handshake 'auth'
- auth, initNonce, randomPrivKey, _, _ := initiator.startHandshake(receiver.pubKeyDER, sessionToken)
+ auth, initNonce, randomPrivKey, _, err := initiator.startHandshake(receiver.pubKeyS, sessionToken)
+ if err != nil {
+ t.Errorf("%v", err)
+ }
+
// receiver reads auth and responds with response
- response, remoteRecNonce, remoteInitNonce, remoteRandomPrivKey, _ := receiver.respondToHandshake(auth, crypto.FromECDSAPub(pub0), sessionToken)
+ response, remoteRecNonce, remoteInitNonce, remoteRandomPrivKey, remoteInitRandomPubKey, err := receiver.respondToHandshake(auth, crypto.FromECDSAPub(pub0), sessionToken)
+ if err != nil {
+ t.Errorf("%v", err)
+ }
+
// initiator reads receiver's response and the key exchange completes
- recNonce, remoteRandomPubKey, _, _ := initiator.completeHandshake(response)
+ recNonce, remoteRandomPubKey, _, err := initiator.completeHandshake(response)
+ if err != nil {
+ t.Errorf("%v", err)
+ }
// now both parties should have the same session parameters
- initSessionToken, initSecretRW, _ := initiator.newSession(initNonce, recNonce, auth, randomPrivKey, remoteRandomPubKey)
- recSessionToken, recSecretRW, _ := receiver.newSession(remoteInitNonce, remoteRecNonce, auth, remoteRandomPrivKey, &randomPrivKey.PublicKey)
+ 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)
+ }
- fmt.Printf("%x\n%x\n%x\n%x\n%x\n%x\n%x\n%x\n%x\n%x\n", auth, initNonce, response, remoteRecNonce, remoteInitNonce, remoteRandomPubKey, recNonce, &randomPrivKey.PublicKey, initSessionToken, initSecretRW)
+ 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)
if !bytes.Equal(initNonce, remoteInitNonce) {
t.Errorf("nonces do not match")