From 5d46d3a4b41e6a91ad1846c676ca094efd4be658 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Fri, 9 Jan 2015 13:06:44 +0000
Subject: [PATCH] AuthenticatedCipher interface isn't needed outside crypto
 package.

---
 .../briarproject/api/crypto/CryptoComponent.java    |  3 ---
 .../briarproject}/crypto/AuthenticatedCipher.java   | 12 ++++++------
 .../crypto/AuthenticatedCipherImpl.java             | 13 ++++---------
 .../briarproject/crypto/CryptoComponentImpl.java    | 13 ++-----------
 .../crypto/StreamDecrypterFactoryImpl.java          |  6 ++++--
 .../briarproject/crypto/StreamDecrypterImpl.java    |  1 -
 .../crypto/StreamEncrypterFactoryImpl.java          |  8 ++++----
 .../briarproject/crypto/StreamEncrypterImpl.java    |  1 -
 .../crypto/StreamEncrypterImplTest.java             |  1 -
 .../crypto/TestAuthenticatedCipher.java             |  7 -------
 10 files changed, 20 insertions(+), 45 deletions(-)
 rename {briar-api/src/org/briarproject/api => briar-core/src/org/briarproject}/crypto/AuthenticatedCipher.java (65%)

diff --git a/briar-api/src/org/briarproject/api/crypto/CryptoComponent.java b/briar-api/src/org/briarproject/api/crypto/CryptoComponent.java
index 0f448daef8..6add0417b7 100644
--- a/briar-api/src/org/briarproject/api/crypto/CryptoComponent.java
+++ b/briar-api/src/org/briarproject/api/crypto/CryptoComponent.java
@@ -77,9 +77,6 @@ public interface CryptoComponent {
 	 */
 	SecretKey deriveFrameKey(byte[] secret, long streamNumber, boolean alice);
 
-	/** Returns a cipher for encrypting and authenticating frames. */
-	AuthenticatedCipher getFrameCipher();
-
 	/** Encodes the pseudo-random tag that is used to recognise a stream. */
 	void encodeTag(byte[] tag, SecretKey tagKey, long streamNumber);
 
diff --git a/briar-api/src/org/briarproject/api/crypto/AuthenticatedCipher.java b/briar-core/src/org/briarproject/crypto/AuthenticatedCipher.java
similarity index 65%
rename from briar-api/src/org/briarproject/api/crypto/AuthenticatedCipher.java
rename to briar-core/src/org/briarproject/crypto/AuthenticatedCipher.java
index 8256b31feb..3225b65e49 100644
--- a/briar-api/src/org/briarproject/api/crypto/AuthenticatedCipher.java
+++ b/briar-core/src/org/briarproject/crypto/AuthenticatedCipher.java
@@ -1,11 +1,14 @@
-package org.briarproject.api.crypto;
+package org.briarproject.crypto;
 
 import java.security.GeneralSecurityException;
 
-public interface AuthenticatedCipher {
+import org.briarproject.api.crypto.SecretKey;
+
+interface AuthenticatedCipher {
 
 	/**
-	 * Initializes this cipher with a key and an initialisation vector (IV).
+	 * Initializes this cipher for encryption or decryption with a key and an
+	 * initialisation vector (IV).
 	 */
 	void init(boolean encrypt, SecretKey key, byte[] iv)
 			throws GeneralSecurityException;
@@ -16,7 +19,4 @@ public interface AuthenticatedCipher {
 
 	/** Returns the length of the message authentication code (MAC) in bytes. */
 	int getMacBytes();
-
-	/** Returns the block size of the cipher in bytes. */
-	int getBlockBytes();
 }
diff --git a/briar-core/src/org/briarproject/crypto/AuthenticatedCipherImpl.java b/briar-core/src/org/briarproject/crypto/AuthenticatedCipherImpl.java
index d840313846..671a73e855 100644
--- a/briar-core/src/org/briarproject/crypto/AuthenticatedCipherImpl.java
+++ b/briar-core/src/org/briarproject/crypto/AuthenticatedCipherImpl.java
@@ -1,8 +1,9 @@
 package org.briarproject.crypto;
 
+import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
+
 import java.security.GeneralSecurityException;
 
-import org.briarproject.api.crypto.AuthenticatedCipher;
 import org.briarproject.api.crypto.SecretKey;
 import org.spongycastle.crypto.DataLengthException;
 import org.spongycastle.crypto.InvalidCipherTextException;
@@ -15,8 +16,6 @@ import org.spongycastle.crypto.params.KeyParameter;
 
 class AuthenticatedCipherImpl implements AuthenticatedCipher {
 
-	private static final int MAC_BYTES = 16;
-
 	private final AEADBlockCipher cipher;
 
 	AuthenticatedCipherImpl() {
@@ -44,7 +43,7 @@ class AuthenticatedCipherImpl implements AuthenticatedCipher {
 			throws GeneralSecurityException {
 		KeyParameter k = new KeyParameter(key.getBytes());
 		// Authenticate the IV by passing it as additional authenticated data
-		AEADParameters params = new AEADParameters(k, MAC_BYTES * 8, iv, iv);
+		AEADParameters params = new AEADParameters(k, MAC_LENGTH * 8, iv, iv);
 		try {
 			cipher.init(encrypt, params);
 		} catch(IllegalArgumentException e) {
@@ -53,10 +52,6 @@ class AuthenticatedCipherImpl implements AuthenticatedCipher {
 	}
 
 	public int getMacBytes() {
-		return MAC_BYTES;
-	}
-
-	public int getBlockBytes() {
-		return cipher.getUnderlyingCipher().getBlockSize();
+		return MAC_LENGTH;
 	}
 }
diff --git a/briar-core/src/org/briarproject/crypto/CryptoComponentImpl.java b/briar-core/src/org/briarproject/crypto/CryptoComponentImpl.java
index 5474bcbec8..34a36f0fdc 100644
--- a/briar-core/src/org/briarproject/crypto/CryptoComponentImpl.java
+++ b/briar-core/src/org/briarproject/crypto/CryptoComponentImpl.java
@@ -17,7 +17,6 @@ import java.util.logging.Logger;
 
 import javax.inject.Inject;
 
-import org.briarproject.api.crypto.AuthenticatedCipher;
 import org.briarproject.api.crypto.CryptoComponent;
 import org.briarproject.api.crypto.KeyPair;
 import org.briarproject.api.crypto.KeyParser;
@@ -290,14 +289,6 @@ class CryptoComponentImpl implements CryptoComponent {
 		return new SecretKey(counterModeKdf(secret, label, context));
 	}
 
-	public AuthenticatedCipher getFrameCipher() {
-		return getAuthenticatedCipher();
-	}
-
-	private AuthenticatedCipher getAuthenticatedCipher() {
-		return new AuthenticatedCipherImpl();
-	}
-
 	public void encodeTag(byte[] tag, SecretKey tagKey, long streamNumber) {
 		if(tag.length < TAG_LENGTH) throw new IllegalArgumentException();
 		if(streamNumber < 0 || streamNumber > MAX_32_BIT_UNSIGNED)
@@ -312,7 +303,7 @@ class CryptoComponentImpl implements CryptoComponent {
 	}
 
 	public byte[] encryptWithPassword(byte[] input, String password) {
-		AuthenticatedCipher cipher = getAuthenticatedCipher();
+		AuthenticatedCipher cipher = new AuthenticatedCipherImpl();
 		int macBytes = cipher.getMacBytes();
 		// Generate a random salt
 		byte[] salt = new byte[PBKDF_SALT_BYTES];
@@ -342,7 +333,7 @@ class CryptoComponentImpl implements CryptoComponent {
 	}
 
 	public byte[] decryptWithPassword(byte[] input, String password) {
-		AuthenticatedCipher cipher = getAuthenticatedCipher();
+		AuthenticatedCipher cipher = new AuthenticatedCipherImpl();
 		int macBytes = cipher.getMacBytes();
 		// The input contains the salt, iterations, IV, ciphertext and MAC
 		if(input.length < PBKDF_SALT_BYTES + 4 + STORAGE_IV_BYTES + macBytes)
diff --git a/briar-core/src/org/briarproject/crypto/StreamDecrypterFactoryImpl.java b/briar-core/src/org/briarproject/crypto/StreamDecrypterFactoryImpl.java
index bed0503c24..23ea5e245c 100644
--- a/briar-core/src/org/briarproject/crypto/StreamDecrypterFactoryImpl.java
+++ b/briar-core/src/org/briarproject/crypto/StreamDecrypterFactoryImpl.java
@@ -27,7 +27,8 @@ class StreamDecrypterFactoryImpl implements StreamDecrypterFactory {
 		boolean alice = !ctx.getAlice();
 		SecretKey frameKey = crypto.deriveFrameKey(secret, streamNumber, alice);
 		// Create the decrypter
-		return new StreamDecrypterImpl(in, crypto.getFrameCipher(), frameKey);
+		AuthenticatedCipher cipher = new AuthenticatedCipherImpl();
+		return new StreamDecrypterImpl(in, cipher, frameKey);
 	}
 
 	public StreamDecrypter createInvitationStreamDecrypter(InputStream in,
@@ -35,6 +36,7 @@ class StreamDecrypterFactoryImpl implements StreamDecrypterFactory {
 		// Derive the frame key
 		SecretKey frameKey = crypto.deriveFrameKey(secret, 0, alice);
 		// Create the decrypter
-		return new StreamDecrypterImpl(in, crypto.getFrameCipher(), frameKey);
+		AuthenticatedCipher cipher = new AuthenticatedCipherImpl();
+		return new StreamDecrypterImpl(in, cipher, frameKey);
 	}
 }
diff --git a/briar-core/src/org/briarproject/crypto/StreamDecrypterImpl.java b/briar-core/src/org/briarproject/crypto/StreamDecrypterImpl.java
index e950fff2f6..3e3e240552 100644
--- a/briar-core/src/org/briarproject/crypto/StreamDecrypterImpl.java
+++ b/briar-core/src/org/briarproject/crypto/StreamDecrypterImpl.java
@@ -12,7 +12,6 @@ import java.io.InputStream;
 import java.security.GeneralSecurityException;
 
 import org.briarproject.api.FormatException;
-import org.briarproject.api.crypto.AuthenticatedCipher;
 import org.briarproject.api.crypto.SecretKey;
 import org.briarproject.api.crypto.StreamDecrypter;
 
diff --git a/briar-core/src/org/briarproject/crypto/StreamEncrypterFactoryImpl.java b/briar-core/src/org/briarproject/crypto/StreamEncrypterFactoryImpl.java
index ec8286e061..ae0429e05e 100644
--- a/briar-core/src/org/briarproject/crypto/StreamEncrypterFactoryImpl.java
+++ b/briar-core/src/org/briarproject/crypto/StreamEncrypterFactoryImpl.java
@@ -33,8 +33,8 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
 		// Derive the frame key
 		SecretKey frameKey = crypto.deriveFrameKey(secret, streamNumber, alice);
 		// Create the encrypter
-		return new StreamEncrypterImpl(out, crypto.getFrameCipher(), frameKey,
-				tag);
+		AuthenticatedCipher cipher = new AuthenticatedCipherImpl();
+		return new StreamEncrypterImpl(out, cipher, frameKey, tag);
 	}
 
 	public StreamEncrypter createInvitationStreamEncrypter(OutputStream out,
@@ -42,7 +42,7 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
 		// Derive the frame key
 		SecretKey frameKey = crypto.deriveFrameKey(secret, 0, alice);
 		// Create the encrypter
-		return new StreamEncrypterImpl(out, crypto.getFrameCipher(), frameKey,
-				null);
+		AuthenticatedCipher cipher = new AuthenticatedCipherImpl();
+		return new StreamEncrypterImpl(out, cipher, frameKey, null);
 	}
 }
diff --git a/briar-core/src/org/briarproject/crypto/StreamEncrypterImpl.java b/briar-core/src/org/briarproject/crypto/StreamEncrypterImpl.java
index ce06b09c41..118648a2b7 100644
--- a/briar-core/src/org/briarproject/crypto/StreamEncrypterImpl.java
+++ b/briar-core/src/org/briarproject/crypto/StreamEncrypterImpl.java
@@ -11,7 +11,6 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.security.GeneralSecurityException;
 
-import org.briarproject.api.crypto.AuthenticatedCipher;
 import org.briarproject.api.crypto.SecretKey;
 import org.briarproject.api.crypto.StreamEncrypter;
 
diff --git a/briar-tests/src/org/briarproject/crypto/StreamEncrypterImplTest.java b/briar-tests/src/org/briarproject/crypto/StreamEncrypterImplTest.java
index a874314165..922d1fa17c 100644
--- a/briar-tests/src/org/briarproject/crypto/StreamEncrypterImplTest.java
+++ b/briar-tests/src/org/briarproject/crypto/StreamEncrypterImplTest.java
@@ -9,7 +9,6 @@ import java.io.ByteArrayOutputStream;
 import java.util.Random;
 
 import org.briarproject.BriarTestCase;
-import org.briarproject.api.crypto.AuthenticatedCipher;
 import org.briarproject.api.crypto.SecretKey;
 import org.junit.Test;
 
diff --git a/briar-tests/src/org/briarproject/crypto/TestAuthenticatedCipher.java b/briar-tests/src/org/briarproject/crypto/TestAuthenticatedCipher.java
index 018db0b6c2..3861318177 100644
--- a/briar-tests/src/org/briarproject/crypto/TestAuthenticatedCipher.java
+++ b/briar-tests/src/org/briarproject/crypto/TestAuthenticatedCipher.java
@@ -4,13 +4,10 @@ import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
 
 import java.security.GeneralSecurityException;
 
-import org.briarproject.api.crypto.AuthenticatedCipher;
 import org.briarproject.api.crypto.SecretKey;
 
 class TestAuthenticatedCipher implements AuthenticatedCipher {
 
-	private static final int BLOCK_BYTES = 16;
-
 	private boolean encrypt = false;
 
 	public void init(boolean encrypt, SecretKey key, byte[] iv)
@@ -38,8 +35,4 @@ class TestAuthenticatedCipher implements AuthenticatedCipher {
 	public int getMacBytes() {
 		return MAC_LENGTH;
 	}
-
-	public int getBlockBytes() {
-		return BLOCK_BYTES;
-	}
 }
-- 
GitLab