mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 04:18:53 +01:00
Renamed "encrypted IVs" as "tags" (actual crypto changes to follow).
This commit is contained in:
@@ -52,6 +52,7 @@ import net.sf.briar.api.transport.ConnectionReader;
|
||||
import net.sf.briar.api.transport.ConnectionReaderFactory;
|
||||
import net.sf.briar.api.transport.ConnectionWriter;
|
||||
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import net.sf.briar.crypto.CryptoModule;
|
||||
import net.sf.briar.db.DatabaseModule;
|
||||
import net.sf.briar.lifecycle.LifecycleModule;
|
||||
@@ -206,13 +207,13 @@ public class ProtocolIntegrationTest extends TestCase {
|
||||
|
||||
private void read(byte[] connectionData) throws Exception {
|
||||
InputStream in = new ByteArrayInputStream(connectionData);
|
||||
byte[] encryptedIv = new byte[16];
|
||||
assertEquals(16, in.read(encryptedIv, 0, 16));
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
assertEquals(TAG_LENGTH, in.read(tag, 0, TAG_LENGTH));
|
||||
ConnectionContext ctx =
|
||||
connectionContextFactory.createConnectionContext(contactId,
|
||||
transportIndex, connection, Arrays.clone(secret));
|
||||
ConnectionReader r = connectionReaderFactory.createConnectionReader(in,
|
||||
ctx, encryptedIv);
|
||||
ctx, tag);
|
||||
in = r.getInputStream();
|
||||
ProtocolReader protocolReader =
|
||||
protocolReaderFactory.createProtocolReader(in);
|
||||
|
||||
@@ -29,8 +29,8 @@ public class KeyDerivationTest extends TestCase {
|
||||
List<ErasableKey> keys = new ArrayList<ErasableKey>();
|
||||
keys.add(crypto.deriveFrameKey(secret, true));
|
||||
keys.add(crypto.deriveFrameKey(secret, false));
|
||||
keys.add(crypto.deriveIvKey(secret, true));
|
||||
keys.add(crypto.deriveIvKey(secret, false));
|
||||
keys.add(crypto.deriveTagKey(secret, true));
|
||||
keys.add(crypto.deriveTagKey(secret, false));
|
||||
keys.add(crypto.deriveMacKey(secret, true));
|
||||
keys.add(crypto.deriveMacKey(secret, false));
|
||||
for(int i = 0; i < 6; i++) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.IV_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -25,8 +25,8 @@ public class ConnectionDecrypterImplTest extends TestCase {
|
||||
|
||||
private static final int MAC_LENGTH = 32;
|
||||
|
||||
private final Cipher ivCipher, frameCipher;
|
||||
private final ErasableKey ivKey, frameKey;
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
private final TransportIndex transportIndex = new TransportIndex(13);
|
||||
private final long connection = 12345L;
|
||||
|
||||
@@ -34,9 +34,9 @@ public class ConnectionDecrypterImplTest extends TestCase {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||
ivCipher = crypto.getIvCipher();
|
||||
tagCipher = crypto.getTagCipher();
|
||||
frameCipher = crypto.getFrameCipher();
|
||||
ivKey = crypto.generateTestKey();
|
||||
tagKey = crypto.generateTestKey();
|
||||
frameKey = crypto.generateTestKey();
|
||||
}
|
||||
|
||||
@@ -53,9 +53,9 @@ public class ConnectionDecrypterImplTest extends TestCase {
|
||||
private void testDecryption(boolean initiator) throws Exception {
|
||||
// Calculate the plaintext and ciphertext for the IV
|
||||
byte[] iv = IvEncoder.encodeIv(transportIndex.getInt(), connection);
|
||||
ivCipher.init(Cipher.ENCRYPT_MODE, ivKey);
|
||||
byte[] encryptedIv = ivCipher.doFinal(iv);
|
||||
assertEquals(IV_LENGTH, encryptedIv.length);
|
||||
tagCipher.init(Cipher.ENCRYPT_MODE, tagKey);
|
||||
byte[] tag = tagCipher.doFinal(iv);
|
||||
assertEquals(TAG_LENGTH, tag.length);
|
||||
// Calculate the expected plaintext for the first frame
|
||||
byte[] ciphertext = new byte[123];
|
||||
byte[] ciphertextMac = new byte[MAC_LENGTH];
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.IV_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -23,8 +23,8 @@ public class ConnectionEncrypterImplTest extends TestCase {
|
||||
|
||||
private static final int MAC_LENGTH = 32;
|
||||
|
||||
private final Cipher ivCipher, frameCipher;
|
||||
private final ErasableKey ivKey, frameKey;
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
private final TransportIndex transportIndex = new TransportIndex(13);
|
||||
private final long connection = 12345L;
|
||||
|
||||
@@ -32,9 +32,9 @@ public class ConnectionEncrypterImplTest extends TestCase {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||
ivCipher = crypto.getIvCipher();
|
||||
tagCipher = crypto.getTagCipher();
|
||||
frameCipher = crypto.getFrameCipher();
|
||||
ivKey = crypto.generateTestKey();
|
||||
tagKey = crypto.generateTestKey();
|
||||
frameKey = crypto.generateTestKey();
|
||||
}
|
||||
|
||||
@@ -51,9 +51,9 @@ public class ConnectionEncrypterImplTest extends TestCase {
|
||||
private void testEncryption(boolean initiator) throws Exception {
|
||||
// Calculate the expected ciphertext for the IV
|
||||
byte[] iv = IvEncoder.encodeIv(transportIndex.getInt(), connection);
|
||||
ivCipher.init(Cipher.ENCRYPT_MODE, ivKey);
|
||||
byte[] encryptedIv = ivCipher.doFinal(iv);
|
||||
assertEquals(IV_LENGTH, encryptedIv.length);
|
||||
tagCipher.init(Cipher.ENCRYPT_MODE, tagKey);
|
||||
byte[] tag = tagCipher.doFinal(iv);
|
||||
assertEquals(TAG_LENGTH, tag.length);
|
||||
// Calculate the expected ciphertext for the first frame
|
||||
byte[] plaintext = new byte[123];
|
||||
byte[] plaintextMac = new byte[MAC_LENGTH];
|
||||
@@ -76,7 +76,7 @@ public class ConnectionEncrypterImplTest extends TestCase {
|
||||
offset);
|
||||
// Concatenate the ciphertexts
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(encryptedIv);
|
||||
out.write(tag);
|
||||
out.write(ciphertext);
|
||||
out.write(ciphertext1);
|
||||
byte[] expected = out.toByteArray();
|
||||
@@ -84,7 +84,7 @@ public class ConnectionEncrypterImplTest extends TestCase {
|
||||
out.reset();
|
||||
iv = IvEncoder.encodeIv(transportIndex.getInt(), connection);
|
||||
ConnectionEncrypter e = new ConnectionEncrypterImpl(out, Long.MAX_VALUE,
|
||||
iv, ivCipher, frameCipher, ivKey, frameKey);
|
||||
iv, tagCipher, frameCipher, tagKey, frameKey);
|
||||
e.getOutputStream().write(plaintext);
|
||||
e.writeMac(plaintextMac);
|
||||
e.getOutputStream().write(plaintext1);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.IV_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@@ -83,7 +83,7 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
Executor executor = new ImmediateExecutor();
|
||||
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
|
||||
executor);
|
||||
assertNull(c.acceptConnection(transportId, new byte[IV_LENGTH]));
|
||||
assertNull(c.acceptConnection(transportId, new byte[TAG_LENGTH]));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@@ -111,18 +111,18 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
Executor executor = new ImmediateExecutor();
|
||||
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
|
||||
executor);
|
||||
byte[] encryptedIv = calculateIv();
|
||||
// The IV should not be expected by the wrong transport
|
||||
byte[] tag = calculateTag();
|
||||
// The tag should not be expected by the wrong transport
|
||||
TransportId wrong = new TransportId(TestUtils.getRandomId());
|
||||
assertNull(c.acceptConnection(wrong, encryptedIv));
|
||||
// The IV should be expected by the right transport
|
||||
ConnectionContext ctx = c.acceptConnection(transportId, encryptedIv);
|
||||
assertNull(c.acceptConnection(wrong, tag));
|
||||
// The tag should be expected by the right transport
|
||||
ConnectionContext ctx = c.acceptConnection(transportId, tag);
|
||||
assertNotNull(ctx);
|
||||
assertEquals(contactId, ctx.getContactId());
|
||||
assertEquals(remoteIndex, ctx.getTransportIndex());
|
||||
assertEquals(3, ctx.getConnectionNumber());
|
||||
// The IV should no longer be expected
|
||||
assertNull(c.acceptConnection(transportId, encryptedIv));
|
||||
// The tag should no longer be expected
|
||||
assertNull(c.acceptConnection(transportId, tag));
|
||||
// The window should have advanced
|
||||
Map<Long, byte[]> unseen = window.getUnseen();
|
||||
assertEquals(19, unseen.size());
|
||||
@@ -152,15 +152,15 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
Executor executor = new ImmediateExecutor();
|
||||
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
|
||||
executor);
|
||||
byte[] encryptedIv = calculateIv();
|
||||
byte[] tag = calculateTag();
|
||||
// Ensure the recogniser is initialised
|
||||
assertFalse(c.isInitialised());
|
||||
assertNull(c.acceptConnection(transportId, new byte[IV_LENGTH]));
|
||||
assertNull(c.acceptConnection(transportId, new byte[TAG_LENGTH]));
|
||||
assertTrue(c.isInitialised());
|
||||
// Remove the contact
|
||||
c.eventOccurred(new ContactRemovedEvent(contactId));
|
||||
// The IV should not be expected
|
||||
assertNull(c.acceptConnection(transportId, encryptedIv));
|
||||
// The tag should not be expected
|
||||
assertNull(c.acceptConnection(transportId, tag));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@@ -179,12 +179,12 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
Executor executor = new ImmediateExecutor();
|
||||
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
|
||||
executor);
|
||||
byte[] encryptedIv = calculateIv();
|
||||
byte[] tag = calculateTag();
|
||||
// Remove the contact
|
||||
c.eventOccurred(new ContactRemovedEvent(contactId));
|
||||
// The IV should not be expected
|
||||
// The tag should not be expected
|
||||
assertFalse(c.isInitialised());
|
||||
assertNull(c.acceptConnection(transportId, encryptedIv));
|
||||
assertNull(c.acceptConnection(transportId, tag));
|
||||
assertTrue(c.isInitialised());
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -216,21 +216,21 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
Executor executor = new ImmediateExecutor();
|
||||
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
|
||||
executor);
|
||||
byte[] encryptedIv = calculateIv();
|
||||
// The IV should not be expected
|
||||
byte[] tag = calculateTag();
|
||||
// The tag should not be expected
|
||||
assertFalse(c.isInitialised());
|
||||
assertNull(c.acceptConnection(transportId, encryptedIv));
|
||||
assertNull(c.acceptConnection(transportId, tag));
|
||||
assertTrue(c.isInitialised());
|
||||
// Add the transport
|
||||
c.eventOccurred(new TransportAddedEvent(transportId));
|
||||
// The IV should be expected
|
||||
ConnectionContext ctx = c.acceptConnection(transportId, encryptedIv);
|
||||
// The tag should be expected
|
||||
ConnectionContext ctx = c.acceptConnection(transportId, tag);
|
||||
assertNotNull(ctx);
|
||||
assertEquals(contactId, ctx.getContactId());
|
||||
assertEquals(remoteIndex, ctx.getTransportIndex());
|
||||
assertEquals(3, ctx.getConnectionNumber());
|
||||
// The IV should no longer be expected
|
||||
assertNull(c.acceptConnection(transportId, encryptedIv));
|
||||
// The tag should no longer be expected
|
||||
assertNull(c.acceptConnection(transportId, tag));
|
||||
// The window should have advanced
|
||||
Map<Long, byte[]> unseen = window.getUnseen();
|
||||
assertEquals(19, unseen.size());
|
||||
@@ -264,19 +264,19 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
Executor executor = new ImmediateExecutor();
|
||||
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
|
||||
executor);
|
||||
byte[] encryptedIv = calculateIv();
|
||||
byte[] tag = calculateTag();
|
||||
// Add the transport
|
||||
c.eventOccurred(new TransportAddedEvent(transportId));
|
||||
// The IV should be expected
|
||||
// The tag should be expected
|
||||
assertFalse(c.isInitialised());
|
||||
ConnectionContext ctx = c.acceptConnection(transportId, encryptedIv);
|
||||
ConnectionContext ctx = c.acceptConnection(transportId, tag);
|
||||
assertTrue(c.isInitialised());
|
||||
assertNotNull(ctx);
|
||||
assertEquals(contactId, ctx.getContactId());
|
||||
assertEquals(remoteIndex, ctx.getTransportIndex());
|
||||
assertEquals(3, ctx.getConnectionNumber());
|
||||
// The IV should no longer be expected
|
||||
assertNull(c.acceptConnection(transportId, encryptedIv));
|
||||
// The tag should no longer be expected
|
||||
assertNull(c.acceptConnection(transportId, tag));
|
||||
// The window should have advanced
|
||||
Map<Long, byte[]> unseen = window.getUnseen();
|
||||
assertEquals(19, unseen.size());
|
||||
@@ -311,22 +311,22 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
Executor executor = new ImmediateExecutor();
|
||||
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
|
||||
executor);
|
||||
byte[] encryptedIv = calculateIv();
|
||||
// The IV should not be expected
|
||||
byte[] tag = calculateTag();
|
||||
// The tag should not be expected
|
||||
assertFalse(c.isInitialised());
|
||||
assertNull(c.acceptConnection(transportId, encryptedIv));
|
||||
assertNull(c.acceptConnection(transportId, tag));
|
||||
assertTrue(c.isInitialised());
|
||||
// Update the contact
|
||||
c.eventOccurred(new RemoteTransportsUpdatedEvent(contactId,
|
||||
remoteTransports));
|
||||
// The IV should be expected
|
||||
ConnectionContext ctx = c.acceptConnection(transportId, encryptedIv);
|
||||
// The tag should be expected
|
||||
ConnectionContext ctx = c.acceptConnection(transportId, tag);
|
||||
assertNotNull(ctx);
|
||||
assertEquals(contactId, ctx.getContactId());
|
||||
assertEquals(remoteIndex, ctx.getTransportIndex());
|
||||
assertEquals(3, ctx.getConnectionNumber());
|
||||
// The IV should no longer be expected
|
||||
assertNull(c.acceptConnection(transportId, encryptedIv));
|
||||
// The tag should no longer be expected
|
||||
assertNull(c.acceptConnection(transportId, tag));
|
||||
// The window should have advanced
|
||||
Map<Long, byte[]> unseen = window.getUnseen();
|
||||
assertEquals(19, unseen.size());
|
||||
@@ -360,20 +360,20 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
Executor executor = new ImmediateExecutor();
|
||||
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
|
||||
executor);
|
||||
byte[] encryptedIv = calculateIv();
|
||||
byte[] tag = calculateTag();
|
||||
// Update the contact
|
||||
c.eventOccurred(new RemoteTransportsUpdatedEvent(contactId,
|
||||
remoteTransports));
|
||||
// The IV should be expected
|
||||
// The tag should be expected
|
||||
assertFalse(c.isInitialised());
|
||||
ConnectionContext ctx = c.acceptConnection(transportId, encryptedIv);
|
||||
ConnectionContext ctx = c.acceptConnection(transportId, tag);
|
||||
assertTrue(c.isInitialised());
|
||||
assertNotNull(ctx);
|
||||
assertEquals(contactId, ctx.getContactId());
|
||||
assertEquals(remoteIndex, ctx.getTransportIndex());
|
||||
assertEquals(3, ctx.getConnectionNumber());
|
||||
// The IV should no longer be expected
|
||||
assertNull(c.acceptConnection(transportId, encryptedIv));
|
||||
// The tag should no longer be expected
|
||||
assertNull(c.acceptConnection(transportId, tag));
|
||||
// The window should have advanced
|
||||
Map<Long, byte[]> unseen = window.getUnseen();
|
||||
assertEquals(19, unseen.size());
|
||||
@@ -403,16 +403,16 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
Executor executor = new ImmediateExecutor();
|
||||
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
|
||||
executor);
|
||||
byte[] encryptedIv = calculateIv();
|
||||
byte[] tag = calculateTag();
|
||||
// Ensure the recogniser is initialised
|
||||
assertFalse(c.isInitialised());
|
||||
assertNull(c.acceptConnection(transportId, new byte[IV_LENGTH]));
|
||||
assertNull(c.acceptConnection(transportId, new byte[TAG_LENGTH]));
|
||||
assertTrue(c.isInitialised());
|
||||
// Update the contact
|
||||
c.eventOccurred(new RemoteTransportsUpdatedEvent(contactId,
|
||||
Collections.<Transport>emptyList()));
|
||||
// The IV should not be expected
|
||||
assertNull(c.acceptConnection(transportId, encryptedIv));
|
||||
// The tag should not be expected
|
||||
assertNull(c.acceptConnection(transportId, tag));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@@ -433,13 +433,13 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
Executor executor = new ImmediateExecutor();
|
||||
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
|
||||
executor);
|
||||
byte[] encryptedIv = calculateIv();
|
||||
byte[] tag = calculateTag();
|
||||
// Update the contact
|
||||
c.eventOccurred(new RemoteTransportsUpdatedEvent(contactId,
|
||||
Collections.<Transport>emptyList()));
|
||||
// The IV should not be expected
|
||||
// The tag should not be expected
|
||||
assertFalse(c.isInitialised());
|
||||
assertNull(c.acceptConnection(transportId, encryptedIv));
|
||||
assertNull(c.acceptConnection(transportId, tag));
|
||||
assertTrue(c.isInitialised());
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -499,24 +499,24 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
Executor executor = new ImmediateExecutor();
|
||||
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
|
||||
executor);
|
||||
byte[] encryptedIv = calculateIv();
|
||||
byte[] tag = calculateTag();
|
||||
// Ensure the recogniser is initialised
|
||||
assertFalse(c.isInitialised());
|
||||
assertNull(c.acceptConnection(transportId, new byte[IV_LENGTH]));
|
||||
assertNull(c.acceptConnection(transportId, new byte[TAG_LENGTH]));
|
||||
assertTrue(c.isInitialised());
|
||||
// Update the contact
|
||||
c.eventOccurred(new RemoteTransportsUpdatedEvent(contactId,
|
||||
remoteTransports1));
|
||||
// The IV should not be expected by the old transport
|
||||
assertNull(c.acceptConnection(transportId, encryptedIv));
|
||||
// The IV should be expected by the new transport
|
||||
ConnectionContext ctx = c.acceptConnection(transportId1, encryptedIv);
|
||||
// The tag should not be expected by the old transport
|
||||
assertNull(c.acceptConnection(transportId, tag));
|
||||
// The tag should be expected by the new transport
|
||||
ConnectionContext ctx = c.acceptConnection(transportId1, tag);
|
||||
assertNotNull(ctx);
|
||||
assertEquals(contactId, ctx.getContactId());
|
||||
assertEquals(remoteIndex, ctx.getTransportIndex());
|
||||
assertEquals(3, ctx.getConnectionNumber());
|
||||
// The IV should no longer be expected
|
||||
assertNull(c.acceptConnection(transportId1, encryptedIv));
|
||||
// The tag should no longer be expected
|
||||
assertNull(c.acceptConnection(transportId1, tag));
|
||||
// The window should have advanced
|
||||
Map<Long, byte[]> unseen = window.getUnseen();
|
||||
assertEquals(19, unseen.size());
|
||||
@@ -576,22 +576,22 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
Executor executor = new ImmediateExecutor();
|
||||
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
|
||||
executor);
|
||||
byte[] encryptedIv = calculateIv();
|
||||
byte[] tag = calculateTag();
|
||||
// Update the contact
|
||||
c.eventOccurred(new RemoteTransportsUpdatedEvent(contactId,
|
||||
remoteTransports1));
|
||||
// The IV should not be expected by the old transport
|
||||
// The tag should not be expected by the old transport
|
||||
assertFalse(c.isInitialised());
|
||||
assertNull(c.acceptConnection(transportId, encryptedIv));
|
||||
assertNull(c.acceptConnection(transportId, tag));
|
||||
assertTrue(c.isInitialised());
|
||||
// The IV should be expected by the new transport
|
||||
ConnectionContext ctx = c.acceptConnection(transportId1, encryptedIv);
|
||||
// The tag should be expected by the new transport
|
||||
ConnectionContext ctx = c.acceptConnection(transportId1, tag);
|
||||
assertNotNull(ctx);
|
||||
assertEquals(contactId, ctx.getContactId());
|
||||
assertEquals(remoteIndex, ctx.getTransportIndex());
|
||||
assertEquals(3, ctx.getConnectionNumber());
|
||||
// The IV should no longer be expected
|
||||
assertNull(c.acceptConnection(transportId1, encryptedIv));
|
||||
// The tag should no longer be expected
|
||||
assertNull(c.acceptConnection(transportId1, tag));
|
||||
// The window should have advanced
|
||||
Map<Long, byte[]> unseen = window.getUnseen();
|
||||
assertEquals(19, unseen.size());
|
||||
@@ -608,17 +608,17 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
};
|
||||
}
|
||||
|
||||
private byte[] calculateIv() throws Exception {
|
||||
private byte[] calculateTag() throws Exception {
|
||||
// Calculate the shared secret for connection number 3
|
||||
byte[] secret = inSecret;
|
||||
for(int i = 0; i < 4; i++) {
|
||||
secret = crypto.deriveNextSecret(secret, remoteIndex.getInt(), i);
|
||||
}
|
||||
// Calculate the expected IV for connection number 3
|
||||
ErasableKey ivKey = crypto.deriveIvKey(secret, true);
|
||||
Cipher ivCipher = crypto.getIvCipher();
|
||||
ivCipher.init(Cipher.ENCRYPT_MODE, ivKey);
|
||||
// Calculate the expected tag for connection number 3
|
||||
ErasableKey tagKey = crypto.deriveTagKey(secret, true);
|
||||
Cipher tagCipher = crypto.getTagCipher();
|
||||
tagCipher.init(Cipher.ENCRYPT_MODE, tagKey);
|
||||
byte[] iv = IvEncoder.encodeIv(remoteIndex.getInt(), 3);
|
||||
return ivCipher.doFinal(iv);
|
||||
return tagCipher.doFinal(iv);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.IV_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -28,10 +28,10 @@ import com.google.inject.Injector;
|
||||
public class FrameReadWriteTest extends TestCase {
|
||||
|
||||
private final CryptoComponent crypto;
|
||||
private final Cipher ivCipher, frameCipher;
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final Random random;
|
||||
private final byte[] outSecret;
|
||||
private final ErasableKey ivKey, frameKey, macKey;
|
||||
private final ErasableKey tagKey, frameKey, macKey;
|
||||
private final Mac mac;
|
||||
private final TransportIndex transportIndex = new TransportIndex(13);
|
||||
private final long connection = 12345L;
|
||||
@@ -40,13 +40,13 @@ public class FrameReadWriteTest extends TestCase {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
crypto = i.getInstance(CryptoComponent.class);
|
||||
ivCipher = crypto.getIvCipher();
|
||||
tagCipher = crypto.getTagCipher();
|
||||
frameCipher = crypto.getFrameCipher();
|
||||
random = new Random();
|
||||
// Since we're sending frames to ourselves, we only need outgoing keys
|
||||
outSecret = new byte[32];
|
||||
random.nextBytes(outSecret);
|
||||
ivKey = crypto.deriveIvKey(outSecret, true);
|
||||
tagKey = crypto.deriveTagKey(outSecret, true);
|
||||
frameKey = crypto.deriveFrameKey(outSecret, true);
|
||||
macKey = crypto.deriveMacKey(outSecret, true);
|
||||
mac = crypto.getMac();
|
||||
@@ -65,9 +65,9 @@ public class FrameReadWriteTest extends TestCase {
|
||||
private void testWriteAndRead(boolean initiator) throws Exception {
|
||||
// Create and encrypt the IV
|
||||
byte[] iv = IvEncoder.encodeIv(transportIndex.getInt(), connection);
|
||||
ivCipher.init(Cipher.ENCRYPT_MODE, ivKey);
|
||||
byte[] encryptedIv = ivCipher.doFinal(iv);
|
||||
assertEquals(IV_LENGTH, encryptedIv.length);
|
||||
tagCipher.init(Cipher.ENCRYPT_MODE, tagKey);
|
||||
byte[] tag = tagCipher.doFinal(iv);
|
||||
assertEquals(TAG_LENGTH, tag.length);
|
||||
// Generate two random frames
|
||||
byte[] frame = new byte[12345];
|
||||
random.nextBytes(frame);
|
||||
@@ -75,12 +75,12 @@ public class FrameReadWriteTest extends TestCase {
|
||||
random.nextBytes(frame1);
|
||||
// Copy the keys - the copies will be erased
|
||||
ErasableKey frameCopy = frameKey.copy();
|
||||
ErasableKey ivCopy = ivKey.copy();
|
||||
ErasableKey tagCopy = tagKey.copy();
|
||||
ErasableKey macCopy = macKey.copy();
|
||||
// Write the frames
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ConnectionEncrypter encrypter = new ConnectionEncrypterImpl(out,
|
||||
Long.MAX_VALUE, iv, ivCipher, frameCipher, ivCopy, frameCopy);
|
||||
Long.MAX_VALUE, iv, tagCipher, frameCipher, tagCopy, frameCopy);
|
||||
ConnectionWriter writer = new ConnectionWriterImpl(encrypter, mac,
|
||||
macCopy);
|
||||
OutputStream out1 = writer.getOutputStream();
|
||||
@@ -90,12 +90,12 @@ public class FrameReadWriteTest extends TestCase {
|
||||
out1.flush();
|
||||
// Read the IV back
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
byte[] recoveredEncryptedIv = new byte[IV_LENGTH];
|
||||
assertEquals(IV_LENGTH, in.read(recoveredEncryptedIv));
|
||||
assertArrayEquals(encryptedIv, recoveredEncryptedIv);
|
||||
byte[] recoveredTag = new byte[TAG_LENGTH];
|
||||
assertEquals(TAG_LENGTH, in.read(recoveredTag));
|
||||
assertArrayEquals(tag, recoveredTag);
|
||||
// Decrypt the IV
|
||||
ivCipher.init(Cipher.DECRYPT_MODE, ivKey);
|
||||
byte[] recoveredIv = ivCipher.doFinal(recoveredEncryptedIv);
|
||||
tagCipher.init(Cipher.DECRYPT_MODE, tagKey);
|
||||
byte[] recoveredIv = tagCipher.doFinal(recoveredTag);
|
||||
iv = IvEncoder.encodeIv(transportIndex.getInt(), connection);
|
||||
assertArrayEquals(iv, recoveredIv);
|
||||
// Read the frames back
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.sf.briar.transport.batch;
|
||||
|
||||
import static net.sf.briar.api.transport.TransportConstants.IV_LENGTH;
|
||||
import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -170,11 +170,11 @@ public class BatchConnectionReadWriteTest extends TestCase {
|
||||
// Create a connection recogniser and recognise the connection
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
ConnectionRecogniser rec = bob.getInstance(ConnectionRecogniser.class);
|
||||
byte[] encryptedIv = new byte[IV_LENGTH];
|
||||
int read = in.read(encryptedIv);
|
||||
assertEquals(encryptedIv.length, read);
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
int read = in.read(tag);
|
||||
assertEquals(tag.length, read);
|
||||
TestCallback callback = new TestCallback();
|
||||
rec.acceptConnection(transportId, encryptedIv, callback);
|
||||
rec.acceptConnection(transportId, tag, callback);
|
||||
callback.latch.await();
|
||||
ConnectionContext ctx = callback.ctx;
|
||||
assertNotNull(ctx);
|
||||
@@ -187,7 +187,7 @@ public class BatchConnectionReadWriteTest extends TestCase {
|
||||
bob.getInstance(ProtocolReaderFactory.class);
|
||||
BatchTransportReader reader = new TestBatchTransportReader(in);
|
||||
IncomingBatchConnection batchIn = new IncomingBatchConnection(
|
||||
connFactory, db, protoFactory, ctx, reader, encryptedIv);
|
||||
connFactory, db, protoFactory, ctx, reader, tag);
|
||||
// No messages should have been added yet
|
||||
assertFalse(listener.messagesAdded);
|
||||
// Read whatever needs to be read
|
||||
|
||||
Reference in New Issue
Block a user