1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 02:16:10 -05:00
v2fly/transport/internet/kcp/crypt.go

77 lines
1.6 KiB
Go
Raw Normal View History

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"
2016-06-17 10:51:41 -04:00
"hash/fnv"
2017-09-19 17:27:49 -04:00
"v2ray.com/core/common"
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().
2017-04-13 16:17:58 -04:00
func (*SimpleAuthenticator) NonceSize() int {
2016-12-08 10:27:41 -05:00
return 0
}
2016-12-08 10:32:53 -05:00
// Overhead implements cipher.AEAD.NonceSize().
2017-04-13 16:17:58 -04:00
func (*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().
2017-04-13 16:17:58 -04:00
func (a *SimpleAuthenticator) Seal(dst, nonce, plain, extra []byte) []byte {
2016-12-08 10:27:41 -05:00
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()
2017-09-19 17:27:49 -04:00
common.Must2(fnvHash.Write(dst[4:]))
2016-12-08 10:27:41 -05:00
fnvHash.Sum(dst[:0])
2016-06-17 10:51:41 -04:00
2016-12-08 10:27:41 -05:00
len := len(dst)
xtra := 4 - len%4
2016-12-08 10:27:41 -05:00
if xtra != 4 {
dst = append(dst, make([]byte, xtra)...)
}
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().
2017-04-13 16:17:58 -04:00
func (a *SimpleAuthenticator) Open(dst, nonce, cipherText, extra []byte) ([]byte, error) {
2016-12-08 10:27:41 -05:00
dst = append(dst, cipherText...)
dstLen := len(dst)
xtra := 4 - dstLen%4
if xtra != 4 {
dst = append(dst, make([]byte, xtra)...)
}
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()
2017-09-19 17:27:49 -04:00
common.Must2(fnvHash.Write(dst[4:]))
2016-12-08 10:27:41 -05:00
if serial.BytesToUint32(dst[:4]) != fnvHash.Sum32() {
2017-04-09 09:04:04 -04:00
return nil, newError("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-04-09 09:04:04 -04:00
return nil, newError("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
}