1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-08 14:24:36 -04:00
v2fly/proxy/vmess/encoding/auth.go

119 lines
2.7 KiB
Go
Raw Normal View History

2016-07-23 07:17:51 -04:00
package encoding
2016-02-27 10:41:21 -05:00
import (
2016-12-07 15:43:41 -05:00
"crypto/md5"
2018-11-02 10:47:58 -04:00
"encoding/binary"
2017-01-13 17:42:39 -05:00
"hash/fnv"
2016-12-13 03:32:29 -05:00
2017-09-19 17:27:49 -04:00
"v2ray.com/core/common"
2016-12-06 18:31:15 -05:00
"v2ray.com/core/common/serial"
2017-09-19 17:27:49 -04:00
"golang.org/x/crypto/sha3"
2016-02-27 10:41:21 -05:00
)
2016-12-13 03:32:29 -05:00
// Authenticate authenticates a byte array using Fnv hash.
2016-02-27 10:41:21 -05:00
func Authenticate(b []byte) uint32 {
fnv1hash := fnv.New32a()
2017-09-19 17:27:49 -04:00
common.Must2(fnv1hash.Write(b))
2016-02-27 10:41:21 -05:00
return fnv1hash.Sum32()
}
2016-12-06 18:31:15 -05:00
2017-05-01 18:28:16 -04:00
type NoOpAuthenticator struct{}
func (NoOpAuthenticator) NonceSize() int {
return 0
}
func (NoOpAuthenticator) Overhead() int {
return 0
}
// Seal implements AEAD.Seal().
func (NoOpAuthenticator) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
return append(dst[:0], plaintext...)
}
// Open implements AEAD.Open().
func (NoOpAuthenticator) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
return append(dst[:0], ciphertext...), nil
}
2016-12-13 03:32:29 -05:00
// FnvAuthenticator is an AEAD based on Fnv hash.
2016-12-06 18:31:15 -05:00
type FnvAuthenticator struct {
}
2016-12-13 03:32:29 -05:00
// NonceSize implements AEAD.NonceSize().
2017-07-25 16:21:59 -04:00
func (*FnvAuthenticator) NonceSize() int {
2016-12-06 18:31:15 -05:00
return 0
}
2016-12-13 03:32:29 -05:00
// Overhead impelements AEAD.Overhead().
2017-07-25 16:21:59 -04:00
func (*FnvAuthenticator) Overhead() int {
2016-12-06 18:31:15 -05:00
return 4
}
2016-12-13 03:32:29 -05:00
// Seal implements AEAD.Seal().
2017-07-25 16:21:59 -04:00
func (*FnvAuthenticator) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
2016-12-07 16:52:56 -05:00
dst = serial.Uint32ToBytes(Authenticate(plaintext), dst)
2016-12-06 18:31:15 -05:00
return append(dst, plaintext...)
}
2016-12-13 03:32:29 -05:00
// Open implements AEAD.Open().
2017-07-25 16:21:59 -04:00
func (*FnvAuthenticator) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
2018-11-02 10:47:58 -04:00
if binary.BigEndian.Uint32(ciphertext[:4]) != Authenticate(ciphertext[4:]) {
2017-04-08 19:43:25 -04:00
return dst, newError("invalid authentication")
2016-12-06 18:31:15 -05:00
}
2016-12-07 16:52:56 -05:00
return append(dst, ciphertext[4:]...), nil
2016-12-06 18:31:15 -05:00
}
2016-12-07 15:43:41 -05:00
2016-12-13 03:32:29 -05:00
// GenerateChacha20Poly1305Key generates a 32-byte key from a given 16-byte array.
2016-12-07 15:43:41 -05:00
func GenerateChacha20Poly1305Key(b []byte) []byte {
key := make([]byte, 32)
t := md5.Sum(b)
copy(key, t[:])
t = md5.Sum(key[:16])
copy(key[16:], t[:])
return key
}
2017-04-23 07:30:08 -04:00
type ShakeSizeParser struct {
shake sha3.ShakeHash
buffer [2]byte
}
func NewShakeSizeParser(nonce []byte) *ShakeSizeParser {
shake := sha3.NewShake128()
2017-09-19 17:27:49 -04:00
common.Must2(shake.Write(nonce))
2017-04-23 07:30:08 -04:00
return &ShakeSizeParser{
shake: shake,
}
}
2018-04-03 05:32:03 -04:00
func (*ShakeSizeParser) SizeBytes() int32 {
2017-04-23 07:30:08 -04:00
return 2
}
func (s *ShakeSizeParser) next() uint16 {
2017-09-19 17:27:49 -04:00
common.Must2(s.shake.Read(s.buffer[:]))
2018-11-02 10:47:58 -04:00
return binary.BigEndian.Uint16(s.buffer[:])
2017-04-23 07:30:08 -04:00
}
func (s *ShakeSizeParser) Decode(b []byte) (uint16, error) {
mask := s.next()
2018-11-02 10:47:58 -04:00
size := binary.BigEndian.Uint16(b)
2017-04-23 07:30:08 -04:00
return mask ^ size, nil
}
func (s *ShakeSizeParser) Encode(size uint16, b []byte) []byte {
mask := s.next()
return serial.Uint16ToBytes(mask^size, b[:0])
}
2018-07-07 09:42:24 -04:00
func (s *ShakeSizeParser) NextPaddingLen() uint16 {
return s.next() % 64
}
2018-07-08 15:46:13 -04:00
2018-07-18 04:40:28 -04:00
func (s *ShakeSizeParser) MaxPaddingLen() uint16 {
2018-07-08 15:46:13 -04:00
return 64
}