)
const (
+ // RSize is size in bytes of channel binding random value
RSize = 8
+ // SSize is size in bytes of shared secret half
SSize = 32
)
+// Handshake is state of a handshake/negotiation between client and server
type Handshake struct {
addr string
conn io.Writer
return &hashed
}
-// Create new handshake state.
+// NewHandshake creates new handshake state.
func NewHandshake(addr string, conn io.Writer, conf *PeerConf) *Handshake {
state := Handshake{
addr: addr,
}
// Generate ID tag from client identification and data.
-func idTag(id *PeerId, timeSync int, data []byte) []byte {
+func idTag(id *PeerID, timeSync int, data []byte) []byte {
enc := make([]byte, 8)
copy(enc, data)
AddTimeSync(timeSync, enc)
panic(err)
}
mac.Write(enc)
- mac.Sum(enc[:0])
- return enc
+ sum := mac.Sum(nil)
+ return sum[len(sum)-8:]
}
-// Start handshake's procedure from the client. It is the entry point
-// for starting the handshake procedure. // First handshake packet
-// will be sent immediately.
+// HandshakeStarts start handshake's procedure from the client.
+// It is the entry point for starting the handshake procedure.
+// First handshake packet will be sent immediately.
func HandshakeStart(addr string, conn io.Writer, conf *PeerConf) *Handshake {
state := NewHandshake(addr, conn, conf)
var dhPubRepr *[32]byte
chacha20.XORKeyStream(enc, enc, state.rNonce, state.dsaPubH)
}
data := append(state.rNonce[8:], enc...)
- data = append(data, idTag(state.Conf.Id, state.Conf.TimeSync, state.rNonce[8:])...)
+ data = append(data, idTag(state.Conf.ID, state.Conf.TimeSync, state.rNonce[8:])...)
state.conn.Write(data)
return state
}
-// Process handshake message on the server side.
+// Server processes handshake message on the server side.
// This function is intended to be called on server's side.
// If this is the final handshake message, then new Peer object
// will be created and used as a transport. If no mutually
// Send that to client
h.conn.Write(append(encPub, append(
- encRs, idTag(h.Conf.Id, h.Conf.TimeSync, encPub)...,
+ encRs, idTag(h.Conf.ID, h.Conf.TimeSync, encPub)...,
)...))
h.LastPing = time.Now()
} else
} else {
chacha20.XORKeyStream(enc, enc, h.rNonceNext(2), h.key)
}
- h.conn.Write(append(enc, idTag(h.Conf.Id, h.Conf.TimeSync, enc)...))
+ h.conn.Write(append(enc, idTag(h.Conf.ID, h.Conf.TimeSync, enc)...))
// Switch peer
peer := newPeer(
return nil
}
-// Process handshake message on the client side.
+// Client processes handshake message on the client side.
// This function is intended to be called on client's side.
// If this is the final handshake message, then new Peer object
// will be created and used as a transport. If no mutually
}
// Send that to server
- h.conn.Write(append(enc, idTag(h.Conf.Id, h.Conf.TimeSync, enc)...))
+ h.conn.Write(append(enc, idTag(h.Conf.ID, h.Conf.TimeSync, enc)...))
h.LastPing = time.Now()
} else
// ENC(K, R+2, RC) + IDtag