10 "golang.org/x/crypto/poly1305"
11 "golang.org/x/crypto/salsa20"
12 "golang.org/x/crypto/xtea"
18 TagSize = poly1305.TagSize
19 // S20BS is Salsa20's internal blocksize in bytes
21 // Maximal amount of bytes transfered with single key (4 GiB)
22 MaxBytesPerKey int64 = 1 << 32
23 // Size of packet's size mark in bytes
25 // Heartbeat rate, relative to Timeout
27 // Minimal valid packet length
28 MinPktLength = 2 + 16 + 8
31 func newNonceCipher(key *[32]byte) *xtea.Cipher {
32 nonceKey := make([]byte, 16)
36 make([]byte, xtea.BlockSize),
39 ciph, err := xtea.NewCipher(nonceKey)
54 CPRCycle time.Duration `json:"-"`
56 // Cryptography related
57 Key *[SSize]byte `json:"-"`
58 NonceCipher *xtea.Cipher `json:"-"`
62 NonceExpect uint64 `json:"-"`
63 nonceBucket0 map[uint64]struct{}
64 nonceBucket1 map[uint64]struct{}
70 Timeout time.Duration `json:"-"`
74 willSentCycle time.Time
89 BusyR sync.Mutex `json:"-"`
96 BusyT sync.Mutex `json:"-"`
104 func (p *Peer) String() string {
105 return p.Id.String() + ":" + p.Addr
108 // Zero peer's memory state.
109 func (p *Peer) Zero() {
115 sliceZero(p.keyAuthR[:])
116 sliceZero(p.keyAuthT[:])
121 func newPeer(isClient bool, addr string, conn io.Writer, conf *PeerConf, key *[SSize]byte) *Peer {
123 timeout := conf.Timeout
125 cprCycle := cprCycleCalculate(conf.CPR)
126 noiseEnable := conf.NoiseEnable
131 timeout = timeout / TimeoutHeartbeat
139 NoiseEnable: noiseEnable,
144 NonceCipher: newNonceCipher(key),
145 nonceBucket0: make(map[uint64]struct{}, NonceBucketSize),
146 nonceBucket1: make(map[uint64]struct{}, NonceBucketSize),
152 bufR: make([]byte, S20BS+MTU+NonceSize),
153 bufT: make([]byte, S20BS+MTU+NonceSize),
154 tagR: new([TagSize]byte),
155 tagT: new([TagSize]byte),
156 keyAuthR: new([SSize]byte),
157 keyAuthT: new([SSize]byte),
161 peer.NonceExpect = 0 + 2
164 peer.NonceExpect = 1 + 2
170 // Process incoming Ethernet packet.
171 // ready channel is TAPListen's synchronization channel used to tell him
172 // that he is free to receive new packets. Encrypted and authenticated
173 // packets will be sent to remote Peer side immediately.
174 func (p *Peer) EthProcess(data []byte) {
178 // Zero size is a heartbeat packet
180 // If this heartbeat is necessary
181 if !p.LastSent.Add(p.Timeout).Before(p.now) {
185 p.bufT[S20BS+0] = byte(0)
186 p.bufT[S20BS+1] = byte(0)
189 // Copy payload to our internal buffer and we are ready to
190 // accept the next one
191 binary.BigEndian.PutUint16(
192 p.bufT[S20BS:S20BS+PktSizeSize],
195 copy(p.bufT[S20BS+PktSizeSize:], data)
196 p.BytesPayloadOut += int64(len(data))
200 p.frameT = p.bufT[S20BS : S20BS+MTU-TagSize]
202 p.frameT = p.bufT[S20BS : S20BS+PktSizeSize+len(data)+NonceSize]
205 binary.BigEndian.PutUint64(p.frameT[len(p.frameT)-NonceSize:], p.nonceOur)
206 p.NonceCipher.Encrypt(
207 p.frameT[len(p.frameT)-NonceSize:],
208 p.frameT[len(p.frameT)-NonceSize:],
210 for i := 0; i < SSize; i++ {
213 salsa20.XORKeyStream(
214 p.bufT[:S20BS+len(p.frameT)-NonceSize],
215 p.bufT[:S20BS+len(p.frameT)-NonceSize],
216 p.frameT[len(p.frameT)-NonceSize:],
220 copy(p.keyAuthT[:], p.bufT[:SSize])
221 poly1305.Sum(p.tagT, p.frameT, p.keyAuthT)
223 atomic.AddInt64(&p.BytesOut, int64(len(p.frameT)+TagSize))
226 if p.CPRCycle != time.Duration(0) {
227 p.willSentCycle = p.LastSent.Add(p.CPRCycle)
228 if p.willSentCycle.After(p.now) {
229 time.Sleep(p.willSentCycle.Sub(p.now))
230 p.now = p.willSentCycle
235 p.Conn.Write(append(p.tagT[:], p.frameT...))
239 func (p *Peer) PktProcess(data []byte, tap io.Writer, reorderable bool) bool {
241 for i := 0; i < SSize; i++ {
244 copy(p.bufR[S20BS:], data[TagSize:])
245 salsa20.XORKeyStream(
246 p.bufR[:S20BS+len(data)-TagSize-NonceSize],
247 p.bufR[:S20BS+len(data)-TagSize-NonceSize],
248 data[len(data)-NonceSize:],
252 copy(p.keyAuthR[:], p.bufR[:SSize])
253 copy(p.tagR[:], data[:TagSize])
254 if !poly1305.Verify(p.tagR, data[TagSize:], p.keyAuthR) {
260 // Check if received nonce is known to us in either of two buckets.
261 // If yes, then this is ignored duplicate.
262 // Check from the oldest bucket, as in most cases this will result
263 // in constant time check.
264 // If Bucket0 is filled, then it becomes Bucket1.
265 p.NonceCipher.Decrypt(
266 data[len(data)-NonceSize:],
267 data[len(data)-NonceSize:],
269 p.nonceRecv = binary.BigEndian.Uint64(data[len(data)-NonceSize:])
271 _, p.nonceFound0 = p.nonceBucket0[p.nonceRecv]
272 _, p.nonceFound1 = p.nonceBucket1[p.nonceRecv]
273 if p.nonceFound0 || p.nonceFound1 || p.nonceRecv+2*NonceBucketSize < p.nonceLatest {
278 p.nonceBucket0[p.nonceRecv] = struct{}{}
280 if p.nonceBucketN == NonceBucketSize {
281 p.nonceBucket1 = p.nonceBucket0
282 p.nonceBucket0 = make(map[uint64]struct{}, NonceBucketSize)
286 if p.nonceRecv != p.NonceExpect {
293 if p.nonceRecv > p.nonceLatest {
294 p.nonceLatest = p.nonceRecv
298 atomic.AddInt64(&p.BytesIn, int64(len(data)))
299 p.LastPing = time.Now()
300 p.pktSizeR = binary.BigEndian.Uint16(p.bufR[S20BS : S20BS+PktSizeSize])
307 p.BytesPayloadIn += int64(p.pktSizeR)
308 tap.Write(p.bufR[S20BS+PktSizeSize : S20BS+PktSizeSize+p.pktSizeR])