int privateValueSize,
BigInteger g,
BigInteger p)
throws Exception
{
DHParameterSpec dhParams = new DHParameterSpec(p, g, privateValueSize);
String algName = "DH";
int size = p.bitLength();
new BouncyCastleProvider().setParameter(ConfigurableProvider.DH_DEFAULT_PARAMS, dhParams);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algName, "BC");
keyGen.initialize(dhParams.getP().bitLength());
testTwoParty("DH", size, privateValueSize, keyGen);
KeyPair aKeyPair = keyGen.generateKeyPair();
new BouncyCastleProvider().setParameter(ConfigurableProvider.DH_DEFAULT_PARAMS, null);
//
// public key encoding test
//
byte[] pubEnc = aKeyPair.getPublic().getEncoded();
KeyFactory keyFac = KeyFactory.getInstance(algName, "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()))