Skip to content

Commit

Permalink
allow encrypting empty chunks
Browse files Browse the repository at this point in the history
cherry-picked from f07ef0e, dd3ac84, 2445d1c

# Conflicts:
#	src/main/java/org/cryptomator/cryptolib/v3/FileContentCryptorImpl.java
#	src/test/java/org/cryptomator/cryptolib/v3/FileContentCryptorImplTest.java
  • Loading branch information
overheadhunter committed Jan 24, 2025
1 parent 20ad8e3 commit f8c27f9
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
});
}

Expand All @@ -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<HELLO WORL><D 1HELLO W><ORLD 2>", UTF_8.decode(dstFile).toString());
}

@Test
Expand All @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public class Encryption {

@DisplayName("encrypt chunk with invalid size")
@ParameterizedTest(name = "cleartext size: {0}")
@ValueSource(ints = {0, Constants.PAYLOAD_SIZE + 1})
@ValueSource(ints = {Constants.PAYLOAD_SIZE + 1})
public void testEncryptChunkOfInvalidSize(int size) {
ByteBuffer cleartext = ByteBuffer.allocate(size);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void resetGcmNonce() {

@DisplayName("encrypt chunk with invalid size")
@ParameterizedTest(name = "cleartext size: {0}")
@ValueSource(ints = {0, org.cryptomator.cryptolib.v2.Constants.PAYLOAD_SIZE + 1})
@ValueSource(ints = {Constants.PAYLOAD_SIZE + 1})
public void testEncryptChunkOfInvalidSize(int size) {
ByteBuffer cleartext = ByteBuffer.allocate(size);

Expand Down Expand Up @@ -184,7 +184,7 @@ public class Decryption {

@DisplayName("decrypt chunk with invalid size")
@ParameterizedTest(name = "ciphertext size: {0}")
@ValueSource(ints = {0, Constants.GCM_NONCE_SIZE + Constants.GCM_TAG_SIZE - 1, org.cryptomator.cryptolib.v2.Constants.CHUNK_SIZE + 1})
@ValueSource(ints = {0, Constants.GCM_NONCE_SIZE + Constants.GCM_TAG_SIZE - 1, Constants.CHUNK_SIZE + 1})
public void testDecryptChunkOfInvalidSize(int size) {
ByteBuffer ciphertext = ByteBuffer.allocate(size);

Expand Down

0 comments on commit f8c27f9

Please sign in to comment.