mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 03:09:04 +01:00
Added factory methods for segmented connection readers.
This commit is contained in:
@@ -94,7 +94,7 @@ public class FrameReadWriteTest extends BriarTestCase {
|
||||
assertEquals(0L, TagEncoder.decodeTag(tag, tagCipher, tagKey));
|
||||
// Read the frames back
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
|
||||
tagCipher, frameCipher, tagKey, frameKey, false);
|
||||
tagCipher, frameCipher, tagKey, frameKey, false, recoveredTag);
|
||||
IncomingErrorCorrectionLayer correcter1 =
|
||||
new NullIncomingErrorCorrectionLayer(decrypter);
|
||||
ConnectionReader reader = new ConnectionReaderImpl(correcter1, mac,
|
||||
|
||||
@@ -12,7 +12,7 @@ import javax.crypto.spec.IvParameterSpec;
|
||||
import net.sf.briar.BriarTestCase;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.crypto.ErasableKey;
|
||||
import net.sf.briar.api.plugins.Segment;
|
||||
import net.sf.briar.api.transport.Segment;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
|
||||
import org.apache.commons.io.output.ByteArrayOutputStream;
|
||||
@@ -38,6 +38,9 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testDecryptionWithFirstSegmentTagged() throws Exception {
|
||||
// Calculate the tag for the first segment
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
|
||||
// Calculate the ciphertext for the first segment
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
@@ -53,15 +56,15 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext1 = frameCipher.doFinal(plaintext1, 0,
|
||||
plaintext1.length);
|
||||
// Concatenate the ciphertexts
|
||||
// Concatenate the ciphertexts, excluding the first tag
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(ciphertext);
|
||||
out.write(ciphertext1);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
// Use the encryption layer to decrypt the ciphertext
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
|
||||
tagCipher, frameCipher, tagKey, frameKey, false);
|
||||
// First frame
|
||||
tagCipher, frameCipher, tagKey, frameKey, false, tag);
|
||||
// First segment
|
||||
Segment s = new SegmentImpl();
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext.length, s.getLength());
|
||||
@@ -70,7 +73,7 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
for(int i = 0; i < plaintext.length; i++) {
|
||||
assertEquals(plaintext[i], decrypted[i]);
|
||||
}
|
||||
// Second frame
|
||||
// Second segment
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext1.length, s.getLength());
|
||||
assertEquals(1L, s.getSegmentNumber());
|
||||
@@ -82,6 +85,9 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testDecryptionWithEverySegmentTagged() throws Exception {
|
||||
// Calculate the tag for the first segment
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
|
||||
// Calculate the ciphertext for the first segment
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
@@ -89,25 +95,27 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext, 0, plaintext.length);
|
||||
// Calculate the ciphertext for the second segment, including its tag
|
||||
// Calculate the tag for the second segment
|
||||
byte[] tag1 = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag1, 1L, tagCipher, tagKey);
|
||||
// Calculate the ciphertext for the second segment
|
||||
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0);
|
||||
byte[] ciphertext1 = new byte[TAG_LENGTH + plaintext1.length];
|
||||
TagEncoder.encodeTag(ciphertext1, 1L, tagCipher, tagKey);
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
frameCipher.doFinal(plaintext1, 0, plaintext1.length, ciphertext1,
|
||||
TAG_LENGTH);
|
||||
// Concatenate the ciphertexts
|
||||
byte[] ciphertext1 = frameCipher.doFinal(plaintext1, 0,
|
||||
plaintext1.length);
|
||||
// Concatenate the ciphertexts, excluding the first tag
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(ciphertext);
|
||||
out.write(tag1);
|
||||
out.write(ciphertext1);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
// Use the encryption layer to decrypt the ciphertext
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
|
||||
tagCipher, frameCipher, tagKey, frameKey, true);
|
||||
// First frame
|
||||
tagCipher, frameCipher, tagKey, frameKey, true, tag);
|
||||
// First segment
|
||||
Segment s = new SegmentImpl();
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext.length, s.getLength());
|
||||
@@ -116,7 +124,7 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
for(int i = 0; i < plaintext.length; i++) {
|
||||
assertEquals(plaintext[i], decrypted[i]);
|
||||
}
|
||||
// Second frame
|
||||
// Second segment
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext1.length, s.getLength());
|
||||
assertEquals(1L, s.getSegmentNumber());
|
||||
|
||||
@@ -12,8 +12,8 @@ import javax.crypto.spec.IvParameterSpec;
|
||||
import net.sf.briar.BriarTestCase;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.crypto.ErasableKey;
|
||||
import net.sf.briar.api.plugins.Segment;
|
||||
import net.sf.briar.api.plugins.SegmentSource;
|
||||
import net.sf.briar.api.transport.Segment;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -38,13 +38,16 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testDecryptionWithFirstSegmentTagged() throws Exception {
|
||||
// Calculate the ciphertext for the first segment
|
||||
// Calculate the ciphertext for the first segment, including its tag
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
byte[] ciphertext = new byte[TAG_LENGTH + plaintext.length];
|
||||
TagEncoder.encodeTag(ciphertext, 0L, tagCipher, tagKey);
|
||||
byte[] iv = IvEncoder.encodeIv(0L, frameCipher.getBlockSize());
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext, 0, plaintext.length);
|
||||
frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext,
|
||||
TAG_LENGTH);
|
||||
// Calculate the ciphertext for the second segment
|
||||
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0);
|
||||
@@ -53,13 +56,17 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext1 = frameCipher.doFinal(plaintext1, 0,
|
||||
plaintext1.length);
|
||||
// Buffer the first segment and create a source for the second
|
||||
Segment buffered = new SegmentImpl();
|
||||
System.arraycopy(ciphertext, 0, buffered.getBuffer(), 0,
|
||||
ciphertext.length);
|
||||
buffered.setLength(ciphertext.length);
|
||||
SegmentSource in = new ByteArraySegmentSource(ciphertext1);
|
||||
// Use the encryption layer to decrypt the ciphertext
|
||||
byte[][] frames = new byte[][] { ciphertext, ciphertext1 };
|
||||
SegmentSource in = new ByteArraySegmentSource(frames);
|
||||
IncomingEncryptionLayer decrypter =
|
||||
new IncomingSegmentedEncryptionLayer(in, tagCipher, frameCipher,
|
||||
tagKey, frameKey, false);
|
||||
// First frame
|
||||
tagKey, frameKey, false, buffered);
|
||||
// First segment
|
||||
Segment s = new SegmentImpl();
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext.length, s.getLength());
|
||||
@@ -68,7 +75,7 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
for(int i = 0; i < plaintext.length; i++) {
|
||||
assertEquals(plaintext[i], decrypted[i]);
|
||||
}
|
||||
// Second frame
|
||||
// Second segment
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext1.length, s.getLength());
|
||||
assertEquals(1L, s.getSegmentNumber());
|
||||
@@ -80,13 +87,16 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testDecryptionWithEverySegmentTagged() throws Exception {
|
||||
// Calculate the ciphertext for the first frame
|
||||
// Calculate the ciphertext for the first segment, including its tag
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
byte[] ciphertext = new byte[TAG_LENGTH + plaintext.length];
|
||||
TagEncoder.encodeTag(ciphertext, 0L, tagCipher, tagKey);
|
||||
byte[] iv = IvEncoder.encodeIv(0L, frameCipher.getBlockSize());
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext, 0, plaintext.length);
|
||||
frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext,
|
||||
TAG_LENGTH);
|
||||
// Calculate the ciphertext for the second frame, including its tag
|
||||
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0);
|
||||
@@ -97,13 +107,17 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
frameCipher.doFinal(plaintext1, 0, plaintext1.length, ciphertext1,
|
||||
TAG_LENGTH);
|
||||
// Buffer the first segment and create a source for the second
|
||||
Segment buffered = new SegmentImpl();
|
||||
System.arraycopy(ciphertext, 0, buffered.getBuffer(), 0,
|
||||
ciphertext.length);
|
||||
buffered.setLength(ciphertext.length);
|
||||
SegmentSource in = new ByteArraySegmentSource(ciphertext1);
|
||||
// Use the encryption layer to decrypt the ciphertext
|
||||
byte[][] frames = new byte[][] { ciphertext, ciphertext1 };
|
||||
SegmentSource in = new ByteArraySegmentSource(frames);
|
||||
IncomingEncryptionLayer decrypter =
|
||||
new IncomingSegmentedEncryptionLayer(in, tagCipher, frameCipher,
|
||||
tagKey, frameKey, true);
|
||||
// First frame
|
||||
tagKey, frameKey, true, buffered);
|
||||
// First segment
|
||||
Segment s = new SegmentImpl();
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext.length, s.getLength());
|
||||
@@ -112,7 +126,7 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
for(int i = 0; i < plaintext.length; i++) {
|
||||
assertEquals(plaintext[i], decrypted[i]);
|
||||
}
|
||||
// Second frame
|
||||
// Second segment
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
assertEquals(plaintext1.length, s.getLength());
|
||||
assertEquals(1L, s.getSegmentNumber());
|
||||
@@ -124,20 +138,15 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
|
||||
private static class ByteArraySegmentSource implements SegmentSource {
|
||||
|
||||
private final byte[][] segments;
|
||||
private final byte[] segment;
|
||||
|
||||
private int segmentNumber = 0;
|
||||
|
||||
private ByteArraySegmentSource(byte[][] frames) {
|
||||
this.segments = frames;
|
||||
private ByteArraySegmentSource(byte[] segment) {
|
||||
this.segment = segment;
|
||||
}
|
||||
|
||||
public boolean readSegment(Segment s) throws IOException {
|
||||
if(segmentNumber == segments.length) return false;
|
||||
byte[] src = segments[segmentNumber];
|
||||
System.arraycopy(src, 0, s.getBuffer(), 0, src.length);
|
||||
s.setLength(src.length);
|
||||
segmentNumber++;
|
||||
System.arraycopy(segment, 0, s.getBuffer(), 0, segment.length);
|
||||
s.setLength(segment.length);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import net.sf.briar.api.FormatException;
|
||||
import net.sf.briar.api.plugins.Segment;
|
||||
import net.sf.briar.api.transport.Segment;
|
||||
|
||||
/** An encryption layer that performs no encryption. */
|
||||
class NullIncomingEncryptionLayer implements IncomingEncryptionLayer {
|
||||
|
||||
@@ -3,7 +3,7 @@ package net.sf.briar.transport;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import net.sf.briar.api.plugins.Segment;
|
||||
import net.sf.briar.api.transport.Segment;
|
||||
|
||||
/** An encryption layer that performs no encryption. */
|
||||
class NullOutgoingEncryptionLayer implements OutgoingEncryptionLayer {
|
||||
|
||||
@@ -11,7 +11,7 @@ import javax.crypto.spec.IvParameterSpec;
|
||||
import net.sf.briar.BriarTestCase;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.crypto.ErasableKey;
|
||||
import net.sf.briar.api.plugins.Segment;
|
||||
import net.sf.briar.api.transport.Segment;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -12,8 +12,8 @@ import javax.crypto.spec.IvParameterSpec;
|
||||
import net.sf.briar.BriarTestCase;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.crypto.ErasableKey;
|
||||
import net.sf.briar.api.plugins.Segment;
|
||||
import net.sf.briar.api.plugins.SegmentSink;
|
||||
import net.sf.briar.api.transport.Segment;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
Reference in New Issue
Block a user