use of java.security.InvalidAlgorithmParameterException in project robovm by robovm.
the class BaseMac method engineInit.
protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
if (key == null) {
throw new InvalidKeyException("key is null");
}
if (key instanceof BCPBEKey) {
BCPBEKey k = (BCPBEKey) key;
if (k.getParam() != null) {
param = k.getParam();
} else if (params instanceof PBEParameterSpec) {
param = PBE.Util.makePBEMacParameters(k, params);
} else {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
} else if (params instanceof IvParameterSpec) {
param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
} else if (params == null) {
param = new KeyParameter(key.getEncoded());
} else {
throw new InvalidAlgorithmParameterException("unknown parameter type.");
}
macEngine.init(param);
}
use of java.security.InvalidAlgorithmParameterException 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;
}
use of java.security.InvalidAlgorithmParameterException in project robovm by robovm.
the class BaseWrapCipher 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);
}
use of java.security.InvalidAlgorithmParameterException in project robovm by robovm.
the class OpenSSLCipher method engineInitInternal.
private void engineInitInternal(int opmode, Key key, byte[] iv, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
if (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE) {
encrypting = true;
} else if (opmode == Cipher.DECRYPT_MODE || opmode == Cipher.UNWRAP_MODE) {
encrypting = false;
} else {
throw new InvalidParameterException("Unsupported opmode " + opmode);
}
if (!(key instanceof SecretKey)) {
throw new InvalidKeyException("Only SecretKey is supported");
}
final byte[] encodedKey = key.getEncoded();
if (encodedKey == null) {
throw new InvalidKeyException("key.getEncoded() == null");
}
checkSupportedKeySize(encodedKey.length);
final long cipherType = NativeCrypto.EVP_get_cipherbyname(getCipherName(encodedKey.length, mode));
if (cipherType == 0) {
throw new InvalidAlgorithmParameterException("Cannot find name for key length = " + (encodedKey.length * 8) + " and mode = " + mode);
}
final int ivLength = NativeCrypto.EVP_CIPHER_iv_length(cipherType);
if (iv == null && ivLength != 0) {
iv = new byte[ivLength];
if (encrypting) {
if (random == null) {
random = new SecureRandom();
}
random.nextBytes(iv);
}
} else if (iv != null && iv.length != ivLength) {
throw new InvalidAlgorithmParameterException("expected IV length of " + ivLength);
}
this.iv = iv;
if (supportsVariableSizeKey()) {
NativeCrypto.EVP_CipherInit_ex(cipherCtx.getContext(), cipherType, null, null, encrypting);
NativeCrypto.EVP_CIPHER_CTX_set_key_length(cipherCtx.getContext(), encodedKey.length);
NativeCrypto.EVP_CipherInit_ex(cipherCtx.getContext(), 0, encodedKey, iv, encrypting);
} else {
NativeCrypto.EVP_CipherInit_ex(cipherCtx.getContext(), cipherType, encodedKey, iv, encrypting);
}
// OpenSSL only supports PKCS5 Padding.
NativeCrypto.EVP_CIPHER_CTX_set_padding(cipherCtx.getContext(), padding == Padding.PKCS5PADDING);
modeBlockSize = NativeCrypto.EVP_CIPHER_CTX_block_size(cipherCtx.getContext());
calledUpdate = false;
}
use of java.security.InvalidAlgorithmParameterException in project robovm by robovm.
the class OpenSSLCipher method engineInit.
@Override
protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
final AlgorithmParameterSpec spec;
try {
spec = params.getParameterSpec(IvParameterSpec.class);
} catch (InvalidParameterSpecException e) {
throw new InvalidAlgorithmParameterException(e);
}
engineInit(opmode, key, spec, random);
}
Aggregations