Pass segments rather than frames to/from segmented plugins.

This commit is contained in:
akwizgran
2012-01-13 15:05:42 +00:00
parent d0e402062a
commit ac136d3732
23 changed files with 155 additions and 61 deletions

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -1,15 +1,15 @@
package net.sf.briar.api.plugins.duplex; package net.sf.briar.api.plugins.duplex;
import net.sf.briar.api.plugins.FrameSink; import net.sf.briar.api.plugins.SegmentSink;
import net.sf.briar.api.plugins.FrameSource; import net.sf.briar.api.plugins.SegmentSource;
/** /**
* An interface for reading and writing data over a duplex segmented transport. * An interface for reading and writing data over a duplex segmented transport.
* The connection is not responsible for encrypting/decrypting or authenticating * The connection is not responsible for encrypting/decrypting or authenticating
* the data. * the data.
*/ */
public interface DuplexSegmentedTransportConnection extends FrameSource, public interface DuplexSegmentedTransportConnection extends SegmentSource,
FrameSink { SegmentSink {
/** Returns the maximum length of a segment in bytes. */ /** Returns the maximum length of a segment in bytes. */
int getMaximumSegmentLength(); int getMaximumSegmentLength();

View File

@@ -1,13 +1,13 @@
package net.sf.briar.api.plugins.simplex; 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 * An interface for reading data from a simplex segmented transport. The reader
* is not responsible for decrypting or authenticating the data before * is not responsible for decrypting or authenticating the data before
* returning it. * returning it.
*/ */
public interface SimplexSegmentedTransportReader extends FrameSource { public interface SimplexSegmentedTransportReader extends SegmentSource {
/** /**
* Closes the reader and disposes of any associated resources. The first * Closes the reader and disposes of any associated resources. The first

View File

@@ -1,12 +1,12 @@
package net.sf.briar.api.plugins.simplex; 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 * An interface for writing data to a simplex segmented transport. The writer is
* not responsible for authenticating or encrypting the data before writing it. * 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. */ /** Returns the capacity of the transport in bytes. */
long getCapacity(); long getCapacity();

View File

@@ -14,7 +14,6 @@ import javax.crypto.spec.IvParameterSpec;
import net.sf.briar.api.FormatException; import net.sf.briar.api.FormatException;
import net.sf.briar.api.crypto.ErasableKey; import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.plugins.FrameSource;
class ConnectionDecrypter implements FrameSource { class ConnectionDecrypter implements FrameSource {
@@ -49,6 +48,8 @@ class ConnectionDecrypter implements FrameSource {
} catch(GeneralSecurityException badIvOrKey) { } catch(GeneralSecurityException badIvOrKey) {
throw new RuntimeException(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 { try {
// Read the first block // Read the first block
int offset = 0; int offset = 0;
@@ -64,7 +65,7 @@ class ConnectionDecrypter implements FrameSource {
// Decrypt the first block // Decrypt the first block
try { try {
int decrypted = frameCipher.update(b, 0, blockSize, b); int decrypted = frameCipher.update(b, 0, blockSize, b);
assert decrypted == blockSize; if(decrypted != blockSize) throw new RuntimeException();
} catch(GeneralSecurityException badCipher) { } catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher); throw new RuntimeException(badCipher);
} }
@@ -86,7 +87,8 @@ class ConnectionDecrypter implements FrameSource {
try { try {
int decrypted = frameCipher.doFinal(b, blockSize, int decrypted = frameCipher.doFinal(b, blockSize,
length - blockSize, b, blockSize); length - blockSize, b, blockSize);
assert decrypted == length - blockSize; if(decrypted != length - blockSize)
throw new RuntimeException();
} catch(GeneralSecurityException badCipher) { } catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher); throw new RuntimeException(badCipher);
} }

View File

@@ -2,7 +2,6 @@ package net.sf.briar.transport;
import java.io.IOException; import java.io.IOException;
import net.sf.briar.api.plugins.FrameSink;
/** Encrypts authenticated data to be sent over a connection. */ /** Encrypts authenticated data to be sent over a connection. */
interface ConnectionEncrypter extends FrameSink { interface ConnectionEncrypter extends FrameSink {

View File

@@ -50,7 +50,7 @@ class ConnectionEncrypterImpl implements ConnectionEncrypter {
try { try {
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
int encrypted = frameCipher.doFinal(b, 0, len, b); int encrypted = frameCipher.doFinal(b, 0, len, b);
assert encrypted == len; if(encrypted != len) throw new RuntimeException();
} catch(GeneralSecurityException badCipher) { } catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher); throw new RuntimeException(badCipher);
} }

View File

@@ -7,7 +7,6 @@ import javax.crypto.Mac;
import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey; 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.ConnectionReader;
import net.sf.briar.api.transport.ConnectionReaderFactory; import net.sf.briar.api.transport.ConnectionReaderFactory;
import net.sf.briar.util.ByteUtils; import net.sf.briar.util.ByteUtils;

View File

@@ -12,7 +12,6 @@ import javax.crypto.Mac;
import net.sf.briar.api.FormatException; import net.sf.briar.api.FormatException;
import net.sf.briar.api.crypto.ErasableKey; 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.ConnectionReader;
class ConnectionReaderImpl extends InputStream implements ConnectionReader { class ConnectionReaderImpl extends InputStream implements ConnectionReader {

View File

@@ -1,8 +1,8 @@
package net.sf.briar.api.plugins; package net.sf.briar.transport;
import java.io.IOException; import java.io.IOException;
public interface FrameSink { interface FrameSink {
/** Writes the given frame. */ /** Writes the given frame. */
void writeFrame(byte[] b, int len) throws IOException; void writeFrame(byte[] b, int len) throws IOException;

View File

@@ -1,8 +1,8 @@
package net.sf.briar.api.plugins; package net.sf.briar.transport;
import java.io.IOException; import java.io.IOException;
public interface FrameSource { interface FrameSource {
/** /**
* Reads a frame into the given buffer and returns its length, or -1 if no * Reads a frame into the given buffer and returns its length, or -1 if no

View File

@@ -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;
}
}

View File

@@ -12,19 +12,21 @@ import javax.crypto.spec.IvParameterSpec;
import net.sf.briar.api.FormatException; import net.sf.briar.api.FormatException;
import net.sf.briar.api.crypto.ErasableKey; 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 { class SegmentedConnectionDecrypter implements FrameSource {
private final FrameSource in; private final SegmentSource in;
private final Cipher frameCipher; private final Cipher frameCipher;
private final ErasableKey frameKey; private final ErasableKey frameKey;
private final int macLength, blockSize; private final int macLength, blockSize;
private final byte[] iv; private final byte[] iv;
private final Segment segment;
private long frame = 0L; private long frame = 0L;
SegmentedConnectionDecrypter(FrameSource in, Cipher frameCipher, SegmentedConnectionDecrypter(SegmentSource in, Cipher frameCipher,
ErasableKey frameKey, int macLength) { ErasableKey frameKey, int macLength) {
this.in = in; this.in = in;
this.frameCipher = frameCipher; this.frameCipher = frameCipher;
@@ -34,6 +36,7 @@ class SegmentedConnectionDecrypter implements FrameSource {
if(blockSize < FRAME_HEADER_LENGTH) if(blockSize < FRAME_HEADER_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
iv = IvEncoder.encodeIv(0, blockSize); iv = IvEncoder.encodeIv(0, blockSize);
segment = new SegmentImpl();
} }
public int readFrame(byte[] b) throws IOException { public int readFrame(byte[] b) throws IOException {
@@ -47,17 +50,22 @@ class SegmentedConnectionDecrypter implements FrameSource {
} catch(GeneralSecurityException badIvOrKey) { } catch(GeneralSecurityException badIvOrKey) {
throw new RuntimeException(badIvOrKey); throw new RuntimeException(badIvOrKey);
} }
// Clear the buffer before exposing it to the transport plugin
segment.clear();
try { try {
// Read the frame // Read the frame
int length = in.readFrame(b); if(!in.readSegment(segment)) return -1;
if(length == -1) return -1; if(segment.getTransmissionNumber() != frame)
throw new FormatException();
int length = segment.getLength();
if(length > MAX_FRAME_LENGTH) throw new FormatException(); if(length > MAX_FRAME_LENGTH) throw new FormatException();
if(length < FRAME_HEADER_LENGTH + macLength) if(length < FRAME_HEADER_LENGTH + macLength)
throw new FormatException(); throw new FormatException();
// Decrypt the frame // Decrypt the frame
try { try {
int decrypted = frameCipher.doFinal(b, 0, length, b); int decrypted = frameCipher.doFinal(segment.getBuffer(), 0,
assert decrypted == length; length, b);
if(decrypted != length) throw new RuntimeException();
} catch(GeneralSecurityException badCipher) { } catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher); throw new RuntimeException(badCipher);
} }

View File

@@ -1,6 +1,7 @@
package net.sf.briar.transport; package net.sf.briar.transport;
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED; 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.io.IOException;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
@@ -9,20 +10,23 @@ import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.IvParameterSpec;
import net.sf.briar.api.crypto.ErasableKey; 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 { class SegmentedConnectionEncrypter implements ConnectionEncrypter {
private final FrameSink out; private final SegmentSink out;
private final Cipher frameCipher; private final Cipher frameCipher;
private final ErasableKey frameKey; private final ErasableKey frameKey;
private final byte[] iv, tag; private final byte[] iv, tag;
private final Segment segment;
private long capacity, frame = 0L; private long capacity, frame = 0L;
private boolean tagWritten = false; private boolean tagWritten = false;
SegmentedConnectionEncrypter(FrameSink out, long capacity, Cipher tagCipher, SegmentedConnectionEncrypter(SegmentSink out, long capacity,
Cipher frameCipher, ErasableKey tagKey, ErasableKey frameKey) { Cipher tagCipher, Cipher frameCipher, ErasableKey tagKey,
ErasableKey frameKey) {
this.out = out; this.out = out;
this.capacity = capacity; this.capacity = capacity;
this.frameCipher = frameCipher; this.frameCipher = frameCipher;
@@ -31,16 +35,16 @@ class SegmentedConnectionEncrypter implements ConnectionEncrypter {
// Encrypt the tag // Encrypt the tag
tag = TagEncoder.encodeTag(0, tagCipher, tagKey); tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
tagKey.erase(); tagKey.erase();
segment = new SegmentImpl();
} }
public void writeFrame(byte[] b, int len) throws IOException { public void writeFrame(byte[] b, int len) throws IOException {
if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException(); if(frame > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
int offset = 0; int offset = 0;
if(!tagWritten) { if(!tagWritten) {
if(tag.length + len > b.length) if(tag.length + len > MAX_FRAME_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
System.arraycopy(b, 0, b, tag.length, len); System.arraycopy(tag, 0, segment.getBuffer(), 0, tag.length);
System.arraycopy(tag, 0, b, 0, tag.length);
capacity -= tag.length; capacity -= tag.length;
tagWritten = true; tagWritten = true;
offset = tag.length; offset = tag.length;
@@ -49,13 +53,15 @@ class SegmentedConnectionEncrypter implements ConnectionEncrypter {
IvParameterSpec ivSpec = new IvParameterSpec(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv);
try { try {
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
int encrypted = frameCipher.doFinal(b, offset, len, b, offset); int encrypted = frameCipher.doFinal(b, 0, len, segment.getBuffer(),
assert encrypted == len; offset);
if(encrypted != len) throw new RuntimeException();
} catch(GeneralSecurityException badCipher) { } catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher); throw new RuntimeException(badCipher);
} }
segment.setLength(offset + len);
try { try {
out.writeFrame(b, offset + len); out.writeSegment(segment);
} catch(IOException e) { } catch(IOException e) {
frameKey.erase(); frameKey.erase();
throw e; throw e;

View File

@@ -49,7 +49,7 @@
<test name='net.sf.briar.serial.ReaderImplTest'/> <test name='net.sf.briar.serial.ReaderImplTest'/>
<test name='net.sf.briar.serial.WriterImplTest'/> <test name='net.sf.briar.serial.WriterImplTest'/>
<test name='net.sf.briar.setup.SetupWorkerTest'/> <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.ConnectionEncrypterImplTest'/>
<test name='net.sf.briar.transport.ConnectionReaderImplTest'/> <test name='net.sf.briar.transport.ConnectionReaderImplTest'/>
<test name='net.sf.briar.transport.ConnectionRecogniserImplTest'/> <test name='net.sf.briar.transport.ConnectionRecogniserImplTest'/>

View File

@@ -11,7 +11,6 @@ import javax.crypto.spec.IvParameterSpec;
import net.sf.briar.BriarTestCase; import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey; import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.plugins.FrameSource;
import net.sf.briar.crypto.CryptoModule; import net.sf.briar.crypto.CryptoModule;
import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -20,14 +19,14 @@ import org.junit.Test;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
public class ConnectionDecrypterImplTest extends BriarTestCase { public class ConnectionDecrypterTest extends BriarTestCase {
private static final int MAC_LENGTH = 32; private static final int MAC_LENGTH = 32;
private final Cipher frameCipher; private final Cipher frameCipher;
private final ErasableKey frameKey; private final ErasableKey frameKey;
public ConnectionDecrypterImplTest() { public ConnectionDecrypterTest() {
super(); super();
Injector i = Guice.createInjector(new CryptoModule()); Injector i = Guice.createInjector(new CryptoModule());
CryptoComponent crypto = i.getInstance(CryptoComponent.class); CryptoComponent crypto = i.getInstance(CryptoComponent.class);

View File

@@ -8,7 +8,6 @@ import java.io.ByteArrayInputStream;
import net.sf.briar.TestUtils; import net.sf.briar.TestUtils;
import net.sf.briar.api.FormatException; import net.sf.briar.api.FormatException;
import net.sf.briar.api.plugins.FrameSource;
import net.sf.briar.api.transport.ConnectionReader; import net.sf.briar.api.transport.ConnectionReader;
import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.commons.io.output.ByteArrayOutputStream;

View File

@@ -15,7 +15,6 @@ import javax.crypto.Mac;
import net.sf.briar.BriarTestCase; import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey; 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.ConnectionReader;
import net.sf.briar.api.transport.ConnectionWriter; import net.sf.briar.api.transport.ConnectionWriter;
import net.sf.briar.crypto.CryptoModule; import net.sf.briar.crypto.CryptoModule;

View File

@@ -8,7 +8,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import net.sf.briar.api.FormatException; import net.sf.briar.api.FormatException;
import net.sf.briar.api.plugins.FrameSource;
/** A connection decrypter that performs no decryption. */ /** A connection decrypter that performs no decryption. */
class NullConnectionDecrypter implements FrameSource { class NullConnectionDecrypter implements FrameSource {

View File

@@ -11,7 +11,8 @@ import javax.crypto.spec.IvParameterSpec;
import net.sf.briar.BriarTestCase; import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey; 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 net.sf.briar.crypto.CryptoModule;
import org.junit.Test; import org.junit.Test;
@@ -53,7 +54,7 @@ public class SegmentedConnectionDecrypterTest extends BriarTestCase {
plaintext1.length); plaintext1.length);
// Use a connection decrypter to decrypt the ciphertext // Use a connection decrypter to decrypt the ciphertext
byte[][] frames = new byte[][] { ciphertext, ciphertext1 }; byte[][] frames = new byte[][] { ciphertext, ciphertext1 };
FrameSource in = new ByteArrayFrameSource(frames); SegmentSource in = new ByteArraySegmentSource(frames);
FrameSource decrypter = new SegmentedConnectionDecrypter(in, FrameSource decrypter = new SegmentedConnectionDecrypter(in,
frameCipher, frameKey, MAC_LENGTH); frameCipher, frameKey, MAC_LENGTH);
// First frame // 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 final byte[][] frames;
private int frame = 0; private int frame = 0;
private ByteArrayFrameSource(byte[][] frames) { private ByteArraySegmentSource(byte[][] frames) {
this.frames = frames; this.frames = frames;
} }
public int readFrame(byte[] b) throws IOException { public boolean readSegment(Segment s) throws IOException {
byte[] src = frames[frame++]; if(frame == frames.length) return false;
System.arraycopy(src, 0, b, 0, src.length); byte[] src = frames[frame];
return src.length; System.arraycopy(src, 0, s.getBuffer(), 0, src.length);
s.setLength(src.length);
s.setTransmissionNumber(frame);
frame++;
return true;
} }
} }
} }

View File

@@ -11,8 +11,8 @@ import javax.crypto.spec.IvParameterSpec;
import net.sf.briar.BriarTestCase; import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey; import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.plugins.FrameSink; import net.sf.briar.api.plugins.Segment;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH; import net.sf.briar.api.plugins.SegmentSink;
import net.sf.briar.crypto.CryptoModule; import net.sf.briar.crypto.CryptoModule;
import org.junit.Test; import org.junit.Test;
@@ -60,13 +60,11 @@ public class SegmentedConnectionEncrypterTest extends BriarTestCase {
out.write(ciphertext1); out.write(ciphertext1);
byte[] expected = out.toByteArray(); byte[] expected = out.toByteArray();
// Use a connection encrypter to encrypt the plaintext // Use a connection encrypter to encrypt the plaintext
ByteArrayFrameSink sink = new ByteArrayFrameSink(); ByteArraySegmentSink sink = new ByteArraySegmentSink();
ConnectionEncrypter e = new SegmentedConnectionEncrypter(sink, ConnectionEncrypter e = new SegmentedConnectionEncrypter(sink,
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey); Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey);
// The first frame's buffer must have enough space for the tag // The first frame's buffer must have enough space for the tag
byte[] b = new byte[TAG_LENGTH + plaintext.length]; e.writeFrame(plaintext, plaintext.length);
System.arraycopy(plaintext, 0, b, 0, plaintext.length);
e.writeFrame(b, plaintext.length);
e.writeFrame(plaintext1, plaintext1.length); e.writeFrame(plaintext1, plaintext1.length);
byte[] actual = out.toByteArray(); byte[] actual = out.toByteArray();
// Check that the actual ciphertext matches the expected ciphertext // 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()); assertEquals(Long.MAX_VALUE - actual.length, e.getRemainingCapacity());
} }
private static class ByteArrayFrameSink extends ByteArrayOutputStream private static class ByteArraySegmentSink extends ByteArrayOutputStream
implements FrameSink { implements SegmentSink {
public void writeFrame(byte[] b, int len) throws IOException { public void writeSegment(Segment s) throws IOException {
write(b, 0, len); write(s.getBuffer(), 0, s.getLength());
} }
} }
} }