shadowaead

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 22 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package shadowaead implements a simple AEAD-protected secure protocol.

In general, there are two types of connections: stream-oriented and packet-oriented. Stream-oriented connections (e.g. TCP) assume reliable and orderly delivery of bytes. Packet-oriented connections (e.g. UDP) assume unreliable and out-of-order delivery of packets, where each packet is either delivered intact or lost.

An encrypted stream starts with a random salt to derive a session key, followed by any number of encrypted records. Each encrypted record has the following structure:

[encrypted payload length]
[payload length tag]
[encrypted payload]
[payload tag]

Payload length is 2-byte unsigned big-endian integer capped at 0x3FFF (16383). The higher 2 bits are reserved and must be set to zero. The first AEAD encrypt/decrypt operation uses a counting nonce starting from 0. After each encrypt/decrypt operation, the nonce is incremented by one as if it were an unsigned little-endian integer.

Each encrypted packet transmitted on a packet-oriented connection has the following structure:

[random salt]
[encrypted payload]
[payload tag]

The salt is used to derive a subkey to initiate an AEAD. Packets are encrypted/decrypted independently using zero nonce.

In both stream-oriented and packet-oriented connections, length of nonce and tag varies depending on which AEAD is used. Salt should be at least 16-byte long.

Index

Constants

View Source
const (
	DefaultSFCapacity = 1e6
	// FalsePositiveRate
	DefaultSFFPR  = 1e-6
	DefaultSFSlot = 10
)

Those suggest value are all set according to https://github.com/shadowsocks/shadowsocks-org/issues/44#issuecomment-281021054 Due to this package contains various internal implementation so const named with DefaultBR prefix

View Source
const EnvironmentPrefix = "SHADOWSOCKS_"

Variables

View Source
var ErrRepeatedSalt = errors.New("repeated salt detected")

ErrRepeatedSalt means detected a reused salt

View Source
var ErrShortPacket = errors.New("short packet")

ErrShortPacket means that the packet is too short for a valid encrypted packet.

Functions

func AESGCM

func AESGCM(meta core.MetaCipher, password string) (core.ShadowCipher, error)

AESGCM creates a new Cipher with a pre-shared key. len(psk) must be one of 16, 24, or 32 to select AES-128/196/256-GCM.

func AddSalt

func AddSalt(b []byte)

AddSalt salt to filter

func Chacha20Poly1305

func Chacha20Poly1305(meta core.MetaCipher, password string) (core.ShadowCipher, error)

Chacha20Poly1305 creates a new Cipher with a pre-shared key. len(psk) must be 32.

func CheckSalt

func CheckSalt(b []byte) bool

func NewConn

func NewConn(c net.Conn, ciph core.ShadowCipher) core.TCPConn

NewConn wraps a stream-oriented net.Conn with cipher.

func NewReader

func NewReader(r io.Reader, aead cipher.AEAD) io.Reader

NewReader wraps an io.Reader with AEAD decryption.

func NewWriter

func NewWriter(w io.Writer, aead cipher.AEAD) io.Writer

NewWriter wraps an io.Writer with AEAD encryption.

func Pack

func Pack(dst, plaintext []byte, ciph core.ShadowCipher) ([]byte, error)

Pack encrypts plaintext using Cipher with a randomly generated salt and returns a slice of dst containing the encrypted packet and any error occurred. Ensure len(dst) >= ciph.SaltSize() + len(plaintext) + aead.Overhead().

func TestSalt

func TestSalt(b []byte) bool

TestSalt returns true if salt is repeated

func Unpack

func Unpack(dst, pkt []byte, ciph core.ShadowCipher) ([]byte, error)

Unpack decrypts pkt using Cipher and returns a slice of dst containing the decrypted payload and any error occurred. Ensure len(dst) >= len(pkt) - aead.SaltSize() - aead.Overhead().

Types

type AEADSessionManager

type AEADSessionManager struct {
	// contains filtered or unexported fields
}

AEADSessionManager implements UDPSessionManager for classic Shadowsocks AEAD.

Design: Simple NAT-based mapping - No session IDs, no packet counters, no replay protection - Maps client address -> outbound connection - Each incoming packet is decrypted independently (stateless) - Target address is extracted from each packet

func NewAEADSessionManager

func NewAEADSessionManager(cipher core.ShadowCipher, timeout time.Duration, role int) *AEADSessionManager

NewAEADSessionManager creates a session manager for AEAD protocol.

func (*AEADSessionManager) ClientHandleInbound

func (m *AEADSessionManager) ClientHandleInbound(payload []byte, target socks.Addr, clientAddr netip.AddrPort) (core.UDPSession, []byte, error)

HandleOutbound encrypts a packet for sending to the server (client-side). AEAD protocol: prepend target address to payload, then encrypt.

func (*AEADSessionManager) ClientHandleOutbound

func (m *AEADSessionManager) ClientHandleOutbound(encrypted []byte, udpSession core.UDPSession) ([]byte, error)

func (*AEADSessionManager) Close

func (m *AEADSessionManager) Close() error

Close cleans up all sessions and connections.

func (*AEADSessionManager) ServerHandleInbound

func (m *AEADSessionManager) ServerHandleInbound(encrypted []byte, clientAddr netip.AddrPort) (core.UDPSession, socks.Addr, []byte, error)

ServerHandleReceive processes an incoming encrypted packet from a client and forwards it. AEAD protocol flow: 1. Decrypt packet: extract salt, decrypt with AEAD 2. Parse target address from decrypted payload

func (*AEADSessionManager) ServerHandleOutbound

func (m *AEADSessionManager) ServerHandleOutbound(plaintext []byte, target socks.Addr, session core.UDPSession) ([]byte, error)

type BloomRing

type BloomRing struct {
	// contains filtered or unexported fields
}

func NewBloomRing

func NewBloomRing(slot, capacity int, falsePositiveRate float64) *BloomRing

func (*BloomRing) Add

func (r *BloomRing) Add(b []byte)

func (*BloomRing) Check

func (r *BloomRing) Check(b []byte) bool

func (*BloomRing) Test

func (r *BloomRing) Test(b []byte) bool

type Mode

type Mode int
const (
	RemoteServer Mode = iota
	RelayClient
	SocksClient
)

type SessionManager

type SessionManager struct {
	// contains filtered or unexported fields
}

SessionManager is a session table mapping client addresses to sessions. For AEAD: stateless NAT, no session IDs.

func NewSessionManager

func NewSessionManager(timeout time.Duration) *SessionManager

func (*SessionManager) Close

func (m *SessionManager) Close() error

Close stops the cleanup loop and closes all sessions.

func (*SessionManager) Del

func (m *SessionManager) Del(key netip.AddrPort) *aeadSession

func (*SessionManager) Get

func (m *SessionManager) Get(key netip.AddrPort) *aeadSession

func (*SessionManager) GetOrCreate

func (m *SessionManager) GetOrCreate(clientAddr netip.AddrPort, target socks.Addr) *aeadSession

func (*SessionManager) Set

func (m *SessionManager) Set(key netip.AddrPort, session *aeadSession)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL