Converted the outgoing encryption layer from frames to segments.

This commit is contained in:
akwizgran
2012-01-17 17:37:38 +00:00
parent f6ed6dd60b
commit 87e1c42bf8
15 changed files with 214 additions and 102 deletions

View File

@@ -21,8 +21,11 @@ public class ConnectionWriterImplTest extends TransportTest {
@Test
public void testFlushWithoutWriteProducesNothing() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutgoingEncryptionLayer encrypter = new NullOutgoingEncryptionLayer(out);
ConnectionWriter w = new ConnectionWriterImpl(encrypter, mac, macKey);
OutgoingEncryptionLayer encrypter =
new NullOutgoingEncryptionLayer(out);
OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter);
ConnectionWriter w = new ConnectionWriterImpl(correcter, mac, macKey);
w.getOutputStream().flush();
w.getOutputStream().flush();
w.getOutputStream().flush();
@@ -41,8 +44,11 @@ public class ConnectionWriterImplTest extends TransportTest {
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength);
// Check that the ConnectionWriter gets the same results
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutgoingEncryptionLayer encrypter = new NullOutgoingEncryptionLayer(out);
ConnectionWriter w = new ConnectionWriterImpl(encrypter, mac, macKey);
OutgoingEncryptionLayer encrypter =
new NullOutgoingEncryptionLayer(out);
OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter);
ConnectionWriter w = new ConnectionWriterImpl(correcter, mac, macKey);
w.getOutputStream().write(0);
w.getOutputStream().flush();
assertArrayEquals(frame, out.toByteArray());
@@ -51,8 +57,11 @@ public class ConnectionWriterImplTest extends TransportTest {
@Test
public void testWriteByteToMaxLengthWritesFrame() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutgoingEncryptionLayer encrypter = new NullOutgoingEncryptionLayer(out);
ConnectionWriter w = new ConnectionWriterImpl(encrypter, mac, macKey);
OutgoingEncryptionLayer encrypter =
new NullOutgoingEncryptionLayer(out);
OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter);
ConnectionWriter w = new ConnectionWriterImpl(correcter, mac, macKey);
OutputStream out1 = w.getOutputStream();
// The first maxPayloadLength - 1 bytes should be buffered
for(int i = 0; i < MAX_PAYLOAD_LENGTH - 1; i++) out1.write(0);
@@ -65,8 +74,11 @@ public class ConnectionWriterImplTest extends TransportTest {
@Test
public void testWriteArrayToMaxLengthWritesFrame() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutgoingEncryptionLayer encrypter = new NullOutgoingEncryptionLayer(out);
ConnectionWriter w = new ConnectionWriterImpl(encrypter, mac, macKey);
OutgoingEncryptionLayer encrypter =
new NullOutgoingEncryptionLayer(out);
OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter);
ConnectionWriter w = new ConnectionWriterImpl(correcter, mac, macKey);
OutputStream out1 = w.getOutputStream();
// The first maxPayloadLength - 1 bytes should be buffered
out1.write(new byte[MAX_PAYLOAD_LENGTH - 1]);
@@ -100,8 +112,11 @@ public class ConnectionWriterImplTest extends TransportTest {
byte[] expected = out.toByteArray();
// Check that the ConnectionWriter gets the same results
out.reset();
OutgoingEncryptionLayer encrypter = new NullOutgoingEncryptionLayer(out);
ConnectionWriter w = new ConnectionWriterImpl(encrypter, mac, macKey);
OutgoingEncryptionLayer encrypter =
new NullOutgoingEncryptionLayer(out);
OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter);
ConnectionWriter w = new ConnectionWriterImpl(correcter, mac, macKey);
w.getOutputStream().write(new byte[123]);
w.getOutputStream().flush();
w.getOutputStream().write(new byte[1234]);

View File

