encryption

package
v0.0.0-...-9d49dfe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2025 License: MPL-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MaxNonce = bytes.Repeat([]byte{255}, 12)
View Source
var OutBytesPool = sync.Pool{
	New: func() any {
		return make([]byte, 5+8192+16)
	},
}

Functions

func CreatPadding

func CreatPadding(paddingLens, paddingGaps [][3]int) (length int, lens []int, gaps []time.Duration)

func DecodeHeader

func DecodeHeader(h []byte) (l int, err error)

func DecodeLength

func DecodeLength(b []byte) int

func EncodeHeader

func EncodeHeader(h []byte, l int)

func EncodeLength

func EncodeLength(l int) []byte

func IncreaseNonce

func IncreaseNonce(nonce []byte) []byte

func NewCTR

func NewCTR(key, iv []byte) cipher.Stream

func ParsePadding

func ParsePadding(padding string, paddingLens, paddingGaps *[][3]int) (err error)

Types

type AEAD

type AEAD struct {
	cipher.AEAD
	Nonce [12]byte
}

func NewAEAD

func NewAEAD(ctx, key []byte, useAES bool) *AEAD

func (*AEAD) Open

func (a *AEAD) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error)

func (*AEAD) Seal

func (a *AEAD) Seal(dst, nonce, plaintext, additionalData []byte) []byte

type ClientInstance

type ClientInstance struct {
	NfsPKeys      []any
	NfsPKeysBytes [][]byte
	Hash32s       [][32]byte
	RelaysLength  int
	XorMode       uint32
	Seconds       uint32
	PaddingLens   [][3]int
	PaddingGaps   [][3]int

	RWLock sync.RWMutex
	Expire time.Time
	PfsKey []byte
	Ticket []byte
}

func (*ClientInstance) Handshake

func (i *ClientInstance) Handshake(conn net.Conn) (*CommonConn, error)

func (*ClientInstance) Init

func (i *ClientInstance) Init(nfsPKeysBytes [][]byte, xorMode, seconds uint32, padding string) (err error)

type CommonConn

type CommonConn struct {
	net.Conn
	UseAES      bool
	Client      *ClientInstance
	UnitedKey   []byte
	PreWrite    []byte
	AEAD        *AEAD
	PeerAEAD    *AEAD
	PeerPadding []byte
	// contains filtered or unexported fields
}

func NewCommonConn

func NewCommonConn(conn net.Conn, useAES bool) *CommonConn

func (*CommonConn) Read

func (c *CommonConn) Read(b []byte) (int, error)

func (*CommonConn) Write

func (c *CommonConn) Write(b []byte) (int, error)

type ServerInstance

type ServerInstance struct {
	NfsSKeys      []any
	NfsPKeysBytes [][]byte
	Hash32s       [][32]byte
	RelaysLength  int
	XorMode       uint32
	SecondsFrom   int64
	SecondsTo     int64
	PaddingLens   [][3]int
	PaddingGaps   [][3]int

	RWLock   sync.RWMutex
	Closed   bool
	Lasts    map[int64][16]byte
	Tickets  [][16]byte
	Sessions map[[16]byte]*ServerSession
}

func (*ServerInstance) Close

func (i *ServerInstance) Close() (err error)

func (*ServerInstance) Handshake

func (i *ServerInstance) Handshake(conn net.Conn, fallback *[]byte) (*CommonConn, error)

func (*ServerInstance) Init

func (i *ServerInstance) Init(nfsSKeysBytes [][]byte, xorMode uint32, secondsFrom, secondsTo int64, padding string) (err error)

type ServerSession

type ServerSession struct {
	PfsKey  []byte
	NfsKeys sync.Map
}

type XorConn

type XorConn struct {
	net.Conn
	CTR       cipher.Stream
	PeerCTR   cipher.Stream
	OutSkip   int
	OutHeader []byte
	InSkip    int
	InHeader  []byte
}

func NewXorConn

func NewXorConn(conn net.Conn, ctr, peerCTR cipher.Stream, outSkip, inSkip int) *XorConn

func (*XorConn) Read

func (c *XorConn) Read(b []byte) (int, error)

func (*XorConn) Write

func (c *XorConn) Write(b []byte) (int, error)