1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 17:27:50 -04:00
v2fly/transport/internet/kcp/crypt.go

79 lines
1.7 KiB
Go
Raw Normal View History

2019-02-01 14:08:21 -05:00
// +build !confonly
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"
2018-11-02 10:47:58 -04:00
"encoding/binary"
2016-06-17 10:51:41 -04:00
"hash/fnv"
2017-09-19 17:27:49 -04:00
"v2ray.com/core/common"
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 {
2018-11-02 13:20:02 -04:00
dst = append(dst, 0, 0, 0, 0, 0, 0) // 4 bytes for hash, and then 2 bytes for length
binary.BigEndian.PutUint16(dst[4:], uint16(len(plain)))
2016-12-08 10:27:41 -05:00
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
2018-07-19 07:31:57 -04:00
dstLen := len(dst)
xtra := 4 - dstLen%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 {
2018-07-19 07:31:57 -04:00
dst = dst[:dstLen]
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:]))
2018-11-02 10:47:58 -04:00
if binary.BigEndian.Uint32(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
}
2018-11-02 10:47:58 -04:00
length := binary.BigEndian.Uint16(dst[4:6])
2016-12-08 10:27:41 -05:00
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
}