Search in sources :

Example 51 with AlgorithmParameters

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

the class SameBuffer method runTest.

/*
     * Run single test case with given parameters
     */
static void runTest(Provider p, String algo, String mode, String padding, int keyLength, int textLength, int AADLength, int offset) throws Exception {
    System.out.println("Testing " + keyLength + " key length; " + textLength + " text lenght; " + AADLength + " AAD length; " + offset + " offset");
    if (keyLength > Cipher.getMaxAllowedKeyLength(algo)) {
        // configured in the jce jurisdiction policy files
        return;
    }
    SameBuffer test = new SameBuffer(p, algo, mode, padding, keyLength, textLength, AADLength);
    /*
         * There are four test cases:
         *   1. AAD and text are placed in separated byte arrays
         *   2. AAD and text are placed in the same byte array
         *   3. AAD and text are placed in separated byte buffers
         *   4. AAD and text are placed in the same byte buffer
         */
    Cipher ci = test.createCipher(Cipher.ENCRYPT_MODE, null);
    AlgorithmParameters params = ci.getParameters();
    test.doTestWithSeparateArrays(offset, params);
    test.doTestWithSameArrays(offset, params);
    test.doTestWithSeparatedBuffer(offset, params);
    test.doTestWithSameBuffer(offset, params);
}
Also used : Cipher(javax.crypto.Cipher) AlgorithmParameters(java.security.AlgorithmParameters)

Example 52 with AlgorithmParameters

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

the class CTSMode method test2.

/**
     * Test with a combination of update/doFinal calls and make
     * sure that same data is recovered after decryption.
     */
private static void test2() throws Exception {
    for (int i = 0; i < ALGORITHMS2.length; i++) {
        String algo = ALGORITHMS2[i];
        System.out.println("Running test2_" + i + " (" + algo + ")");
        int keySize = KEYSIZES2[i];
        int MAX_KEYSIZE = Cipher.getMaxAllowedKeyLength(algo);
        if (keySize > MAX_KEYSIZE) {
            // what's configured in jce jurisdiction policy files.
            continue;
        }
        Cipher cipher = Cipher.getInstance(algo + "/CTS/NoPadding", "SunJCE");
        int blockSize = cipher.getBlockSize();
        SecretKeySpec key = new SecretKeySpec(new byte[keySize], algo);
        // Make sure encryption works for inputs with valid length
        byte[] plainText = PLAIN1[3];
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] cipherText = new byte[plainText.length];
        int firstPartLen = blockSize + 1;
        int processed1 = cipher.update(plainText, 0, firstPartLen, cipherText, 0);
        int processed2 = cipher.doFinal(plainText, firstPartLen, plainText.length - firstPartLen, cipherText, processed1);
        AlgorithmParameters params = cipher.getParameters();
        if ((processed1 + processed2) != plainText.length) {
            System.out.println("processed1 = " + processed1);
            System.out.println("processed2 = " + processed2);
            System.out.println("total length = " + plainText.length);
            throw new RuntimeException("Encryption failure for test " + i);
        }
        // with less-than-a-block length
        try {
            cipher.doFinal(new byte[blockSize - 1]);
            throw new RuntimeException("Expected IBSE is not thrown");
        } catch (IllegalBlockSizeException ibse) {
        }
        // Make sure data is encrypted as in CBC mode for inputs
        // which is exactly one block long
        IvParameterSpec iv2 = new IvParameterSpec(IV2_SRC, 0, blockSize);
        cipher.init(Cipher.ENCRYPT_MODE, key, iv2);
        Cipher cipher2 = Cipher.getInstance(algo + "/CBC/NoPadding", "SunJCE");
        cipher2.init(Cipher.ENCRYPT_MODE, key, iv2);
        byte[] eout = cipher.doFinal(IV2_SRC, 0, blockSize);
        byte[] eout2 = cipher2.doFinal(IV2_SRC, 0, blockSize);
        if (!Arrays.equals(eout, eout2)) {
            throw new RuntimeException("Different encryption output " + "for CBC and CTS");
        }
        // Make sure decryption works for inputs with valid length
        cipher.init(Cipher.DECRYPT_MODE, key, params);
        byte[] recoveredText = new byte[cipher.getOutputSize(cipherText.length)];
        processed1 = cipher.update(cipherText, 0, firstPartLen, recoveredText, 0);
        processed2 = cipher.update(cipherText, firstPartLen, cipherText.length - firstPartLen, recoveredText, processed1);
        int processed3 = cipher.doFinal(recoveredText, processed1 + processed2);
        if ((processed1 + processed2 + processed3) != plainText.length) {
            System.out.println("processed1 = " + processed1);
            System.out.println("processed2 = " + processed2);
            System.out.println("processed3 = " + processed3);
            System.out.println("total length = " + plainText.length);
            throw new RuntimeException("Decryption failure for test " + i);
        }
        if (Arrays.equals(plainText, recoveredText) == false) {
            System.out.println("plain:  " + toString(plainText));
            System.out.println("recovered: " + toString(recoveredText));
            throw new RuntimeException("Decryption failure for test " + i);
        }
        // with less-than-a-block length
        try {
            cipher.doFinal(new byte[blockSize - 1]);
            throw new RuntimeException("Expected IBSE is not thrown");
        } catch (IllegalBlockSizeException ibse) {
        }
        // Make sure data is decrypted as in CBC mode for inputs
        // which is exactly one block long
        cipher.init(Cipher.DECRYPT_MODE, key, iv2);
        cipher2.init(Cipher.DECRYPT_MODE, key, iv2);
        byte[] dout = cipher.doFinal(eout);
        byte[] dout2 = cipher2.doFinal(eout2);
        if (!Arrays.equals(dout, dout2)) {
            throw new RuntimeException("Different decryption output " + "for CBC and CTS");
        }
    }
}
Also used : AlgorithmParameters(java.security.AlgorithmParameters)

