Skip to content

Commit b4a63a8

Browse files
Adapted new cryptolib API
1 parent ccc30ad commit b4a63a8

19 files changed

+80
-68
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@
9292
<version>2.6.1</version>
9393
<scope>test</scope>
9494
</dependency>
95+
<dependency>
96+
<groupId>org.bouncycastle</groupId>
97+
<artifactId>bcprov-jdk15on</artifactId>
98+
<version>1.54</version>
99+
<!-- not actually needed, but otherwise unit tests fail in Eclipe, if the "cryptolib" project is opened in the same workspace -->
100+
<scope>test</scope>
101+
</dependency>
95102
</dependencies>
96103

97104
<build>

src/main/java/org/cryptomator/cryptofs/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ final class Constants {
1414
public static final String BACKUPKEY_FILE_NAME = "masterkey.cryptomator.bkup";
1515
public static final String DATA_DIR_NAME = "d";
1616
public static final String DIR_PREFIX = "0";
17+
public static final int VAULT_VERSION = 4;
1718

1819
}

src/main/java/org/cryptomator/cryptofs/CryptoBasicFileAttributes.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
import java.nio.file.attribute.BasicFileAttributes;
1818
import java.util.Collections;
1919

20-
import org.cryptomator.cryptolib.FileHeader;
21-
import org.cryptomator.cryptolib.FileHeaderCryptor;
20+
import org.cryptomator.cryptolib.api.FileHeaderCryptor;
2221

