use of org.bouncycastle.crypto.params.ParametersWithIV in project robovm by robovm.
the class DESedeWrapEngine method init.
/**
* Method init
*
* @param forWrapping
* @param param
*/
public void init(boolean forWrapping, CipherParameters param) {
this.forWrapping = forWrapping;
this.engine = new CBCBlockCipher(new DESedeEngine());
SecureRandom sr;
if (param instanceof ParametersWithRandom) {
ParametersWithRandom pr = (ParametersWithRandom) param;
param = pr.getParameters();
sr = pr.getRandom();
} else {
sr = new SecureRandom();
}
if (param instanceof KeyParameter) {
this.param = (KeyParameter) param;
if (this.forWrapping) {
// Hm, we have no IV but we want to wrap ?!?
// well, then we have to create our own IV.
this.iv = new byte[8];
sr.nextBytes(iv);
this.paramPlusIV = new ParametersWithIV(this.param, this.iv);
}
} else if (param instanceof ParametersWithIV) {
this.paramPlusIV = (ParametersWithIV) param;
this.iv = this.paramPlusIV.getIV();
this.param = (KeyParameter) this.paramPlusIV.getParameters();
if (this.forWrapping) {
if ((this.iv == null) || (this.iv.length != 8)) {
throw new IllegalArgumentException("IV is not 8 octets");
}
} else {
throw new IllegalArgumentException("You should not supply an IV for unwrapping");
}
}
}
use of org.bouncycastle.crypto.params.ParametersWithIV in project robovm by robovm.
the class PKCS5S1ParametersGenerator method generateDerivedParameters.
/**
* Generate a key with initialisation vector parameter derived from
* the password, salt, and iteration count we are currently initialised
* with.
*
* @param keySize the size of the key we want (in bits)
* @param ivSize the size of the iv we want (in bits)
* @return a ParametersWithIV object.
* @exception IllegalArgumentException if keySize + ivSize is larger than the base hash size.
*/
public CipherParameters generateDerivedParameters(int keySize, int ivSize) {
keySize = keySize / 8;
ivSize = ivSize / 8;
if ((keySize + ivSize) > digest.getDigestSize()) {
throw new IllegalArgumentException("Can't generate a derived key " + (keySize + ivSize) + " bytes long.");
}
byte[] dKey = generateDerivedKey();
return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), dKey, keySize, ivSize);
}
use of org.bouncycastle.crypto.params.ParametersWithIV in project robovm by robovm.
the class SICBlockCipher method init.
public void init(//ignored by this CTR mode
boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
if (params instanceof ParametersWithIV) {
ParametersWithIV ivParam = (ParametersWithIV) params;
byte[] iv = ivParam.getIV();
System.arraycopy(iv, 0, IV, 0, IV.length);
reset();
// if null it's an IV changed only.
if (ivParam.getParameters() != null) {
cipher.init(true, ivParam.getParameters());
}
} else {
throw new IllegalArgumentException("SIC mode requires ParametersWithIV");
}
}
use of org.bouncycastle.crypto.params.ParametersWithIV in project spring-security by spring-projects.
the class BouncyCastleAesCbcBytesEncryptor method encrypt.
@Override
public byte[] encrypt(byte[] bytes) {
byte[] iv = this.ivGenerator.generateKey();
PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()), new PKCS7Padding());
blockCipher.init(true, new ParametersWithIV(secretKey, iv));
byte[] encrypted = process(blockCipher, bytes);
return iv != null ? concatenate(iv, encrypted) : encrypted;
}
use of org.bouncycastle.crypto.params.ParametersWithIV in project XobotOS by xamarin.
the class JCEBlockCipher method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.engineParams = null;
//
if (!(key instanceof SecretKey)) {
throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
}
//
if (key instanceof JCEPBEKey) {
JCEPBEKey k = (JCEPBEKey) key;
if (k.getOID() != null) {
pbeAlgorithm = k.getOID().getId();
} else {
pbeAlgorithm = k.getAlgorithm();
}
if (k.getParam() != null) {
param = k.getParam();
pbeSpec = new PBEParameterSpec(k.getSalt(), k.getIterationCount());
} else if (params instanceof PBEParameterSpec) {
pbeSpec = (PBEParameterSpec) params;
param = PBE.Util.makePBEParameters(k, params, cipher.getUnderlyingCipher().getAlgorithmName());
} else {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
if (param instanceof ParametersWithIV) {
ivParam = (ParametersWithIV) param;
}
} else if (params == null) {
param = new KeyParameter(key.getEncoded());
} else if (params instanceof IvParameterSpec) {
if (ivLength != 0) {
IvParameterSpec p = (IvParameterSpec) params;
if (p.getIV().length != ivLength && !isAEADModeName(modeName)) {
throw new InvalidAlgorithmParameterException("IV must be " + ivLength + " bytes long.");
}
param = new ParametersWithIV(new KeyParameter(key.getEncoded()), p.getIV());
ivParam = (ParametersWithIV) param;
} else {
if (modeName != null && modeName.equals("ECB")) {
throw new InvalidAlgorithmParameterException("ECB mode does not use an IV");
}
param = new KeyParameter(key.getEncoded());
}
} else // BEGIN android-removed
// else if (params instanceof GOST28147ParameterSpec)
// {
// GOST28147ParameterSpec gost28147Param = (GOST28147ParameterSpec)params;
//
// param = new ParametersWithSBox(
// new KeyParameter(key.getEncoded()), ((GOST28147ParameterSpec)params).getSbox());
//
// if (gost28147Param.getIV() != null && ivLength != 0)
// {
// param = new ParametersWithIV(param, gost28147Param.getIV());
// ivParam = (ParametersWithIV)param;
// }
// }
// else if (params instanceof RC2ParameterSpec)
// {
// RC2ParameterSpec rc2Param = (RC2ParameterSpec)params;
//
// param = new RC2Parameters(key.getEncoded(), ((RC2ParameterSpec)params).getEffectiveKeyBits());
//
// if (rc2Param.getIV() != null && ivLength != 0)
// {
// param = new ParametersWithIV(param, rc2Param.getIV());
// ivParam = (ParametersWithIV)param;
// }
// }
// else if (params instanceof RC5ParameterSpec)
// {
// RC5ParameterSpec rc5Param = (RC5ParameterSpec)params;
//
// param = new RC5Parameters(key.getEncoded(), ((RC5ParameterSpec)params).getRounds());
// if (baseEngine.getAlgorithmName().startsWith("RC5"))
// {
// if (baseEngine.getAlgorithmName().equals("RC5-32"))
// {
// if (rc5Param.getWordSize() != 32)
// {
// throw new InvalidAlgorithmParameterException("RC5 already set up for a word size of 32 not " + rc5Param.getWordSize() + ".");
// }
// }
// else if (baseEngine.getAlgorithmName().equals("RC5-64"))
// {
// if (rc5Param.getWordSize() != 64)
// {
// throw new InvalidAlgorithmParameterException("RC5 already set up for a word size of 64 not " + rc5Param.getWordSize() + ".");
// }
// }
// }
// else
// {
// throw new InvalidAlgorithmParameterException("RC5 parameters passed to a cipher that is not RC5.");
// }
// if ((rc5Param.getIV() != null) && (ivLength != 0))
// {
// param = new ParametersWithIV(param, rc5Param.getIV());
// ivParam = (ParametersWithIV)param;
// }
// }
// END android-removed
{
throw new InvalidAlgorithmParameterException("unknown parameter type.");
}
if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
SecureRandom ivRandom = random;
if (ivRandom == null) {
ivRandom = new SecureRandom();
}
if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
byte[] iv = new byte[ivLength];
ivRandom.nextBytes(iv);
param = new ParametersWithIV(param, iv);
ivParam = (ParametersWithIV) param;
} else if (cipher.getUnderlyingCipher().getAlgorithmName().indexOf("PGPCFB") < 0) {
throw new InvalidAlgorithmParameterException("no IV set when one expected");
}
}
if (random != null && padded) {
param = new ParametersWithRandom(param, random);
}
try {
switch(opmode) {
case Cipher.ENCRYPT_MODE:
case Cipher.WRAP_MODE:
cipher.init(true, param);
break;
case Cipher.DECRYPT_MODE:
case Cipher.UNWRAP_MODE:
cipher.init(false, param);
break;
default:
throw new InvalidParameterException("unknown opmode " + opmode + " passed");
}
} catch (Exception e) {
throw new InvalidKeyException(e.getMessage());
}
}
Aggregations