{"record":{"id":"85b6b0831e70f33c","repo":"siyuan-note/siyuan","slug":"unsupported-encrypted-envelope-algorithm","errorCode":null,"errorMessage":"unsupported encrypted envelope algorithm","messagePattern":"unsupported encrypted envelope algorithm","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/kdf.go","lineNumber":113,"sourceCode":"\n// Decrypt 对应 Encrypt 的解密。密钥错误、格式无效或密文被篡改时返回错误。\nfunc Decrypt(key, ciphertext []byte) ([]byte, error) {\n\treturn decryptGCM(key, ciphertext, nil, \"Decrypt\")\n}\n\n// EncryptionNonce 从 AES-GCM 密文信封中提取 nonce。\nfunc EncryptionNonce(ciphertext []byte) ([]byte, error) {\n\tif !hasEncryptionMagic(ciphertext) {\n\t\treturn nil, errors.New(\"invalid encrypted envelope magic\")\n\t}\n\tif len(ciphertext) < encryptionEnvelopeHeaderSize {\n\t\treturn nil, errors.New(\"encrypted envelope too short\")\n\t}\n\tif ciphertext[len(encryptionMagic)] != EncryptionSpec {\n\t\treturn nil, errors.New(\"unsupported encrypted envelope spec\")\n\t}\n\tif ciphertext[len(encryptionMagic)+1] != encryptionAlgorithmAES256GCM {\n\t\treturn nil, errors.New(\"unsupported encrypted envelope algorithm\")\n\t}\n\tnonceLength := int(ciphertext[len(encryptionMagic)+2])\n\tif nonceLength == 0 || len(ciphertext) < encryptionEnvelopeHeaderSize+nonceLength {\n\t\treturn nil, errors.New(\"invalid encrypted envelope nonce length\")\n\t}\n\treturn append([]byte(nil), ciphertext[encryptionEnvelopeHeaderSize:encryptionEnvelopeHeaderSize+nonceLength]...), nil\n}\n\n// DeriveSubKey 用 HKDF-SHA256 从主 DEK 派生用途隔离的子密钥。\n// 同一 (dek, purpose) 多次调用结果一致；不同 purpose 派生出相互独立的子密钥，\n// 实现用途分离——.sy/assets/AV 各用独立子密钥，互不可替代，限制单点密钥泄漏的影响面。\nfunc DeriveSubKey(dek []byte, purpose string) []byte {\n\t// HKDF info 用 purpose 字节；salt 为 nil（DEK 本身已是高熵随机密钥，无需额外 salt）\n\tr := hkdf.New(sha256.New, dek, nil, []byte(purpose))\n\tout := make([]byte, 32) // AES-256\n\tif _, err := io.ReadFull(r, out); err != nil {\n\t\t// hkdf.Read 不应出错（除非 dek 为空）；防御性 panic 避免静默返回弱密钥\n\t\tpanic(\"hkdf derive failed: \" + err.Error())","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/util/kdf.go#L95-L131","documentation":"Raised by EncryptionNonce when the algorithm byte at offset len(encryptionMagic)+1 (5) does not equal encryptionAlgorithmAES256GCM (1). Only AES-256-GCM is supported in the envelope today; any other algorithm byte is rejected.","triggerScenarios":"EncryptionNonce reads a blob whose 6th byte is not 1. The byte was corrupted, or the blob came from a build that emitted a different algorithm identifier.","commonSituations":"Bit-rot in stored conf/asset ciphertext; a future envelope that adds a second algorithm but is read by a current kernel; an experimental build that wrote a different algorithm byte.","solutions":["Restore the blob from a known-good backup if corruption is suspected.","Ensure you are running a kernel version compatible with the envelope's algorithm byte.","Regenerate the ciphertext via Encrypt/EncryptWithAAD on a current build."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if util.IsCiphertext(blob) && len(blob) > 5 && blob[5] != 1 { // encryptionAlgorithmAES256GCM\n    return fmt.Errorf(\"envelope algorithm %d is unsupported\", blob[5])\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only AES-256-GCM (byte 1) is supported; reject others early.","Restore corrupted ciphertext from backup.","Regenerate envelopes on a current build."],"tags":["crypto","aes-gcm","encryption","validation","corruption"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}