mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
Unit test for key derivation.
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
<test name='net.sf.briar.LockFairnessTest'/>
|
<test name='net.sf.briar.LockFairnessTest'/>
|
||||||
<test name='net.sf.briar.ProtocolIntegrationTest'/>
|
<test name='net.sf.briar.ProtocolIntegrationTest'/>
|
||||||
<test name='net.sf.briar.crypto.CounterModeTest'/>
|
<test name='net.sf.briar.crypto.CounterModeTest'/>
|
||||||
|
<test name='net.sf.briar.crypto.KeyDerivationTest'/>
|
||||||
<test name='net.sf.briar.db.BasicH2Test'/>
|
<test name='net.sf.briar.db.BasicH2Test'/>
|
||||||
<test name='net.sf.briar.db.DatabaseCleanerImplTest'/>
|
<test name='net.sf.briar.db.DatabaseCleanerImplTest'/>
|
||||||
<test name='net.sf.briar.db.DatabaseComponentImplTest'/>
|
<test name='net.sf.briar.db.DatabaseComponentImplTest'/>
|
||||||
|
|||||||
74
test/net/sf/briar/crypto/KeyDerivationTest.java
Normal file
74
test/net/sf/briar/crypto/KeyDerivationTest.java
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package net.sf.briar.crypto;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import net.sf.briar.api.crypto.CryptoComponent;
|
||||||
|
import net.sf.briar.api.crypto.ErasableKey;
|
||||||
|
import net.sf.briar.api.protocol.ProtocolConstants;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class KeyDerivationTest extends TestCase {
|
||||||
|
|
||||||
|
private final CryptoComponent crypto;
|
||||||
|
private final byte[] secret;
|
||||||
|
|
||||||
|
public KeyDerivationTest() {
|
||||||
|
super();
|
||||||
|
crypto = new CryptoComponentImpl();
|
||||||
|
secret = new byte[32];
|
||||||
|
new Random().nextBytes(secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSixKeysAreDistinct() {
|
||||||
|
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.deriveMacKey(secret, true));
|
||||||
|
keys.add(crypto.deriveMacKey(secret, false));
|
||||||
|
for(int i = 0; i < 6; i++) {
|
||||||
|
byte[] keyI = keys.get(i).getEncoded();
|
||||||
|
for(int j = 0; j < 6; j++) {
|
||||||
|
byte[] keyJ = keys.get(j).getEncoded();
|
||||||
|
assertEquals(i == j, Arrays.equals(keyI, keyJ));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTransportIndexAffectsDerivation() {
|
||||||
|
List<byte[]> secrets = new ArrayList<byte[]>();
|
||||||
|
for(int i = 0; i < ProtocolConstants.MAX_TRANSPORTS; i++) {
|
||||||
|
secrets.add(crypto.deriveNextSecret(secret, i, 0));
|
||||||
|
}
|
||||||
|
for(int i = 0; i < ProtocolConstants.MAX_TRANSPORTS; i++) {
|
||||||
|
byte[] secretI = secrets.get(i);
|
||||||
|
for(int j = 0; j < ProtocolConstants.MAX_TRANSPORTS; j++) {
|
||||||
|
byte[] secretJ = secrets.get(j);
|
||||||
|
assertEquals(i == j, Arrays.equals(secretI, secretJ));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConnectionNumberAffectsDerivation() {
|
||||||
|
List<byte[]> secrets = new ArrayList<byte[]>();
|
||||||
|
for(int i = 0; i < 20; i++) {
|
||||||
|
secrets.add(crypto.deriveNextSecret(secret, 0, i));
|
||||||
|
}
|
||||||
|
for(int i = 0; i < 20; i++) {
|
||||||
|
byte[] secretI = secrets.get(i);
|
||||||
|
for(int j = 0; j < 20; j++) {
|
||||||
|
byte[] secretJ = secrets.get(j);
|
||||||
|
assertEquals(i == j, Arrays.equals(secretI, secretJ));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,8 +22,8 @@ public class ConnectionWindowImplTest extends TestCase {
|
|||||||
private final byte[] secret;
|
private final byte[] secret;
|
||||||
private final TransportIndex transportIndex = new TransportIndex(13);
|
private final TransportIndex transportIndex = new TransportIndex(13);
|
||||||
|
|
||||||
public ConnectionWindowImplTest(String name) {
|
public ConnectionWindowImplTest() {
|
||||||
super(name);
|
super();
|
||||||
Injector i = Guice.createInjector(new CryptoModule());
|
Injector i = Guice.createInjector(new CryptoModule());
|
||||||
crypto = i.getInstance(CryptoComponent.class);
|
crypto = i.getInstance(CryptoComponent.class);
|
||||||
secret = new byte[32];
|
secret = new byte[32];
|
||||||
|
|||||||
Reference in New Issue
Block a user