mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-21 04:06:44 +00:00
a few minor changes from review
This commit is contained in:
parent
2e76f1b6ba
commit
94af107fbc
3 changed files with 14 additions and 16 deletions
|
|
@ -14,13 +14,13 @@ import (
|
|||
|
||||
var clogger = ethlogger.NewLogger("CRYPTOID")
|
||||
|
||||
var (
|
||||
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
|
||||
keyLen int = 32 // ECDSA
|
||||
msgLen int = 194 // sigLen + keyLen + pubLen + keyLen + 1 = 194
|
||||
resLen int = 97 // pubLen + keyLen + 1
|
||||
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
|
||||
)
|
||||
|
|
@ -191,7 +191,7 @@ func (self *cryptoId) startHandshake(remotePubKeyS, sessionToken []byte) (auth [
|
|||
return
|
||||
}
|
||||
|
||||
var tokenFlag byte
|
||||
var tokenFlag byte // = 0x00
|
||||
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
|
||||
|
|
@ -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 {
|
||||
return
|
||||
}
|
||||
// tokenFlag = 0x00 // redundant
|
||||
} else {
|
||||
// for known peers, we use stored token from the previous session
|
||||
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)
|
||||
// allocate msgLen long message,
|
||||
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 {
|
||||
return
|
||||
}
|
||||
|
|
@ -238,9 +237,9 @@ func (self *cryptoId) startHandshake(remotePubKeyS, sessionToken []byte) (auth [
|
|||
if randomPubKey64, err = ExportPublicKey(&randomPrvKey.PublicKey); err != nil {
|
||||
return
|
||||
}
|
||||
copy(msg[sigLen:sigLen+keyLen], crypto.Sha3(randomPubKey64))
|
||||
copy(msg[sigLen:sigLen+shaLen], crypto.Sha3(randomPubKey64))
|
||||
// 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
|
||||
// stick tokenFlag byte to the end
|
||||
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
|
||||
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
|
||||
// they prove they own the private key belonging to ecdhe-random-pubk
|
||||
// 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
|
||||
var resp = make([]byte, resLen)
|
||||
// generate keyLen long nonce
|
||||
respNonce = resp[pubLen : pubLen+keyLen]
|
||||
// generate shaLen long nonce
|
||||
respNonce = resp[pubLen : pubLen+shaLen]
|
||||
if _, err = rand.Read(respNonce); err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -343,7 +342,7 @@ func (self *cryptoId) completeHandshake(auth []byte) (respNonce []byte, remoteRa
|
|||
return
|
||||
}
|
||||
|
||||
respNonce = msg[pubLen : pubLen+keyLen]
|
||||
respNonce = msg[pubLen : pubLen+shaLen]
|
||||
var remoteRandomPubKeyS = msg[:pubLen]
|
||||
if remoteRandomPubKey, err = ImportPublicKey(remoteRandomPubKeyS); err != nil {
|
||||
return
|
||||
|
|
|
|||
|
|
@ -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 uses the legacy devp2p packet structure (temporary)
|
||||
*/
|
||||
|
||||
type CryptoMsgRW struct {
|
||||
r io.Reader
|
||||
w io.Writer
|
||||
|
|
|
|||
|
|
@ -217,8 +217,8 @@ func (p *Peer) loop() (reason DiscReason, err error) {
|
|||
// from here on everything can be encrypted, authenticated
|
||||
return DiscProtocolError, err // no graceful disconnect
|
||||
}
|
||||
close(p.cryptoReady)
|
||||
defer p.crw.Close()
|
||||
close(p.cryptoReady)
|
||||
|
||||
// read loop
|
||||
protoDone := make(chan struct{}, 1)
|
||||
|
|
@ -258,7 +258,6 @@ loop:
|
|||
// read or write failed. there is no need to run the
|
||||
// polite disconnect sequence because the connection
|
||||
// is probably dead anyway.
|
||||
// TODO: handle write errors as well
|
||||
return DiscNetworkError, err
|
||||
case err = <-p.protoErr:
|
||||
reason = discReasonForError(err)
|
||||
|
|
@ -330,6 +329,7 @@ func (p *Peer) handleCryptoHandshake() (err error) {
|
|||
// cryptoId is just created for the lifecycle of the handshake
|
||||
// it is survived by an encrypted readwriter
|
||||
var initiator bool
|
||||
// TODO: this is clearly a placeholder until we figure how we store session Token
|
||||
var sessionToken []byte
|
||||
sessionToken = make([]byte, keyLen)
|
||||
if _, err = rand.Read(sessionToken); err != nil {
|
||||
|
|
|
|||
Loading…
Reference in a new issue