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
29 func newNonceCipher(key *[32]byte) *xtea.Cipher {
30 nonceKey := make([]byte, 16)
34 make([]byte, xtea.BlockSize),
37 ciph, err := xtea.NewCipher(nonceKey)
52 CPRCycle time.Duration `json:"-"`
54 // Cryptography related
55 Key *[SSize]byte `json:"-"`
56 NonceCipher *xtea.Cipher `json:"-"`
60 NonceExpect uint64 `json:"-"`
61 nonceBucket0 map[uint64]struct{}
62 nonceBucket1 map[uint64]struct{}
68 Timeout time.Duration `json:"-"`
72 willSentCycle time.Time
87 BusyR sync.Mutex `json:"-"`
94 BusyT sync.Mutex `json:"-"`
102 func (p *Peer) String() string {
103 return p.Id.String() + ":" + p.Addr
106 // Zero peer's memory state.
107 func (p *Peer) Zero() {
113 sliceZero(p.keyAuthR[:])
114 sliceZero(p.keyAuthT[:])
119 func newPeer(isClient bool, addr string, conn io.Writer, conf *PeerConf, key *[SSize]byte) *Peer {
121 timeout := conf.Timeout
123 cprCycle := cprCycleCalculate(conf.CPR)
124 noiseEnable := conf.NoiseEnable
129 timeout = timeout / TimeoutHeartbeat
137 NoiseEnable: noiseEnable,
142 NonceCipher: newNonceCipher(key),
143 nonceBucket0: make(map[uint64]struct{}, NonceBucketSize),
144 nonceBucket1: make(map[uint64]struct{}, NonceBucketSize),
150 bufR: make([]byte, S20BS+MTU+NonceSize),
151 bufT: make([]byte, S20BS+MTU+NonceSize),
152 tagR: new([TagSize]byte),
153 tagT: new([TagSize]byte),
154 keyAuthR: new([SSize]byte),
155 keyAuthT: new([SSize]byte),
159 peer.NonceExpect = 0 + 2
162 peer.NonceExpect = 1 + 2
168 // Process incoming Ethernet packet.
169 // ready channel is TAPListen's synchronization channel used to tell him
170 // that he is free to receive new packets. Encrypted and authenticated
171 // packets will be sent to remote Peer side immediately.
172 func (p *Peer) EthProcess(data []byte) {
176 // Zero size is a heartbeat packet
178 // If this heartbeat is necessary
179 if !p.LastSent.Add(p.Timeout).Before(p.now) {
183 p.bufT[S20BS+0] = byte(0)
184 p.bufT[S20BS+1] = byte(0)
187 // Copy payload to our internal buffer and we are ready to
188 // accept the next one
189 binary.BigEndian.PutUint16(
190 p.bufT[S20BS:S20BS+PktSizeSize],
193 copy(p.bufT[S20BS+PktSizeSize:], data)
194 p.BytesPayloadOut += int64(len(data))
198 p.frameT = p.bufT[S20BS : S20BS+MTU-TagSize]
200 p.frameT = p.bufT[S20BS : S20BS+PktSizeSize+len(data)+NonceSize]
203 binary.BigEndian.PutUint64(p.frameT[len(p.frameT)-NonceSize:], p.nonceOur)
204 p.NonceCipher.Encrypt(
205 p.frameT[len(p.frameT)-NonceSize:],
206 p.frameT[len(p.frameT)-NonceSize:],
208 for i := 0; i < SSize; i++ {
211 salsa20.XORKeyStream(
212 p.bufT[:S20BS+len(p.frameT)-NonceSize],
213 p.bufT[:S20BS+len(p.frameT)-NonceSize],
214 p.frameT[len(p.frameT)-NonceSize:],
218 copy(p.keyAuthT[:], p.bufT[:SSize])
219 poly1305.Sum(p.tagT, p.frameT, p.keyAuthT)
221 atomic.AddInt64(&p.BytesOut, int64(len(p.frameT)+TagSize))
224 if p.CPRCycle != time.Duration(0) {
225 p.willSentCycle = p.LastSent.Add(p.CPRCycle)
226 if p.willSentCycle.After(p.now) {
227 time.Sleep(p.willSentCycle.Sub(p.now))
228 p.now = p.willSentCycle
233 p.Conn.Write(append(p.tagT[:], p.frameT...))
237 func (p *Peer) PktProcess(data []byte, tap io.Writer, reorderable bool) bool {
239 for i := 0; i < SSize; i++ {
242 copy(p.bufR[S20BS:], data[TagSize:])
243 salsa20.XORKeyStream(
244 p.bufR[:S20BS+len(data)-TagSize-NonceSize],
245 p.bufR[:S20BS+len(data)-TagSize-NonceSize],
246 data[len(data)-NonceSize:],
250 copy(p.keyAuthR[:], p.bufR[:SSize])
251 copy(p.tagR[:], data[:TagSize])
252 if !poly1305.Verify(p.tagR, data[TagSize:], p.keyAuthR) {
258 // Check if received nonce is known to us in either of two buckets.
259 // If yes, then this is ignored duplicate.
260 // Check from the oldest bucket, as in most cases this will result
261 // in constant time check.
262 // If Bucket0 is filled, then it becomes Bucket1.
263 p.NonceCipher.Decrypt(
264 data[len(data)-NonceSize:],
265 data[len(data)-NonceSize:],
267 p.nonceRecv = binary.BigEndian.Uint64(data[len(data)-NonceSize:])
269 _, p.nonceFound0 = p.nonceBucket0[p.nonceRecv]
270 _, p.nonceFound1 = p.nonceBucket1[p.nonceRecv]
271 if p.nonceFound0 || p.nonceFound1 || p.nonceRecv+2*NonceBucketSize < p.nonceLatest {
276 p.nonceBucket0[p.nonceRecv] = struct{}{}
278 if p.nonceBucketN == NonceBucketSize {
279 p.nonceBucket1 = p.nonceBucket0
280 p.nonceBucket0 = make(map[uint64]struct{}, NonceBucketSize)
284 if p.nonceRecv != p.NonceExpect {
291 if p.nonceRecv > p.nonceLatest {
292 p.nonceLatest = p.nonceRecv
296 atomic.AddInt64(&p.BytesIn, int64(len(data)))
297 p.LastPing = time.Now()
298 p.pktSizeR = binary.BigEndian.Uint16(p.bufR[S20BS : S20BS+PktSizeSize])
305 p.BytesPayloadIn += int64(p.pktSizeR)
306 tap.Write(p.bufR[S20BS+PktSizeSize : S20BS+PktSizeSize+p.pktSizeR])