{"record":{"id":"5c79639a22a39629","repo":"siyuan-note/siyuan","slug":"argon2id-keylength-must-be-32","errorCode":null,"errorMessage":"Argon2id KeyLength must be 32","messagePattern":"Argon2id KeyLength must be 32","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/kdf.go","lineNumber":65,"sourceCode":"\tParallelism uint8  `json:\"parallelism\"` // 并行线程数\n\tKeyLength   uint32 `json:\"keyLength\"`   // 输出密钥长度，单位字节\n}\n\n// DefaultArgon2Params 返回 OWASP 2023 推荐的 Argon2id 参数。\nfunc DefaultArgon2Params() Argon2Params {\n\treturn Argon2Params{\n\t\tMemory:      64 * 1024,\n\t\tIterations:  3,\n\t\tParallelism: 4,\n\t\tKeyLength:   32,\n\t}\n}\n\n// ValidateArgon2Params 校验 Argon2id 参数是否在合理范围内，防止恶意备份设置极大内存导致 OOM，\n// 或过弱参数降低安全性。\nfunc ValidateArgon2Params(p Argon2Params) (Argon2Params, error) {\n\tif p.KeyLength != 32 {\n\t\treturn p, errors.New(\"Argon2id KeyLength must be 32\")\n\t}\n\tif p.Memory < 64*1024 {\n\t\treturn p, errors.New(\"Argon2id Memory too low (minimum 64 MB)\")\n\t}\n\tif p.Memory > 256*1024 {\n\t\treturn p, errors.New(\"Argon2id Memory too high (maximum 256 MB)\")\n\t}\n\tif p.Iterations < 3 {\n\t\treturn p, errors.New(\"Argon2id Iterations too low (minimum 3)\")\n\t}\n\tif p.Iterations > 10 {\n\t\treturn p, errors.New(\"Argon2id Iterations too high (maximum 10)\")\n\t}\n\tif p.Parallelism == 0 || p.Parallelism > 16 {\n\t\treturn p, errors.New(\"Argon2id Parallelism must be between 1 and 16\")\n\t}\n\treturn p, nil\n}","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/kdf.go#L47-L83","documentation":"ValidateArgon2Params checks Argon2id KDF parameters before deriving keys for encrypted notebooks. The derived key length is a fixed format constant: exactly 32 bytes (AES-256). Any configured KeyLength other than 32 is rejected because it would produce keys incompatible with the on-disk encryption format, so the library refuses with this error before any derivation happens.","triggerScenarios":"Calling ValidateArgon2Params (via deriveKEK, EnableEncryptedNotebook, ImportNotebookCryptoBackup, or notebook crypto backup/restore paths) with an Argon2Params struct whose KeyLength field is not 32 — e.g. a hand-edited backup config, a migrated config from tooling, or a constructed params struct that left KeyLength at 0/default.","commonSituations":"Manually editing the notebook crypto settings JSON and changing keyLength; importing a crypto backup produced by an incompatible/older or third-party tool; a bug in code that builds Argon2Params without setting KeyLength=32; corruption of the stored config.","solutions":["Set KeyLength to exactly 32 in the Argon2Params before calling — it is not user-tunable","If it comes from an imported backup/config, correct the keyLength field to 32 (or re-export the backup with the official tooling)","If the config is corrupt and no valid backup exists, restore from a notebook crypto backup that authenticates; never regenerate salts to work around it","In code, always construct params via the existing helpers (deriveKEK etc.) so KeyLength is fixed at 32"],"exampleFix":"// before\np := util.Argon2Params{Memory: 128 * 1024, Iterations: 3, KeyLength: 64}\n_, err := util.ValidateArgon2Params(p) // error\n// after\np := util.Argon2Params{Memory: 128 * 1024, Iterations: 3, KeyLength: 32}\n_, err := util.ValidateArgon2Params(p) // ok","handlingStrategy":"validation","validationCode":"if p.KeyLength != 32 { return fmt.Errorf(\"KeyLength must be 32, got %d\", p.KeyLength) }\n// then call util.ValidateArgon2Params(p)","typeGuard":"func hasValidKeyLength(p util.Argon2Params) bool { return p.KeyLength == 32 }","tryCatchPattern":"if _, err := util.ValidateArgon2Params(p); err != nil && strings.Contains(err.Error(), \"KeyLength must be 32\") {\n    p.KeyLength = 32 // format constant; retry\n}","preventionTips":["Treat KeyLength as a fixed format constant (32), never user-configurable","Validate imported crypto backups before use","Construct Argon2Params only via existing helper functions"],"tags":["kdf","argon2","encryption","validation"],"backgroundTag":"invalid-config-value","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}