-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
196 lines (172 loc) · 4.19 KB
/
stream.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
package encdec
import (
"bytes"
"crypto/cipher"
"errors"
"io"
"golang.org/x/crypto/chacha20poly1305"
)
// Writer writes to underlying writer encrypting the data.
type Writer struct {
aead cipher.AEAD
chunkSize int64
dst io.Writer
nonce [chacha20poly1305.NonceSize]byte
buff bytes.Buffer
err error
}
// NewWriter creates a new Writer using a 256-bit key.
func NewWriter(key []byte, dst io.Writer, params *Params) (*Writer, error) {
if params == nil {
return nil, ErrNilParams
}
err := params.checkFormatted()
if err != nil {
return nil, err
}
aead, err := chacha20poly1305.New(key)
if err != nil {
return nil, err
}
w := &Writer{
aead: aead,
dst: dst,
chunkSize: params.ChunkSize,
}
w.buff.Grow(int(w.chunkSize + chacha20poly1305.Overhead))
return w, nil
}
func (w *Writer) flush() error {
ciphertext := w.aead.Seal(w.buff.Bytes()[:0], w.nonce[:], w.buff.Bytes(), nil)
_, err := w.dst.Write(ciphertext)
if err != nil {
return err
}
w.buff.Reset()
err = incNonce(w.nonce[:])
return err
}
// Write writes len(p) bytes from p to the buffer.
// If the buffer is complete it will encrypt the data and
// write to the underlying writer with the AEAD tag appended to it.
// It returns the number of bytes written to the buffer and an error,
// if any.
func (w *Writer) Write(p []byte) (int, error) {
if w.err != nil {
return 0, w.err
}
total := len(p)
for len(p) > 0 {
size := min(int(w.chunkSize)-w.buff.Len(), len(p))
n, _ := w.buff.Write(p[:size])
p = p[n:]
if w.buff.Len() == int(w.chunkSize) {
err := w.flush()
if err != nil {
w.err = err
return 0, w.err
}
}
}
return total, nil
}
// Close encrypt and write any remaning data in the buffer plus the AEAD tag,
// to the underlying writer. Close returns an error if it has already been called.
func (w *Writer) Close() error {
if w.err != nil {
return w.err
}
w.err = w.flush()
if w.err != nil {
return w.err
}
w.err = errors.New("operation on closed writer")
return nil
}
// Reader reads encrypted data from the underlying reader.
type Reader struct {
aead cipher.AEAD
chunkSize int
src io.Reader
nonce [chacha20poly1305.NonceSize]byte
buff bytes.Buffer
lastChunk bool
err error
}
// NewReader creates a new Reader using a 256-bit key.
func NewReader(key []byte, src io.Reader, params *Params) (*Reader, error) {
if params == nil {
return nil, ErrNilParams
}
err := params.checkFormatted()
if err != nil {
return nil, err
}
aead, err := chacha20poly1305.New(key)
if err != nil {
return nil, err
}
r := &Reader{
aead: aead,
src: src,
chunkSize: int(params.ChunkSize),
}
r.buff.Grow(r.chunkSize + chacha20poly1305.Overhead)
return r, nil
}
// readChunk reads the next chunk from src and decrypt it.
// Returns true if it is the last chunk.
func (r *Reader) readChunk() (bool, error) {
var last bool
r.buff.Reset()
n, err := io.CopyN(&r.buff, r.src, int64(r.chunkSize)+chacha20poly1305.Overhead)
if err != nil {
if err != io.EOF {
return false, err
}
last = true
}
if n < (int64(r.chunkSize) + chacha20poly1305.Overhead) {
last = true
}
plaintext, err := r.aead.Open(r.buff.Bytes()[:0], r.nonce[:], r.buff.Bytes(), nil)
if err != nil {
return false, err
}
r.buff.Truncate(len(plaintext))
err = incNonce(r.nonce[:])
if err != nil {
return false, err
}
return last, nil
}
// Read up to len(p) bytes, decrypting they and storing them in p.
// It returns the number of bytes read and any error encountered.
// At the end of file, Read returns 0 and io.EOF.
func (r *Reader) Read(p []byte) (int, error) {
if r.err != nil {
return 0, r.err
}
var total int
for len(p) > 0 {
if r.buff.Len() == 0 {
if r.lastChunk {
r.err = io.EOF
if total == 0 {
return 0, r.err
}
return total, nil
}
last, err := r.readChunk()
if err != nil {
r.err = err
return 0, r.err
}
r.lastChunk = last
}
n, _ := r.buff.Read(p)
total += n
p = p[n:]
}
return total, nil
}