2016-06-14 17:25:06 -04:00
|
|
|
package kcp
|
|
|
|
|
2016-06-17 10:51:41 -04:00
|
|
|
import (
|
2016-12-08 10:27:41 -05:00
|
|
|
"crypto/cipher"
|
2017-02-06 07:49:03 -05:00
|
|
|
"errors"
|
2016-06-17 10:51:41 -04:00
|
|
|
"hash/fnv"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/serial"
|
2016-12-08 10:27:41 -05:00
|
|
|
)
|
|
|
|
|
2016-12-08 10:32:53 -05:00
|
|
|
// SimpleAuthenticator is a legacy AEAD used for KCP encryption.
|
2016-06-17 10:51:41 -04:00
|
|
|
type SimpleAuthenticator struct{}
|
|
|
|
|
2016-12-08 10:32:53 -05:00
|
|
|
// NewSimpleAuthenticator creates a new SimpleAuthenticator
|
2016-12-08 10:27:41 -05:00
|
|
|
func NewSimpleAuthenticator() cipher.AEAD {
|
2016-06-17 10:51:41 -04:00
|
|
|
return &SimpleAuthenticator{}
|
2016-06-14 17:25:06 -04:00
|
|
|
}
|
|
|
|
|
2016-12-08 10:32:53 -05:00
|
|
|
// NonceSize implements cipher.AEAD.NonceSize().
|
2016-12-08 10:27:41 -05:00
|
|
|
func (v *SimpleAuthenticator) NonceSize() int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-12-08 10:32:53 -05:00
|
|
|
// Overhead implements cipher.AEAD.NonceSize().
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *SimpleAuthenticator) Overhead() int {
|
2016-06-17 10:51:41 -04:00
|
|
|
return 6
|
2016-06-14 17:25:06 -04:00
|
|
|
}
|
|
|
|
|
2016-12-08 10:32:53 -05:00
|
|
|
// Seal implements cipher.AEAD.Seal().
|
2016-12-08 10:27:41 -05:00
|
|
|
func (v *SimpleAuthenticator) Seal(dst, nonce, plain, extra []byte) []byte {
|
|
|
|
dst = append(dst, 0, 0, 0, 0)
|
|
|
|
dst = serial.Uint16ToBytes(uint16(len(plain)), dst)
|
|
|
|
dst = append(dst, plain...)
|
|
|
|
|
2016-06-17 10:51:41 -04:00
|
|
|
fnvHash := fnv.New32a()
|
2016-12-08 10:27:41 -05:00
|
|
|
fnvHash.Write(dst[4:])
|
|
|
|
fnvHash.Sum(dst[:0])
|
2016-06-17 10:51:41 -04:00
|
|
|
|
2016-12-08 10:27:41 -05:00
|
|
|
len := len(dst)
|
2016-06-17 20:13:35 -04:00
|
|
|
xtra := 4 - len%4
|
2016-12-08 10:27:41 -05:00
|
|
|
if xtra != 4 {
|
|
|
|
dst = append(dst, make([]byte, xtra)...)
|
2016-06-17 20:13:35 -04:00
|
|
|
}
|
2016-12-08 10:27:41 -05:00
|
|
|
xorfwd(dst)
|
|
|
|
if xtra != 4 {
|
|
|
|
dst = dst[:len]
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
2016-12-08 10:27:41 -05:00
|
|
|
return dst
|
2016-06-14 17:25:06 -04:00
|
|
|
}
|
|
|
|
|
2016-12-08 10:32:53 -05:00
|
|
|
// Open implements cipher.AEAD.Open().
|
2016-12-08 10:27:41 -05:00
|
|
|
func (v *SimpleAuthenticator) Open(dst, nonce, cipherText, extra []byte) ([]byte, error) {
|
|
|
|
dst = append(dst, cipherText...)
|
|
|
|
dstLen := len(dst)
|
|
|
|
xtra := 4 - dstLen%4
|
|
|
|
if xtra != 4 {
|
|
|
|
dst = append(dst, make([]byte, xtra)...)
|
2016-06-17 20:13:35 -04:00
|
|
|
}
|
2016-12-08 10:27:41 -05:00
|
|
|
xorbkd(dst)
|
|
|
|
if xtra != 4 {
|
|
|
|
dst = dst[:dstLen]
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fnvHash := fnv.New32a()
|
2016-12-08 10:27:41 -05:00
|
|
|
fnvHash.Write(dst[4:])
|
|
|
|
if serial.BytesToUint32(dst[:4]) != fnvHash.Sum32() {
|
2017-02-06 07:49:03 -05:00
|
|
|
return nil, errors.New("KCP:SimpleAuthenticator: Invalid auth.")
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
2016-12-08 10:27:41 -05:00
|
|
|
length := serial.BytesToUint16(dst[4:6])
|
|
|
|
if len(dst)-6 != int(length) {
|
2017-02-06 07:49:03 -05:00
|
|
|
return nil, errors.New("KCP:SimpleAuthenticator: Invalid auth.")
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
2016-12-08 10:27:41 -05:00
|
|
|
return dst[6:], nil
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|