Example 53 with AlgorithmParameters

use of java.security.AlgorithmParameters in project j2objc by google.

the class AlgorithmParametersTest method testAlgorithmParametersConst.

/**
     * Tests OAEP AlgorithmParameters provider
     *
    TODO(tball): enable when OAEP provider is implemented.
    public void testOAEPProvider() throws Exception {
        AlgorithmParameters params = AlgorithmParameters.getInstance("OAEP");

        assertEquals("Algorithm", "OAEP", params.getAlgorithm());
    }
    */
/**
     * Test for <code>AlgorithmParameters</code> constructor
     * Assertion: returns AlgorithmParameters object
     */
public void testAlgorithmParametersConst() throws Exception {
    AlgorithmParametersSpi spi = new MyAlgorithmParameters();
    AlgorithmParameters ap = new myAlgP(spi, p, "ABC");
    checkUnititialized(ap);
    ap.init(new byte[6], "aaa");
    checkAP(ap, p);
    //NULL parameters
    try {
        ap = new myAlgP(null, null, null);
    } catch (Exception e) {
        fail("Exception should be not thrown");
    }
}
Also used : AlgorithmParametersSpi(java.security.AlgorithmParametersSpi) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 54 with AlgorithmParameters

use of java.security.AlgorithmParameters in project j2objc by google.

the class AlgorithmParametersTest method test_init$BLjava_lang_String.

/**
     * java.security.AlgorithmParameters#init(byte[],String)
     */
public void test_init$BLjava_lang_String() throws Exception {
    //
    // test: corresponding spi method is invoked
    //
    final byte[] enc = new byte[] { 0x02, 0x01, 0x03 };
    final String strFormatParam = "format";
    MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {

        protected void engineInit(byte[] params, String format) throws IOException {
            runEngineInitB$String = true;
            assertSame(enc, params);
            assertSame(strFormatParam, format);
        }
    };
    AlgorithmParameters params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(enc, strFormatParam);
    assertTrue(paramSpi.runEngineInitB$String);
    //
    try {
        params.init(enc, strFormatParam);
        fail("No expected IOException");
    } catch (IOException e) {
    // expected
    }
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(new MyAlgorithmParameterSpec());
    try {
        params.init(enc, strFormatParam);
        fail("No expected IOException");
    } catch (IOException e) {
    // expected
    }
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(enc);
    try {
        params.init(enc, strFormatParam);
        fail("No expected IOException");
    } catch (IOException e) {
    // expected
    }
    //
    // test: if params and format are null
    // Regression test for HARMONY-2724
    //
    paramSpi = new MyAlgorithmParameters() {

        protected void engineInit(byte[] params, String format) throws IOException {
            runEngineInitB$String = true;
            // null is passed to spi-provider
            assertNull(params);
            assertNull(format);
        }
    };
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(null, null);
    assertTrue(paramSpi.runEngineInitB$String);
}
Also used : IOException(java.io.IOException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 55 with AlgorithmParameters

use of java.security.AlgorithmParameters in project j2objc by google.

the class AlgorithmParametersTest method test_init$B.

/**
     * java.security.AlgorithmParameters#init(byte[])
     */
public void test_init$B() throws Exception {
    //
    // test: corresponding spi method is invoked
    //
    final byte[] enc = new byte[] { 0x02, 0x01, 0x03 };
    MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {

        protected void engineInit(byte[] params) throws IOException {
            runEngineInitB$ = true;
            assertSame(enc, params);
        }
    };
    AlgorithmParameters params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(enc);
    assertTrue(paramSpi.runEngineInitB$);
    //
    try {
        params.init(enc);
        fail("No expected IOException");
    } catch (IOException e) {
    // expected
    }
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(new MyAlgorithmParameterSpec());
    try {
        params.init(enc);
        fail("No expected IOException");
    } catch (IOException e) {
    // expected
    }
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(enc, "format");
    try {
        params.init(enc);
        fail("No expected IOException");
    } catch (IOException e) {
    // expected
    }
    //
    // test: if params is null
    //
    paramSpi = new MyAlgorithmParameters() {

        protected void engineInit(byte[] params) throws IOException {
            runEngineInitB$ = true;
            // null is passed to spi-provider
            assertNull(params);
        }
    };
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init((byte[]) null);
    assertTrue(paramSpi.runEngineInitB$);
}
Also used : IOException(java.io.IOException) 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