{"record":{"id":"ffbb7988d56933ee","repo":"JuliusBrussee/caveman","slug":"envelope-data-key-entropy-w","errorCode":null,"errorMessage":"envelope: data key entropy: %w","messagePattern":"envelope: data key entropy: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/envelope/envelope.go","lineNumber":72,"sourceCode":"func Seal(plaintext []byte) (ciphertext []byte, metaJSON []byte, err error) {\n\treturn seal(plaintext, schemeV1, nil, \"\")\n}\n\n// SealForScope binds ciphertext authentication to tenant/object scope. Moving\n// ciphertext plus metadata to another tenant, project, or object class makes\n// decryption fail even when storage and KMS credentials are compromised.\nfunc SealForScope(plaintext []byte, scope Scope) (ciphertext []byte, metaJSON []byte, err error) {\n\taad, scopeHash, err := scopeAAD(scope)\n\tif err != nil {\n\t\treturn nil, nil, err\n\t}\n\treturn seal(plaintext, schemeV2, aad, scopeHash)\n}\n\nfunc seal(plaintext []byte, scheme string, aad []byte, scopeHash string) (ciphertext []byte, metaJSON []byte, err error) {\n\tdataKey := make([]byte, 32)\n\tif _, err := rand.Read(dataKey); err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: data key entropy: %w\", err)\n\t}\n\tblock, err := aes.NewCipher(dataKey)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: aes: %w\", err)\n\t}\n\tgcm, err := cipher.NewGCM(block)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: gcm: %w\", err)\n\t}\n\tnonce := make([]byte, gcm.NonceSize())\n\tif _, err := rand.Read(nonce); err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: nonce entropy: %w\", err)\n\t}\n\tciphertext = gcm.Seal(nonce, nonce, plaintext, aad)\n\n\twrapped, err := secretbox.EncryptPayloadKey(dataKey)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: wrap data key: %w\", err)","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/envelope/envelope.go#L54-L90","documentation":"In envelope.seal, a fresh 32-byte data key is drawn from crypto/rand before AES-GCM encryption; if rand.Read fails, the error is wrapped as 'envelope: data key entropy: %w'. This is a fail-closed guard: the library will never encrypt with a predictable or missing key source.","triggerScenarios":"Calling Seal or SealForScope when crypto/rand's underlying OS entropy source read fails (getrandom(2) error, degraded RNG on the host).","commonSituations":"Containers with blocked/insufficient entropy; unusual OSes or sandboxes where getrandom fails; extremely rare crypto/rand panics/errors on misconfigured systems.","solutions":["Inspect the wrapped cause for the OS-level RNG error.","Fix the host entropy source (check /dev/urandom availability, getrandom support, container seccomp rules).","Restart/redeploy the process on a healthy host and retry.","Upgrade Go if running on a platform with known crypto/rand issues."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"ciphertext, meta, err := envelope.Seal(plaintext, aad)\nif err != nil {\n    var entropyErr error\n    if errors.As(err, &entropyErr) && strings.Contains(err.Error(), \"data key entropy\") {\n        // host RNG failure: alert, do NOT retry on same degraded host\n        return fmt.Errorf(\"crypto RNG unavailable, aborting: %w\", err)\n    }\n    return err\n}","preventionTips":["Monitor host entropy health in containers/VMs (getrandom availability).","Avoid seccomp/apparmor rules that block getrandom(2).","Treat this as fail-closed: never substitute a fallback RNG."],"tags":["crypto","entropy","encryption"],"backgroundTag":"internal-invariant-violation","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}