noise

package
v0.32.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 5 Imported by: 0 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 noise wraps github.com/flynn/noise to expose a narrow, opinionated surface for the Noise_IK_25519_ChaChaPoly_BLAKE2s cipher suite used by Mobile Protocol v2 (see docs/protocol-mobile.md § End-to-end encryption and docs/knowledge/decisions/024-noise-ik-mobile-e2e.md). The package owns the cipher-suite pin and the empty-associated-data invariant; callers (internal/relay) do not reach into github.com/flynn/noise directly.

Empty associated-data is intentional. The v2 spec § Transport stipulates that the outer routing envelope (conn_id, frame) is plaintext and NOT bound to the AEAD; per-handshake key derivation isolates one session's ciphertext from another, and per-session nonce-counter discipline isolates frames within a session. CipherState.Encrypt and .Decrypt therefore take no AD parameter — there is no way to pass non-empty AD without modifying this package, which would itself require a spec amendment.

SECURITY: this package consumes the binary's X25519 static private key (32 bytes) via NewResponder / NewInitiator. The package never logs and never wraps key bytes into error messages. Callers must uphold the same contract; see internal/keys for the on-disk side of the lifecycle.

Index

Constants

View Source
const KeyLen = 32

KeyLen is the length in bytes of an X25519 static or public key. All key-shaped arguments to this package must be exactly this length.

Variables

View Source
var ErrInvalidKeyLength = errors.New("noise: invalid key length")

ErrInvalidKeyLength is returned (wrapped) by NewResponder and NewInitiator when a key argument is not exactly KeyLen bytes. Match with errors.Is.

Functions

This section is empty.

Types

type CipherState

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

CipherState wraps a *flynn/noise.CipherState. It is the post-handshake AEAD that encrypts and decrypts transport frames. Each CipherState carries a monotonic 64-bit counter; callers must use it strictly in order and must not share a CipherState across goroutines.

Encrypt and Decrypt invoke flynn's Encrypt(out=nil, ad=nil, plaintext) and Decrypt(out=nil, ad=nil, ciphertext); the empty associated-data is the spec-mandated v2 transport invariant.

func (*CipherState) Decrypt

func (c *CipherState) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt opens ciphertext under the next expected nonce and returns plaintext. On MAC failure, counter mismatch, or any other AEAD error returns (nil, wrapped error).

func (*CipherState) Encrypt

func (c *CipherState) Encrypt(plaintext []byte) ([]byte, error)

Encrypt seals plaintext under the next nonce and returns ciphertext. On flynn/noise error returns (nil, wrapped error).

type Initiator

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

Initiator runs the IK initiator side (writes message 1, reads message 2). Same concurrency contract as Responder.

func NewInitiator

func NewInitiator(staticPriv, peerStaticPub []byte) (*Initiator, error)

NewInitiator constructs a Noise_IK initiator configured with the caller's static X25519 private key and the peer's (responder's) static X25519 public key. Both arguments must be exactly KeyLen bytes.

func (*Initiator) ReadResp

func (i *Initiator) ReadResp(respMsg []byte) (earlyData []byte, send, recv *CipherState, err error)

ReadResp consumes IK message 2, returns the responder's early-data payload, and returns the paired CipherStates. send is the initiator's encrypt-outbound state; recv is the initiator's decrypt-inbound state.

func (*Initiator) WriteInit

func (i *Initiator) WriteInit(earlyData []byte) ([]byte, error)

WriteInit produces IK message 1, carrying earlyData as the early-data payload.

type Responder

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

Responder runs the IK responder side of the handshake (reads message 1, writes message 2). After WriteResp returns, the handshake is complete and the returned CipherStates are used for transport.

Methods on Responder are NOT safe for concurrent use. The expected call pattern is exactly ReadInit followed by WriteResp; calling out of order returns an error from flynn/noise wrapped with a "noise:" prefix.

func NewResponder

func NewResponder(staticPriv []byte) (*Responder, error)

NewResponder constructs a Noise_IK responder configured with the binary's static X25519 private key. staticPriv must be exactly KeyLen bytes; the wrapper derives the matching public key via crypto/ecdh and supplies the full DHKey to flynn/noise.

func (*Responder) PeerStatic

func (r *Responder) PeerStatic() []byte

PeerStatic returns a copy of the initiator's 32-byte X25519 static public key as learned from IK message 1.

Callable only after ReadInit has returned nil. flynn/noise's contract is "an error to call before a handshake message containing a static key has been read"; the wrapper does NOT panic on misuse. Before ReadInit has succeeded, the underlying flynn field is the zero value for []byte (nil), so this method returns a zero-length slice (its length is 0; bytes.Equal against any non-empty key returns false). Callers that need stricter enforcement should track handshake state in their own session struct.

The returned slice is a fresh allocation; mutating it does not affect the underlying HandshakeState, and a subsequent call returns an independent copy. Mirrors the defensive-copy posture of NewResponder's StaticKeypair.Private assignment.

func (*Responder) ReadInit

func (r *Responder) ReadInit(initMsg []byte) ([]byte, error)

ReadInit consumes IK message 1 (the wire bytes of noise_init's decoded `data` field — raw bytes, not base64) and returns the initiator's early-data payload.

On MAC failure, malformed message, or out-of-order call, returns (nil, wrapped error). The error wraps flynn/noise's underlying error and the wrapper adds no key material to the message.

func (*Responder) WriteResp

func (r *Responder) WriteResp(earlyData []byte) (respMsg []byte, send, recv *CipherState, err error)

WriteResp produces IK message 2, carrying earlyData as the early-data payload, and returns the paired CipherStates for the post-handshake transport. send is the CipherState this side (responder) uses to encrypt outbound frames; recv is the CipherState this side uses to decrypt inbound frames. earlyData may be nil or zero-length.

Jump to

Keyboard shortcuts

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