mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 13:49:53 +01:00
IntroductionCrypto: Create dedicated class to handle introduction related crypto
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
package org.briarproject.briar.introduction2;
|
||||
|
||||
import org.briarproject.bramble.api.client.ClientHelper;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.KeyPair;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorFactory;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.briar.api.client.SessionId;
|
||||
import org.briarproject.briar.test.BriarIntegrationTestComponent;
|
||||
import org.briarproject.briar.test.DaggerBriarIntegrationTestComponent;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTransportPropertiesMap;
|
||||
import static org.briarproject.bramble.util.StringUtils.fromHexString;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class IntroductionCryptoImplTest extends BrambleTestCase {
|
||||
|
||||
@Inject
|
||||
ClientHelper clientHelper;
|
||||
@Inject
|
||||
AuthorFactory authorFactory;
|
||||
@Inject
|
||||
CryptoComponent cryptoComponent;
|
||||
|
||||
private final IntroductionCryptoImpl crypto;
|
||||
|
||||
private final Author introducer;
|
||||
private final LocalAuthor alice, bob;
|
||||
private final long aliceAcceptTimestamp = 42L;
|
||||
private final long bobAcceptTimestamp = 1337L;
|
||||
private final SecretKey masterKey =
|
||||
new SecretKey(getRandomBytes(SecretKey.LENGTH));
|
||||
private final KeyPair aliceEphemeral, bobEphemeral;
|
||||
private final Map<TransportId, TransportProperties> aliceTransport =
|
||||
getTransportPropertiesMap(3);
|
||||
private final Map<TransportId, TransportProperties> bobTransport =
|
||||
getTransportPropertiesMap(3);
|
||||
|
||||
public IntroductionCryptoImplTest() {
|
||||
BriarIntegrationTestComponent component =
|
||||
DaggerBriarIntegrationTestComponent.builder().build();
|
||||
component.inject(this);
|
||||
crypto = new IntroductionCryptoImpl(cryptoComponent, clientHelper);
|
||||
|
||||
// create actual deterministic authors for testing
|
||||
introducer = authorFactory
|
||||
.createAuthor("Introducer", new byte[] {0x1, 0x2, 0x3});
|
||||
alice = authorFactory.createLocalAuthor("Alice",
|
||||
fromHexString(
|
||||
"A626F080C94771698F86B4B4094C4F560904B53398805AE02BA2343F1829187A"),
|
||||
fromHexString(
|
||||
"60F010187AF91ACA15141E8C811EC8E79C7CAA6461C21A852BB03066C89B0A70"));
|
||||
bob = authorFactory.createLocalAuthor("Bob",
|
||||
fromHexString(
|
||||
"A0D0FED1CE4674D8B6441AD0A664E41BF60D489F35DA11F52AF923540848546F"),
|
||||
fromHexString(
|
||||
"20B25BE7E999F68FE07189449E91984FA79121DBFF28A651669A3CF512D6A758"));
|
||||
aliceEphemeral = crypto.generateKeyPair();
|
||||
bobEphemeral = crypto.generateKeyPair();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSessionId() {
|
||||
SessionId s1 = crypto.getSessionId(introducer, alice, bob);
|
||||
SessionId s2 = crypto.getSessionId(introducer, bob, alice);
|
||||
assertEquals(s1, s2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAlice() {
|
||||
assertTrue(crypto.isAlice(alice.getId(), bob.getId()));
|
||||
assertFalse(crypto.isAlice(bob.getId(), alice.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeriveMasterKey() throws Exception {
|
||||
SecretKey aliceMasterKey = crypto.deriveMasterKey(alice.getPublicKey(),
|
||||
alice.getPrivateKey(), bob.getPublicKey(), true);
|
||||
SecretKey bobMasterKey = crypto.deriveMasterKey(bob.getPublicKey(),
|
||||
bob.getPrivateKey(), alice.getPublicKey(), false);
|
||||
assertArrayEquals(aliceMasterKey.getBytes(), bobMasterKey.getBytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAliceMac() throws Exception {
|
||||
SecretKey aliceMacKey = crypto.deriveMacKey(masterKey, true);
|
||||
byte[] aliceMac =
|
||||
crypto.mac(aliceMacKey, introducer.getId(), alice.getId(),
|
||||
bob.getId(), aliceAcceptTimestamp, bobAcceptTimestamp,
|
||||
aliceEphemeral.getPublic().getEncoded(),
|
||||
bobEphemeral.getPublic().getEncoded(), aliceTransport,
|
||||
bobTransport, true);
|
||||
|
||||
crypto.verifyMac(aliceMac, masterKey, introducer.getId(), bob.getId(),
|
||||
alice.getId(), bobAcceptTimestamp, aliceAcceptTimestamp,
|
||||
bobEphemeral.getPublic().getEncoded(),
|
||||
aliceEphemeral.getPublic().getEncoded(), bobTransport,
|
||||
aliceTransport, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBobMac() throws Exception {
|
||||
SecretKey bobMacKey = crypto.deriveMacKey(masterKey, false);
|
||||
byte[] bobMac =
|
||||
crypto.mac(bobMacKey, introducer.getId(), bob.getId(),
|
||||
alice.getId(), bobAcceptTimestamp, aliceAcceptTimestamp,
|
||||
bobEphemeral.getPublic().getEncoded(),
|
||||
aliceEphemeral.getPublic().getEncoded(), bobTransport,
|
||||
aliceTransport, false);
|
||||
|
||||
crypto.verifyMac(bobMac, masterKey, introducer.getId(), alice.getId(),
|
||||
bob.getId(), aliceAcceptTimestamp, bobAcceptTimestamp,
|
||||
aliceEphemeral.getPublic().getEncoded(),
|
||||
bobEphemeral.getPublic().getEncoded(), aliceTransport,
|
||||
bobTransport, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSign() throws Exception {
|
||||
KeyPair keyPair = cryptoComponent.generateSignatureKeyPair();
|
||||
SecretKey macKey = crypto.deriveMacKey(masterKey, true);
|
||||
byte[] signature =
|
||||
crypto.sign(macKey, keyPair.getPrivate().getEncoded());
|
||||
crypto.verifySignature(macKey, keyPair.getPublic().getEncoded(),
|
||||
signature);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package org.briarproject.briar.introduction2;
|
||||
|
||||
import org.briarproject.bramble.api.UniqueId;
|
||||
import org.briarproject.bramble.api.client.ClientHelper;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.briar.api.client.SessionId;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.briar.api.introduction2.IntroductionConstants.LABEL_SESSION_ID;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class IntroductionCryptoTest extends BrambleMockTestCase {
|
||||
|
||||
private final CryptoComponent cryptoComponent =
|
||||
context.mock(CryptoComponent.class);
|
||||
private final ClientHelper clientHelper = context.mock(ClientHelper.class);
|
||||
|
||||
private final IntroductionCrypto crypto =
|
||||
new IntroductionCryptoImpl(cryptoComponent, clientHelper);
|
||||
|
||||
private final Author introducer = getAuthor();
|
||||
private final Author alice = getAuthor(), bob = getAuthor();
|
||||
private final byte[] hash = getRandomBytes(UniqueId.LENGTH);
|
||||
|
||||
@Test
|
||||
public void testGetSessionId() {
|
||||
boolean isAlice = crypto.isAlice(alice.getId(), bob.getId());
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(cryptoComponent).hash(
|
||||
LABEL_SESSION_ID,
|
||||
introducer.getId().getBytes(),
|
||||
isAlice ? alice.getId().getBytes() : bob.getId().getBytes(),
|
||||
isAlice ? bob.getId().getBytes() : alice.getId().getBytes()
|
||||
);
|
||||
will(returnValue(hash));
|
||||
}});
|
||||
SessionId sessionId = crypto.getSessionId(introducer, alice, bob);
|
||||
assertEquals(new SessionId(hash), sessionId);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -37,6 +37,7 @@ import org.briarproject.briar.blog.BlogModule;
|
||||
import org.briarproject.briar.client.BriarClientModule;
|
||||
import org.briarproject.briar.forum.ForumModule;
|
||||
import org.briarproject.briar.introduction.IntroductionModule;
|
||||
import org.briarproject.briar.introduction2.IntroductionCryptoImplTest;
|
||||
import org.briarproject.briar.introduction2.MessageEncoderParserIntegrationTest;
|
||||
import org.briarproject.briar.introduction2.SessionEncoderParserIntegrationTest;
|
||||
import org.briarproject.briar.messaging.MessagingModule;
|
||||
@@ -80,6 +81,7 @@ public interface BriarIntegrationTestComponent {
|
||||
|
||||
void inject(MessageEncoderParserIntegrationTest init);
|
||||
void inject(SessionEncoderParserIntegrationTest init);
|
||||
void inject(IntroductionCryptoImplTest init);
|
||||
|
||||
void inject(BlogModule.EagerSingletons init);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user