a few minor changes from review

This commit is contained in:
zelig 2015-01-26 14:08:30 +00:00
parent 2e76f1b6ba
commit 94af107fbc
3 changed files with 14 additions and 16 deletions

View file

@ -14,13 +14,13 @@ import (
var clogger = ethlogger.NewLogger("CRYPTOID") var clogger = ethlogger.NewLogger("CRYPTOID")
var ( const (
sskLen int = 16 // ecies.MaxSharedKeyLength(pubKey) / 2 sskLen int = 16 // ecies.MaxSharedKeyLength(pubKey) / 2
sigLen int = 65 // elliptic S256 sigLen int = 65 // elliptic S256
pubLen int = 64 // 512 bit pubkey in uncompressed representation without format byte pubLen int = 64 // 512 bit pubkey in uncompressed representation without format byte
keyLen int = 32 // ECDSA shaLen int = 32 // hash length (for nonce etc)
msgLen int = 194 // sigLen + keyLen + pubLen + keyLen + 1 = 194 msgLen int = 194 // sigLen + shaLen + pubLen + shaLen + 1 = 194
resLen int = 97 // pubLen + keyLen + 1 resLen int = 97 // pubLen + shaLen + 1
iHSLen int = 307 // size of the final ECIES payload sent as initiator's handshake 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 rHSLen int = 210 // size of the final ECIES payload sent as receiver's handshake
) )
@ -191,7 +191,7 @@ func (self *cryptoId) startHandshake(remotePubKeyS, sessionToken []byte) (auth [
return return
} }
var tokenFlag byte var tokenFlag byte // = 0x00
if sessionToken == nil { if sessionToken == nil {
// no session token found means we need to generate shared secret. // no session token found means we need to generate shared secret.
// ecies shared secret is used as initial session token for new peers // ecies shared secret is used as initial session token for new peers
@ -199,7 +199,6 @@ func (self *cryptoId) startHandshake(remotePubKeyS, sessionToken []byte) (auth [
if sessionToken, err = ecies.ImportECDSA(self.prvKey).GenerateShared(ecies.ImportECDSAPublic(remotePubKey), sskLen, sskLen); err != nil { if sessionToken, err = ecies.ImportECDSA(self.prvKey).GenerateShared(ecies.ImportECDSAPublic(remotePubKey), sskLen, sskLen); err != nil {
return return
} }
// tokenFlag = 0x00 // redundant
} else { } else {
// for known peers, we use stored token from the previous session // for known peers, we use stored token from the previous session
tokenFlag = 0x01 tokenFlag = 0x01
@ -209,7 +208,7 @@ func (self *cryptoId) startHandshake(remotePubKeyS, sessionToken []byte) (auth [
// E(remote-pubk, S(ecdhe-random, token^nonce) || H(ecdhe-random-pubk) || pubk || nonce || 0x1) // E(remote-pubk, S(ecdhe-random, token^nonce) || H(ecdhe-random-pubk) || pubk || nonce || 0x1)
// allocate msgLen long message, // allocate msgLen long message,
var msg []byte = make([]byte, msgLen) var msg []byte = make([]byte, msgLen)
initNonce = msg[msgLen-keyLen-1 : msgLen-1] initNonce = msg[msgLen-shaLen-1 : msgLen-1]
if _, err = rand.Read(initNonce); err != nil { if _, err = rand.Read(initNonce); err != nil {
return return
} }
@ -238,9 +237,9 @@ func (self *cryptoId) startHandshake(remotePubKeyS, sessionToken []byte) (auth [
if randomPubKey64, err = ExportPublicKey(&randomPrvKey.PublicKey); err != nil { if randomPubKey64, err = ExportPublicKey(&randomPrvKey.PublicKey); err != nil {
return return
} }
copy(msg[sigLen:sigLen+keyLen], crypto.Sha3(randomPubKey64)) copy(msg[sigLen:sigLen+shaLen], crypto.Sha3(randomPubKey64))
// pubkey copied to the correct segment. // pubkey copied to the correct segment.
copy(msg[sigLen+keyLen:sigLen+keyLen+pubLen], self.pubKeyS) copy(msg[sigLen+shaLen:sigLen+shaLen+pubLen], self.pubKeyS)
// nonce is already in the slice // nonce is already in the slice
// stick tokenFlag byte to the end // stick tokenFlag byte to the end
msg[msgLen-1] = tokenFlag msg[msgLen-1] = tokenFlag
@ -288,7 +287,7 @@ func (self *cryptoId) respondToHandshake(auth, remotePubKeyS, sessionToken []byt
} }
// the initiator nonce is read off the end of the message // the initiator nonce is read off the end of the message
initNonce = msg[msgLen-keyLen-1 : msgLen-1] 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 // 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 // they prove they own the private key belonging to ecdhe-random-pubk
// we can now reconstruct the signed message and recover the peers pubkey // we can now reconstruct the signed message and recover the peers pubkey
@ -304,8 +303,8 @@ func (self *cryptoId) respondToHandshake(auth, remotePubKeyS, sessionToken []byt
// now we find ourselves a long task too, fill it random // now we find ourselves a long task too, fill it random
var resp = make([]byte, resLen) var resp = make([]byte, resLen)
// generate keyLen long nonce // generate shaLen long nonce
respNonce = resp[pubLen : pubLen+keyLen] respNonce = resp[pubLen : pubLen+shaLen]
if _, err = rand.Read(respNonce); err != nil { if _, err = rand.Read(respNonce); err != nil {
return return
} }
@ -343,7 +342,7 @@ func (self *cryptoId) completeHandshake(auth []byte) (respNonce []byte, remoteRa
return return
} }
respNonce = msg[pubLen : pubLen+keyLen] respNonce = msg[pubLen : pubLen+shaLen]
var remoteRandomPubKeyS = msg[:pubLen] var remoteRandomPubKeyS = msg[:pubLen]
if remoteRandomPubKey, err = ImportPublicKey(remoteRandomPubKeyS); err != nil { if remoteRandomPubKey, err = ImportPublicKey(remoteRandomPubKeyS); err != nil {
return return

View file

@ -19,7 +19,6 @@ CryptoMsgRW implements MsgReadWriter a message read writer with encryption and a
it is initialised by cryptoId.NewSession() after a successful crypto handshake on the same IO it is initialised by cryptoId.NewSession() after a successful crypto handshake on the same IO
It uses the legacy devp2p packet structure (temporary) It uses the legacy devp2p packet structure (temporary)
*/ */
type CryptoMsgRW struct { type CryptoMsgRW struct {
r io.Reader r io.Reader
w io.Writer w io.Writer

View file

@ -217,8 +217,8 @@ func (p *Peer) loop() (reason DiscReason, err error) {
// from here on everything can be encrypted, authenticated // from here on everything can be encrypted, authenticated
return DiscProtocolError, err // no graceful disconnect return DiscProtocolError, err // no graceful disconnect
} }
close(p.cryptoReady)
defer p.crw.Close() defer p.crw.Close()
close(p.cryptoReady)
// read loop // read loop
protoDone := make(chan struct{}, 1) protoDone := make(chan struct{}, 1)
@ -258,7 +258,6 @@ loop:
// read or write failed. there is no need to run the // read or write failed. there is no need to run the
// polite disconnect sequence because the connection // polite disconnect sequence because the connection
// is probably dead anyway. // is probably dead anyway.
// TODO: handle write errors as well
return DiscNetworkError, err return DiscNetworkError, err
case err = <-p.protoErr: case err = <-p.protoErr:
reason = discReasonForError(err) reason = discReasonForError(err)
@ -330,6 +329,7 @@ func (p *Peer) handleCryptoHandshake() (err error) {
// cryptoId is just created for the lifecycle of the handshake // cryptoId is just created for the lifecycle of the handshake
// it is survived by an encrypted readwriter // it is survived by an encrypted readwriter
var initiator bool var initiator bool
// TODO: this is clearly a placeholder until we figure how we store session Token
var sessionToken []byte var sessionToken []byte
sessionToken = make([]byte, keyLen) sessionToken = make([]byte, keyLen)
if _, err = rand.Read(sessionToken); err != nil { if _, err = rand.Read(sessionToken); err != nil {