JuliusBrussee/caveman · critical

nonce entropy

Error message

nonce entropy: %w

What it means

Before sealing, secretbox.Encrypt fills the GCM nonce with crypto/rand.Read. If the OS cryptographic random source fails, the error is wrapped as 'nonce entropy'. Because a predictable or reused nonce breaks AES-GCM confidentiality/authenticity, the library correctly refuses to proceed rather than continuing with a weak nonce.

Solutions

  1. Check the wrapped cause: errors.Unwrap to distinguish getrandom failure vs /dev/urandom open failure.
  2. Ensure the runtime OS/kernel supports and permits getrandom(2); update seccomp/AppArmor profiles to allow it.
  3. Use a base image with functioning /dev/urandom (not a stripped one) and a recent kernel.
  4. Restart the host/container if the entropy pool is in a degenerate state, and monitor rand availability.

Example fix

// before: docker run with seccomp blocking getrandom
docker run --security-opt seccomp:strict-old.json myapp

// after: allow getrandom in the profile
"syscalls": [{ "names": ["getrandom"], "action": "SCMP_ACT_ALLOW" }]
Defensive patterns

Strategy: try-catch

Validate before calling

if _, err := rand.Read(make([]byte, 12)); err != nil {
    log.Fatalf("crypto/rand unavailable at startup: %v", err)
}

Try / catch

env, err := secretbox.Encrypt(pt)
if err != nil {
    var ne randReadError
    if strings.Contains(err.Error(), "nonce entropy") {
        // non-retryable until entropy source is fixed; crash loudly
        log.Fatalf("entropy source broken: %v", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling secretbox.Encrypt (local path, non-KMS) when crypto/rand cannot read gcm.NonceSize() (12) bytes — e.g. /dev/urandom unavailable or exhausted in a restricted container, getrandom(2) failing, or a seccomp/sandbox policy blocking the getrandom syscall.

Common situations: Minimal Docker images (distroless/scratch) missing entropy setup on old kernels; heavy sandboxing (gVisor, some CI sandboxes) that blocks getrandom; severely restricted chroots without /dev/urandom; VMs with entropy-starved kernels pre-modern-getrandom.

Related errors


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

Appendix: source

Thrown at shared/platform/secretbox/secretbox.go:80

	}
	if runtimeenv.IsProduction() {
		return nil, fmt.Errorf("secretbox: production requires CAVE_KMS_PROVIDER=scaleway")
	}
	keyBytes, err := loadKey()
	if err != nil {
		return nil, err
	}
	block, err := aes.NewCipher(keyBytes)
	if err != nil {
		return nil, fmt.Errorf("aes cipher: %w", err)
	}
	gcm, err := cipher.NewGCM(block)
	if err != nil {
		return nil, fmt.Errorf("aes-gcm: %w", err)
	}
	nonce := make([]byte, gcm.NonceSize())
	if _, err := rand.Read(nonce); err != nil {
		return nil, fmt.Errorf("nonce entropy: %w", err)
	}
	// Seal appends the ciphertext+tag to nonce, so the returned slice is the
	// full nonce||ciphertext envelope.
	return gcm.Seal(nonce, nonce, plaintext, nil), nil
}

// EncryptPayloadKey wraps an artifact data-encryption key. Production uses the
// dedicated payload KEK; local development retains the same AES-GCM envelope as
// other local secrets.
func EncryptPayloadKey(plaintext []byte) ([]byte, error) {
	if useKMS() {
		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
		defer cancel()
		wrapped, err := kms.EncryptPayload(ctx, plaintext)
		if err != nil {
			return nil, fmt.Errorf("secretbox: payload KMS encrypt: %w", err)
		}
		return wrapped, nil

View on GitHub (pinned to 3ee70a1026)