cosmos

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MinPSKLen      = 32                        // Increased PSK length for better security (HMAC-SHA256 needs 32-byte key)
	NonceLen       = 12                        // AES-GCM nonce length
	TagLen         = 16                        // AES-GCM authentication tag length
	AESKeyLen      = 32                        // AES-256 key length (from BLAKE2b-256 hash)
	HMACKeyLen     = 32                        // HMAC-SHA256 key length
	HMACSize       = 32                        // SHA256 output size (32 bytes)
	SequenceNumLen = 8                         // Sequence number length (uint64)
	StateTokenLen  = SequenceNumLen + HMACSize // Total state token length

	// Dynamic padding/header limits
	MaxDynamicPadding = 128 // Max random padding bytes for various sections
	MinDynamicPadding = 32  // Min random padding bytes

	// Mode A (HTTP GET Mimicry) constants
	HTTPLikeMinLen = 100  // Minimum length for a believable HTTP header part
	MaxContentLen  = 8192 // Max content length for embedded payload

	// Mode B (Generic Binary) constants
	BinaryMagicLen = 4          // Length of magic bytes
	BinaryMagic    = 0x434F534D // "COSM" in ASCII
)

Constants for Cosmos protocol (moved from main obfs/cosmos.go)

Variables

This section is empty.

Functions

func DeriveAESKey

func DeriveAESKey(psk []byte, sequenceNumber uint64) ([]byte, error)

DeriveAESKey derives the AES key for payload encryption. It incorporates the current state (sequence number) into the key derivation.

func DeriveHMACKey

func DeriveHMACKey(psk []byte) ([]byte, error)

DeriveHMACKey derives the HMAC key for state token.

func DeriveKey

func DeriveKey(psk []byte, salt string, keyLen int) ([]byte, error)

DeriveKey derives a fixed-size key from the PSK and a context-specific salt.

func GenerateRandomBytes

func GenerateRandomBytes(length int) ([]byte, error)

GenerateRandomBytes generates a slice of cryptographically secure random bytes of the given length.

func GenerateStateToken

func GenerateStateToken(psk []byte, sequenceNumber uint64, encryptedPayloadWithTag []byte) ([]byte, error)

GenerateStateToken generates the sequence number and its HMAC.

func VerifyStateToken

func VerifyStateToken(psk []byte, expectedSequenceNumber uint64, token, encryptedPayloadWithTag []byte) (bool, error)

VerifyStateToken verifies the received state token.

Types

type CosmosObfuscator

type CosmosObfuscator struct {
	PSK []byte // Pre-shared key for all key derivations
	// contains filtered or unexported fields
}

CosmosObfuscator implements an encrypted state machine where packet format and encryption parameters change based on a synchronized state (sequence number).

func (*CosmosObfuscator) Deobfuscate

func (o *CosmosObfuscator) Deobfuscate(in, out []byte) int

Deobfuscate reconstructs and decrypts the payload from a Cosmos packet, advancing the state machine upon successful decryption and validation. Returns the length of the decrypted data, or 0 if an error occurs (e.g., state mismatch, decryption failure).

func (*CosmosObfuscator) Obfuscate

func (o *CosmosObfuscator) Obfuscate(in, out []byte) int

Obfuscate encrypts the input 'in' and embeds it into a state-dependent packet format. Returns the total length of the obfuscated packet, or 0 if an error occurs or 'out' is too small.

type Obfuscator

type Obfuscator interface {
	Obfuscate(in, out []byte) int
	Deobfuscate(in, out []byte) int
}

Obfuscator is the interface that wraps the Obfuscate and Deobfuscate methods. Both methods return the number of bytes written to out. If a packet is not valid, the methods should return 0.

func NewCosmosObfuscator

func NewCosmosObfuscator(psk []byte) (Obfuscator, error)

NewCosmosObfuscator creates a new CosmosObfuscator instance. psk: The pre-shared key. Must be at least MinPSKLen bytes long.

Jump to

Keyboard shortcuts

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