1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/common/crypto/auth.go

121 lines
2.5 KiB
Go
Raw Normal View History

2016-12-06 18:31:15 -05:00
package crypto
import (
"crypto/cipher"
"errors"
"io"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/serial"
)
var (
ErrAuthenticationFailed = errors.New("Authentication failed.")
errInsufficientBuffer = errors.New("Insufficient buffer.")
)
type BytesGenerator func() []byte
type AuthenticationReader struct {
aead cipher.AEAD
buffer *alloc.Buffer
reader io.Reader
ivGen BytesGenerator
extraGen BytesGenerator
chunk []byte
}
func NewAuthenticationReader(aead cipher.AEAD, reader io.Reader, ivGen BytesGenerator, extraGen BytesGenerator) *AuthenticationReader {
return &AuthenticationReader{
aead: aead,
buffer: alloc.NewLocalBuffer(32 * 1024),
reader: reader,
ivGen: ivGen,
extraGen: extraGen,
}
}
func (v *AuthenticationReader) NextChunk() error {
if v.buffer.Len() < 2 {
return errInsufficientBuffer
}
2016-12-06 18:31:15 -05:00
size := int(serial.BytesToUint16(v.buffer.BytesTo(2)))
if size > v.buffer.Len()-2 {
return errInsufficientBuffer
}
if size == v.aead.Overhead() {
return io.EOF
}
2016-12-06 18:31:15 -05:00
cipherChunk := v.buffer.BytesRange(2, size+2)
plainChunk, err := v.aead.Open(cipherChunk, v.ivGen(), cipherChunk, v.extraGen())
if err != nil {
return err
}
v.chunk = plainChunk
2016-12-06 18:52:36 -05:00
v.buffer.SliceFrom(size + 2)
2016-12-06 18:31:15 -05:00
return nil
}
func (v *AuthenticationReader) CopyChunk(b []byte) int {
nBytes := copy(b, v.chunk)
if nBytes == len(v.chunk) {
v.chunk = nil
} else {
v.chunk = v.chunk[nBytes:]
}
return nBytes
}
func (v *AuthenticationReader) Read(b []byte) (int, error) {
if len(v.chunk) > 0 {
nBytes := v.CopyChunk(b)
return nBytes, nil
}
err := v.NextChunk()
if err == errInsufficientBuffer {
_, err = v.buffer.FillFrom(v.reader)
}
if err != nil {
return 0, err
2016-12-06 18:31:15 -05:00
}
totalBytes := 0
for {
totalBytes += v.CopyChunk(b)
if len(b) == 0 {
break
}
if err := v.NextChunk(); err != nil {
break
}
}
return totalBytes, nil
}
type AuthenticationWriter struct {
aead cipher.AEAD
buffer []byte
writer io.Writer
ivGen BytesGenerator
extraGen BytesGenerator
}
func NewAuthenticationWriter(aead cipher.AEAD, writer io.Writer, ivGen BytesGenerator, extraGen BytesGenerator) *AuthenticationWriter {
return &AuthenticationWriter{
aead: aead,
buffer: make([]byte, 32*1024),
writer: writer,
ivGen: ivGen,
extraGen: extraGen,
}
}
func (v *AuthenticationWriter) Write(b []byte) (int, error) {
cipherChunk := v.aead.Seal(v.buffer[2:], v.ivGen(), b, v.extraGen())
serial.Uint16ToBytes(uint16(len(cipherChunk)), b[:0])
_, err := v.writer.Write(v.buffer[:2+len(cipherChunk)])
return len(b), err
}