private void generationTest()
throws Exception
{
Signature s = Signature.getInstance("GOST3410", "BC");
KeyPairGenerator g = KeyPairGenerator.getInstance("GOST3410", "BC");
byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
GOST3410ParameterSpec gost3410P = new GOST3410ParameterSpec(CryptoProObjectIdentifiers.gostR3410_94_CryptoPro_A.getId());
g.initialize(gost3410P, 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("GOST3410", "BC");
s.initVerify(vKey);
s.update(data);
if (!s.verify(sigBytes))
{
fail("GOST3410 verification failed");
}
//
// default iniialisation test
//
s = Signature.getInstance("GOST3410", "BC");
g = KeyPairGenerator.getInstance("GOST3410", "BC");
p = g.generateKeyPair();
sKey = p.getPrivate();
vKey = p.getPublic();
s.initSign(sKey);
s.update(data);
sigBytes = s.sign();
s = Signature.getInstance("GOST3410", "BC");
s.initVerify(vKey);
s.update(data);
if (!s.verify(sigBytes))
{
fail("GOST3410 verification failed");
}
//
// encoded test
//
KeyFactory f = KeyFactory.getInstance("GOST3410", "BC");
X509EncodedKeySpec x509s = new X509EncodedKeySpec(vKey.getEncoded());
GOST3410PublicKey k1 = (GOST3410PublicKey)f.generatePublic(x509s);
if (!k1.getY().equals(((GOST3410PublicKey)vKey).getY()))
{
fail("public number not decoded properly");
}
PKCS8EncodedKeySpec pkcs8 = new PKCS8EncodedKeySpec(sKey.getEncoded());
GOST3410PrivateKey k2 = (GOST3410PrivateKey)f.generatePrivate(pkcs8);
if (!k2.getX().equals(((GOST3410PrivateKey)sKey).getX()))
{
fail("private number not decoded properly");
}
//
// ECGOST3410 generation test
//
s = Signature.getInstance("ECGOST3410", "BC");
g = KeyPairGenerator.getInstance("ECGOST3410", "BC");
BigInteger mod_p = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564821041"); //p
ECCurve curve = new ECCurve.Fp(
mod_p, // p
new BigInteger("7"), // a
new BigInteger("43308876546767276905765904595650931995942111794451039583252968842033849580414")); // b
ECParameterSpec ecSpec = new ECParameterSpec(
curve,
new ECPoint.Fp(curve,
new ECFieldElement.Fp(mod_p,new BigInteger("2")), // x
new ECFieldElement.Fp(mod_p,new BigInteger("4018974056539037503335449422937059775635739389905545080690979365213431566280"))), // y
new BigInteger("57896044618658097711785492504343953927082934583725450622380973592137631069619")); // q
g.initialize(ecSpec, new SecureRandom());
p = g.generateKeyPair();
sKey = p.getPrivate();
vKey = p.getPublic();
s.initSign(sKey);