cryptoutil

package
v0.85.0-pre.2 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: BSD-3-Clause Imports: 11 Imported by: 0

README

kit/cryptoutil

github.com/vormadev/vorma/kit/cryptoutil

cryptoutil provides focused crypto primitives for common backend tasks:

  • random bytes and fixed-size key helpers
  • symmetric and asymmetric signature verification
  • SHA-256 and HMAC-SHA-256
  • HKDF-SHA-256 key derivation
  • authenticated encryption (XChaCha20-Poly1305 and AES-GCM)

This package assumes callers enforce reasonable input sizes.

Import

import "github.com/vormadev/vorma/kit/cryptoutil"

Quick Start

Convert raw key bytes into Key32
raw := make([]byte, cryptoutil.KeySize) // 32 bytes
if _, err := rand.Read(raw); err != nil {
	return err
}
key, err := cryptoutil.ToKey32(raw)
if err != nil {
	return err
}
encrypted, err := cryptoutil.EncryptSymmetricXChaCha20Poly1305([]byte("hello"), key)
if err != nil {
	return err
}
decrypted, err := cryptoutil.DecryptSymmetricXChaCha20Poly1305(encrypted, key)
if err != nil {
	return err
}
_ = decrypted
Compute and validate HMAC-SHA-256
mac, err := cryptoutil.HmacSha256([]byte("payload"), []byte("secret"))
if err != nil {
	return err
}
valid, err := cryptoutil.ValidateHmacSha256([]byte("payload"), []byte("secret"), mac)
if err != nil {
	return err
}
if !valid {
	return errors.New("invalid MAC")
}
Derive context-specific keys with HKDF
signingKey, err := cryptoutil.HkdfSha256(key, []byte("app-salt"), "signing")
if err != nil {
	return err
}
_ = signingKey

Signing and Verification

Symmetric signing
  • SignSymmetric prepends an HMAC digest to the message.
  • VerifyAndReadSymmetric validates and strips the digest.

Use this when both signer and verifier share the same secret key.

Asymmetric verification
  • VerifyAndReadAsymmetric verifies Ed25519 signatures using a 32-byte public key.
  • VerifyAndReadAsymmetricBase64 does the same for base64-encoded inputs.

Encryption Behavior

  • EncryptSymmetricGeneric prepends a random nonce to the ciphertext.
  • DecryptSymmetricGeneric expects that nonce-prefixed format.
  • XChaCha20-Poly1305 and AES-GCM wrappers call the generic functions with built-in AEAD constructors.

HMAC Validation Semantics

ValidateHmacSha256 returns:

  • true, nil when MAC is valid
  • false, nil when MAC is invalid
  • false, err on malformed inputs or other computation errors

Callers must check the boolean, not just the error.

Input Preconditions and Common Errors

  • RandomBytes(byteLen) rejects negative lengths.
  • ToKey32 requires exactly 32 bytes.
  • FromKey32 requires a non-nil key pointer.
  • HMAC helpers reject nil/empty key material.
  • DecryptSymmetric* returns ErrCipherTextTooShort for malformed ciphertext payloads.

API Coverage

Constants
  • const KeySize
Variables
  • var ErrCipherTextTooShort
  • var ErrHMACInvalid
  • var ErrSecretKeyIsNil
Types
  • type Base64
  • type Key32
  • type ToAEADFunc
Functions
  • func DecryptSymmetricAESGCM(encryptedMsg []byte, secretKey Key32) ([]byte, error)
  • func DecryptSymmetricGeneric(toAEADFunc ToAEADFunc, ciphertext []byte, secretKey Key32) ([]byte, error)
  • func DecryptSymmetricXChaCha20Poly1305(encryptedMsg []byte, secretKey Key32) ([]byte, error)
  • func EncryptSymmetricAESGCM(msg []byte, secretKey Key32) ([]byte, error)
  • func EncryptSymmetricGeneric(toAEADFunc ToAEADFunc, msg []byte, secretKey Key32) ([]byte, error)
  • func EncryptSymmetricXChaCha20Poly1305(msg []byte, secretKey Key32) ([]byte, error)
  • func FromKey32(key Key32) ([]byte, error)
  • func HkdfSha256(secretKey Key32, salt []byte, info string) (Key32, error)
  • func HmacSha256(msg []byte, key []byte) ([]byte, error)
  • func RandomBytes(byteLen int) ([]byte, error)
  • func Sha256Hash(msg []byte) []byte
  • func SignSymmetric(msg []byte, secretKey Key32) ([]byte, error)
  • func ToAEADFuncAESGCM(secretKey Key32) (cipher.AEAD, error)
  • func ToAEADFuncXChaCha20Poly1305(secretKey Key32) (cipher.AEAD, error)
  • func ToKey32(b []byte) (Key32, error)
  • func ValidateHmacSha256(attemptedMsg, attemptedKey, knownGoodMAC []byte) (bool, error)
  • func VerifyAndReadAsymmetric(signedMsg []byte, publicKey Key32) ([]byte, error)
  • func VerifyAndReadAsymmetricBase64(signedMsg, publicKey Base64) ([]byte, error)
  • func VerifyAndReadSymmetric(signedMsg []byte, secretKey Key32) ([]byte, error)

