mirror of
https://github.com/signalapp/Signal-Android.git
synced 2025-12-27 06:29:54 +00:00
Fix crash when skipping in BetterCipherInputStream.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user