public void initialize( AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException { if (!(params instanceof DSAParameterSpec)) { throw new InvalidAlgorithmParameterException("parameter object not a DSAParameterSpec"); } DSAParameterSpec dsaParams = (DSAParameterSpec)params; param = new DSAKeyGenerationParameters(random, new DSAParameters(dsaParams.getP(), dsaParams.getQ(), dsaParams.getG())); engine.init(param); initialised = true; }
public AsymmetricCipherKeyPair generateKeyPair() { BigInteger p, q, g, x, y; DSAParameters dsaParams = param.getParameters(); SecureRandom random = param.getRandom(); q = dsaParams.getQ(); p = dsaParams.getP(); g = dsaParams.getG(); do { x = new BigInteger(160, random); } while (x.equals(ZERO) || x.compareTo(q) >= 0); // // calculate the public key. // y = g.modPow(x, p); return new AsymmetricCipherKeyPair( new DSAPublicKeyParameters(y, dsaParams), new DSAPrivateKeyParameters(x, dsaParams)); }
@Override public AsymmetricCipherKeyPairGenerator createAsymmetricCipherKeyPairGenerator(final boolean initWithDefaults) { final DSAKeyPairGenerator generator = new DSAKeyPairGenerator(); if (initWithDefaults) { /* * How certain do we want to be that the chosen primes are really primes. * <p> * The higher this number, the more tests are done to make sure they are primes (and not composites). * <p> * See: <a href="http://crypto.stackexchange.com/questions/3114/what-is-the-correct-value-for-certainty-in-rsa-key-pair-generation">What is the correct value for “certainty” in RSA key pair generation?</a> * and * <a href="http://crypto.stackexchange.com/questions/3126/does-a-high-exponent-compensate-for-a-low-degree-of-certainty?lq=1">Does a high exponent compensate for a low degree of certainty?</a> */ final int certainty = 12; final SecureRandom random = new SecureRandom(); final DSAParametersGenerator pGen = new DSAParametersGenerator(); pGen.init(4096, certainty, random); final DSAParameters dsaParameters = pGen.generateParameters(); final DSAKeyGenerationParameters dsaKeyGenerationParameters = new DSAKeyGenerationParameters(random, dsaParameters); generator.init(dsaKeyGenerationParameters); } return generator; }
private DSAKeyGenerationParameters createDsaKeyGenerationParameters(final CreatePgpKeyParam createPgpKeyParam) { /* * How certain do we want to be that the chosen primes are really primes. * <p> * The higher this number, the more tests are done to make sure they are primes (and not composites). * <p> * See: <a href="http://crypto.stackexchange.com/questions/3114/what-is-the-correct-value-for-certainty-in-rsa-key-pair-generation">What is the correct value for “certainty” in RSA key pair generation?</a> * and * <a href="http://crypto.stackexchange.com/questions/3126/does-a-high-exponent-compensate-for-a-low-degree-of-certainty?lq=1">Does a high exponent compensate for a low degree of certainty?</a> */ final int certainty = 12; final SecureRandom random = getSecureRandom(); final DSAParametersGenerator pGen = new DSAParametersGenerator(); pGen.init(createPgpKeyParam.getStrength(), certainty, random); final DSAParameters dsaParameters = pGen.generateParameters(); return new DSAKeyGenerationParameters(random, dsaParameters); }
/** * return true if the value r and s represent a DSA signature for * the passed in message for standard DSA the message should be a * SHA-1 hash of the real message to be verified. */ public boolean verifySignature( byte[] message, BigInteger r, BigInteger s) { DSAParameters params = key.getParameters(); BigInteger m = calculateE(params.getQ(), message); BigInteger zero = BigInteger.valueOf(0); if (zero.compareTo(r) >= 0 || params.getQ().compareTo(r) <= 0) { return false; } if (zero.compareTo(s) >= 0 || params.getQ().compareTo(s) <= 0) { return false; } BigInteger w = s.modInverse(params.getQ()); BigInteger u1 = m.multiply(w).mod(params.getQ()); BigInteger u2 = r.multiply(w).mod(params.getQ()); u1 = params.getG().modPow(u1, params.getP()); u2 = ((DSAPublicKeyParameters)key).getY().modPow(u2, params.getP()); BigInteger v = u1.multiply(u2).mod(params.getP()).mod(params.getQ()); return v.equals(r); }
/** * which generates the p and g values from the given parameters, * returning the DSAParameters object. * <p> * Note: can take a while... */ public DSAParameters generateParameters() { return (use186_3) ? generateParameters_FIPS186_3() : generateParameters_FIPS186_2(); }
public AsymmetricCipherKeyPair generateKeyPair() { DSAParameters dsaParams = param.getParameters(); BigInteger x = generatePrivateKey(dsaParams.getQ(), param.getRandom()); BigInteger y = calculatePublicKey(dsaParams.getP(), dsaParams.getG(), x); return new AsymmetricCipherKeyPair( new DSAPublicKeyParameters(y, dsaParams), new DSAPrivateKeyParameters(x, dsaParams)); }
static public AsymmetricKeyParameter generatePublicKeyParameter( PublicKey key) throws InvalidKeyException { if (key instanceof DSAPublicKey) { DSAPublicKey k = (DSAPublicKey)key; return new DSAPublicKeyParameters(k.getY(), new DSAParameters(k.getParams().getP(), k.getParams().getQ(), k.getParams().getG())); } throw new InvalidKeyException("can't identify DSA public key: " + key.getClass().getName()); }
static public AsymmetricKeyParameter generatePrivateKeyParameter( PrivateKey key) throws InvalidKeyException { if (key instanceof DSAPrivateKey) { DSAPrivateKey k = (DSAPrivateKey)key; return new DSAPrivateKeyParameters(k.getX(), new DSAParameters(k.getParams().getP(), k.getParams().getQ(), k.getParams().getG())); } throw new InvalidKeyException("can't identify DSA private key."); }
public static KeyPair generateDSAKeypair(DSAParameters dsaParams, SecureRandom random) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { DSAParameterSpec dsaParamSpec = new DSAParameterSpec(dsaParams.getP(), dsaParams.getQ(), dsaParams.getG()); KeyPairGenerator kpGen = getKeyPairGenerator("DSA"); synchronized (kpGen) { kpGen.initialize(dsaParamSpec, random); return kpGen.generateKeyPair(); } }
public static DSAParameterSpec getNewDSAParameterSpec(int plength, int qlength, SecureRandom random) { final int certainty = 80; SecureRandom tmpRandom = (random == null) ? new SecureRandom() : random; DSAParametersGenerator paramGen = new DSAParametersGenerator(new SHA512Digest()); DSAParameterGenerationParameters genParams = new DSAParameterGenerationParameters( plength, qlength, certainty, tmpRandom); paramGen.init(genParams); DSAParameters dsaParams = paramGen.generateParameters(); return new DSAParameterSpec(dsaParams.getP(), dsaParams.getQ(), dsaParams.getG()); }
@Override // CHECKSTYLE:OFF protected P11Identity generateDSAKeypair0(BigInteger p, BigInteger q, BigInteger g, String label, P11NewKeyControl control) throws P11TokenException { // CHECKSTYLE:ON DSAParameters dsaParams = new DSAParameters(p, q, g); KeyPair keypair; try { keypair = KeyUtil.generateDSAKeypair(dsaParams, random); } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException ex) { throw new P11TokenException(ex.getMessage(), ex); } return saveP11Entity(keypair, label); }
protected AlgorithmParameters engineGenerateParameters() { DSAParametersGenerator pGen = new DSAParametersGenerator(); if ( random != null ) { pGen.init(strength, 20, random); } else { pGen.init(strength, 20, new SecureRandom()); } DSAParameters p = pGen.generateParameters(); AlgorithmParameters params; try { params = AlgorithmParameters.getInstance("DSA", BouncyCastleProvider.PROVIDER_NAME); params.init(new DSAParameterSpec(p.getP(), p.getQ(), p.getG())); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return params; }
/** * generate a signature for the given message using the key we were * initialised with. For conventional DSA the message should be a SHA-1 * hash of the message of interest. * * @param message the message that will be verified later. */ public BigInteger[] generateSignature( byte[] message) { DSAParameters params = key.getParameters(); BigInteger q = params.getQ(); BigInteger m = calculateE(q, message); BigInteger x = ((DSAPrivateKeyParameters)key).getX(); if (kCalculator.isDeterministic()) { kCalculator.init(q, x, message); } else { kCalculator.init(q, random); } BigInteger k = kCalculator.nextK(); BigInteger r = params.getG().modPow(k, params.getP()).mod(q); k = k.modInverse(q).multiply(m.add(x.multiply(r))); BigInteger s = k.mod(q); return new BigInteger[]{ r, s }; }
/** * return true if the value r and s represent a DSA signature for * the passed in message for standard DSA the message should be a * SHA-1 hash of the real message to be verified. */ public boolean verifySignature( byte[] message, BigInteger r, BigInteger s) { DSAParameters params = key.getParameters(); BigInteger q = params.getQ(); BigInteger m = calculateE(q, message); BigInteger zero = BigInteger.valueOf(0); if (zero.compareTo(r) >= 0 || q.compareTo(r) <= 0) { return false; } if (zero.compareTo(s) >= 0 || q.compareTo(s) <= 0) { return false; } BigInteger w = s.modInverse(q); BigInteger u1 = m.multiply(w).mod(q); BigInteger u2 = r.multiply(w).mod(q); BigInteger p = params.getP(); u1 = params.getG().modPow(u1, p); u2 = ((DSAPublicKeyParameters)key).getY().modPow(u2, p); BigInteger v = u1.multiply(u2).mod(p).mod(q); return v.equals(r); }
/** {@inheritDoc} */ public void initSign() { if (signKey == null) { throw new IllegalStateException( "Sign key must be set prior to initialization."); } final DSAPrivateKey privKey = (DSAPrivateKey) signKey; final DSAParams params = privKey.getParams(); final DSAPrivateKeyParameters bcParams = new DSAPrivateKeyParameters( privKey.getX(), new DSAParameters(params.getP(), params.getQ(), params.getG())); init(true, bcParams); }
/** {@inheritDoc} */ public void initVerify() { if (verifyKey == null) { throw new IllegalStateException( "Verify key must be set prior to initialization."); } final DSAPublicKey pubKey = (DSAPublicKey) verifyKey; final DSAParams params = pubKey.getParams(); final DSAPublicKeyParameters bcParams = new DSAPublicKeyParameters( pubKey.getY(), new DSAParameters(params.getP(), params.getQ(), params.getG())); init(false, bcParams); }
/** * generate a signature for the given message using the key we were * initialised with. For conventional DSA the message should be a SHA-1 * hash of the message of interest. * * @param message the message that will be verified later. */ public BigInteger[] generateSignature( byte[] message) { DSAParameters params = key.getParameters(); BigInteger m = calculateE(params.getQ(), message); if (kCalculator.isDeterministic()) { kCalculator.init(params.getQ(), ((DSAPrivateKeyParameters)key).getX(), message); } else { kCalculator.init(params.getQ(), random); } BigInteger k = kCalculator.nextK(); BigInteger r = params.getG().modPow(k, params.getP()).mod(params.getQ()); k = k.modInverse(params.getQ()).multiply( m.add(((DSAPrivateKeyParameters)key).getX().multiply(r))); BigInteger s = k.mod(params.getQ()); BigInteger[] res = new BigInteger[2]; res[0] = r; res[1] = s; return res; }