siyuan-note/siyuan · error
requires a 32-byte (AES-256) key
Error message
requires a 32-byte (AES-256) key
What it means
encryptGCM enforces that the key passed to Encrypt or EncryptWithAAD is exactly 32 bytes, since the envelope uses AES-256-GCM. The operation name (e.g. "Encrypt", "EncryptWithAAD") is prepended to the message. A shorter or longer key is rejected before any AES work, because a non-256-bit key would silently produce AES-128 or fail in NewCipher.
Solutions
- Derive a 32-byte key with util.DeriveKey(password, salt, util.DefaultArgon2Params()) instead of using raw password/encoded bytes
- Decode hex/base64 key material to raw bytes and verify len(key) == 32 before calling Encrypt/EncryptWithAAD
- If using a master DEK, derive the purpose-specific subkey with util.DeriveSubKey(dek, purpose), which always returns 32 bytes
- Add a caller-side guard: if len(key) != 32 { return errors.New(...) } before encryption calls
Example fix
// before key := []byte(userPassword) ciphertext, err := util.Encrypt(key, data) // after salt := make([]byte, 16) io.ReadFull(rand.Reader, salt) key := util.DeriveKey(userPassword, salt, util.DefaultArgon2Params()) // 32 bytes ciphertext, err := util.Encrypt(key, data)
Defensive patterns
Strategy: validation
Validate before calling
func validKey(key []byte) bool { return len(key) == 32 }
if !validKey(key) { return nil, fmt.Errorf("key must be 32 bytes, got %d", len(key)) } Try / catch
if err != nil {
if strings.HasSuffix(err.Error(), "requires a 32-byte (AES-256) key") {
// re-derive key via util.DeriveKey / DeriveSubKey and retry once
}
return err
} Prevention
- Always obtain keys from util.DeriveKey or util.DeriveSubKey, never from raw passwords or encoded strings
- Decode hex/base64 key storage formats to raw bytes immediately after load
- Assert key length in unit tests for every code path that constructs keys
- Run util.ValidateArgon2Params on persisted Argon2Params so KeyLength is always 32
When it happens
Trigger: Calling Encrypt(key, plaintext) or EncryptWithAAD(key, plaintext, aad) with a key slice whose len() != 32 — e.g. a raw passphrase string used as the key, a 16/24-byte key, a hex-encoded 64-char string (64 bytes), or a truncated DeriveKey output.
Common situations: Developers pass a user password directly instead of running it through DeriveKey (Argon2id) or DeriveSubKey first; store a hex/base64 encoded key without decoding; or hand-roll key material with KeyLength != 32 in Argon2Params (which ValidateArgon2Params would also reject).
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- cannot write incomplete notebook crypto backup
- master password migration is pending
- notebook crypto backup is incomplete or corrupted
- unsupported notebook crypto backup spec
- 346
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/51af4a06f71abb3b.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/util/kdf.go:151
return out
}
// EncryptWithAAD 用 AES-256-GCM 加密并绑定 AAD(附加认证数据)。
// AAD 不被加密,但参与 GCM 认证——解密时必须提供相同 AAD,否则认证失败。
// 把用途/boxID/路径等元数据放入 AAD,可防止同 box 内密文被替换用途或路径(bind 到上下文)。
// 返回格式与 Encrypt 一致,但 AAD 参与校验。
func EncryptWithAAD(key, plaintext, aad []byte) ([]byte, error) {
return encryptGCM(key, plaintext, aad, "EncryptWithAAD")
}
// DecryptWithAAD 对应 EncryptWithAAD 的解密。格式无效、AAD 不匹配或密文被篡改时返回错误。
func DecryptWithAAD(key, ciphertext, aad []byte) ([]byte, error) {
return decryptGCM(key, ciphertext, aad, "DecryptWithAAD")
}
func encryptGCM(key, plaintext, aad []byte, operation string) ([]byte, error) {
if len(key) != 32 {
return nil, errors.New(operation + " requires a 32-byte (AES-256) key")
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
nonce := make([]byte, nonceSize)
if _, err = rand.Read(nonce); err != nil {
return nil, err
}
envelope := make([]byte, encryptionEnvelopeHeaderSize, encryptionEnvelopeHeaderSize+nonceSize+len(plaintext)+gcm.Overhead())
copy(envelope, encryptionMagic[:])
envelope[len(encryptionMagic)] = EncryptionSpec
envelope[len(encryptionMagic)+1] = encryptionAlgorithmAES256GCMView on GitHub (pinned to 9f775e8a12)