From f07ef0e507a358c7c6af633cf1c70cdd29172a98 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Fri, 29 Nov 2024 13:56:57 +0100 Subject: [PATCH] allow empty chunks, so UVF's EOF-chunks can be added --- .../common/EncryptingWritableByteChannel.java | 14 ++------------ .../cryptolib/v1/FileContentCryptorImpl.java | 2 +- .../cryptolib/v2/FileContentCryptorImpl.java | 2 +- .../common/EncryptingWritableByteChannelTest.java | 15 ++++----------- 4 files changed, 8 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/cryptomator/cryptolib/common/EncryptingWritableByteChannel.java b/src/main/java/org/cryptomator/cryptolib/common/EncryptingWritableByteChannel.java index f336258..3b45836 100644 --- a/src/main/java/org/cryptomator/cryptolib/common/EncryptingWritableByteChannel.java +++ b/src/main/java/org/cryptomator/cryptolib/common/EncryptingWritableByteChannel.java @@ -1,11 +1,3 @@ -/******************************************************************************* - * Copyright (c) 2016 Sebastian Stenzel and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the accompanying LICENSE.txt. - * - * Contributors: - * Sebastian Stenzel - initial API and implementation - *******************************************************************************/ package org.cryptomator.cryptolib.common; import org.cryptomator.cryptolib.api.Cryptor; @@ -66,10 +58,8 @@ private void writeHeaderOnFirstWrite() throws IOException { private void encryptAndFlushBuffer() throws IOException { cleartextBuffer.flip(); - if (cleartextBuffer.hasRemaining()) { - ByteBuffer ciphertextBuffer = cryptor.fileContentCryptor().encryptChunk(cleartextBuffer, chunkNumber++, header); - delegate.write(ciphertextBuffer); - } + ByteBuffer ciphertextBuffer = cryptor.fileContentCryptor().encryptChunk(cleartextBuffer, chunkNumber++, header); + delegate.write(ciphertextBuffer); cleartextBuffer.clear(); } diff --git a/src/main/java/org/cryptomator/cryptolib/v1/FileContentCryptorImpl.java b/src/main/java/org/cryptomator/cryptolib/v1/FileContentCryptorImpl.java index 9196d48..729490f 100644 --- a/src/main/java/org/cryptomator/cryptolib/v1/FileContentCryptorImpl.java +++ b/src/main/java/org/cryptomator/cryptolib/v1/FileContentCryptorImpl.java @@ -65,7 +65,7 @@ public ByteBuffer encryptChunk(ByteBuffer cleartextChunk, long chunkNumber, File @Override public void encryptChunk(ByteBuffer cleartextChunk, ByteBuffer ciphertextChunk, long chunkNumber, FileHeader header) { - if (cleartextChunk.remaining() <= 0 || cleartextChunk.remaining() > PAYLOAD_SIZE) { + if (cleartextChunk.remaining() < 0 || cleartextChunk.remaining() > PAYLOAD_SIZE) { throw new IllegalArgumentException("Invalid cleartext chunk size: " + cleartextChunk.remaining() + ", expected range [1, " + PAYLOAD_SIZE + "]"); } if (ciphertextChunk.remaining() < CHUNK_SIZE) { diff --git a/src/main/java/org/cryptomator/cryptolib/v2/FileContentCryptorImpl.java b/src/main/java/org/cryptomator/cryptolib/v2/FileContentCryptorImpl.java index 45e6f79..24e00e3 100644 --- a/src/main/java/org/cryptomator/cryptolib/v2/FileContentCryptorImpl.java +++ b/src/main/java/org/cryptomator/cryptolib/v2/FileContentCryptorImpl.java @@ -63,7 +63,7 @@ public ByteBuffer encryptChunk(ByteBuffer cleartextChunk, long chunkNumber, File @Override public void encryptChunk(ByteBuffer cleartextChunk, ByteBuffer ciphertextChunk, long chunkNumber, FileHeader header) { - if (cleartextChunk.remaining() <= 0 || cleartextChunk.remaining() > PAYLOAD_SIZE) { + if (cleartextChunk.remaining() < 0 || cleartextChunk.remaining() > PAYLOAD_SIZE) { throw new IllegalArgumentException("Invalid cleartext chunk size: " + cleartextChunk.remaining() + ", expected range [1, " + PAYLOAD_SIZE + "]"); } if (ciphertextChunk.remaining() < CHUNK_SIZE) { diff --git a/src/test/java/org/cryptomator/cryptolib/common/EncryptingWritableByteChannelTest.java b/src/test/java/org/cryptomator/cryptolib/common/EncryptingWritableByteChannelTest.java index a900757..396d75c 100644 --- a/src/test/java/org/cryptomator/cryptolib/common/EncryptingWritableByteChannelTest.java +++ b/src/test/java/org/cryptomator/cryptolib/common/EncryptingWritableByteChannelTest.java @@ -1,11 +1,3 @@ -/******************************************************************************* - * Copyright (c) 2016 Sebastian Stenzel and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the accompanying LICENSE.txt. - * - * Contributors: - * Sebastian Stenzel - initial API and implementation - *******************************************************************************/ package org.cryptomator.cryptolib.common; import org.cryptomator.cryptolib.api.Cryptor; @@ -49,7 +41,8 @@ public void setup() { Mockito.when(contentCryptor.encryptChunk(Mockito.any(ByteBuffer.class), Mockito.anyLong(), Mockito.any(FileHeader.class))).thenAnswer(invocation -> { ByteBuffer input = invocation.getArgument(0); String inStr = UTF_8.decode(input).toString(); - return ByteBuffer.wrap(inStr.toUpperCase().getBytes(UTF_8)); + String outStr = "<" + inStr.toUpperCase() + ">"; + return UTF_8.encode(outStr); }); } @@ -60,7 +53,7 @@ public void testEncryption() throws IOException { ch.write(UTF_8.encode("hello world 2")); } dstFile.flip(); - Assertions.assertArrayEquals("hhhhhHELLO WORLD 1HELLO WORLD 2".getBytes(), Arrays.copyOfRange(dstFile.array(), 0, dstFile.remaining())); + Assertions.assertEquals("hhhhh", UTF_8.decode(dstFile).toString()); } @Test @@ -69,7 +62,7 @@ public void testEncryptionOfEmptyFile() throws IOException { // empty, so write nothing } dstFile.flip(); - Assertions.assertArrayEquals("hhhhh".getBytes(), Arrays.copyOfRange(dstFile.array(), 0, dstFile.remaining())); + Assertions.assertEquals("hhhhh<>", UTF_8.decode(dstFile).toString()); } }