Use the same maximum frame length for all transports.

This commit is contained in:
akwizgran
2015-01-05 16:24:44 +00:00
parent 358166bc12
commit d3bf2d59a1
60 changed files with 194 additions and 321 deletions

View File

@@ -4,6 +4,7 @@ import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_FRAME_LENGTH;
import java.io.ByteArrayInputStream;
@@ -14,6 +15,7 @@ import org.briarproject.api.FormatException;
import org.briarproject.api.crypto.AuthenticatedCipher;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.SecretKey;
import org.briarproject.util.ByteUtils;
import org.junit.Test;
import com.google.inject.Guice;
@@ -23,9 +25,8 @@ public class StreamDecrypterImplTest extends BriarTestCase {
// FIXME: This is an integration test, not a unit test
private static final int FRAME_LENGTH = 1024;
private static final int MAX_PAYLOAD_LENGTH =
FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
MAX_FRAME_LENGTH - HEADER_LENGTH - MAC_LENGTH;
private final CryptoComponent crypto;
private final AuthenticatedCipher frameCipher;
@@ -42,16 +43,16 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testReadValidFrames() throws Exception {
// Generate two valid frames
byte[] frame = generateFrame(0, FRAME_LENGTH, 123, false, false);
byte[] frame1 = generateFrame(1, FRAME_LENGTH, 123, false, false);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, false);
byte[] frame1 = generateFrame(1, MAX_FRAME_LENGTH, 123, false, false);
// Concatenate the frames
byte[] valid = new byte[FRAME_LENGTH * 2];
System.arraycopy(frame, 0, valid, 0, FRAME_LENGTH);
System.arraycopy(frame1, 0, valid, FRAME_LENGTH, FRAME_LENGTH);
byte[] valid = new byte[MAX_FRAME_LENGTH * 2];
System.arraycopy(frame, 0, valid, 0, MAX_FRAME_LENGTH);
System.arraycopy(frame1, 0, valid, MAX_FRAME_LENGTH, MAX_FRAME_LENGTH);
// Read the frames
ByteArrayInputStream in = new ByteArrayInputStream(valid);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
byte[] payload = new byte[MAX_PAYLOAD_LENGTH];
assertEquals(123, i.readFrame(payload));
assertEquals(123, i.readFrame(payload));
@@ -60,14 +61,14 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testTruncatedFrameThrowsException() throws Exception {
// Generate a valid frame
byte[] frame = generateFrame(0, FRAME_LENGTH, 123, false, false);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, false);
// Chop off the last byte
byte[] truncated = new byte[FRAME_LENGTH - 1];
System.arraycopy(frame, 0, truncated, 0, FRAME_LENGTH - 1);
byte[] truncated = new byte[MAX_FRAME_LENGTH - 1];
System.arraycopy(frame, 0, truncated, 0, MAX_FRAME_LENGTH - 1);
// Try to read the frame, which should fail due to truncation
ByteArrayInputStream in = new ByteArrayInputStream(truncated);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail();
@@ -77,13 +78,13 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testModifiedFrameThrowsException() throws Exception {
// Generate a valid frame
byte[] frame = generateFrame(0, FRAME_LENGTH, 123, false, false);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, false);
// Modify a randomly chosen byte of the frame
frame[(int) (Math.random() * FRAME_LENGTH)] ^= 1;
frame[(int) (Math.random() * MAX_FRAME_LENGTH)] ^= 1;
// Try to read the frame, which should fail due to modification
ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail();
@@ -93,11 +94,12 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testShortNonFinalFrameThrowsException() throws Exception {
// Generate a short non-final frame
byte[] frame = generateFrame(0, FRAME_LENGTH - 1, 123, false, false);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH - 1, 123, false,
false);
// Try to read the frame, which should fail due to invalid length
ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail();
@@ -107,11 +109,11 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testShortFinalFrameDoesNotThrowException() throws Exception {
// Generate a short final frame
byte[] frame = generateFrame(0, FRAME_LENGTH - 1, 123, true, false);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH - 1, 123, true, false);
// Read the frame
ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
int length = i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
assertEquals(123, length);
}
@@ -119,12 +121,12 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testInvalidPayloadLengthThrowsException() throws Exception {
// Generate a frame with an invalid payload length
byte[] frame = generateFrame(0, FRAME_LENGTH, MAX_PAYLOAD_LENGTH + 1,
false, false);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, false);
ByteUtils.writeUint16(MAX_PAYLOAD_LENGTH + 1, frame, 0);
// Try to read the frame, which should fail due to invalid length
ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail();
@@ -134,11 +136,11 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testNonZeroPaddingThrowsException() throws Exception {
// Generate a frame with bad padding
byte[] frame = generateFrame(0, FRAME_LENGTH, 123, false, true);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, 123, false, true);
// Try to read the frame, which should fail due to bad padding
ByteArrayInputStream in = new ByteArrayInputStream(frame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
try {
i.readFrame(new byte[MAX_PAYLOAD_LENGTH]);
fail();
@@ -148,17 +150,18 @@ public class StreamDecrypterImplTest extends BriarTestCase {
@Test
public void testCannotReadBeyondFinalFrame() throws Exception {
// Generate a valid final frame and another valid final frame after it
byte[] frame = generateFrame(0, FRAME_LENGTH, MAX_PAYLOAD_LENGTH, true,
false);
byte[] frame1 = generateFrame(1, FRAME_LENGTH, 123, true, false);
byte[] frame = generateFrame(0, MAX_FRAME_LENGTH, MAX_PAYLOAD_LENGTH,
true, false);
byte[] frame1 = generateFrame(1, MAX_FRAME_LENGTH, 123, true, false);
// Concatenate the frames
byte[] extraFrame = new byte[FRAME_LENGTH * 2];
System.arraycopy(frame, 0, extraFrame, 0, FRAME_LENGTH);
System.arraycopy(frame1, 0, extraFrame, FRAME_LENGTH, FRAME_LENGTH);
byte[] extraFrame = new byte[MAX_FRAME_LENGTH * 2];
System.arraycopy(frame, 0, extraFrame, 0, MAX_FRAME_LENGTH);
System.arraycopy(frame1, 0, extraFrame, MAX_FRAME_LENGTH,
MAX_FRAME_LENGTH);
// Read the final frame, which should first read the tag
ByteArrayInputStream in = new ByteArrayInputStream(extraFrame);
StreamDecrypterImpl i = new StreamDecrypterImpl(in, frameCipher,
frameKey, FRAME_LENGTH);
frameKey);
byte[] payload = new byte[MAX_PAYLOAD_LENGTH];
assertEquals(MAX_PAYLOAD_LENGTH, i.readFrame(payload));
// The frame after the final frame should not be read

View File

@@ -4,6 +4,7 @@ import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_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.TAG_LENGTH;
import java.io.ByteArrayOutputStream;
@@ -24,8 +25,6 @@ public class StreamEncrypterImplTest extends BriarTestCase {
// FIXME: This is an integration test, not a unit test
private static final int FRAME_LENGTH = 1024;
private final CryptoComponent crypto;
private final AuthenticatedCipher frameCipher;
@@ -40,8 +39,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
public void testEncryptionWithoutTag() throws Exception {
int payloadLength = 123;
byte[] iv = new byte[IV_LENGTH], aad = new byte[AAD_LENGTH];
byte[] plaintext = new byte[FRAME_LENGTH - MAC_LENGTH];
byte[] ciphertext = new byte[FRAME_LENGTH];
byte[] plaintext = new byte[MAX_FRAME_LENGTH - MAC_LENGTH];
byte[] ciphertext = new byte[MAX_FRAME_LENGTH];
SecretKey frameKey = crypto.generateSecretKey();
// Calculate the expected ciphertext
FrameEncoder.encodeIv(iv, 0);
@@ -51,12 +50,12 @@ public class StreamEncrypterImplTest extends BriarTestCase {
frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0);
// Check that the actual ciphertext matches what's expected
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamEncrypterImpl o = new StreamEncrypterImpl(out,
frameCipher, frameKey, FRAME_LENGTH, null);
StreamEncrypterImpl o = new StreamEncrypterImpl(out, frameCipher,
frameKey, null);
o.writeFrame(new byte[payloadLength], payloadLength, false);
byte[] actual = out.toByteArray();
assertEquals(FRAME_LENGTH, actual.length);
for(int i = 0; i < FRAME_LENGTH; i++)
assertEquals(MAX_FRAME_LENGTH, actual.length);
for(int i = 0; i < MAX_FRAME_LENGTH; i++)
assertEquals(ciphertext[i], actual[i]);
}
@@ -66,8 +65,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
new Random().nextBytes(tag);
int payloadLength = 123;
byte[] iv = new byte[IV_LENGTH], aad = new byte[AAD_LENGTH];
byte[] plaintext = new byte[FRAME_LENGTH - MAC_LENGTH];
byte[] ciphertext = new byte[FRAME_LENGTH];
byte[] plaintext = new byte[MAX_FRAME_LENGTH - MAC_LENGTH];
byte[] ciphertext = new byte[MAX_FRAME_LENGTH];
SecretKey frameKey = crypto.generateSecretKey();
// Calculate the expected ciphertext
FrameEncoder.encodeIv(iv, 0);
@@ -77,13 +76,13 @@ public class StreamEncrypterImplTest extends BriarTestCase {
frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0);
// Check that the actual tag and ciphertext match what's expected
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamEncrypterImpl o = new StreamEncrypterImpl(out,
frameCipher, frameKey, FRAME_LENGTH, tag);
StreamEncrypterImpl o = new StreamEncrypterImpl(out, frameCipher,
frameKey, tag);
o.writeFrame(new byte[payloadLength], payloadLength, false);
byte[] actual = out.toByteArray();
assertEquals(TAG_LENGTH + FRAME_LENGTH, actual.length);
assertEquals(TAG_LENGTH + MAX_FRAME_LENGTH, actual.length);
for(int i = 0; i < TAG_LENGTH; i++) assertEquals(tag[i], actual[i]);
for(int i = 0; i < FRAME_LENGTH; i++)
for(int i = 0; i < MAX_FRAME_LENGTH; i++)
assertEquals(ciphertext[i], actual[TAG_LENGTH + i]);
}
@@ -93,10 +92,10 @@ public class StreamEncrypterImplTest extends BriarTestCase {
new Random().nextBytes(tag);
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Initiator's constructor
StreamEncrypterImpl o = new StreamEncrypterImpl(out,
frameCipher, crypto.generateSecretKey(), FRAME_LENGTH, tag);
StreamEncrypterImpl o = new StreamEncrypterImpl(out, frameCipher,
crypto.generateSecretKey(), tag);
// Write an empty final frame without having written any other frames
o.writeFrame(new byte[FRAME_LENGTH - MAC_LENGTH], 0, true);
o.writeFrame(new byte[MAX_FRAME_LENGTH - MAC_LENGTH], 0, true);
// The tag and the empty frame should be written to the output stream
assertEquals(TAG_LENGTH + HEADER_LENGTH + MAC_LENGTH, out.size());
}