crypto

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 15 Imported by: ? 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 crypto provides cryptographic utilities for certificate management.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidKey is returned when the key is not exactly 32 bytes (AES-256).
	ErrInvalidKey = errors.New("crypto: key must be 32 bytes (AES-256)")

	// ErrAADRequired is returned when the AAD is empty. Domain-separation AAD is
	// MANDATORY: a naked AEAD call (nil/empty AAD) is refused by construction, so
	// two different ciphertext domains can never be confused or cross-decrypted,
	// and "forgot the AAD" is impossible to do silently.
	ErrAADRequired = errors.New("crypto: AAD is required (domain separation; no naked AEAD calls)")

	// ErrMalformedCiphertext is returned when a ciphertext is too short to carry
	// its prepended nonce.
	ErrMalformedCiphertext = errors.New("crypto: ciphertext too short")
)
View Source
var ErrInfoRequired = errors.New("crypto: HKDF info is required (domain separation; no naked seal calls)")

ErrInfoRequired is returned when the HKDF info string is empty. Like the AEAD's mandatory AAD, the domain-separation info is required by construction so two sealing surfaces can never derive the same key from the same recipient.

View Source
var ErrLpsContextIncomplete = errors.New("crypto: LPS seal context requires non-empty device, action, and username")

ErrLpsContextIncomplete is returned when any of the (device, action, username) context fields is empty. All three are required so the AAD unambiguously binds the sealed password to exactly one record; a non-empty-but-partial join (e.g. "device|action|") would bind loosely and is refused.

Functions

func CAFingerprintFromPEM

func CAFingerprintFromPEM(certPEM []byte) (string, error)

CAFingerprintFromPEM returns the lowercase-hex SHA-256 of the certificate's DER bytes. It is byte-for-byte identical to the control server's ca.FingerprintFromPEM, so an operator-delivered pin computed from the control CA (e.g. `openssl x509 -in ca.crt -outform DER | sha256sum`) matches what the agent derives from the CA returned at registration. This is the out-of-band trust anchor for the optional enrollment CA-pin.

func GenerateCSR

func GenerateCSR(hostname string) (csrPEM, keyPEM []byte, err error)

GenerateCSR creates a new ECDSA P-256 key pair and returns the CSR (PEM) and private key (PEM).

The CSR carries no SANs (no DNSNames, IPAddresses, EmailAddresses, or URIs). Agent certificates are client certs — identified by the deviceID that the Control Server writes into the issued cert's Subject.SerialNumber, not by anything the agent puts in the CSR. The Control Server's CA rejects any CSR with SANs:

internal/ca/ca.go: "CSR must not request subject alternative names"

so including a DNS SAN here fails registration immediately. The hostname is still put in the CSR's CN for operator debuggability (the CA discards the CN and replaces it with the device ID), but the DNS SAN is omitted.

func GenerateCSRFromKey

func GenerateCSRFromKey(hostname string, keyPEM []byte) (csrPEM []byte, err error)

GenerateCSRFromKey creates a CSR using an existing PEM-encoded ECDSA private key. This is used for certificate renewal where the key pair is reused.

func GenerateX25519 added in v0.5.3

func GenerateX25519() (*ecdh.PrivateKey, error)

GenerateX25519 generates a recipient keypair for SealToPublicKey / OpenWithPrivateKey. The private key's Bytes() form is what a recipient persists (encrypted at rest); reconstruct with ecdh.X25519().NewPrivateKey.

func OpenLpsPassword added in v0.5.3

func OpenLpsPassword(priv *ecdh.PrivateKey, sealed []byte, deviceID, actionID, username string) (string, error)

OpenLpsPassword reverses SealLpsPassword on the control server: it unseals with the LPS private key under the SAME (deviceID, actionID, username) context. A blob sealed for a different context, tampered, or sealed to a different key fails authentication and returns an error and no plaintext.

func OpenWithAAD

func OpenWithAAD(key, ciphertext, aad []byte) ([]byte, error)

OpenWithAAD reverses SealWithAAD: it splits the prepended nonce, then authenticates and decrypts under key with the SAME aad. Any authentication failure — a wrong key, a wrong-domain aad, or a tampered ciphertext — returns an error and NO plaintext. A 32-byte key and a non-empty aad are required.

func OpenWithPrivateKey added in v0.5.3

func OpenWithPrivateKey(priv *ecdh.PrivateKey, sealed, aad []byte, info string) ([]byte, error)

OpenWithPrivateKey reverses SealToPublicKey: it splits the ephemeral public key, re-derives the sealing key with the SAME info, and authenticates + decrypts under the SAME aad. Any mismatch — a tampered byte anywhere in the blob, a different aad, a different info, or the wrong private key — returns an error and no plaintext.

func ParseX25519PublicKey added in v0.5.3

func ParseX25519PublicKey(raw []byte) (*ecdh.PublicKey, error)

ParseX25519PublicKey parses the 32-byte encoding of an X25519 public key (the PublicKey().Bytes() form that crosses the wire).

func SealLpsPassword added in v0.5.3

func SealLpsPassword(recipient *ecdh.PublicKey, password, deviceID, actionID, username string) ([]byte, error)

SealLpsPassword seals password to the control server's LPS public key, binding the (deviceID, actionID, username) context. The output is the opaque blob the agent puts on the wire; only the holder of the matching private key (control) can open it. All three context fields are required — an empty one makes the AAD ambiguous and is refused by the underlying AEAD.

func SealToPublicKey added in v0.5.3

func SealToPublicKey(recipient *ecdh.PublicKey, plaintext, aad []byte, info string) ([]byte, error)

SealToPublicKey encrypts plaintext so that only the holder of recipient's private key can read it, binding aad as authenticated context and info as the mandatory domain-separation string. Returns ephemeralPub(32) || nonce || ciphertext || tag.

A fresh ephemeral keypair and a fresh AEAD nonce are generated per call from crypto/rand; an RNG failure fails closed. Both a non-empty aad and a non-empty info are required by construction.

func SealWithAAD

func SealWithAAD(key, plaintext, aad []byte) ([]byte, error)

SealWithAAD encrypts plaintext with AES-256-GCM under key, binding aad as additional authenticated data, and returns nonce||ciphertext||tag.

A fresh random 96-bit nonce is generated PER CALL and prepended to the output. The nonce is generated here, never taken from the caller, so nonce reuse under the same key (catastrophic for GCM) cannot be introduced by a caller mistake; a crypto/rand failure fails closed rather than emitting a predictable nonce.

Both a 32-byte key and a NON-EMPTY aad are required: an empty aad is rejected (ErrAADRequired) so every ciphertext is domain-separated. The AEAD tag covers the plaintext AND the aad, so a wrong key, a wrong-domain aad, or any tamper fails authentication in OpenWithAAD.

func VerifyCAContinuity

func VerifyCAContinuity(oldCAPEM, newCAPEM []byte) error

VerifyCAContinuity reports whether newCAPEM is an acceptable rotation of oldCAPEM: it must be byte-identical to the enrolled CA, OR be a CA that is cross-signed by (chains to) the enrolled CA. An unrelated CA is refused — that is the trust-anchor swap an attacker (or a compromised/MITM'd control origin) would attempt at certificate renewal. A hard CA swap (a new self-signed root not cross-signed by the old one) is intentionally refused too: it requires re-enrollment, not silent adoption over the renewal channel.

Types

This section is empty.

Jump to

Keyboard shortcuts

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