Skip to content

Commit cad2d7d

Browse files
Merge branch 'hotfix/2.0.1'
2 parents 8e5a822 + 7b180ca commit cad2d7d

File tree

9 files changed

+43
-38
lines changed

9 files changed

+43
-38
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>org.cryptomator</groupId>
44
<artifactId>cryptolib</artifactId>
5-
<version>2.0.0</version>
5+
<version>2.0.1</version>
66
<name>Cryptomator Crypto Library</name>
77
<description>This library contains all cryptographic functions that are used by Cryptomator.</description>
88
<url>https://github.com/cryptomator/cryptolib</url>

src/main/java/org/cryptomator/cryptolib/api/Masterkey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static Masterkey from(DestroyableSecretKey encKey, DestroyableSecretKey m
4646
}
4747

4848
@Override
49-
public Masterkey clone() {
49+
public Masterkey copy() {
5050
return new Masterkey(getEncoded());
5151
}
5252

src/main/java/org/cryptomator/cryptolib/common/AesKeyWrap.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public class AesKeyWrap {
2222
* @return Wrapped key
2323
*/
2424
public static byte[] wrap(DestroyableSecretKey kek, SecretKey key) {
25-
try {
26-
final Cipher cipher = CipherSupplier.RFC3394_KEYWRAP.forWrapping(kek);
25+
try (DestroyableSecretKey kekCopy = kek.copy()) {
26+
final Cipher cipher = CipherSupplier.RFC3394_KEYWRAP.forWrapping(kekCopy);
2727
return cipher.wrap(key);
2828
} catch (InvalidKeyException | IllegalBlockSizeException e) {
2929
throw new IllegalArgumentException("Unable to wrap key.", e);
@@ -43,8 +43,8 @@ public static DestroyableSecretKey unwrap(DestroyableSecretKey kek, byte[] wrapp
4343

4444
// visible for testing
4545
static DestroyableSecretKey unwrap(DestroyableSecretKey kek, byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType) throws InvalidKeyException {
46-
final Cipher cipher = CipherSupplier.RFC3394_KEYWRAP.forUnwrapping(kek);
47-
try {
46+
try (DestroyableSecretKey kekCopy = kek.copy()) {
47+
final Cipher cipher = CipherSupplier.RFC3394_KEYWRAP.forUnwrapping(kekCopy);
4848
return DestroyableSecretKey.from(cipher.unwrap(wrappedKey, wrappedKeyAlgorithm, wrappedKeyType));
4949
} catch (NoSuchAlgorithmException e) {
5050
throw new IllegalArgumentException("Invalid algorithm: " + wrappedKeyAlgorithm, e);

src/main/java/org/cryptomator/cryptolib/common/CipherSupplier.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111
import javax.crypto.Cipher;
1212
import javax.crypto.NoSuchPaddingException;
13+
import javax.crypto.SecretKey;
1314
import java.security.InvalidAlgorithmParameterException;
1415
import java.security.InvalidKeyException;
1516
import java.security.NoSuchAlgorithmException;
1617
import java.security.spec.AlgorithmParameterSpec;
18+
import java.util.function.Function;
1719

1820
public final class CipherSupplier {
1921

@@ -41,27 +43,27 @@ protected Cipher initialValue() {
4143
}
4244
}
4345

44-
public Cipher forEncryption(DestroyableSecretKey key, AlgorithmParameterSpec params) {
46+
public Cipher forEncryption(SecretKey key, AlgorithmParameterSpec params) {
4547
return forMode(Cipher.ENCRYPT_MODE, key, params);
4648
}
4749

48-
public Cipher forDecryption(DestroyableSecretKey key, AlgorithmParameterSpec params) {
50+
public Cipher forDecryption(SecretKey key, AlgorithmParameterSpec params) {
4951
return forMode(Cipher.DECRYPT_MODE, key, params);
5052
}
5153

52-
public Cipher forWrapping(DestroyableSecretKey kek) {
54+
public Cipher forWrapping(SecretKey kek) {
5355
return forMode(Cipher.WRAP_MODE, kek, null);
5456
}
5557

56-
public Cipher forUnwrapping(DestroyableSecretKey kek) {
58+
public Cipher forUnwrapping(SecretKey kek) {
5759
return forMode(Cipher.UNWRAP_MODE, kek, null);
5860
}
5961

6062
// visible for testing
61-
Cipher forMode(int ciphermode, DestroyableSecretKey key, AlgorithmParameterSpec params) {
63+
Cipher forMode(int ciphermode, SecretKey key, AlgorithmParameterSpec params) {
6264
final Cipher cipher = threadLocal.get();
63-
try (DestroyableSecretKey clone = key.clone()) {
64-
cipher.init(ciphermode, clone, params); // use cloned key, as this may destroy key.getEncoded()
65+
try {
66+
cipher.init(ciphermode, key, params);
6567
return cipher;
6668
} catch (InvalidKeyException e) {
6769
throw new IllegalArgumentException("Invalid key.", e);

src/main/java/org/cryptomator/cryptolib/common/DestroyableSecretKey.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
* actually implements {@link Destroyable}.
1616
* <p>
1717
* Furthermore, this implementation will not create copies when accessing {@link #getEncoded()}.
18-
* Instead it implements {@link AutoCloseable} and {@link Cloneable} in an exception-free manner. To prevent mutation of the exposed key,
18+
* Instead it implements {@link #copy} and {@link AutoCloseable} in an exception-free manner. To prevent mutation of the exposed key,
1919
* you would want to make sure to always work on scoped copies, such as in this example:
2020
*
2121
* <pre>
22-
* // clone "key" to protect it from unwanted modifications:
23-
* try (DestroyableSecretKey k = key.clone()) {
22+
* // copy "key" to protect it from unwanted modifications:
23+
* try (DestroyableSecretKey k = key.copy()) {
2424
* // use "k":
2525
* Cipher cipher = Cipher.init(k, ...)
2626
* cipher.doFinal(...)
2727
* } // "k" will get destroyed here
2828
* </pre>
2929
*/
30-
public class DestroyableSecretKey implements SecretKey, AutoCloseable, Cloneable {
30+
public class DestroyableSecretKey implements SecretKey, AutoCloseable {
3131

3232
private transient final byte[] key;
3333
private final String algorithm;
@@ -109,7 +109,7 @@ public String getFormat() {
109109
* Returns the raw key bytes this instance wraps.
110110
* <p>
111111
* <b>Important:</b> Any change to the returned array will reflect in this key. Make sure to
112-
* {@link #clone() make a local copy} if you can't rule out mutations.
112+
* {@link #copy() make a local copy} if you can't rule out mutations.
113113
*
114114
* @return A byte array holding the secret key
115115
*/
@@ -119,8 +119,11 @@ public byte[] getEncoded() {
119119
return key;
120120
}
121121

122-
@Override
123-
public DestroyableSecretKey clone() {
122+
/**
123+
* Returns an independent copy of this key
124+
* @return New copy of <code>this</code>
125+
*/
126+
public DestroyableSecretKey copy() {
124127
Preconditions.checkState(!destroyed, "Key has been destroyed");
125128
return new DestroyableSecretKey(key, algorithm); // key will get copied by the constructor as per contract
126129
}

src/main/java/org/cryptomator/cryptolib/v1/FileContentCryptorImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void decryptChunk(ByteBuffer ciphertextChunk, ByteBuffer cleartextChunk,
103103

104104
// visible for testing
105105
void encryptChunk(ByteBuffer cleartextChunk, ByteBuffer ciphertextChunk, long chunkNumber, byte[] headerNonce, DestroyableSecretKey fileKey) {
106-
try (DestroyableSecretKey fk = fileKey.clone()) {
106+
try (DestroyableSecretKey fk = fileKey.copy()) {
107107
// nonce:
108108
byte[] nonce = new byte[NONCE_SIZE];
109109
random.nextBytes(nonce);
@@ -131,7 +131,7 @@ void encryptChunk(ByteBuffer cleartextChunk, ByteBuffer ciphertextChunk, long ch
131131
void decryptChunk(ByteBuffer ciphertextChunk, ByteBuffer cleartextChunk, DestroyableSecretKey fileKey) {
132132
assert ciphertextChunk.remaining() >= NONCE_SIZE + MAC_SIZE;
133133

134-
try (DestroyableSecretKey fk = fileKey.clone()) {
134+
try (DestroyableSecretKey fk = fileKey.copy()) {
135135
// nonce:
136136
final byte[] nonce = new byte[NONCE_SIZE];
137137
final ByteBuffer chunkNonceBuf = ciphertextChunk.asReadOnlyBuffer();

src/main/java/org/cryptomator/cryptolib/v2/FileContentCryptorImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void decryptChunk(ByteBuffer ciphertextChunk, ByteBuffer cleartextChunk,
9797

9898
// visible for testing
9999
void encryptChunk(ByteBuffer cleartextChunk, ByteBuffer ciphertextChunk, long chunkNumber, byte[] headerNonce, DestroyableSecretKey fileKey) {
100-
try (DestroyableSecretKey fk = fileKey.clone()) {
100+
try (DestroyableSecretKey fk = fileKey.copy()) {
101101
// nonce:
102102
byte[] nonce = new byte[GCM_NONCE_SIZE];
103103
random.nextBytes(nonce);
@@ -121,7 +121,7 @@ void encryptChunk(ByteBuffer cleartextChunk, ByteBuffer ciphertextChunk, long ch
121121
void decryptChunk(ByteBuffer ciphertextChunk, ByteBuffer cleartextChunk, long chunkNumber, byte[] headerNonce, DestroyableSecretKey fileKey) throws AuthenticationFailedException {
122122
assert ciphertextChunk.remaining() >= GCM_NONCE_SIZE + GCM_TAG_SIZE;
123123

124-
try (DestroyableSecretKey fk = fileKey.clone()) {
124+
try (DestroyableSecretKey fk = fileKey.copy()) {
125125
// nonce:
126126
final byte[] nonce = new byte[GCM_NONCE_SIZE];
127127
final ByteBuffer chunkNonceBuf = ciphertextChunk.asReadOnlyBuffer();

src/test/java/org/cryptomator/cryptolib/common/DestroyableSecretKeyTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void testConstructorCreatesLocalCopy() {
7474
}
7575

7676
@Test
77-
public void testConstructorClonesKey() {
77+
public void testConstructorCopiesKey() {
7878
byte[] empty = new byte[32];
7979
byte[] rawKey = new byte[32];
8080
new Random(42).nextBytes(rawKey);
@@ -135,12 +135,12 @@ public void testGetEncoded() {
135135
}
136136

137137
@Test
138-
@DisplayName("clone() returns equal copy")
139-
public void testClone() {
140-
DestroyableSecretKey clone = key.clone();
138+
@DisplayName("copy() returns equal copy")
139+
public void testCopy() {
140+
DestroyableSecretKey copy = key.copy();
141141

142-
Assertions.assertEquals(key, clone);
143-
Assertions.assertNotSame(key, clone);
142+
Assertions.assertEquals(key, copy);
143+
Assertions.assertNotSame(key, copy);
144144
}
145145

146146
@Test
@@ -194,9 +194,9 @@ public void testGetEncoded() {
194194
}
195195

196196
@Test
197-
@DisplayName("clone() throws IllegalStateException")
198-
public void testClone() {
199-
Assertions.assertThrows(IllegalStateException.class, key::clone);
197+
@DisplayName("copy() throws IllegalStateException")
198+
public void testCopy() {
199+
Assertions.assertThrows(IllegalStateException.class, key::copy);
200200
}
201201

202202
}

src/test/java/org/cryptomator/cryptolib/common/MasterkeyTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ public void testGetMacKey() {
6767
}
6868

6969
@Test
70-
public void testClone() {
70+
public void testCopy() {
7171
byte[] raw = new byte[64];
7272
Arrays.fill(raw, (byte) 0x55);
7373
Masterkey original = new Masterkey(raw);
7474

75-
Masterkey clone = original.clone();
75+
Masterkey copy = original.copy();
7676

77-
Assertions.assertEquals(original, clone);
78-
clone.destroy();
79-
Assertions.assertNotEquals(original, clone);
77+
Assertions.assertEquals(original, copy);
78+
copy.destroy();
79+
Assertions.assertNotEquals(original, copy);
8080
}
8181

8282
}

0 commit comments

Comments
 (0)