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