JuliusBrussee/caveman · error

envelope: nonce entropy

Error message

envelope: nonce entropy: %w

What it means

seal draws the GCM nonce from crypto/rand and wraps a failed read as 'envelope: nonce entropy: %w'. Random nonces are mandatory for GCM security, so the library fails closed rather than sealing with a predictable or repeated nonce.

Solutions

  1. Inspect the wrapped cause for the OS RNG error.
  2. Restore host entropy (fix /dev/urandom, seccomp, or getrandom availability).
  3. Redeploy on a healthy host and retry the operation.
  4. Update Go to the latest patch release if the platform has known crypto/rand defects.
Defensive patterns

Strategy: try-catch

Try / catch

ciphertext, meta, err := envelope.Seal(plaintext, aad)
if err != nil {
    if strings.Contains(err.Error(), "nonce entropy") {
        return fmt.Errorf("RNG failed during nonce generation; refusing to encrypt: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Seal/SealForScope while the second rand.Read (nonce) fails — same OS entropy failure mode as the data key read.

Common situations: Host entropy exhaustion or getrandom failures in containers/sandboxes; degraded VM RNG; seccomp policies blocking getrandom.

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/892b04f59334e11e. Report an issue: GitHub.

Appendix: source

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

	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)
	}
	meta := Metadata{Scheme: scheme, WrappedDataKey: base64.StdEncoding.EncodeToString(wrapped), ScopeHash: scopeHash}
	metaJSON, err = json.Marshal(meta)
	if err != nil {
		return nil, nil, fmt.Errorf("envelope: marshal metadata: %w", err)
	}
	return ciphertext, metaJSON, nil
}

// Open reverses Seal: it unwraps the data key from metadata and decrypts the
// ciphertext. An unknown scheme fails closed.
func Open(ciphertext []byte, metaJSON []byte) ([]byte, error) {

View on GitHub (pinned to 3ee70a1026)