util

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 1, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MinArgon2Time      uint32 = 1
	MinArgon2MemoryKiB uint32 = 19 * 1024 // 19 MiB — OWASP minimum recommendation
	MinArgon2Parallel  uint8  = 1
)

Minimum acceptable Argon2id parameters. Any profile or custom configuration must meet or exceed these to prevent dangerously weak KDF settings. Values are based on OWASP Password Storage Cheat Sheet recommendations for Argon2id.

View Source
const (
	// KDFProfileInteractive targets sub-second derivation on modest hardware.
	// Suitable for development, testing, and high-throughput API servers.
	// OWASP: meets minimum recommendation (Argon2id t=2, m=19MiB).
	KDFProfileInteractive = "interactive"

	// KDFProfileModerate is the production default. Balances security and
	// latency for typical web-application deployments.
	// OWASP: exceeds minimum recommendation (Argon2id t=3, m=64MiB).
	KDFProfileModerate = "moderate"

	// KDFProfileSensitive targets higher-value secrets where multi-second
	// derivation is acceptable. Suitable for CA root keys, backup encryption,
	// and credential export.
	// OWASP: well above minimum recommendation (Argon2id t=4, m=128MiB).
	KDFProfileSensitive = "sensitive"
)

Named KDF profiles for different deployment scenarios. Profiles are ordered from lowest to highest cost.

View Source
const (
	AESKeySize = 32
)
View Source
const HKDFKeyLength = 32

Variables

This section is empty.

Functions

func CompareArgon2idKey

func CompareArgon2idKey(passphrase string, salt []byte, params Argon2idParams, expectedKey []byte) (bool, error)

CompareArgon2idKey derives a key and compares it in constant time to the expected key.

func CopyBytes

func CopyBytes(src []byte) []byte

func DecryptAES

func DecryptAES(cipherText, rawKey []byte) ([]byte, error)

func DecryptAESWithAAD

func DecryptAESWithAAD(cipherText, rawKey, aad []byte) ([]byte, error)

func DeriveArgon2idKey

func DeriveArgon2idKey(passphrase string, salt []byte, params Argon2idParams) ([]byte, error)

DeriveArgon2idKey derives a 32-byte key from a passphrase using Argon2id.

func EncryptAES

func EncryptAES(plainText, rawKey []byte) ([]byte, error)

func EncryptAESWithAAD

func EncryptAESWithAAD(plainText, rawKey, aad []byte) ([]byte, error)

func GenerateSelfSignedCert

func GenerateSelfSignedCert() (tls.Certificate, error)

GenerateSelfSignedCert generates a self-signed certificate in memory.

func HKDF

func HKDF(seed []byte, salt []byte, info []byte) ([]byte, error)

func HexDecode

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

func HexEncode

func HexEncode(b []byte) string

func NewAESKey

func NewAESKey() ([]byte, error)

func NewTwoSecretKey

func NewTwoSecretKey(passphrase string, saltPass []byte, argonParams Argon2idParams, secretKey []byte, saltSecret []byte, info []byte) ([]byte, error)

func Normalize

func Normalize(s string) string

func RandomBytes

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

func RandomChars

func RandomChars(n int) (string, error)

func RandomInt

func RandomInt() (int, error)

func RandomIntn

func RandomIntn(max int) (int, error)

func SharedSecret

func SharedSecret(priv [32]byte, pub [32]byte) ([32]byte, error)

func ValidateArgon2idParams

func ValidateArgon2idParams(p Argon2idParams) error

ValidateArgon2idParams checks that the given parameters meet the minimum acceptable thresholds. Returns an error describing which parameter is too low. This prevents operators from accidentally configuring dangerously weak KDF settings.

func WipeArray32

func WipeArray32(a *[32]byte)

WipeArray32 best-effort zeroes the provided 32-byte array in place.

func WipeBytes

func WipeBytes(b []byte)

WipeBytes best-effort zeroes the provided byte slice in place.

func Xor

func Xor(a, b []byte) ([]byte, error)

Types

type Argon2idParams

type Argon2idParams struct {
	Time        uint32 `json:"time"`
	MemoryKiB   uint32 `json:"memory"`
	Parallelism uint8  `json:"parallelism"`
	KeyLen      uint32 `json:"key_len"`
}

Argon2idParams configures the Argon2id key derivation function. Parameters are stored alongside vault state so that existing vaults keep working when defaults are raised.

func Argon2idProfile

func Argon2idProfile(name string) (Argon2idParams, error)

Argon2idProfile returns the Argon2idParams for a named profile. Returns an error for unknown profile names.

func DefaultArgon2idParams

func DefaultArgon2idParams() Argon2idParams

DefaultArgon2idParams returns the default Argon2id parameters for vault operations. This uses the "moderate" profile: Time=3, Memory=64 MiB, Parallelism=4 — aligned with OWASP Password Storage Cheat Sheet guidance for Argon2id.

Existing vaults are NOT affected by changes to this default because KDF parameters are persisted in vault state at creation time. Only newly created vaults use the current default.

type KeyPair

type KeyPair struct {
	Private [32]byte
	Public  [32]byte
}

func GenerateX25519Keypair

func GenerateX25519Keypair() (KeyPair, error)

Jump to

Keyboard shortcuts

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