-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpacket.go
634 lines (522 loc) · 20.1 KB
/
packet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
package netcode
import (
"fmt"
)
const CONNECT_TOKEN_PRIVATE_BYTES = 1024
const CHALLENGE_TOKEN_BYTES = 300
const VERSION_INFO_BYTES = 13
const USER_DATA_BYTES = 256
const MAX_PACKET_BYTES = 1220
const MAX_PAYLOAD_BYTES = 1200
const MAX_ADDRESS_STRING_LENGTH = 256
const REPLAY_PROTECTION_BUFFER_SIZE = 256
const KEY_BYTES = 32
const MAC_BYTES = 16
const NONCE_BYTES = 8
const MAX_SERVERS_PER_CONNECT = 32
const VERSION_INFO = "NETCODE 1.01\x00"
const (
ConnectionRequest PacketType = iota
ConnectionDenied
ConnectionChallenge
ConnectionResponse
ConnectionKeepAlive
ConnectionPayload
ConnectionDisconnect
)
// Used for determining the type of packet, part of the serialization protocol
type PacketType uint8
func (p PacketType) Peek(packetBuffer []byte) PacketType {
prefix := uint8(packetBuffer[0])
return PacketType(prefix & 0xF)
}
// reference map of packet -> string values
var packetTypeMap = map[PacketType]string{
ConnectionRequest: "CONNECTION_REQUEST",
ConnectionDenied: "CONNECTION_DENIED",
ConnectionChallenge: "CONNECTION_CHALLENGE",
ConnectionResponse: "CONNECTION_RESPONSE",
ConnectionKeepAlive: "CONNECTION_KEEPALIVE",
ConnectionPayload: "CONNECTION_PAYLOAD",
ConnectionDisconnect: "CONNECTION_DISCONNECT",
}
// not a packet type, but value is last packetType+1
const ConnectionNumPackets = ConnectionDisconnect + 1
// Packet interface supporting reading and writing.
type Packet interface {
GetType() PacketType // The type of packet
Sequence() uint64 // sequence number of this packet, if it supports it // returns the packet type
Write(buf []byte, protocolId, sequence uint64, writePacketKey []byte) (int, error) // writes and encrypts the packet data to the supplied buffer.
Read(packetData []byte, packetLen int, protocolId, currentTimestamp uint64, readPacketKey, privateKey, allowedPackets []byte, replayProtection *ReplayProtection) error // reads in and decrypts from the supplied buffer to set the packet properties
}
// Returns the type of packet given packetbuffer by peaking the packet type
func NewPacket(packetBuffer []byte) Packet {
var packetType PacketType
t := packetType.Peek(packetBuffer)
switch t {
case ConnectionRequest:
return &RequestPacket{}
case ConnectionDenied:
return &DeniedPacket{}
case ConnectionChallenge:
return &ChallengePacket{}
case ConnectionResponse:
return &ResponsePacket{}
case ConnectionKeepAlive:
return &KeepAlivePacket{}
case ConnectionPayload:
return &PayloadPacket{}
case ConnectionDisconnect:
return &DisconnectPacket{}
}
return nil
}
// The connection request packet
type RequestPacket struct {
VersionInfo []byte // version information of communications
ProtocolId uint64 // protocol id used in communications
ConnectTokenExpireTimestamp uint64 // when the connect token expires
ConnectTokenSequence uint64 // the sequence id of this token
Token *ConnectTokenPrivate // reference to the private parts of this packet
ConnectTokenData []byte // the encrypted Token after Write -> Encrypt
}
// request packets do not have a sequence value
func (p *RequestPacket) Sequence() uint64 {
return 0
}
// Writes the RequestPacket data to a supplied buffer and returns the length of bytes written to it.
func (p *RequestPacket) Write(buf []byte, protocolId, sequence uint64, writePacketKey []byte) (int, error) {
buffer := NewBufferFromRef(buf)
buffer.WriteUint8(uint8(ConnectionRequest))
buffer.WriteBytes(p.VersionInfo)
buffer.WriteUint64(p.ProtocolId)
buffer.WriteUint64(p.ConnectTokenExpireTimestamp)
buffer.WriteUint64(p.ConnectTokenSequence)
buffer.WriteBytes(p.ConnectTokenData) // write the encrypted connection token private data
if buffer.Pos != 1+13+8+8+8+CONNECT_TOKEN_PRIVATE_BYTES {
return -1, ErrInvalidBufferSize
}
return buffer.Pos, nil
}
// Reads a request packet and decrypts the connect token private data. Request packets do not return a sequenceId
func (p *RequestPacket) Read(packetData []byte, packetLen int, protocolId, currentTimestamp uint64, readPacketKey, privateKey, allowedPackets []byte, replayProtection *ReplayProtection) error {
var err error
var packetType uint8
packetBuffer := NewBufferFromRef(packetData)
if packetType, err = packetBuffer.GetUint8(); err != nil || PacketType(packetType) != ConnectionRequest {
return ErrInvalidPacket
}
if allowedPackets[0] == 0 {
return ErrRequestPacketTypeNotAllowed
}
if packetLen != 1+VERSION_INFO_BYTES+8+8+8+CONNECT_TOKEN_PRIVATE_BYTES {
return ErrRequestBadPacketLength
}
if privateKey == nil {
return ErrRequestPacketNoPrivateKey
}
p.VersionInfo, err = packetBuffer.GetBytes(VERSION_INFO_BYTES)
if err != nil {
return ErrRequestPacketBadVersionInfoBytes
}
if string(p.VersionInfo) != VERSION_INFO {
return ErrRequestPacketBadVersionInfo
}
p.ProtocolId, err = packetBuffer.GetUint64()
if err != nil || p.ProtocolId != protocolId {
return ErrRequestPacketBadProtocolId
}
p.ConnectTokenExpireTimestamp, err = packetBuffer.GetUint64()
if err != nil || p.ConnectTokenExpireTimestamp <= currentTimestamp {
return ErrRequestPacketConnectTokenExpired
}
p.ConnectTokenSequence, err = packetBuffer.GetUint64()
if err != nil {
return err
}
if packetBuffer.Pos != 1+VERSION_INFO_BYTES+8+8+8 {
return ErrRequestPacketBufferInvalidLength
}
var tokenBuffer []byte
tokenBuffer, err = packetBuffer.GetBytes(CONNECT_TOKEN_PRIVATE_BYTES)
if err != nil {
return err
}
p.Token = NewConnectTokenPrivateEncrypted(tokenBuffer)
if _, err := p.Token.Decrypt(p.ProtocolId, p.ConnectTokenExpireTimestamp, p.ConnectTokenSequence, privateKey); err != nil {
return fmt.Errorf("error decrypting connect token private data: %s", err)
}
if err := p.Token.Read(); err != nil {
return fmt.Errorf("error reading decrypted connect token private data: %s", err)
}
return nil
}
func (p *RequestPacket) GetType() PacketType {
return ConnectionRequest
}
// Denied packet type, contains no information
type DeniedPacket struct {
sequence uint64
}
func (p *DeniedPacket) Sequence() uint64 {
return p.sequence
}
func (p *DeniedPacket) Write(buf []byte, protocolId, sequence uint64, writePacketKey []byte) (int, error) {
buffer := NewBufferFromRef(buf)
prefixByte, err := writePacketPrefix(p, buffer, sequence)
if err != nil {
return -1, err
}
// denied packets are empty
return encryptPacket(buffer, buffer.Pos, buffer.Pos, prefixByte, protocolId, sequence, writePacketKey)
}
func (p *DeniedPacket) Read(packetData []byte, packetLen int, protocolId, currentTimestamp uint64, readPacketKey, privateKey, allowedPackets []byte, replayProtection *ReplayProtection) error {
packetBuffer := NewBufferFromRef(packetData)
sequence, decryptedBuf, err := decryptPacket(packetBuffer, packetLen, protocolId, readPacketKey, allowedPackets, replayProtection)
if err != nil {
return err
}
p.sequence = sequence
if decryptedBuf.Len() != 0 {
return ErrDeniedPacketDecryptedDataSize
}
return nil
}
func (p *DeniedPacket) GetType() PacketType {
return ConnectionDenied
}
// Challenge packet containing token data and the sequence id used
type ChallengePacket struct {
sequence uint64
ChallengeTokenSequence uint64
ChallengeTokenData []byte
}
func (p *ChallengePacket) Sequence() uint64 {
return p.sequence
}
func (p *ChallengePacket) Write(buf []byte, protocolId, sequence uint64, writePacketKey []byte) (int, error) {
buffer := NewBufferFromRef(buf)
prefixByte, err := writePacketPrefix(p, buffer, sequence)
if err != nil {
return -1, err
}
encryptedStart := buffer.Pos
buffer.WriteUint64(p.ChallengeTokenSequence)
buffer.WriteBytesN(p.ChallengeTokenData, CHALLENGE_TOKEN_BYTES)
encryptedFinish := buffer.Pos
return encryptPacket(buffer, encryptedStart, encryptedFinish, prefixByte, protocolId, sequence, writePacketKey)
}
func (p *ChallengePacket) Read(packetData []byte, packetLen int, protocolId, currentTimestamp uint64, readPacketKey, privateKey, allowedPackets []byte, replayProtection *ReplayProtection) error {
packetBuffer := NewBufferFromRef(packetData)
sequence, decryptedBuf, err := decryptPacket(packetBuffer, packetLen, protocolId, readPacketKey, allowedPackets, replayProtection)
if err != nil {
return err
}
p.sequence = sequence
if decryptedBuf.Len() != 8+CHALLENGE_TOKEN_BYTES {
return ErrChallengePacketDecryptedDataSize
}
p.ChallengeTokenSequence, err = decryptedBuf.GetUint64()
if err != nil {
return ErrChallengePacketTokenSequence
}
p.ChallengeTokenData, err = decryptedBuf.GetBytes(CHALLENGE_TOKEN_BYTES)
if err != nil {
return ErrChallengePacketTokenData
}
return nil
}
func (p *ChallengePacket) GetType() PacketType {
return ConnectionChallenge
}
// Response packet, containing the token data and sequence id
type ResponsePacket struct {
sequence uint64
ChallengeTokenSequence uint64
ChallengeTokenData []byte
}
func (p *ResponsePacket) Sequence() uint64 {
return p.sequence
}
func (p *ResponsePacket) Write(buf []byte, protocolId, sequence uint64, writePacketKey []byte) (int, error) {
buffer := NewBufferFromRef(buf)
prefixByte, err := writePacketPrefix(p, buffer, sequence)
if err != nil {
return -1, err
}
encryptedStart := buffer.Pos
buffer.WriteUint64(p.ChallengeTokenSequence)
buffer.WriteBytesN(p.ChallengeTokenData, CHALLENGE_TOKEN_BYTES)
encryptedFinish := buffer.Pos
return encryptPacket(buffer, encryptedStart, encryptedFinish, prefixByte, protocolId, sequence, writePacketKey)
}
func (p *ResponsePacket) Read(packetData []byte, packetLen int, protocolId, currentTimestamp uint64, readPacketKey, privateKey, allowedPackets []byte, replayProtection *ReplayProtection) error {
packetBuffer := NewBufferFromRef(packetData)
sequence, decryptedBuf, err := decryptPacket(packetBuffer, packetLen, protocolId, readPacketKey, allowedPackets, replayProtection)
if err != nil {
return err
}
p.sequence = sequence
if decryptedBuf.Len() != 8+CHALLENGE_TOKEN_BYTES {
return ErrResponsePacketDecryptedDataSize
}
p.ChallengeTokenSequence, err = decryptedBuf.GetUint64()
if err != nil {
return ErrResponsePacketTokenSequence
}
p.ChallengeTokenData, err = decryptedBuf.GetBytes(CHALLENGE_TOKEN_BYTES)
if err != nil {
return ErrResponsePacketTokenData
}
return nil
}
func (p *ResponsePacket) GetType() PacketType {
return ConnectionResponse
}
// used for heart beats
type KeepAlivePacket struct {
sequence uint64
ClientIndex uint32
MaxClients uint32
}
func (p *KeepAlivePacket) Sequence() uint64 {
return p.sequence
}
func (p *KeepAlivePacket) Write(buf []byte, protocolId, sequence uint64, writePacketKey []byte) (int, error) {
buffer := NewBufferFromRef(buf)
prefixByte, err := writePacketPrefix(p, buffer, sequence)
if err != nil {
return -1, err
}
encryptedStart := buffer.Pos
buffer.WriteUint32(uint32(p.ClientIndex))
buffer.WriteUint32(uint32(p.MaxClients))
encryptedFinish := buffer.Pos
return encryptPacket(buffer, encryptedStart, encryptedFinish, prefixByte, protocolId, sequence, writePacketKey)
}
func (p *KeepAlivePacket) Read(packetData []byte, packetLen int, protocolId, currentTimestamp uint64, readPacketKey, privateKey, allowedPackets []byte, replayProtection *ReplayProtection) error {
packetBuffer := NewBufferFromRef(packetData)
sequence, decryptedBuf, err := decryptPacket(packetBuffer, packetLen, protocolId, readPacketKey, allowedPackets, replayProtection)
if err != nil {
return err
}
p.sequence = sequence
if decryptedBuf.Len() != 8 {
return ErrKeepAlivePacketDecryptedDataSize
}
p.ClientIndex, err = decryptedBuf.GetUint32()
if err != nil {
return ErrKeepAlivePacketClientIndex
}
p.MaxClients, err = decryptedBuf.GetUint32()
if err != nil {
return ErrKeepAlivePacketMaxClients
}
return nil
}
func (p *KeepAlivePacket) GetType() PacketType {
return ConnectionKeepAlive
}
// Contains user supplied payload data between server <-> client
type PayloadPacket struct {
sequence uint64
PayloadBytes uint32
PayloadData []byte
}
func (p *PayloadPacket) GetType() PacketType {
return ConnectionPayload
}
// Helper function to create a new payload packet with the supplied buffer
func NewPayloadPacket(payloadData []byte) *PayloadPacket {
packet := &PayloadPacket{}
packet.PayloadBytes = uint32(len(payloadData))
packet.PayloadData = payloadData
return packet
}
func (p *PayloadPacket) Sequence() uint64 {
return p.sequence
}
func (p *PayloadPacket) Write(buf []byte, protocolId, sequence uint64, writePacketKey []byte) (int, error) {
buffer := NewBufferFromRef(buf)
prefixByte, err := writePacketPrefix(p, buffer, sequence)
if err != nil {
return -1, err
}
encryptedStart := buffer.Pos
buffer.WriteBytesN(p.PayloadData, int(p.PayloadBytes))
encryptedFinish := buffer.Pos
return encryptPacket(buffer, encryptedStart, encryptedFinish, prefixByte, protocolId, sequence, writePacketKey)
}
func (p *PayloadPacket) Read(packetData []byte, packetLen int, protocolId, currentTimestamp uint64, readPacketKey, privateKey, allowedPackets []byte, replayProtection *ReplayProtection) error {
packetBuffer := NewBufferFromRef(packetData)
sequence, decryptedBuf, err := decryptPacket(packetBuffer, packetLen, protocolId, readPacketKey, allowedPackets, replayProtection)
if err != nil {
return err
}
p.sequence = sequence
decryptedSize := uint32(decryptedBuf.Len())
if decryptedSize < 1 {
return ErrPayloadPacketTooSmall
}
if decryptedSize > MAX_PAYLOAD_BYTES {
return ErrPayloadPacketTooLarge
}
p.PayloadBytes = decryptedSize
p.PayloadData = decryptedBuf.Bytes()
return nil
}
// Signals to server/client to disconnect, contains no data.
type DisconnectPacket struct {
sequence uint64
}
func (p *DisconnectPacket) Sequence() uint64 {
return p.sequence
}
func (p *DisconnectPacket) Write(buf []byte, protocolId, sequence uint64, writePacketKey []byte) (int, error) {
buffer := NewBufferFromRef(buf)
prefixByte, err := writePacketPrefix(p, buffer, sequence)
if err != nil {
return -1, err
}
// denied packets are empty
return encryptPacket(buffer, buffer.Pos, buffer.Pos, prefixByte, protocolId, sequence, writePacketKey)
}
func (p *DisconnectPacket) Read(packetData []byte, packetLen int, protocolId, currentTimestamp uint64, readPacketKey, privateKey, allowedPackets []byte, replayProtection *ReplayProtection) error {
packetBuffer := NewBufferFromRef(packetData)
sequence, decryptedBuf, err := decryptPacket(packetBuffer, packetLen, protocolId, readPacketKey, allowedPackets, replayProtection)
if err != nil {
return err
}
p.sequence = sequence
if decryptedBuf.Len() != 0 {
return ErrDisconnectPacketDecryptedDataSize
}
return nil
}
func (p *DisconnectPacket) GetType() PacketType {
return ConnectionDisconnect
}
// Decrypts the packet after reading in the prefix byte and sequence id. Used for all PacketTypes except RequestPacket. Returns a buffer containing the decrypted data
func decryptPacket(packetBuffer *Buffer, packetLen int, protocolId uint64, readPacketKey, allowedPackets []byte, replayProtection *ReplayProtection) (uint64, *Buffer, error) {
var packetSequence uint64
prefixByte, err := packetBuffer.GetUint8()
if err != nil {
return 0, nil, ErrInvalidBufferLength
}
if packetSequence, err = readSequence(packetBuffer, packetLen, prefixByte); err != nil {
return 0, nil, err
}
if err := validateSequence(packetLen, prefixByte, packetSequence, readPacketKey, allowedPackets, replayProtection); err != nil {
return 0, nil, err
}
// decrypt the per-packet type data
additionalData, nonce := packetCryptData(prefixByte, protocolId, packetSequence)
encryptedSize := packetLen - packetBuffer.Pos
if encryptedSize < MAC_BYTES {
return 0, nil, ErrDecryptPacketPayloadTooSmall
}
encryptedBuff, err := packetBuffer.GetBytes(encryptedSize)
if err != nil {
return 0, nil, ErrDecryptPacketPayloadTooSmall
}
decryptedBuff, err := DecryptAead(encryptedBuff, additionalData, nonce, readPacketKey)
if err != nil {
return 0, nil, fmt.Errorf("ignored encrypted packet. failed to decrypt: %s", err)
}
return packetSequence, NewBufferFromRef(decryptedBuff), nil
}
// Reads and verifies the sequence id
func readSequence(packetBuffer *Buffer, packetLen int, prefixByte uint8) (uint64, error) {
var sequence uint64
sequenceBytes := prefixByte >> 4
if sequenceBytes < 1 || sequenceBytes > 8 {
return 0, ErrEncryptedPacketSequenceOutOfRange
}
if packetLen < 1+int(sequenceBytes)+MAC_BYTES {
return 0, ErrEncryptedPacketBufferTooSmall
}
var i uint8
// read variable length sequence number [1,8]
for i = 0; i < sequenceBytes; i += 1 {
val, err := packetBuffer.GetUint8()
if err != nil {
return 0, err
}
sequence |= (uint64(val) << (8 * i))
}
return sequence, nil
}
// Validates the data prior to the encrypted segment before we bother attempting to decrypt.
func validateSequence(packetLen int, prefixByte uint8, sequence uint64, readPacketKey, allowedPackets []byte, replayProtection *ReplayProtection) error {
if readPacketKey == nil {
return ErrEmptyPacketKey
}
if packetLen < 1+1+MAC_BYTES {
return ErrEncryptedPacketTooSmall
}
packetType := prefixByte & 0xF
if PacketType(packetType) >= ConnectionNumPackets {
return fmt.Errorf("ignored encrypted packet. packet type %s is invalid", packetTypeMap[PacketType(packetType)])
}
if allowedPackets[packetType] == 0 {
return fmt.Errorf("ignored encrypted packet. packet type %s is invalid", packetTypeMap[PacketType(packetType)])
}
// replay protection (optional)
if replayProtection != nil && PacketType(packetType) >= ConnectionKeepAlive {
if replayProtection.AlreadyReceived(sequence) {
return fmt.Errorf("ignored connection payload packet. sequence %d already received (replay protection)", sequence)
}
}
return nil
}
// write the prefix byte (this is a combination of the packet type and number of sequence bytes)
func writePacketPrefix(p Packet, buffer *Buffer, sequence uint64) (uint8, error) {
sequenceBytes := sequenceNumberBytesRequired(sequence)
if sequenceBytes < 1 || sequenceBytes > 8 {
return 0, ErrInvalidSequenceBytes
}
prefixByte := uint8(p.GetType()) | uint8(sequenceBytes<<4)
buffer.WriteUint8(prefixByte)
sequenceTemp := sequence
var i uint8
for ; i < sequenceBytes; i += 1 {
buffer.WriteUint8(uint8(sequenceTemp & 0xFF))
sequenceTemp >>= 8
}
return prefixByte, nil
}
// Encrypts the packet data of the supplied buffer between encryptedStart and encrypedFinish.
func encryptPacket(buffer *Buffer, encryptedStart, encryptedFinish int, prefixByte uint8, protocolId, sequence uint64, writePacketKey []byte) (int, error) {
// slice up the buffer for the bits we will encrypt
encryptedBuffer := buffer.Buf[encryptedStart:encryptedFinish]
additionalData, nonce := packetCryptData(prefixByte, protocolId, sequence)
if err := EncryptAead(encryptedBuffer, additionalData, nonce, writePacketKey); err != nil {
return -1, err
}
buffer.Pos += MAC_BYTES
return buffer.Pos, nil
}
// used for encrypting the per-packet packet written with the prefix byte, protocol id and version as the associated data. this must match to decrypt.
func packetCryptData(prefixByte uint8, protocolId, sequence uint64) ([]byte, []byte) {
additionalData := NewBuffer(VERSION_INFO_BYTES + 8 + 1)
additionalData.WriteBytesN([]byte(VERSION_INFO), VERSION_INFO_BYTES)
additionalData.WriteUint64(protocolId)
additionalData.WriteUint8(prefixByte)
nonce := NewBuffer(SizeUint64 + SizeUint32)
nonce.WriteUint32(0)
nonce.WriteUint64(sequence)
return additionalData.Buf, nonce.Buf
}
// Depending on size of sequence number, we need to reserve N bytes
func sequenceNumberBytesRequired(sequence uint64) uint8 {
var mask uint64
mask = 0xFF00000000000000
var i uint8
for ; i < 7; i += 1 {
if sequence&mask != 0 {
break
}
mask >>= 8
}
return 8 - i
}