Initial support for tagging every segment (untested).

This commit is contained in:
akwizgran
2012-01-13 15:50:43 +00:00
parent ac136d3732
commit 07f8607c04
12 changed files with 86 additions and 72 deletions

View File

@@ -1,5 +1,6 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import static org.junit.Assert.assertArrayEquals;
import java.io.ByteArrayOutputStream;
@@ -37,7 +38,8 @@ public class ConnectionEncrypterImplTest extends BriarTestCase {
@Test
public void testEncryption() throws Exception {
// Calculate the expected tag
byte[] tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
byte[] tag = new byte[TAG_LENGTH];
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
// Calculate the expected ciphertext for the first frame
byte[] iv = new byte[frameCipher.getBlockSize()];
byte[] plaintext = new byte[123 + MAC_LENGTH];
@@ -59,7 +61,7 @@ public class ConnectionEncrypterImplTest extends BriarTestCase {
// Use a ConnectionEncrypter to encrypt the plaintext
out.reset();
ConnectionEncrypter e = new ConnectionEncrypterImpl(out, Long.MAX_VALUE,
tagCipher, frameCipher, tagKey, frameKey);
tagCipher, frameCipher, tagKey, frameKey, false);
e.writeFrame(plaintext, plaintext.length);
e.writeFrame(plaintext1, plaintext1.length);
byte[] actual = out.toByteArray();

View File

@@ -617,6 +617,8 @@ public class ConnectionRecogniserImplTest extends BriarTestCase {
// Calculate the expected tag for connection number 3
ErasableKey tagKey = crypto.deriveTagKey(secret, true);
Cipher tagCipher = crypto.getTagCipher();
return TagEncoder.encodeTag(0, tagCipher, tagKey);
byte[] tag = new byte[TAG_LENGTH];
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
return tag;
}
}

View File

@@ -61,7 +61,8 @@ public class FrameReadWriteTest extends BriarTestCase {
private void testWriteAndRead(boolean initiator) throws Exception {
// Encode the tag
byte[] tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
byte[] tag = new byte[TAG_LENGTH];
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
// Generate two random frames
byte[] frame = new byte[12345];
random.nextBytes(frame);
@@ -74,7 +75,8 @@ public class FrameReadWriteTest extends BriarTestCase {
// Write the frames
ByteArrayOutputStream out = new ByteArrayOutputStream();
ConnectionEncrypter encrypter = new ConnectionEncrypterImpl(out,
Long.MAX_VALUE, tagCipher, frameCipher, tagCopy, frameCopy);
Long.MAX_VALUE, tagCipher, frameCipher, tagCopy, frameCopy,
false);
ConnectionWriter writer = new ConnectionWriterImpl(encrypter, mac,
macCopy);
OutputStream out1 = writer.getOutputStream();

View File

@@ -1,5 +1,6 @@
package net.sf.briar.transport;
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import static org.junit.Assert.assertArrayEquals;
import java.io.ByteArrayOutputStream;
@@ -40,7 +41,8 @@ public class SegmentedConnectionEncrypterTest extends BriarTestCase {
@Test
public void testEncryption() throws Exception {
// Calculate the expected tag
byte[] tag = TagEncoder.encodeTag(0, tagCipher, tagKey);
byte[] tag = new byte[TAG_LENGTH];
TagEncoder.encodeTag(tag, 0, tagCipher, tagKey);
// Calculate the expected ciphertext for the first frame
byte[] iv = new byte[frameCipher.getBlockSize()];
byte[] plaintext = new byte[123 + MAC_LENGTH];
@@ -62,7 +64,8 @@ public class SegmentedConnectionEncrypterTest extends BriarTestCase {
// Use a connection encrypter to encrypt the plaintext
ByteArraySegmentSink sink = new ByteArraySegmentSink();
ConnectionEncrypter e = new SegmentedConnectionEncrypter(sink,
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey);
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey,
false);
// The first frame's buffer must have enough space for the tag
e.writeFrame(plaintext, plaintext.length);
e.writeFrame(plaintext1, plaintext1.length);