public void init( boolean forSigning, CipherParameters param) { if (forSigning) { if (param instanceof ParametersWithRandom) { ParametersWithRandom rParam = (ParametersWithRandom)param; this.random = rParam.getRandom(); this.key = (DSAPrivateKeyParameters)rParam.getParameters(); } else { this.random = new SecureRandom(); this.key = (DSAPrivateKeyParameters)param; } } else { this.key = (DSAPublicKeyParameters)param; } }
public KeyPair generateKeyPair() { if (!initialised) { DSAParametersGenerator pGen = new DSAParametersGenerator(); pGen.init(strength, certainty, random); param = new DSAKeyGenerationParameters(random, pGen.generateParameters()); engine.init(param); initialised = true; } AsymmetricCipherKeyPair pair = engine.generateKeyPair(); DSAPublicKeyParameters pub = (DSAPublicKeyParameters)pair.getPublic(); DSAPrivateKeyParameters priv = (DSAPrivateKeyParameters)pair.getPrivate(); return new KeyPair(new BCDSAPublicKey(pub), new BCDSAPrivateKey(priv)); }
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)); }
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 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 void init( boolean forSigning, CipherParameters param) { SecureRandom providedRandom = null; if (forSigning) { if (param instanceof ParametersWithRandom) { ParametersWithRandom rParam = (ParametersWithRandom)param; this.key = (DSAPrivateKeyParameters)rParam.getParameters(); providedRandom = rParam.getRandom(); } else { this.key = (DSAPrivateKeyParameters)param; } } else { this.key = (DSAPublicKeyParameters)param; } this.random = initSecureRandom(forSigning && !kCalculator.isDeterministic(), providedRandom); }
/** * 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 }; }
/** {@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); }
/** * 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; }
/** * 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); BigInteger k; int qBitLength = params.getQ().bitLength(); do { k = new BigInteger(qBitLength, random); } while (k.compareTo(params.getQ()) >= 0); 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; }
@Test public void generates1024bitKeys() throws Exception { final AsymmetricCipherKeyPair pair = DsaKeyGenerator.dsa1024().generate(random); assertThat(pair.getPrivate()) .isInstanceOf(DSAPrivateKeyParameters.class); final DSAPrivateKeyParameters privateKeys = (DSAPrivateKeyParameters) pair.getPrivate(); assertThat(privateKeys.getParameters().getP().toByteArray().length) .isEqualTo(129); }
@Test public void generates2048bitKeys() throws Exception { final AsymmetricCipherKeyPair pair = DsaKeyGenerator.dsa2048().generate(random); assertThat(pair.getPrivate()) .isInstanceOf(DSAPrivateKeyParameters.class); final DSAPrivateKeyParameters privateKeys = (DSAPrivateKeyParameters) pair.getPrivate(); assertThat(privateKeys.getParameters().getP().toByteArray().length) .isIn(257); }
@Test public void generates3072bitKeys() throws Exception { final AsymmetricCipherKeyPair pair = DsaKeyGenerator.dsa3072().generate(random); assertThat(pair.getPrivate()) .isInstanceOf(DSAPrivateKeyParameters.class); final DSAPrivateKeyParameters privateKeys = (DSAPrivateKeyParameters) pair.getPrivate(); assertThat(privateKeys.getParameters().getP().toByteArray().length) .isIn(385); }
public void enableClientAuthentication(Certificate clientCertificate, AsymmetricKeyParameter clientPrivateKey) { if (clientCertificate == null) { throw new IllegalArgumentException("'clientCertificate' cannot be null"); } if (clientCertificate.certs.length == 0) { throw new IllegalArgumentException("'clientCertificate' cannot be empty"); } if (clientPrivateKey == null) { throw new IllegalArgumentException("'clientPrivateKey' cannot be null"); } if (!clientPrivateKey.isPrivate()) { throw new IllegalArgumentException("'clientPrivateKey' must be private"); } if (clientPrivateKey instanceof RSAKeyParameters) { clientSigner = new TlsRSASigner(); } else if (clientPrivateKey instanceof DSAPrivateKeyParameters) { clientSigner = new TlsDSSSigner(); } else { throw new IllegalArgumentException("'clientPrivateKey' type not supported: " + clientPrivateKey.getClass().getName()); } this.clientCert = clientCertificate; this.clientPrivateKey = clientPrivateKey; }
JDKDSAPrivateKey( DSAPrivateKeyParameters params) { this.x = params.getX(); this.dsaSpec = new DSAParameterSpec(params.getParameters().getP(), params.getParameters().getQ(), params.getParameters().getG()); }