siyuan-note/siyuan · error

invalid encrypted asset plaintext chunk size

Error message

invalid encrypted asset plaintext chunk size

What it means

After AEAD-decrypting a chunk, DecryptAssetToWriter checks that the plaintext length equals encryptedAssetChunkSize, except for the final chunk which may equal the remaining bytes of metadata.Size. A mismatch means the ciphertext does not match the declared metadata (Size/Chunks) — the stream is internally inconsistent, so the plaintext buffer is zeroed and decryption aborts. AEAD authenticated the chunk in isolation, but its length contradicts the envelope.

Solutions

  1. Restore the whole asset from a single consistent backup/sync source — never merge chunks from different versions
  2. Re-encrypt the asset from the original file (EncryptAsset) to regenerate consistent metadata + chunks
  3. Verify metadata integrity: the Size/Chunks must correspond to the chunk stream actually present
  4. If reproducible with known-good input, report as a regression — the regression suite covers chunk-boundary and splicing cases
Defensive patterns

Strategy: validation

Validate before calling

// ensure the asset came from a single consistent source before decrypting
if srcChecksum, ok := checksums[diskName]; ok && checksum(srcPath) != srcChecksum {
    return errors.New("asset checksum mismatch: refusing to decrypt")
}

Try / catch

plain, err := model.DecryptAssetWithName(boxID, diskName, dek, data)
if err != nil {
    if strings.Contains(err.Error(), "plaintext chunk size") {
        return reencryptFromOriginal(boxID, diskName) // rebuild metadata+chunks
    }
    return err
}

Prevention

When it happens

Trigger: Decrypting an asset whose metadata.Size or Chunks field was forged/altered and re-protected, or whose chunks come from a differently-chunked encryption (chunk splicing across specs/versions); also a chunk decrypting to an unexpected length due to tampering that happened to pass AAD for that index.

Common situations: Manually assembled or repaired ciphertext; mixing chunks across container IDs where the last chunk AAD coincidentally validates; restoring half of an asset from one backup and the rest from another.

Understand the failure class

Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/0d70d528de01b619. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/crypto.go:2476

		aad := encryptedAssetChunkAAD(aadPrefix, metadata.ContainerID, chunkIndex)
		if metadata.Spec == encryptedAssetLegacySpec {
			aad = []byte(fmt.Sprintf("%s:content:%d", aadPrefix, chunkIndex))
		}
		plainChunk, chunkErr := util.DecryptWithAAD(
			assetKey,
			encryptedChunk,
			aad,
		)
		if chunkErr != nil {
			return "", chunkErr
		}
		expectedSize := int64(encryptedAssetChunkSize)
		if remaining := metadata.Size - written; remaining < expectedSize {
			expectedSize = remaining
		}
		if int64(len(plainChunk)) != expectedSize {
			zeroAndClear(plainChunk)
			return "", errors.New("invalid encrypted asset plaintext chunk size")
		}
		n, writeErr := writer.Write(plainChunk)
		written += int64(n)
		zeroAndClear(plainChunk)
		if writeErr != nil {
			return "", writeErr
		}
		if n != len(plainChunk) {
			return "", io.ErrShortWrite
		}
	}
	var terminator uint32
	if err = binary.Read(reader, binary.BigEndian, &terminator); err != nil {
		return "", err
	}
	if terminator != 0 || written != metadata.Size {
		return "", errors.New("invalid encrypted asset content length")
	}

View on GitHub (pinned to 9f775e8a12)