Skip to content

Commit

Permalink
Merge branch 'develop' into feature/uvf-draft
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/test/java/org/cryptomator/cryptolib/v2/FileContentCryptorImplTest.java
  • Loading branch information
overheadhunter committed Jan 24, 2025
2 parents dcea94d + f8c27f9 commit dcc1aa0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ public DirectoryMetadataImpl decryptDirectoryMetadata(byte[] ciphertext) throws
}
int headerSize = cryptor.fileHeaderCryptor().headerSize();
ByteBuffer buffer = ByteBuffer.wrap(ciphertext);
ByteBuffer headerBuf = buffer.duplicate().position(0).limit(headerSize);
ByteBuffer contentBuf = buffer.duplicate().position(headerSize);
ByteBuffer headerBuf = buffer.duplicate();
headerBuf.position(0).limit(headerSize);
ByteBuffer contentBuf = buffer.duplicate();
contentBuf.position(headerSize);
FileHeaderImpl header = cryptor.fileHeaderCryptor().decryptHeader(headerBuf);
ByteBuffer plaintext = cryptor.fileContentCryptor().decryptChunk(contentBuf, 0, header, true);
assert plaintext.remaining() == 32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public void testChunkEncryption() {
@Test
@DisplayName("encrypt chunk with offset ByteBuffer")
public void testChunkEncryptionWithByteBufferView() {
ByteBuffer cleartext = US_ASCII.encode("12345hello world12345").position(5).limit(16);
ByteBuffer cleartext = US_ASCII.encode("12345hello world12345");
cleartext.position(5).limit(16);
ByteBuffer ciphertext = fileContentCryptor.encryptChunk(cleartext, 0, header);
ByteBuffer expected = ByteBuffer.wrap(BaseEncoding.base64().decode("AAAAAAAAAAAAAAAAAAAAALTwrBTNYP7m3yTGKlhka9WPvX1Lpn5EYfVxlyX1ISgRXtdRnivM7r6F3Og="));
Assertions.assertEquals(expected, ciphertext);
Expand Down Expand Up @@ -171,7 +172,9 @@ public void testChunkDecryption() throws AuthenticationFailedException {
public void testChunkDecryptionWithByteBufferView() throws AuthenticationFailedException {
byte[] actualCiphertext = BaseEncoding.base64().decode("AAAAAAAAAAAAAAAAAAAAALTwrBTNYP7m3yTGKlhka9WPvX1Lpn5EYfVxlyX1ISgRXtdRnivM7r6F3Og=");
ByteBuffer ciphertext = ByteBuffer.allocate(100);
ciphertext.position(10).put(actualCiphertext).position(10).limit(10 + actualCiphertext.length);
ciphertext.position(10);
ciphertext.put(actualCiphertext);
ciphertext.position(10).limit(10 + actualCiphertext.length);
ByteBuffer cleartext = fileContentCryptor.decryptChunk(ciphertext, 0, header, true);
ByteBuffer expected = US_ASCII.encode("hello world");
Assertions.assertEquals(expected, cleartext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@
package org.cryptomator.cryptolib.v2;

import com.google.common.io.BaseEncoding;
import org.cryptomator.cryptolib.api.*;
import org.cryptomator.cryptolib.common.*;
import org.cryptomator.cryptolib.api.AuthenticationFailedException;
import org.cryptomator.cryptolib.api.Cryptor;
import org.cryptomator.cryptolib.api.FileHeader;
import org.cryptomator.cryptolib.api.PerpetualMasterkey;
import org.cryptomator.cryptolib.common.CipherSupplier;
import org.cryptomator.cryptolib.common.DecryptingReadableByteChannel;
import org.cryptomator.cryptolib.common.DestroyableSecretKey;
import org.cryptomator.cryptolib.common.EncryptingWritableByteChannel;
import org.cryptomator.cryptolib.common.GcmTestHelper;
import org.cryptomator.cryptolib.common.ObjectPool;
import org.cryptomator.cryptolib.common.SecureRandomMock;
import org.cryptomator.cryptolib.common.SeekableByteChannelMock;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -122,7 +132,8 @@ public void testChunkEncryptionWithByteBufferView() {
Arrays.fill(nonce, (byte) 0x33);
return null;
}).when(CSPRNG).nextBytes(Mockito.any());
ByteBuffer cleartext = US_ASCII.encode("12345hello world12345").position(5).limit(16);
ByteBuffer cleartext = US_ASCII.encode("12345hello world12345");
cleartext.position(5).limit(16);
ByteBuffer ciphertext = fileContentCryptor.encryptChunk(cleartext, 0, header);
byte[] expected = BaseEncoding.base64().decode("MzMzMzMzMzMzMzMzbYvL7CusRmzk70Kn1QxFA5WQg/hgKeba4bln");
Assertions.assertEquals(ByteBuffer.wrap(expected), ciphertext);
Expand Down Expand Up @@ -196,7 +207,9 @@ public void testChunkDecryption() throws AuthenticationFailedException {
public void testChunkDecryptionWithByteBufferView() throws AuthenticationFailedException {
byte[] actualCiphertext = BaseEncoding.base64().decode("VVVVVVVVVVVVVVVVnHVdh+EbedvPeiCwCdaTYpzn1CXQjhSh7PHv");
ByteBuffer ciphertext = ByteBuffer.allocate(100);
ciphertext.position(10).put(actualCiphertext).position(10).limit(10 + actualCiphertext.length);
ciphertext.position(10);
ciphertext.put(actualCiphertext);
ciphertext.position(10).limit(10 + actualCiphertext.length);
ByteBuffer cleartext = fileContentCryptor.decryptChunk(ciphertext, 0, header, true);
ByteBuffer expected = US_ASCII.encode("hello world");
Assertions.assertEquals(expected, cleartext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public void testChunkEncryptionWithByteBufferView() {
Arrays.fill(nonce, (byte) 0x33);
return null;
}).when(CSPRNG).nextBytes(Mockito.any());
ByteBuffer cleartext = US_ASCII.encode("12345hello world12345").position(5).limit(16);
ByteBuffer cleartext = US_ASCII.encode("12345hello world12345");
cleartext.position(5).limit(16);
ByteBuffer ciphertext = fileContentCryptor.encryptChunk(cleartext, 0, header);
byte[] expected = BaseEncoding.base64().decode("MzMzMzMzMzMzMzMzbYvL7CusRmzk70Kn1QxFA5WQg/hgKeba4bln");
Assertions.assertEquals(ByteBuffer.wrap(expected), ciphertext);
Expand Down Expand Up @@ -210,7 +211,9 @@ public void testChunkDecryption() throws AuthenticationFailedException {
public void testChunkDecryptionWithByteBufferView() throws AuthenticationFailedException {
byte[] actualCiphertext = BaseEncoding.base64().decode("VVVVVVVVVVVVVVVVnHVdh+EbedvPeiCwCdaTYpzn1CXQjhSh7PHv");
ByteBuffer ciphertext = ByteBuffer.allocate(100);
ciphertext.position(10).put(actualCiphertext).position(10).limit(10 + actualCiphertext.length);
ciphertext.position(10);
ciphertext.put(actualCiphertext);
ciphertext.position(10).limit(10 + actualCiphertext.length);
ByteBuffer cleartext = fileContentCryptor.decryptChunk(ciphertext, 0, header, true);
ByteBuffer expected = US_ASCII.encode("hello world");
Assertions.assertEquals(expected, cleartext);
Expand Down

0 comments on commit dcc1aa0

Please sign in to comment.