From ab722f9371942fd862c10aa274ab24bdf6ffb70c Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Fri, 2 Dec 2011 16:32:50 +0000 Subject: [PATCH] Minor refactoring. --- .../briar/transport/ConnectionDecrypter.java | 4 +- .../transport/ConnectionDecrypterImpl.java | 40 +++++++------------ .../briar/transport/ConnectionEncrypter.java | 4 +- .../transport/ConnectionEncrypterImpl.java | 24 +++++------ .../briar/transport/ConnectionReaderImpl.java | 2 +- .../briar/transport/ConnectionWriterImpl.java | 2 +- .../transport/PaddedConnectionWriter.java | 2 +- .../ConnectionDecrypterImplTest.java | 4 +- .../ConnectionEncrypterImplTest.java | 4 +- .../transport/NullConnectionDecrypter.java | 2 +- .../transport/NullConnectionEncrypter.java | 2 +- 11 files changed, 37 insertions(+), 53 deletions(-) diff --git a/components/net/sf/briar/transport/ConnectionDecrypter.java b/components/net/sf/briar/transport/ConnectionDecrypter.java index 80bd798eee..b4763679b6 100644 --- a/components/net/sf/briar/transport/ConnectionDecrypter.java +++ b/components/net/sf/briar/transport/ConnectionDecrypter.java @@ -9,6 +9,6 @@ interface ConnectionDecrypter { /** Returns an input stream from which decrypted data can be read. */ InputStream getInputStream(); - /** Reads and decrypts the MAC for the current frame. */ - void readMac(byte[] mac) throws IOException; + /** Reads and decrypts the remainder of the current frame. */ + void readFinal(byte[] b) throws IOException; } diff --git a/components/net/sf/briar/transport/ConnectionDecrypterImpl.java b/components/net/sf/briar/transport/ConnectionDecrypterImpl.java index ac9557c88c..ff354a57b6 100644 --- a/components/net/sf/briar/transport/ConnectionDecrypterImpl.java +++ b/components/net/sf/briar/transport/ConnectionDecrypterImpl.java @@ -6,13 +6,9 @@ import java.io.EOFException; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; -import java.security.InvalidAlgorithmParameterException; -import java.security.InvalidKeyException; +import java.security.GeneralSecurityException; -import javax.crypto.BadPaddingException; import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.ShortBufferException; import javax.crypto.spec.IvParameterSpec; import net.sf.briar.api.crypto.ErasableKey; @@ -41,29 +37,25 @@ implements ConnectionDecrypter { return this; } - public void readMac(byte[] mac) throws IOException { + public void readFinal(byte[] b) throws IOException { try { if(betweenFrames) throw new IllegalStateException(); - // If we have any plaintext in the buffer, copy it into the MAC - System.arraycopy(buf, bufOff, mac, 0, bufLen); - // Read the remainder of the MAC + // If we have any plaintext in the buffer, copy it into the frame + System.arraycopy(buf, bufOff, b, 0, bufLen); + // Read the remainder of the frame int offset = bufLen; - while(offset < mac.length) { - int read = in.read(mac, offset, mac.length - offset); + while(offset < b.length) { + int read = in.read(b, offset, b.length - offset); if(read == -1) break; offset += read; } - if(offset < mac.length) throw new EOFException(); // Unexpected EOF - // Decrypt the remainder of the MAC + if(offset < b.length) throw new EOFException(); // Unexpected EOF + // Decrypt the remainder of the frame try { - int length = mac.length - bufLen; - int i = frameCipher.doFinal(mac, bufLen, length, mac, bufLen); + int length = b.length - bufLen; + int i = frameCipher.doFinal(b, bufLen, length, b, bufLen); if(i < length) throw new RuntimeException(); - } catch(BadPaddingException badCipher) { - throw new RuntimeException(badCipher); - } catch(IllegalBlockSizeException badCipher) { - throw new RuntimeException(badCipher); - } catch(ShortBufferException badCipher) { + } catch(GeneralSecurityException badCipher) { throw new RuntimeException(badCipher); } bufOff = bufLen = 0; @@ -140,7 +132,7 @@ implements ConnectionDecrypter { try { int i = frameCipher.update(buf, 0, offset, buf); if(i < offset) throw new RuntimeException(); - } catch(ShortBufferException badCipher) { + } catch(GeneralSecurityException badCipher) { throw new RuntimeException(badCipher); } return true; @@ -153,10 +145,8 @@ implements ConnectionDecrypter { IvParameterSpec ivSpec = new IvParameterSpec(iv); try { frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec); - } catch(InvalidAlgorithmParameterException badIv) { - throw new RuntimeException(badIv); - } catch(InvalidKeyException badKey) { - throw new RuntimeException(badKey); + } catch(GeneralSecurityException badIvOrKey) { + throw new RuntimeException(badIvOrKey); } frame++; betweenFrames = false; diff --git a/components/net/sf/briar/transport/ConnectionEncrypter.java b/components/net/sf/briar/transport/ConnectionEncrypter.java index 304cc1672b..dd9d1334fd 100644 --- a/components/net/sf/briar/transport/ConnectionEncrypter.java +++ b/components/net/sf/briar/transport/ConnectionEncrypter.java @@ -9,8 +9,8 @@ interface ConnectionEncrypter { /** Returns an output stream to which unencrypted data can be written. */ OutputStream getOutputStream(); - /** Encrypts and writes the MAC for the current frame. */ - void writeMac(byte[] mac) throws IOException; + /** Encrypts and writes the remainder of the current frame. */ + void writeFinal(byte[] b) throws IOException; /** Returns the maximum number of bytes that can be written. */ long getRemainingCapacity(); diff --git a/components/net/sf/briar/transport/ConnectionEncrypterImpl.java b/components/net/sf/briar/transport/ConnectionEncrypterImpl.java index 54446b09e2..cb0feb08ba 100644 --- a/components/net/sf/briar/transport/ConnectionEncrypterImpl.java +++ b/components/net/sf/briar/transport/ConnectionEncrypterImpl.java @@ -6,15 +6,13 @@ import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED; import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.security.InvalidAlgorithmParameterException; -import java.security.InvalidKeyException; +import java.security.GeneralSecurityException; -import javax.crypto.BadPaddingException; import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import net.sf.briar.api.crypto.ErasableKey; import javax.crypto.spec.IvParameterSpec; +import net.sf.briar.api.crypto.ErasableKey; + class ConnectionEncrypterImpl extends FilterOutputStream implements ConnectionEncrypter { @@ -42,17 +40,15 @@ implements ConnectionEncrypter { return this; } - public void writeMac(byte[] mac) throws IOException { + public void writeFinal(byte[] b) throws IOException { try { if(!tagWritten || betweenFrames) throw new IllegalStateException(); try { - out.write(frameCipher.doFinal(mac)); - } catch(BadPaddingException badCipher) { - throw new RuntimeException(badCipher); - } catch(IllegalBlockSizeException badCipher) { + out.write(frameCipher.doFinal(b)); + } catch(GeneralSecurityException badCipher) { throw new RuntimeException(badCipher); } - capacity -= mac.length; + capacity -= b.length; betweenFrames = true; } catch(IOException e) { frameKey.erase(); @@ -114,10 +110,8 @@ implements ConnectionEncrypter { IvParameterSpec ivSpec = new IvParameterSpec(iv); try { frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); - } catch(InvalidAlgorithmParameterException badIv) { - throw new RuntimeException(badIv); - } catch(InvalidKeyException badKey) { - throw new RuntimeException(badKey); + } catch(GeneralSecurityException badIvOrKey) { + throw new RuntimeException(badIvOrKey); } frame++; betweenFrames = false; diff --git a/components/net/sf/briar/transport/ConnectionReaderImpl.java b/components/net/sf/briar/transport/ConnectionReaderImpl.java index 258eb824ee..880119052d 100644 --- a/components/net/sf/briar/transport/ConnectionReaderImpl.java +++ b/components/net/sf/briar/transport/ConnectionReaderImpl.java @@ -128,7 +128,7 @@ implements ConnectionReader { } // Read the MAC byte[] expectedMac = mac.doFinal(); - decrypter.readMac(footer); + decrypter.readFinal(footer); if(!Arrays.equals(expectedMac, footer)) throw new FormatException(); frame++; if(payloadLen > 0) betweenFrames = false; diff --git a/components/net/sf/briar/transport/ConnectionWriterImpl.java b/components/net/sf/briar/transport/ConnectionWriterImpl.java index 4f1c790769..673a7896be 100644 --- a/components/net/sf/briar/transport/ConnectionWriterImpl.java +++ b/components/net/sf/briar/transport/ConnectionWriterImpl.java @@ -103,7 +103,7 @@ implements ConnectionWriter { mac.update(header); out.write(payload); mac.update(payload); - encrypter.writeMac(mac.doFinal()); + encrypter.writeFinal(mac.doFinal()); frame++; buf.reset(); } diff --git a/components/net/sf/briar/transport/PaddedConnectionWriter.java b/components/net/sf/briar/transport/PaddedConnectionWriter.java index ae61f11bfa..6bc7c2762b 100644 --- a/components/net/sf/briar/transport/PaddedConnectionWriter.java +++ b/components/net/sf/briar/transport/PaddedConnectionWriter.java @@ -99,7 +99,7 @@ class PaddedConnectionWriter extends ConnectionWriterImpl { mac.update(payload); out.write(padding, 0, paddingLength); mac.update(padding, 0, paddingLength); - encrypter.writeMac(mac.doFinal()); + encrypter.writeFinal(mac.doFinal()); frame++; buf.reset(); } diff --git a/test/net/sf/briar/transport/ConnectionDecrypterImplTest.java b/test/net/sf/briar/transport/ConnectionDecrypterImplTest.java index e8a13dc3f3..086adca1ce 100644 --- a/test/net/sf/briar/transport/ConnectionDecrypterImplTest.java +++ b/test/net/sf/briar/transport/ConnectionDecrypterImplTest.java @@ -80,12 +80,12 @@ public class ConnectionDecrypterImplTest extends TestCase { byte[] decrypted = new byte[ciphertext.length]; TestUtils.readFully(d.getInputStream(), decrypted); byte[] decryptedMac = new byte[MAC_LENGTH]; - d.readMac(decryptedMac); + d.readFinal(decryptedMac); // Second frame byte[] decrypted1 = new byte[ciphertext1.length]; TestUtils.readFully(d.getInputStream(), decrypted1); byte[] decryptedMac1 = new byte[MAC_LENGTH]; - d.readMac(decryptedMac1); + d.readFinal(decryptedMac1); // Check that the actual plaintext matches the expected plaintext out.reset(); out.write(plaintext); diff --git a/test/net/sf/briar/transport/ConnectionEncrypterImplTest.java b/test/net/sf/briar/transport/ConnectionEncrypterImplTest.java index b7463f0126..01f87b7810 100644 --- a/test/net/sf/briar/transport/ConnectionEncrypterImplTest.java +++ b/test/net/sf/briar/transport/ConnectionEncrypterImplTest.java @@ -79,9 +79,9 @@ public class ConnectionEncrypterImplTest extends TestCase { ConnectionEncrypter e = new ConnectionEncrypterImpl(out, Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey); e.getOutputStream().write(plaintext); - e.writeMac(plaintextMac); + e.writeFinal(plaintextMac); e.getOutputStream().write(plaintext1); - e.writeMac(plaintextMac); + e.writeFinal(plaintextMac); byte[] actual = out.toByteArray(); // Check that the actual ciphertext matches the expected ciphertext assertArrayEquals(expected, actual); diff --git a/test/net/sf/briar/transport/NullConnectionDecrypter.java b/test/net/sf/briar/transport/NullConnectionDecrypter.java index 0c6bf77f6e..bfeb8b877a 100644 --- a/test/net/sf/briar/transport/NullConnectionDecrypter.java +++ b/test/net/sf/briar/transport/NullConnectionDecrypter.java @@ -17,7 +17,7 @@ class NullConnectionDecrypter implements ConnectionDecrypter { return in; } - public void readMac(byte[] mac) throws IOException { + public void readFinal(byte[] mac) throws IOException { int offset = 0; while(offset < mac.length) { int read = in.read(mac, offset, mac.length - offset); diff --git a/test/net/sf/briar/transport/NullConnectionEncrypter.java b/test/net/sf/briar/transport/NullConnectionEncrypter.java index aa08e4fa79..29ebcdd0e8 100644 --- a/test/net/sf/briar/transport/NullConnectionEncrypter.java +++ b/test/net/sf/briar/transport/NullConnectionEncrypter.java @@ -23,7 +23,7 @@ implements ConnectionEncrypter { return this; } - public void writeMac(byte[] mac) throws IOException { + public void writeFinal(byte[] mac) throws IOException { out.write(mac); capacity -= mac.length; } -- GitLab