AesFlushingCipher { public void updateInPlace(byte[] data, int offset, int length) { update(data, offset, length, data, offset); } AesFlushingCipher(int mode, byte[] secretKey, long nonce, long offset); void updateInPlace(byte[] data, int offset, int length); void update(byte[] in, int inOffset, int length, byte[] out, int outOffset);  }
@Test public void testSingle() { byte[] reference = TestUtil.buildTestData(DATA_LENGTH); byte[] data = reference.clone(); encryptCipher.updateInPlace(data, 0, data.length); int unchangedByteCount = data.length - getDifferingByteCount(reference, data); assertThat(unchangedByteCount <= getMaxUnchangedBytesAllowedPostEncryption(data.length)) .isTrue(); decryptCipher.updateInPlace(data, 0, data.length); int differingByteCount = getDifferingByteCount(reference, data); assertThat(differingByteCount).isEqualTo(0); }
@Test public void testAligned() { byte[] reference = TestUtil.buildTestData(DATA_LENGTH); byte[] data = reference.clone(); Random random = new Random(RANDOM_SEED); int offset = 0; while (offset < data.length) { int bytes = (1 + random.nextInt(50)) * 16; bytes = Math.min(bytes, data.length - offset); assertThat(bytes % 16).isEqualTo(0); encryptCipher.updateInPlace(data, offset, bytes); offset += bytes; } int unchangedByteCount = data.length - getDifferingByteCount(reference, data); assertThat(unchangedByteCount <= getMaxUnchangedBytesAllowedPostEncryption(data.length)) .isTrue(); offset = 0; while (offset < data.length) { int bytes = (1 + random.nextInt(50)) * 16; bytes = Math.min(bytes, data.length - offset); assertThat(bytes % 16).isEqualTo(0); decryptCipher.updateInPlace(data, offset, bytes); offset += bytes; } int differingByteCount = getDifferingByteCount(reference, data); assertThat(differingByteCount).isEqualTo(0); }
@Test public void testUnAligned() { byte[] reference = TestUtil.buildTestData(DATA_LENGTH); byte[] data = reference.clone(); Random random = new Random(RANDOM_SEED); int offset = 0; while (offset < data.length) { int bytes = 1 + random.nextInt(4095); bytes = Math.min(bytes, data.length - offset); encryptCipher.updateInPlace(data, offset, bytes); offset += bytes; } int unchangedByteCount = data.length - getDifferingByteCount(reference, data); assertThat(unchangedByteCount <= getMaxUnchangedBytesAllowedPostEncryption(data.length)) .isTrue(); offset = 0; while (offset < data.length) { int bytes = 1 + random.nextInt(4095); bytes = Math.min(bytes, data.length - offset); decryptCipher.updateInPlace(data, offset, bytes); offset += bytes; } int differingByteCount = getDifferingByteCount(reference, data); assertThat(differingByteCount).isEqualTo(0); }
@Test public void testMidJoin() { byte[] reference = TestUtil.buildTestData(DATA_LENGTH); byte[] data = reference.clone(); Random random = new Random(RANDOM_SEED); int offset = 0; while (offset < data.length) { int bytes = 1 + random.nextInt(4095); bytes = Math.min(bytes, data.length - offset); encryptCipher.updateInPlace(data, offset, bytes); offset += bytes; } int unchangedByteCount = data.length - getDifferingByteCount(reference, data); assertThat(unchangedByteCount <= getMaxUnchangedBytesAllowedPostEncryption(data.length)) .isTrue(); offset = random.nextInt(4096); decryptCipher = new AesFlushingCipher(Cipher.DECRYPT_MODE, KEY, NONCE, offset + START_OFFSET); int remainingLength = data.length - offset; int originalOffset = offset; while (remainingLength > 0) { int bytes = 1 + random.nextInt(4095); bytes = Math.min(bytes, remainingLength); decryptCipher.updateInPlace(data, offset, bytes); offset += bytes; remainingLength -= bytes; } int differingByteCount = getDifferingByteCount(reference, data, originalOffset); assertThat(differingByteCount).isEqualTo(0); }