JuliusBrussee/caveman · error
envelope: wrap data key: %w
Error message
envelope: wrap data key: %w
What it means
seal successfully encrypted the plaintext under a fresh data key, but secretbox.EncryptPayloadKey failed to wrap that data key with the KMS. The ciphertext is discarded — envelope encryption requires the key to be recoverable via the wrapped copy, so an unwrappable result must not be persisted.
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 766dce6b13)
Solutions
- Check KMS availability, credentials, and key URI
- Retry the seal once the KMS is healthy
Defensive patterns
Strategy: retry
When it happens
Trigger: Thrown at shared/platform/envelope/envelope.go:90 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of JuliusBrussee/caveman@766dce6b13 (2026-08-18).
Data as JSON: /api/errors/6510855f70d9a82f.
Report an issue: GitHub.