forked from badfortrains/spotcontrol
/
shannon.go
175 lines (143 loc) · 3.52 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
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
// Package stringutil contains utility functions for working with strings.
package spotcontrol
// #include "./shn.h"
import "C"
import (
"bytes"
"encoding/binary"
"io"
"log"
"unsafe"
)
type shnCtx C.shn_ctx
type ShannonStream struct {
sendNonce uint32
sendCipher C.shn_ctx
recvNonce uint32
recvCipher C.shn_ctx
reader io.Reader
writer io.Writer
}
func setKey(ctx *C.shn_ctx, key []uint8) {
C.shn_key(ctx,
(*C.uchar)(unsafe.Pointer(&key[0])),
C.int(len(key)))
nonce := make([]byte, 4)
binary.BigEndian.PutUint32(nonce, 0)
C.shn_nonce(ctx,
(*C.uchar)(unsafe.Pointer(&nonce[0])),
C.int(len(nonce)))
}
func SetupStream(keys SharedKeys, conn PlainConnection) ShannonStream {
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)
C.shn_encrypt(&s.sendCipher,
(*C.uchar)(unsafe.Pointer(&messageBytes[0])),
C.int(len(messageBytes)))
return messageBytes
}
func (s *ShannonStream) EncryptBytes(messageBytes []byte) []byte {
C.shn_encrypt(&s.sendCipher,
(*C.uchar)(unsafe.Pointer(&messageBytes[0])),
C.int(len(messageBytes)))
return messageBytes
}
func (s *ShannonStream) Decrypt(messageBytes []byte) []byte {
C.shn_decrypt(&s.recvCipher,
(*C.uchar)(unsafe.Pointer(&messageBytes[0])),
C.int(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)
C.shn_finish(&s.sendCipher,
(*C.uchar)(unsafe.Pointer(&mac[0])),
C.int(count))
s.sendNonce += 1
nonce := make([]uint8, 4)
binary.BigEndian.PutUint32(nonce, s.sendNonce)
C.shn_nonce(&s.sendCipher,
(*C.uchar)(unsafe.Pointer(&nonce[0])),
C.int(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)
C.shn_finish(&s.recvCipher,
(*C.uchar)(unsafe.Pointer(&mac2[0])),
C.int(count))
if !bytes.Equal(mac, mac2) {
//log.Fatal("received mac doesn't match")
log.Println("received mac doesn't match")
}
s.recvNonce += 1
nonce := make([]uint8, 4)
binary.BigEndian.PutUint32(nonce, s.recvNonce)
C.shn_nonce(&s.recvCipher,
(*C.uchar)(unsafe.Pointer(&nonce[0])),
C.int(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
}