1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-10 09:50:43 +00:00
v2fly/common/crypto/auth.go

186 lines
3.8 KiB
Go
Raw Normal View History

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