Documentation

Overview

Package cryptoutil provides utility functions for cryptographic operations. It is the consumer's responsibility to ensure that inputs are reasonably sized so as to avoid memory exhaustion attacks.

Index

Constants

View Source
const KeySize = 32

Variables

View Source
var (
	ErrSecretKeyIsNil     = errors.New("secret key is nil")
	ErrCipherTextTooShort = errors.New("ciphertext too short")
	ErrHMACInvalid        = errors.New("HMAC is invalid")
)

Functions

func DecryptSymmetricAESGCM

func DecryptSymmetricAESGCM(encryptedMsg []byte, secretKey Key32) ([]byte, error)

DecryptSymmetricAESGCM decrypts a message using AES-256-GCM.

func DecryptSymmetricGeneric

func DecryptSymmetricGeneric(
	toAEADFunc ToAEADFunc,
	ciphertext []byte,
	secretKey Key32,
) ([]byte, error)

DecryptSymmetricGeneric decrypts a message using a generic AEAD function.

func DecryptSymmetricXChaCha20Poly1305

func DecryptSymmetricXChaCha20Poly1305(encryptedMsg []byte, secretKey Key32) ([]byte, error)

DecryptSymmetricXChaCha20Poly1305 decrypts a message using XChaCha20-Poly1305.

func EncryptSymmetricAESGCM

func EncryptSymmetricAESGCM(msg []byte, secretKey Key32) ([]byte, error)

EncryptSymmetricAESGCM encrypts a message using AES-256-GCM.

func EncryptSymmetricGeneric

func EncryptSymmetricGeneric(
	toAEADFunc ToAEADFunc,
	msg []byte,
	secretKey Key32,
) ([]byte, error)

EncryptSymmetricGeneric encrypts a message using a generic AEAD function.

func EncryptSymmetricXChaCha20Poly1305

func EncryptSymmetricXChaCha20Poly1305(msg []byte, secretKey Key32) ([]byte, error)

EncryptSymmetricXChaCha20Poly1305 encrypts a message using XChaCha20-Poly1305.

func FromKey32

func FromKey32(key Key32) ([]byte, error)

func HmacSha256

func HmacSha256(msg []byte, key []byte) ([]byte, error)

HmacSha256 computes the HMAC-SHA-256 of a message using secret key. As a security precaution, returns an error if the key is nil or empty. If this isn't what you want, use the standard library directly.

func RandomBytes

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

Random returns a slice of cryptographically random bytes of length byteLen.

func Sha256Hash

func Sha256Hash(msg []byte) []byte

Sha256Hash returns the SHA-256 hash of a message as a byte slice.

func SignSymmetric

func SignSymmetric(msg []byte, secretKey Key32) ([]byte, error)

SignSymmetric signs a message using a symmetric key. It is a convenience wrapper around the nacl/auth package, which uses HMAC-SHA-512-256.

func ToAEADFuncAESGCM

func ToAEADFuncAESGCM(secretKey Key32) (cipher.AEAD, error)

ToAEADFuncAESGCM returns an AEAD for AES-256-GCM.

func ToAEADFuncXChaCha20Poly1305

func ToAEADFuncXChaCha20Poly1305(secretKey Key32) (cipher.AEAD, error)

ToAEADFuncXChaCha20Poly1305 returns an AEAD for XChaCha20-Poly1305.

func ValidateHmacSha256

func ValidateHmacSha256(attemptedMsg, attemptedKey, knownGoodMAC []byte) (bool, error)

ValidateHmacSha256 constant-time compares the HMAC-SHA-256 of an attempted message and attempted key against a known good MAC. Returns true if the resulting MAC is valid and false if it is not. Does NOT necessarily return an error if the MAC is invalid, so callers must rely on the boolean return value to determine validity.

func VerifyAndReadAsymmetric

func VerifyAndReadAsymmetric(signedMsg []byte, publicKey Key32) ([]byte, error)

VerifyAndReadAsymmetric verifies a signed message using an Ed25519 public key and returns the original message.

func VerifyAndReadAsymmetricBase64

func VerifyAndReadAsymmetricBase64(signedMsg, publicKey Base64) ([]byte, error)

VerifyAndReadAsymmetricBase64 verifies a signed message using a base64 encoded Ed25519 public key and returns the original message.

func VerifyAndReadSymmetric

func VerifyAndReadSymmetric(signedMsg []byte, secretKey Key32) ([]byte, error)

VerifyAndReadSymmetric verifies a signed message using a symmetric key and returns the original message. It is a convenience wrapper around the nacl/auth package, which uses HMAC-SHA-512-256.

Types

type Base64

type Base64 = string

type Key32

type Key32 = *[KeySize]byte

Alias for a pointer to a size 32 byte array.

func HkdfSha256

func HkdfSha256(secretKey Key32, salt []byte, info string) (Key32, error)

HkdfSha256 derives a new cryptographic key from a 32-byte secret key using HKDF. It uses SHA-256 as the hash function and returns a 32-byte key. Salt and/or info can be nil.

func ToKey32

func ToKey32(b []byte) (Key32, error)

type ToAEADFunc

type ToAEADFunc func(secretKey Key32) (cipher.AEAD, error)

Jump to

Keyboard shortcuts

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