JuliusBrussee/caveman · error

envelope: data key entropy

Error message

envelope: data key entropy: %w

What it means

In envelope.seal, a fresh 32-byte data key is drawn from crypto/rand before AES-GCM encryption; if rand.Read fails, the error is wrapped as 'envelope: data key entropy: %w'. This is a fail-closed guard: the library will never encrypt with a predictable or missing key source.

Solutions

  1. Inspect the wrapped cause for the OS-level RNG error.
  2. Fix the host entropy source (check /dev/urandom availability, getrandom support, container seccomp rules).
  3. Restart/redeploy the process on a healthy host and retry.
  4. Upgrade Go if running on a platform with known crypto/rand issues.
Defensive patterns

Strategy: try-catch

Try / catch

ciphertext, meta, err := envelope.Seal(plaintext, aad)
if err != nil {
    var entropyErr error
    if errors.As(err, &entropyErr) && strings.Contains(err.Error(), "data key entropy") {
        // host RNG failure: alert, do NOT retry on same degraded host
        return fmt.Errorf("crypto RNG unavailable, aborting: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling Seal or SealForScope when crypto/rand's underlying OS entropy source read fails (getrandom(2) error, degraded RNG on the host).

Common situations: Containers with blocked/insufficient entropy; unusual OSes or sandboxes where getrandom fails; extremely rare crypto/rand panics/errors on misconfigured systems.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/ffbb7988d56933ee. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/envelope/envelope.go:72

func Seal(plaintext []byte) (ciphertext []byte, metaJSON []byte, err error) {
	return seal(plaintext, schemeV1, nil, "")
}

// SealForScope binds ciphertext authentication to tenant/object scope. Moving
// ciphertext plus metadata to another tenant, project, or object class makes
// decryption fail even when storage and KMS credentials are compromised.
func SealForScope(plaintext []byte, scope Scope) (ciphertext []byte, metaJSON []byte, err error) {
	aad, scopeHash, err := scopeAAD(scope)
	if err != nil {
		return nil, nil, err
	}
	return seal(plaintext, schemeV2, aad, scopeHash)
}

func seal(plaintext []byte, scheme string, aad []byte, scopeHash string) (ciphertext []byte, metaJSON []byte, err error) {
	dataKey := make([]byte, 32)
	if _, err := rand.Read(dataKey); err != nil {
		return nil, nil, fmt.Errorf("envelope: data key entropy: %w", err)
	}
	block, err := aes.NewCipher(dataKey)
	if err != nil {
		return nil, nil, fmt.Errorf("envelope: aes: %w", err)
	}
	gcm, err := cipher.NewGCM(block)
	if err != nil {
		return nil, nil, fmt.Errorf("envelope: gcm: %w", err)
	}
	nonce := make([]byte, gcm.NonceSize())
	if _, err := rand.Read(nonce); err != nil {
		return nil, nil, fmt.Errorf("envelope: nonce entropy: %w", err)
	}
	ciphertext = gcm.Seal(nonce, nonce, plaintext, aad)

	wrapped, err := secretbox.EncryptPayloadKey(dataKey)
	if err != nil {
		return nil, nil, fmt.Errorf("envelope: wrap data key: %w", err)

View on GitHub (pinned to 3ee70a1026)