JuliusBrussee/caveman · error
envelope: wrap data key
Error message
envelope: wrap data key: %w
What it means
After sealing, seal wraps the plaintext data key via secretbox.EncryptPayloadKey and wraps failure as 'envelope: wrap data key: %w'. Envelope encryption requires the data key to be encrypted under the master/keyring key; if the wrapping step fails, no usable ciphertext metadata can be produced, so sealing aborts.
Solutions
- Inspect the wrapped cause for the underlying secretbox failure.
- Ensure the master/wrapping key is loaded and initialized before calling Seal (complete bootstrap/KMS setup first).
- Verify secretbox configuration matches the deployed key material (rotation can leave stale keys).
- Retry after re-initializing the crypto context; if persistent, check library version compatibility of key formats.
Example fix
// before
ciphertext, meta, err := envelope.Seal(plaintext, aad) // master key not loaded yet
// error: envelope: wrap data key: ...
// after
if err := kms.Init(ctx); err != nil { return err } // load master key first
ciphertext, meta, err := envelope.Seal(plaintext, aad) Defensive patterns
Strategy: validation
Validate before calling
// before sealing, ensure the master wrapping key is initialized
if !secretbox.Ready() { // or equivalent readiness check for your bootstrap
return errors.New("master wrapping key not loaded; complete KMS/bootstrap first")
} Try / catch
ciphertext, meta, err := envelope.Seal(plaintext, aad)
if err != nil {
var wrapErr error
if errors.As(err, &wrapErr) && strings.Contains(err.Error(), "wrap data key") {
// re-init key material, then retry once
if initErr := kms.Init(ctx); initErr == nil {
ciphertext, meta, err = envelope.Seal(plaintext, aad)
}
}
return err
} Prevention
- Initialize KMS/master key material during service startup, before any Seal calls.
- Handle key rotation so the wrapping key context stays current.
- Verify key format compatibility when upgrading library versions.
When it happens
Trigger: Seal/SealForScope while secretbox.EncryptPayloadKey fails — typically the master wrapping key/secretbox context is uninitialized, or the underlying crypto primitives fail.
Common situations: Service started before KMS/master key material was loaded; rotated or missing master key; misconfigured secretbox init in a custom bootstrap path.
Related errors
- aes-gcm open
- aes-gcm
- bundle completeness cannot be attested by unsigned export…
- ciphertext too short
- ed25519 public key must be 32 bytes, got
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/6510855f70d9a82f.
Report an issue: GitHub.
Appendix: source
Thrown at shared/platform/envelope/envelope.go:90
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) {
var meta Metadata
if err := json.Unmarshal(metaJSON, &meta); err != nil {
return nil, fmt.Errorf("envelope: parse metadata: %w", err)
}
if meta.Scheme == schemeV2 {
return nil, fmt.Errorf("envelope: tenant scope required for scheme %q", meta.Scheme)View on GitHub (pinned to 3ee70a1026)