v2ray-core/transport/internet/kcp/crypt.go

77 lines
1.7 KiB
Go
Raw Normal View History

2016-06-14 21:25:06 +00:00
package kcp
2016-06-17 14:51:41 +00:00
import (
2016-12-08 15:27:41 +00:00
"crypto/cipher"
2018-11-02 14:47:58 +00:00
"encoding/binary"
2016-06-17 14:51:41 +00:00
"hash/fnv"
2017-09-19 21:27:49 +00:00
"v2ray.com/core/common"
2016-12-08 15:27:41 +00:00
)
2016-12-08 15:32:53 +00:00
// SimpleAuthenticator is a legacy AEAD used for KCP encryption.
2016-06-17 14:51:41 +00:00
type SimpleAuthenticator struct{}
2016-12-08 15:32:53 +00:00
// NewSimpleAuthenticator creates a new SimpleAuthenticator
2016-12-08 15:27:41 +00:00
func NewSimpleAuthenticator() cipher.AEAD {
2016-06-17 14:51:41 +00:00
return &SimpleAuthenticator{}
2016-06-14 21:25:06 +00:00
}
2016-12-08 15:32:53 +00:00
// NonceSize implements cipher.AEAD.NonceSize().
2017-04-13 20:17:58 +00:00
func (*SimpleAuthenticator) NonceSize() int {
2016-12-08 15:27:41 +00:00
return 0
}
2016-12-08 15:32:53 +00:00
// Overhead implements cipher.AEAD.NonceSize().
2017-04-13 20:17:58 +00:00
func (*SimpleAuthenticator) Overhead() int {
2016-06-17 14:51:41 +00:00
return 6
2016-06-14 21:25:06 +00:00
}
2016-12-08 15:32:53 +00:00
// Seal implements cipher.AEAD.Seal().
2017-04-13 20:17:58 +00:00
func (a *SimpleAuthenticator) Seal(dst, nonce, plain, extra []byte) []byte {
2018-11-02 17:20:02 +00: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 15:27:41 +00:00
dst = append(dst, plain...)
2016-06-17 14:51:41 +00:00
fnvHash := fnv.New32a()
2017-09-19 21:27:49 +00:00
common.Must2(fnvHash.Write(dst[4:]))
2016-12-08 15:27:41 +00:00
fnvHash.Sum(dst[:0])
2016-06-17 14:51:41 +00:00
2018-07-19 11:31:57 +00:00
dstLen := len(dst)
xtra := 4 - dstLen%4
2016-12-08 15:27:41 +00:00
if xtra != 4 {
dst = append(dst, make([]byte, xtra)...)
}
2016-12-08 15:27:41 +00:00
xorfwd(dst)
if xtra != 4 {
2018-07-19 11:31:57 +00:00
dst = dst[:dstLen]
2016-06-17 14:51:41 +00:00
}
2016-12-08 15:27:41 +00:00
return dst
2016-06-14 21:25:06 +00:00
}
2016-12-08 15:32:53 +00:00
// Open implements cipher.AEAD.Open().
2017-04-13 20:17:58 +00:00
func (a *SimpleAuthenticator) Open(dst, nonce, cipherText, extra []byte) ([]byte, error) {
2016-12-08 15:27:41 +00:00
dst = append(dst, cipherText...)
dstLen := len(dst)
xtra := 4 - dstLen%4
if xtra != 4 {
dst = append(dst, make([]byte, xtra)...)
}
2016-12-08 15:27:41 +00:00
xorbkd(dst)
if xtra != 4 {
dst = dst[:dstLen]
2016-06-17 14:51:41 +00:00
}
fnvHash := fnv.New32a()
2017-09-19 21:27:49 +00:00
common.Must2(fnvHash.Write(dst[4:]))
2018-11-02 14:47:58 +00:00
if binary.BigEndian.Uint32(dst[:4]) != fnvHash.Sum32() {
2017-04-09 13:04:04 +00:00
return nil, newError("invalid auth")
2016-06-17 14:51:41 +00:00
}
2018-11-02 14:47:58 +00:00
length := binary.BigEndian.Uint16(dst[4:6])
2016-12-08 15:27:41 +00:00
if len(dst)-6 != int(length) {
2017-04-09 13:04:04 +00:00
return nil, newError("invalid auth")
2016-06-17 14:51:41 +00:00
}
2016-12-08 15:27:41 +00:00
return dst[6:], nil
2016-06-17 14:51:41 +00:00
}