BigInteger p)
throws Exception
{
DHParameterSpec dhParams = new DHParameterSpec(p, g, privateValueSize);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH", "BC");
keyGen.initialize(dhParams);
//
// a side
//
KeyPair aKeyPair = keyGen.generateKeyPair();
KeyAgreement aKeyAgree = KeyAgreement.getInstance("DH", "BC");
checkKeySize(privateValueSize, aKeyPair);
aKeyAgree.init(aKeyPair.getPrivate());
//
// b side
//
KeyPair bKeyPair = keyGen.generateKeyPair();
KeyAgreement bKeyAgree = KeyAgreement.getInstance("DH", "BC");
checkKeySize(privateValueSize, bKeyPair);
bKeyAgree.init(bKeyPair.getPrivate());
//
// agreement
//
aKeyAgree.doPhase(bKeyPair.getPublic(), true);
bKeyAgree.doPhase(aKeyPair.getPublic(), true);
BigInteger k1 = new BigInteger(aKeyAgree.generateSecret());
BigInteger k2 = new BigInteger(bKeyAgree.generateSecret());
if (!k1.equals(k2))
{
fail(size + " bit 2-way test failed");
}
//
// public key encoding test
//
byte[] pubEnc = aKeyPair.getPublic().getEncoded();
KeyFactory keyFac = KeyFactory.getInstance("DH", "BC");
X509EncodedKeySpec pubX509 = new X509EncodedKeySpec(pubEnc);
DHPublicKey pubKey = (DHPublicKey)keyFac.generatePublic(pubX509);
DHParameterSpec spec = pubKey.getParams();
if (!spec.getG().equals(dhParams.getG()) || !spec.getP().equals(dhParams.getP()))
{
fail(size + " bit public key encoding/decoding test failed on parameters");
}
if (!((DHPublicKey)aKeyPair.getPublic()).getY().equals(pubKey.getY()))
{
fail(size + " bit public key encoding/decoding test failed on y value");
}
//
// public key serialisation test
//
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ObjectOutputStream oOut = new ObjectOutputStream(bOut);
oOut.writeObject(aKeyPair.getPublic());
ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray());
ObjectInputStream oIn = new ObjectInputStream(bIn);
pubKey = (DHPublicKey)oIn.readObject();
spec = pubKey.getParams();
if (!spec.getG().equals(dhParams.getG()) || !spec.getP().equals(dhParams.getP()))
{
fail(size + " bit public key serialisation test failed on parameters");
}
if (!((DHPublicKey)aKeyPair.getPublic()).getY().equals(pubKey.getY()))
{
fail(size + " bit public key serialisation test failed on y value");
}
//
// private key encoding test
//
byte[] privEnc = aKeyPair.getPrivate().getEncoded();
PKCS8EncodedKeySpec privPKCS8 = new PKCS8EncodedKeySpec(privEnc);
DHPrivateKey privKey = (DHPrivateKey)keyFac.generatePrivate(privPKCS8);
spec = privKey.getParams();
if (!spec.getG().equals(dhParams.getG()) || !spec.getP().equals(dhParams.getP()))
{
fail(size + " bit private key encoding/decoding test failed on parameters");
}
if (!((DHPrivateKey)aKeyPair.getPrivate()).getX().equals(privKey.getX()))
{
fail(size + " bit private key encoding/decoding test failed on y value");
}
//
// private key serialisation test
//
bOut = new ByteArrayOutputStream();
oOut = new ObjectOutputStream(bOut);
oOut.writeObject(aKeyPair.getPrivate());
bIn = new ByteArrayInputStream(bOut.toByteArray());
oIn = new ObjectInputStream(bIn);
privKey = (DHPrivateKey)oIn.readObject();
spec = privKey.getParams();
if (!spec.getG().equals(dhParams.getG()) || !spec.getP().equals(dhParams.getP()))
{
fail(size + " bit private key serialisation test failed on parameters");
}
if (!((DHPrivateKey)aKeyPair.getPrivate()).getX().equals(privKey.getX()))
{
fail(size + " bit private key serialisation test failed on y value");
}
//
// three party test
//
KeyPairGenerator aPairGen = KeyPairGenerator.getInstance("DH", "BC");
aPairGen.initialize(spec);
KeyPair aPair = aPairGen.generateKeyPair();
KeyPairGenerator bPairGen = KeyPairGenerator.getInstance("DH", "BC");
bPairGen.initialize(spec);
KeyPair bPair = bPairGen.generateKeyPair();
KeyPairGenerator cPairGen = KeyPairGenerator.getInstance("DH", "BC");
cPairGen.initialize(spec);
KeyPair cPair = cPairGen.generateKeyPair();
aKeyAgree.init(aPair.getPrivate());
bKeyAgree.init(bPair.getPrivate());