Search in sources :

Example 16 with DSAParams

use of java.security.interfaces.DSAParams in project jdk8u_jdk by JetBrains.

the class DSAPrivateKey method getParams.

/**
     * Returns the DSA parameters associated with this key, or null if the
     * parameters could not be parsed.
     */
public DSAParams getParams() {
    try {
        if (algid instanceof DSAParams) {
            return (DSAParams) algid;
        } else {
            DSAParameterSpec paramSpec;
            AlgorithmParameters algParams = algid.getParameters();
            if (algParams == null) {
                return null;
            }
            paramSpec = algParams.getParameterSpec(DSAParameterSpec.class);
            return (DSAParams) paramSpec;
        }
    } catch (InvalidParameterSpecException e) {
        return null;
    }
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) DSAParams(java.security.interfaces.DSAParams) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 17 with DSAParams

use of java.security.interfaces.DSAParams in project jdk8u_jdk by JetBrains.

the class DSAPublicKey method getParams.

/**
     * Returns the DSA parameters associated with this key, or null if the
     * parameters could not be parsed.
     */
public DSAParams getParams() {
    try {
        if (algid instanceof DSAParams) {
            return (DSAParams) algid;
        } else {
            DSAParameterSpec paramSpec;
            AlgorithmParameters algParams = algid.getParameters();
            if (algParams == null) {
                return null;
            }
            paramSpec = algParams.getParameterSpec(DSAParameterSpec.class);
            return (DSAParams) paramSpec;
        }
    } catch (InvalidParameterSpecException e) {
        return null;
    }
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) DSAParams(java.security.interfaces.DSAParams) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 18 with DSAParams

use of java.security.interfaces.DSAParams in project jdk8u_jdk by JetBrains.

the class KeyUtil method getKeySize.

/**
     * Returns the key size of the given key object in bits.
     *
     * @param key the key object, cannot be null
     * @return the key size of the given key object in bits, or -1 if the
     *       key size is not accessible
     */
public static final int getKeySize(Key key) {
    int size = -1;
    if (key instanceof Length) {
        try {
            Length ruler = (Length) key;
            size = ruler.length();
        } catch (UnsupportedOperationException usoe) {
        // ignore the exception
        }
        if (size >= 0) {
            return size;
        }
    }
    // try to parse the length from key specification
    if (key instanceof SecretKey) {
        SecretKey sk = (SecretKey) key;
        String format = sk.getFormat();
        if ("RAW".equals(format) && sk.getEncoded() != null) {
            size = (sk.getEncoded().length * 8);
        }
    // Otherwise, it may be a unextractable key of PKCS#11, or
    // a key we are not able to handle.
    } else if (key instanceof RSAKey) {
        RSAKey pubk = (RSAKey) key;
        size = pubk.getModulus().bitLength();
    } else if (key instanceof ECKey) {
        ECKey pubk = (ECKey) key;
        size = pubk.getParams().getOrder().bitLength();
    } else if (key instanceof DSAKey) {
        DSAKey pubk = (DSAKey) key;
        // params can be null
        DSAParams params = pubk.getParams();
        size = (params != null) ? params.getP().bitLength() : -1;
    } else if (key instanceof DHKey) {
        DHKey pubk = (DHKey) key;
        size = pubk.getParams().getP().bitLength();
    }
    return size;
}
Also used : SecretKey(javax.crypto.SecretKey) RSAKey(java.security.interfaces.RSAKey) DSAKey(java.security.interfaces.DSAKey) ECKey(java.security.interfaces.ECKey) DSAParams(java.security.interfaces.DSAParams) DHKey(javax.crypto.interfaces.DHKey)

Example 19 with DSAParams

use of java.security.interfaces.DSAParams in project bitsquare by bitsquare.

the class KeyStorage method loadKeyPair.

