mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
Decryption code for tagging every segment.
This commit is contained in:
@@ -2,6 +2,7 @@ package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
@@ -23,19 +24,21 @@ public class ConnectionDecrypterTest extends BriarTestCase {
|
||||
|
||||
private static final int MAC_LENGTH = 32;
|
||||
|
||||
private final Cipher frameCipher;
|
||||
private final ErasableKey frameKey;
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
|
||||
public ConnectionDecrypterTest() {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||
tagCipher = crypto.getTagCipher();
|
||||
frameCipher = crypto.getFrameCipher();
|
||||
tagKey = crypto.generateTestKey();
|
||||
frameKey = crypto.generateTestKey();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryption() throws Exception {
|
||||
public void testDecryptionWithFirstSegmentTagged() throws Exception {
|
||||
// Calculate the ciphertext for the first frame
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
@@ -57,8 +60,48 @@ public class ConnectionDecrypterTest extends BriarTestCase {
|
||||
out.write(ciphertext1);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
// Use a ConnectionDecrypter to decrypt the ciphertext
|
||||
FrameSource decrypter = new ConnectionDecrypter(in, frameCipher,
|
||||
frameKey, MAC_LENGTH);
|
||||
FrameSource decrypter = new ConnectionDecrypter(in, tagCipher,
|
||||
frameCipher, tagKey, frameKey, MAC_LENGTH, false);
|
||||
// First frame
|
||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||
for(int i = 0; i < plaintext.length; i++) {
|
||||
assertEquals(plaintext[i], decrypted[i]);
|
||||
}
|
||||
// Second frame
|
||||
assertEquals(plaintext1.length, decrypter.readFrame(decrypted));
|
||||
for(int i = 0; i < plaintext1.length; i++) {
|
||||
assertEquals(plaintext1[i], decrypted[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptionWithEverySegmentTagged() throws Exception {
|
||||
// Calculate the ciphertext for the first frame
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
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);
|
||||
// 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);
|
||||
byte[] ciphertext1 = new byte[TAG_LENGTH + plaintext1.length];
|
||||
TagEncoder.encodeTag(ciphertext1, 1, 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
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(ciphertext);
|
||||
out.write(ciphertext1);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
// Use a ConnectionDecrypter to decrypt the ciphertext
|
||||
FrameSource decrypter = new ConnectionDecrypter(in, tagCipher,
|
||||
frameCipher, tagKey, frameKey, MAC_LENGTH, true);
|
||||
// First frame
|
||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||
|
||||
@@ -91,8 +91,8 @@ public class FrameReadWriteTest extends BriarTestCase {
|
||||
assertArrayEquals(tag, recoveredTag);
|
||||
assertTrue(TagEncoder.validateTag(tag, 0, tagCipher, tagKey));
|
||||
// Read the frames back
|
||||
FrameSource decrypter = new ConnectionDecrypter(in, frameCipher,
|
||||
frameKey, mac.getMacLength());
|
||||
FrameSource decrypter = new ConnectionDecrypter(in, tagCipher,
|
||||
frameCipher, tagKey, frameKey, mac.getMacLength(), false);
|
||||
ConnectionReader reader = new ConnectionReaderImpl(decrypter, mac,
|
||||
macKey);
|
||||
InputStream in1 = reader.getInputStream();
|
||||
|
||||
@@ -2,6 +2,7 @@ package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -24,19 +25,21 @@ public class SegmentedConnectionDecrypterTest extends BriarTestCase {
|
||||
|
||||
private static final int MAC_LENGTH = 32;
|
||||
|
||||
private final Cipher frameCipher;
|
||||
private final ErasableKey frameKey;
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
|
||||
public SegmentedConnectionDecrypterTest() {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||
tagCipher = crypto.getTagCipher();
|
||||
frameCipher = crypto.getFrameCipher();
|
||||
tagKey = crypto.generateTestKey();
|
||||
frameKey = crypto.generateTestKey();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryption() throws Exception {
|
||||
public void testDecryptionWithFirstSegmentTagged() throws Exception {
|
||||
// Calculate the ciphertext for the first frame
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
@@ -55,8 +58,45 @@ public class SegmentedConnectionDecrypterTest extends BriarTestCase {
|
||||
// Use a connection decrypter to decrypt the ciphertext
|
||||
byte[][] frames = new byte[][] { ciphertext, ciphertext1 };
|
||||
SegmentSource in = new ByteArraySegmentSource(frames);
|
||||
FrameSource decrypter = new SegmentedConnectionDecrypter(in,
|
||||
frameCipher, frameKey, MAC_LENGTH);
|
||||
FrameSource decrypter = new SegmentedConnectionDecrypter(in, tagCipher,
|
||||
frameCipher, tagKey, frameKey, MAC_LENGTH, false);
|
||||
// First frame
|
||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||
for(int i = 0; i < plaintext.length; i++) {
|
||||
assertEquals(plaintext[i], decrypted[i]);
|
||||
}
|
||||
// Second frame
|
||||
assertEquals(plaintext1.length, decrypter.readFrame(decrypted));
|
||||
for(int i = 0; i < plaintext1.length; i++) {
|
||||
assertEquals(plaintext1[i], decrypted[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptionWithEverySegmentTagged() throws Exception {
|
||||
// Calculate the ciphertext for the first frame
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
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);
|
||||
// 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);
|
||||
byte[] ciphertext1 = new byte[TAG_LENGTH + plaintext1.length];
|
||||
TagEncoder.encodeTag(ciphertext1, 1, 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);
|
||||
// Use a connection decrypter to decrypt the ciphertext
|
||||
byte[][] frames = new byte[][] { ciphertext, ciphertext1 };
|
||||
SegmentSource in = new ByteArraySegmentSource(frames);
|
||||
FrameSource decrypter = new SegmentedConnectionDecrypter(in, tagCipher,
|
||||
frameCipher, tagKey, frameKey, MAC_LENGTH, true);
|
||||
// First frame
|
||||
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
|
||||
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
|
||||
@@ -85,7 +125,6 @@ public class SegmentedConnectionDecrypterTest extends BriarTestCase {
|
||||
byte[] src = frames[frame];
|
||||
System.arraycopy(src, 0, s.getBuffer(), 0, src.length);
|
||||
s.setLength(src.length);
|
||||
s.setTransmissionNumber(frame);
|
||||
frame++;
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user