crypto

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package crypto provides encryption, decryption, and obfuscation for Asymptote.

Index

Constants

View Source
const HandshakeTimeout = 10 * time.Second

HandshakeTimeout is the maximum time allowed for the handshake.

View Source
const MaxPadding = 255

MaxPadding is the maximum random padding bytes added to each packet.

Variables

View Source
var (
	ErrDecryptionFailed = errors.New("asymptote/crypto: decryption failed")
	ErrInvalidPayload   = errors.New("asymptote/crypto: invalid payload")
	ErrKeyLength        = errors.New("asymptote/crypto: key must be 32 bytes")
)

Errors for crypto operations.

View Source
var (
	ErrHandshakeFailed  = errors.New("asymptote/crypto: handshake failed")
	ErrInvalidHello     = errors.New("asymptote/crypto: invalid client hello")
	ErrInvalidResponse  = errors.New("asymptote/crypto: invalid server response")
	ErrHandshakeTimeout = errors.New("asymptote/crypto: handshake timeout")
)

Handshake errors.

Functions

func BuildChangeCipherSpec

func BuildChangeCipherSpec() []byte

BuildChangeCipherSpec creates a fake TLS ChangeCipherSpec message. This is a single-byte message (0x01) wrapped in a TLS record with content type 0x14. In real TLS 1.3, this is sent for backward compatibility.

func BuildFakeClientFinished

func BuildFakeClientFinished() ([]byte, error)

BuildFakeClientFinished creates a fake client Finished message. Mimics the encrypted Finished message sent by the client in TLS 1.3.

func BuildFakeEncryptedExtensions

func BuildFakeEncryptedExtensions() ([]byte, error)

BuildFakeEncryptedExtensions creates a fake encrypted extensions message. After ChangeCipherSpec, all further TLS messages appear encrypted. We send random data in an Application Data record to mimic this.

func DeriveHeaderKey

func DeriveHeaderKey(sessionKey []byte) []byte

DeriveHeaderKey derives a 32-byte key for header encryption from the session key. Uses SHA-256(sessionKey || "asymptote-header-key") as a simple KDF.

func GenerateKey

func GenerateKey() ([]byte, error)

GenerateKey generates a random 32-byte key for ChaCha20-Poly1305.

func ParseClientHello

func ParseClientHello(data []byte) (peerPublicKey *ecdh.PublicKey, err error)

ParseClientHello verifies that the received data looks like a TLS 1.3 ClientHello and extracts the X25519 public key from the key_share extension.

func XORBytes

func XORBytes(data []byte, key []byte, counter uint32) []byte

XORBytes XORs src with the keystream and returns the result. The keystream is generated by hashing the key with a counter. This is used for lightweight header obfuscation (not full AEAD).

Types

type Handshaker

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

Handshaker performs the initial key exchange disguised as a TLS 1.3 handshake.

func NewHandshaker

func NewHandshaker(sni string) (*Handshaker, error)

NewHandshaker creates a new handshaker with the given SNI (Server Name Indication).

func (*Handshaker) BuildClientHello

func (h *Handshaker) BuildClientHello() ([]byte, error)

BuildClientHello creates a packet that mimics a TLS 1.3 ClientHello byte-for-byte. This is designed to fool DPI systems into thinking it's regular HTTPS traffic.

func (*Handshaker) BuildServerHello

func (h *Handshaker) BuildServerHello() ([]byte, error)

BuildServerHello creates a server response that looks like a TLS 1.3 ServerHello. It includes the server's X25519 public key.

func (*Handshaker) DeriveSessionKey

func (h *Handshaker) DeriveSessionKey(peerPublicKey *ecdh.PublicKey) ([]byte, error)

DeriveSessionKey performs X25519 key exchange and derives a 32-byte session key.

type Session

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

Session holds the AEAD cipher and related state for a connection.

func NewSession

func NewSession(key []byte) (*Session, error)

NewSession creates a new crypto session with the given 32-byte key.

func (*Session) Open

func (s *Session) Open(data []byte) ([]byte, error)

Open decrypts ciphertext and removes padding. Input format: [Nonce:12][Ciphertext+Tag]

func (*Session) Overhead

func (s *Session) Overhead() int

Overhead returns the total overhead per packet (nonce + padding len + max padding + AEAD tag).

func (*Session) Seal

func (s *Session) Seal(plaintext []byte) ([]byte, error)

Seal encrypts plaintext with random padding and returns the ciphertext. Format: [Nonce:12][PaddingLen:1][Ciphertext:[Payload + Padding]:N+Tag:16]

Jump to

Keyboard shortcuts

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