JuliusBrussee/caveman · error

envelope: unwrap data key

Error message

envelope: unwrap data key: %w

What it means

After base64-decoding, open calls secretbox.DecryptPayloadKey to unwrap the data key using the master key. If the wrapped key fails authentication/decryption (wrong master key, tampered blob, wrong key version), it returns this error and the payload is never decrypted.

Solutions

  1. Verify the master key configured for this environment matches the one that sealed the envelope
  2. Check key-rotation tooling re-wrapped (or kept available) keys for all existing envelopes
  3. Compare the envelope's metadata/key version against the active keyring version
  4. Re-seal affected objects under the current key once the correct key is restored — decryption failures are permanent fail-closed

Example fix

// before
master := loadKey("staging-master.key") // wrong env
plain, err := envelope.OpenForScope(ciphertext, metaJSON, scope)
// after
master := loadMasterKey(env) // env-derived key
if _, err := envelope.OpenForScope(ciphertext, metaJSON, scope); err != nil {
	if strings.Contains(err.Error(), "unwrap data key") {
		return fmt.Errorf("envelope sealed under a different master key (env=%s): %w", env, err)
	}
}
Defensive patterns

Strategy: retry

Validate before calling

// Before bulk reads, verify master-key health:
if _, _, err := envelope.Seal([]byte("probe")); err != nil {
	return fmt.Errorf("master key unusable in this env: %w", err)
}

Try / catch

plain, err := envelope.OpenForScope(ciphertext, metaJSON, scope)
if err != nil && strings.Contains(err.Error(), "unwrap data key") {
	// wrong/rotated master key: do NOT retry blindly; re-check key config
	return fmt.Errorf("key unwrap failed — master key mismatch: %w", err)
}

Prevention

When it happens

Trigger: Calling Open/OpenForScope when the master key or key-encryption key has been rotated or differs from the one used at SealForScope time, the wrapped-key bytes were altered, or the metadata belongs to a different keyring/environment (e.g. staging blob opened in production).

Common situations: Key rotation without re-encrypting stored envelopes, copying objects between environments with different master keys, KMS/master-key config pointing at the wrong secret.

Understand the failure class

Background: Checksum mismatch errors: "checksum verification failed", "digest mismatch", "expected vs actual checksum" — what they mean and how to fix them — this error's family across 41 libraries.

Related errors


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

Appendix: source

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

	}
	aad, scopeHash, err := scopeAAD(scope)
	if err != nil {
		return nil, err
	}
	if meta.ScopeHash != scopeHash {
		return nil, fmt.Errorf("envelope: tenant scope mismatch")
	}
	return open(ciphertext, meta, aad)
}

func open(ciphertext []byte, meta Metadata, aad []byte) ([]byte, error) {
	wrapped, err := base64.StdEncoding.DecodeString(meta.WrappedDataKey)
	if err != nil {
		return nil, fmt.Errorf("envelope: decode wrapped key: %w", err)
	}
	dataKey, err := secretbox.DecryptPayloadKey(wrapped)
	if err != nil {
		return nil, fmt.Errorf("envelope: unwrap data key: %w", err)
	}
	block, err := aes.NewCipher(dataKey)
	if err != nil {
		return nil, fmt.Errorf("envelope: aes: %w", err)
	}
	gcm, err := cipher.NewGCM(block)
	if err != nil {
		return nil, fmt.Errorf("envelope: gcm: %w", err)
	}
	ns := gcm.NonceSize()
	if len(ciphertext) < ns {
		return nil, fmt.Errorf("envelope: ciphertext too short")
	}
	nonce, ct := ciphertext[:ns], ciphertext[ns:]
	plaintext, err := gcm.Open(nil, nonce, ct, aad)
	if err != nil {
		return nil, fmt.Errorf("envelope: open: %w", err)
	}

View on GitHub (pinned to 3ee70a1026)