-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypt.go
234 lines (205 loc) · 5.33 KB
/
crypt.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
//go:build js && wasm
package aesctr
import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha512"
"errors"
"fmt"
"syscall/js"
)
const BufferSize int = 16 * 1024
const IvSize int = 16
const V1 byte = 0x1
const hmacSize = sha512.Size
var global = js.Global()
var uint8Array = global.Get("Uint8Array")
var arrayBuffer = global.Get("ArrayBuffer")
var (
// ErrInvalidHMAC for authentication failure
ErrInvalidHMAC = errors.New("invalid HMAC")
ErrSystem = func(msg string) error {
return fmt.Errorf("system error %v", msg)
}
ErrInvalidParameters = errors.New("invalid parameters")
)
// Encrypt the stream using the given AES-CTR and SHA512-HMAC key
// @param in ArrayBuffer
// @param outFun function(chunk)
func Encrypt(in js.Value, outFun js.Value, keyAES, keyHMAC js.Value) (err error) {
if in.Get("byteLength").Int() == 0 {
return ErrInvalidParameters
}
iv := make([]byte, IvSize)
_, err = rand.Read(iv)
if err != nil {
return ErrSystem(err.Error())
}
keyAESBytes := make([]byte, keyAES.Get("byteLength").Int())
if js.CopyBytesToGo(keyAESBytes, keyAES) == 0 {
return ErrInvalidParameters
}
keyHMACBytes := make([]byte, keyHMAC.Get("byteLength").Int())
if js.CopyBytesToGo(keyHMACBytes, keyHMAC) == 0 {
return ErrInvalidParameters
}
AES, err := aes.NewCipher(keyAESBytes)
if err != nil {
return ErrSystem(err.Error())
}
ctr := cipher.NewCTR(AES, iv)
HMAC := hmac.New(sha512.New, keyHMACBytes)
ch1 := uint8Array.New(1)
if js.CopyBytesToJS(ch1, []byte{V1}) == 0 {
return ErrSystem("version convert")
}
outFun.Invoke(ch1)
_, err = HMAC.Write(iv)
if err != nil {
return
}
ch1 = uint8Array.New(IvSize)
if js.CopyBytesToJS(ch1, iv) == 0 {
return ErrSystem("iv convert")
}
outFun.Invoke(ch1)
offset := 0
buf := uint8Array.New(0)
for {
buf = uint8Array.New(in.Call("slice", offset, offset+BufferSize))
bl := buf.Get("byteLength").Int()
if bl == 0 {
break
}
if bl != 0 {
goBytes := make([]byte, bl)
if js.CopyBytesToGo(goBytes, buf) == 0 {
return ErrSystem("read bytes convert")
}
buf = uint8Array.New(0)
outBuf := make([]byte, bl)
ctr.XORKeyStream(outBuf, goBytes[:bl])
goBytes = nil
_, err = HMAC.Write(outBuf)
if err != nil {
return err
}
jsBytes := uint8Array.New(len(outBuf))
if ii := js.CopyBytesToJS(jsBytes, outBuf); ii == 0 {
return ErrSystem("encrypted bytes convert")
}
outBuf = nil
outFun.Invoke(jsBytes)
jsBytes = uint8Array.New(0)
offset += bl
}
}
sum := HMAC.Sum(nil)
ch1 = uint8Array.New(hmacSize)
if js.CopyBytesToJS(ch1, sum) == 0 {
return ErrSystem("hmac bytes convert")
}
outFun.Invoke(ch1)
return err
}
// Decrypt the stream and verify HMAC using the given AES-CTR and SHA512-HMAC key
// Do not trust the out io.Writer contents until the function returns the result
// of validating the ending HMAC hash.
// @param in ArrayBuffer
// @param outFun function(chunk)
func Decrypt(in js.Value, outFun js.Value, keyAES, keyHMAC js.Value) (err error) {
keyAESBytes := make([]byte, keyAES.Get("byteLength").Int())
ii := js.CopyBytesToGo(keyAESBytes, keyAES)
if ii == 0 {
return ErrInvalidParameters
}
keyHMACBytes := make([]byte, keyHMAC.Get("byteLength").Int())
ii = js.CopyBytesToGo(keyHMACBytes, keyHMAC)
if ii == 0 {
return ErrInvalidParameters
}
offset := 0
// Read version (up to 0-255)
var version int8
jsVersion := uint8Array.New(in.Call("slice", offset, 1))
goVersion := make([]byte, 1)
if js.CopyBytesToGo(goVersion, jsVersion) == 0 {
return ErrSystem("1")
}
offset += 1
version = int8(goVersion[0])
if version != int8(V1) {
return ErrSystem("version convert")
}
iv := make([]byte, IvSize)
jsIv := uint8Array.New(in.Call("slice", offset, offset+IvSize))
if jsIv.Get("byteLength").Int() == 0 {
return ErrSystem("iv size calculate")
}
if js.CopyBytesToGo(iv, jsIv) == 0 {
return ErrSystem("iv convert")
}
offset += IvSize
AES, err := aes.NewCipher(keyAESBytes)
if err != nil {
return
}
ctr := cipher.NewCTR(AES, iv)
h := hmac.New(sha512.New, keyHMACBytes)
h.Write(iv)
mac := make([]byte, hmacSize)
b := uint8Array.New(0)
var limit int
for {
b = uint8Array.New(in.Call("slice", offset, offset+BufferSize))
bl := b.Get("byteLength").Int()
if bl == 0 {
break
}
gb := make([]byte, bl)
if js.CopyBytesToGo(gb, b) == 0 {
return ErrSystem("read bytes convert")
}
limit = bl - hmacSize
// We reached the end
if limit == 0 {
if bl < hmacSize {
return errors.New("not enough left")
}
copy(mac, gb[bl-hmacSize:bl])
if bl == hmacSize {
break
}
}
h.Write(gb[:limit])
// We always leave at least hmacSize bytes left in the buffer
// That way, our next Peek() might be EOF, but we will still have enough
b = uint8Array.New(in.Call("slice", offset, offset+len(gb[:limit])))
bl = b.Get("byteLength").Int()
if bl == 0 {
break
}
gb = make([]byte, bl)
//fmt.Println(b)
if js.CopyBytesToGo(gb, b) == 0 {
return ErrSystem("read bytes convert")
}
outBuf := make([]byte, int64(limit))
ctr.XORKeyStream(outBuf, gb[:limit])
gb = nil
jsBytes := uint8Array.New(len(outBuf))
if js.CopyBytesToJS(jsBytes, outBuf) == 0 {
return ErrSystem("decrypted bytes convert")
}
outBuf = nil
outFun.Invoke(jsBytes)
jsBytes = uint8Array.New(0)
offset += bl
}
if !hmac.Equal(mac, h.Sum(nil)) {
return ErrInvalidHMAC
}
return nil
}