Search in sources :

Example 31 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.

the class CipherTest method testRSA_ECB_NoPadding_Private_UpdateInAndOutPlusDoFinal_TooBig_Failure.

private void testRSA_ECB_NoPadding_Private_UpdateInAndOutPlusDoFinal_TooBig_Failure(String provider) throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
    final PrivateKey privKey = kf.generatePrivate(keySpec);
    Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
    /*
         * You're actually encrypting with public keys, but there is no
         * distinction made here. It's all keyed off of what kind of key you're
         * using. ENCRYPT_MODE and DECRYPT_MODE are the same.
         */
    c.init(Cipher.ENCRYPT_MODE, privKey);
    byte[] output = new byte[RSA_2048_Vector1.length];
    c.update(RSA_Vector1_ZeroPadded_Encrypted, 0, RSA_Vector1_ZeroPadded_Encrypted.length, output);
    try {
        c.doFinal(RSA_Vector1_ZeroPadded_Encrypted);
        fail("Should have error when block size is too big.");
    } catch (IllegalBlockSizeException success) {
        assertFalse(provider, "BC".equals(provider));
    } catch (ArrayIndexOutOfBoundsException success) {
        assertEquals("BC", provider);
    }
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 32 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.

the class CipherTest method testRSA_ECB_NoPadding_Private_OnlyDoFinal_TooBig_Failure.

private void testRSA_ECB_NoPadding_Private_OnlyDoFinal_TooBig_Failure(String provider) throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
    final PrivateKey privKey = kf.generatePrivate(keySpec);
    Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
    /*
         * You're actually encrypting with public keys, but there is no
         * distinction made here. It's all keyed off of what kind of key you're
         * using. ENCRYPT_MODE and DECRYPT_MODE are the same.
         */
    c.init(Cipher.ENCRYPT_MODE, privKey);
    byte[] tooBig_Vector = new byte[RSA_Vector1_ZeroPadded_Encrypted.length * 2];
    System.arraycopy(RSA_Vector1_ZeroPadded_Encrypted, 0, tooBig_Vector, 0, RSA_Vector1_ZeroPadded_Encrypted.length);
    System.arraycopy(RSA_Vector1_ZeroPadded_Encrypted, 0, tooBig_Vector, RSA_Vector1_ZeroPadded_Encrypted.length, RSA_Vector1_ZeroPadded_Encrypted.length);
    try {
        c.doFinal(tooBig_Vector);
        fail("Should have error when block size is too big.");
    } catch (IllegalBlockSizeException success) {
        assertFalse(provider, "BC".equals(provider));
    } catch (ArrayIndexOutOfBoundsException success) {
        assertEquals("BC", provider);
    }
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 33 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.

the class CipherTest method checkCipher.

