Search in sources :

Example 26 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project platform_frameworks_base by android.

the class AndroidKeyStoreKeyPairGeneratorSpi method initAlgorithmSpecificParameters.

private void initAlgorithmSpecificParameters() throws InvalidAlgorithmParameterException {
    AlgorithmParameterSpec algSpecificSpec = mSpec.getAlgorithmParameterSpec();
    switch(mKeymasterAlgorithm) {
        case KeymasterDefs.KM_ALGORITHM_RSA:
            {
                BigInteger publicExponent = null;
                if (algSpecificSpec instanceof RSAKeyGenParameterSpec) {
                    RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec) algSpecificSpec;
                    if (mKeySizeBits == -1) {
                        mKeySizeBits = rsaSpec.getKeysize();
                    } else if (mKeySizeBits != rsaSpec.getKeysize()) {
                        throw new InvalidAlgorithmParameterException("RSA key size must match " + " between " + mSpec + " and " + algSpecificSpec + ": " + mKeySizeBits + " vs " + rsaSpec.getKeysize());
                    }
                    publicExponent = rsaSpec.getPublicExponent();
                } else if (algSpecificSpec != null) {
                    throw new InvalidAlgorithmParameterException("RSA may only use RSAKeyGenParameterSpec");
                }
                if (publicExponent == null) {
                    publicExponent = RSAKeyGenParameterSpec.F4;
                }
                if (publicExponent.compareTo(BigInteger.ZERO) < 1) {
                    throw new InvalidAlgorithmParameterException("RSA public exponent must be positive: " + publicExponent);
                }
                if (publicExponent.compareTo(KeymasterArguments.UINT64_MAX_VALUE) > 0) {
                    throw new InvalidAlgorithmParameterException("Unsupported RSA public exponent: " + publicExponent + ". Maximum supported value: " + KeymasterArguments.UINT64_MAX_VALUE);
                }
                mRSAPublicExponent = publicExponent;
                break;
            }
        case KeymasterDefs.KM_ALGORITHM_EC:
            if (algSpecificSpec instanceof ECGenParameterSpec) {
                ECGenParameterSpec ecSpec = (ECGenParameterSpec) algSpecificSpec;
                String curveName = ecSpec.getName();
                Integer ecSpecKeySizeBits = SUPPORTED_EC_NIST_CURVE_NAME_TO_SIZE.get(curveName.toLowerCase(Locale.US));
                if (ecSpecKeySizeBits == null) {
                    throw new InvalidAlgorithmParameterException("Unsupported EC curve name: " + curveName + ". Supported: " + SUPPORTED_EC_NIST_CURVE_NAMES);
                }
                if (mKeySizeBits == -1) {
                    mKeySizeBits = ecSpecKeySizeBits;
                } else if (mKeySizeBits != ecSpecKeySizeBits) {
                    throw new InvalidAlgorithmParameterException("EC key size must match " + " between " + mSpec + " and " + algSpecificSpec + ": " + mKeySizeBits + " vs " + ecSpecKeySizeBits);
                }
            } else if (algSpecificSpec != null) {
                throw new InvalidAlgorithmParameterException("EC may only use ECGenParameterSpec");
            }
            break;
        default:
            throw new ProviderException("Unsupported algorithm: " + mKeymasterAlgorithm);
    }
}
Also used : BigInteger(java.math.BigInteger) ASN1Integer(com.android.org.bouncycastle.asn1.ASN1Integer) DERInteger(com.android.org.bouncycastle.asn1.DERInteger) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ProviderException(java.security.ProviderException) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) BigInteger(java.math.BigInteger) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) DERBitString(com.android.org.bouncycastle.asn1.DERBitString) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 27 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project XobotOS by xamarin.

the class PEMUtilities method crypt.

