secure

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package secure provides an authenticated, encrypted record layer for a stream connection (the TCP transport).

The wire format follows the Shadowsocks-AEAD / TLS-record pattern. Each direction is independent and begins with a random 32-byte salt sent in the clear; both peers derive a per-direction session key from the pre-shared password and that salt via HKDF, so the two directions never share a key and each can start its nonce counter at zero without risk of nonce reuse. After the salt, the stream is a sequence of records:

seal(length uint16) || seal(payload[length])

where each seal is ChaCha20-Poly1305 with a 12-byte little-endian counter nonce that increments once per seal. Because every record carries a Poly1305 tag, tampering (or a wrong password) is detected and rejected.

Index

Constants

View Source
const (
	// KeySize is the session key size in bytes.
	KeySize = 32

	// KeyIteration is the number of PBKDF2 iterations used to derive the master
	// key from the password.
	KeyIteration = 4096
)

Variables

View Source
var ErrCorruptStream = errors.New("secure: corrupt stream")

ErrCorruptStream is returned when a record past the first fails to authenticate or declares an invalid length.

View Source
var ErrInvalidPassword = errors.New("secure: invalid password")

ErrInvalidPassword is returned when the first record fails to authenticate, which is what happens when the two peers were configured with different passwords.

Functions

This section is empty.

Types

type EncryptedConnection

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

EncryptedConnection wraps a stream connection and transparently applies the authenticated record layer described in the package documentation. Each direction lazily performs its salt handshake on first use: the send salt is written on the first Write, and the receive salt is read on the first Read.

func NewEncryptedConnection

func NewEncryptedConnection(conn io.ReadWriteCloser, password []byte, initiator bool) *EncryptedConnection

NewEncryptedConnection wraps conn. initiator must be true on the dialing end (the client) and false on the accepting end (the server), so the two ends agree on which direction uses which key.

func (*EncryptedConnection) Close

func (self *EncryptedConnection) Close() error

func (*EncryptedConnection) Read

func (self *EncryptedConnection) Read(buffer []byte) (int, error)

func (*EncryptedConnection) Write

func (self *EncryptedConnection) Write(plaintext []byte) (int, error)

Jump to

Keyboard shortcuts

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