2016-12-06 18:31:15 -05:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/cipher"
|
|
|
|
"io"
|
2016-12-07 07:00:32 -05:00
|
|
|
|
2017-02-14 08:16:43 -05:00
|
|
|
"golang.org/x/crypto/sha3"
|
2016-12-27 18:53:29 -05:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 05:35:27 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-02-06 07:31:36 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2016-12-06 18:31:15 -05:00
|
|
|
"v2ray.com/core/common/serial"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-12-07 07:00:32 -05:00
|
|
|
errInsufficientBuffer = errors.New("Insufficient buffer.")
|
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() {
|
2017-02-06 07:31:36 -05:00
|
|
|
return nil, errors.New("Crypto:AEADAuthenticator: Invalid nonce size: ", len(iv))
|
2016-12-07 07:00:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2017-02-06 07:31:36 -05:00
|
|
|
return nil, errors.New("Crypto:AEADAuthenticator: Invalid nonce size: ", len(iv))
|
2016-12-07 07:00:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
additionalData := v.AdditionalDataGenerator.Next()
|
|
|
|
return v.AEAD.Seal(dst, iv, plainText, additionalData), nil
|
|
|
|
}
|
2016-12-06 18:31:15 -05:00
|
|
|
|
2017-02-14 08:16:43 -05:00
|
|
|
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[:])
|
|
|
|
}
|
|
|
|
|
2016-12-06 18:31:15 -05:00
|
|
|
type AuthenticationReader struct {
|
2017-02-14 04:11:26 -05:00
|
|
|
auth Authenticator
|
|
|
|
buffer *buf.Buffer
|
|
|
|
reader io.Reader
|
2017-02-14 08:16:43 -05:00
|
|
|
sizeMask Uint16Generator
|
2016-12-06 18:31:15 -05:00
|
|
|
|
2017-01-30 16:07:22 -05:00
|
|
|
chunk []byte
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
|
|
|
|
2017-01-27 07:42:31 -05:00
|
|
|
const (
|
|
|
|
readerBufferSize = 32 * 1024
|
|
|
|
)
|
|
|
|
|
2017-02-14 08:16:43 -05:00
|
|
|
func NewAuthenticationReader(auth Authenticator, reader io.Reader, sizeMask Uint16Generator) *AuthenticationReader {
|
2016-12-06 18:31:15 -05:00
|
|
|
return &AuthenticationReader{
|
2017-02-14 04:11:26 -05:00
|
|
|
auth: auth,
|
|
|
|
buffer: buf.NewLocal(readerBufferSize),
|
|
|
|
reader: reader,
|
|
|
|
sizeMask: sizeMask,
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 16:45:36 -05:00
|
|
|
func (v *AuthenticationReader) nextChunk(mask uint16) error {
|
2016-12-06 18:49:56 -05:00
|
|
|
if v.buffer.Len() < 2 {
|
|
|
|
return errInsufficientBuffer
|
|
|
|
}
|
2017-02-14 08:16:43 -05:00
|
|
|
size := int(serial.BytesToUint16(v.buffer.BytesTo(2)) ^ mask)
|
2016-12-06 18:31:15 -05:00
|
|
|
if size > v.buffer.Len()-2 {
|
|
|
|
return errInsufficientBuffer
|
|
|
|
}
|
2017-01-27 07:42:31 -05:00
|
|
|
if size > readerBufferSize-2 {
|
2017-02-06 07:31:36 -05:00
|
|
|
return errors.New("Crypto:AuthenticationReader: Size too large: ", size)
|
2017-01-27 07:42:31 -05:00
|
|
|
}
|
2016-12-07 07:00:32 -05:00
|
|
|
if size == v.auth.Overhead() {
|
2016-12-06 18:49:56 -05:00
|
|
|
return io.EOF
|
|
|
|
}
|
2016-12-07 11:32:40 -05:00
|
|
|
if size < v.auth.Overhead() {
|
2017-02-14 16:45:36 -05:00
|
|
|
return errors.New("Crypto:AuthenticationReader: invalid packet size:", size)
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2017-02-14 16:45:36 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-02-14 16:45:36 -05:00
|
|
|
func (v *AuthenticationReader) ensureChunk() error {
|
2017-02-06 08:06:41 -05:00
|
|
|
atHead := false
|
2017-02-06 07:31:36 -05:00
|
|
|
if v.buffer.IsEmpty() {
|
|
|
|
v.buffer.Clear()
|
2017-02-06 08:06:41 -05:00
|
|
|
atHead = true
|
2017-02-06 07:31:36 -05:00
|
|
|
}
|
|
|
|
|
2017-02-14 08:16:43 -05:00
|
|
|
mask := v.sizeMask.Next()
|
2016-12-06 18:31:15 -05:00
|
|
|
for {
|
2017-02-14 16:45:36 -05:00
|
|
|
err := v.nextChunk(mask)
|
2017-02-06 07:31:36 -05:00
|
|
|
if err != errInsufficientBuffer {
|
|
|
|
return err
|
2016-12-07 09:00:04 -05:00
|
|
|
}
|
2017-02-06 07:31:36 -05:00
|
|
|
|
|
|
|
leftover := v.buffer.Bytes()
|
2017-02-06 08:06:41 -05:00
|
|
|
if !atHead && len(leftover) > 0 {
|
2017-02-06 07:31:36 -05:00
|
|
|
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
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
2016-12-07 09:00:04 -05:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2017-02-14 16:45:36 -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
|
|
|
|
2017-02-14 16:45:36 -05:00
|
|
|
err := v.ensureChunk()
|
2016-12-07 09:00:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
2016-12-07 09:00:04 -05:00
|
|
|
|
2017-02-14 16:45:36 -05:00
|
|
|
return v.copyChunk(b), nil
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type AuthenticationWriter struct {
|
2017-02-14 04:11:26 -05:00
|
|
|
auth Authenticator
|
|
|
|
buffer []byte
|
|
|
|
writer io.Writer
|
2017-02-14 08:16:43 -05:00
|
|
|
sizeMask Uint16Generator
|
2016-12-06 18:31:15 -05:00
|
|
|
}
|
|
|
|
|
2017-02-14 08:16:43 -05:00
|
|
|
func NewAuthenticationWriter(auth Authenticator, writer io.Writer, sizeMask Uint16Generator) *AuthenticationWriter {
|
2016-12-06 18:31:15 -05:00
|
|
|
return &AuthenticationWriter{
|
2017-02-14 04:11:26 -05:00
|
|
|
auth: auth,
|
|
|
|
buffer: make([]byte, 32*1024),
|
|
|
|
writer: writer,
|
|
|
|
sizeMask: sizeMask,
|
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
|
|
|
|
2017-02-14 08:16:43 -05:00
|
|
|
size := uint16(len(cipherChunk)) ^ v.sizeMask.Next()
|
2017-02-14 04:11:26 -05:00
|
|
|
serial.Uint16ToBytes(size, 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
|
|
|
|
}
|