diff --git a/api/net/sf/briar/api/plugins/Segment.java b/api/net/sf/briar/api/plugins/Segment.java new file mode 100644 index 0000000000000000000000000000000000000000..47ed6147732e9b629101c6256fac6573edf84f38 --- /dev/null +++ b/api/net/sf/briar/api/plugins/Segment.java @@ -0,0 +1,16 @@ +package net.sf.briar.api.plugins; + +public interface Segment { + + void clear(); + + byte[] getBuffer(); + + int getLength(); + + long getTransmissionNumber(); + + void setLength(int length); + + void setTransmissionNumber(int transmission); +} diff --git a/api/net/sf/briar/api/plugins/SegmentSink.java b/api/net/sf/briar/api/plugins/SegmentSink.java new file mode 100644 index 0000000000000000000000000000000000000000..8f017c96e27647a472a54fd2033475c76b428393 --- /dev/null +++ b/api/net/sf/briar/api/plugins/SegmentSink.java @@ -0,0 +1,9 @@ +package net.sf.briar.api.plugins; + +import java.io.IOException; + +public interface SegmentSink { + + /** Writes the given segment. */ + void writeSegment(Segment s) throws IOException; +} diff --git a/api/net/sf/briar/api/plugins/SegmentSource.java b/api/net/sf/briar/api/plugins/SegmentSource.java new file mode 100644 index 0000000000000000000000000000000000000000..bb5a92a18faddad1698425a62b4176749a711dd1 --- /dev/null +++ b/api/net/sf/briar/api/plugins/SegmentSource.java @@ -0,0 +1,12 @@ +package net.sf.briar.api.plugins; + +import java.io.IOException; + +public interface SegmentSource { + + /** + * Attempts to read a segment into the given buffer and returns true if a + * segment was read, or false if no more segments can be read. + */ + boolean readSegment(Segment s) throws IOException; +} diff --git a/api/net/sf/briar/api/plugins/duplex/DuplexSegmentedTransportConnection.java b/api/net/sf/briar/api/plugins/duplex/DuplexSegmentedTransportConnection.java index 728f3e60b4302849db500d0776bae9bffe685132..c9b16844edc621f914643b04c9fde63a8472f078 100644 --- a/api/net/sf/briar/api/plugins/duplex/DuplexSegmentedTransportConnection.java +++ b/api/net/sf/briar/api/plugins/duplex/DuplexSegmentedTransportConnection.java @@ -1,15 +1,15 @@ package net.sf.briar.api.plugins.duplex; -import net.sf.briar.api.plugins.FrameSink; -import net.sf.briar.api.plugins.FrameSource; +import net.sf.briar.api.plugins.SegmentSink; +import net.sf.briar.api.plugins.SegmentSource; /** * An interface for reading and writing data over a duplex segmented transport. * The connection is not responsible for encrypting/decrypting or authenticating * the data. */ -public interface DuplexSegmentedTransportConnection extends FrameSource, -FrameSink { +public interface DuplexSegmentedTransportConnection extends SegmentSource, +SegmentSink { /** Returns the maximum length of a segment in bytes. */ int getMaximumSegmentLength(); diff --git a/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportReader.java b/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportReader.java index 66ab1a91c40234f26c49cd3148f5fde5cd81efe9..c34946d5cf14a8a512f70d44973c671e2100745b 100644 --- a/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportReader.java +++ b/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportReader.java @@ -1,13 +1,13 @@ package net.sf.briar.api.plugins.simplex; -import net.sf.briar.api.plugins.FrameSource; +import net.sf.briar.api.plugins.SegmentSource; /** * An interface for reading data from a simplex segmented transport. The reader * is not responsible for decrypting or authenticating the data before * returning it. */ -public interface SimplexSegmentedTransportReader extends FrameSource { +public interface SimplexSegmentedTransportReader extends SegmentSource { /** * Closes the reader and disposes of any associated resources. The first diff --git a/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportWriter.java b/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportWriter.java index 2a8cb810bccf6163de87fa7f2521c9492d8ce648..364faf6ab34708a0713a27f4fef8cdfccd8de41d 100644 --- a/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportWriter.java +++ b/api/net/sf/briar/api/plugins/simplex/SimplexSegmentedTransportWriter.java @@ -1,12 +1,12 @@ package net.sf.briar.api.plugins.simplex; -import net.sf.briar.api.plugins.FrameSink; +import net.sf.briar.api.plugins.SegmentSink; /** * An interface for writing data to a simplex segmented transport. The writer is * not responsible for authenticating or encrypting the data before writing it. */ -public interface SimplexSegmentedTransportWriter extends FrameSink { +public interface SimplexSegmentedTransportWriter extends SegmentSink { /** Returns the capacity of the transport in bytes. */ long getCapacity(); diff --git a/components/net/sf/briar/transport/ConnectionDecrypter.java b/components/net/sf/briar/transport/ConnectionDecrypter.java index c8facbdc74edef04b11c0f7dbc99dd53e308bdf9..227c09bbdf39a3825fe2420b2a7e50ce54f9e507 100644 --- a/components/net/sf/briar/transport/ConnectionDecrypter.java +++ b/components/net/sf/briar/transport/ConnectionDecrypter.java @@ -14,7 +14,6 @@ import javax.crypto.spec.IvParameterSpec; import net.sf.briar.api.FormatException; import net.sf.briar.api.crypto.ErasableKey; -import net.sf.briar.api.plugins.FrameSource; class ConnectionDecrypter implements FrameSource { @@ -49,6 +48,8 @@ class ConnectionDecrypter implements FrameSource { } catch(GeneralSecurityException badIvOrKey) { throw new RuntimeException(badIvOrKey); } + // Clear the buffer before exposing it to the transport plugin + for(int i = 0; i < b.length; i++) b[i] = 0; try { // Read the first block int offset = 0; @@ -64,7 +65,7 @@ class ConnectionDecrypter implements FrameSource { // Decrypt the first block try { int decrypted = frameCipher.update(b, 0, blockSize, b); - assert decrypted == blockSize; + if(decrypted != blockSize) throw new RuntimeException(); } catch(GeneralSecurityException badCipher) { throw new RuntimeException(badCipher); } @@ -86,7 +87,8 @@ class ConnectionDecrypter implements FrameSource { try { int decrypted = frameCipher.doFinal(b, blockSize, length - blockSize, b, blockSize); - assert decrypted == length - blockSize; + if(decrypted != length - blockSize) + throw new RuntimeException(); } catch(GeneralSecurityException badCipher) { throw new RuntimeException(badCipher); } diff --git a/components/net/sf/briar/transport/ConnectionEncrypter.java b/components/net/sf/briar/transport/ConnectionEncrypter.java index 930f1ee81988065ae27a53a6774664738bc7a853..01fcee7708d5513035b261d2f776f4f56941e2d9 100644 --- a/components/net/sf/briar/transport/ConnectionEncrypter.java +++ b/components/net/sf/briar/transport/ConnectionEncrypter.java @@ -2,7 +2,6 @@ package net.sf.briar.transport; import java.io.IOException; -import net.sf.briar.api.plugins.FrameSink; /** Encrypts authenticated data to be sent over a connection. */ interface ConnectionEncrypter extends FrameSink { diff --git a/components/net/sf/briar/transport/ConnectionEncrypterImpl.java b/components/net/sf/briar/transport/ConnectionEncrypterImpl.java index 9370c20f9a1095f4ed2ae24dd583031e08eafdb7..0e83690360ef9da0c4b72d0b84a491c9b32ec337 100644 --- a/components/net/sf/briar/transport/ConnectionEncrypterImpl.java +++ b/components/net/sf/briar/transport/ConnectionEncrypterImpl.java @@ -50,7 +50,7 @@ class ConnectionEncrypterImpl implements ConnectionEncrypter { try { frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); int encrypted = frameCipher.doFinal(b, 0, len, b); - assert encrypted == len; + if(encrypted != len) throw new RuntimeException(); } catch(GeneralSecurityException badCipher) { throw new RuntimeException(badCipher); } diff --git a/components/net/sf/briar/transport/ConnectionReaderFactoryImpl.java b/components/net/sf/briar/transport/ConnectionReaderFactoryImpl.java index 91c092932004664608ea55ce925e2b428f93f028..36be2d4143dd74b56ddb826367081a0d2167fc20 100644 --- a/components/net/sf/briar/transport/ConnectionReaderFactoryImpl.java +++ b/components/net/sf/briar/transport/ConnectionReaderFactoryImpl.java @@ -7,7 +7,6 @@ import javax.crypto.Mac; import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.ErasableKey; -import net.sf.briar.api.plugins.FrameSource; import net.sf.briar.api.transport.ConnectionReader; import net.sf.briar.api.transport.ConnectionReaderFactory; import net.sf.briar.util.ByteUtils; diff --git a/components/net/sf/briar/transport/ConnectionReaderImpl.java b/components/net/sf/briar/transport/ConnectionReaderImpl.java index 49928cd4e7ff41421c7d81987fcc1714b850f2c4..1d7b1de19e32d43bdd61387a2485be5dc2ab3757 100644 --- a/components/net/sf/briar/transport/ConnectionReaderImpl.java +++ b/components/net/sf/briar/transport/ConnectionReaderImpl.java @@ -12,7 +12,6 @@ import javax.crypto.Mac; import net.sf.briar.api.FormatException; import net.sf.briar.api.crypto.ErasableKey; -import net.sf.briar.api.plugins.FrameSource; import net.sf.briar.api.transport.ConnectionReader; class ConnectionReaderImpl extends InputStream implements ConnectionReader { diff --git a/api/net/sf/briar/api/plugins/FrameSink.java b/components/net/sf/briar/transport/FrameSink.java similarity index 65% rename from api/net/sf/briar/api/plugins/FrameSink.java rename to components/net/sf/briar/transport/FrameSink.java index 841eb81592de6a0be9586492eb2f6b4d3c3bab5c..710f60a5e37f6a394c79bd4fd9f628971125b511 100644 --- a/api/net/sf/briar/api/plugins/FrameSink.java +++ b/components/net/sf/briar/transport/FrameSink.java @@ -1,8 +1,8 @@ -package net.sf.briar.api.plugins; +package net.sf.briar.transport; import java.io.IOException; -public interface FrameSink { +interface FrameSink { /** Writes the given frame. */ void writeFrame(byte[] b, int len) throws IOException; diff --git a/api/net/sf/briar/api/plugins/FrameSource.java b/components/net/sf/briar/transport/FrameSource.java similarity index 74% rename from api/net/sf/briar/api/plugins/FrameSource.java rename to components/net/sf/briar/transport/FrameSource.java index 59472821406614067106a27656e7170eb6beea85..7347b6ccbab2452ab9160661deae8ad6c7618301 100644 --- a/api/net/sf/briar/api/plugins/FrameSource.java +++ b/components/net/sf/briar/transport/FrameSource.java @@ -1,8 +1,8 @@ -package net.sf.briar.api.plugins; +package net.sf.briar.transport; import java.io.IOException; -public interface FrameSource { +interface FrameSource { /** * Reads a frame into the given buffer and returns its length, or -1 if no diff --git a/components/net/sf/briar/transport/SegmentImpl.java b/components/net/sf/briar/transport/SegmentImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..b7cb6398e4265572ea4e1235e5b1f3cfe3019ed2 --- /dev/null +++ b/components/net/sf/briar/transport/SegmentImpl.java @@ -0,0 +1,45 @@ +package net.sf.briar.transport; + +import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH; +import net.sf.briar.api.plugins.Segment; +import net.sf.briar.util.ByteUtils; + +class SegmentImpl implements Segment { + + private final byte[] buf = new byte[MAX_FRAME_LENGTH]; + + private int length = -1; + private long transmission = -1; + + public void clear() { + for(int i = 0; i < buf.length; i++) buf[i] = 0; + length = -1; + transmission = -1; + } + + public byte[] getBuffer() { + return buf; + } + + public int getLength() { + if(length == -1) throw new IllegalStateException(); + return length; + } + + public long getTransmissionNumber() { + if(transmission == -1) throw new IllegalStateException(); + return transmission; + } + + public void setLength(int length) { + if(length < 0 || length > buf.length) + throw new IllegalArgumentException(); + this.length = length; + } + + public void setTransmissionNumber(int transmission) { + if(transmission < 0 || transmission > ByteUtils.MAX_32_BIT_UNSIGNED) + throw new IllegalArgumentException(); + this.transmission = transmission; + } +} diff --git a/components/net/sf/briar/transport/SegmentedConnectionDecrypter.java b/components/net/sf/briar/transport/SegmentedConnectionDecrypter.java index 385d9671209d948597c0f54addafb4addede318a..47bc7b1423c287af5e38a353d4fefa358a47fe11 100644 --- a/components/net/sf/briar/transport/SegmentedConnectionDecrypter.java +++ b/components/net/sf/briar/transport/SegmentedConnectionDecrypter.java @@ -12,19 +12,21 @@ import javax.crypto.spec.IvParameterSpec; import net.sf.briar.api.FormatException; import net.sf.briar.api.crypto.ErasableKey; -import net.sf.briar.api.plugins.FrameSource; +import net.sf.briar.api.plugins.Segment; +import net.sf.briar.api.plugins.SegmentSource; class SegmentedConnectionDecrypter implements FrameSource { - private final FrameSource in; + private final SegmentSource in; private final Cipher frameCipher; private final ErasableKey frameKey; private final int macLength, blockSize; private final byte[] iv; + private final Segment segment; private long frame = 0L; - SegmentedConnectionDecrypter(FrameSource in, Cipher frameCipher, + SegmentedConnectionDecrypter(SegmentSource in, Cipher frameCipher, ErasableKey frameKey, int macLength) { this.in = in; this.frameCipher = frameCipher; @@ -34,6 +36,7 @@ class SegmentedConnectionDecrypter implements FrameSource { if(blockSize < FRAME_HEADER_LENGTH) throw new IllegalArgumentException(); iv = IvEncoder.encodeIv(0, blockSize); + segment = new SegmentImpl(); } public int readFrame(byte[] b) throws IOException { @@ -47,17 +50,22 @@ class SegmentedConnectionDecrypter implements FrameSource { } catch(GeneralSecurityException badIvOrKey) { throw new RuntimeException(badIvOrKey); } + // Clear the buffer before exposing it to the transport plugin + segment.clear(); try { // Read the frame - int length = in.readFrame(b); - if(length == -1) return -1; + if(!in.readSegment(segment)) return -1; + if(segment.getTransmissionNumber() != frame) + throw new FormatException(); + int length = segment.getLength(); if(length > MAX_FRAME_LENGTH) throw new FormatException(); if(length < FRAME_HEADER_LENGTH + macLength) throw new FormatException(); // Decrypt the frame try { - int decrypted = frameCipher.doFinal(b, 0, length, b); - assert decrypted == length; + int decrypted = frameCipher.doFinal(segment.getBuffer(), 0, + length, b); + if(decrypted != length) throw new RuntimeException(); } catch(GeneralSecurityException badCipher) { throw new RuntimeException(badCipher); } diff --git a/components/net/sf/briar/transport/SegmentedConnectionEncrypter.java b/components/net/sf/briar/transport/SegmentedConnectionEncrypter.java index f67d9b9d91be969bdfa89fc34ae8ce670477d95c..60f905120c0d2d9774aab30ca1bf19cb9ae961a9 100644 --- a/components/net/sf/briar/transport/SegmentedConnectionEncrypter.java +++ b/components/net/sf/briar/transport/SegmentedConnectionEncrypter.java @@ -1,6 +1,7 @@ package net.sf.briar.transport; import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED; +import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH; import java.io.IOException; import java.security.GeneralSecurityException; @@ -9,20 +10,23 @@ import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import net.sf.briar.api.crypto.ErasableKey; -import net.sf.briar.api.plugins.FrameSink; +import net.sf.briar.api.plugins.Segment; +import net.sf.briar.api.plugins.SegmentSink; class SegmentedConnectionEncrypter implements ConnectionEncrypter { - private final FrameSink out; + private final SegmentSink out; private final Cipher frameCipher; private final ErasableKey frameKey; private final byte[] iv, tag; + private final Segment segment; private long capacity, frame = 0L; private boolean tagWritten = false; - SegmentedConnectionEncrypter(FrameSink out, long capacity, Cipher tagCipher, - Cipher frameCipher, ErasableKey tagKey, ErasableKey frameKey) { + SegmentedConnectionEncrypter(SegmentSink out, long capacity, + Cipher tagCipher, Cipher frameCipher, ErasableKey tagKey, + ErasableKey frameKey) { this.out = out; this.capacity = capacity; this.frameCipher = frameCipher; @@ -31,16 +35,16 @@ class SegmentedConnectionEncrypter implements ConnectionEncrypter { // Encrypt the tag tag = TagEncoder.encodeTag(0, tagCipher, tagKey); tagKey.erase(); + segment = new SegmentImpl(); } public void writeFrame(byte[] b, int len) throws IOException { if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException(); int offset = 0; if(!tagWritten) { - if(tag.length + len > b.length) + if(tag.length + len > MAX_FRAME_LENGTH) throw new IllegalArgumentException(); - System.arraycopy(b, 0, b, tag.length, len); - System.arraycopy(tag, 0, b, 0, tag.length); + System.arraycopy(tag, 0, segment.getBuffer(), 0, tag.length); capacity -= tag.length; tagWritten = true; offset = tag.length; @@ -49,13 +53,15 @@ class SegmentedConnectionEncrypter implements ConnectionEncrypter { IvParameterSpec ivSpec = new IvParameterSpec(iv); try { frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); - int encrypted = frameCipher.doFinal(b, offset, len, b, offset); - assert encrypted == len; + int encrypted = frameCipher.doFinal(b, 0, len, segment.getBuffer(), + offset); + if(encrypted != len) throw new RuntimeException(); } catch(GeneralSecurityException badCipher) { throw new RuntimeException(badCipher); } + segment.setLength(offset + len); try { - out.writeFrame(b, offset + len); + out.writeSegment(segment); } catch(IOException e) { frameKey.erase(); throw e; diff --git a/test/build.xml b/test/build.xml index 8c643549dee314b3ff7bb973c9ba3d643dd31745..f0d6316227a6e022b46645fe7daa51d23c04716a 100644 --- a/test/build.xml +++ b/test/build.xml @@ -49,7 +49,7 @@ <test name='net.sf.briar.serial.ReaderImplTest'/> <test name='net.sf.briar.serial.WriterImplTest'/> <test name='net.sf.briar.setup.SetupWorkerTest'/> - <test name='net.sf.briar.transport.ConnectionDecrypterImplTest'/> + <test name='net.sf.briar.transport.ConnectionDecrypterTest'/> <test name='net.sf.briar.transport.ConnectionEncrypterImplTest'/> <test name='net.sf.briar.transport.ConnectionReaderImplTest'/> <test name='net.sf.briar.transport.ConnectionRecogniserImplTest'/> diff --git a/test/net/sf/briar/transport/ConnectionDecrypterImplTest.java b/test/net/sf/briar/transport/ConnectionDecrypterTest.java similarity index 94% rename from test/net/sf/briar/transport/ConnectionDecrypterImplTest.java rename to test/net/sf/briar/transport/ConnectionDecrypterTest.java index f8d09593a49b3ea3be86965291ff9091eb5127e4..829c73335a54842e8b1979a03d4bdf13e1b038bf 100644 --- a/test/net/sf/briar/transport/ConnectionDecrypterImplTest.java +++ b/test/net/sf/briar/transport/ConnectionDecrypterTest.java @@ -11,7 +11,6 @@ import javax.crypto.spec.IvParameterSpec; import net.sf.briar.BriarTestCase; import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.ErasableKey; -import net.sf.briar.api.plugins.FrameSource; import net.sf.briar.crypto.CryptoModule; import org.apache.commons.io.output.ByteArrayOutputStream; @@ -20,14 +19,14 @@ import org.junit.Test; import com.google.inject.Guice; import com.google.inject.Injector; -public class ConnectionDecrypterImplTest extends BriarTestCase { +public class ConnectionDecrypterTest extends BriarTestCase { private static final int MAC_LENGTH = 32; private final Cipher frameCipher; private final ErasableKey frameKey; - public ConnectionDecrypterImplTest() { + public ConnectionDecrypterTest() { super(); Injector i = Guice.createInjector(new CryptoModule()); CryptoComponent crypto = i.getInstance(CryptoComponent.class); diff --git a/test/net/sf/briar/transport/ConnectionReaderImplTest.java b/test/net/sf/briar/transport/ConnectionReaderImplTest.java index 7a6f8000ad130c2e263122195ccd03645e4a1af5..798f8e457224fee5fbb0ffaec7fbe63d1bf59f97 100644 --- a/test/net/sf/briar/transport/ConnectionReaderImplTest.java +++ b/test/net/sf/briar/transport/ConnectionReaderImplTest.java @@ -8,7 +8,6 @@ import java.io.ByteArrayInputStream; import net.sf.briar.TestUtils; import net.sf.briar.api.FormatException; -import net.sf.briar.api.plugins.FrameSource; import net.sf.briar.api.transport.ConnectionReader; import org.apache.commons.io.output.ByteArrayOutputStream; diff --git a/test/net/sf/briar/transport/FrameReadWriteTest.java b/test/net/sf/briar/transport/FrameReadWriteTest.java index ac51080048affe9549f54043f830b99d7e19e367..b763682a4b4e31fd83e6b35f508c550bd5a9f21c 100644 --- a/test/net/sf/briar/transport/FrameReadWriteTest.java +++ b/test/net/sf/briar/transport/FrameReadWriteTest.java @@ -15,7 +15,6 @@ import javax.crypto.Mac; import net.sf.briar.BriarTestCase; import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.ErasableKey; -import net.sf.briar.api.plugins.FrameSource; import net.sf.briar.api.transport.ConnectionReader; import net.sf.briar.api.transport.ConnectionWriter; import net.sf.briar.crypto.CryptoModule; diff --git a/test/net/sf/briar/transport/NullConnectionDecrypter.java b/test/net/sf/briar/transport/NullConnectionDecrypter.java index 319df394cc3f4f4988c6c1843734a46c717f4cdf..32b5a2164968b9f7d462e81fd8d48bfb737959d6 100644 --- a/test/net/sf/briar/transport/NullConnectionDecrypter.java +++ b/test/net/sf/briar/transport/NullConnectionDecrypter.java @@ -8,7 +8,6 @@ import java.io.IOException; import java.io.InputStream; import net.sf.briar.api.FormatException; -import net.sf.briar.api.plugins.FrameSource; /** A connection decrypter that performs no decryption. */ class NullConnectionDecrypter implements FrameSource { diff --git a/test/net/sf/briar/transport/SegmentedConnectionDecrypterTest.java b/test/net/sf/briar/transport/SegmentedConnectionDecrypterTest.java index a987ede8f8c86c26f04645fc4abc65eba9469e05..20727365e85fa21a137c3383e43d00300cce2193 100644 --- a/test/net/sf/briar/transport/SegmentedConnectionDecrypterTest.java +++ b/test/net/sf/briar/transport/SegmentedConnectionDecrypterTest.java @@ -11,7 +11,8 @@ import javax.crypto.spec.IvParameterSpec; import net.sf.briar.BriarTestCase; import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.ErasableKey; -import net.sf.briar.api.plugins.FrameSource; +import net.sf.briar.api.plugins.Segment; +import net.sf.briar.api.plugins.SegmentSource; import net.sf.briar.crypto.CryptoModule; import org.junit.Test; @@ -53,7 +54,7 @@ public class SegmentedConnectionDecrypterTest extends BriarTestCase { plaintext1.length); // Use a connection decrypter to decrypt the ciphertext byte[][] frames = new byte[][] { ciphertext, ciphertext1 }; - FrameSource in = new ByteArrayFrameSource(frames); + SegmentSource in = new ByteArraySegmentSource(frames); FrameSource decrypter = new SegmentedConnectionDecrypter(in, frameCipher, frameKey, MAC_LENGTH); // First frame @@ -69,20 +70,24 @@ public class SegmentedConnectionDecrypterTest extends BriarTestCase { } } - private static class ByteArrayFrameSource implements FrameSource { + private static class ByteArraySegmentSource implements SegmentSource { private final byte[][] frames; private int frame = 0; - private ByteArrayFrameSource(byte[][] frames) { + private ByteArraySegmentSource(byte[][] frames) { this.frames = frames; } - public int readFrame(byte[] b) throws IOException { - byte[] src = frames[frame++]; - System.arraycopy(src, 0, b, 0, src.length); - return src.length; + public boolean readSegment(Segment s) throws IOException { + if(frame == frames.length) return false; + byte[] src = frames[frame]; + System.arraycopy(src, 0, s.getBuffer(), 0, src.length); + s.setLength(src.length); + s.setTransmissionNumber(frame); + frame++; + return true; } } } diff --git a/test/net/sf/briar/transport/SegmentedConnectionEncrypterTest.java b/test/net/sf/briar/transport/SegmentedConnectionEncrypterTest.java index 9c4cf62dc6833b1d0cbab6bec69949162ba4bf20..638d9ac31d2c72895f3675817b987dc3864c61a5 100644 --- a/test/net/sf/briar/transport/SegmentedConnectionEncrypterTest.java +++ b/test/net/sf/briar/transport/SegmentedConnectionEncrypterTest.java @@ -11,8 +11,8 @@ import javax.crypto.spec.IvParameterSpec; import net.sf.briar.BriarTestCase; import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.ErasableKey; -import net.sf.briar.api.plugins.FrameSink; -import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH; +import net.sf.briar.api.plugins.Segment; +import net.sf.briar.api.plugins.SegmentSink; import net.sf.briar.crypto.CryptoModule; import org.junit.Test; @@ -60,13 +60,11 @@ public class SegmentedConnectionEncrypterTest extends BriarTestCase { out.write(ciphertext1); byte[] expected = out.toByteArray(); // Use a connection encrypter to encrypt the plaintext - ByteArrayFrameSink sink = new ByteArrayFrameSink(); + ByteArraySegmentSink sink = new ByteArraySegmentSink(); ConnectionEncrypter e = new SegmentedConnectionEncrypter(sink, Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey); // The first frame's buffer must have enough space for the tag - byte[] b = new byte[TAG_LENGTH + plaintext.length]; - System.arraycopy(plaintext, 0, b, 0, plaintext.length); - e.writeFrame(b, plaintext.length); + e.writeFrame(plaintext, plaintext.length); e.writeFrame(plaintext1, plaintext1.length); byte[] actual = out.toByteArray(); // Check that the actual ciphertext matches the expected ciphertext @@ -74,11 +72,11 @@ public class SegmentedConnectionEncrypterTest extends BriarTestCase { assertEquals(Long.MAX_VALUE - actual.length, e.getRemainingCapacity()); } - private static class ByteArrayFrameSink extends ByteArrayOutputStream - implements FrameSink { + private static class ByteArraySegmentSink extends ByteArrayOutputStream + implements SegmentSink { - public void writeFrame(byte[] b, int len) throws IOException { - write(b, 0, len); + public void writeSegment(Segment s) throws IOException { + write(s.getBuffer(), 0, s.getLength()); } } }