Skip to content

Commit 20a705f

Browse files
clean up legacy code, simplify API
1 parent c907ae7 commit 20a705f

File tree

10 files changed

+331
-754
lines changed

10 files changed

+331
-754
lines changed

src/main/java/org/cryptomator/siv/CMac.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ class CMac extends MacSpi {
2424
private static final String AES_ALGORITHM = "AES";
2525
private static final String AES_ECB_NO_PADDING = "AES/ECB/NoPadding";
2626

27-
private static final ThreadLocal<Cipher> AES = ThreadLocals.withInitial(() -> {
28-
try {
29-
return Cipher.getInstance(AES_ECB_NO_PADDING);
30-
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
31-
throw new AssertionError("Every implementation of the Java platform is required to support [...] AES/ECB/NoPadding", e);
32-
}
33-
});
34-
3527
// MAC keys:
3628
private Cipher cipher;
3729
private byte[] k1;
@@ -40,8 +32,8 @@ class CMac extends MacSpi {
4032
// MAC state:
4133
private final byte[] buffer = new byte[BLOCK_SIZE];
4234
private int bufferPos = 0;
43-
private byte[] x = new byte[BLOCK_SIZE]; // X := const_Zero;
44-
private byte[] y = new byte[BLOCK_SIZE];
35+
private final byte[] x = new byte[BLOCK_SIZE]; // X := const_Zero;
36+
private final byte[] y = new byte[BLOCK_SIZE];
4537
private int msgLen = 0;
4638

4739
@Override
@@ -51,12 +43,11 @@ protected int engineGetMacLength() {
5143

5244
@Override
5345
protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException {
54-
this.cipher = AES.get();
55-
// try {
56-
// this.cipher = Cipher.getInstance(AES_ECB_NO_PADDING);
57-
// } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
58-
// throw new AssertionError("Every implementation of the Java platform is required to support [...] AES/ECB/NoPadding", e);
59-
// }
46+
try {
47+
this.cipher = Cipher.getInstance(AES_ECB_NO_PADDING);
48+
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
49+
throw new AssertionError("Every implementation of the Java platform is required to support [...] AES/ECB/NoPadding", e);
50+
}
6051
cipher.init(Cipher.ENCRYPT_MODE, key);
6152

6253
// init subkeys K1 and K2
@@ -150,7 +141,6 @@ protected void engineReset() {
150141
Arrays.fill(y, (byte) 0);
151142
}
152143

153-
// TODO make instance method, remove cipher param?
154144
private static void encryptBlock(Cipher cipher, byte[] block, byte[] output) {
155145
try {
156146
cipher.doFinal(block, 0, BLOCK_SIZE, output);

src/main/java/org/cryptomator/siv/JceAesCtrComputer.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/main/java/org/cryptomator/siv/SivMode.java

Lines changed: 52 additions & 160 deletions
Large diffs are not rendered by default.

src/main/java/org/cryptomator/siv/ThreadLocals.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/main/java/org/cryptomator/siv/package-info.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Java implementation of RFC 5297 SIV Authenticated Encryption.
33
* <p>
44
* Use an instance of the {@link org.cryptomator.siv.SivMode} class to
5-
* {@link org.cryptomator.siv.SivMode#encrypt(javax.crypto.SecretKey, javax.crypto.SecretKey, byte[], byte[]...) encrypt} or
6-
* {@link org.cryptomator.siv.SivMode#decrypt(javax.crypto.SecretKey, javax.crypto.SecretKey, byte[], byte[]...) decrypt} data.
5+
* {@link org.cryptomator.siv.SivMode#encrypt(byte[], byte[]...) encrypt} or
6+
* {@link org.cryptomator.siv.SivMode#decrypt(byte[], byte[]...) decrypt} data.
77
*/
88
package org.cryptomator.siv;

src/main/java9/org.cryptomator.siv/ThreadLocals.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/test/java/org/cryptomator/siv/EncryptionTestCase.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public int getTestCaseNumber() {
5050
return testCaseNumber;
5151
}
5252

53+
public byte[] getKey() {
54+
byte[] key = new byte[macKey.length + ctrKey.length];
55+
System.arraycopy(macKey, 0, key, 0, macKey.length);
56+
System.arraycopy(ctrKey, 0, key, macKey.length, ctrKey.length);
57+
return key;
58+
}
59+
5360
public byte[] getCtrKey() {
5461
return Arrays.copyOf(ctrKey, ctrKey.length);
5562
}

src/test/java/org/cryptomator/siv/JceAesCtrComputerTest.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/test/java/org/cryptomator/siv/SivModeBenchmark.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,25 @@
2828
public class SivModeBenchmark {
2929

3030
private int run;
31-
private final byte[] encKey = new byte[16];
32-
private final byte[] macKey = new byte[16];
31+
private final byte[] key = new byte[32];
3332
private final byte[] cleartextData = new byte[1000];
3433
private final byte[] associatedData = new byte[100];
3534

36-
private final SivMode jceSivMode = new SivMode();
35+
private SivMode siv;
3736

3837
@Setup(Level.Trial)
3938
public void shuffleData() {
4039
run++;
41-
Arrays.fill(encKey, (byte) (run & 0xFF));
42-
Arrays.fill(macKey, (byte) (run & 0xFF));
40+
Arrays.fill(key, (byte) (run & 0xFF));
41+
siv = new SivMode(key);
4342
Arrays.fill(cleartextData, (byte) (run & 0xFF));
4443
Arrays.fill(associatedData, (byte) (run & 0xFF));
4544
}
4645

4746
@Benchmark
4847
public void benchmarkJce(Blackhole bh) throws UnauthenticCiphertextException, IllegalBlockSizeException {
49-
byte[] encrypted = jceSivMode.encrypt(encKey, macKey, cleartextData, associatedData);
50-
byte[] decrypted = jceSivMode.decrypt(encKey, macKey, encrypted, associatedData);
48+
byte[] encrypted = siv.encrypt(cleartextData, associatedData);
49+
byte[] decrypted = siv.decrypt(encrypted, associatedData);
5150
Assertions.assertArrayEquals(cleartextData, decrypted);
5251
bh.consume(encrypted);
5352
bh.consume(decrypted);

0 commit comments

Comments
 (0)