RNCryptor/JNCryptor — pull request #15 by carllindberg (2014-06-22 14:56:49+00:00)
Title: Fix HMac validation error on large buffers

Fix issue #6 I think (Mac validation fails); for me (running on MacOS X) the bug would happen with any larger amount of data (more than a 512 bytes, probably due to a CipherInputStream buffer).

When doing the lookahead byte, it is working on the higher-level InputStream, and that operation does not affect the lower-level rawOutputStrem, so there is no need to call reset() on it. Until the last block, the rawOutputStream would never actually get written to until that one-byte read() call (presumably due to buffers in higher-level streams in the chain), and therefore the reset() call would clear it out without ever updating the Mac, meaning that no bytes from the encrypted stream were ever written to the Mac until the (partial) last block, meaning that (of course) the Mac verification failed.

I added a test case which encrypts 50k or so which demonstrates the bug (though it would probably manifest on 1k in my case). The behavior might be different on different platforms, as it's probably dependent on the CipherInputStream implementation (and perhaps the Cipher provider). Simply removing the reset() call lets the new unit test pass.