Code cleanup for the transport protocol.

This commit is contained in:
akwizgran
2012-05-24 19:08:53 +01:00
parent d6b260ed61
commit 3eb3f8be3d
20 changed files with 158 additions and 219 deletions

View File

@@ -9,7 +9,7 @@ public interface TransportConstants {
static final int MAX_FRAME_LENGTH = 65536; // 2^16, 64 KiB
/** The length of the frame header in bytes. */
static final int FRAME_HEADER_LENGTH = 9;
static final int HEADER_LENGTH = 9;
/** The length of the MAC in bytes. */
static final int MAC_LENGTH = 16;

View File

@@ -34,7 +34,7 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
Cipher framePeekingCipher = crypto.getFramePeekingCipher();
IvEncoder frameIvEncoder = crypto.getFrameIvEncoder();
IvEncoder framePeekingIvEncoder = crypto.getFramePeekingIvEncoder();
FrameReader encryption = new IncomingEncryptionLayerImpl(in, tagCipher,
FrameReader encryption = new IncomingEncryptionLayer(in, tagCipher,
frameCipher, framePeekingCipher, frameIvEncoder,
framePeekingIvEncoder, tagKey, frameKey, !initiator);
return new ConnectionReaderImpl(encryption);

View File

@@ -1,6 +1,7 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.io.IOException;
import java.io.InputStream;
@@ -11,14 +12,14 @@ import net.sf.briar.api.transport.ConnectionReader;
class ConnectionReaderImpl extends InputStream implements ConnectionReader {
private final FrameReader in;
private final Frame frame;
private final byte[] frame;
private int offset = 0, length = 0;
ConnectionReaderImpl(FrameReader in) {
this.in = in;
frame = new Frame();
offset = FRAME_HEADER_LENGTH;
frame = new byte[MAX_FRAME_LENGTH];
offset = HEADER_LENGTH;
}
public InputStream getInputStream() {
@@ -29,7 +30,7 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
public int read() throws IOException {
if(length == -1) return -1;
while(length == 0) if(!readFrame()) return -1;
int b = frame.getBuffer()[offset] & 0xff;
int b = frame[offset] & 0xff;
offset++;
length--;
return b;
@@ -45,7 +46,7 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
if(length == -1) return -1;
while(length == 0) if(!readFrame()) return -1;
len = Math.min(len, length);
System.arraycopy(frame.getBuffer(), offset, b, off, len);
System.arraycopy(frame, offset, b, off, len);
offset += len;
length -= len;
return len;
@@ -53,19 +54,17 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
private boolean readFrame() throws IOException {
assert length == 0;
byte[] buf = frame.getBuffer();
if(HeaderEncoder.isLastFrame(buf)) {
if(HeaderEncoder.isLastFrame(frame)) {
length = -1;
return false;
}
frame.reset();
if(!in.readFrame(frame)) throw new FormatException();
offset = FRAME_HEADER_LENGTH;
length = HeaderEncoder.getPayloadLength(buf);
offset = HEADER_LENGTH;
length = HeaderEncoder.getPayloadLength(frame);
// The padding must be all zeroes
int padding = HeaderEncoder.getPaddingLength(buf);
int padding = HeaderEncoder.getPaddingLength(frame);
for(int i = offset + length; i < offset + length + padding; i++) {
if(buf[i] != 0) throw new FormatException();
if(frame[i] != 0) throw new FormatException();
}
return true;
}

View File

@@ -32,7 +32,7 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
Cipher tagCipher = crypto.getTagCipher();
Cipher frameCipher = crypto.getFrameCipher();
IvEncoder frameIvEncoder = crypto.getFrameIvEncoder();
FrameWriter encryption = new OutgoingEncryptionLayerImpl(
FrameWriter encryption = new OutgoingEncryptionLayer(
out, capacity, tagCipher, frameCipher, frameIvEncoder, tagKey,
frameKey);
return new ConnectionWriterImpl(encryption);

View File

@@ -1,6 +1,6 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
@@ -19,15 +19,15 @@ import net.sf.briar.api.transport.ConnectionWriter;
class ConnectionWriterImpl extends OutputStream implements ConnectionWriter {
private final FrameWriter out;
private final Frame frame;
private final byte[] frame;
private int offset;
private long frameNumber;
ConnectionWriterImpl(FrameWriter out) {
this.out = out;
frame = new Frame();
offset = FRAME_HEADER_LENGTH;
frame = new byte[MAX_FRAME_LENGTH];
offset = HEADER_LENGTH;
frameNumber = 0L;
}
@@ -38,29 +38,29 @@ class ConnectionWriterImpl extends OutputStream implements ConnectionWriter {
public long getRemainingCapacity() {
long capacity = out.getRemainingCapacity();
// If there's any data buffered, subtract it and its overhead
if(offset > FRAME_HEADER_LENGTH) capacity -= offset + MAC_LENGTH;
if(offset > HEADER_LENGTH) capacity -= offset + MAC_LENGTH;
// Subtract the overhead from the remaining capacity
long frames = (long) Math.ceil((double) capacity / MAX_FRAME_LENGTH);
int overheadPerFrame = FRAME_HEADER_LENGTH + MAC_LENGTH;
int overheadPerFrame = HEADER_LENGTH + MAC_LENGTH;
return Math.max(0L, capacity - frames * overheadPerFrame);
}
@Override
public void close() throws IOException {
if(offset > FRAME_HEADER_LENGTH || frameNumber > 0L) writeFrame(true);
if(offset > HEADER_LENGTH || frameNumber > 0L) writeFrame(true);
out.flush();
super.close();
}
@Override
public void flush() throws IOException {
if(offset > FRAME_HEADER_LENGTH) writeFrame(false);
if(offset > HEADER_LENGTH) writeFrame(false);
out.flush();
}
@Override
public void write(int b) throws IOException {
frame.getBuffer()[offset++] = (byte) b;
frame[offset++] = (byte) b;
if(offset + MAC_LENGTH == MAX_FRAME_LENGTH) writeFrame(false);
}
@@ -71,30 +71,26 @@ class ConnectionWriterImpl extends OutputStream implements ConnectionWriter {
@Override
public void write(byte[] b, int off, int len) throws IOException {
byte[] buf = frame.getBuffer();
int available = MAX_FRAME_LENGTH - offset - MAC_LENGTH;
while(available <= len) {
System.arraycopy(b, off, buf, offset, available);
System.arraycopy(b, off, frame, offset, available);
offset += available;
writeFrame(false);
off += available;
len -= available;
available = MAX_FRAME_LENGTH - offset - MAC_LENGTH;
}
System.arraycopy(b, off, buf, offset, len);
System.arraycopy(b, off, frame, offset, len);
offset += len;
}
private void writeFrame(boolean lastFrame) throws IOException {
if(frameNumber > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
int payload = offset - FRAME_HEADER_LENGTH;
int payload = offset - HEADER_LENGTH;
assert payload >= 0;
HeaderEncoder.encodeHeader(frame.getBuffer(), frameNumber, payload, 0,
lastFrame);
frame.setLength(offset);
HeaderEncoder.encodeHeader(frame, frameNumber, payload, 0, lastFrame);
out.writeFrame(frame);
frame.reset();
offset = FRAME_HEADER_LENGTH;
offset = HEADER_LENGTH;
frameNumber++;
}
}

View File

@@ -1,34 +0,0 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
class Frame {
private final byte[] buf;
private int length = -1;
Frame() {
buf = new byte[MAX_FRAME_LENGTH];
}
public byte[] getBuffer() {
return buf;
}
public int getLength() {
if(length == -1) throw new IllegalStateException();
return length;
}
public void setLength(int length) {
if(length < FRAME_HEADER_LENGTH || length > buf.length)
throw new IllegalArgumentException();
this.length = length;
}
public void reset() {
length = -1;
}
}

View File

@@ -8,5 +8,5 @@ interface FrameReader {
* Reads a frame into the given buffer. Returns false if no more frames can
* be read from the connection.
*/
boolean readFrame(Frame f) throws IOException;
boolean readFrame(byte[] frame) throws IOException;
}

View File

@@ -5,7 +5,7 @@ import java.io.IOException;
interface FrameWriter {
/** Writes the given frame. */
void writeFrame(Frame f) throws IOException;
void writeFrame(byte[] frame) throws IOException;
/** Flushes the stack. */
void flush() throws IOException;

View File

@@ -1,6 +1,6 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
import net.sf.briar.util.ByteUtils;
@@ -8,7 +8,7 @@ class HeaderEncoder {
static void encodeHeader(byte[] header, long frameNumber, int payload,
int padding, boolean lastFrame) {
if(header.length < FRAME_HEADER_LENGTH)
if(header.length < HEADER_LENGTH)
throw new IllegalArgumentException();
if(frameNumber < 0 || frameNumber > ByteUtils.MAX_32_BIT_UNSIGNED)
throw new IllegalArgumentException();
@@ -23,37 +23,32 @@ class HeaderEncoder {
}
static boolean checkHeader(byte[] header, int length) {
if(header.length < FRAME_HEADER_LENGTH)
throw new IllegalArgumentException();
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
int payload = getPayloadLength(header);
int padding = getPaddingLength(header);
if(FRAME_HEADER_LENGTH + payload + padding + MAC_LENGTH != length)
if(HEADER_LENGTH + payload + padding + MAC_LENGTH != length)
return false;
if(header[8] != 0 && header[8] != 1) return false;
return true;
}
static long getFrameNumber(byte[] header) {
if(header.length < FRAME_HEADER_LENGTH)
throw new IllegalArgumentException();
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
return ByteUtils.readUint32(header, 0);
}
static int getPayloadLength(byte[] header) {
if(header.length < FRAME_HEADER_LENGTH)
throw new IllegalArgumentException();
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
return ByteUtils.readUint16(header, 4);
}
static int getPaddingLength(byte[] header) {
if(header.length < FRAME_HEADER_LENGTH)
throw new IllegalArgumentException();
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
return ByteUtils.readUint16(header, 6);
}
static boolean isLastFrame(byte[] header) {
if(header.length < FRAME_HEADER_LENGTH)
throw new IllegalArgumentException();
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
return header[8] == 1;
}
}

View File

@@ -1,6 +1,6 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
@@ -17,7 +17,7 @@ import net.sf.briar.api.FormatException;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.crypto.IvEncoder;
class IncomingEncryptionLayerImpl implements FrameReader {
class IncomingEncryptionLayer implements FrameReader {
private final InputStream in;
private final Cipher tagCipher, frameCipher, framePeekingCipher;
@@ -29,7 +29,7 @@ class IncomingEncryptionLayerImpl implements FrameReader {
private boolean readTag;
private long frameNumber;
IncomingEncryptionLayerImpl(InputStream in, Cipher tagCipher,
IncomingEncryptionLayer(InputStream in, Cipher tagCipher,
Cipher frameCipher, Cipher framePeekingCipher,
IvEncoder frameIvEncoder, IvEncoder framePeekingIvEncoder,
ErasableKey tagKey, ErasableKey frameKey, boolean readTag) {
@@ -43,15 +43,14 @@ class IncomingEncryptionLayerImpl implements FrameReader {
this.frameKey = frameKey;
this.readTag = readTag;
blockSize = frameCipher.getBlockSize();
if(blockSize < FRAME_HEADER_LENGTH)
throw new IllegalArgumentException();
if(blockSize < HEADER_LENGTH) throw new IllegalArgumentException();
frameIv = frameIvEncoder.encodeIv(0L);
framePeekingIv = framePeekingIvEncoder.encodeIv(0L);
ciphertext = new byte[MAX_FRAME_LENGTH];
frameNumber = 0L;
}
public boolean readFrame(Frame f) throws IOException {
public boolean readFrame(byte[] frame) throws IOException {
try {
// Read the tag if it hasn't already been read
if(readTag) {
@@ -79,19 +78,18 @@ class IncomingEncryptionLayerImpl implements FrameReader {
// Decrypt the first block of the frame to peek at the header
framePeekingIvEncoder.updateIv(framePeekingIv, frameNumber);
IvParameterSpec ivSpec = new IvParameterSpec(framePeekingIv);
byte[] plaintext = f.getBuffer();
try {
framePeekingCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
int decrypted = framePeekingCipher.update(ciphertext, 0,
blockSize, plaintext);
blockSize, frame);
if(decrypted != blockSize) throw new RuntimeException();
} catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher);
}
// Parse the frame header
int payload = HeaderEncoder.getPayloadLength(plaintext);
int padding = HeaderEncoder.getPaddingLength(plaintext);
int length = FRAME_HEADER_LENGTH + payload + padding + MAC_LENGTH;
int payload = HeaderEncoder.getPayloadLength(frame);
int padding = HeaderEncoder.getPaddingLength(frame);
int length = HEADER_LENGTH + payload + padding + MAC_LENGTH;
if(length > MAX_FRAME_LENGTH) throw new FormatException();
// Read the remainder of the frame
while(offset < length) {
@@ -105,13 +103,12 @@ class IncomingEncryptionLayerImpl implements FrameReader {
try {
frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
int decrypted = frameCipher.doFinal(ciphertext, 0, length,
plaintext);
frame);
if(decrypted != length - MAC_LENGTH)
throw new RuntimeException();
} catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher);
}
f.setLength(length - MAC_LENGTH);
frameNumber++;
return true;
} catch(IOException e) {

View File

@@ -1,5 +1,6 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
@@ -14,7 +15,7 @@ import javax.crypto.spec.IvParameterSpec;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.api.crypto.IvEncoder;
class OutgoingEncryptionLayerImpl implements FrameWriter {
class OutgoingEncryptionLayer implements FrameWriter {
private final OutputStream out;
private final Cipher tagCipher, frameCipher;
@@ -24,9 +25,9 @@ class OutgoingEncryptionLayerImpl implements FrameWriter {
private long capacity, frameNumber;
OutgoingEncryptionLayerImpl(OutputStream out, long capacity,
Cipher tagCipher, Cipher frameCipher, IvEncoder frameIvEncoder,
ErasableKey tagKey, ErasableKey frameKey) {
OutgoingEncryptionLayer(OutputStream out, long capacity, Cipher tagCipher,
Cipher frameCipher, IvEncoder frameIvEncoder, ErasableKey tagKey,
ErasableKey frameKey) {
this.out = out;
this.capacity = capacity;
this.tagCipher = tagCipher;
@@ -39,9 +40,10 @@ class OutgoingEncryptionLayerImpl implements FrameWriter {
frameNumber = 0L;
}
public void writeFrame(Frame f) throws IOException {
byte[] plaintext = f.getBuffer();
int offset = 0, length = f.getLength();
public void writeFrame(byte[] frame) throws IOException {
int payload = HeaderEncoder.getPayloadLength(frame);
int padding = HeaderEncoder.getPaddingLength(frame);
int offset = 0, length = HEADER_LENGTH + payload + padding;
if(frameNumber == 0) {
TagEncoder.encodeTag(ciphertext, tagCipher, tagKey);
offset = TAG_LENGTH;
@@ -50,8 +52,8 @@ class OutgoingEncryptionLayerImpl implements FrameWriter {
IvParameterSpec ivSpec = new IvParameterSpec(frameIv);
try {
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
int encrypted = frameCipher.doFinal(plaintext, 0, length,
ciphertext, offset);
int encrypted = frameCipher.doFinal(frame, 0, length, ciphertext,
offset);
if(encrypted != length + MAC_LENGTH) throw new RuntimeException();
} catch(GeneralSecurityException badCipher) {
throw new RuntimeException(badCipher);

View File

@@ -55,8 +55,8 @@
<test name='net.sf.briar.transport.ConnectionWriterImplTest'/>
<test name='net.sf.briar.transport.ConnectionWriterTest'/>
<test name='net.sf.briar.transport.FrameReadWriteTest'/>
<test name='net.sf.briar.transport.IncomingEncryptionLayerImplTest'/>
<test name='net.sf.briar.transport.OutgoingEncryptionLayerImplTest'/>
<test name='net.sf.briar.transport.IncomingEncryptionLayerTest'/>
<test name='net.sf.briar.transport.OutgoingEncryptionLayerTest'/>
<test name='net.sf.briar.util.ByteUtilsTest'/>
<test name='net.sf.briar.util.FileUtilsTest'/>
<test name='net.sf.briar.util.StringUtilsTest'/>

View File

@@ -1,6 +1,6 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.junit.Assert.assertArrayEquals;
@@ -8,6 +8,7 @@ import static org.junit.Assert.assertArrayEquals;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.transport.ConnectionReader;
@@ -15,7 +16,10 @@ import net.sf.briar.api.transport.ConnectionReader;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.junit.Test;
public class ConnectionReaderImplTest extends TransportTest {
public class ConnectionReaderImplTest extends BriarTestCase {
private static final int MAX_PAYLOAD_LENGTH =
MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
public ConnectionReaderImplTest() throws Exception {
super();
@@ -23,7 +27,7 @@ public class ConnectionReaderImplTest extends TransportTest {
@Test
public void testLengthZero() throws Exception {
byte[] frame = new byte[FRAME_HEADER_LENGTH + MAC_LENGTH];
byte[] frame = new byte[HEADER_LENGTH + MAC_LENGTH];
HeaderEncoder.encodeHeader(frame, 0, 0, 0, true);
// Read the frame
ByteArrayInputStream in = new ByteArrayInputStream(frame);
@@ -34,7 +38,7 @@ public class ConnectionReaderImplTest extends TransportTest {
@Test
public void testLengthOne() throws Exception {
byte[] frame = new byte[FRAME_HEADER_LENGTH + 1 + MAC_LENGTH];
byte[] frame = new byte[HEADER_LENGTH + 1 + MAC_LENGTH];
HeaderEncoder.encodeHeader(frame, 0, 1, 0, true);
// Read the frame
ByteArrayInputStream in = new ByteArrayInputStream(frame);
@@ -100,12 +104,12 @@ public class ConnectionReaderImplTest extends TransportTest {
@Test
public void testNonZeroPadding() throws Exception {
int payloadLength = 10, paddingLength = 10;
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
+ paddingLength + MAC_LENGTH];
byte[] frame = new byte[HEADER_LENGTH + payloadLength + paddingLength
+ MAC_LENGTH];
HeaderEncoder.encodeHeader(frame, 0, payloadLength, paddingLength,
false);
// Set a byte of the padding to a non-zero value
frame[FRAME_HEADER_LENGTH + payloadLength] = 1;
frame[HEADER_LENGTH + payloadLength] = 1;
// Read the frame
ByteArrayInputStream in = new ByteArrayInputStream(frame);
ConnectionReader r = createConnectionReader(in);
@@ -120,13 +124,11 @@ public class ConnectionReaderImplTest extends TransportTest {
public void testMultipleFrames() throws Exception {
// First frame: 123-byte payload
int payloadLength = 123;
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
+ MAC_LENGTH];
byte[] frame = new byte[HEADER_LENGTH + payloadLength + MAC_LENGTH];
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0, false);
// Second frame: 1234-byte payload
int payloadLength1 = 1234;
byte[] frame1 = new byte[FRAME_HEADER_LENGTH + payloadLength1
+ MAC_LENGTH];
byte[] frame1 = new byte[HEADER_LENGTH + payloadLength1 + MAC_LENGTH];
HeaderEncoder.encodeHeader(frame1, 1, payloadLength1, 0, true);
// Concatenate the frames
ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -148,13 +150,11 @@ public class ConnectionReaderImplTest extends TransportTest {
public void testLastFrameNotMarkedAsSuch() throws Exception {
// First frame: 123-byte payload
int payloadLength = 123;
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
+ MAC_LENGTH];
byte[] frame = new byte[HEADER_LENGTH + payloadLength + MAC_LENGTH];
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0, false);
// Second frame: 1234-byte payload
int payloadLength1 = 1234;
byte[] frame1 = new byte[FRAME_HEADER_LENGTH + payloadLength1
+ MAC_LENGTH];
byte[] frame1 = new byte[HEADER_LENGTH + payloadLength1 + MAC_LENGTH];
HeaderEncoder.encodeHeader(frame1, 1, payloadLength1, 0, false);
// Concatenate the frames
ByteArrayOutputStream out = new ByteArrayOutputStream();

View File

@@ -1,6 +1,6 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static org.junit.Assert.assertArrayEquals;
@@ -8,11 +8,15 @@ import static org.junit.Assert.assertArrayEquals;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.transport.ConnectionWriter;
import org.junit.Test;
public class ConnectionWriterImplTest extends TransportTest {
public class ConnectionWriterImplTest extends BriarTestCase {
private static final int MAX_PAYLOAD_LENGTH =
MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
public ConnectionWriterImplTest() throws Exception {
super();
@@ -31,7 +35,7 @@ public class ConnectionWriterImplTest extends TransportTest {
@Test
public void testSingleByteFrame() throws Exception {
// Create a single-byte frame
byte[] frame = new byte[FRAME_HEADER_LENGTH + 1 + MAC_LENGTH];
byte[] frame = new byte[HEADER_LENGTH + 1 + MAC_LENGTH];
HeaderEncoder.encodeHeader(frame, 0, 1, 0, false);
// Check that the ConnectionWriter gets the same results
ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -70,10 +74,10 @@ public class ConnectionWriterImplTest extends TransportTest {
@Test
public void testMultipleFrames() throws Exception {
// First frame: 123-byte payload
byte[] frame = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
byte[] frame = new byte[HEADER_LENGTH + 123 + MAC_LENGTH];
HeaderEncoder.encodeHeader(frame, 0, 123, 0, false);
// Second frame: 1234-byte payload
byte[] frame1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH];
byte[] frame1 = new byte[HEADER_LENGTH + 1234 + MAC_LENGTH];
HeaderEncoder.encodeHeader(frame1, 1, 1234, 0, false);
// Concatenate the frames
ByteArrayOutputStream out = new ByteArrayOutputStream();

View File

@@ -74,7 +74,7 @@ public class FrameReadWriteTest extends BriarTestCase {
ErasableKey frameCopy = frameKey.copy();
// Write the frames
ByteArrayOutputStream out = new ByteArrayOutputStream();
FrameWriter encryptionOut = new OutgoingEncryptionLayerImpl(out,
FrameWriter encryptionOut = new OutgoingEncryptionLayer(out,
Long.MAX_VALUE, tagCipher, frameCipher, frameIvEncoder, tagCopy,
frameCopy);
ConnectionWriter writer = new ConnectionWriterImpl(encryptionOut);
@@ -90,7 +90,7 @@ public class FrameReadWriteTest extends BriarTestCase {
assertArrayEquals(tag, recoveredTag);
assertTrue(TagEncoder.decodeTag(recoveredTag, tagCipher, tagKey));
// Read the frames back
FrameReader encryptionIn = new IncomingEncryptionLayerImpl(in,
FrameReader encryptionIn = new IncomingEncryptionLayer(in,
tagCipher, frameCipher, framePeekingCipher, frameIvEncoder,
framePeekingIvEncoder, tagKey, frameKey, false);
ConnectionReader reader = new ConnectionReaderImpl(encryptionIn);

View File

@@ -1,6 +1,7 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayInputStream;
@@ -20,13 +21,13 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class IncomingEncryptionLayerImplTest extends BriarTestCase {
public class IncomingEncryptionLayerTest extends BriarTestCase {
private final Cipher tagCipher, frameCipher, framePeekingCipher;
private final IvEncoder frameIvEncoder, framePeekingIvEncoder;
private final ErasableKey tagKey, frameKey;
public IncomingEncryptionLayerImplTest() {
public IncomingEncryptionLayerTest() {
super();
Injector i = Guice.createInjector(new CryptoModule());
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
@@ -45,14 +46,14 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
byte[] tag = new byte[TAG_LENGTH];
TagEncoder.encodeTag(tag, tagCipher, tagKey);
// Calculate the ciphertext for the first frame
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123];
byte[] plaintext = new byte[HEADER_LENGTH + 123];
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0, false);
byte[] iv = frameIvEncoder.encodeIv(0L);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
byte[] ciphertext = frameCipher.doFinal(plaintext);
// Calculate the ciphertext for the second frame
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234];
byte[] plaintext1 = new byte[HEADER_LENGTH + 1234];
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0, false);
frameIvEncoder.updateIv(iv, 1L);
ivSpec = new IvParameterSpec(iv);
@@ -66,39 +67,45 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
out.write(ciphertext1);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
// Use the encryption layer to decrypt the ciphertext
FrameReader decrypter = new IncomingEncryptionLayerImpl(in, tagCipher,
FrameReader decrypter = new IncomingEncryptionLayer(in, tagCipher,
frameCipher, framePeekingCipher, frameIvEncoder,
framePeekingIvEncoder, tagKey, frameKey, true);
// First frame
Frame f = new Frame();
assertTrue(decrypter.readFrame(f));
assertEquals(plaintext.length, f.getLength());
byte[] decrypted = f.getBuffer();
assertEquals(0L, HeaderEncoder.getFrameNumber(decrypted));
byte[] frame = new byte[MAX_FRAME_LENGTH];
assertTrue(decrypter.readFrame(frame));
assertEquals(0L, HeaderEncoder.getFrameNumber(frame));
int payload = HeaderEncoder.getPayloadLength(frame);
assertEquals(123, payload);
int padding = HeaderEncoder.getPaddingLength(frame);
assertEquals(0, padding);
assertEquals(plaintext.length, HEADER_LENGTH + payload + padding);
for(int i = 0; i < plaintext.length; i++) {
assertEquals(plaintext[i], decrypted[i]);
assertEquals(plaintext[i], frame[i]);
}
// Second frame
assertTrue(decrypter.readFrame(f));
assertEquals(plaintext1.length, f.getLength());
decrypted = f.getBuffer();
assertEquals(1L, HeaderEncoder.getFrameNumber(decrypted));
assertTrue(decrypter.readFrame(frame));
assertEquals(1L, HeaderEncoder.getFrameNumber(frame));
payload = HeaderEncoder.getPayloadLength(frame);
assertEquals(1234, payload);
padding = HeaderEncoder.getPaddingLength(frame);
assertEquals(0, padding);
assertEquals(plaintext1.length, HEADER_LENGTH + payload + padding);
for(int i = 0; i < plaintext1.length; i++) {
assertEquals(plaintext1[i], decrypted[i]);
assertEquals(plaintext1[i], frame[i]);
}
}
@Test
public void testDecryptionWithoutTag() throws Exception {
// Calculate the ciphertext for the first frame
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123];
byte[] plaintext = new byte[HEADER_LENGTH + 123];
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0, false);
byte[] iv = frameIvEncoder.encodeIv(0L);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
byte[] ciphertext = frameCipher.doFinal(plaintext);
// Calculate the ciphertext for the second frame
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234];
byte[] plaintext1 = new byte[HEADER_LENGTH + 1234];
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0, false);
frameIvEncoder.updateIv(iv, 1L);
ivSpec = new IvParameterSpec(iv);
@@ -111,25 +118,31 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
out.write(ciphertext1);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
// Use the encryption layer to decrypt the ciphertext
FrameReader decrypter = new IncomingEncryptionLayerImpl(in, tagCipher,
FrameReader decrypter = new IncomingEncryptionLayer(in, tagCipher,
frameCipher, framePeekingCipher, frameIvEncoder,
framePeekingIvEncoder, tagKey, frameKey, false);
// First frame
Frame f = new Frame();
assertTrue(decrypter.readFrame(f));
assertEquals(plaintext.length, f.getLength());
byte[] decrypted = f.getBuffer();
assertEquals(0L, HeaderEncoder.getFrameNumber(decrypted));
byte[] frame = new byte[MAX_FRAME_LENGTH];
assertTrue(decrypter.readFrame(frame));
assertEquals(0L, HeaderEncoder.getFrameNumber(frame));
int payload = HeaderEncoder.getPayloadLength(frame);
assertEquals(123, payload);
int padding = HeaderEncoder.getPaddingLength(frame);
assertEquals(0, padding);
assertEquals(plaintext.length, HEADER_LENGTH + payload + padding);
for(int i = 0; i < plaintext.length; i++) {
assertEquals(plaintext[i], decrypted[i]);
assertEquals(plaintext[i], frame[i]);
}
// Second frame
assertTrue(decrypter.readFrame(f));
assertEquals(plaintext1.length, f.getLength());
assertEquals(1L, HeaderEncoder.getFrameNumber(decrypted));
decrypted = f.getBuffer();
assertTrue(decrypter.readFrame(frame));
assertEquals(1L, HeaderEncoder.getFrameNumber(frame));
payload = HeaderEncoder.getPayloadLength(frame);
assertEquals(1234, payload);
padding = HeaderEncoder.getPaddingLength(frame);
assertEquals(0, padding);
assertEquals(plaintext1.length, HEADER_LENGTH + payload + padding);
for(int i = 0; i < plaintext1.length; i++) {
assertEquals(plaintext1[i], decrypted[i]);
assertEquals(plaintext1[i], frame[i]);
}
}
}

View File

@@ -1,6 +1,6 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
@@ -19,12 +19,11 @@ class NullIncomingEncryptionLayer implements FrameReader {
this.in = in;
}
public boolean readFrame(Frame f) throws IOException {
byte[] buf = f.getBuffer();
public boolean readFrame(byte[] frame) throws IOException {
// Read the frame header
int offset = 0, length = FRAME_HEADER_LENGTH;
int offset = 0, length = HEADER_LENGTH;
while(offset < length) {
int read = in.read(buf, offset, length - offset);
int read = in.read(frame, offset, length - offset);
if(read == -1) {
if(offset == 0) return false;
throw new EOFException();
@@ -32,17 +31,16 @@ class NullIncomingEncryptionLayer implements FrameReader {
offset += read;
}
// Parse the frame header
int payload = HeaderEncoder.getPayloadLength(buf);
int padding = HeaderEncoder.getPaddingLength(buf);
length = FRAME_HEADER_LENGTH + payload + padding + MAC_LENGTH;
int payload = HeaderEncoder.getPayloadLength(frame);
int padding = HeaderEncoder.getPaddingLength(frame);
length = HEADER_LENGTH + payload + padding + MAC_LENGTH;
if(length > MAX_FRAME_LENGTH) throw new FormatException();
// Read the remainder of the frame
while(offset < length) {
int read = in.read(buf, offset, length - offset);
int read = in.read(frame, offset, length - offset);
if(read == -1) throw new EOFException();
offset += read;
}
f.setLength(length - MAC_LENGTH);
return true;
}
}

View File

@@ -1,5 +1,6 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
import java.io.IOException;
@@ -22,9 +23,12 @@ class NullOutgoingEncryptionLayer implements FrameWriter {
this.capacity = capacity;
}
public void writeFrame(Frame f) throws IOException {
out.write(f.getBuffer(), 0, f.getLength() + MAC_LENGTH);
capacity -= f.getLength();
public void writeFrame(byte[] frame) throws IOException {
int payload = HeaderEncoder.getPayloadLength(frame);
int padding = HeaderEncoder.getPaddingLength(frame);
int length = HEADER_LENGTH + payload + padding + MAC_LENGTH;
out.write(frame, 0, length);
capacity -= length;
}
public void flush() throws IOException {

View File

@@ -1,5 +1,6 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import static org.junit.Assert.assertArrayEquals;
@@ -19,13 +20,13 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
public class OutgoingEncryptionLayerTest extends BriarTestCase {
private final Cipher tagCipher, frameCipher;
private final IvEncoder frameIvEncoder;
private final ErasableKey tagKey, frameKey;
public OutgoingEncryptionLayerImplTest() {
public OutgoingEncryptionLayerTest() {
super();
Injector i = Guice.createInjector(new CryptoModule());
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
@@ -43,12 +44,14 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
TagEncoder.encodeTag(tag, tagCipher, tagKey);
// Calculate the expected ciphertext for the first frame
byte[] iv = frameIvEncoder.encodeIv(0L);
byte[] plaintext = new byte[123];
byte[] plaintext = new byte[HEADER_LENGTH + 123];
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0, false);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
byte[] ciphertext = frameCipher.doFinal(plaintext);
// Calculate the expected ciphertext for the second frame
byte[] plaintext1 = new byte[1234];
byte[] plaintext1 = new byte[HEADER_LENGTH + 1234];
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0, true);
frameIvEncoder.updateIv(iv, 1L);
ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
@@ -61,16 +64,10 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
byte[] expected = out.toByteArray();
// Use the encryption layer to encrypt the plaintext
out.reset();
FrameWriter encrypter = new OutgoingEncryptionLayerImpl(out,
Long.MAX_VALUE, tagCipher, frameCipher, frameIvEncoder, tagKey,
frameKey);
Frame f = new Frame();
System.arraycopy(plaintext, 0, f.getBuffer(), 0, plaintext.length);
f.setLength(plaintext.length);
encrypter.writeFrame(f);
System.arraycopy(plaintext1, 0, f.getBuffer(), 0, plaintext1.length);
f.setLength(plaintext1.length);
encrypter.writeFrame(f);
FrameWriter encrypter = new OutgoingEncryptionLayer(out, Long.MAX_VALUE,
tagCipher, frameCipher, frameIvEncoder, tagKey, frameKey);
encrypter.writeFrame(plaintext);
encrypter.writeFrame(plaintext1);
byte[] actual = out.toByteArray();
// Check that the actual ciphertext matches the expected ciphertext
assertArrayEquals(expected, actual);

View File

@@ -1,32 +0,0 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAC_LENGTH;
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import javax.crypto.Cipher;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.ErasableKey;
import net.sf.briar.crypto.CryptoModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
public abstract class TransportTest extends BriarTestCase {
static final int MAX_PAYLOAD_LENGTH =
MAX_FRAME_LENGTH - FRAME_HEADER_LENGTH - MAC_LENGTH;
protected final Cipher frameCipher;
protected final ErasableKey frameKey;
public TransportTest() throws Exception {
super();
Injector i = Guice.createInjector(new CryptoModule());
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
frameCipher = crypto.getFrameCipher();
frameKey = crypto.generateTestKey();
}
}