private DSAParameters generateParameters_FIPS186_2() { byte[] seed = new byte[20]; byte[] part1 = new byte[20]; byte[] part2 = new byte[20]; byte[] u = new byte[20]; int n = (L - 1) / 160; byte[] w = new byte[L / 8]; if (!(digest instanceof SHA1Digest)) { throw new IllegalStateException("can only use SHA-1 for generating FIPS 186-2 parameters"); } for (;;) { random.nextBytes(seed); hash(digest, seed, part1); System.arraycopy(seed, 0, part2, 0, seed.length); inc(part2); hash(digest, part2, part2); for (int i = 0; i != u.length; i++) { u[i] = (byte)(part1[i] ^ part2[i]); } u[0] |= (byte)0x80; u[19] |= (byte)0x01; BigInteger q = new BigInteger(1, u); if (!q.isProbablePrime(certainty)) { continue; } byte[] offset = Arrays.clone(seed); inc(offset); for (int counter = 0; counter < 4096; ++counter) { for (int k = 0; k < n; k++) { inc(offset); hash(digest, offset, part1); System.arraycopy(part1, 0, w, w.length - (k + 1) * part1.length, part1.length); } inc(offset); hash(digest, offset, part1); System.arraycopy(part1, part1.length - ((w.length - (n) * part1.length)), w, 0, w.length - n * part1.length); w[0] |= (byte)0x80; BigInteger x = new BigInteger(1, w); BigInteger c = x.mod(q.shiftLeft(1)); BigInteger p = x.subtract(c.subtract(ONE)); if (p.bitLength() != L) { continue; } if (p.isProbablePrime(certainty)) { BigInteger g = calculateGenerator_FIPS186_2(p, q, random); return new DSAParameters(p, q, g, new DSAValidationParameters(seed, counter)); } } } }
private DSAParameters generateParameters_FIPS186_2() { byte[] seed = new byte[20]; byte[] part1 = new byte[20]; byte[] part2 = new byte[20]; byte[] u = new byte[20]; int n = (L - 1) / 160; byte[] w = new byte[L / 8]; if (!(digest instanceof SHA1Digest)) { throw new IllegalStateException("can only use SHA-1 for generating FIPS 186-2 parameters"); } for (;;) { random.nextBytes(seed); hash(digest, seed, part1, 0); System.arraycopy(seed, 0, part2, 0, seed.length); inc(part2); hash(digest, part2, part2, 0); for (int i = 0; i != u.length; i++) { u[i] = (byte)(part1[i] ^ part2[i]); } u[0] |= (byte)0x80; u[19] |= (byte)0x01; BigInteger q = new BigInteger(1, u); if (!isProbablePrime(q)) { continue; } byte[] offset = Arrays.clone(seed); inc(offset); for (int counter = 0; counter < 4096; ++counter) { { for (int k = 1; k <= n; k++) { inc(offset); hash(digest, offset, w, w.length - k * part1.length); } int remaining = w.length - (n * part1.length); inc(offset); hash(digest, offset, part1, 0); System.arraycopy(part1, part1.length - remaining, w, 0, remaining); w[0] |= (byte)0x80; } BigInteger x = new BigInteger(1, w); BigInteger c = x.mod(q.shiftLeft(1)); BigInteger p = x.subtract(c.subtract(ONE)); if (p.bitLength() != L) { continue; } if (isProbablePrime(p)) { BigInteger g = calculateGenerator_FIPS186_2(p, q, random); return new DSAParameters(p, q, g, new DSAValidationParameters(seed, counter)); } } } }
public void performTest() { BigInteger r = new BigInteger("68076202252361894315274692543577577550894681403"); BigInteger s = new BigInteger("1089214853334067536215539335472893651470583479365"); DSAParametersGenerator pGen = new DSAParametersGenerator(); pGen.init(512, 80, random); DSAParameters params = pGen.generateParameters(); DSAValidationParameters pValid = params.getValidationParameters(); if (pValid.getCounter() != 105) { fail("Counter wrong"); } if (!pValue.equals(params.getP()) || !qValue.equals(params.getQ())) { fail("p or q wrong"); } DSAKeyPairGenerator dsaKeyGen = new DSAKeyPairGenerator(); DSAKeyGenerationParameters genParam = new DSAKeyGenerationParameters(keyRandom, params); dsaKeyGen.init(genParam); AsymmetricCipherKeyPair pair = dsaKeyGen.generateKeyPair(); ParametersWithRandom param = new ParametersWithRandom(pair.getPrivate(), keyRandom); DSASigner dsa = new DSASigner(); dsa.init(true, param); byte[] message = BigIntegers.asUnsignedByteArray(new BigInteger("968236873715988614170569073515315707566766479517")); BigInteger[] sig = dsa.generateSignature(message); if (!r.equals(sig[0])) { fail("r component wrong.", r, sig[0]); } if (!s.equals(sig[1])) { fail("s component wrong.", s, sig[1]); } dsa.init(false, pair.getPublic()); if (!dsa.verifySignature(message, sig[0], sig[1])) { fail("verification fails"); } dsa2Test1(); dsa2Test2(); dsa2Test3(); dsa2Test4(); }
public void performTest() { BigInteger r = new BigInteger("68076202252361894315274692543577577550894681403"); BigInteger s = new BigInteger("1089214853334067536215539335472893651470583479365"); DSAParametersGenerator pGen = new DSAParametersGenerator(); pGen.init(512, 20, random); DSAParameters params = pGen.generateParameters(); DSAValidationParameters pValid = params.getValidationParameters(); if (pValid.getCounter() != 105) { fail("Counter wrong"); } if (!pValue.equals(params.getP()) || !qValue.equals(params.getQ())) { fail("p or q wrong"); } DSAKeyPairGenerator dsaKeyGen = new DSAKeyPairGenerator(); DSAKeyGenerationParameters genParam = new DSAKeyGenerationParameters(keyRandom, params); dsaKeyGen.init(genParam); AsymmetricCipherKeyPair pair = dsaKeyGen.generateKeyPair(); ParametersWithRandom param = new ParametersWithRandom(pair.getPrivate(), keyRandom); DSASigner dsa = new DSASigner(); dsa.init(true, param); byte[] message = BigIntegers.asUnsignedByteArray(new BigInteger("968236873715988614170569073515315707566766479517")); BigInteger[] sig = dsa.generateSignature(message); if (!r.equals(sig[0])) { fail("r component wrong.", r, sig[0]); } if (!s.equals(sig[1])) { fail("s component wrong.", s, sig[1]); } dsa.init(false, pair.getPublic()); if (!dsa.verifySignature(message, sig[0], sig[1])) { fail("verification fails"); } //dsa2Test1(); //dsa2Test2(); //dsa2Test3(); //dsa2Test4(); }