Changed the root package from net.sf.briar to org.briarproject.

This commit is contained in:
akwizgran
2014-01-08 16:18:30 +00:00
parent dce70f487c
commit 832476412c
427 changed files with 2507 additions and 2507 deletions

View File

@@ -0,0 +1,23 @@
package org.briarproject.crypto;
import static org.junit.Assert.assertArrayEquals;
import org.briarproject.BriarTestCase;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.KeyPair;
import org.junit.Test;
public class KeyAgreementTest extends BriarTestCase {
@Test
public void testKeyAgreement() throws Exception {
CryptoComponent crypto = new CryptoComponentImpl();
KeyPair aPair = crypto.generateAgreementKeyPair();
byte[] aPub = aPair.getPublic().getEncoded();
KeyPair bPair = crypto.generateAgreementKeyPair();
byte[] bPub = bPair.getPublic().getEncoded();
byte[] aSecret = crypto.deriveMasterSecret(aPub, bPair, true);
byte[] bSecret = crypto.deriveMasterSecret(bPub, aPair, false);
assertArrayEquals(aSecret, bSecret);
}
}

View File

@@ -0,0 +1,75 @@
package org.briarproject.crypto;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import org.briarproject.BriarTestCase;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.SecretKey;
import org.junit.Test;
public class KeyDerivationTest extends BriarTestCase {
private final CryptoComponent crypto;
private final byte[] secret;
public KeyDerivationTest() {
crypto = new CryptoComponentImpl();
secret = new byte[32];
new Random().nextBytes(secret);
}
@Test
public void testKeysAreDistinct() {
List<SecretKey> keys = new ArrayList<SecretKey>();
keys.add(crypto.deriveFrameKey(secret, 0, false, false));
keys.add(crypto.deriveFrameKey(secret, 0, false, true));
keys.add(crypto.deriveFrameKey(secret, 0, true, false));
keys.add(crypto.deriveFrameKey(secret, 0, true, true));
keys.add(crypto.deriveTagKey(secret, true));
keys.add(crypto.deriveTagKey(secret, false));
for(int i = 0; i < 4; i++) {
byte[] keyI = keys.get(i).getEncoded();
for(int j = 0; j < 4; j++) {
byte[] keyJ = keys.get(j).getEncoded();
assertEquals(i == j, Arrays.equals(keyI, keyJ));
}
}
}
@Test
public void testSecretAffectsDerivation() {
Random r = new Random();
List<byte[]> secrets = new ArrayList<byte[]>();
for(int i = 0; i < 20; i++) {
byte[] b = new byte[32];
r.nextBytes(b);
secrets.add(crypto.deriveNextSecret(b, 0));
}
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));
}
}
}
@Test
public void testConnectionNumberAffectsDerivation() {
List<byte[]> secrets = new ArrayList<byte[]>();
for(int i = 0; i < 20; i++) {
secrets.add(crypto.deriveNextSecret(secret.clone(), 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

@@ -0,0 +1,145 @@
package org.briarproject.crypto;
import static org.junit.Assert.assertArrayEquals;
import java.security.GeneralSecurityException;
import java.util.Random;
import org.briarproject.BriarTestCase;
import org.briarproject.api.crypto.KeyPair;
import org.briarproject.api.crypto.KeyParser;
import org.briarproject.api.crypto.PrivateKey;
import org.briarproject.api.crypto.PublicKey;
import org.junit.Test;
public class KeyEncodingAndParsingTest extends BriarTestCase {
private final CryptoComponentImpl crypto = new CryptoComponentImpl();
@Test
public void testAgreementPublicKeyEncodingAndParsing() throws Exception {
KeyParser parser = crypto.getAgreementKeyParser();
// Generate two key pairs
KeyPair aPair = crypto.generateAgreementKeyPair();
KeyPair bPair = crypto.generateAgreementKeyPair();
// Derive the shared secret
PublicKey aPub = aPair.getPublic();
byte[] secret = crypto.deriveSharedSecret(bPair.getPrivate(), aPub);
// Encode and parse the public key - no exceptions should be thrown
aPub = parser.parsePublicKey(aPub.getEncoded());
aPub = parser.parsePublicKey(aPub.getEncoded());
// Derive the shared secret again - it should be the same
byte[] secret1 = crypto.deriveSharedSecret(bPair.getPrivate(), aPub);
assertArrayEquals(secret, secret1);
}
@Test
public void testAgreementPrivateKeyEncodingAndParsing() throws Exception {
KeyParser parser = crypto.getAgreementKeyParser();
// Generate two key pairs
KeyPair aPair = crypto.generateAgreementKeyPair();
KeyPair bPair = crypto.generateAgreementKeyPair();
// Derive the shared secret
PrivateKey bPriv = bPair.getPrivate();
byte[] secret = crypto.deriveSharedSecret(bPriv, aPair.getPublic());
// Encode and parse the private key - no exceptions should be thrown
bPriv = parser.parsePrivateKey(bPriv.getEncoded());
bPriv = parser.parsePrivateKey(bPriv.getEncoded());
// Derive the shared secret again - it should be the same
byte[] secret1 = crypto.deriveSharedSecret(bPriv, aPair.getPublic());
assertArrayEquals(secret, secret1);
}
@Test
public void testAgreementKeyParserByFuzzing() throws Exception {
KeyParser parser = crypto.getAgreementKeyParser();
// Generate a key pair to get the proper public key length
KeyPair p = crypto.generateAgreementKeyPair();
int pubLength = p.getPublic().getEncoded().length;
int privLength = p.getPrivate().getEncoded().length;
// Parse some random byte arrays - expect GeneralSecurityException
Random random = new Random();
byte[] pubFuzz = new byte[pubLength];
byte[] privFuzz = new byte[privLength];
for(int i = 0; i < 1000; i++) {
random.nextBytes(pubFuzz);
try {
parser.parsePublicKey(pubFuzz);
} catch(GeneralSecurityException expected) {
} catch(Exception e) {
fail();
}
random.nextBytes(privFuzz);
try {
parser.parsePrivateKey(privFuzz);
} catch(GeneralSecurityException expected) {
} catch(Exception e) {
fail();
}
}
}
@Test
public void testSignaturePublicKeyEncodingAndParsing() throws Exception {
KeyParser parser = crypto.getSignatureKeyParser();
// Generate two key pairs
KeyPair aPair = crypto.generateSignatureKeyPair();
KeyPair bPair = crypto.generateSignatureKeyPair();
// Derive the shared secret
PublicKey aPub = aPair.getPublic();
byte[] secret = crypto.deriveSharedSecret(bPair.getPrivate(), aPub);
// Encode and parse the public key - no exceptions should be thrown
aPub = parser.parsePublicKey(aPub.getEncoded());
aPub = parser.parsePublicKey(aPub.getEncoded());
// Derive the shared secret again - it should be the same
byte[] secret1 = crypto.deriveSharedSecret(bPair.getPrivate(), aPub);
assertArrayEquals(secret, secret1);
}
@Test
public void testSignaturePrivateKeyEncodingAndParsing() throws Exception {
KeyParser parser = crypto.getSignatureKeyParser();
// Generate two key pairs
KeyPair aPair = crypto.generateSignatureKeyPair();
KeyPair bPair = crypto.generateSignatureKeyPair();
// Derive the shared secret
PrivateKey bPriv = bPair.getPrivate();
byte[] secret = crypto.deriveSharedSecret(bPriv, aPair.getPublic());
// Encode and parse the private key - no exceptions should be thrown
bPriv = parser.parsePrivateKey(bPriv.getEncoded());
bPriv = parser.parsePrivateKey(bPriv.getEncoded());
// Derive the shared secret again - it should be the same
byte[] secret1 = crypto.deriveSharedSecret(bPriv, aPair.getPublic());
assertArrayEquals(secret, secret1);
}
@Test
public void testSignatureKeyParserByFuzzing() throws Exception {
KeyParser parser = crypto.getSignatureKeyParser();
// Generate a key pair to get the proper public key length
KeyPair p = crypto.generateSignatureKeyPair();
int pubLength = p.getPublic().getEncoded().length;
int privLength = p.getPrivate().getEncoded().length;
// Parse some random byte arrays - expect GeneralSecurityException
Random random = new Random();
byte[] pubFuzz = new byte[pubLength];
byte[] privFuzz = new byte[privLength];
for(int i = 0; i < 1000; i++) {
random.nextBytes(pubFuzz);
try {
parser.parsePublicKey(pubFuzz);
} catch(GeneralSecurityException expected) {
} catch(Exception e) {
fail();
}
random.nextBytes(privFuzz);
try {
parser.parsePrivateKey(privFuzz);
} catch(GeneralSecurityException expected) {
} catch(Exception e) {
fail();
}
}
}
}

View File

@@ -0,0 +1,55 @@
package org.briarproject.crypto;
import static org.junit.Assert.assertArrayEquals;
import java.util.Random;
import org.briarproject.BriarTestCase;
import org.briarproject.api.crypto.CryptoComponent;
import org.junit.Test;
public class PasswordBasedKdfTest extends BriarTestCase {
@Test
public void testEncryptionAndDecryption() {
CryptoComponent crypto = new CryptoComponentImpl();
Random random = new Random();
byte[] input = new byte[1234];
random.nextBytes(input);
char[] password = "password".toCharArray();
byte[] ciphertext = crypto.encryptWithPassword(input, password);
byte[] output = crypto.decryptWithPassword(ciphertext, password);
assertArrayEquals(input, output);
}
@Test
public void testInvalidCiphertextReturnsNull() {
CryptoComponent crypto = new CryptoComponentImpl();
Random random = new Random();
byte[] input = new byte[1234];
random.nextBytes(input);
char[] password = "password".toCharArray();
byte[] ciphertext = crypto.encryptWithPassword(input, password);
// Modify the ciphertext
int position = random.nextInt(ciphertext.length);
int value = random.nextInt(256);
ciphertext[position] = (byte) value;
byte[] output = crypto.decryptWithPassword(ciphertext, password);
assertNull(output);
}
@Test
public void testCalibration() {
CryptoComponentImpl crypto = new CryptoComponentImpl();
// If the target time is unachievable, one iteration should be used
int iterations = crypto.chooseIterationCount(0);
assertEquals(1, iterations);
// If the target time is long, more than one iteration should be used
iterations = crypto.chooseIterationCount(10 * 1000);
assertTrue(iterations > 1);
// If the target time is very long, max iterations should be used
iterations = crypto.chooseIterationCount(Integer.MAX_VALUE);
assertEquals(Integer.MAX_VALUE, iterations);
}
}

View File

@@ -0,0 +1,28 @@
package org.briarproject.crypto;
import static org.junit.Assert.assertArrayEquals;
import java.util.Random;
import org.briarproject.BriarTestCase;
import org.briarproject.api.crypto.SecretKey;
import org.junit.Test;
public class SecretKeyImplTest extends BriarTestCase {
private static final int KEY_BYTES = 32; // 256 bits
@Test
public void testCopiesAreErased() {
byte[] master = new byte[KEY_BYTES];
new Random().nextBytes(master);
SecretKey k = new SecretKeyImpl(master);
byte[] copy = k.getEncoded();
assertArrayEquals(master, copy);
k.erase();
byte[] blank = new byte[KEY_BYTES];
assertArrayEquals(blank, master);
assertArrayEquals(blank, copy);
}
}