Fix crash when skipping in BetterCipherInputStream.

This commit is contained in:
Greyson Parrelli
2025-07-01 08:42:39 -04:00
parent 516b505059
commit 939f2f82b1
2 changed files with 26 additions and 15 deletions

View File

@@ -58,21 +58,6 @@ class BetterCipherInputStream(
override fun markSupported(): Boolean = false
@Throws(IOException::class)
override fun skip(byteCount: Long): Long {
val buffer = ByteArray(4096)
var skipped = 0L
while (skipped < byteCount) {
val remaining = byteCount - skipped
val read = read(buffer, 0, remaining.toInt())
skipped += read.toLong()
}
return skipped
}
@Throws(IOException::class)
private fun readIncremental(outputBuffer: ByteArray, originalOffset: Int, originalLength: Int): Int {
var offset = originalOffset

View File

@@ -101,6 +101,32 @@ class AttachmentCipherTest {
cipherFile.delete()
}
@Test
fun attachment_encryptDecrypt_skipAll_manyFileSizes(){
for (i in 0..99) {
attachment_encryptDecrypt_skipAll(incremental = false, fileSize = MEBIBYTE + Random().nextInt(1, 64 * 1024))
}
}
private fun attachment_encryptDecrypt_skipAll(incremental: Boolean, fileSize: Int) {
val key = Util.getSecretBytes(64)
val plaintextInput = Util.getSecretBytes(fileSize)
val plaintextHash = MessageDigest.getInstance("SHA-256").digest(plaintextInput)
val encryptResult = encryptData(plaintextInput, key, incremental)
val cipherFile = writeToFile(encryptResult.ciphertext)
val integrityCheck = IntegrityCheck(
encryptedDigest = encryptResult.digest,
plaintextHash = plaintextHash
)
val inputStream = AttachmentCipherInputStream.createForAttachment(cipherFile, plaintextInput.size.toLong(), key, integrityCheck, encryptResult.incrementalDigest, encryptResult.chunkSizeChoice)
while(inputStream.skip(cipherFile.length()) > 0) {
// Empty body, just skipping
}
cipherFile.delete()
}
private fun attachment_encryptDecrypt_plaintextHash(incremental: Boolean, fileSize: Int) {
val key = Util.getSecretBytes(64)
val plaintextInput = Util.getSecretBytes(fileSize)