Skip to content

Commit

Permalink
Fix potential issue with finalising of null source and cipher returni…
Browse files Browse the repository at this point in the history
…ng null final bytes (using certian cryto provider such as on Android).
  • Loading branch information
erickok committed Mar 15, 2018
1 parent 7c17e54 commit 905ed0d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/src/main/java/nl/nl2312/okio/cipher/CipherSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ class CipherSource(
}
if (streamEnd) {
// Finalize (with padding) if we are at the end
decipheredBuffer.write(cipher.doFinal())
val remainder = cipher.doFinal()
if (remainder != null) {
decipheredBuffer.write(remainder)
}
}

// Sink the requested number of bytes (or all that were still in the source)
Expand Down
10 changes: 10 additions & 0 deletions lib/src/test/java/nl/nl2312/okio/cipher/CipherSourceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ class CipherSourceTest {
decodeCipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv)
}

@Test
fun read_emptySource() {
val cipheredSource = Buffer()//.write(byteArrayOf())

val decoded = CipherSource(cipheredSource, decodeCipher)

val output = Buffer().also { it.writeAll(decoded) }
assertThat(output.readUtf8()).isEqualTo("")
}

@Test
fun read_cipheredArray_oneBlock() {
// NOTE Input is shorter than 1 cipher block of 16 bytes
Expand Down

0 comments on commit 905ed0d

Please sign in to comment.