mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Updated TestStreamEncrypter/Decrypter.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package org.briarproject.transport;
|
||||
|
||||
import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.crypto.StreamDecrypter;
|
||||
import org.briarproject.util.ByteUtils;
|
||||
|
||||
@@ -11,35 +10,52 @@ import java.io.InputStream;
|
||||
import static org.briarproject.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.STREAM_HEADER_LENGTH;
|
||||
|
||||
class TestStreamDecrypter implements StreamDecrypter {
|
||||
|
||||
private final InputStream in;
|
||||
private final byte[] frame;
|
||||
|
||||
private boolean readStreamHeader = true, finalFrame = false;
|
||||
|
||||
TestStreamDecrypter(InputStream in) {
|
||||
this.in = in;
|
||||
frame = new byte[MAX_FRAME_LENGTH];
|
||||
}
|
||||
|
||||
public int readFrame(byte[] payload) throws IOException {
|
||||
if (finalFrame) return -1;
|
||||
if (readStreamHeader) readStreamHeader();
|
||||
int offset = 0;
|
||||
while (offset < FRAME_HEADER_LENGTH) {
|
||||
int read = in.read(frame, offset, FRAME_HEADER_LENGTH - offset);
|
||||
if (read == -1) throw new EOFException();
|
||||
offset += read;
|
||||
}
|
||||
boolean finalFrame = (frame[0] & 0x80) == 0x80;
|
||||
finalFrame = (frame[0] & 0x80) == 0x80;
|
||||
int payloadLength = ByteUtils.readUint16(frame, 0) & 0x7FFF;
|
||||
while (offset < frame.length) {
|
||||
int read = in.read(frame, offset, frame.length - offset);
|
||||
if (read == -1) break;
|
||||
int paddingLength = ByteUtils.readUint16(frame, 2);
|
||||
int frameLength = FRAME_HEADER_LENGTH + payloadLength + paddingLength
|
||||
+ MAC_LENGTH;
|
||||
while (offset < frameLength) {
|
||||
int read = in.read(frame, offset, frameLength - offset);
|
||||
if (read == -1) throw new EOFException();
|
||||
offset += read;
|
||||
}
|
||||
if (!finalFrame && offset < frame.length) throw new EOFException();
|
||||
if (offset < FRAME_HEADER_LENGTH + payloadLength + MAC_LENGTH)
|
||||
throw new FormatException();
|
||||
System.arraycopy(frame, FRAME_HEADER_LENGTH, payload, 0, payloadLength);
|
||||
return payloadLength;
|
||||
}
|
||||
|
||||
private void readStreamHeader() throws IOException {
|
||||
byte[] streamHeader = new byte[STREAM_HEADER_LENGTH];
|
||||
int offset = 0;
|
||||
while (offset < STREAM_HEADER_LENGTH) {
|
||||
int read = in.read(streamHeader, offset,
|
||||
STREAM_HEADER_LENGTH - offset);
|
||||
if (read == -1) throw new EOFException();
|
||||
offset += read;
|
||||
}
|
||||
readStreamHeader = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,38 +8,41 @@ import java.io.OutputStream;
|
||||
|
||||
import static org.briarproject.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.STREAM_HEADER_LENGTH;
|
||||
|
||||
class TestStreamEncrypter implements StreamEncrypter {
|
||||
|
||||
private final OutputStream out;
|
||||
private final byte[] tag, frame;
|
||||
private final byte[] tag;
|
||||
|
||||
private boolean writeTag = true;
|
||||
private boolean writeTagAndHeader = true;
|
||||
|
||||
TestStreamEncrypter(OutputStream out, byte[] tag) {
|
||||
this.out = out;
|
||||
this.tag = tag;
|
||||
frame = new byte[MAX_FRAME_LENGTH];
|
||||
}
|
||||
|
||||
public void writeFrame(byte[] payload, int payloadLength,
|
||||
int paddingLength, boolean finalFrame) throws IOException {
|
||||
if (writeTag) {
|
||||
out.write(tag);
|
||||
writeTag = false;
|
||||
}
|
||||
ByteUtils.writeUint16(payloadLength, frame, 0);
|
||||
if (finalFrame) frame[0] |= 0x80;
|
||||
System.arraycopy(payload, 0, frame, FRAME_HEADER_LENGTH, payloadLength);
|
||||
for (int i = FRAME_HEADER_LENGTH + payloadLength; i < frame.length; i++)
|
||||
frame[i] = 0;
|
||||
if (finalFrame)
|
||||
out.write(frame, 0, FRAME_HEADER_LENGTH + payloadLength + MAC_LENGTH);
|
||||
else out.write(frame, 0, frame.length);
|
||||
if (writeTagAndHeader) writeTagAndHeader();
|
||||
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
|
||||
ByteUtils.writeUint16(payloadLength, frameHeader, 0);
|
||||
ByteUtils.writeUint16(paddingLength, frameHeader, 2);
|
||||
if (finalFrame) frameHeader[0] |= 0x80;
|
||||
out.write(frameHeader);
|
||||
out.write(payload, 0, payloadLength);
|
||||
out.write(new byte[paddingLength]);
|
||||
out.write(new byte[MAC_LENGTH]);
|
||||
}
|
||||
|
||||
public void flush() throws IOException {
|
||||
if (writeTagAndHeader) writeTagAndHeader();
|
||||
out.flush();
|
||||
}
|
||||
|
||||
private void writeTagAndHeader() throws IOException {
|
||||
out.write(tag);
|
||||
out.write(new byte[STREAM_HEADER_LENGTH]);
|
||||
writeTagAndHeader = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,30 +12,19 @@ import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.STREAM_HEADER_LENGTH;
|
||||
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TransportIntegrationTest extends BriarTestCase {
|
||||
|
||||
private final Random random;
|
||||
|
||||
public TransportIntegrationTest() {
|
||||
random = new Random();
|
||||
}
|
||||
private final Random random = new Random();
|
||||
|
||||
@Test
|
||||
public void testInitiatorWriteAndRead() throws Exception {
|
||||
testWriteAndRead(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResponderWriteAndRead() throws Exception {
|
||||
testWriteAndRead(false);
|
||||
}
|
||||
|
||||
private void testWriteAndRead(boolean initiator) throws Exception {
|
||||
public void testWriteAndRead() throws Exception {
|
||||
// Generate a random tag
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
random.nextBytes(tag);
|
||||
@@ -53,7 +42,10 @@ public class TransportIntegrationTest extends BriarTestCase {
|
||||
streamWriter.write(payload2);
|
||||
streamWriter.flush();
|
||||
byte[] output = out.toByteArray();
|
||||
assertEquals(TAG_LENGTH + MAX_FRAME_LENGTH * 2, output.length);
|
||||
assertEquals(TAG_LENGTH + STREAM_HEADER_LENGTH
|
||||
+ FRAME_HEADER_LENGTH + payload1.length + MAC_LENGTH
|
||||
+ FRAME_HEADER_LENGTH + payload2.length + MAC_LENGTH,
|
||||
output.length);
|
||||
// Read the tag back
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(output);
|
||||
byte[] recoveredTag = new byte[tag.length];
|
||||
|
||||
Reference in New Issue
Block a user