const (
NonceSize = 8
- KeySize = 32
// S20BS is Salsa20's internal blocksize in bytes
S20BS = 64
// Maximal amount of bytes transfered with single key (4 GiB)
type Peer struct {
Addr *net.UDPAddr
- Id PeerId
+ Id *PeerId
// Traffic behaviour
NoiseEnable bool
CPRCycle time.Duration `json:"-"`
// Cryptography related
- Key *[KeySize]byte `json:"-"`
+ Key *[SSize]byte `json:"-"`
Noncediff int
NonceOur uint64 `json:"-"`
NonceRecv uint64 `json:"-"`
// This variables are initialized only once to relief GC
buf []byte
tag *[poly1305.TagSize]byte
- keyAuth *[KeySize]byte
+ keyAuth *[32]byte
nonceRecv uint64
frame []byte
nonce []byte
return p.Id.String() + ":" + p.Addr.String()
}
-// Zero peer's memory state
+// Zero peer's memory state.
func (p *Peer) Zero() {
sliceZero(p.Key[:])
sliceZero(p.tag[:])
return sink, buf, sinkReady
}
-func newNonceCipher(key *[KeySize]byte) *xtea.Cipher {
+func newNonceCipher(key *[32]byte) *xtea.Cipher {
nonceKey := make([]byte, 16)
salsa20.XORKeyStream(
nonceKey,
- make([]byte, KeySize),
+ make([]byte, 32),
make([]byte, xtea.BlockSize),
key,
)
return time.Second / time.Duration(rate*(1<<10)/MTU)
}
-func newPeer(addr *net.UDPAddr, id PeerId, nonce int, key *[KeySize]byte) *Peer {
+func newPeer(addr *net.UDPAddr, conf *PeerConf, nonce int, key *[SSize]byte) *Peer {
now := time.Now()
- conf := id.ConfGet()
timeout := conf.Timeout
cprCycle := cprCycleCalculate(conf.CPR)
noiseEnable := conf.NoiseEnable
Timeout: timeout,
Established: now,
LastPing: now,
- Id: id,
+ Id: conf.Id,
NoiseEnable: noiseEnable,
CPR: conf.CPR,
CPRCycle: cprCycle,
NonceCipher: newNonceCipher(key),
buf: make([]byte, MTU+S20BS),
tag: new([poly1305.TagSize]byte),
- keyAuth: new([KeySize]byte),
+ keyAuth: new([SSize]byte),
nonce: make([]byte, NonceSize),
}
return &peer
udpPkt[:NonceSize],
p.Key,
)
- copy(p.keyAuth[:], p.buf[:KeySize])
+ copy(p.keyAuth[:], p.buf[:SSize])
if !poly1305.Verify(p.tag, udpPkt[:size-poly1305.TagSize], p.keyAuth) {
ready <- struct{}{}
p.FramesUnauth++
salsa20.XORKeyStream(p.buf, p.buf, p.nonce, p.Key)
copy(p.buf[S20BS-NonceSize:S20BS], p.nonce)
- copy(p.keyAuth[:], p.buf[:KeySize])
+ copy(p.keyAuth[:], p.buf[:SSize])
if p.NoiseEnable {
p.frame = p.buf[S20BS-NonceSize : S20BS+MTU-NonceSize-poly1305.TagSize]
} else {