mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
Code cleanup for the transport protocol.
This commit is contained in:
@@ -9,7 +9,7 @@ public interface TransportConstants {
|
|||||||
static final int MAX_FRAME_LENGTH = 65536; // 2^16, 64 KiB
|
static final int MAX_FRAME_LENGTH = 65536; // 2^16, 64 KiB
|
||||||
|
|
||||||
/** The length of the frame header in bytes. */
|
/** 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. */
|
/** The length of the MAC in bytes. */
|
||||||
static final int MAC_LENGTH = 16;
|
static final int MAC_LENGTH = 16;
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
|
|||||||
Cipher framePeekingCipher = crypto.getFramePeekingCipher();
|
Cipher framePeekingCipher = crypto.getFramePeekingCipher();
|
||||||
IvEncoder frameIvEncoder = crypto.getFrameIvEncoder();
|
IvEncoder frameIvEncoder = crypto.getFrameIvEncoder();
|
||||||
IvEncoder framePeekingIvEncoder = crypto.getFramePeekingIvEncoder();
|
IvEncoder framePeekingIvEncoder = crypto.getFramePeekingIvEncoder();
|
||||||
FrameReader encryption = new IncomingEncryptionLayerImpl(in, tagCipher,
|
FrameReader encryption = new IncomingEncryptionLayer(in, tagCipher,
|
||||||
frameCipher, framePeekingCipher, frameIvEncoder,
|
frameCipher, framePeekingCipher, frameIvEncoder,
|
||||||
framePeekingIvEncoder, tagKey, frameKey, !initiator);
|
framePeekingIvEncoder, tagKey, frameKey, !initiator);
|
||||||
return new ConnectionReaderImpl(encryption);
|
return new ConnectionReaderImpl(encryption);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package net.sf.briar.transport;
|
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.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -11,14 +12,14 @@ import net.sf.briar.api.transport.ConnectionReader;
|
|||||||
class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
||||||
|
|
||||||
private final FrameReader in;
|
private final FrameReader in;
|
||||||
private final Frame frame;
|
private final byte[] frame;
|
||||||
|
|
||||||
private int offset = 0, length = 0;
|
private int offset = 0, length = 0;
|
||||||
|
|
||||||
ConnectionReaderImpl(FrameReader in) {
|
ConnectionReaderImpl(FrameReader in) {
|
||||||
this.in = in;
|
this.in = in;
|
||||||
frame = new Frame();
|
frame = new byte[MAX_FRAME_LENGTH];
|
||||||
offset = FRAME_HEADER_LENGTH;
|
offset = HEADER_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InputStream getInputStream() {
|
public InputStream getInputStream() {
|
||||||
@@ -29,7 +30,7 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
|||||||
public int read() throws IOException {
|
public int read() throws IOException {
|
||||||
if(length == -1) return -1;
|
if(length == -1) return -1;
|
||||||
while(length == 0) if(!readFrame()) return -1;
|
while(length == 0) if(!readFrame()) return -1;
|
||||||
int b = frame.getBuffer()[offset] & 0xff;
|
int b = frame[offset] & 0xff;
|
||||||
offset++;
|
offset++;
|
||||||
length--;
|
length--;
|
||||||
return b;
|
return b;
|
||||||
@@ -45,7 +46,7 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
|||||||
if(length == -1) return -1;
|
if(length == -1) return -1;
|
||||||
while(length == 0) if(!readFrame()) return -1;
|
while(length == 0) if(!readFrame()) return -1;
|
||||||
len = Math.min(len, length);
|
len = Math.min(len, length);
|
||||||
System.arraycopy(frame.getBuffer(), offset, b, off, len);
|
System.arraycopy(frame, offset, b, off, len);
|
||||||
offset += len;
|
offset += len;
|
||||||
length -= len;
|
length -= len;
|
||||||
return len;
|
return len;
|
||||||
@@ -53,19 +54,17 @@ class ConnectionReaderImpl extends InputStream implements ConnectionReader {
|
|||||||
|
|
||||||
private boolean readFrame() throws IOException {
|
private boolean readFrame() throws IOException {
|
||||||
assert length == 0;
|
assert length == 0;
|
||||||
byte[] buf = frame.getBuffer();
|
if(HeaderEncoder.isLastFrame(frame)) {
|
||||||
if(HeaderEncoder.isLastFrame(buf)) {
|
|
||||||
length = -1;
|
length = -1;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
frame.reset();
|
|
||||||
if(!in.readFrame(frame)) throw new FormatException();
|
if(!in.readFrame(frame)) throw new FormatException();
|
||||||
offset = FRAME_HEADER_LENGTH;
|
offset = HEADER_LENGTH;
|
||||||
length = HeaderEncoder.getPayloadLength(buf);
|
length = HeaderEncoder.getPayloadLength(frame);
|
||||||
// The padding must be all zeroes
|
// 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++) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
|
|||||||
Cipher tagCipher = crypto.getTagCipher();
|
Cipher tagCipher = crypto.getTagCipher();
|
||||||
Cipher frameCipher = crypto.getFrameCipher();
|
Cipher frameCipher = crypto.getFrameCipher();
|
||||||
IvEncoder frameIvEncoder = crypto.getFrameIvEncoder();
|
IvEncoder frameIvEncoder = crypto.getFrameIvEncoder();
|
||||||
FrameWriter encryption = new OutgoingEncryptionLayerImpl(
|
FrameWriter encryption = new OutgoingEncryptionLayer(
|
||||||
out, capacity, tagCipher, frameCipher, frameIvEncoder, tagKey,
|
out, capacity, tagCipher, frameCipher, frameIvEncoder, tagKey,
|
||||||
frameKey);
|
frameKey);
|
||||||
return new ConnectionWriterImpl(encryption);
|
return new ConnectionWriterImpl(encryption);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package net.sf.briar.transport;
|
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.MAC_LENGTH;
|
||||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||||
import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
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 {
|
class ConnectionWriterImpl extends OutputStream implements ConnectionWriter {
|
||||||
|
|
||||||
private final FrameWriter out;
|
private final FrameWriter out;
|
||||||
private final Frame frame;
|
private final byte[] frame;
|
||||||
|
|
||||||
private int offset;
|
private int offset;
|
||||||
private long frameNumber;
|
private long frameNumber;
|
||||||
|
|
||||||
ConnectionWriterImpl(FrameWriter out) {
|
ConnectionWriterImpl(FrameWriter out) {
|
||||||
this.out = out;
|
this.out = out;
|
||||||
frame = new Frame();
|
frame = new byte[MAX_FRAME_LENGTH];
|
||||||
offset = FRAME_HEADER_LENGTH;
|
offset = HEADER_LENGTH;
|
||||||
frameNumber = 0L;
|
frameNumber = 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,29 +38,29 @@ class ConnectionWriterImpl extends OutputStream implements ConnectionWriter {
|
|||||||
public long getRemainingCapacity() {
|
public long getRemainingCapacity() {
|
||||||
long capacity = out.getRemainingCapacity();
|
long capacity = out.getRemainingCapacity();
|
||||||
// If there's any data buffered, subtract it and its overhead
|
// 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
|
// Subtract the overhead from the remaining capacity
|
||||||
long frames = (long) Math.ceil((double) capacity / MAX_FRAME_LENGTH);
|
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);
|
return Math.max(0L, capacity - frames * overheadPerFrame);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
if(offset > FRAME_HEADER_LENGTH || frameNumber > 0L) writeFrame(true);
|
if(offset > HEADER_LENGTH || frameNumber > 0L) writeFrame(true);
|
||||||
out.flush();
|
out.flush();
|
||||||
super.close();
|
super.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void flush() throws IOException {
|
public void flush() throws IOException {
|
||||||
if(offset > FRAME_HEADER_LENGTH) writeFrame(false);
|
if(offset > HEADER_LENGTH) writeFrame(false);
|
||||||
out.flush();
|
out.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(int b) throws IOException {
|
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);
|
if(offset + MAC_LENGTH == MAX_FRAME_LENGTH) writeFrame(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,30 +71,26 @@ class ConnectionWriterImpl extends OutputStream implements ConnectionWriter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(byte[] b, int off, int len) throws IOException {
|
public void write(byte[] b, int off, int len) throws IOException {
|
||||||
byte[] buf = frame.getBuffer();
|
|
||||||
int available = MAX_FRAME_LENGTH - offset - MAC_LENGTH;
|
int available = MAX_FRAME_LENGTH - offset - MAC_LENGTH;
|
||||||
while(available <= len) {
|
while(available <= len) {
|
||||||
System.arraycopy(b, off, buf, offset, available);
|
System.arraycopy(b, off, frame, offset, available);
|
||||||
offset += available;
|
offset += available;
|
||||||
writeFrame(false);
|
writeFrame(false);
|
||||||
off += available;
|
off += available;
|
||||||
len -= available;
|
len -= available;
|
||||||
available = MAX_FRAME_LENGTH - offset - MAC_LENGTH;
|
available = MAX_FRAME_LENGTH - offset - MAC_LENGTH;
|
||||||
}
|
}
|
||||||
System.arraycopy(b, off, buf, offset, len);
|
System.arraycopy(b, off, frame, offset, len);
|
||||||
offset += len;
|
offset += len;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeFrame(boolean lastFrame) throws IOException {
|
private void writeFrame(boolean lastFrame) throws IOException {
|
||||||
if(frameNumber > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
if(frameNumber > MAX_32_BIT_UNSIGNED) throw new IllegalStateException();
|
||||||
int payload = offset - FRAME_HEADER_LENGTH;
|
int payload = offset - HEADER_LENGTH;
|
||||||
assert payload >= 0;
|
assert payload >= 0;
|
||||||
HeaderEncoder.encodeHeader(frame.getBuffer(), frameNumber, payload, 0,
|
HeaderEncoder.encodeHeader(frame, frameNumber, payload, 0, lastFrame);
|
||||||
lastFrame);
|
|
||||||
frame.setLength(offset);
|
|
||||||
out.writeFrame(frame);
|
out.writeFrame(frame);
|
||||||
frame.reset();
|
offset = HEADER_LENGTH;
|
||||||
offset = FRAME_HEADER_LENGTH;
|
|
||||||
frameNumber++;
|
frameNumber++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,5 +8,5 @@ interface FrameReader {
|
|||||||
* Reads a frame into the given buffer. Returns false if no more frames can
|
* Reads a frame into the given buffer. Returns false if no more frames can
|
||||||
* be read from the connection.
|
* be read from the connection.
|
||||||
*/
|
*/
|
||||||
boolean readFrame(Frame f) throws IOException;
|
boolean readFrame(byte[] frame) throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import java.io.IOException;
|
|||||||
interface FrameWriter {
|
interface FrameWriter {
|
||||||
|
|
||||||
/** Writes the given frame. */
|
/** Writes the given frame. */
|
||||||
void writeFrame(Frame f) throws IOException;
|
void writeFrame(byte[] frame) throws IOException;
|
||||||
|
|
||||||
/** Flushes the stack. */
|
/** Flushes the stack. */
|
||||||
void flush() throws IOException;
|
void flush() throws IOException;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package net.sf.briar.transport;
|
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.MAC_LENGTH;
|
||||||
import net.sf.briar.util.ByteUtils;
|
import net.sf.briar.util.ByteUtils;
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ class HeaderEncoder {
|
|||||||
|
|
||||||
static void encodeHeader(byte[] header, long frameNumber, int payload,
|
static void encodeHeader(byte[] header, long frameNumber, int payload,
|
||||||
int padding, boolean lastFrame) {
|
int padding, boolean lastFrame) {
|
||||||
if(header.length < FRAME_HEADER_LENGTH)
|
if(header.length < HEADER_LENGTH)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
if(frameNumber < 0 || frameNumber > ByteUtils.MAX_32_BIT_UNSIGNED)
|
if(frameNumber < 0 || frameNumber > ByteUtils.MAX_32_BIT_UNSIGNED)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
@@ -23,37 +23,32 @@ class HeaderEncoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static boolean checkHeader(byte[] header, int length) {
|
static boolean checkHeader(byte[] header, int length) {
|
||||||
if(header.length < FRAME_HEADER_LENGTH)
|
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
|
||||||
throw new IllegalArgumentException();
|
|
||||||
int payload = getPayloadLength(header);
|
int payload = getPayloadLength(header);
|
||||||
int padding = getPaddingLength(header);
|
int padding = getPaddingLength(header);
|
||||||
if(FRAME_HEADER_LENGTH + payload + padding + MAC_LENGTH != length)
|
if(HEADER_LENGTH + payload + padding + MAC_LENGTH != length)
|
||||||
return false;
|
return false;
|
||||||
if(header[8] != 0 && header[8] != 1) return false;
|
if(header[8] != 0 && header[8] != 1) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static long getFrameNumber(byte[] header) {
|
static long getFrameNumber(byte[] header) {
|
||||||
if(header.length < FRAME_HEADER_LENGTH)
|
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
|
||||||
throw new IllegalArgumentException();
|
|
||||||
return ByteUtils.readUint32(header, 0);
|
return ByteUtils.readUint32(header, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int getPayloadLength(byte[] header) {
|
static int getPayloadLength(byte[] header) {
|
||||||
if(header.length < FRAME_HEADER_LENGTH)
|
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
|
||||||
throw new IllegalArgumentException();
|
|
||||||
return ByteUtils.readUint16(header, 4);
|
return ByteUtils.readUint16(header, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int getPaddingLength(byte[] header) {
|
static int getPaddingLength(byte[] header) {
|
||||||
if(header.length < FRAME_HEADER_LENGTH)
|
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
|
||||||
throw new IllegalArgumentException();
|
|
||||||
return ByteUtils.readUint16(header, 6);
|
return ByteUtils.readUint16(header, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean isLastFrame(byte[] header) {
|
static boolean isLastFrame(byte[] header) {
|
||||||
if(header.length < FRAME_HEADER_LENGTH)
|
if(header.length < HEADER_LENGTH) throw new IllegalArgumentException();
|
||||||
throw new IllegalArgumentException();
|
|
||||||
return header[8] == 1;
|
return header[8] == 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package net.sf.briar.transport;
|
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.MAC_LENGTH;
|
||||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||||
import static net.sf.briar.api.transport.TransportConstants.TAG_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.ErasableKey;
|
||||||
import net.sf.briar.api.crypto.IvEncoder;
|
import net.sf.briar.api.crypto.IvEncoder;
|
||||||
|
|
||||||
class IncomingEncryptionLayerImpl implements FrameReader {
|
class IncomingEncryptionLayer implements FrameReader {
|
||||||
|
|
||||||
private final InputStream in;
|
private final InputStream in;
|
||||||
private final Cipher tagCipher, frameCipher, framePeekingCipher;
|
private final Cipher tagCipher, frameCipher, framePeekingCipher;
|
||||||
@@ -29,7 +29,7 @@ class IncomingEncryptionLayerImpl implements FrameReader {
|
|||||||
private boolean readTag;
|
private boolean readTag;
|
||||||
private long frameNumber;
|
private long frameNumber;
|
||||||
|
|
||||||
IncomingEncryptionLayerImpl(InputStream in, Cipher tagCipher,
|
IncomingEncryptionLayer(InputStream in, Cipher tagCipher,
|
||||||
Cipher frameCipher, Cipher framePeekingCipher,
|
Cipher frameCipher, Cipher framePeekingCipher,
|
||||||
IvEncoder frameIvEncoder, IvEncoder framePeekingIvEncoder,
|
IvEncoder frameIvEncoder, IvEncoder framePeekingIvEncoder,
|
||||||
ErasableKey tagKey, ErasableKey frameKey, boolean readTag) {
|
ErasableKey tagKey, ErasableKey frameKey, boolean readTag) {
|
||||||
@@ -43,15 +43,14 @@ class IncomingEncryptionLayerImpl implements FrameReader {
|
|||||||
this.frameKey = frameKey;
|
this.frameKey = frameKey;
|
||||||
this.readTag = readTag;
|
this.readTag = readTag;
|
||||||
blockSize = frameCipher.getBlockSize();
|
blockSize = frameCipher.getBlockSize();
|
||||||
if(blockSize < FRAME_HEADER_LENGTH)
|
if(blockSize < HEADER_LENGTH) throw new IllegalArgumentException();
|
||||||
throw new IllegalArgumentException();
|
|
||||||
frameIv = frameIvEncoder.encodeIv(0L);
|
frameIv = frameIvEncoder.encodeIv(0L);
|
||||||
framePeekingIv = framePeekingIvEncoder.encodeIv(0L);
|
framePeekingIv = framePeekingIvEncoder.encodeIv(0L);
|
||||||
ciphertext = new byte[MAX_FRAME_LENGTH];
|
ciphertext = new byte[MAX_FRAME_LENGTH];
|
||||||
frameNumber = 0L;
|
frameNumber = 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean readFrame(Frame f) throws IOException {
|
public boolean readFrame(byte[] frame) throws IOException {
|
||||||
try {
|
try {
|
||||||
// Read the tag if it hasn't already been read
|
// Read the tag if it hasn't already been read
|
||||||
if(readTag) {
|
if(readTag) {
|
||||||
@@ -79,19 +78,18 @@ class IncomingEncryptionLayerImpl implements FrameReader {
|
|||||||
// Decrypt the first block of the frame to peek at the header
|
// Decrypt the first block of the frame to peek at the header
|
||||||
framePeekingIvEncoder.updateIv(framePeekingIv, frameNumber);
|
framePeekingIvEncoder.updateIv(framePeekingIv, frameNumber);
|
||||||
IvParameterSpec ivSpec = new IvParameterSpec(framePeekingIv);
|
IvParameterSpec ivSpec = new IvParameterSpec(framePeekingIv);
|
||||||
byte[] plaintext = f.getBuffer();
|
|
||||||
try {
|
try {
|
||||||
framePeekingCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
|
framePeekingCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
|
||||||
int decrypted = framePeekingCipher.update(ciphertext, 0,
|
int decrypted = framePeekingCipher.update(ciphertext, 0,
|
||||||
blockSize, plaintext);
|
blockSize, frame);
|
||||||
if(decrypted != blockSize) throw new RuntimeException();
|
if(decrypted != blockSize) throw new RuntimeException();
|
||||||
} catch(GeneralSecurityException badCipher) {
|
} catch(GeneralSecurityException badCipher) {
|
||||||
throw new RuntimeException(badCipher);
|
throw new RuntimeException(badCipher);
|
||||||
}
|
}
|
||||||
// Parse the frame header
|
// Parse the frame header
|
||||||
int payload = HeaderEncoder.getPayloadLength(plaintext);
|
int payload = HeaderEncoder.getPayloadLength(frame);
|
||||||
int padding = HeaderEncoder.getPaddingLength(plaintext);
|
int padding = HeaderEncoder.getPaddingLength(frame);
|
||||||
int length = FRAME_HEADER_LENGTH + payload + padding + MAC_LENGTH;
|
int length = HEADER_LENGTH + payload + padding + MAC_LENGTH;
|
||||||
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
||||||
// Read the remainder of the frame
|
// Read the remainder of the frame
|
||||||
while(offset < length) {
|
while(offset < length) {
|
||||||
@@ -105,13 +103,12 @@ class IncomingEncryptionLayerImpl implements FrameReader {
|
|||||||
try {
|
try {
|
||||||
frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
|
frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
|
||||||
int decrypted = frameCipher.doFinal(ciphertext, 0, length,
|
int decrypted = frameCipher.doFinal(ciphertext, 0, length,
|
||||||
plaintext);
|
frame);
|
||||||
if(decrypted != length - MAC_LENGTH)
|
if(decrypted != length - MAC_LENGTH)
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
} catch(GeneralSecurityException badCipher) {
|
} catch(GeneralSecurityException badCipher) {
|
||||||
throw new RuntimeException(badCipher);
|
throw new RuntimeException(badCipher);
|
||||||
}
|
}
|
||||||
f.setLength(length - MAC_LENGTH);
|
|
||||||
frameNumber++;
|
frameNumber++;
|
||||||
return true;
|
return true;
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package net.sf.briar.transport;
|
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.MAC_LENGTH;
|
||||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||||
import static net.sf.briar.api.transport.TransportConstants.TAG_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.ErasableKey;
|
||||||
import net.sf.briar.api.crypto.IvEncoder;
|
import net.sf.briar.api.crypto.IvEncoder;
|
||||||
|
|
||||||
class OutgoingEncryptionLayerImpl implements FrameWriter {
|
class OutgoingEncryptionLayer implements FrameWriter {
|
||||||
|
|
||||||
private final OutputStream out;
|
private final OutputStream out;
|
||||||
private final Cipher tagCipher, frameCipher;
|
private final Cipher tagCipher, frameCipher;
|
||||||
@@ -24,9 +25,9 @@ class OutgoingEncryptionLayerImpl implements FrameWriter {
|
|||||||
|
|
||||||
private long capacity, frameNumber;
|
private long capacity, frameNumber;
|
||||||
|
|
||||||
OutgoingEncryptionLayerImpl(OutputStream out, long capacity,
|
OutgoingEncryptionLayer(OutputStream out, long capacity, Cipher tagCipher,
|
||||||
Cipher tagCipher, Cipher frameCipher, IvEncoder frameIvEncoder,
|
Cipher frameCipher, IvEncoder frameIvEncoder, ErasableKey tagKey,
|
||||||
ErasableKey tagKey, ErasableKey frameKey) {
|
ErasableKey frameKey) {
|
||||||
this.out = out;
|
this.out = out;
|
||||||
this.capacity = capacity;
|
this.capacity = capacity;
|
||||||
this.tagCipher = tagCipher;
|
this.tagCipher = tagCipher;
|
||||||
@@ -39,9 +40,10 @@ class OutgoingEncryptionLayerImpl implements FrameWriter {
|
|||||||
frameNumber = 0L;
|
frameNumber = 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeFrame(Frame f) throws IOException {
|
public void writeFrame(byte[] frame) throws IOException {
|
||||||
byte[] plaintext = f.getBuffer();
|
int payload = HeaderEncoder.getPayloadLength(frame);
|
||||||
int offset = 0, length = f.getLength();
|
int padding = HeaderEncoder.getPaddingLength(frame);
|
||||||
|
int offset = 0, length = HEADER_LENGTH + payload + padding;
|
||||||
if(frameNumber == 0) {
|
if(frameNumber == 0) {
|
||||||
TagEncoder.encodeTag(ciphertext, tagCipher, tagKey);
|
TagEncoder.encodeTag(ciphertext, tagCipher, tagKey);
|
||||||
offset = TAG_LENGTH;
|
offset = TAG_LENGTH;
|
||||||
@@ -50,8 +52,8 @@ class OutgoingEncryptionLayerImpl implements FrameWriter {
|
|||||||
IvParameterSpec ivSpec = new IvParameterSpec(frameIv);
|
IvParameterSpec ivSpec = new IvParameterSpec(frameIv);
|
||||||
try {
|
try {
|
||||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||||
int encrypted = frameCipher.doFinal(plaintext, 0, length,
|
int encrypted = frameCipher.doFinal(frame, 0, length, ciphertext,
|
||||||
ciphertext, offset);
|
offset);
|
||||||
if(encrypted != length + MAC_LENGTH) throw new RuntimeException();
|
if(encrypted != length + MAC_LENGTH) throw new RuntimeException();
|
||||||
} catch(GeneralSecurityException badCipher) {
|
} catch(GeneralSecurityException badCipher) {
|
||||||
throw new RuntimeException(badCipher);
|
throw new RuntimeException(badCipher);
|
||||||
@@ -55,8 +55,8 @@
|
|||||||
<test name='net.sf.briar.transport.ConnectionWriterImplTest'/>
|
<test name='net.sf.briar.transport.ConnectionWriterImplTest'/>
|
||||||
<test name='net.sf.briar.transport.ConnectionWriterTest'/>
|
<test name='net.sf.briar.transport.ConnectionWriterTest'/>
|
||||||
<test name='net.sf.briar.transport.FrameReadWriteTest'/>
|
<test name='net.sf.briar.transport.FrameReadWriteTest'/>
|
||||||
<test name='net.sf.briar.transport.IncomingEncryptionLayerImplTest'/>
|
<test name='net.sf.briar.transport.IncomingEncryptionLayerTest'/>
|
||||||
<test name='net.sf.briar.transport.OutgoingEncryptionLayerImplTest'/>
|
<test name='net.sf.briar.transport.OutgoingEncryptionLayerTest'/>
|
||||||
<test name='net.sf.briar.util.ByteUtilsTest'/>
|
<test name='net.sf.briar.util.ByteUtilsTest'/>
|
||||||
<test name='net.sf.briar.util.FileUtilsTest'/>
|
<test name='net.sf.briar.util.FileUtilsTest'/>
|
||||||
<test name='net.sf.briar.util.StringUtilsTest'/>
|
<test name='net.sf.briar.util.StringUtilsTest'/>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package net.sf.briar.transport;
|
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.MAC_LENGTH;
|
||||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
@@ -8,6 +8,7 @@ import static org.junit.Assert.assertArrayEquals;
|
|||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import net.sf.briar.BriarTestCase;
|
||||||
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.transport.ConnectionReader;
|
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.apache.commons.io.output.ByteArrayOutputStream;
|
||||||
import org.junit.Test;
|
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 {
|
public ConnectionReaderImplTest() throws Exception {
|
||||||
super();
|
super();
|
||||||
@@ -23,7 +27,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLengthZero() throws Exception {
|
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);
|
HeaderEncoder.encodeHeader(frame, 0, 0, 0, true);
|
||||||
// Read the frame
|
// Read the frame
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||||
@@ -34,7 +38,7 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLengthOne() throws Exception {
|
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);
|
HeaderEncoder.encodeHeader(frame, 0, 1, 0, true);
|
||||||
// Read the frame
|
// Read the frame
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||||
@@ -100,12 +104,12 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testNonZeroPadding() throws Exception {
|
public void testNonZeroPadding() throws Exception {
|
||||||
int payloadLength = 10, paddingLength = 10;
|
int payloadLength = 10, paddingLength = 10;
|
||||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
|
byte[] frame = new byte[HEADER_LENGTH + payloadLength + paddingLength
|
||||||
+ paddingLength + MAC_LENGTH];
|
+ MAC_LENGTH];
|
||||||
HeaderEncoder.encodeHeader(frame, 0, payloadLength, paddingLength,
|
HeaderEncoder.encodeHeader(frame, 0, payloadLength, paddingLength,
|
||||||
false);
|
false);
|
||||||
// Set a byte of the padding to a non-zero value
|
// 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
|
// Read the frame
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
ByteArrayInputStream in = new ByteArrayInputStream(frame);
|
||||||
ConnectionReader r = createConnectionReader(in);
|
ConnectionReader r = createConnectionReader(in);
|
||||||
@@ -120,13 +124,11 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
public void testMultipleFrames() throws Exception {
|
public void testMultipleFrames() throws Exception {
|
||||||
// First frame: 123-byte payload
|
// First frame: 123-byte payload
|
||||||
int payloadLength = 123;
|
int payloadLength = 123;
|
||||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
|
byte[] frame = new byte[HEADER_LENGTH + payloadLength + MAC_LENGTH];
|
||||||
+ MAC_LENGTH];
|
|
||||||
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0, false);
|
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0, false);
|
||||||
// Second frame: 1234-byte payload
|
// Second frame: 1234-byte payload
|
||||||
int payloadLength1 = 1234;
|
int payloadLength1 = 1234;
|
||||||
byte[] frame1 = new byte[FRAME_HEADER_LENGTH + payloadLength1
|
byte[] frame1 = new byte[HEADER_LENGTH + payloadLength1 + MAC_LENGTH];
|
||||||
+ MAC_LENGTH];
|
|
||||||
HeaderEncoder.encodeHeader(frame1, 1, payloadLength1, 0, true);
|
HeaderEncoder.encodeHeader(frame1, 1, payloadLength1, 0, true);
|
||||||
// Concatenate the frames
|
// Concatenate the frames
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
@@ -148,13 +150,11 @@ public class ConnectionReaderImplTest extends TransportTest {
|
|||||||
public void testLastFrameNotMarkedAsSuch() throws Exception {
|
public void testLastFrameNotMarkedAsSuch() throws Exception {
|
||||||
// First frame: 123-byte payload
|
// First frame: 123-byte payload
|
||||||
int payloadLength = 123;
|
int payloadLength = 123;
|
||||||
byte[] frame = new byte[FRAME_HEADER_LENGTH + payloadLength
|
byte[] frame = new byte[HEADER_LENGTH + payloadLength + MAC_LENGTH];
|
||||||
+ MAC_LENGTH];
|
|
||||||
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0, false);
|
HeaderEncoder.encodeHeader(frame, 0, payloadLength, 0, false);
|
||||||
// Second frame: 1234-byte payload
|
// Second frame: 1234-byte payload
|
||||||
int payloadLength1 = 1234;
|
int payloadLength1 = 1234;
|
||||||
byte[] frame1 = new byte[FRAME_HEADER_LENGTH + payloadLength1
|
byte[] frame1 = new byte[HEADER_LENGTH + payloadLength1 + MAC_LENGTH];
|
||||||
+ MAC_LENGTH];
|
|
||||||
HeaderEncoder.encodeHeader(frame1, 1, payloadLength1, 0, false);
|
HeaderEncoder.encodeHeader(frame1, 1, payloadLength1, 0, false);
|
||||||
// Concatenate the frames
|
// Concatenate the frames
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package net.sf.briar.transport;
|
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.MAC_LENGTH;
|
||||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
@@ -8,11 +8,15 @@ import static org.junit.Assert.assertArrayEquals;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
import net.sf.briar.BriarTestCase;
|
||||||
import net.sf.briar.api.transport.ConnectionWriter;
|
import net.sf.briar.api.transport.ConnectionWriter;
|
||||||
|
|
||||||
import org.junit.Test;
|
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 {
|
public ConnectionWriterImplTest() throws Exception {
|
||||||
super();
|
super();
|
||||||
@@ -31,7 +35,7 @@ public class ConnectionWriterImplTest extends TransportTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testSingleByteFrame() throws Exception {
|
public void testSingleByteFrame() throws Exception {
|
||||||
// Create a single-byte frame
|
// 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);
|
HeaderEncoder.encodeHeader(frame, 0, 1, 0, false);
|
||||||
// Check that the ConnectionWriter gets the same results
|
// Check that the ConnectionWriter gets the same results
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
@@ -70,10 +74,10 @@ public class ConnectionWriterImplTest extends TransportTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testMultipleFrames() throws Exception {
|
public void testMultipleFrames() throws Exception {
|
||||||
// First frame: 123-byte payload
|
// 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);
|
HeaderEncoder.encodeHeader(frame, 0, 123, 0, false);
|
||||||
// Second frame: 1234-byte payload
|
// 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);
|
HeaderEncoder.encodeHeader(frame1, 1, 1234, 0, false);
|
||||||
// Concatenate the frames
|
// Concatenate the frames
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ public class FrameReadWriteTest extends BriarTestCase {
|
|||||||
ErasableKey frameCopy = frameKey.copy();
|
ErasableKey frameCopy = frameKey.copy();
|
||||||
// Write the frames
|
// Write the frames
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
FrameWriter encryptionOut = new OutgoingEncryptionLayerImpl(out,
|
FrameWriter encryptionOut = new OutgoingEncryptionLayer(out,
|
||||||
Long.MAX_VALUE, tagCipher, frameCipher, frameIvEncoder, tagCopy,
|
Long.MAX_VALUE, tagCipher, frameCipher, frameIvEncoder, tagCopy,
|
||||||
frameCopy);
|
frameCopy);
|
||||||
ConnectionWriter writer = new ConnectionWriterImpl(encryptionOut);
|
ConnectionWriter writer = new ConnectionWriterImpl(encryptionOut);
|
||||||
@@ -90,7 +90,7 @@ public class FrameReadWriteTest extends BriarTestCase {
|
|||||||
assertArrayEquals(tag, recoveredTag);
|
assertArrayEquals(tag, recoveredTag);
|
||||||
assertTrue(TagEncoder.decodeTag(recoveredTag, tagCipher, tagKey));
|
assertTrue(TagEncoder.decodeTag(recoveredTag, tagCipher, tagKey));
|
||||||
// Read the frames back
|
// Read the frames back
|
||||||
FrameReader encryptionIn = new IncomingEncryptionLayerImpl(in,
|
FrameReader encryptionIn = new IncomingEncryptionLayer(in,
|
||||||
tagCipher, frameCipher, framePeekingCipher, frameIvEncoder,
|
tagCipher, frameCipher, framePeekingCipher, frameIvEncoder,
|
||||||
framePeekingIvEncoder, tagKey, frameKey, false);
|
framePeekingIvEncoder, tagKey, frameKey, false);
|
||||||
ConnectionReader reader = new ConnectionReaderImpl(encryptionIn);
|
ConnectionReader reader = new ConnectionReaderImpl(encryptionIn);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package net.sf.briar.transport;
|
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 static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
@@ -20,13 +21,13 @@ 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 IncomingEncryptionLayerImplTest extends BriarTestCase {
|
public class IncomingEncryptionLayerTest extends BriarTestCase {
|
||||||
|
|
||||||
private final Cipher tagCipher, frameCipher, framePeekingCipher;
|
private final Cipher tagCipher, frameCipher, framePeekingCipher;
|
||||||
private final IvEncoder frameIvEncoder, framePeekingIvEncoder;
|
private final IvEncoder frameIvEncoder, framePeekingIvEncoder;
|
||||||
private final ErasableKey tagKey, frameKey;
|
private final ErasableKey tagKey, frameKey;
|
||||||
|
|
||||||
public IncomingEncryptionLayerImplTest() {
|
public IncomingEncryptionLayerTest() {
|
||||||
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);
|
||||||
@@ -45,14 +46,14 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
|||||||
byte[] tag = new byte[TAG_LENGTH];
|
byte[] tag = new byte[TAG_LENGTH];
|
||||||
TagEncoder.encodeTag(tag, tagCipher, tagKey);
|
TagEncoder.encodeTag(tag, tagCipher, tagKey);
|
||||||
// Calculate the ciphertext for the first frame
|
// 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);
|
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0, false);
|
||||||
byte[] iv = frameIvEncoder.encodeIv(0L);
|
byte[] iv = frameIvEncoder.encodeIv(0L);
|
||||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||||
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
||||||
// Calculate the ciphertext for the second frame
|
// 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);
|
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0, false);
|
||||||
frameIvEncoder.updateIv(iv, 1L);
|
frameIvEncoder.updateIv(iv, 1L);
|
||||||
ivSpec = new IvParameterSpec(iv);
|
ivSpec = new IvParameterSpec(iv);
|
||||||
@@ -66,39 +67,45 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
|||||||
out.write(ciphertext1);
|
out.write(ciphertext1);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
// Use the encryption layer to decrypt the ciphertext
|
// Use the encryption layer to decrypt the ciphertext
|
||||||
FrameReader decrypter = new IncomingEncryptionLayerImpl(in, tagCipher,
|
FrameReader decrypter = new IncomingEncryptionLayer(in, tagCipher,
|
||||||
frameCipher, framePeekingCipher, frameIvEncoder,
|
frameCipher, framePeekingCipher, frameIvEncoder,
|
||||||
framePeekingIvEncoder, tagKey, frameKey, true);
|
framePeekingIvEncoder, tagKey, frameKey, true);
|
||||||
// First frame
|
// First frame
|
||||||
Frame f = new Frame();
|
byte[] frame = new byte[MAX_FRAME_LENGTH];
|
||||||
assertTrue(decrypter.readFrame(f));
|
assertTrue(decrypter.readFrame(frame));
|
||||||
assertEquals(plaintext.length, f.getLength());
|
assertEquals(0L, HeaderEncoder.getFrameNumber(frame));
|
||||||
byte[] decrypted = f.getBuffer();
|
int payload = HeaderEncoder.getPayloadLength(frame);
|
||||||
assertEquals(0L, HeaderEncoder.getFrameNumber(decrypted));
|
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++) {
|
for(int i = 0; i < plaintext.length; i++) {
|
||||||
assertEquals(plaintext[i], decrypted[i]);
|
assertEquals(plaintext[i], frame[i]);
|
||||||
}
|
}
|
||||||
// Second frame
|
// Second frame
|
||||||
assertTrue(decrypter.readFrame(f));
|
assertTrue(decrypter.readFrame(frame));
|
||||||
assertEquals(plaintext1.length, f.getLength());
|
assertEquals(1L, HeaderEncoder.getFrameNumber(frame));
|
||||||
decrypted = f.getBuffer();
|
payload = HeaderEncoder.getPayloadLength(frame);
|
||||||
assertEquals(1L, HeaderEncoder.getFrameNumber(decrypted));
|
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++) {
|
for(int i = 0; i < plaintext1.length; i++) {
|
||||||
assertEquals(plaintext1[i], decrypted[i]);
|
assertEquals(plaintext1[i], frame[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecryptionWithoutTag() throws Exception {
|
public void testDecryptionWithoutTag() throws Exception {
|
||||||
// Calculate the ciphertext for the first frame
|
// 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);
|
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0, false);
|
||||||
byte[] iv = frameIvEncoder.encodeIv(0L);
|
byte[] iv = frameIvEncoder.encodeIv(0L);
|
||||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||||
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
||||||
// Calculate the ciphertext for the second frame
|
// 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);
|
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0, false);
|
||||||
frameIvEncoder.updateIv(iv, 1L);
|
frameIvEncoder.updateIv(iv, 1L);
|
||||||
ivSpec = new IvParameterSpec(iv);
|
ivSpec = new IvParameterSpec(iv);
|
||||||
@@ -111,25 +118,31 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
|||||||
out.write(ciphertext1);
|
out.write(ciphertext1);
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
// Use the encryption layer to decrypt the ciphertext
|
// Use the encryption layer to decrypt the ciphertext
|
||||||
FrameReader decrypter = new IncomingEncryptionLayerImpl(in, tagCipher,
|
FrameReader decrypter = new IncomingEncryptionLayer(in, tagCipher,
|
||||||
frameCipher, framePeekingCipher, frameIvEncoder,
|
frameCipher, framePeekingCipher, frameIvEncoder,
|
||||||
framePeekingIvEncoder, tagKey, frameKey, false);
|
framePeekingIvEncoder, tagKey, frameKey, false);
|
||||||
// First frame
|
// First frame
|
||||||
Frame f = new Frame();
|
byte[] frame = new byte[MAX_FRAME_LENGTH];
|
||||||
assertTrue(decrypter.readFrame(f));
|
assertTrue(decrypter.readFrame(frame));
|
||||||
assertEquals(plaintext.length, f.getLength());
|
assertEquals(0L, HeaderEncoder.getFrameNumber(frame));
|
||||||
byte[] decrypted = f.getBuffer();
|
int payload = HeaderEncoder.getPayloadLength(frame);
|
||||||
assertEquals(0L, HeaderEncoder.getFrameNumber(decrypted));
|
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++) {
|
for(int i = 0; i < plaintext.length; i++) {
|
||||||
assertEquals(plaintext[i], decrypted[i]);
|
assertEquals(plaintext[i], frame[i]);
|
||||||
}
|
}
|
||||||
// Second frame
|
// Second frame
|
||||||
assertTrue(decrypter.readFrame(f));
|
assertTrue(decrypter.readFrame(frame));
|
||||||
assertEquals(plaintext1.length, f.getLength());
|
assertEquals(1L, HeaderEncoder.getFrameNumber(frame));
|
||||||
assertEquals(1L, HeaderEncoder.getFrameNumber(decrypted));
|
payload = HeaderEncoder.getPayloadLength(frame);
|
||||||
decrypted = f.getBuffer();
|
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++) {
|
for(int i = 0; i < plaintext1.length; i++) {
|
||||||
assertEquals(plaintext1[i], decrypted[i]);
|
assertEquals(plaintext1[i], frame[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package net.sf.briar.transport;
|
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.MAC_LENGTH;
|
||||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||||
|
|
||||||
@@ -19,12 +19,11 @@ class NullIncomingEncryptionLayer implements FrameReader {
|
|||||||
this.in = in;
|
this.in = in;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean readFrame(Frame f) throws IOException {
|
public boolean readFrame(byte[] frame) throws IOException {
|
||||||
byte[] buf = f.getBuffer();
|
|
||||||
// Read the frame header
|
// Read the frame header
|
||||||
int offset = 0, length = FRAME_HEADER_LENGTH;
|
int offset = 0, length = HEADER_LENGTH;
|
||||||
while(offset < length) {
|
while(offset < length) {
|
||||||
int read = in.read(buf, offset, length - offset);
|
int read = in.read(frame, offset, length - offset);
|
||||||
if(read == -1) {
|
if(read == -1) {
|
||||||
if(offset == 0) return false;
|
if(offset == 0) return false;
|
||||||
throw new EOFException();
|
throw new EOFException();
|
||||||
@@ -32,17 +31,16 @@ class NullIncomingEncryptionLayer implements FrameReader {
|
|||||||
offset += read;
|
offset += read;
|
||||||
}
|
}
|
||||||
// Parse the frame header
|
// Parse the frame header
|
||||||
int payload = HeaderEncoder.getPayloadLength(buf);
|
int payload = HeaderEncoder.getPayloadLength(frame);
|
||||||
int padding = HeaderEncoder.getPaddingLength(buf);
|
int padding = HeaderEncoder.getPaddingLength(frame);
|
||||||
length = FRAME_HEADER_LENGTH + payload + padding + MAC_LENGTH;
|
length = HEADER_LENGTH + payload + padding + MAC_LENGTH;
|
||||||
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
if(length > MAX_FRAME_LENGTH) throw new FormatException();
|
||||||
// Read the remainder of the frame
|
// Read the remainder of the frame
|
||||||
while(offset < length) {
|
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();
|
if(read == -1) throw new EOFException();
|
||||||
offset += read;
|
offset += read;
|
||||||
}
|
}
|
||||||
f.setLength(length - MAC_LENGTH);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package net.sf.briar.transport;
|
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.MAC_LENGTH;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -22,9 +23,12 @@ class NullOutgoingEncryptionLayer implements FrameWriter {
|
|||||||
this.capacity = capacity;
|
this.capacity = capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeFrame(Frame f) throws IOException {
|
public void writeFrame(byte[] frame) throws IOException {
|
||||||
out.write(f.getBuffer(), 0, f.getLength() + MAC_LENGTH);
|
int payload = HeaderEncoder.getPayloadLength(frame);
|
||||||
capacity -= f.getLength();
|
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 {
|
public void flush() throws IOException {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package net.sf.briar.transport;
|
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 net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
|
||||||
@@ -19,13 +20,13 @@ 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 OutgoingEncryptionLayerImplTest extends BriarTestCase {
|
public class OutgoingEncryptionLayerTest extends BriarTestCase {
|
||||||
|
|
||||||
private final Cipher tagCipher, frameCipher;
|
private final Cipher tagCipher, frameCipher;
|
||||||
private final IvEncoder frameIvEncoder;
|
private final IvEncoder frameIvEncoder;
|
||||||
private final ErasableKey tagKey, frameKey;
|
private final ErasableKey tagKey, frameKey;
|
||||||
|
|
||||||
public OutgoingEncryptionLayerImplTest() {
|
public OutgoingEncryptionLayerTest() {
|
||||||
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);
|
||||||
@@ -43,12 +44,14 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
|
|||||||
TagEncoder.encodeTag(tag, tagCipher, tagKey);
|
TagEncoder.encodeTag(tag, tagCipher, tagKey);
|
||||||
// Calculate the expected ciphertext for the first frame
|
// Calculate the expected ciphertext for the first frame
|
||||||
byte[] iv = frameIvEncoder.encodeIv(0L);
|
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);
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||||
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
||||||
// Calculate the expected ciphertext for the second frame
|
// 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);
|
frameIvEncoder.updateIv(iv, 1L);
|
||||||
ivSpec = new IvParameterSpec(iv);
|
ivSpec = new IvParameterSpec(iv);
|
||||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||||
@@ -61,16 +64,10 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
|
|||||||
byte[] expected = out.toByteArray();
|
byte[] expected = out.toByteArray();
|
||||||
// Use the encryption layer to encrypt the plaintext
|
// Use the encryption layer to encrypt the plaintext
|
||||||
out.reset();
|
out.reset();
|
||||||
FrameWriter encrypter = new OutgoingEncryptionLayerImpl(out,
|
FrameWriter encrypter = new OutgoingEncryptionLayer(out, Long.MAX_VALUE,
|
||||||
Long.MAX_VALUE, tagCipher, frameCipher, frameIvEncoder, tagKey,
|
tagCipher, frameCipher, frameIvEncoder, tagKey, frameKey);
|
||||||
frameKey);
|
encrypter.writeFrame(plaintext);
|
||||||
Frame f = new Frame();
|
encrypter.writeFrame(plaintext1);
|
||||||
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);
|
|
||||||
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
|
||||||
assertArrayEquals(expected, actual);
|
assertArrayEquals(expected, actual);
|
||||||
@@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user