}
private void testGeneration()
throws Exception
{
Signature s = Signature.getInstance("DSA", "BC");
KeyPairGenerator g = KeyPairGenerator.getInstance("DSA", "BC");
byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
// test exception
//
try
{
g.initialize(513, new SecureRandom());
fail("illegal parameter 513 check failed.");
}
catch (IllegalArgumentException e)
{
// expected
}
try
{
g.initialize(510, new SecureRandom());
fail("illegal parameter 510 check failed.");
}
catch (IllegalArgumentException e)
{
// expected
}
try
{
g.initialize(1025, new SecureRandom());
fail("illegal parameter 1025 check failed.");
}
catch (IllegalArgumentException e)
{
// expected
}
g.initialize(512, new SecureRandom());
KeyPair p = g.generateKeyPair();
PrivateKey sKey = p.getPrivate();
PublicKey vKey = p.getPublic();
s.initSign(sKey);
s.update(data);
byte[] sigBytes = s.sign();
s = Signature.getInstance("DSA", "BC");
s.initVerify(vKey);
s.update(data);
if (!s.verify(sigBytes))
{
fail("DSA verification failed");
}
//
// key decoding test - serialisation test
//
DSAPublicKey k1 = (DSAPublicKey)serializeDeserialize(vKey);
checkPublic(k1, vKey);
checkEquals(k1, vKey);
DSAPrivateKey k2 = (DSAPrivateKey)serializeDeserialize(sKey);
checkPrivateKey(k2, sKey);
checkEquals(k2, sKey);
if (!(k2 instanceof PKCS12BagAttributeCarrier))
{
fail("private key not implementing PKCS12 attribute carrier");
}
//
// ECDSA Fp generation test
//
s = Signature.getInstance("ECDSA", "BC");
g = KeyPairGenerator.getInstance("ECDSA", "BC");
ECCurve curve = new ECCurve.Fp(
new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q
new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a
new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b
ECParameterSpec ecSpec = new ECParameterSpec(
curve,
curve.decodePoint(Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G
new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n
g.initialize(ecSpec, new SecureRandom());
p = g.generateKeyPair();
sKey = p.getPrivate();
vKey = p.getPublic();
s.initSign(sKey);
s.update(data);
sigBytes = s.sign();
s = Signature.getInstance("ECDSA", "BC");
s.initVerify(vKey);
s.update(data);
if (!s.verify(sigBytes))
{
fail("ECDSA verification failed");
}
//
// key decoding test - serialisation test
//
PublicKey eck1 = (PublicKey)serializeDeserialize(vKey);
checkEquals(eck1, vKey);
PrivateKey eck2 = (PrivateKey)serializeDeserialize(sKey);
checkEquals(eck2, sKey);
// Named curve parameter
g.initialize(new ECNamedCurveGenParameterSpec("P-256"), new SecureRandom());
p = g.generateKeyPair();
sKey = p.getPrivate();
vKey = p.getPublic();
s.initSign(sKey);
s.update(data);
sigBytes = s.sign();
s = Signature.getInstance("ECDSA", "BC");
s.initVerify(vKey);
s.update(data);
if (!s.verify(sigBytes))
{
fail("ECDSA verification failed");
}
//
// key decoding test - serialisation test
//
eck1 = (PublicKey)serializeDeserialize(vKey);
checkEquals(eck1, vKey);
eck2 = (PrivateKey)serializeDeserialize(sKey);
checkEquals(eck2, sKey);
//
// ECDSA F2m generation test
//
s = Signature.getInstance("ECDSA", "BC");
g = KeyPairGenerator.getInstance("ECDSA", "BC");
curve = new ECCurve.F2m(
239, // m
36, // k
new BigInteger("32010857077C5431123A46B808906756F543423E8D27877578125778AC76", 16), // a
new BigInteger("790408F2EEDAF392B012EDEFB3392F30F4327C0CA3F31FC383C422AA8C16", 16)); // b
ecSpec = new ECParameterSpec(
curve,
curve.decodePoint(Hex.decode("0457927098FA932E7C0A96D3FD5B706EF7E5F5C156E16B7E7C86038552E91D61D8EE5077C33FECF6F1A16B268DE469C3C7744EA9A971649FC7A9616305")), // G
new BigInteger("220855883097298041197912187592864814557886993776713230936715041207411783"), // n
BigInteger.valueOf(4)); // h
g.initialize(ecSpec, new SecureRandom());
p = g.generateKeyPair();
sKey = p.getPrivate();
vKey = p.getPublic();
s.initSign(sKey);
s.update(data);
sigBytes = s.sign();
s = Signature.getInstance("ECDSA", "BC");
s.initVerify(vKey);
s.update(data);
if (!s.verify(sigBytes))
{
fail("ECDSA verification failed");
}
//