@@ -77,7 +77,9 @@ public class FrameReadWriteTest extends BriarTestCase {
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out,
Long.MAX_VALUE, tagCipher, frameCipher, tagCopy, frameCopy,
false);
ConnectionWriter writer = new ConnectionWriterImpl(encrypter, mac,
OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter);
ConnectionWriter writer = new ConnectionWriterImpl(correcter, mac,
macCopy);
OutputStream out1 = writer.getOutputStream();
out1.write(frame);
@@ -93,9 +95,9 @@ public class FrameReadWriteTest extends BriarTestCase {
// Read the frames back
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
tagCipher, frameCipher, tagKey, frameKey, false);
IncomingErrorCorrectionLayer correcter =
IncomingErrorCorrectionLayer correcter1 =
new NullIncomingErrorCorrectionLayer(decrypter);
ConnectionReader reader = new ConnectionReaderImpl(correcter, mac,
ConnectionReader reader = new ConnectionReaderImpl(correcter1, mac,
macKey);
InputStream in1 = reader.getInputStream();
byte[] recovered = new byte[frame.length];

View File

@@ -3,6 +3,8 @@ package net.sf.briar.transport;
import java.io.IOException;
import java.io.OutputStream;
import net.sf.briar.api.plugins.Segment;
/** An encryption layer that performs no encryption. */
class NullOutgoingEncryptionLayer implements OutgoingEncryptionLayer {
@@ -20,9 +22,9 @@ class NullOutgoingEncryptionLayer implements OutgoingEncryptionLayer {
this.capacity = capacity;
}
public void writeFrame(byte[] b, int len) throws IOException {
out.write(b, 0, len);
capacity -= len;
public void writeSegment(Segment s) throws IOException {
out.write(s.getBuffer(), 0, s.getLength());
capacity -= s.getLength();
}
public void flush() throws IOException {

View File

@@ -11,6 +11,7 @@ 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.Segment;
import net.sf.briar.crypto.CryptoModule;
import org.junit.Test;
@@ -63,8 +64,15 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out,
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey,
false);
encrypter.writeFrame(plaintext, plaintext.length);
encrypter.writeFrame(plaintext1, plaintext1.length);
Segment s = new SegmentImpl();
System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length);
s.setLength(plaintext.length);
s.setSegmentNumber(0L);
encrypter.writeSegment(s);
System.arraycopy(plaintext1, 0, s.getBuffer(), 0, plaintext1.length);
s.setLength(plaintext1.length);
s.setSegmentNumber(1L);
encrypter.writeSegment(s);
byte[] actual = out.toByteArray();
// Check that the actual ciphertext matches the expected ciphertext
assertArrayEquals(expected, actual);
@@ -103,8 +111,15 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
out.reset();
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out,
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey, true);
encrypter.writeFrame(plaintext, plaintext.length);
encrypter.writeFrame(plaintext1, plaintext1.length);
Segment s = new SegmentImpl();
System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length);
s.setLength(plaintext.length);
s.setSegmentNumber(0L);
encrypter.writeSegment(s);
System.arraycopy(plaintext1, 0, s.getBuffer(), 0, plaintext1.length);
s.setLength(plaintext1.length);
s.setSegmentNumber(1L);
encrypter.writeSegment(s);
byte[] actual = out.toByteArray();
// Check that the actual ciphertext matches the expected ciphertext
assertArrayEquals(expected, actual);

View File

@@ -66,9 +66,15 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
OutgoingEncryptionLayer encrypter =
new OutgoingSegmentedEncryptionLayer(sink, Long.MAX_VALUE,
tagCipher, frameCipher, tagKey, frameKey, false);
// The first frame's buffer must have enough space for the tag
encrypter.writeFrame(plaintext, plaintext.length);
encrypter.writeFrame(plaintext1, plaintext1.length);
Segment s = new SegmentImpl();
System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length);
s.setLength(plaintext.length);
s.setSegmentNumber(0L);
encrypter.writeSegment(s);
System.arraycopy(plaintext1, 0, s.getBuffer(), 0, plaintext1.length);
s.setLength(plaintext1.length);
s.setSegmentNumber(1L);
encrypter.writeSegment(s);
byte[] actual = out.toByteArray();
// Check that the actual ciphertext matches the expected ciphertext
assertArrayEquals(expected, actual);
@@ -108,8 +114,15 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
OutgoingEncryptionLayer encrypter =
new OutgoingSegmentedEncryptionLayer(sink, Long.MAX_VALUE,
tagCipher, frameCipher, tagKey, frameKey, true);
encrypter.writeFrame(plaintext, plaintext.length);
encrypter.writeFrame(plaintext1, plaintext1.length);
Segment s = new SegmentImpl();
System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length);
s.setLength(plaintext.length);
s.setSegmentNumber(0L);
encrypter.writeSegment(s);
System.arraycopy(plaintext1, 0, s.getBuffer(), 0, plaintext1.length);
s.setLength(plaintext1.length);
s.setSegmentNumber(1L);
encrypter.writeSegment(s);
byte[] actual = out.toByteArray();
// Check that the actual ciphertext matches the expected ciphertext
assertArrayEquals(expected, actual);