- Notifications
You must be signed in to change notification settings - Fork55
/
Copy pathwriter-v1.go
223 lines (208 loc) · 5.92 KB
/
writer-v1.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
// Copyright (C) 2017 Minio Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sio
import"io"
typedecWriterV10struct {
authDecV10
dst io.Writer
bufferpackageV10
offsetint
closeErrerror
}
// decryptWriterV10 returns an io.WriteCloser wrapping the given io.Writer.
// The returned io.WriteCloser decrypts everything written to it using DARE 1.0
// and writes all decrypted plaintext to the wrapped io.Writer.
//
// The io.WriteCloser must be closed to finalize the decryption successfully.
funcdecryptWriterV10(dst io.Writer,config*Config) (*decWriterV10,error) {
ad,err:=newAuthDecV10(config)
iferr!=nil {
returnnil,err
}
buf:=packageBufferPool.Get().([]byte)[:maxPackageSize]
fori:=rangebuf {
buf[i]=0
}
return&decWriterV10{
authDecV10:ad,
dst:dst,
buffer:buf,
},nil
}
func (w*decWriterV10)Write(p []byte) (nint,errerror) {
ifw.offset>0&&w.offset<headerSize {// buffer the header -> special code b/c we don't know when to decrypt without header
remaining:=headerSize-w.offset
iflen(p)<remaining {
n=copy(w.buffer[w.offset:],p)
w.offset+=n
return
}
n=copy(w.buffer[w.offset:],p[:remaining])
p=p[remaining:]
w.offset+=n
}
ifw.offset>=headerSize {// buffer the ciphertext and tag -> here we know when to decrypt
remaining:=w.buffer.Length()-w.offset
iflen(p)<remaining {
nn:=copy(w.buffer[w.offset:],p)
w.offset+=nn
returnn+nn,err
}
n+=copy(w.buffer[w.offset:],p[:remaining])
iferr=w.Open(w.buffer.Payload(),w.buffer[:w.buffer.Length()]);err!=nil {
returnn,err
}
iferr=flush(w.dst,w.buffer.Payload());err!=nil {// write to underlying io.Writer
returnn,err
}
p=p[remaining:]
w.offset=0
}
forlen(p)>headerSize {
packageLen:=headerSize+tagSize+headerV10(p).Len()
iflen(p)<packageLen {// p contains not the full package -> cannot decrypt
w.offset=copy(w.buffer[:],p)
n+=w.offset
returnn,err
}
iferr=w.Open(w.buffer[headerSize:packageLen-tagSize],p[:packageLen]);err!=nil {
returnn,err
}
iferr=flush(w.dst,w.buffer[headerSize:packageLen-tagSize]);err!=nil {// write to underlying io.Writer
returnn,err
}
p=p[packageLen:]
n+=packageLen
}
iflen(p)>0 {
w.offset=copy(w.buffer[:],p)
n+=w.offset
}
returnn,nil
}
func (w*decWriterV10)Close() (errerror) {
ifw.buffer==nil {
returnw.closeErr
}
deferfunc() {
w.closeErr=err
recyclePackageBufferPool(w.buffer)
w.buffer=nil
}()
ifw.offset>0 {
ifw.offset<=headerSize+tagSize {
returnerrInvalidPayloadSize// the payload is always > 0
}
header:=headerV10(w.buffer[:headerSize])
ifw.offset<headerSize+header.Len()+tagSize {
returnerrInvalidPayloadSize// there is less data than specified by the header
}
iferr=w.Open(w.buffer.Payload(),w.buffer[:w.buffer.Length()]);err!=nil {
returnerr
}
iferr=flush(w.dst,w.buffer.Payload());err!=nil {// write to underlying io.Writer
returnerr
}
}
ifdst,ok:=w.dst.(io.Closer);ok {
returndst.Close()
}
returnnil
}
typeencWriterV10struct {
authEncV10
dst io.Writer
bufferpackageV10
offsetint
payloadSizeint
closeErrerror
}
// encryptWriterV10 returns an io.WriteCloser wrapping the given io.Writer.
// The returned io.WriteCloser encrypts everything written to it using DARE 1.0
// and writes all encrypted ciphertext as well as the package header and tag
// to the wrapped io.Writer.
//
// The io.WriteCloser must be closed to finalize the encryption successfully.
funcencryptWriterV10(dst io.Writer,config*Config) (*encWriterV10,error) {
ae,err:=newAuthEncV10(config)
iferr!=nil {
returnnil,err
}
return&encWriterV10{
authEncV10:ae,
dst:dst,
buffer:packageBufferPool.Get().([]byte)[:maxPackageSize],
payloadSize:config.PayloadSize,
},nil
}
func (w*encWriterV10)Write(p []byte) (nint,errerror) {
ifw.offset>0 {// buffer the plaintext
remaining:=w.payloadSize-w.offset
iflen(p)<remaining {
n=copy(w.buffer[headerSize+w.offset:],p)
w.offset+=n
return
}
n=copy(w.buffer[headerSize+w.offset:],p[:remaining])
w.Seal(w.buffer,w.buffer[headerSize:headerSize+w.payloadSize])
iferr=flush(w.dst,w.buffer[:w.buffer.Length()]);err!=nil {// write to underlying io.Writer
returnn,err
}
p=p[remaining:]
w.offset=0
}
forlen(p)>=w.payloadSize {
w.Seal(w.buffer[:],p[:w.payloadSize])
iferr=flush(w.dst,w.buffer[:w.buffer.Length()]);err!=nil {// write to underlying io.Writer
returnn,err
}
p=p[w.payloadSize:]
n+=w.payloadSize
}
iflen(p)>0 {
w.offset=copy(w.buffer[headerSize:],p)
n+=w.offset
}
return
}
func (w*encWriterV10)Close() (errerror) {
ifw.buffer==nil {
returnw.closeErr
}
deferfunc() {
w.closeErr=err
recyclePackageBufferPool(w.buffer)
w.buffer=nil
}()
ifw.offset>0 {
w.Seal(w.buffer[:],w.buffer[headerSize:headerSize+w.offset])
iferr:=flush(w.dst,w.buffer[:w.buffer.Length()]);err!=nil {// write to underlying io.Writer
returnerr
}
}
ifdst,ok:=w.dst.(io.Closer);ok {
returndst.Close()
}
returnnil
}
funcflush(w io.Writer,p []byte)error {
n,err:=w.Write(p)
iferr!=nil {
returnerr
}
ifn!=len(p) {// not necessary if the w follows the io.Writer doc *precisely*
returnio.ErrShortWrite
}
returnnil
}