Unit test for key derivation.

This commit is contained in:
akwizgran
2011-11-16 15:54:17 +00:00
parent f6ae4734ce
commit a13a1769e5
3 changed files with 77 additions and 2 deletions

View File

@@ -17,6 +17,7 @@
<test name='net.sf.briar.LockFairnessTest'/>
<test name='net.sf.briar.ProtocolIntegrationTest'/>
<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.DatabaseCleanerImplTest'/>
<test name='net.sf.briar.db.DatabaseComponentImplTest'/>

View 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));
}
}
}
}

View File

@@ -22,8 +22,8 @@ public class ConnectionWindowImplTest extends TestCase {
private final byte[] secret;
private final TransportIndex transportIndex = new TransportIndex(13);
public ConnectionWindowImplTest(String name) {
super(name);
public ConnectionWindowImplTest() {
super();
Injector i = Guice.createInjector(new CryptoModule());
crypto = i.getInstance(CryptoComponent.class);
secret = new byte[32];