1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 02:26:49 -05:00
v2fly/common/crypto/auth.go

189 lines
3.9 KiB
Go
Raw Normal View History

2016-12-06 18:31:15 -05:00
package crypto
import (
"crypto/cipher"
"errors"
"io"
2016-12-07 07:00:32 -05:00
2016-12-06 18:31:15 -05:00
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/serial"
)
var (
ErrAuthenticationFailed = errors.New("Authentication failed.")
2016-12-07 07:00:32 -05:00
errInsufficientBuffer = errors.New("Insufficient buffer.")
errInvalidNonce = errors.New("Invalid nonce.")
2016-12-06 18:31:15 -05:00
)
2016-12-07 07:00:32 -05:00
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, errInvalidNonce
}
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, errInvalidNonce
}
additionalData := v.AdditionalDataGenerator.Next()
return v.AEAD.Seal(dst, iv, plainText, additionalData), nil
}
2016-12-06 18:31:15 -05:00
type AuthenticationReader struct {
2016-12-07 07:00:32 -05:00
auth Authenticator
buffer *alloc.Buffer
reader io.Reader
2016-12-06 18:31:15 -05:00
2016-12-07 07:00:32 -05:00
chunk []byte
aggressive bool
2016-12-06 18:31:15 -05:00
}
2016-12-07 07:00:32 -05:00
func NewAuthenticationReader(auth Authenticator, reader io.Reader, aggressive bool) *AuthenticationReader {
2016-12-06 18:31:15 -05:00
return &AuthenticationReader{
2016-12-07 07:00:32 -05:00
auth: auth,
buffer: alloc.NewLocalBuffer(32 * 1024),
reader: reader,
aggressive: aggressive,
2016-12-06 18:31:15 -05:00
}
}
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
}
2016-12-07 07:00:32 -05:00
if size == v.auth.Overhead() {
return io.EOF
}
2016-12-07 11:32:40 -05:00
if size < v.auth.Overhead() {
return errors.New("AuthenticationReader: invalid packet size.")
}
2016-12-06 18:31:15 -05:00
cipherChunk := v.buffer.BytesRange(2, size+2)
2016-12-07 16:52:56 -05:00
plainChunk, err := v.auth.Open(cipherChunk[:0], cipherChunk)
2016-12-06 18:31:15 -05:00
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 {
2016-12-07 07:00:32 -05:00
if len(v.chunk) == 0 {
return 0
}
2016-12-06 18:31:15 -05:00
nBytes := copy(b, v.chunk)
if nBytes == len(v.chunk) {
v.chunk = nil
} else {
v.chunk = v.chunk[nBytes:]
}
return nBytes
}
2016-12-07 09:00:04 -05:00
func (v *AuthenticationReader) EnsureChunk() error {
2016-12-06 18:31:15 -05:00
for {
2016-12-07 07:00:32 -05:00
err := v.NextChunk()
2016-12-07 09:00:04 -05:00
if err == nil {
return nil
}
2016-12-07 07:00:32 -05:00
if err == errInsufficientBuffer {
2016-12-07 16:52:56 -05:00
if v.buffer.IsEmpty() {
v.buffer.Clear()
} else {
2016-12-07 09:00:04 -05:00
leftover := v.buffer.Bytes()
v.buffer.SetBytesFunc(func(b []byte) int {
return copy(b, leftover)
})
2016-12-07 07:00:32 -05:00
}
_, err = v.buffer.FillFrom(v.reader)
2016-12-06 18:31:15 -05:00
}
2016-12-07 09:00:04 -05:00
return err
}
}
2016-12-07 07:00:32 -05:00
2016-12-07 09:00:04 -05:00
func (v *AuthenticationReader) Read(b []byte) (int, error) {
if len(v.chunk) > 0 {
2016-12-07 07:00:32 -05:00
nBytes := v.CopyChunk(b)
2016-12-07 09:00:04 -05:00
return nBytes, nil
}
2016-12-07 07:00:32 -05:00
2016-12-07 09:00:04 -05:00
err := v.EnsureChunk()
if err != nil {
return 0, err
2016-12-06 18:31:15 -05:00
}
2016-12-07 09:00:04 -05:00
nBytes := v.CopyChunk(b)
return nBytes, nil
2016-12-06 18:31:15 -05:00
}
type AuthenticationWriter struct {
2016-12-07 07:00:32 -05:00
auth Authenticator
2016-12-06 18:31:15 -05:00
buffer []byte
writer io.Writer
ivGen BytesGenerator
extraGen BytesGenerator
}
2016-12-07 07:00:32 -05:00
func NewAuthenticationWriter(auth Authenticator, writer io.Writer) *AuthenticationWriter {
2016-12-06 18:31:15 -05:00
return &AuthenticationWriter{
2016-12-07 07:00:32 -05:00
auth: auth,
buffer: make([]byte, 32*1024),
writer: writer,
2016-12-06 18:31:15 -05:00
}
}
func (v *AuthenticationWriter) Write(b []byte) (int, error) {
2016-12-07 16:52:56 -05:00
cipherChunk, err := v.auth.Seal(v.buffer[2:2], b)
2016-12-07 07:00:32 -05:00
if err != nil {
return 0, err
}
2016-12-07 16:52:56 -05:00
2016-12-07 11:32:40 -05:00
serial.Uint16ToBytes(uint16(len(cipherChunk)), v.buffer[:0])
2016-12-07 07:00:32 -05:00
_, err = v.writer.Write(v.buffer[:2+len(cipherChunk)])
2016-12-06 18:31:15 -05:00
return len(b), err
}