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)); }
/** * 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); }
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()); }
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); }
/** * 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 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); }
JDKDSAPublicKey( DSAPublicKeyParameters params) { this.y = params.getY(); this.dsaSpec = new DSAParameterSpec(params.getParameters().getP(), params.getParameters().getQ(), params.getParameters().getG()); }
public boolean isValidPublicKey(AsymmetricKeyParameter publicKey) { return publicKey instanceof DSAPublicKeyParameters; }
static short getClientCertificateType(Certificate clientCertificate, Certificate serverCertificate) throws IOException { if (clientCertificate.isEmpty()) { return -1; } org.bouncycastle.asn1.x509.Certificate x509Cert = clientCertificate.getCertificateAt(0); SubjectPublicKeyInfo keyInfo = x509Cert.getSubjectPublicKeyInfo(); try { AsymmetricKeyParameter publicKey = PublicKeyFactory.createKey(keyInfo); if (publicKey.isPrivate()) { throw new TlsFatalAlert(AlertDescription.internal_error); } /* * TODO RFC 5246 7.4.6. The certificates MUST be signed using an acceptable hash/ * signature algorithm pair, as described in Section 7.4.4. Note that this relaxes the * constraints on certificate-signing algorithms found in prior versions of TLS. */ /* * RFC 5246 7.4.6. Client Certificate */ /* * RSA public key; the certificate MUST allow the key to be used for signing with the * signature scheme and hash algorithm that will be employed in the certificate verify * message. */ if (publicKey instanceof RSAKeyParameters) { validateKeyUsage(x509Cert, KeyUsage.digitalSignature); return ClientCertificateType.rsa_sign; } /* * DSA public key; the certificate MUST allow the key to be used for signing with the * hash algorithm that will be employed in the certificate verify message. */ if (publicKey instanceof DSAPublicKeyParameters) { validateKeyUsage(x509Cert, KeyUsage.digitalSignature); return ClientCertificateType.dss_sign; } /* * ECDSA-capable public key; the certificate MUST allow the key to be used for signing * with the hash algorithm that will be employed in the certificate verify message; the * public key MUST use a curve and point format supported by the server. */ if (publicKey instanceof ECPublicKeyParameters) { validateKeyUsage(x509Cert, KeyUsage.digitalSignature); // TODO Check the curve and point format return ClientCertificateType.ecdsa_sign; } // TODO Add support for ClientCertificateType.*_fixed_* } catch (Exception e) { } throw new TlsFatalAlert(AlertDescription.unsupported_certificate); }
BCDSAPublicKey( DSAPublicKeyParameters params) { this.y = params.getY(); this.dsaSpec = new DSAParameterSpec(params.getParameters().getP(), params.getParameters().getQ(), params.getParameters().getG()); }