-
Notifications
You must be signed in to change notification settings - Fork 14
/
shannon.go
146 lines (118 loc) · 2.89 KB
/
shannon.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
package spotcontrol
import (
"bytes"
"encoding/binary"
"io"
"log"
)
type shannonStream struct {
sendNonce uint32
sendCipher shn_ctx
recvCipher shn_ctx
recvNonce uint32
reader io.Reader
writer io.Writer
}
func setKey(ctx *shn_ctx, key []uint8) {
shn_key(ctx, key, len(key))
nonce := make([]byte, 4)
binary.BigEndian.PutUint32(nonce, 0)
shn_nonce(ctx, nonce, len(nonce))
}
func setupStream(keys sharedKeys, conn plainConnection) packetStream {
s := &shannonStream{
reader: conn.reader,
writer: conn.writer,
}
setKey(&s.recvCipher, keys.recvKey)
setKey(&s.sendCipher, keys.sendKey)
return s
}
func (s *shannonStream) SendPacket(cmd uint8, data []byte) (err error) {
_, err = s.Write(cipherPacket(cmd, data))
if err != nil {
return
}
err = s.FinishSend()
return
}
func cipherPacket(cmd uint8, data []byte) []byte {
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, cmd)
binary.Write(buf, binary.BigEndian, uint16(len(data)))
buf.Write(data)
return buf.Bytes()
}
func (s *shannonStream) Encrypt(message string) []byte {
messageBytes := []byte(message)
return s.EncryptBytes(messageBytes)
}
func (s *shannonStream) EncryptBytes(messageBytes []byte) []byte {
shn_encrypt(&s.sendCipher, messageBytes, len(messageBytes))
return messageBytes
}
func (s *shannonStream) Decrypt(messageBytes []byte) []byte {
shn_decrypt(&s.recvCipher, messageBytes, len(messageBytes))
return messageBytes
}
func (s *shannonStream) WrapReader(reader io.Reader) {
s.reader = reader
}
func (s *shannonStream) WrapWriter(writer io.Writer) {
s.writer = writer
}
func (s *shannonStream) Read(p []byte) (n int, err error) {
n, err = s.reader.Read(p)
p = s.Decrypt(p)
return n, err
}
func (s *shannonStream) Write(p []byte) (n int, err error) {
p = s.EncryptBytes(p)
return s.writer.Write(p)
}
func (s *shannonStream) FinishSend() (err error) {
count := 4
mac := make([]byte, count)
shn_finish(&s.sendCipher, mac, count)
s.sendNonce += 1
nonce := make([]uint8, 4)
binary.BigEndian.PutUint32(nonce, s.sendNonce)
shn_nonce(&s.sendCipher, nonce, len(nonce))
_, err = s.writer.Write(mac)
return
}
func (s *shannonStream) finishRecv() {
count := 4
mac := make([]byte, count)
io.ReadFull(s.reader, mac)
mac2 := make([]byte, count)
shn_finish(&s.recvCipher, mac2, count)
if !bytes.Equal(mac, mac2) {
log.Println("received mac doesn't match")
}
s.recvNonce += 1
nonce := make([]uint8, 4)
binary.BigEndian.PutUint32(nonce, s.recvNonce)
shn_nonce(&s.recvCipher, nonce, len(nonce))
}
func (s *shannonStream) RecvPacket() (cmd uint8, buf []byte, err error) {
err = binary.Read(s, binary.BigEndian, &cmd)
if err != nil {
return
}
var size uint16
err = binary.Read(s, binary.BigEndian, &size)
if err != nil {
return
}
if size > 0 {
buf = make([]byte, size)
_, err = io.ReadFull(s.reader, buf)
if err != nil {
return
}
buf = s.Decrypt(buf)
}
s.finishRecv()
return
}