diff --git a/components/net/sf/briar/transport/ConnectionDecrypter.java b/components/net/sf/briar/transport/ConnectionDecrypter.java index 80bd798eeebe57fa3d0084634758b3ff2e277d8c..b4763679b63427e08bf1b35ac5ac60a17025a8ba 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 ac9557c88cd0091d98898c2c89a19941a9b05281..ff354a57b6c36a1d216a523315049452838b046d 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 304cc1672b1e5155c222bfda553f64ff2d04771e..dd9d1334fd1ae56cd19b241bcbbb543f6018d35b 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 54446b09e2367590a8ee653cd8cea35018d72866..cb0feb08baec5439bf0dd2e10c2e030432dffd6b 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 258eb824ee7384b9248fe569405d4fef6ff1c4fd..880119052d23d6a649d692ece22f9a58cf0d9b4c 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 4f1c790769f21d1a7e8583adcb926b8f30abc3e5..673a7896bee71dad9a79d344003bfb76032d3c3b 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 ae61f11bfae438c68a6025a92b5f98cfe288879f..6bc7c2762b34d46b859393de582fcafd8ca367c7 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 e8a13dc3f38fac0e6168a8c04f246ed229c6b70b..086adca1ced2766195fdb66555f63902a1c21e40 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 b7463f0126c78a49af560c21401faed2178e9b38..01f87b78101b28ec443c4167d31de1c8469d61e5 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 0c6bf77f6e50fa3304f6e72faa9223800bae6e07..bfeb8b877ab4dfeb4c5f3e4cceff36ab2a67c1a5 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 aa08e4fa79cc8ee8f2dbd5a7c58f5d270410ccc5..29ebcdd0e830f3d14c7fe0eccedc7ecdeee3e35a 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; }