2322
public class CryptoBasicFileAttributes implements DelegatingBasicFileAttributes {
2423

@@ -60,20 +59,20 @@ public boolean isSymbolicLink() {
6059

6160
@Override
6261
public long size() {
63-
if (isRegularFile() && getDelegate().size() >= FileHeader.SIZE && size == -1) {
62+
if (isRegularFile() && getDelegate().size() >= headerCryptor.headerSize() && size == -1) {
6463
size = readSizeFromHeader();
6564
}
6665
return size;
6766
}
6867

6968
private long readSizeFromHeader() {
7069
try {
71-
ByteBuffer buf = ByteBuffer.allocate(FileHeader.SIZE);
70+
ByteBuffer buf = ByteBuffer.allocate(headerCryptor.headerSize());
7271
try (ReadableByteChannel r = ciphertextPath.getFileSystem().provider().newByteChannel(ciphertextPath, Collections.singleton(StandardOpenOption.READ))) {
7372
r.read(buf);
7473
}
7574
buf.flip();
76-
return headerCryptor.decryptHeader(buf).getPayload().getFilesize();
75+
return headerCryptor.decryptHeader(buf).getFilesize();
7776
} catch (IOException e) {
7877
throw new UncheckedIOException(e);
7978
}

src/main/java/org/cryptomator/cryptofs/CryptoDirectoryStream.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import java.util.regex.Pattern;
2020

2121
import org.cryptomator.cryptofs.CryptoPathMapper.Directory;
22-
import org.cryptomator.cryptolib.AuthenticationFailedException;
23-
import org.cryptomator.cryptolib.FileNameCryptor;
22+
import org.cryptomator.cryptolib.api.AuthenticationFailedException;
23+
import org.cryptomator.cryptolib.api.FileNameCryptor;
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
2626

@@ -62,7 +62,7 @@ private Path decrypt(Path ciphertextPath) {
6262
String cleartext = filenameCryptor.decryptFilename(ciphertext, directoryId.getBytes(StandardCharsets.UTF_8));
6363
return cleartextDir.resolve(cleartext);
6464
} catch (AuthenticationFailedException e) {
65-
LOG.warn(ciphertextPath + " not decryptable.", e);
65+
LOG.warn(ciphertextPath + " not decryptable due to an unauthentic ciphertext.");
6666
return null;
6767
}
6868
} else {

src/main/java/org/cryptomator/cryptofs/CryptoDosFileAttributes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.nio.file.Path;
1212
import java.nio.file.attribute.DosFileAttributes;
1313

14-
import org.cryptomator.cryptolib.FileHeaderCryptor;
14+
import org.cryptomator.cryptolib.api.FileHeaderCryptor;
1515

1616
public class CryptoDosFileAttributes extends CryptoBasicFileAttributes implements DelegatingDosFileAttributes {
1717

src/main/java/org/cryptomator/cryptofs/CryptoFileAttributeProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import java.util.HashMap;
1717
import java.util.Map;
1818

19-
import org.cryptomator.cryptolib.FileHeaderCryptor;
19+
import org.cryptomator.cryptolib.api.FileHeaderCryptor;
2020

2121
class CryptoFileAttributeProvider {
2222

src/main/java/org/cryptomator/cryptofs/CryptoFileChannel.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
*******************************************************************************/
99
package org.cryptomator.cryptofs;
1010

11-
import static org.cryptomator.cryptolib.Constants.CHUNK_SIZE;
12-
import static org.cryptomator.cryptolib.Constants.PAYLOAD_SIZE;
13-
1411
import java.io.IOException;
1512
import java.nio.ByteBuffer;
1613
import java.nio.MappedByteBuffer;
@@ -26,9 +23,9 @@
2623
import java.util.Set;
2724
import java.util.concurrent.ExecutionException;
2825

29-
import org.cryptomator.cryptolib.AuthenticationFailedException;
30-
import org.cryptomator.cryptolib.Cryptor;
31-
import org.cryptomator.cryptolib.FileHeader;
26+
import org.cryptomator.cryptolib.api.AuthenticationFailedException;
27+
import org.cryptomator.cryptolib.api.Cryptor;
28+
import org.cryptomator.cryptolib.api.FileHeader;
3229
import org.slf4j.Logger;
3330
import org.slf4j.LoggerFactory;
3431

@@ -63,7 +60,7 @@ public CryptoFileChannel(Cryptor cryptor, Path ciphertextPath, Set<? extends Ope
6360
if (adjustedOptions.contains(StandardOpenOption.CREATE_NEW) || adjustedOptions.contains(StandardOpenOption.CREATE) && ch.size() == 0) {
6461
header = cryptor.fileHeaderCryptor().create();
6562
} else {
66-
ByteBuffer existingHeaderBuf = ByteBuffer.allocate(FileHeader.SIZE);
63+
ByteBuffer existingHeaderBuf = ByteBuffer.allocate(cryptor.fileHeaderCryptor().headerSize());
6764
ch.position(0);
6865
ch.read(existingHeaderBuf);
6966
existingHeaderBuf.flip();
@@ -77,11 +74,12 @@ public int read(ByteBuffer dst, long position) throws IOException {
7774
int origLimit = dst.limit();
7875
dst.limit((int) Math.min(origLimit, size() - position));
7976
int read = 0;
77+
int payloadSize = cryptor.fileContentCryptor().cleartextChunkSize();
8078
while (dst.hasRemaining()) {
8179
long pos = position + read;
82-
long chunkIndex = pos / PAYLOAD_SIZE;
83-
int offset = (int) pos % PAYLOAD_SIZE;
84-
int len = Math.min(dst.remaining(), PAYLOAD_SIZE - offset);
80+
long chunkIndex = pos / payloadSize;
81+
int offset = (int) pos % payloadSize;
82+
int len = Math.min(dst.remaining(), payloadSize - offset);
8583
final ByteBuffer chunkBuf = loadCleartextChunk(chunkIndex);
8684
chunkBuf.position(offset).limit(Math.min(chunkBuf.limit(), len));
8785
dst.put(chunkBuf);
@@ -94,18 +92,19 @@ public int read(ByteBuffer dst, long position) throws IOException {
9492
@Override
9593
public int write(ByteBuffer src, long position) throws IOException {
9694
int written = 0;
95+
int payloadSize = cryptor.fileContentCryptor().cleartextChunkSize();
9796
while (src.hasRemaining()) {
9897
long pos = position + written;
99-
long chunkIndex = pos / PAYLOAD_SIZE;
100-
int offset = (int) pos % PAYLOAD_SIZE;
101-
int len = Math.min(src.remaining(), PAYLOAD_SIZE - offset);
98+
long chunkIndex = pos / payloadSize;
99+
int offset = (int) pos % payloadSize;
100+
int len = Math.min(src.remaining(), payloadSize - offset);
102101
if (pos + len > size()) {
103102
// append
104103
setSize(pos + len);
105104
}
106-
if (len == PAYLOAD_SIZE) {
105+
if (len == payloadSize) {
107106
// complete chunk, no need to load and decrypt from file:
108-
cleartextChunks.put(chunkIndex, ByteBuffer.allocate(PAYLOAD_SIZE));
107+
cleartextChunks.put(chunkIndex, ByteBuffer.allocate(payloadSize));
109108
}
110109
final ByteBuffer chunkBuf = loadCleartextChunk(chunkIndex);
111110
chunkBuf.position(offset).limit(Math.max(chunkBuf.limit(), len));
@@ -120,16 +119,16 @@ public int write(ByteBuffer src, long position) throws IOException {
120119

121120
@Override
122121
public long size() {
123-
return header.getPayload().getFilesize();
122+
return header.getFilesize();
124123
}
125124

126125
private void setSize(long size) {
127-
header.getPayload().setFilesize(size);
126+
header.setFilesize(size);
128127
}
129128

130129
@Override
131130
public FileChannel truncate(long size) throws IOException {
132-
ch.truncate(FileHeader.SIZE);
131+
ch.truncate(cryptor.fileHeaderCryptor().headerSize());
133132
setSize(Math.min(size, size()));
134133
// TODO Auto-generated method stub
135134
return null;
@@ -195,12 +194,14 @@ private class CleartextChunkLoader extends CacheLoader<Long, ByteBuffer> {
195194
@Override
196195
public ByteBuffer load(Long chunkIndex) throws Exception {
197196
LOG.debug("load chunk" + chunkIndex);
198-
long ciphertextPos = chunkIndex * CHUNK_SIZE + FileHeader.SIZE;
199-
ByteBuffer ciphertextBuf = ByteBuffer.allocate(CHUNK_SIZE);
197+
int payloadSize = cryptor.fileContentCryptor().cleartextChunkSize();
198+
int chunkSize = cryptor.fileContentCryptor().ciphertextChunkSize();
199+
long ciphertextPos = chunkIndex * chunkSize + cryptor.fileHeaderCryptor().headerSize();
200+
ByteBuffer ciphertextBuf = ByteBuffer.allocate(chunkSize);
200201
int read = ch.read(ciphertextBuf, ciphertextPos);
201202
if (read == -1) {
202203
// append
203-
return ByteBuffer.allocate(PAYLOAD_SIZE);
204+
return ByteBuffer.allocate(payloadSize);
204205
} else {
205206
ciphertextBuf.flip();
206207
return cryptor.fileContentCryptor().decryptChunk(ciphertextBuf, chunkIndex, header, true);
@@ -214,9 +215,9 @@ private class CleartextChunkSaver implements RemovalListener<Long, ByteBuffer> {
214215
@Override
215216
public void onRemoval(RemovalNotification<Long, ByteBuffer> notification) {
216217
long chunkIndex = notification.getKey();
217-
if (openOptions.contains(StandardOpenOption.WRITE) && chunkIndex * PAYLOAD_SIZE < size()) {
218+
if (openOptions.contains(StandardOpenOption.WRITE) && chunkIndex * cryptor.fileContentCryptor().cleartextChunkSize() < size()) {
218219
LOG.debug("save chunk" + chunkIndex);
219-
long ciphertextPos = chunkIndex * CHUNK_SIZE + FileHeader.SIZE;
220+
long ciphertextPos = chunkIndex * cryptor.fileContentCryptor().ciphertextChunkSize() + cryptor.fileHeaderCryptor().headerSize();
220221
ByteBuffer cleartextBuf = notification.getValue().asReadOnlyBuffer();
221222
cleartextBuf.flip();
222223
ByteBuffer ciphertextBuf = cryptor.fileContentCryptor().encryptChunk(cleartextBuf, chunkIndex, header);

src/main/java/org/cryptomator/cryptofs/CryptoFileSystem.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
import java.nio.file.spi.FileSystemProvider;
2020
import java.util.Set;
2121

22-
import org.cryptomator.cryptolib.Cryptor;
23-
import org.cryptomator.cryptolib.CryptorProvider;
24-
import org.cryptomator.cryptolib.InvalidPassphraseException;
25-
import org.cryptomator.cryptolib.UnsupportedVaultFormatException;
22+
import org.cryptomator.cryptolib.api.Cryptor;
23+
import org.cryptomator.cryptolib.api.InvalidPassphraseException;
24+
import org.cryptomator.cryptolib.api.KeyFile;
25+
import org.cryptomator.cryptolib.api.UnsupportedVaultFormatException;
26+
import org.cryptomator.cryptolib.v1.CryptorProviderImpl;
2627

2728
class CryptoFileSystem extends BasicFileSystem {
2829

@@ -34,7 +35,7 @@ class CryptoFileSystem extends BasicFileSystem {
3435
private final CryptoPathMapper cryptoPathMapper;
3536
private final CryptoFileAttributeProvider fileAttributeProvider;
3637

37-
public CryptoFileSystem(CryptoFileSystemProvider provider, CryptorProvider cryptorProvider, Path pathToVault, CharSequence passphrase)
38+
public CryptoFileSystem(CryptoFileSystemProvider provider, CryptorProviderImpl cryptorProvider, Path pathToVault, CharSequence passphrase)
3839
throws UnsupportedVaultFormatException, InvalidPassphraseException, UncheckedIOException {
3940
this.provider = provider;
4041
this.pathToVault = pathToVault;
@@ -45,11 +46,11 @@ public CryptoFileSystem(CryptoFileSystemProvider provider, CryptorProvider crypt
4546
Path backupKeyPath = pathToVault.resolve(Constants.BACKUPKEY_FILE_NAME);
4647
if (Files.isRegularFile(masterKeyPath)) {
4748
byte[] keyFileContents = Files.readAllBytes(masterKeyPath);
48-
this.cryptor = cryptorProvider.createFromKeyFile(keyFileContents, passphrase);
49+
this.cryptor = cryptorProvider.createFromKeyFile(KeyFile.parse(keyFileContents), passphrase, Constants.VAULT_VERSION);
4950
Files.copy(masterKeyPath, backupKeyPath, StandardCopyOption.REPLACE_EXISTING);
5051
} else {
5152
this.cryptor = cryptorProvider.createNew();
52-
byte[] keyFileContents = cryptor.writeKeysToMasterkeyFile(passphrase);
53+
byte[] keyFileContents = cryptor.writeKeysToMasterkeyFile(passphrase, Constants.VAULT_VERSION).serialize();
5354
Files.write(masterKeyPath, keyFileContents);
5455
}
5556
} catch (IOException e) {

src/main/java/org/cryptomator/cryptofs/CryptoFileSystemProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
import java.util.concurrent.ExecutorService;
4141

4242
import org.cryptomator.cryptofs.CryptoPathMapper.Directory;
43-
import org.cryptomator.cryptolib.CryptorProvider;
44-
import org.cryptomator.cryptolib.ReseedingSecureRandom;
43+
import org.cryptomator.cryptolib.common.ReseedingSecureRandom;
44+
import org.cryptomator.cryptolib.v1.CryptorProviderImpl;
4545

4646
public class CryptoFileSystemProvider extends FileSystemProvider {
4747

@@ -52,11 +52,11 @@ public class CryptoFileSystemProvider extends FileSystemProvider {
5252

5353
public static final String FS_ENV_PW = "passphrase";
5454

55-
private final CryptorProvider cryptorProvider;
55+
private final CryptorProviderImpl cryptorProvider;
5656
private final ConcurrentHashMap<Path, CryptoFileSystem> fileSystems = new ConcurrentHashMap<>();
5757

5858
public CryptoFileSystemProvider(SecureRandom csprng) {
59-
this.cryptorProvider = new CryptorProvider(csprng);
59+
this.cryptorProvider = new CryptorProviderImpl(csprng);
6060
}
6161

6262
public CryptoFileSystemProvider() {

src/main/java/org/cryptomator/cryptofs/CryptoPathMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.nio.charset.StandardCharsets;
1313
import java.nio.file.Path;
1414

15-
import org.cryptomator.cryptolib.Cryptor;
15+
import org.cryptomator.cryptolib.api.Cryptor;
1616

1717
class CryptoPathMapper {
1818

0 commit comments

Comments
 (0)