| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package kcp
- import (
- "crypto/cipher"
- "errors"
- "hash/fnv"
- "v2ray.com/core/common/serial"
- )
- var (
- errInvalidAuth = errors.New("Invalid auth.")
- )
- type SimpleAuthenticator struct{}
- func NewSimpleAuthenticator() cipher.AEAD {
- return &SimpleAuthenticator{}
- }
- func (v *SimpleAuthenticator) NonceSize() int {
- return 0
- }
- func (v *SimpleAuthenticator) Overhead() int {
- return 6
- }
- 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...)
- fnvHash := fnv.New32a()
- fnvHash.Write(dst[4:])
- fnvHash.Sum(dst[:0])
- len := len(dst)
- xtra := 4 - len%4
- if xtra != 4 {
- dst = append(dst, make([]byte, xtra)...)
- }
- xorfwd(dst)
- if xtra != 4 {
- dst = dst[:len]
- }
- return dst
- }
- 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)...)
- }
- xorbkd(dst)
- if xtra != 4 {
- dst = dst[:dstLen]
- }
- fnvHash := fnv.New32a()
- fnvHash.Write(dst[4:])
- if serial.BytesToUint32(dst[:4]) != fnvHash.Sum32() {
- return nil, errInvalidAuth
- }
- length := serial.BytesToUint16(dst[4:6])
- if len(dst)-6 != int(length) {
- return nil, errInvalidAuth
- }
- return dst[6:], nil
- }
|