-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
264 lines (222 loc) · 5.37 KB
/
auth.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package crypto
import (
"crypto/cipher"
"io"
"golang.org/x/crypto/sha3"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/serial"
)
var (
errInsufficientBuffer = newError("insufficient buffer")
)
type BytesGenerator interface {
Next() []byte
}
type NoOpBytesGenerator struct {
buffer [1]byte
}
func (v NoOpBytesGenerator) Next() []byte {
return v.buffer[:0]
}
type StaticBytesGenerator struct {
Content []byte
}
func (v StaticBytesGenerator) Next() []byte {
return v.Content
}
type Authenticator interface {
NonceSize() int
Overhead() int
Open(dst, cipherText []byte) ([]byte, error)
Seal(dst, plainText []byte) ([]byte, error)
}
type AEADAuthenticator struct {
cipher.AEAD
NonceGenerator BytesGenerator
AdditionalDataGenerator BytesGenerator
}
func (v *AEADAuthenticator) Open(dst, cipherText []byte) ([]byte, error) {
iv := v.NonceGenerator.Next()
if len(iv) != v.AEAD.NonceSize() {
return nil, newError("invalid AEAD nonce size: ", len(iv))
}
additionalData := v.AdditionalDataGenerator.Next()
return v.AEAD.Open(dst, iv, cipherText, additionalData)
}
func (v *AEADAuthenticator) Seal(dst, plainText []byte) ([]byte, error) {
iv := v.NonceGenerator.Next()
if len(iv) != v.AEAD.NonceSize() {
return nil, newError("invalid AEAD nonce size: ", len(iv))
}
additionalData := v.AdditionalDataGenerator.Next()
return v.AEAD.Seal(dst, iv, plainText, additionalData), nil
}
type Uint16Generator interface {
Next() uint16
}
type StaticUint16Generator uint16
func (g StaticUint16Generator) Next() uint16 {
return uint16(g)
}
type ShakeUint16Generator struct {
shake sha3.ShakeHash
buffer [2]byte
}
func NewShakeUint16Generator(nonce []byte) *ShakeUint16Generator {
shake := sha3.NewShake128()
shake.Write(nonce)
return &ShakeUint16Generator{
shake: shake,
}
}
func (g *ShakeUint16Generator) Next() uint16 {
g.shake.Read(g.buffer[:])
return serial.BytesToUint16(g.buffer[:])
}
type AuthenticationReader struct {
auth Authenticator
buffer *buf.Buffer
reader io.Reader
sizeMask Uint16Generator
chunk []byte
}
const (
readerBufferSize = 32 * 1024
)
func NewAuthenticationReader(auth Authenticator, reader io.Reader, sizeMask Uint16Generator) *AuthenticationReader {
return &AuthenticationReader{
auth: auth,
buffer: buf.NewLocal(readerBufferSize),
reader: reader,
sizeMask: sizeMask,
}
}
func (v *AuthenticationReader) nextChunk(mask uint16) error {
if v.buffer.Len() < 2 {
return errInsufficientBuffer
}
size := int(serial.BytesToUint16(v.buffer.BytesTo(2)) ^ mask)
if size > v.buffer.Len()-2 {
return errInsufficientBuffer
}
if size > readerBufferSize-2 {
return newError("size too large: ", size)
}
if size == v.auth.Overhead() {
return io.EOF
}
if size < v.auth.Overhead() {
return newError("invalid packet size: ", size)
}
cipherChunk := v.buffer.BytesRange(2, size+2)
plainChunk, err := v.auth.Open(cipherChunk[:0], cipherChunk)
if err != nil {
return err
}
v.chunk = plainChunk
v.buffer.SliceFrom(size + 2)
return nil
}
func (v *AuthenticationReader) copyChunk(b []byte) int {
if len(v.chunk) == 0 {
return 0
}
nBytes := copy(b, v.chunk)
if nBytes == len(v.chunk) {
v.chunk = nil
} else {
v.chunk = v.chunk[nBytes:]
}
return nBytes
}
func (v *AuthenticationReader) ensureChunk() error {
atHead := false
if v.buffer.IsEmpty() {
v.buffer.Clear()
atHead = true
}
mask := v.sizeMask.Next()
for {
err := v.nextChunk(mask)
if err != errInsufficientBuffer {
return err
}
leftover := v.buffer.Bytes()
if !atHead && len(leftover) > 0 {
common.Must(v.buffer.Reset(func(b []byte) (int, error) {
return copy(b, leftover), nil
}))
}
if err := v.buffer.AppendSupplier(buf.ReadFrom(v.reader)); err != nil {
return err
}
}
}
func (v *AuthenticationReader) Read(b []byte) (int, error) {
if len(v.chunk) > 0 {
nBytes := v.copyChunk(b)
return nBytes, nil
}
err := v.ensureChunk()
if err != nil {
return 0, err
}
return v.copyChunk(b), nil
}
func (r *AuthenticationReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
err := r.ensureChunk()
if err != nil {
return nil, err
}
mb := buf.NewMultiBuffer()
for len(r.chunk) > 0 {
b := buf.New()
nBytes, _ := b.Write(r.chunk)
mb.Append(b)
r.chunk = r.chunk[nBytes:]
}
r.chunk = nil
return mb, nil
}
type AuthenticationWriter struct {
auth Authenticator
buffer []byte
writer io.Writer
sizeMask Uint16Generator
}
func NewAuthenticationWriter(auth Authenticator, writer io.Writer, sizeMask Uint16Generator) *AuthenticationWriter {
return &AuthenticationWriter{
auth: auth,
buffer: make([]byte, 32*1024),
writer: writer,
sizeMask: sizeMask,
}
}
func (w *AuthenticationWriter) Write(b []byte) (int, error) {
cipherChunk, err := w.auth.Seal(w.buffer[2:2], b)
if err != nil {
return 0, err
}
size := uint16(len(cipherChunk)) ^ w.sizeMask.Next()
serial.Uint16ToBytes(size, w.buffer[:0])
_, err = w.writer.Write(w.buffer[:2+len(cipherChunk)])
return len(b), err
}
func (w *AuthenticationWriter) WriteMultiBuffer(mb buf.MultiBuffer) (int, error) {
defer mb.Release()
const StartIndex = 17 * 1024
var totalBytes int
for {
payloadLen, _ := mb.Read(w.buffer[StartIndex:])
nBytes, err := w.Write(w.buffer[StartIndex : StartIndex+payloadLen])
totalBytes += nBytes
if err != nil {
return totalBytes, err
}
if mb.IsEmpty() {
break
}
}
return totalBytes, nil
}