Help us shape the future of Go! Take the 2026 Go Developer Survey

crypto

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package crypto provides authenticated encryption for secrets at rest, key file management, random tokens and token hashing.

Index

Constants

View Source
const (
	ScopeABEntryPassword              = "ab_entry.password"
	ScopeABEntryHash                  = "ab_entry.hash"
	ScopeAddressBookDefaultPassword   = "address_book.password"
	ScopeIdentityProviderClientSecret = "identity_provider.client_secret"
	ScopeShareLinkPassword            = "share_link.password"
	ScopeServerShareLinkToken         = "server_share_link.token"
	ScopeOAuthStatePKCEVerifier       = "oauth_state.pkce_verifier"
	ScopeTOTPSecret                   = "totp"
)

Associated-data (AD) conventions for AEAD-encrypted columns.

Every *_enc column is sealed with AD "<scope>:<row id>", which binds the ciphertext to one column of one row: copying it into another row or column makes Open fail. The scope strings below are part of the on-disk format and MUST NOT change; every writer (client API, admin API, legacy importer) and reader uses these helpers instead of formatting AD strings by hand.

column                                   AD
address_book_entries.password_enc        "ab_entry.password:<entry id>"
address_book_entries.hash_enc            "ab_entry.hash:<entry id>"
address_books.default_password_enc       "address_book.password:<book id>"
identity_providers.client_secret_enc     "identity_provider.client_secret:<provider id>"
share_links.password_enc                 "share_link.password:<link id>"
server_share_links.token_enc             "server_share_link.token:<link id>"
oauth_states.pkce_verifier_enc           "oauth_state.pkce_verifier:<state id>"
totp_secrets.secret_enc                  "totp:<user id>"
settings["mail.smtp"].password_enc       "settings.smtp_password:0"
settings["client.options"].ice_servers_enc "settings.ice_servers:0"
settings["federation.peer.<id>.token"].token_enc "settings.federation_peer_token:<peer id>"

Because the AD contains the row id, a row must be inserted first (to learn its id) and the ciphertext written afterwards, or the id must be known in advance (preserved legacy ids).

KeySize is the AEAD key length in bytes.

View Source
const MinSecretLen = 16

MinSecretLen is the shortest secret LoadOrCreateHexSecret accepts from an existing file.

View Source
const ScopeFederationPeerToken = "settings.federation_peer_token"

ScopeFederationPeerToken seals the token this panel presents to a federation peer, stored in the panel settings (key "federation.peer.<peer id>.token"; the id is the federation_peers row).

View Source
const ScopeICEServers = "settings.ice_servers"

ScopeICEServers seals the client ICE server list (TURN credentials) stored in the panel settings (settings key "client.options"; a single value, id 0).

View Source
const ScopeLDAPBindPassword = "settings.ldap_bind_password"

ScopeLDAPBindPassword seals the LDAP bind password stored in ldap_settings (a single value, id 0).

View Source
const ScopeSMTPPassword = "settings.smtp_password"

ScopeSMTPPassword seals the SMTP password stored in the panel settings (settings key "mail.smtp"; a single value, so the id is always 0).

Variables

View Source
var ErrDecrypt = errors.New("crypto: decryption failed")

ErrDecrypt is returned when a ciphertext is malformed, was produced with a different key, or its associated data does not match.

Functions

func ABEntryHashAD

func ABEntryHashAD(entryID int64) string

ABEntryHashAD is the AD of address_book_entries.hash_enc.

func ABEntryPasswordAD

func ABEntryPasswordAD(entryID int64) string

ABEntryPasswordAD is the AD of address_book_entries.password_enc.

func AD(scope string, id int64) string

AD returns the associated data "<scope>:<id>".

func AddressBookDefaultPasswordAD

func AddressBookDefaultPasswordAD(bookID int64) string

AddressBookDefaultPasswordAD is the AD of address_books.default_password_enc.

func DecodeKey

func DecodeKey(s string) ([]byte, error)

DecodeKey decodes a base64 (std or url, padded or not) 32-byte key.

func Equal

func Equal(a, b []byte) bool

Equal compares two byte slices in constant time (for equal lengths).

func EqualString

func EqualString(a, b string) bool

EqualString compares two strings in constant time (for equal lengths).

func FederationPeerTokenAD added in v0.11.0

func FederationPeerTokenAD(peerID int64) string

FederationPeerTokenAD is the AD of a stored federation peer token.