static byte[] crypt(boolean encrypt, Provider provider, byte[] bytes, char[] password, String dekAlgName, byte[] iv) throws IOException {
    AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
    String alg;
    String blockMode = "CBC";
    String padding = "PKCS5Padding";
    Key sKey;
    // Figure out block mode and padding.
    if (dekAlgName.endsWith("-CFB")) {
        blockMode = "CFB";
        padding = "NoPadding";
    }
    if (dekAlgName.endsWith("-ECB") || "DES-EDE".equals(dekAlgName) || "DES-EDE3".equals(dekAlgName)) {
        // ECB is actually the default (though seldom used) when OpenSSL
        // uses DES-EDE (des2) or DES-EDE3 (des3).
        blockMode = "ECB";
        paramSpec = null;
    }
    if (dekAlgName.endsWith("-OFB")) {
        blockMode = "OFB";
        padding = "NoPadding";
    }
    // Figure out algorithm and key size.
    if (dekAlgName.startsWith("DES-EDE")) {
        alg = "DESede";
        // "DES-EDE" is actually des2 in OpenSSL-speak!
        // "DES-EDE3" is des3.
        boolean des2 = !dekAlgName.startsWith("DES-EDE3");
        sKey = getKey(password, alg, 24, iv, des2);
    } else if (dekAlgName.startsWith("DES-")) {
        alg = "DES";
        sKey = getKey(password, alg, 8, iv);
    } else if (dekAlgName.startsWith("BF-")) {
        alg = "Blowfish";
        sKey = getKey(password, alg, 16, iv);
    } else if (dekAlgName.startsWith("RC2-")) {
        alg = "RC2";
        int keyBits = 128;
        if (dekAlgName.startsWith("RC2-40-")) {
            keyBits = 40;
        } else if (dekAlgName.startsWith("RC2-64-")) {
            keyBits = 64;
        }
        sKey = getKey(password, alg, keyBits / 8, iv);
        if (// ECB block mode
        paramSpec == null) {
            paramSpec = new RC2ParameterSpec(keyBits);
        } else {
            paramSpec = new RC2ParameterSpec(keyBits, iv);
        }
    } else if (dekAlgName.startsWith("AES-")) {
        alg = "AES";
        byte[] salt = iv;
        if (salt.length > 8) {
            salt = new byte[8];
            System.arraycopy(iv, 0, salt, 0, 8);
        }
        int keyBits;
        if (dekAlgName.startsWith("AES-128-")) {
            keyBits = 128;
        } else if (dekAlgName.startsWith("AES-192-")) {
            keyBits = 192;
        } else if (dekAlgName.startsWith("AES-256-")) {
            keyBits = 256;
        } else {
            throw new EncryptionException("unknown AES encryption with private key");
        }
        sKey = getKey(password, "AES", keyBits / 8, salt);
    } else {
        throw new EncryptionException("unknown encryption with private key");
    }
    String transformation = alg + "/" + blockMode + "/" + padding;
    try {
        Cipher c = Cipher.getInstance(transformation, provider);
        int mode = encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
        if (// ECB block mode
        paramSpec == null) {
            c.init(mode, sKey);
        } else {
            c.init(mode, sKey, paramSpec);
        }
        return c.doFinal(bytes);
    } catch (Exception e) {
        throw new EncryptionException("exception using cipher - please check password and data.", e);
    }
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key) SecretKey(javax.crypto.SecretKey) IOException(java.io.IOException)

Example 28 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project XobotOS by xamarin.

the class WrapCipherSpi method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    AlgorithmParameterSpec paramSpec = null;
    if (params != null) {
        for (int i = 0; i != availableSpecs.length; i++) {
            try {
                paramSpec = params.getParameterSpec(availableSpecs[i]);
                break;
            } catch (Exception e) {
            // try next spec
            }
        }
        if (paramSpec == null) {
            throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
        }
    }
    engineParams = params;
    engineInit(opmode, key, paramSpec, random);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 29 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project camel by apache.

the class CryptoDataFormat method createDataFormat.

@Override
protected DataFormat createDataFormat(RouteContext routeContext) {
    DataFormat cryptoFormat = super.createDataFormat(routeContext);
    if (ObjectHelper.isNotEmpty(keyRef)) {
        Key key = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), keyRef, Key.class);
        setProperty(routeContext.getCamelContext(), cryptoFormat, "key", key);
    }
    if (ObjectHelper.isNotEmpty(algorithmParameterRef)) {
        AlgorithmParameterSpec spec = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), algorithmParameterRef, AlgorithmParameterSpec.class);
        setProperty(routeContext.getCamelContext(), cryptoFormat, "AlgorithmParameterSpec", spec);
    }
    if (ObjectHelper.isNotEmpty(initVectorRef)) {
        byte[] iv = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), initVectorRef, byte[].class);
        setProperty(routeContext.getCamelContext(), cryptoFormat, "InitializationVector", iv);
    }
    return cryptoFormat;
}
Also used : DataFormat(org.apache.camel.spi.DataFormat) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key)

Example 30 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.

the class BaseStreamCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    AlgorithmParameterSpec paramSpec = null;
    if (params != null) {
        for (int i = 0; i != availableSpecs.length; i++) {
            try {
                paramSpec = params.getParameterSpec(availableSpecs[i]);
                break;
            } catch (Exception e) {
                continue;
            }
        }
        if (paramSpec == null) {
            throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
        }
    }
    engineInit(opmode, key, paramSpec, random);
    engineParams = params;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) DataLengthException(org.bouncycastle.crypto.DataLengthException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) InvalidKeyException(java.security.InvalidKeyException) ShortBufferException(javax.crypto.ShortBufferException)

Aggregations

AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)186 IvParameterSpec (javax.crypto.spec.IvParameterSpec)59 Cipher (javax.crypto.Cipher)55 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)51 InvalidKeyException (java.security.InvalidKeyException)42 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)37 SecureRandom (java.security.SecureRandom)27 SecretKey (javax.crypto.SecretKey)27 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)24 BigInteger (java.math.BigInteger)21 BadPaddingException (javax.crypto.BadPaddingException)21 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)20 RSAKeyGenParameterSpec (java.security.spec.RSAKeyGenParameterSpec)19 ShortBufferException (javax.crypto.ShortBufferException)19 Key (java.security.Key)18 SecretKeySpec (javax.crypto.spec.SecretKeySpec)18 AlgorithmParameters (java.security.AlgorithmParameters)17 KeyGenerator (javax.crypto.KeyGenerator)17 OAEPParameterSpec (javax.crypto.spec.OAEPParameterSpec)15 IOException (java.io.IOException)14