Rewrote EC key encoding and added fuzzing tests to track down bug #33.

This commit is contained in:
akwizgran
2014-01-05 21:25:56 +00:00
parent 3779f6ea8b
commit bf1a72c826
5 changed files with 90 additions and 24 deletions

View File

@@ -1,6 +1,10 @@
package net.sf.briar.crypto;
import static org.junit.Assert.assertArrayEquals;
import java.security.GeneralSecurityException;
import java.util.Random;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.KeyPair;
import net.sf.briar.api.crypto.KeyParser;
@@ -47,6 +51,35 @@ public class KeyEncodingAndParsingTest extends BriarTestCase {
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();
@@ -80,4 +113,33 @@ public class KeyEncodingAndParsingTest extends BriarTestCase {
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();
}
}
}
}