Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add v3 impl compatible to UVF draft #7

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
chenkins marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public int ciphertextChunkSize() {

@Override
public ByteBuffer encryptChunk(ByteBuffer cleartextChunk, long chunkNumber, FileHeader header) {
byte[] nonce = new byte[org.cryptomator.cryptolib.v3.Constants.GCM_NONCE_SIZE];
random.nextBytes(nonce);
return encryptChunk(cleartextChunk, chunkNumber, header, nonce);
}

@Override
public ByteBuffer encryptChunk(ByteBuffer cleartextChunk, long chunkNumber, FileHeader header, byte[] chunkNonce) {
ByteBuffer ciphertextChunk = ByteBuffer.allocate(CHUNK_SIZE);
encryptChunk(cleartextChunk, ciphertextChunk, chunkNumber, header);
ciphertextChunk.flip();
Expand All @@ -55,14 +62,21 @@ 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) {
throw new IllegalArgumentException("Invalid cleartext chunk size: " + cleartextChunk.remaining() + ", expected range [1, " + PAYLOAD_SIZE + "]");
byte[] nonce = new byte[org.cryptomator.cryptolib.v3.Constants.GCM_NONCE_SIZE];
random.nextBytes(nonce);
encryptChunk(cleartextChunk, ciphertextChunk, chunkNumber, header, nonce);
}

@Override
public void encryptChunk(ByteBuffer cleartextChunk, ByteBuffer ciphertextChunk, long chunkNumber, FileHeader header, byte[] chunkNonce) {
if (cleartextChunk.remaining() < 0 || cleartextChunk.remaining() > org.cryptomator.cryptolib.v3.Constants.PAYLOAD_SIZE) {
throw new IllegalArgumentException("Invalid cleartext chunk size: " + cleartextChunk.remaining() + ", expected range [1, " + org.cryptomator.cryptolib.v3.Constants.PAYLOAD_SIZE + "]");
}
if (ciphertextChunk.remaining() < CHUNK_SIZE) {
throw new IllegalArgumentException("Invalid cipehrtext chunk size: " + ciphertextChunk.remaining() + ", must fit up to " + CHUNK_SIZE + " bytes.");
if (ciphertextChunk.remaining() < org.cryptomator.cryptolib.v3.Constants.CHUNK_SIZE) {
throw new IllegalArgumentException("Invalid cipehrtext chunk size: " + ciphertextChunk.remaining() + ", must fit up to " + org.cryptomator.cryptolib.v3.Constants.CHUNK_SIZE + " bytes.");
}
FileHeaderImpl headerImpl = FileHeaderImpl.cast(header);
encryptChunk(cleartextChunk, ciphertextChunk, chunkNumber, headerImpl.getNonce(), headerImpl.getContentKey());
org.cryptomator.cryptolib.v3.FileHeaderImpl headerImpl = org.cryptomator.cryptolib.v3.FileHeaderImpl.cast(header);
encryptChunk(cleartextChunk, ciphertextChunk, chunkNumber, headerImpl.getNonce(), headerImpl.getContentKey(), chunkNonce);
}

@Override
Expand Down Expand Up @@ -90,11 +104,8 @@ public void decryptChunk(ByteBuffer ciphertextChunk, ByteBuffer cleartextChunk,
}

// visible for testing
void encryptChunk(ByteBuffer cleartextChunk, ByteBuffer ciphertextChunk, long chunkNumber, byte[] headerNonce, DestroyableSecretKey fileKey) {
void encryptChunk(ByteBuffer cleartextChunk, ByteBuffer ciphertextChunk, long chunkNumber, byte[] headerNonce, DestroyableSecretKey fileKey, byte[] nonce) {
try (DestroyableSecretKey fk = fileKey.copy()) {
// nonce:
byte[] nonce = new byte[GCM_NONCE_SIZE];
random.nextBytes(nonce);

// payload:
try (ObjectPool.Lease<Cipher> cipher = CipherSupplier.AES_GCM.encryptionCipher(fk, new GCMParameterSpec(GCM_TAG_SIZE * Byte.SIZE, nonce))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class FileContentCryptorImplBenchmark {
@Setup(Level.Trial)
public void prepareData() {
cleartextChunk.rewind();
fileContentCryptor.encryptChunk(cleartextChunk, ciphertextChunk, 0l, new byte[12], ENC_KEY);
fileContentCryptor.encryptChunk(cleartextChunk, ciphertextChunk, 0l, new byte[12], ENC_KEY, new byte[12]);
ciphertextChunk.flip();
}

Expand All @@ -54,7 +54,7 @@ public void shuffleData() {

@Benchmark
public void benchmarkEncryption() {
fileContentCryptor.encryptChunk(cleartextChunk, ciphertextChunk, chunkNumber, headerNonce, ENC_KEY);
fileContentCryptor.encryptChunk(cleartextChunk, ciphertextChunk, chunkNumber, headerNonce, ENC_KEY, new byte[12]);
}

@Benchmark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void testDecryptedEncryptedEqualsPlaintext() throws AuthenticationFailedE
DestroyableSecretKey fileKey = new DestroyableSecretKey(new byte[16], "AES");
ByteBuffer ciphertext = ByteBuffer.allocate(fileContentCryptor.ciphertextChunkSize());
ByteBuffer cleartext = ByteBuffer.allocate(fileContentCryptor.cleartextChunkSize());
fileContentCryptor.encryptChunk(UTF_8.encode("asd"), ciphertext, 42l, new byte[12], fileKey);
fileContentCryptor.encryptChunk(UTF_8.encode("asd"), ciphertext, 42l, new byte[12], fileKey, new byte[GCM_NONCE_SIZE]);
ciphertext.flip();
fileContentCryptor.decryptChunk(ciphertext, cleartext, 42l, new byte[12], fileKey);
cleartext.flip();
Expand Down
Loading