aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/crypto.go
diff options
context:
space:
mode:
Diffstat (limited to 'p2p/crypto.go')
-rw-r--r--p2p/crypto.go429
1 files changed, 194 insertions, 235 deletions
diff --git a/p2p/crypto.go b/p2p/crypto.go
index cb0534cba..2692d708c 100644
--- a/p2p/crypto.go
+++ b/p2p/crypto.go
@@ -10,28 +10,25 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
ethlogger "github.com/ethereum/go-ethereum/logger"
+ "github.com/ethereum/go-ethereum/p2p/discover"
"github.com/obscuren/ecies"
)
var clogger = ethlogger.NewLogger("CRYPTOID")
const (
- sskLen int = 16 // ecies.MaxSharedKeyLength(pubKey) / 2
- sigLen int = 65 // elliptic S256
- pubLen int = 64 // 512 bit pubkey in uncompressed representation without format byte
- shaLen int = 32 // hash length (for nonce etc)
- msgLen int = 194 // sigLen + shaLen + pubLen + shaLen + 1 = 194
- resLen int = 97 // pubLen + shaLen + 1
- iHSLen int = 307 // size of the final ECIES payload sent as initiator's handshake
- rHSLen int = 210 // size of the final ECIES payload sent as receiver's handshake
-)
+ sskLen = 16 // ecies.MaxSharedKeyLength(pubKey) / 2
+ sigLen = 65 // elliptic S256
+ pubLen = 64 // 512 bit pubkey in uncompressed representation without format byte
+ shaLen = 32 // hash length (for nonce etc)
-// secretRW implements a message read writer with encryption and authentication
-// it is initialised by cryptoId.Run() after a successful crypto handshake
-// aesSecret, macSecret, egressMac, ingress
-type secretRW struct {
- aesSecret, macSecret, egressMac, ingressMac []byte
-}
+ authMsgLen = sigLen + shaLen + pubLen + shaLen + 1
+ authRespLen = pubLen + shaLen + 1
+
+ eciesBytes = 65 + 16 + 32
+ iHSLen = authMsgLen + eciesBytes // size of the final ECIES payload sent as initiator's handshake
+ rHSLen = authRespLen + eciesBytes // size of the final ECIES payload sent as receiver's handshake
+)
type hexkey []byte
@@ -39,150 +36,73 @@ func (self hexkey) String() string {
return fmt.Sprintf("(%d) %x", len(self), []byte(self))
}
-var nonceF = func(b []byte) (n int, err error) {
- return rand.Read(b)
-}
-
-var step = 0
-var detnonceF = func(b []byte) (n int, err error) {
- step++
- copy(b, crypto.Sha3([]byte("privacy"+string(step))))
- fmt.Printf("detkey %v: %v\n", step, hexkey(b))
- return
+func encHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, dial *discover.Node) (
+ remoteID discover.NodeID,
+ sessionToken []byte,
+ err error,
+) {
+ if dial == nil {
+ var remotePubkey []byte
+ sessionToken, remotePubkey, err = inboundEncHandshake(conn, prv, nil)
+ copy(remoteID[:], remotePubkey)
+ } else {
+ remoteID = dial.ID
+ sessionToken, err = outboundEncHandshake(conn, prv, remoteID[:], nil)
+ }
+ return remoteID, sessionToken, err
}
-var keyF = func() (priv *ecdsa.PrivateKey, err error) {
- priv, err = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
+// outboundEncHandshake negotiates a session token on conn.
+// it should be called on the dialing side of the connection.
+//
+// privateKey is the local client's private key
+// remotePublicKey is the remote peer's node ID
+// sessionToken is the token from a previous session with this node.
+func outboundEncHandshake(conn io.ReadWriter, prvKey *ecdsa.PrivateKey, remotePublicKey []byte, sessionToken []byte) (
+ newSessionToken []byte,
+ err error,
+) {
+ auth, initNonce, randomPrivKey, err := authMsg(prvKey, remotePublicKey, sessionToken)
if err != nil {
- return
+ return nil, err
}
- return
-}
-
-var detkeyF = func() (priv *ecdsa.PrivateKey, err error) {
- s := make([]byte, 32)
- detnonceF(s)
- priv = crypto.ToECDSA(s)
- return
-}
-
-/*
-NewSecureSession(connection, privateKey, remotePublicKey, sessionToken, initiator) is called when the peer connection starts to set up a secure session by performing a crypto handshake.
-
- connection is (a buffered) network connection.
-
- privateKey is the local client's private key (*ecdsa.PrivateKey)
-
- remotePublicKey is the remote peer's node Id ([]byte)
-
- sessionToken is the token from the previous session with this same peer. Nil if no token is found.
-
- initiator is a boolean flag. True if the node is the initiator of the connection (ie., remote is an outbound peer reached by dialing out). False if the connection was established by accepting a call from the remote peer via a listener.
-
- It returns a secretRW which implements the MsgReadWriter interface.
-*/
-func NewSecureSession(conn io.ReadWriter, prvKey *ecdsa.PrivateKey, remotePubKeyS []byte, sessionToken []byte, initiator bool) (token []byte, rw *secretRW, err error) {
- var auth, initNonce, recNonce []byte
- var read int
- var randomPrivKey *ecdsa.PrivateKey
- var remoteRandomPubKey *ecdsa.PublicKey
- clogger.Debugf("attempting session with %v", hexkey(remotePubKeyS))
- if initiator {
- if auth, initNonce, randomPrivKey, _, err = startHandshake(prvKey, remotePubKeyS, sessionToken); err != nil {
- return
- }
- if sessionToken != nil {
- clogger.Debugf("session-token: %v", hexkey(sessionToken))
- }
- clogger.Debugf("initiator-nonce: %v", hexkey(initNonce))
- clogger.Debugf("initiator-random-private-key: %v", hexkey(crypto.FromECDSA(randomPrivKey)))
- randomPublicKeyS, _ := ExportPublicKey(&randomPrivKey.PublicKey)
- clogger.Debugf("initiator-random-public-key: %v", hexkey(randomPublicKeyS))
-
- if _, err = conn.Write(auth); err != nil {
- return
- }
- clogger.Debugf("initiator handshake (sent to %v):\n%v", hexkey(remotePubKeyS), hexkey(auth))
- var response []byte = make([]byte, rHSLen)
- if read, err = conn.Read(response); err != nil || read == 0 {
- return
- }
- if read != rHSLen {
- err = fmt.Errorf("remote receiver's handshake has invalid length. expect %v, got %v", rHSLen, read)
- return
- }
- // write out auth message
- // wait for response, then call complete
- if recNonce, remoteRandomPubKey, _, err = completeHandshake(response, prvKey); err != nil {
- return
- }
- clogger.Debugf("receiver-nonce: %v", hexkey(recNonce))
- remoteRandomPubKeyS, _ := ExportPublicKey(remoteRandomPubKey)
- clogger.Debugf("receiver-random-public-key: %v", hexkey(remoteRandomPubKeyS))
-
- } else {
- auth = make([]byte, iHSLen)
- clogger.Debugf("waiting for initiator handshake (from %v)", hexkey(remotePubKeyS))
- if read, err = conn.Read(auth); err != nil {
- return
- }
- if read != iHSLen {
- err = fmt.Errorf("remote initiator's handshake has invalid length. expect %v, got %v", iHSLen, read)
- return
- }
- clogger.Debugf("received initiator handshake (from %v):\n%v", hexkey(remotePubKeyS), hexkey(auth))
- // we are listening connection. we are responders in the handshake.
- // Extract info from the authentication. The initiator starts by sending us a handshake that we need to respond to.
- // so we read auth message first, then respond
- var response []byte
- if response, recNonce, initNonce, randomPrivKey, remoteRandomPubKey, err = respondToHandshake(auth, prvKey, remotePubKeyS, sessionToken); err != nil {
- return
- }
- clogger.Debugf("receiver-nonce: %v", hexkey(recNonce))
- clogger.Debugf("receiver-random-priv-key: %v", hexkey(crypto.FromECDSA(randomPrivKey)))
- if _, err = conn.Write(response); err != nil {
- return
- }
- clogger.Debugf("receiver handshake (sent to %v):\n%v", hexkey(remotePubKeyS), hexkey(response))
+ if sessionToken != nil {
+ clogger.Debugf("session-token: %v", hexkey(sessionToken))
}
- return newSession(initiator, initNonce, recNonce, auth, randomPrivKey, remoteRandomPubKey)
-}
-/*
-ImportPublicKey creates a 512 bit *ecsda.PublicKey from a byte slice. It accepts the simple 64 byte uncompressed format or the 65 byte format given by calling elliptic.Marshal on the EC point represented by the key. Any other length will result in an invalid public key error.
-*/
-func ImportPublicKey(pubKey []byte) (pubKeyEC *ecdsa.PublicKey, err error) {
- var pubKey65 []byte
- switch len(pubKey) {
- case 64:
- pubKey65 = append([]byte{0x04}, pubKey...)
- case 65:
- pubKey65 = pubKey
- default:
- return nil, fmt.Errorf("invalid public key length %v (expect 64/65)", len(pubKey))
+ clogger.Debugf("initiator-nonce: %v", hexkey(initNonce))
+ clogger.Debugf("initiator-random-private-key: %v", hexkey(crypto.FromECDSA(randomPrivKey)))
+ randomPublicKeyS, _ := exportPublicKey(&randomPrivKey.PublicKey)
+ clogger.Debugf("initiator-random-public-key: %v", hexkey(randomPublicKeyS))
+ if _, err = conn.Write(auth); err != nil {
+ return nil, err
}
- return crypto.ToECDSAPub(pubKey65), nil
-}
+ clogger.Debugf("initiator handshake: %v", hexkey(auth))
-/*
-ExportPublicKey exports a *ecdsa.PublicKey into a byte slice using a simple 64-byte format. and is used for simple serialisation in network communication
-*/
-func ExportPublicKey(pubKeyEC *ecdsa.PublicKey) (pubKey []byte, err error) {
- if pubKeyEC == nil {
- return nil, fmt.Errorf("no ECDSA public key given")
+ response := make([]byte, rHSLen)
+ if _, err = io.ReadFull(conn, response); err != nil {
+ return nil, err
+ }
+ recNonce, remoteRandomPubKey, _, err := completeHandshake(response, prvKey)
+ if err != nil {
+ return nil, err
}
- return crypto.FromECDSAPub(pubKeyEC)[1:], nil
-}
-
-/* startHandshake is called by if the node is the initiator of the connection.
-The caller provides the public key of the peer as conjuctured from lookup based on IP:port, given as user input or proven by signatures. The caller must have access to persistant information about the peers, and pass the previous session token as an argument to cryptoId.
+ clogger.Debugf("receiver-nonce: %v", hexkey(recNonce))
+ remoteRandomPubKeyS, _ := exportPublicKey(remoteRandomPubKey)
+ clogger.Debugf("receiver-random-public-key: %v", hexkey(remoteRandomPubKeyS))
+ return newSession(initNonce, recNonce, randomPrivKey, remoteRandomPubKey)
+}
-The first return value is the auth message that is to be sent out to the remote receiver.
-*/
-func startHandshake(prvKey *ecdsa.PrivateKey, remotePubKeyS, sessionToken []byte) (auth []byte, initNonce []byte, randomPrvKey *ecdsa.PrivateKey, remotePubKey *ecdsa.PublicKey, err error) {
+// authMsg creates the initiator handshake.
+func authMsg(prvKey *ecdsa.PrivateKey, remotePubKeyS, sessionToken []byte) (
+ auth, initNonce []byte,
+ randomPrvKey *ecdsa.PrivateKey,
+ err error,
+) {
// session init, common to both parties
- if remotePubKey, err = ImportPublicKey(remotePubKeyS); err != nil {
+ remotePubKey, err := importPublicKey(remotePubKeyS)
+ if err != nil {
return
}
@@ -203,20 +123,18 @@ func startHandshake(prvKey *ecdsa.PrivateKey, remotePubKeyS, sessionToken []byte
//E(remote-pubk, S(ecdhe-random, ecdh-shared-secret^nonce) || H(ecdhe-random-pubk) || pubk || nonce || 0x0)
// E(remote-pubk, S(ecdhe-random, token^nonce) || H(ecdhe-random-pubk) || pubk || nonce || 0x1)
// allocate msgLen long message,
- var msg []byte = make([]byte, msgLen)
- initNonce = msg[msgLen-shaLen-1 : msgLen-1]
- fmt.Printf("init-nonce: ")
- if _, err = nonceF(initNonce); err != nil {
+ var msg []byte = make([]byte, authMsgLen)
+ initNonce = msg[authMsgLen-shaLen-1 : authMsgLen-1]
+ if _, err = rand.Read(initNonce); err != nil {
return
}
// create known message
// ecdh-shared-secret^nonce for new peers
// token^nonce for old peers
- var sharedSecret = Xor(sessionToken, initNonce)
+ var sharedSecret = xor(sessionToken, initNonce)
// generate random keypair to use for signing
- fmt.Printf("init-random-ecdhe-private-key: ")
- if randomPrvKey, err = keyF(); err != nil {
+ if randomPrvKey, err = crypto.GenerateKey(); err != nil {
return
}
// sign shared secret (message known to both parties): shared-secret
@@ -232,11 +150,11 @@ func startHandshake(prvKey *ecdsa.PrivateKey, remotePubKeyS, sessionToken []byte
copy(msg, signature) // copy signed-shared-secret
// H(ecdhe-random-pubk)
var randomPubKey64 []byte
- if randomPubKey64, err = ExportPublicKey(&randomPrvKey.PublicKey); err != nil {
+ if randomPubKey64, err = exportPublicKey(&randomPrvKey.PublicKey); err != nil {
return
}
var pubKey64 []byte
- if pubKey64, err = ExportPublicKey(&prvKey.PublicKey); err != nil {
+ if pubKey64, err = exportPublicKey(&prvKey.PublicKey); err != nil {
return
}
copy(msg[sigLen:sigLen+shaLen], crypto.Sha3(randomPubKey64))
@@ -244,36 +162,98 @@ func startHandshake(prvKey *ecdsa.PrivateKey, remotePubKeyS, sessionToken []byte
copy(msg[sigLen+shaLen:sigLen+shaLen+pubLen], pubKey64)
// nonce is already in the slice
// stick tokenFlag byte to the end
- msg[msgLen-1] = tokenFlag
+ msg[authMsgLen-1] = tokenFlag
// encrypt using remote-pubk
// auth = eciesEncrypt(remote-pubk, msg)
-
if auth, err = crypto.Encrypt(remotePubKey, msg); err != nil {
return
}
-
return
}
-/*
-respondToHandshake is called by peer if it accepted (but not initiated) the connection from the remote. It is passed the initiator handshake received, the public key and session token belonging to the remote initiator.
-
-The first return value is the authentication response (aka receiver handshake) that is to be sent to the remote initiator.
-*/
-func respondToHandshake(auth []byte, prvKey *ecdsa.PrivateKey, remotePubKeyS, sessionToken []byte) (authResp []byte, respNonce []byte, initNonce []byte, randomPrivKey *ecdsa.PrivateKey, remoteRandomPubKey *ecdsa.PublicKey, err error) {
+// completeHandshake is called when the initiator receives an
+// authentication response (aka receiver handshake). It completes the
+// handshake by reading off parameters the remote peer provides needed
+// to set up the secure session.
+func completeHandshake(auth []byte, prvKey *ecdsa.PrivateKey) (
+ respNonce []byte,
+ remoteRandomPubKey *ecdsa.PublicKey,
+ tokenFlag bool,
+ err error,
+) {
var msg []byte
- var remotePubKey *ecdsa.PublicKey
- if remotePubKey, err = ImportPublicKey(remotePubKeyS); err != nil {
+ // they prove that msg is meant for me,
+ // I prove I possess private key if i can read it
+ if msg, err = crypto.Decrypt(prvKey, auth); err != nil {
return
}
+ respNonce = msg[pubLen : pubLen+shaLen]
+ var remoteRandomPubKeyS = msg[:pubLen]
+ if remoteRandomPubKey, err = importPublicKey(remoteRandomPubKeyS); err != nil {
+ return
+ }
+ if msg[authRespLen-1] == 0x01 {
+ tokenFlag = true
+ }
+ return
+}
+
+// inboundEncHandshake negotiates a session token on conn.
+// it should be called on the listening side of the connection.
+//
+// privateKey is the local client's private key
+// sessionToken is the token from a previous session with this node.
+func inboundEncHandshake(conn io.ReadWriter, prvKey *ecdsa.PrivateKey, sessionToken []byte) (
+ token, remotePubKey []byte,
+ err error,
+) {
+ // we are listening connection. we are responders in the
+ // handshake. Extract info from the authentication. The initiator
+ // starts by sending us a handshake that we need to respond to. so
+ // we read auth message first, then respond.
+ auth := make([]byte, iHSLen)
+ if _, err := io.ReadFull(conn, auth); err != nil {
+ return nil, nil, err
+ }
+ response, recNonce, initNonce, remotePubKey, randomPrivKey, remoteRandomPubKey, err := authResp(auth, sessionToken, prvKey)
+ if err != nil {
+ return nil, nil, err
+ }
+ clogger.Debugf("receiver-nonce: %v", hexkey(recNonce))
+ clogger.Debugf("receiver-random-priv-key: %v", hexkey(crypto.FromECDSA(randomPrivKey)))
+ if _, err = conn.Write(response); err != nil {
+ return nil, nil, err
+ }
+ clogger.Debugf("receiver handshake:\n%v", hexkey(response))
+ token, err = newSession(initNonce, recNonce, randomPrivKey, remoteRandomPubKey)
+ return token, remotePubKey, err
+}
+
+// authResp is called by peer if it accepted (but not
+// initiated) the connection from the remote. It is passed the initiator
+// handshake received and the session token belonging to the
+// remote initiator.
+//
+// The first return value is the authentication response (aka receiver
+// handshake) that is to be sent to the remote initiator.
+func authResp(auth, sessionToken []byte, prvKey *ecdsa.PrivateKey) (
+ authResp, respNonce, initNonce, remotePubKeyS []byte,
+ randomPrivKey *ecdsa.PrivateKey,
+ remoteRandomPubKey *ecdsa.PublicKey,
+ err error,
+) {
// they prove that msg is meant for me,
// I prove I possess private key if i can read it
- if msg, err = crypto.Decrypt(prvKey, auth); err != nil {
+ msg, err := crypto.Decrypt(prvKey, auth)
+ if err != nil {
return
}
+ remotePubKeyS = msg[sigLen+shaLen : sigLen+shaLen+pubLen]
+ remotePubKey, _ := importPublicKey(remotePubKeyS)
+
var tokenFlag byte
if sessionToken == nil {
// no session token found means we need to generate shared secret.
@@ -289,42 +269,42 @@ func respondToHandshake(auth []byte, prvKey *ecdsa.PrivateKey, remotePubKeyS, se
}
// the initiator nonce is read off the end of the message
- initNonce = msg[msgLen-shaLen-1 : msgLen-1]
- // I prove that i own prv key (to derive shared secret, and read nonce off encrypted msg) and that I own shared secret
- // they prove they own the private key belonging to ecdhe-random-pubk
- // we can now reconstruct the signed message and recover the peers pubkey
- var signedMsg = Xor(sessionToken, initNonce)
+ initNonce = msg[authMsgLen-shaLen-1 : authMsgLen-1]
+ // I prove that i own prv key (to derive shared secret, and read
+ // nonce off encrypted msg) and that I own shared secret they
+ // prove they own the private key belonging to ecdhe-random-pubk
+ // we can now reconstruct the signed message and recover the peers
+ // pubkey
+ var signedMsg = xor(sessionToken, initNonce)
var remoteRandomPubKeyS []byte
if remoteRandomPubKeyS, err = secp256k1.RecoverPubkey(signedMsg, msg[:sigLen]); err != nil {
return
}
// convert to ECDSA standard
- if remoteRandomPubKey, err = ImportPublicKey(remoteRandomPubKeyS); err != nil {
+ if remoteRandomPubKey, err = importPublicKey(remoteRandomPubKeyS); err != nil {
return
}
// now we find ourselves a long task too, fill it random
- var resp = make([]byte, resLen)
+ var resp = make([]byte, authRespLen)
// generate shaLen long nonce
respNonce = resp[pubLen : pubLen+shaLen]
- fmt.Printf("rec-nonce: ")
- if _, err = nonceF(respNonce); err != nil {
+ if _, err = rand.Read(respNonce); err != nil {
return
}
// generate random keypair for session
- fmt.Printf("rec-random-ecdhe-private-key: ")
- if randomPrivKey, err = keyF(); err != nil {
+ if randomPrivKey, err = crypto.GenerateKey(); err != nil {
return
}
// responder auth message
// E(remote-pubk, ecdhe-random-pubk || nonce || 0x0)
var randomPubKeyS []byte
- if randomPubKeyS, err = ExportPublicKey(&randomPrivKey.PublicKey); err != nil {
+ if randomPubKeyS, err = exportPublicKey(&randomPrivKey.PublicKey); err != nil {
return
}
copy(resp[:pubLen], randomPubKeyS)
// nonce is already in the slice
- resp[resLen-1] = tokenFlag
+ resp[authRespLen-1] = tokenFlag
// encrypt using remote-pubk
// auth = eciesEncrypt(remote-pubk, msg)
@@ -335,70 +315,49 @@ func respondToHandshake(auth []byte, prvKey *ecdsa.PrivateKey, remotePubKeyS, se
return
}
-/*
-completeHandshake is called when the initiator receives an authentication response (aka receiver handshake). It completes the handshake by reading off parameters the remote peer provides needed to set up the secure session
-*/
-func completeHandshake(auth []byte, prvKey *ecdsa.PrivateKey) (respNonce []byte, remoteRandomPubKey *ecdsa.PublicKey, tokenFlag bool, err error) {
- var msg []byte
- // they prove that msg is meant for me,
- // I prove I possess private key if i can read it
- if msg, err = crypto.Decrypt(prvKey, auth); err != nil {
- return
+// newSession is called after the handshake is completed. The
+// arguments are values negotiated in the handshake. The return value
+// is a new session Token to be remembered for the next time we
+// connect with this peer.
+func newSession(initNonce, respNonce []byte, privKey *ecdsa.PrivateKey, remoteRandomPubKey *ecdsa.PublicKey) ([]byte, error) {
+ // 3) Now we can trust ecdhe-random-pubk to derive new keys
+ //ecdhe-shared-secret = ecdh.agree(ecdhe-random, remote-ecdhe-random-pubk)
+ pubKey := ecies.ImportECDSAPublic(remoteRandomPubKey)
+ dhSharedSecret, err := ecies.ImportECDSA(privKey).GenerateShared(pubKey, sskLen, sskLen)
+ if err != nil {
+ return nil, err
}
+ sharedSecret := crypto.Sha3(dhSharedSecret, crypto.Sha3(respNonce, initNonce))
+ sessionToken := crypto.Sha3(sharedSecret)
+ return sessionToken, nil
+}
- respNonce = msg[pubLen : pubLen+shaLen]
- var remoteRandomPubKeyS = msg[:pubLen]
- if remoteRandomPubKey, err = ImportPublicKey(remoteRandomPubKeyS); err != nil {
- return
- }
- if msg[resLen-1] == 0x01 {
- tokenFlag = true
+// importPublicKey unmarshals 512 bit public keys.
+func importPublicKey(pubKey []byte) (pubKeyEC *ecdsa.PublicKey, err error) {
+ var pubKey65 []byte
+ switch len(pubKey) {
+ case 64:
+ // add 'uncompressed key' flag
+ pubKey65 = append([]byte{0x04}, pubKey...)
+ case 65:
+ pubKey65 = pubKey
+ default:
+ return nil, fmt.Errorf("invalid public key length %v (expect 64/65)", len(pubKey))
}
- return
+ return crypto.ToECDSAPub(pubKey65), nil
}
-/*
-newSession is called after the handshake is completed. The arguments are values negotiated in the handshake and the return value is a new session : a new session Token to be remembered for the next time we connect with this peer. And a MsgReadWriter that implements an encrypted and authenticated connection with key material obtained from the crypto handshake key exchange
-*/
-func newSession(initiator bool, initNonce, respNonce, auth []byte, privKey *ecdsa.PrivateKey, remoteRandomPubKey *ecdsa.PublicKey) (sessionToken []byte, rw *secretRW, err error) {
- // 3) Now we can trust ecdhe-random-pubk to derive new keys
- //ecdhe-shared-secret = ecdh.agree(ecdhe-random, remote-ecdhe-random-pubk)
- var dhSharedSecret []byte
- pubKey := ecies.ImportECDSAPublic(remoteRandomPubKey)
- if dhSharedSecret, err = ecies.ImportECDSA(privKey).GenerateShared(pubKey, sskLen, sskLen); err != nil {
- return
+func exportPublicKey(pubKeyEC *ecdsa.PublicKey) (pubKey []byte, err error) {
+ if pubKeyEC == nil {
+ return nil, fmt.Errorf("no ECDSA public key given")
}
- var sharedSecret = crypto.Sha3(append(dhSharedSecret, crypto.Sha3(append(respNonce, initNonce...))...))
- sessionToken = crypto.Sha3(sharedSecret)
- var aesSecret = crypto.Sha3(append(dhSharedSecret, sharedSecret...))
- var macSecret = crypto.Sha3(append(dhSharedSecret, aesSecret...))
- var egressMac, ingressMac []byte
- if initiator {
- egressMac = Xor(macSecret, respNonce)
- ingressMac = Xor(macSecret, initNonce)
- } else {
- egressMac = Xor(macSecret, initNonce)
- ingressMac = Xor(macSecret, respNonce)
- }
- rw = &secretRW{
- aesSecret: aesSecret,
- macSecret: macSecret,
- egressMac: egressMac,
- ingressMac: ingressMac,
- }
- clogger.Debugf("aes-secret: %v", hexkey(aesSecret))
- clogger.Debugf("mac-secret: %v", hexkey(macSecret))
- clogger.Debugf("egress-mac: %v", hexkey(egressMac))
- clogger.Debugf("ingress-mac: %v", hexkey(ingressMac))
- return
+ return crypto.FromECDSAPub(pubKeyEC)[1:], nil
}
-// TODO: optimisation
-// should use cipher.xorBytes from crypto/cipher/xor.go for fast xor
-func Xor(one, other []byte) (xor []byte) {
+func xor(one, other []byte) (xor []byte) {
xor = make([]byte, len(one))
for i := 0; i < len(one); i++ {
xor[i] = one[i] ^ other[i]
}
- return
+ return xor
}