func HashToken

func HashToken(token string) []byte

HashToken returns sha256(token). Tokens (client JWTs, session tokens, share links, OAuth states) are stored only as this digest, never in plain text. A fast hash is adequate because every token has >=128 bits of entropy.

func ICEServersAD added in v0.3.0

func ICEServersAD() string

ICEServersAD is the AD of the stored client ICE server list.

func IdentityProviderClientSecretAD

func IdentityProviderClientSecretAD(providerID int64) string

IdentityProviderClientSecretAD is the AD of identity_providers.client_secret_enc.

func LDAPBindPasswordAD added in v0.7.0

func LDAPBindPasswordAD() string

LDAPBindPasswordAD is the AD of the stored LDAP bind password.

func OAuthStatePKCEVerifierAD

func OAuthStatePKCEVerifierAD(stateID int64) string

OAuthStatePKCEVerifierAD is the AD of oauth_states.pkce_verifier_enc.

func RandomBytes

func RandomBytes(n int) ([]byte, error)

RandomBytes returns n cryptographically random bytes.

func RandomToken

func RandomToken(n int) (string, error)

RandomToken returns n random bytes encoded as unpadded base64url (URL-, header- and cookie-safe).

func SMTPPasswordAD

func SMTPPasswordAD() string

SMTPPasswordAD is the AD of the stored SMTP password.

func ServerShareLinkTokenAD added in v0.14.0

func ServerShareLinkTokenAD(linkID int64) string

ServerShareLinkTokenAD is the AD of server_share_links.token_enc.

func ShareLinkPasswordAD

func ShareLinkPasswordAD(linkID int64) string

ShareLinkPasswordAD is the AD of share_links.password_enc.

func TOTPSecretAD

func TOTPSecretAD(userID int64) string

TOTPSecretAD is the AD of totp_secrets.secret_enc.

Types

type AEAD

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

AEAD seals and opens small secrets (OIDC client secrets, address-book passwords, TOTP seeds) with XChaCha20-Poly1305. It is safe for concurrent use.

func NewAEAD

func NewAEAD(key []byte) (*AEAD, error)

NewAEAD returns an AEAD for a 32-byte key.

func (*AEAD) Open

func (a *AEAD) Open(ciphertext, ad []byte) ([]byte, error)

Open decrypts a ciphertext produced by Seal with the same associated data.

func (*AEAD) OpenString

func (a *AEAD) OpenString(ciphertext []byte, ad string) (string, error)

OpenString is Open returning a string.

func (*AEAD) Seal

func (a *AEAD) Seal(plaintext, ad []byte) ([]byte, error)

Seal encrypts plaintext bound to associated data ad (e.g. "ab_entry.password:<id>"). Binding to ad prevents a ciphertext from being copied into another row/column.

func (*AEAD) SealString

func (a *AEAD) SealString(plaintext string, ad string) ([]byte, error)

SealString is Seal for string plaintexts.

type KeySource

type KeySource string

KeySource tells where a key came from (for logging; never log the key itself).

const (
	KeyFromEnv       KeySource = "env"
	KeyFromFile      KeySource = "file"
	KeyGeneratedFile KeySource = "generated"
)

Key sources.

func LoadAEADKey

func LoadAEADKey(envB64, path string) ([]byte, KeySource, error)

LoadAEADKey resolves the 32-byte secrets-at-rest key: a base64 value (from RDPANEL_SECRET_KEY) wins; otherwise the key file is read, or created with mode 0600 when it does not exist yet.

func LoadOrCreateHexSecret added in v0.12.0

func LoadOrCreateHexSecret(path string, n int) (string, KeySource, error)

LoadOrCreateHexSecret reads a shared-secret file (e.g. the hbbs authorization secret), creating it with n random bytes as lowercase hex when missing. The new file is written to a temporary name, synced and then linked into place, so a concurrent reader (another process) never sees a partial secret and an existing file is never replaced. The secret is the file content without surrounding white space; it must be at least MinSecretLen characters long.

func LoadOrCreateTextKey

func LoadOrCreateTextKey(path string, n int) ([]byte, KeySource, error)

LoadOrCreateTextKey reads a text key file (e.g. the generated JWT key used when RUSTDESK_API_JWT_KEY is unset), creating it with n random bytes encoded as unpadded base64url when missing. The file content (trimmed) is the key.

Jump to

Keyboard shortcuts

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