secure

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package secure provides passphrase-based authenticated encryption for bot backup bundles.

The container wraps an arbitrary plaintext stream (the .memoh.zip bytes) so a bundle that carries credentials can be encrypted at rest. The construction is deliberately small and dependency-light, using only the standard library and golang.org/x/crypto/argon2:

  • Key derivation: Argon2id over the user passphrase + a random 16-byte salt.
  • Encryption: AES-256-GCM in a chunked STREAM construction (RFC-style framed AEAD, the same shape used by age/Tink streaming): the plaintext is split into fixed-size chunks, each sealed independently with a counter-based nonce, and the final chunk is tagged via a "last" flag folded into the nonce. This authenticates chunk order and detects truncation/extension.

On-disk layout:

magic[8] | kdf[1] | argonTime[4] | argonMemoryKiB[4] | argonThreads[1] | salt[16]
then a sequence of frames: ciphertextLen[uint32 BE] | ciphertext

Every encrypted stream ends with a frame produced from the final (possibly empty) chunk sealed with the last flag set, so a decrypter can always tell a complete stream from a truncated one.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPassphraseRequired is returned when an empty passphrase is supplied.
	ErrPassphraseRequired = errors.New("secure: passphrase required")
	// ErrNotEncrypted is returned when the input is not an encrypted bundle.
	ErrNotEncrypted = errors.New("secure: not an encrypted bundle")
	// ErrAuth is returned when authentication fails: a wrong passphrase or
	// tampered ciphertext.
	ErrAuth = errors.New("secure: authentication failed (wrong passphrase or corrupted data)")
	// ErrTruncated is returned when the stream ends before its final frame.
	ErrTruncated = errors.New("secure: truncated bundle")
)

Functions

func Decrypt

func Decrypt(dst io.Writer, src io.Reader, passphrase string) error

Decrypt reads an encrypted bundle from src and writes the plaintext to dst. It returns ErrNotEncrypted if src does not start with the bundle magic, ErrAuth on a wrong passphrase or tampering, and ErrTruncated on a short read.

func Encrypt

func Encrypt(dst io.Writer, src io.Reader, passphrase string) error

Encrypt reads plaintext from src and writes an encrypted bundle to dst.

func IsEncrypted

func IsEncrypted(raw []byte) bool

IsEncrypted reports whether raw begins with the encrypted bundle magic.

Types

This section is empty.

Jump to

Keyboard shortcuts

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