-
Notifications
You must be signed in to change notification settings - Fork 6
/
crypt.go
160 lines (128 loc) · 2.81 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
package aesctr
import (
"bufio"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha512"
"encoding/binary"
"errors"
"io"
)
const BUFFER_SIZE int = 16 * 1024
const IV_SIZE int = 16
const V1 byte = 0x1
const hmacSize = sha512.Size
// ErrInvalidHMAC for authentication failure
var ErrInvalidHMAC = errors.New("Invalid HMAC")
// Encrypt the stream using the given AES-CTR and SHA512-HMAC key
func Encrypt(in io.Reader, out io.Writer, keyAes, keyHmac []byte) (err error) {
iv := make([]byte, IV_SIZE)
_, err = rand.Read(iv)
if err != nil {
return err
}
AES, err := aes.NewCipher(keyAes)
if err != nil {
return err
}
ctr := cipher.NewCTR(AES, iv)
HMAC := hmac.New(sha512.New, keyHmac) // https://golang.org/pkg/crypto/hmac/#New
// Version
_, err = out.Write([]byte{V1})
if err != nil {
return
}
w := io.MultiWriter(out, HMAC)
_, err = w.Write(iv)
if err != nil {
return
}
buf := make([]byte, BUFFER_SIZE)
for {
n, err := in.Read(buf)
if err != nil && err != io.EOF {
return err
}
if n != 0 {
outBuf := make([]byte, n)
ctr.XORKeyStream(outBuf, buf[:n])
_, err = w.Write(outBuf)
if err != nil {
return err
}
}
if err == io.EOF {
break
}
}
_, err = out.Write(HMAC.Sum(nil))
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.
func Decrypt(in io.Reader, out io.Writer, keyAes, keyHmac []byte) (err error) {
// Read version (up to 0-255)
var version int8
err = binary.Read(in, binary.LittleEndian, &version)
if err != nil {
return
}
iv := make([]byte, IV_SIZE)
_, err = io.ReadFull(in, iv)
if err != nil {
return
}
AES, err := aes.NewCipher(keyAes)
if err != nil {
return
}
ctr := cipher.NewCTR(AES, iv)
h := hmac.New(sha512.New, keyHmac)
h.Write(iv)
mac := make([]byte, hmacSize)
w := out
buf := bufio.NewReaderSize(in, BUFFER_SIZE)
var limit int
var b []byte
for {
b, err = buf.Peek(BUFFER_SIZE)
if err != nil && err != io.EOF {
return
}
limit = len(b) - hmacSize
// We reached the end
if err == io.EOF {
left := buf.Buffered()
if left < hmacSize {
return errors.New("not enough left")
}
copy(mac, b[left-hmacSize:left])
if left == hmacSize {
break
}
}
h.Write(b[: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
outBuf := make([]byte, int64(limit))
_, err = buf.Read(b[:limit])
if err != nil {
return
}
ctr.XORKeyStream(outBuf, b[:limit])
_, err = w.Write(outBuf)
if err != nil {
return
}
if err == io.EOF {
break
}
}
if !hmac.Equal(mac, h.Sum(nil)) {
return ErrInvalidHMAC
}
return nil
}