params = dsaKey.getParams(); p = params.getP(); q = params.getQ(); g = params.getG(); x = ((DSAPrivateKey) dsaKey).getX(); digestBI = new BigInteger(1, msgDigest.digest()); k = new BigInteger(1, randomBytes); if (k.compareTo(q) != -1) { continue;
DSAParams params = privateKey.getParams(); privateKey.getX(), params.getP(), params.getQ(), params.getG())); } catch (InvalidKeySpecException e) { throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e); DSAParams params = publicKey.getParams(); return engineGeneratePublic(new DSAPublicKeySpec(publicKey .getY(), params.getP(), params.getQ(), params .getG())); } catch (InvalidKeySpecException e) { throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e); throw new InvalidKeyException("'key' is neither DSAPublicKey nor DSAPrivateKey");
throw new InvalidKeyException("publicKey is not an instance of DSAPublicKey"); DSAParams params = ((DSAPublicKey) publicKey).getParams(); p = params.getP(); q = params.getQ(); y = ((DSAPublicKey) publicKey).getY(); n1 = p.bitLength(); if (p.compareTo(BigInteger.valueOf(1)) != 1 || n1 < 512 || n1 > 1024 || (n1 & 077) != 0) { throw new InvalidKeyException("bad p"); if (q.signum() != 1 || q.bitLength() != 160) { throw new InvalidKeyException("bad q"); if (y.signum() != 1) {
throw new InvalidKeyException(); params = ((DSAPrivateKey) privateKey).getParams(); p = params.getP(); q = params.getQ(); x = ((DSAPrivateKey) privateKey).getX(); n = p.bitLength(); if (p.compareTo(BigInteger.valueOf(1)) != 1 || n < 512 || n > 1024 || (n & 077) != 0) { throw new InvalidKeyException("bad p"); if (q.signum() != 1 && q.bitLength() != 160) { throw new InvalidKeyException("bad q"); if (x.signum() != 1 || x.compareTo(q) != -1) {
private static String computeFingerprint(PublicKey key) { if (key instanceof RSAPublicKey) { RSAPublicKey rsaKey = (RSAPublicKey) key; return SshKeys.fingerprint(rsaKey.getPublicExponent(), rsaKey.getModulus()); } else if (key instanceof DSAPublicKey) { DSAPublicKey dsaKey = (DSAPublicKey) key; return DSAKeys.fingerprint(dsaKey.getParams().getP(), dsaKey.getParams().getQ(), dsaKey.getParams().getG(), dsaKey.getY()); } else if (key instanceof ECPublicKey) { ECPublicKey ecdsaKey = (ECPublicKey) key; return ECDSAKeys.fingerprint(ecdsaKey); } else { throw new IllegalArgumentException("Only RSA and DSA keys are supported"); } }
@Override public void init(int keySize) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(keySize, new SecureRandom()); KeyPair pair = keyGen.generateKeyPair(); _x = ((DSAPrivateKey) pair.getPrivate()).getX().toByteArray(); _y = ((DSAPublicKey) pair.getPublic()).getY().toByteArray(); DSAParams params = ((DSAKey) pair.getPrivate()).getParams(); _p = params.getP().toByteArray(); _q = params.getQ().toByteArray(); _g = params.getG().toByteArray(); }
@Override public DSAPrivateKey clonePrivateKey(DSAPrivateKey key) throws GeneralSecurityException { if (key == null) { return null; } DSAParams params = key.getParams(); if (params == null) { throw new InvalidKeyException("Missing parameters in key"); } return generatePrivateKey(new DSAPrivateKeySpec(key.getX(), params.getP(), params.getQ(), params.getG())); }
private static void checkKeyLength(X509Certificate cert) throws CertificateException { Object pubkey = cert.getPublicKey(); if (pubkey instanceof RSAPublicKey) { int modulusLength = ((RSAPublicKey) pubkey).getModulus().bitLength(); if (modulusLength < MIN_RSA_MODULUS_LEN_BITS) { throw new CertificateException( "RSA modulus is < " + MIN_RSA_MODULUS_LEN_BITS + " bits"); } } else if (pubkey instanceof ECPublicKey) { int fieldSizeBits = ((ECPublicKey) pubkey).getParams().getCurve().getField().getFieldSize(); if (fieldSizeBits < MIN_EC_FIELD_SIZE_BITS) { throw new CertificateException( "EC key field size is < " + MIN_EC_FIELD_SIZE_BITS + " bits"); } } else if (pubkey instanceof DSAPublicKey) { int pLength = ((DSAPublicKey) pubkey).getParams().getP().bitLength(); int qLength = ((DSAPublicKey) pubkey).getParams().getQ().bitLength(); if ((pLength < MIN_DSA_P_LEN_BITS) || (qLength < MIN_DSA_Q_LEN_BITS)) { throw new CertificateException( "DSA key length is < (" + MIN_DSA_P_LEN_BITS + ", " + MIN_DSA_Q_LEN_BITS + ") bits"); } } else { // Unknown keys will be of type X509PublicKey. throw new CertificateException("Rejecting unknown key class " + pubkey.getClass().getName()); } }
public boolean equals( Object o) { if (!(o instanceof DSAPrivateKey)) { return false; } DSAPrivateKey other = (DSAPrivateKey)o; return this.getX().equals(other.getX()) && this.getParams().getG().equals(other.getParams().getG()) && this.getParams().getP().equals(other.getParams().getP()) && this.getParams().getQ().equals(other.getParams().getQ()); }
/** * Get the key size for the given {@code Key}. * * @param key the key * @return the key size or -1 if the key size cannot be determined */ public static int getKeySize(final Key key) { if (key instanceof ECKey) { ECParameterSpec params = ((ECKey) key).getParams(); if (params != null) { return params.getOrder().bitLength(); } } else if (key instanceof DSAKey) { DSAParams params = ((DSAKey) key).getParams(); if (params != null) { return params.getP().bitLength(); } } else if (key instanceof RSAKey) { return ((RSAKey) key).getModulus().bitLength(); } return -1; }
private static KeyPair generateKeyPair(PublicKey pubKey) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { String keyAlg = pubKey.getAlgorithm(); AlgorithmParameterSpec params = null; if ("RSA".equals(keyAlg)) { RSAPublicKey rsaKey = (RSAPublicKey) pubKey; params = new RSAKeyGenParameterSpec(rsaKey.getModulus().bitLength(), rsaKey.getPublicExponent()); } else if ("EC".equals(keyAlg)) { ECPublicKey ecKey = (ECPublicKey) pubKey; params = ecKey.getParams(); } else if ("DSA".equals(keyAlg)) { DSAPublicKey dsaKey = (DSAPublicKey) pubKey; DSAParams p = dsaKey.getParams(); params = new DSAParameterSpec(p.getP(), p.getQ(), p.getG()); } else { throw new UnsupportedOperationException("No support for " + keyAlg); } KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyAlg); keyGen.initialize(params); return keyGen.generateKeyPair(); }
/** * <p>Returns <code>true</code> if the designated object is an instance of * {@link DSAKey} and has the same DSS (Digital Signature Standard) parameter * values as this one.</p> * * @param obj the other non-null DSS key to compare to. * @return <code>true</code> if the designated object is of the same type and * value as this one. */ public boolean equals(Object obj) { if (obj == null) { return false; } if (!(obj instanceof DSAKey)) { return false; } DSAKey that = (DSAKey) obj; return p.equals(that.getParams().getP()) && q.equals(that.getParams().getQ()) && g.equals(that.getParams().getG()); }
/** * Builds a DSA key from an {@link DSAKeyValue} element and the supplied Java {@link DSAParams}, * which supplies key material from a shared key family. * * @param keyDescriptor the {@link DSAKeyValue} key descriptor * @param dsaParams the {@link DSAParams} DSA key family parameters * * @return a new {@link DSAPublicKey} instance of {@link PublicKey} * * @throws KeyException thrown if the key algorithm is not supported by the JCE or the key spec does not * contain valid information */ public static PublicKey getDSAKey(DSAKeyValue keyDescriptor, DSAParams dsaParams) throws KeyException { BigInteger yComponent = keyDescriptor.getY().getValueBigInt(); DSAPublicKeySpec keySpec = new DSAPublicKeySpec(yComponent, dsaParams.getP(), dsaParams.getQ(), dsaParams.getG()); return buildKey(keySpec, "DSA"); }
/** * {@inheritDoc} */ protected void engineInitSign(Key privateKey, SecureRandom secureRandom) throws XMLSignatureException { if (!(privateKey instanceof PrivateKey)) { String supplied = null; if (privateKey != null) { supplied = privateKey.getClass().getName(); } String needed = PrivateKey.class.getName(); Object exArgs[] = { supplied, needed }; throw new XMLSignatureException("algorithms.WrongKeyForThisOperation", exArgs); } try { if (secureRandom == null) { this.signatureAlgorithm.initSign((PrivateKey) privateKey); } else { this.signatureAlgorithm.initSign((PrivateKey) privateKey, secureRandom); } } catch (InvalidKeyException ex) { throw new XMLSignatureException(ex); } size = ((DSAKey)privateKey).getParams().getQ().bitLength(); }
/** * Determines whether the given DSA public and private keys form a proper key pair by computing and verifying a * digital signature with the keys. * * @param pubKey DSA public key. * @param privKey DSA private key. * * @return True if the keys form a functioning keypair, false otherwise. Errors during signature verification are * treated as false. * * @throws org.cryptacular.CryptoException on key validation errors. */ public static boolean isKeyPair(final DSAPublicKey pubKey, final DSAPrivateKey privKey) throws org.cryptacular.CryptoException { final DSASigner signer = new DSASigner(); final DSAParameters params = new DSAParameters( privKey.getParams().getP(), privKey.getParams().getQ(), privKey.getParams().getG()); try { signer.init(true, new DSAPrivateKeyParameters(privKey.getX(), params)); final BigInteger[] sig = signer.generateSignature(SIGN_BYTES); signer.init(false, new DSAPublicKeyParameters(pubKey.getY(), params)); return signer.verifySignature(SIGN_BYTES, sig[0], sig[1]); } catch (RuntimeException e) { throw new org.cryptacular.CryptoException("Signature computation error", e); } }