public KeyPair loadKeyPair(KeyEntry keyEntry) {
    FileUtil.rollingBackup(storageDir, keyEntry.getFileName() + ".key", 20);
    // long now = System.currentTimeMillis();
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(keyEntry.getAlgorithm(), "BC");
        PublicKey publicKey;
        PrivateKey privateKey;
        File filePrivateKey = new File(storageDir + "/" + keyEntry.getFileName() + ".key");
        try (FileInputStream fis = new FileInputStream(filePrivateKey.getPath())) {
            byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
            fis.read(encodedPrivateKey);
            PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
            privateKey = keyFactory.generatePrivate(privateKeySpec);
        } catch (InvalidKeySpecException | IOException e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new RuntimeException("Could not load key " + keyEntry.toString(), e);
        }
        if (privateKey instanceof RSAPrivateCrtKey) {
            RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
            RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPublicExponent());
            publicKey = keyFactory.generatePublic(publicKeySpec);
        } else if (privateKey instanceof DSAPrivateKey) {
            DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
            DSAParams dsaParams = dsaPrivateKey.getParams();
            BigInteger p = dsaParams.getP();
            BigInteger q = dsaParams.getQ();
            BigInteger g = dsaParams.getG();
            BigInteger y = g.modPow(dsaPrivateKey.getX(), p);
            KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
            publicKey = keyFactory.generatePublic(publicKeySpec);
        } else {
            throw new RuntimeException("Unsupported key algo" + keyEntry.getAlgorithm());
        }
        log.debug("load completed in {} msec", System.currentTimeMillis() - new Date().getTime());
        return new KeyPair(publicKey, privateKey);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException | NoSuchProviderException e) {
        e.printStackTrace();
        log.error(e.getMessage());
        throw new RuntimeException("Could not load key " + keyEntry.toString(), e);
    }
}
Also used : DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) IOException(java.io.IOException) DSAParams(java.security.interfaces.DSAParams) FileInputStream(java.io.FileInputStream) Date(java.util.Date) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BigInteger(java.math.BigInteger) File(java.io.File)

Example 20 with DSAParams

use of java.security.interfaces.DSAParams in project wycheproof by google.

the class DsaTest method testBasic.

/**
   * This is just a test for basic functionality of DSA. The test generates a public and private
   * key, generates a signature, verifies it and prints the whole thing out. This test is useful
   * when an implementation is seriously broken.
   */
@SlowTest(providers = { ProviderType.BOUNCY_CASTLE, ProviderType.SPONGY_CASTLE })
@SuppressWarnings("InsecureCryptoUsage")
public void testBasic() throws Exception {
    int keySize = 2048;
    String algorithm = "SHA256WithDSA";
    String hashAlgorithm = "SHA-256";
    String message = "Hello";
    byte[] messageBytes = message.getBytes("UTF-8");
    KeyPairGenerator generator = java.security.KeyPairGenerator.getInstance("DSA");
    generator.initialize(keySize);
    KeyPair keyPair = generator.generateKeyPair();
    DSAPublicKey pub = (DSAPublicKey) keyPair.getPublic();
    DSAPrivateKey priv = (DSAPrivateKey) keyPair.getPrivate();
    Signature signer = Signature.getInstance(algorithm);
    Signature verifier = Signature.getInstance(algorithm);
    signer.initSign(priv);
    signer.update(messageBytes);
    byte[] signature = signer.sign();
    verifier.initVerify(pub);
    verifier.update(messageBytes);
    assertTrue(verifier.verify(signature));
    // Extract some parameters.
    byte[] rawHash = MessageDigest.getInstance(hashAlgorithm).digest(messageBytes);
    DSAParams params = priv.getParams();
    // Print keys and signature, so that it can be used to generate new test vectors.
    System.out.println("Message:" + message);
    System.out.println("Hash:" + TestUtil.bytesToHex(rawHash));
    System.out.println("Params:");
    System.out.println("p:" + params.getP().toString());
    System.out.println("q:" + params.getQ().toString());
    System.out.println("g:" + params.getG().toString());
    System.out.println("Private key:");
    System.out.println("X:" + priv.getX().toString());
    System.out.println("encoded:" + TestUtil.bytesToHex(priv.getEncoded()));
    System.out.println("Public key:");
    System.out.println("Y:" + pub.getY().toString());
    System.out.println("encoded:" + TestUtil.bytesToHex(pub.getEncoded()));
    System.out.println("Signature:" + TestUtil.bytesToHex(signature));
    System.out.println("r:" + extractR(signature).toString());
    System.out.println("s:" + extractS(signature).toString());
}
Also used : KeyPair(java.security.KeyPair) Signature(java.security.Signature) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) KeyPairGenerator(java.security.KeyPairGenerator) DSAParams(java.security.interfaces.DSAParams) DSAPublicKey(java.security.interfaces.DSAPublicKey) SlowTest(com.google.security.wycheproof.WycheproofRunner.SlowTest)

Aggregations

DSAParams (java.security.interfaces.DSAParams)40 DSAPublicKey (java.security.interfaces.DSAPublicKey)19 BigInteger (java.math.BigInteger)16 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)13 DSAParameterSpec (java.security.spec.DSAParameterSpec)11 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)11 InvalidKeyException (java.security.InvalidKeyException)8 PublicKey (java.security.PublicKey)7 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)7 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 KeyPairGenerator (java.security.KeyPairGenerator)6 SecureRandom (java.security.SecureRandom)5 X509Certificate (java.security.cert.X509Certificate)5 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)5 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)5 GeneralSecurityException (java.security.GeneralSecurityException)4 KeyFactory (java.security.KeyFactory)4 KeyPair (java.security.KeyPair)4 CertPathValidatorException (java.security.cert.CertPathValidatorException)4 IOException (java.io.IOException)3