github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/shadowsocksr/utils/info.go (about)

     1  package ssr
     2  
     3  import (
     4  	"encoding/binary"
     5  	"errors"
     6  )
     7  
     8  const ObfsHMACSHA1Len = 10
     9  
    10  var (
    11  	ErrAuthSHA1v4CRC32Error                = errors.New("auth_sha1_v4 post decrypt data crc32 error")
    12  	ErrAuthSHA1v4DataLengthError           = errors.New("auth_sha1_v4 post decrypt data length error")
    13  	ErrAuthSHA1v4IncorrectChecksum         = errors.New("auth_sha1_v4 post decrypt incorrect checksum")
    14  	ErrAuthAES128IncorrectHMAC             = errors.New("auth_aes128_* post decrypt incorrect hmac")
    15  	ErrAuthAES128DataLengthError           = errors.New("auth_aes128_* post decrypt length mismatch")
    16  	ErrAuthChainDataLengthError            = errors.New("auth_chain_* post decrypt length mismatch")
    17  	ErrAuthChainIncorrectHMAC              = errors.New("auth_chain_* post decrypt incorrect hmac")
    18  	ErrAuthAES128IncorrectChecksum         = errors.New("auth_aes128_* post decrypt incorrect checksum")
    19  	ErrAuthAES128PosOutOfRange             = errors.New("auth_aes128_* post decrypt pos out of range")
    20  	ErrTLS12TicketAuthTooShortData         = errors.New("tls1.2_ticket_auth too short data")
    21  	ErrTLS12TicketAuthHMACError            = errors.New("tls1.2_ticket_auth hmac verifying failed")
    22  	ErrTLS12TicketAuthIncorrectMagicNumber = errors.New("tls1.2_ticket_auth incorrect magic number")
    23  )
    24  
    25  type Info struct {
    26  	IVSize int
    27  	Key    []byte
    28  }
    29  
    30  type Shift128plusContext struct {
    31  	v [2]uint64
    32  }
    33  
    34  func (ctx *Shift128plusContext) InitFromBin(bin []byte) {
    35  	var fillBin [16]byte
    36  	copy(fillBin[:], bin)
    37  
    38  	ctx.v[0] = binary.LittleEndian.Uint64(fillBin[:8])
    39  	ctx.v[1] = binary.LittleEndian.Uint64(fillBin[8:])
    40  }
    41  
    42  func (ctx *Shift128plusContext) InitFromBinDatalen(bin []byte, datalen int) {
    43  	var fillBin [16]byte
    44  	copy(fillBin[:], bin)
    45  	binary.LittleEndian.PutUint16(fillBin[:2], uint16(datalen))
    46  
    47  	ctx.v[0] = binary.LittleEndian.Uint64(fillBin[:8])
    48  	ctx.v[1] = binary.LittleEndian.Uint64(fillBin[8:])
    49  
    50  	for i := 0; i < 4; i++ {
    51  		ctx.Next()
    52  	}
    53  }
    54  
    55  func (ctx *Shift128plusContext) Next() uint64 {
    56  	x := ctx.v[0]
    57  	y := ctx.v[1]
    58  	ctx.v[0] = y
    59  	x ^= x << 23
    60  	x ^= y ^ (x >> 17) ^ (y >> 26)
    61  	ctx.v[1] = x
    62  	return x + y
    63  }