Search in sources :

Example 61 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project android_frameworks_base by AOSPA.

the class AndroidKeyStoreUnauthenticatedAESCipherSpi method engineGetParameters.

@Nullable
@Override
protected final AlgorithmParameters engineGetParameters() {
    if (!mIvRequired) {
        return null;
    }
    if ((mIv != null) && (mIv.length > 0)) {
        try {
            AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
            params.init(new IvParameterSpec(mIv));
            return params;
        } catch (NoSuchAlgorithmException e) {
            throw new ProviderException("Failed to obtain AES AlgorithmParameters", e);
        } catch (InvalidParameterSpecException e) {
            throw new ProviderException("Failed to initialize AES AlgorithmParameters with an IV", e);
        }
    }
    return null;
}
Also used : ProviderException(java.security.ProviderException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameters(java.security.AlgorithmParameters) Nullable(android.annotation.Nullable)

Example 62 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project android_frameworks_base by AOSPA.

the class AndroidKeyStoreUnauthenticatedAESCipherSpi method initAlgorithmSpecificParameters.

@Override
protected final void initAlgorithmSpecificParameters(AlgorithmParameters params) throws InvalidAlgorithmParameterException {
    if (!mIvRequired) {
        if (params != null) {
            throw new InvalidAlgorithmParameterException("Unsupported parameters: " + params);
        }
        return;
    }
    // IV is used
    if (params == null) {
        if (!isEncrypting()) {
            // IV must be provided by the caller
            throw new InvalidAlgorithmParameterException("IV required when decrypting" + ". Use IvParameterSpec or AlgorithmParameters to provide it.");
        }
        return;
    }
    if (!"AES".equalsIgnoreCase(params.getAlgorithm())) {
        throw new InvalidAlgorithmParameterException("Unsupported AlgorithmParameters algorithm: " + params.getAlgorithm() + ". Supported: AES");
    }
    IvParameterSpec ivSpec;
    try {
        ivSpec = params.getParameterSpec(IvParameterSpec.class);
    } catch (InvalidParameterSpecException e) {
        if (!isEncrypting()) {
            // IV must be provided by the caller
            throw new InvalidAlgorithmParameterException("IV required when decrypting" + ", but not found in parameters: " + params, e);
        }
        mIv = null;
        return;
    }
    mIv = ivSpec.getIV();
    if (mIv == null) {
        throw new InvalidAlgorithmParameterException("Null IV in AlgorithmParameters");
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException)

Example 63 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project android_frameworks_base by AOSPA.

the class CryptoHelper method decryptBundle.

@Nullable
/* default */
Bundle decryptBundle(@NonNull Bundle bundle) throws GeneralSecurityException {
    Preconditions.checkNotNull(bundle, "Cannot decrypt null bundle.");
    byte[] iv = bundle.getByteArray(KEY_IV);
    byte[] encryptedBytes = bundle.getByteArray(KEY_CIPHER);
    byte[] mac = bundle.getByteArray(KEY_MAC);
    if (!verifyMac(encryptedBytes, iv, mac)) {
        Log.w(TAG, "Escrow mac mismatched!");
        return null;
    }
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, mEncryptionKey, ivSpec);
    byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
    Parcel decryptedParcel = Parcel.obtain();
    decryptedParcel.unmarshall(decryptedBytes, 0, decryptedBytes.length);
    decryptedParcel.setDataPosition(0);
    Bundle decryptedBundle = new Bundle();
    decryptedBundle.readFromParcel(decryptedParcel);
    decryptedParcel.recycle();
    return decryptedBundle;
}
Also used : Parcel(android.os.Parcel) Bundle(android.os.Bundle) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) Nullable(android.annotation.Nullable)

Example 64 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project intellij-community by JetBrains.

the class PropertiesEncryptionSupport method decrypt.

private static byte[] decrypt(byte[] data, Key key) throws Exception {
    final int bodyLength = ((data[0] & 0xFF) << 24) | ((data[1] & 0xFF) << 16) | ((data[2] & 0xFF) << 8) | ((data[3] & 0xFF));
    final int ivlength = data.length - 4 - bodyLength;
    final Cipher ciph = Cipher.getInstance("AES/CBC/PKCS5Padding");
    ciph.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(data, 4, ivlength));
    return ciph.doFinal(data, 4 + ivlength, bodyLength);
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Example 65 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project ABPlayer by winkstu.

the class Crypto method setupCrypto.

private void setupCrypto(SecretKey key) {
    byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
    AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
    try {
        ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
    } catch (Exception e) {
        ecipher = null;
        Log.e("setupCrypto", e);
    }
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

IvParameterSpec (javax.crypto.spec.IvParameterSpec)229 Cipher (javax.crypto.Cipher)150 SecretKeySpec (javax.crypto.spec.SecretKeySpec)107 SecretKey (javax.crypto.SecretKey)49 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)46 InvalidKeyException (java.security.InvalidKeyException)43 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)42 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)39 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)30 BadPaddingException (javax.crypto.BadPaddingException)28 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)25 Key (java.security.Key)21 KeyGenerator (javax.crypto.KeyGenerator)21 IOException (java.io.IOException)19 SecureRandom (java.security.SecureRandom)17 GeneralSecurityException (java.security.GeneralSecurityException)15 MyCipher (org.apache.harmony.crypto.tests.support.MyCipher)15 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)14 MessageDigest (java.security.MessageDigest)13 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)13