Search in sources :

Example 86 with AlgorithmParameters

use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.

the class Encrypt method main.

public static void main(String[] args) throws Exception {
    Provider p = Security.getProvider("SunJCE");
    for (String alg : ALGORITHMS) {
        for (int keyStrength : KEY_STRENGTHS) {
            if (keyStrength > Cipher.getMaxAllowedKeyLength(alg)) {
                // configured in the JCE jurisdiction policy files
                continue;
            }
            for (int textLength : TEXT_LENGTHS) {
                for (int AADLength : AAD_LENGTHS) {
                    Encrypt test = new Encrypt(p, alg, "GCM", "NoPadding", keyStrength, textLength, AADLength);
                    Cipher cipher = test.createCipher(Cipher.ENCRYPT_MODE, null);
                    AlgorithmParameters params = cipher.getParameters();
                    test.doTest(params);
                    System.out.println("Test " + alg + ":" + keyStrength + ":" + textLength + ":" + AADLength + " passed");
                }
            }
        }
    }
}
Also used : Cipher(javax.crypto.Cipher) Provider(java.security.Provider) AlgorithmParameters(java.security.AlgorithmParameters)

Example 87 with AlgorithmParameters

use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.

the class PBESealedObject method runTest.

// Have a generic throws Exception as it can throw many different exceptions
public boolean runTest(Provider p, String algo, PrintStream out) throws Exception {
    byte[] salt = new byte[8];
    int ITERATION_COUNT = 1000;
    AlgorithmParameters pbeParams = null;
    String baseAlgo = new StringTokenizer(algo, "/").nextToken().toUpperCase();
    boolean isAES = baseAlgo.contains("AES");
    try {
        // Initialization
        Cipher ci = Cipher.getInstance(algo, p);
        new Random().nextBytes(salt);
        AlgorithmParameterSpec aps = new PBEParameterSpec(salt, ITERATION_COUNT);
        SecretKeyFactory skf = SecretKeyFactory.getInstance(baseAlgo, p);
        SecretKey key = skf.generateSecret(new PBEKeySpec("Secret Lover".toCharArray()));
        // Seal
        if (isAES) {
            ci.init(Cipher.ENCRYPT_MODE, key);
            pbeParams = ci.getParameters();
        } else {
            ci.init(Cipher.ENCRYPT_MODE, key, aps);
        }
        SealedObject so = new SealedObject(key, ci);
        // Unseal and compare
        if (isAES) {
            ci.init(Cipher.DECRYPT_MODE, key, pbeParams);
        } else {
            ci.init(Cipher.DECRYPT_MODE, key, aps);
        }
        SecretKey unsealedKey;
        unsealedKey = (SecretKey) so.getObject(ci);
        if (!Arrays.equals(unsealedKey.getEncoded(), key.getEncoded())) {
            return false;
        }
        unsealedKey = (SecretKey) so.getObject(key);
        if (!Arrays.equals(unsealedKey.getEncoded(), key.getEncoded())) {
            return false;
        }
        unsealedKey = (SecretKey) so.getObject(key, "SunJCE");
        return Arrays.equals(unsealedKey.getEncoded(), key.getEncoded());
    } catch (InvalidKeyException ex) {
        if (baseAlgo.endsWith("TRIPLEDES") || baseAlgo.endsWith("AES_256")) {
            out.println("Expected exception , keyStrength > 128 within" + algo);
            return true;
        }
        throw ex;
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SealedObject(javax.crypto.SealedObject) InvalidKeyException(java.security.InvalidKeyException) StringTokenizer(java.util.StringTokenizer) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) AlgorithmParameters(java.security.AlgorithmParameters)

Example 88 with AlgorithmParameters

use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.

the class TestCipherKeyWrapperPBEKey method runTest.

// Have a generic throws Exception as it can throw many different exceptions
public boolean runTest(Provider p, String algo, PrintStream out) throws Exception {
    byte[] salt = new byte[8];
    int ITERATION_COUNT = 1000;
    AlgorithmParameters pbeParams = null;
    String baseAlgo = new StringTokenizer(algo, "/").nextToken().toUpperCase();
    boolean isAES = baseAlgo.contains("AES");
    try {
        // Initialization
        new Random().nextBytes(salt);
        AlgorithmParameterSpec aps = new PBEParameterSpec(salt, ITERATION_COUNT);
        SecretKeyFactory skf = SecretKeyFactory.getInstance(baseAlgo, p);
        SecretKey key = skf.generateSecret(new PBEKeySpec("Secret Key".toCharArray()));
        Cipher ci = Cipher.getInstance(algo);
        if (isAES) {
            ci.init(Cipher.WRAP_MODE, key);
            pbeParams = ci.getParameters();
        } else {
            ci.init(Cipher.WRAP_MODE, key, aps);
        }
        byte[] keyWrapper = ci.wrap(key);
        if (isAES) {
            ci.init(Cipher.UNWRAP_MODE, key, pbeParams);
        } else {
            ci.init(Cipher.UNWRAP_MODE, key, aps);
        }
        Key unwrappedKey = ci.unwrap(keyWrapper, algo, Cipher.SECRET_KEY);
        if (baseAlgo.endsWith("TRIPLEDES") || baseAlgo.endsWith("AES_256")) {
            out.print("InvalidKeyException not thrown when keyStrength > 128");
            return false;
        }
        return (Arrays.equals(key.getEncoded(), unwrappedKey.getEncoded()));
    } catch (InvalidKeyException ex) {
        if ((baseAlgo.endsWith("TRIPLEDES") || baseAlgo.endsWith("AES_256"))) {
            out.println("Expected InvalidKeyException, keyStrength > 128");
            return true;
        } else {
            throw ex;
        }
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) InvalidKeyException(java.security.InvalidKeyException) StringTokenizer(java.util.StringTokenizer) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) Key(java.security.Key) SecretKey(javax.crypto.SecretKey) AlgorithmParameters(java.security.AlgorithmParameters)

Example 89 with AlgorithmParameters

use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.

the class PKCS12KeyStore method encryptContent.

/*
     * Encrypt the contents using Password-based (PBE) encryption
     * as defined in PKCS #5.
     *
     * NOTE: Currently pbeWithSHAAnd40BiteRC2-CBC algorithmID is used
     *       to derive the key and IV.
     *
     * @return encrypted contents encoded as EncryptedContentInfo
     */
private byte[] encryptContent(byte[] data, char[] password) throws IOException {
    byte[] encryptedData = null;
    // create AlgorithmParameters
    AlgorithmParameters algParams = getAlgorithmParameters("PBEWithSHA1AndRC2_40");
    DerOutputStream bytes = new DerOutputStream();
    AlgorithmId algId = new AlgorithmId(pbeWithSHAAnd40BitRC2CBC_OID, algParams);
    algId.encode(bytes);
    byte[] encodedAlgId = bytes.toByteArray();
    try {
        // Use JCE
        SecretKey skey = getPBEKey(password);
        Cipher cipher = Cipher.getInstance("PBEWithSHA1AndRC2_40");
        cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
        encryptedData = cipher.doFinal(data);
        if (debug != null) {
            debug.println("  (Cipher algorithm: " + cipher.getAlgorithm() + ")");
        }
    } catch (Exception e) {
        throw new IOException("Failed to encrypt" + " safe contents entry: " + e, e);
    }
    // create EncryptedContentInfo
    DerOutputStream bytes2 = new DerOutputStream();
    bytes2.putOID(ContentInfo.DATA_OID);
    bytes2.write(encodedAlgId);
    // Wrap encrypted data in a context-specific tag.
    DerOutputStream tmpout2 = new DerOutputStream();
    tmpout2.putOctetString(encryptedData);
    bytes2.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, false, (byte) 0), tmpout2);
    // wrap EncryptedContentInfo in a Sequence
    DerOutputStream out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, bytes2);
    return out.toByteArray();
}
Also used : SecretKey(javax.crypto.SecretKey) DerOutputStream(sun.security.util.DerOutputStream) AlgorithmId(sun.security.x509.AlgorithmId) Cipher(javax.crypto.Cipher) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) DestroyFailedException(javax.security.auth.DestroyFailedException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 90 with AlgorithmParameters

use of java.security.AlgorithmParameters in project jdk8u_jdk by JetBrains.

the class SupportedEllipticCurvesExtension method isAvailableCurve.

// check whether the curve is supported by the underlying providers
private static boolean isAvailableCurve(int curveId) {
    String oid = idToOidMap.get(curveId);
    if (oid != null) {
        AlgorithmParameters params = null;
        try {
            params = JsseJce.getAlgorithmParameters("EC");
            params.init(new ECGenParameterSpec(oid));
        } catch (Exception e) {
            return false;
        }
        // cache the parameters
        idToParams.put(curveId, params);
        return true;
    }
    return false;
}
Also used : ECGenParameterSpec(java.security.spec.ECGenParameterSpec) SSLProtocolException(javax.net.ssl.SSLProtocolException) IOException(java.io.IOException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

AlgorithmParameters (java.security.AlgorithmParameters)107 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 IOException (java.io.IOException)31 InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)22 Cipher (javax.crypto.Cipher)22 SecretKey (javax.crypto.SecretKey)18 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)13 NoSuchProviderException (java.security.NoSuchProviderException)12 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 Key (java.security.Key)11 SecureRandom (java.security.SecureRandom)10 EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)10 InvalidKeyException (java.security.InvalidKeyException)8 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)8 UnrecoverableKeyException (java.security.UnrecoverableKeyException)7 KeyPair (java.security.KeyPair)6 KeyPairGenerator (java.security.KeyPairGenerator)6 AlgorithmId (sun.security.x509.AlgorithmId)6 Nullable (android.annotation.Nullable)5 Asn1Integer (com.android.hotspot2.asn1.Asn1Integer)5