2015-01-18 11:46:08 +02:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2015-01-29 05:16:10 +02:00
|
|
|
// "binary"
|
2015-01-18 11:46:08 +02:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
2015-01-20 01:42:13 +02:00
|
|
|
"io"
|
2015-01-18 11:46:08 +02:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-01-26 16:50:12 +02:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/secp256k1"
|
2015-01-21 16:45:53 +02:00
|
|
|
ethlogger "github.com/ethereum/go-ethereum/logger"
|
2015-01-18 11:46:08 +02:00
|
|
|
"github.com/obscuren/ecies"
|
|
|
|
)
|
|
|
|
|
2015-01-21 16:45:53 +02:00
|
|
|
var clogger = ethlogger.NewLogger("CRYPTOID")
|
|
|
|
|
2015-01-26 16:50:12 +02:00
|
|
|
const (
|
2015-01-20 18:47:46 +02:00
|
|
|
sskLen int = 16 // ecies.MaxSharedKeyLength(pubKey) / 2
|
|
|
|
sigLen int = 65 // elliptic S256
|
|
|
|
pubLen int = 64 // 512 bit pubkey in uncompressed representation without format byte
|
2015-01-26 16:50:12 +02:00
|
|
|
shaLen int = 32 // hash length (for nonce etc)
|
|
|
|
msgLen int = 194 // sigLen + shaLen + pubLen + shaLen + 1 = 194
|
|
|
|
resLen int = 97 // pubLen + shaLen + 1
|
2015-01-21 18:22:49 +02:00
|
|
|
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
|
2015-01-18 11:46:08 +02:00
|
|
|
)
|
|
|
|
|
2015-01-21 12:22:07 +02:00
|
|
|
// secretRW implements a message read writer with encryption and authentication
|
|
|
|
// it is initialised by cryptoId.Run() after a successful crypto handshake
|
2015-01-19 01:53:45 +02:00
|
|
|
// aesSecret, macSecret, egressMac, ingress
|
2015-01-18 11:46:08 +02:00
|
|
|
type secretRW struct {
|
|
|
|
aesSecret, macSecret, egressMac, ingressMac []byte
|
|
|
|
}
|
|
|
|
|
2015-01-21 16:45:53 +02:00
|
|
|
type hexkey []byte
|
|
|
|
|
|
|
|
func (self hexkey) String() string {
|
|
|
|
return fmt.Sprintf("(%d) %x", len(self), []byte(self))
|
|
|
|
}
|
|
|
|
|
2015-01-29 05:16:10 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
var keyF = func() (priv *ecdsa.PrivateKey, err error) {
|
|
|
|
priv, err = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var detkeyF = func() (priv *ecdsa.PrivateKey, err error) {
|
|
|
|
s := make([]byte, 32)
|
|
|
|
detnonceF(s)
|
|
|
|
priv = crypto.ToECDSA(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-21 12:22:07 +02:00
|
|
|
/*
|
2015-01-26 18:16:23 +02:00
|
|
|
NewSecureSession(connection, privateKey, remotePublicKey, sessionToken, initiator) is called when the peer connection starts to set up a secure session by performing a crypto handshake.
|
2015-01-21 12:22:07 +02:00
|
|
|
|
|
|
|
connection is (a buffered) network connection.
|
|
|
|
|
2015-01-26 18:16:23 +02:00
|
|
|
privateKey is the local client's private key (*ecdsa.PrivateKey)
|
|
|
|
|
|
|
|
remotePublicKey is the remote peer's node Id ([]byte)
|
2015-01-21 12:22:07 +02:00
|
|
|
|
|
|
|
sessionToken is the token from the previous session with this same peer. Nil if no token is found.
|
|
|
|
|
2015-01-26 18:16:23 +02:00
|
|
|
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.
|
2015-01-21 12:22:07 +02:00
|
|
|
|
|
|
|
It returns a secretRW which implements the MsgReadWriter interface.
|
|
|
|
*/
|
2015-01-26 18:16:23 +02:00
|
|
|
func NewSecureSession(conn io.ReadWriter, prvKey *ecdsa.PrivateKey, remotePubKeyS []byte, sessionToken []byte, initiator bool) (token []byte, rw *secretRW, err error) {
|
2015-01-20 01:42:13 +02:00
|
|
|
var auth, initNonce, recNonce []byte
|
2015-01-21 18:22:49 +02:00
|
|
|
var read int
|
2015-01-20 01:42:13 +02:00
|
|
|
var randomPrivKey *ecdsa.PrivateKey
|
|
|
|
var remoteRandomPubKey *ecdsa.PublicKey
|
2015-01-21 18:22:49 +02:00
|
|
|
clogger.Debugf("attempting session with %v", hexkey(remotePubKeyS))
|
2015-01-20 01:42:13 +02:00
|
|
|
if initiator {
|
2015-01-26 18:16:23 +02:00
|
|
|
if auth, initNonce, randomPrivKey, _, err = startHandshake(prvKey, remotePubKeyS, sessionToken); err != nil {
|
2015-01-20 01:42:13 +02:00
|
|
|
return
|
|
|
|
}
|
2015-01-21 18:53:13 +02:00
|
|
|
if sessionToken != nil {
|
|
|
|
clogger.Debugf("session-token: %v", hexkey(sessionToken))
|
|
|
|
}
|
2015-01-21 18:22:49 +02:00
|
|
|
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
|
|
|
|
}
|
2015-01-20 01:42:13 +02:00
|
|
|
// write out auth message
|
|
|
|
// wait for response, then call complete
|
2015-01-26 18:16:23 +02:00
|
|
|
if recNonce, remoteRandomPubKey, _, err = completeHandshake(response, prvKey); err != nil {
|
2015-01-20 01:42:13 +02:00
|
|
|
return
|
|
|
|
}
|
2015-01-21 18:22:49 +02:00
|
|
|
clogger.Debugf("receiver-nonce: %v", hexkey(recNonce))
|
|
|
|
remoteRandomPubKeyS, _ := ExportPublicKey(remoteRandomPubKey)
|
|
|
|
clogger.Debugf("receiver-random-public-key: %v", hexkey(remoteRandomPubKeyS))
|
|
|
|
|
2015-01-19 13:21:13 +02:00
|
|
|
} else {
|
2015-01-21 18:22:49 +02:00
|
|
|
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))
|
2015-01-20 01:42:13 +02:00
|
|
|
// we are listening connection. we are responders in the handshake.
|
2015-01-19 13:21:13 +02:00
|
|
|
// Extract info from the authentication. The initiator starts by sending us a handshake that we need to respond to.
|
2015-01-20 01:42:13 +02:00
|
|
|
// so we read auth message first, then respond
|
|
|
|
var response []byte
|
2015-01-26 18:16:23 +02:00
|
|
|
if response, recNonce, initNonce, randomPrivKey, remoteRandomPubKey, err = respondToHandshake(auth, prvKey, remotePubKeyS, sessionToken); err != nil {
|
2015-01-20 01:42:13 +02:00
|
|
|
return
|
|
|
|
}
|
2015-01-21 18:22:49 +02:00
|
|
|
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))
|
2015-01-19 13:21:13 +02:00
|
|
|
}
|
2015-01-26 18:16:23 +02:00
|
|
|
return newSession(initiator, initNonce, recNonce, auth, randomPrivKey, remoteRandomPubKey)
|
2015-01-19 13:21:13 +02:00
|
|
|
}
|
|
|
|
|
2015-01-21 12:22:07 +02:00
|
|
|
/*
|
|
|
|
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.
|
2015-01-19 06:53:48 +02:00
|
|
|
*/
|
2015-01-20 18:47:46 +02:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
return crypto.ToECDSAPub(pubKey65), nil
|
|
|
|
}
|
|
|
|
|
2015-01-21 12:22:07 +02:00
|
|
|
/*
|
|
|
|
ExportPublicKey exports a *ecdsa.PublicKey into a byte slice using a simple 64-byte format. and is used for simple serialisation in network communication
|
|
|
|
*/
|
2015-01-20 18:47:46 +02:00
|
|
|
func ExportPublicKey(pubKeyEC *ecdsa.PublicKey) (pubKey []byte, err error) {
|
|
|
|
if pubKeyEC == nil {
|
|
|
|
return nil, fmt.Errorf("no ECDSA public key given")
|
|
|
|
}
|
|
|
|
return crypto.FromECDSAPub(pubKeyEC)[1:], nil
|
|
|
|
}
|
|
|
|
|
2015-01-21 12:22:07 +02:00
|
|
|
/* 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.
|
|
|
|
|
|
|
|
The first return value is the auth message that is to be sent out to the remote receiver.
|
|
|
|
*/
|
2015-01-26 18:16:23 +02:00
|
|
|
func startHandshake(prvKey *ecdsa.PrivateKey, remotePubKeyS, sessionToken []byte) (auth []byte, initNonce []byte, randomPrvKey *ecdsa.PrivateKey, remotePubKey *ecdsa.PublicKey, err error) {
|
2015-01-18 11:46:08 +02:00
|
|
|
// session init, common to both parties
|
2015-01-20 18:47:46 +02:00
|
|
|
if remotePubKey, err = ImportPublicKey(remotePubKeyS); err != nil {
|
2015-01-18 11:46:08 +02:00
|
|
|
return
|
|
|
|
}
|
2015-01-19 01:53:45 +02:00
|
|
|
|
2015-01-26 16:50:12 +02:00
|
|
|
var tokenFlag byte // = 0x00
|
2015-01-18 11:46:08 +02:00
|
|
|
if sessionToken == nil {
|
2015-01-19 01:53:45 +02:00
|
|
|
// no session token found means we need to generate shared secret.
|
|
|
|
// ecies shared secret is used as initial session token for new peers
|
|
|
|
// generate shared key from prv and remote pubkey
|
2015-01-26 18:16:23 +02:00
|
|
|
if sessionToken, err = ecies.ImportECDSA(prvKey).GenerateShared(ecies.ImportECDSAPublic(remotePubKey), sskLen, sskLen); err != nil {
|
2015-01-19 01:53:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// tokenFlag = 0x00 // redundant
|
|
|
|
} else {
|
|
|
|
// for known peers, we use stored token from the previous session
|
|
|
|
tokenFlag = 0x01
|
2015-01-18 11:46:08 +02:00
|
|
|
}
|
2015-01-19 01:53:45 +02:00
|
|
|
|
|
|
|
//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,
|
2015-01-18 11:46:08 +02:00
|
|
|
var msg []byte = make([]byte, msgLen)
|
2015-01-26 16:50:12 +02:00
|
|
|
initNonce = msg[msgLen-shaLen-1 : msgLen-1]
|
2015-01-29 05:16:10 +02:00
|
|
|
fmt.Printf("init-nonce: ")
|
|
|
|
if _, err = nonceF(initNonce); err != nil {
|
2015-01-18 11:46:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// create known message
|
2015-01-19 01:53:45 +02:00
|
|
|
// ecdh-shared-secret^nonce for new peers
|
|
|
|
// token^nonce for old peers
|
|
|
|
var sharedSecret = Xor(sessionToken, initNonce)
|
2015-01-18 11:46:08 +02:00
|
|
|
|
|
|
|
// generate random keypair to use for signing
|
2015-01-29 05:16:10 +02:00
|
|
|
fmt.Printf("init-random-ecdhe-private-key: ")
|
|
|
|
if randomPrvKey, err = keyF(); err != nil {
|
2015-01-18 11:46:08 +02:00
|
|
|
return
|
|
|
|
}
|
2015-01-19 01:53:45 +02:00
|
|
|
// sign shared secret (message known to both parties): shared-secret
|
2015-01-18 11:46:08 +02:00
|
|
|
var signature []byte
|
2015-01-19 01:53:45 +02:00
|
|
|
// signature = sign(ecdhe-random, shared-secret)
|
2015-01-18 11:46:08 +02:00
|
|
|
// uses secp256k1.Sign
|
2015-01-19 06:53:48 +02:00
|
|
|
if signature, err = crypto.Sign(sharedSecret, randomPrvKey); err != nil {
|
2015-01-18 11:46:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-19 01:53:45 +02:00
|
|
|
// message
|
|
|
|
// signed-shared-secret || H(ecdhe-random-pubk) || pubk || nonce || 0x0
|
|
|
|
copy(msg, signature) // copy signed-shared-secret
|
|
|
|
// H(ecdhe-random-pubk)
|
2015-01-20 18:47:46 +02:00
|
|
|
var randomPubKey64 []byte
|
|
|
|
if randomPubKey64, err = ExportPublicKey(&randomPrvKey.PublicKey); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-01-26 18:16:23 +02:00
|
|
|
var pubKey64 []byte
|
|
|
|
if pubKey64, err = ExportPublicKey(&prvKey.PublicKey); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-01-26 16:50:12 +02:00
|
|
|
copy(msg[sigLen:sigLen+shaLen], crypto.Sha3(randomPubKey64))
|
2015-01-19 01:53:45 +02:00
|
|
|
// pubkey copied to the correct segment.
|
2015-01-26 18:16:23 +02:00
|
|
|
copy(msg[sigLen+shaLen:sigLen+shaLen+pubLen], pubKey64)
|
2015-01-19 01:53:45 +02:00
|
|
|
// nonce is already in the slice
|
|
|
|
// stick tokenFlag byte to the end
|
|
|
|
msg[msgLen-1] = tokenFlag
|
|
|
|
|
|
|
|
// encrypt using remote-pubk
|
2015-01-18 11:46:08 +02:00
|
|
|
// auth = eciesEncrypt(remote-pubk, msg)
|
2015-01-19 01:53:45 +02:00
|
|
|
|
2015-01-18 11:46:08 +02:00
|
|
|
if auth, err = crypto.Encrypt(remotePubKey, msg); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-01-19 01:53:45 +02:00
|
|
|
|
2015-01-18 11:46:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-21 12:22:07 +02:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
2015-01-26 18:16:23 +02:00
|
|
|
func respondToHandshake(auth []byte, prvKey *ecdsa.PrivateKey, remotePubKeyS, sessionToken []byte) (authResp []byte, respNonce []byte, initNonce []byte, randomPrivKey *ecdsa.PrivateKey, remoteRandomPubKey *ecdsa.PublicKey, err error) {
|
2015-01-18 11:46:08 +02:00
|
|
|
var msg []byte
|
2015-01-20 18:47:46 +02:00
|
|
|
var remotePubKey *ecdsa.PublicKey
|
|
|
|
if remotePubKey, err = ImportPublicKey(remotePubKeyS); err != nil {
|
2015-01-20 01:42:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-18 11:46:08 +02:00
|
|
|
// they prove that msg is meant for me,
|
|
|
|
// I prove I possess private key if i can read it
|
2015-01-26 18:16:23 +02:00
|
|
|
if msg, err = crypto.Decrypt(prvKey, auth); err != nil {
|
2015-01-18 11:46:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-19 02:55:24 +02:00
|
|
|
var tokenFlag byte
|
|
|
|
if sessionToken == nil {
|
|
|
|
// no session token found means we need to generate shared secret.
|
|
|
|
// ecies shared secret is used as initial session token for new peers
|
|
|
|
// generate shared key from prv and remote pubkey
|
2015-01-26 18:16:23 +02:00
|
|
|
if sessionToken, err = ecies.ImportECDSA(prvKey).GenerateShared(ecies.ImportECDSAPublic(remotePubKey), sskLen, sskLen); err != nil {
|
2015-01-19 02:55:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// tokenFlag = 0x00 // redundant
|
|
|
|
} else {
|
|
|
|
// for known peers, we use stored token from the previous session
|
|
|
|
tokenFlag = 0x01
|
|
|
|
}
|
|
|
|
|
|
|
|
// the initiator nonce is read off the end of the message
|
2015-01-26 16:50:12 +02:00
|
|
|
initNonce = msg[msgLen-shaLen-1 : msgLen-1]
|
2015-01-19 01:53:45 +02:00
|
|
|
// 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
|
2015-01-19 02:55:24 +02:00
|
|
|
// we can now reconstruct the signed message and recover the peers pubkey
|
|
|
|
var signedMsg = Xor(sessionToken, initNonce)
|
2015-01-20 18:47:46 +02:00
|
|
|
var remoteRandomPubKeyS []byte
|
|
|
|
if remoteRandomPubKeyS, err = secp256k1.RecoverPubkey(signedMsg, msg[:sigLen]); err != nil {
|
2015-01-18 11:46:08 +02:00
|
|
|
return
|
|
|
|
}
|
2015-01-19 02:55:24 +02:00
|
|
|
// convert to ECDSA standard
|
2015-01-20 18:47:46 +02:00
|
|
|
if remoteRandomPubKey, err = ImportPublicKey(remoteRandomPubKeyS); err != nil {
|
2015-01-18 11:46:08 +02:00
|
|
|
return
|
|
|
|
}
|
2015-01-19 01:53:45 +02:00
|
|
|
|
2015-01-19 02:55:24 +02:00
|
|
|
// now we find ourselves a long task too, fill it random
|
|
|
|
var resp = make([]byte, resLen)
|
2015-01-26 16:50:12 +02:00
|
|
|
// generate shaLen long nonce
|
|
|
|
respNonce = resp[pubLen : pubLen+shaLen]
|
2015-01-29 05:16:10 +02:00
|
|
|
fmt.Printf("rec-nonce: ")
|
|
|
|
if _, err = nonceF(respNonce); err != nil {
|
2015-01-19 01:53:45 +02:00
|
|
|
return
|
|
|
|
}
|
2015-01-19 02:55:24 +02:00
|
|
|
// generate random keypair for session
|
2015-01-29 05:16:10 +02:00
|
|
|
fmt.Printf("rec-random-ecdhe-private-key: ")
|
|
|
|
if randomPrivKey, err = keyF(); err != nil {
|
2015-01-19 01:53:45 +02:00
|
|
|
return
|
|
|
|
}
|
2015-01-19 02:55:24 +02:00
|
|
|
// responder auth message
|
2015-01-19 01:53:45 +02:00
|
|
|
// E(remote-pubk, ecdhe-random-pubk || nonce || 0x0)
|
2015-01-20 18:47:46 +02:00
|
|
|
var randomPubKeyS []byte
|
|
|
|
if randomPubKeyS, err = ExportPublicKey(&randomPrivKey.PublicKey); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
copy(resp[:pubLen], randomPubKeyS)
|
2015-01-19 01:53:45 +02:00
|
|
|
// nonce is already in the slice
|
2015-01-19 02:55:24 +02:00
|
|
|
resp[resLen-1] = tokenFlag
|
2015-01-19 01:53:45 +02:00
|
|
|
|
|
|
|
// encrypt using remote-pubk
|
|
|
|
// auth = eciesEncrypt(remote-pubk, msg)
|
|
|
|
// why not encrypt with ecdhe-random-remote
|
|
|
|
if authResp, err = crypto.Encrypt(remotePubKey, resp); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-21 12:22:07 +02:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
2015-01-26 18:16:23 +02:00
|
|
|
func completeHandshake(auth []byte, prvKey *ecdsa.PrivateKey) (respNonce []byte, remoteRandomPubKey *ecdsa.PublicKey, tokenFlag bool, err error) {
|
2015-01-19 01:53:45 +02:00
|
|
|
var msg []byte
|
|
|
|
// they prove that msg is meant for me,
|
|
|
|
// I prove I possess private key if i can read it
|
2015-01-26 18:16:23 +02:00
|
|
|
if msg, err = crypto.Decrypt(prvKey, auth); err != nil {
|
2015-01-19 01:53:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-26 16:50:12 +02:00
|
|
|
respNonce = msg[pubLen : pubLen+shaLen]
|
2015-01-20 18:47:46 +02:00
|
|
|
var remoteRandomPubKeyS = msg[:pubLen]
|
|
|
|
if remoteRandomPubKey, err = ImportPublicKey(remoteRandomPubKeyS); err != nil {
|
2015-01-19 01:53:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if msg[resLen-1] == 0x01 {
|
|
|
|
tokenFlag = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-21 12:22:07 +02:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
2015-01-26 18:16:23 +02:00
|
|
|
func newSession(initiator bool, initNonce, respNonce, auth []byte, privKey *ecdsa.PrivateKey, remoteRandomPubKey *ecdsa.PublicKey) (sessionToken []byte, rw *secretRW, err error) {
|
2015-01-19 01:53:45 +02:00
|
|
|
// 3) Now we can trust ecdhe-random-pubk to derive new keys
|
2015-01-18 11:46:08 +02:00
|
|
|
//ecdhe-shared-secret = ecdh.agree(ecdhe-random, remote-ecdhe-random-pubk)
|
|
|
|
var dhSharedSecret []byte
|
2015-01-19 06:53:48 +02:00
|
|
|
pubKey := ecies.ImportECDSAPublic(remoteRandomPubKey)
|
|
|
|
if dhSharedSecret, err = ecies.ImportECDSA(privKey).GenerateShared(pubKey, sskLen, sskLen); err != nil {
|
2015-01-18 11:46:08 +02:00
|
|
|
return
|
|
|
|
}
|
2015-01-19 01:53:45 +02:00
|
|
|
var sharedSecret = crypto.Sha3(append(dhSharedSecret, crypto.Sha3(append(respNonce, initNonce...))...))
|
2015-01-18 11:46:08 +02:00
|
|
|
sessionToken = crypto.Sha3(sharedSecret)
|
|
|
|
var aesSecret = crypto.Sha3(append(dhSharedSecret, sharedSecret...))
|
|
|
|
var macSecret = crypto.Sha3(append(dhSharedSecret, aesSecret...))
|
2015-01-26 16:50:12 +02:00
|
|
|
var egressMac, ingressMac []byte
|
|
|
|
if initiator {
|
|
|
|
egressMac = Xor(macSecret, respNonce)
|
|
|
|
ingressMac = Xor(macSecret, initNonce)
|
|
|
|
} else {
|
|
|
|
egressMac = Xor(macSecret, initNonce)
|
|
|
|
ingressMac = Xor(macSecret, respNonce)
|
|
|
|
}
|
2015-01-18 11:46:08 +02:00
|
|
|
rw = &secretRW{
|
|
|
|
aesSecret: aesSecret,
|
|
|
|
macSecret: macSecret,
|
|
|
|
egressMac: egressMac,
|
|
|
|
ingressMac: ingressMac,
|
|
|
|
}
|
2015-01-21 18:22:49 +02:00
|
|
|
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))
|
2015-01-18 11:46:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-21 12:22:07 +02:00
|
|
|
// TODO: optimisation
|
2015-01-19 01:53:45 +02:00
|
|
|
// should use cipher.xorBytes from crypto/cipher/xor.go for fast xor
|
2015-01-18 11:46:08 +02:00
|
|
|
func Xor(one, other []byte) (xor []byte) {
|
2015-01-19 01:53:45 +02:00
|
|
|
xor = make([]byte, len(one))
|
2015-01-18 11:46:08 +02:00
|
|
|
for i := 0; i < len(one); i++ {
|
|
|
|
xor[i] = one[i] ^ other[i]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|