private void checkCipher(CipherTestParam p, String provider) throws Exception {
    SecretKey key = new SecretKeySpec(p.key, "AES");
    Cipher c = Cipher.getInstance(p.mode + "/PKCS5Padding", provider);
    AlgorithmParameterSpec spec = null;
    if (p.iv != null) {
        spec = new IvParameterSpec(p.iv);
    }
    c.init(Cipher.ENCRYPT_MODE, key, spec);
    final byte[] actualCiphertext = c.doFinal(p.plaintext);
    assertEquals(Arrays.toString(p.ciphertext), Arrays.toString(actualCiphertext));
    byte[] emptyCipherText = c.doFinal();
    assertNotNull(emptyCipherText);
    c.init(Cipher.DECRYPT_MODE, key, spec);
    try {
        c.updateAAD(new byte[8]);
        fail("Cipher should not support AAD");
    } catch (UnsupportedOperationException expected) {
    }
    byte[] emptyPlainText = c.doFinal(emptyCipherText);
    assertEquals(Arrays.toString(EmptyArray.BYTE), Arrays.toString(emptyPlainText));
    // empty decrypt
    {
        if (StandardNames.IS_RI) {
            assertEquals(Arrays.toString(EmptyArray.BYTE), Arrays.toString(c.doFinal()));
            c.update(EmptyArray.BYTE);
            assertEquals(Arrays.toString(EmptyArray.BYTE), Arrays.toString(c.doFinal()));
        } else if (provider.equals("BC")) {
            try {
                c.doFinal();
                fail();
            } catch (IllegalBlockSizeException expected) {
            }
            try {
                c.update(EmptyArray.BYTE);
                c.doFinal();
                fail();
            } catch (IllegalBlockSizeException expected) {
            }
        } else if (provider.equals("AndroidOpenSSL")) {
            assertNull(c.doFinal());
            c.update(EmptyArray.BYTE);
            assertNull(c.doFinal());
        } else {
            throw new AssertionError("Define your behavior here for " + provider);
        }
    }
    // .doFinal(input)
    {
        final byte[] actualPlaintext = c.doFinal(p.ciphertext);
        assertEquals(Arrays.toString(p.plaintext), Arrays.toString(actualPlaintext));
    }
    // .doFinal(input, offset, len, output)
    {
        final byte[] largerThanCiphertext = new byte[p.ciphertext.length + 5];
        System.arraycopy(p.ciphertext, 0, largerThanCiphertext, 5, p.ciphertext.length);
        final byte[] actualPlaintext = new byte[c.getOutputSize(p.ciphertext.length)];
        assertEquals(p.plaintext.length, c.doFinal(largerThanCiphertext, 5, p.ciphertext.length, actualPlaintext));
        assertEquals(Arrays.toString(p.plaintext), Arrays.toString(Arrays.copyOfRange(actualPlaintext, 0, p.plaintext.length)));
    }
    // .doFinal(input, offset, len, output, offset)
    {
        final byte[] largerThanCiphertext = new byte[p.ciphertext.length + 10];
        System.arraycopy(p.ciphertext, 0, largerThanCiphertext, 5, p.ciphertext.length);
        final byte[] actualPlaintext = new byte[c.getOutputSize(p.ciphertext.length) + 2];
        assertEquals(p.plaintext.length, c.doFinal(largerThanCiphertext, 5, p.ciphertext.length, actualPlaintext, 1));
        assertEquals(Arrays.toString(p.plaintext), Arrays.toString(Arrays.copyOfRange(actualPlaintext, 1, p.plaintext.length + 1)));
    }
    Cipher cNoPad = Cipher.getInstance(p.mode + "/NoPadding", provider);
    cNoPad.init(Cipher.DECRYPT_MODE, key, spec);
    final byte[] actualPlaintextPadded = cNoPad.doFinal(p.ciphertext);
    assertEquals(Arrays.toString(p.plaintextPadded), Arrays.toString(actualPlaintextPadded));
    // Test wrapping a key. Every cipher should be able to wrap.
    {
        // Generate a small SecretKey for AES.
        KeyGenerator kg = KeyGenerator.getInstance("AES");
        kg.init(128);
        SecretKey sk = kg.generateKey();
        // Wrap it
        c.init(Cipher.WRAP_MODE, key, spec);
        byte[] cipherText = c.wrap(sk);
        // Unwrap it
        c.init(Cipher.UNWRAP_MODE, key, spec);
        Key decryptedKey = c.unwrap(cipherText, sk.getAlgorithm(), Cipher.SECRET_KEY);
        assertEquals("sk.getAlgorithm()=" + sk.getAlgorithm() + " decryptedKey.getAlgorithm()=" + decryptedKey.getAlgorithm() + " encryptKey.getEncoded()=" + Arrays.toString(sk.getEncoded()) + " decryptedKey.getEncoded()=" + Arrays.toString(decryptedKey.getEncoded()), sk, decryptedKey);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) KeyGenerator(javax.crypto.KeyGenerator) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey)

Example 34 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.

the class CipherTest method test_doFinal$BII.

public void test_doFinal$BII() throws Exception {
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    byte[] b1 = new byte[30];
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    try {
        c.doFinal(b, 0, 10);
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.doFinal(b, 0, 10);
        fail();
    } catch (IllegalStateException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    int len1 = c.doFinal(b, 0, 16).length;
    assertEquals(16, len1);
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    int len2 = c.doFinal(b, 0, 16, b1, 0);
    assertEquals(16, len2);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    try {
        c.doFinal(b1, 0, 24);
        fail();
    } catch (BadPaddingException expected) {
    }
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) BadPaddingException(javax.crypto.BadPaddingException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 35 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.

the class CipherTest method test_doFinal$BI.

public void test_doFinal$BI() throws Exception {
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    byte[] b1 = new byte[30];
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    c.update(b, 0, 10);
    try {
        c.doFinal(b1, 5);
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.doFinal(b1, 5);
        fail();
    } catch (IllegalStateException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    c.update(b, 3, 8);
    int len = c.doFinal(b1, 0);
    assertEquals(0, len);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    c.update(b1, 0, 24);
    try {
        c.doFinal(b, 0);
        fail();
    } catch (BadPaddingException expected) {
    }
    b1 = new byte[6];
    c = Cipher.getInstance("DESede");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
    c.update(b, 3, 6);
    try {
        c.doFinal(b1, 5);
        fail();
    } catch (ShortBufferException expected) {
    }
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) BadPaddingException(javax.crypto.BadPaddingException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Aggregations

IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)118 BadPaddingException (javax.crypto.BadPaddingException)103 InvalidKeyException (java.security.InvalidKeyException)83 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)70 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)69 Cipher (javax.crypto.Cipher)59 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)46 IOException (java.io.IOException)40 IvParameterSpec (javax.crypto.spec.IvParameterSpec)27 SecretKey (javax.crypto.SecretKey)26 UnrecoverableKeyException (java.security.UnrecoverableKeyException)25 CertificateException (java.security.cert.CertificateException)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)25 KeyStoreException (java.security.KeyStoreException)24 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)17 RemoteException (android.os.RemoteException)15 ShortBufferException (javax.crypto.ShortBufferException)14 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)13 KeyGenerator (javax.crypto.KeyGenerator)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12