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
- Variables
- func AESGCM(meta core.MetaCipher, password string) (core.ShadowCipher, error)
- func AddSalt(b []byte)
- func Chacha20Poly1305(meta core.MetaCipher, password string) (core.ShadowCipher, error)
- func CheckSalt(b []byte) bool
- func NewConn(c net.Conn, ciph core.ShadowCipher) core.TCPConn
- func NewReader(r io.Reader, aead cipher.AEAD) io.Reader
- func NewWriter(w io.Writer, aead cipher.AEAD) io.Writer
- func Pack(dst, plaintext []byte, ciph core.ShadowCipher) ([]byte, error)
- func TestSalt(b []byte) bool
- func Unpack(dst, pkt []byte, ciph core.ShadowCipher) ([]byte, error)
- type AEADSessionManager
- func (m *AEADSessionManager) ClientHandleInbound(payload []byte, target socks.Addr, clientAddr netip.AddrPort) (core.UDPSession, []byte, error)
- func (m *AEADSessionManager) ClientHandleOutbound(encrypted []byte, udpSession core.UDPSession) ([]byte, error)
- func (m *AEADSessionManager) Close() error
- func (m *AEADSessionManager) ServerHandleInbound(encrypted []byte, clientAddr netip.AddrPort) (core.UDPSession, socks.Addr, []byte, error)
- func (m *AEADSessionManager) ServerHandleOutbound(plaintext []byte, target socks.Addr, session core.UDPSession) ([]byte, error)
- type BloomRing
- type Mode
- type SessionManager
- func (m *SessionManager) Close() error
- func (m *SessionManager) Del(key netip.AddrPort) *aeadSession
- func (m *SessionManager) Get(key netip.AddrPort) *aeadSession
- func (m *SessionManager) GetOrCreate(clientAddr netip.AddrPort, target socks.Addr) *aeadSession
- func (m *SessionManager) Set(key netip.AddrPort, session *aeadSession)
Constants ¶
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
const EnvironmentPrefix = "SHADOWSOCKS_"
Variables ¶
var ErrRepeatedSalt = errors.New("repeated salt detected")
ErrRepeatedSalt means detected a reused salt
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 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 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().
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 ¶
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)