use of org.bouncycastle.crypto.params.ParametersWithIV in project robovm by robovm.
the class BaseWrapCipher method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
if (key instanceof BCPBEKey) {
BCPBEKey k = (BCPBEKey) key;
if (params instanceof PBEParameterSpec) {
param = PBE.Util.makePBEParameters(k, params, wrapEngine.getAlgorithmName());
} else if (k.getParam() != null) {
param = k.getParam();
} else {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
} else {
param = new KeyParameter(key.getEncoded());
}
if (params instanceof IvParameterSpec) {
IvParameterSpec iv = (IvParameterSpec) params;
param = new ParametersWithIV(param, iv.getIV());
}
if (param instanceof KeyParameter && ivSize != 0) {
iv = new byte[ivSize];
random.nextBytes(iv);
param = new ParametersWithIV(param, iv);
}
switch(opmode) {
case Cipher.WRAP_MODE:
wrapEngine.init(true, param);
break;
case Cipher.UNWRAP_MODE:
wrapEngine.init(false, param);
break;
case Cipher.ENCRYPT_MODE:
case Cipher.DECRYPT_MODE:
throw new IllegalArgumentException("engine only valid for wrapping");
default:
System.out.println("eeek!");
}
}
use of org.bouncycastle.crypto.params.ParametersWithIV in project robovm by robovm.
the class CBCBlockCipher method init.
/**
* Initialise the cipher and, possibly, the initialisation vector (IV).
* If an IV isn't passed as part of the parameter, the IV will be all zeros.
*
* @param encrypting if true the cipher is initialised for
* encryption, if false for decryption.
* @param params the key and other data required by the cipher.
* @exception IllegalArgumentException if the params argument is
* inappropriate.
*/
public void init(boolean encrypting, CipherParameters params) throws IllegalArgumentException {
boolean oldEncrypting = this.encrypting;
this.encrypting = encrypting;
if (params instanceof ParametersWithIV) {
ParametersWithIV ivParam = (ParametersWithIV) params;
byte[] iv = ivParam.getIV();
if (iv.length != blockSize) {
throw new IllegalArgumentException("initialisation vector must be the same length as block size");
}
System.arraycopy(iv, 0, IV, 0, iv.length);
reset();
// if null it's an IV changed only.
if (ivParam.getParameters() != null) {
cipher.init(encrypting, ivParam.getParameters());
} else if (oldEncrypting != encrypting) {
throw new IllegalArgumentException("cannot change encrypting state without providing key.");
}
} else {
reset();
// if it's null, key is to be reused.
if (params != null) {
cipher.init(encrypting, params);
} else if (oldEncrypting != encrypting) {
throw new IllegalArgumentException("cannot change encrypting state without providing key.");
}
}
}
use of org.bouncycastle.crypto.params.ParametersWithIV in project robovm by robovm.
the class CCMBlockCipher method init.
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
this.forEncryption = forEncryption;
if (params instanceof AEADParameters) {
AEADParameters param = (AEADParameters) params;
nonce = param.getNonce();
initialAssociatedText = param.getAssociatedText();
macSize = param.getMacSize() / 8;
keyParam = param.getKey();
} else if (params instanceof ParametersWithIV) {
ParametersWithIV param = (ParametersWithIV) params;
nonce = param.getIV();
initialAssociatedText = null;
macSize = macBlock.length / 2;
keyParam = param.getParameters();
} else {
throw new IllegalArgumentException("invalid parameters passed to CCM");
}
if (nonce == null || nonce.length < 7 || nonce.length > 13) {
throw new IllegalArgumentException("nonce must have length from 7 to 13 octets");
}
}
use of org.bouncycastle.crypto.params.ParametersWithIV in project robovm by robovm.
the class CFBBlockCipher method init.
/**
* Initialise the cipher and, possibly, the initialisation vector (IV).
* If an IV isn't passed as part of the parameter, the IV will be all zeros.
* An IV which is too short is handled in FIPS compliant fashion.
*
* @param encrypting if true the cipher is initialised for
* encryption, if false for decryption.
* @param params the key and other data required by the cipher.
* @exception IllegalArgumentException if the params argument is
* inappropriate.
*/
public void init(boolean encrypting, CipherParameters params) throws IllegalArgumentException {
this.encrypting = encrypting;
if (params instanceof ParametersWithIV) {
ParametersWithIV ivParam = (ParametersWithIV) params;
byte[] iv = ivParam.getIV();
if (iv.length < IV.length) {
// prepend the supplied IV with zeros (per FIPS PUB 81)
System.arraycopy(iv, 0, IV, IV.length - iv.length, iv.length);
for (int i = 0; i < IV.length - iv.length; i++) {
IV[i] = 0;
}
} else {
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 {
reset();
// if it's null, key is to be reused.
if (params != null) {
cipher.init(true, params);
}
}
}
use of org.bouncycastle.crypto.params.ParametersWithIV in project robovm by robovm.
the class GCMBlockCipher method init.
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
this.forEncryption = forEncryption;
this.macBlock = null;
KeyParameter keyParam;
if (params instanceof AEADParameters) {
AEADParameters param = (AEADParameters) params;
nonce = param.getNonce();
initialAssociatedText = param.getAssociatedText();
int macSizeBits = param.getMacSize();
if (macSizeBits < 96 || macSizeBits > 128 || macSizeBits % 8 != 0) {
throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
}
macSize = macSizeBits / 8;
keyParam = param.getKey();
} else if (params instanceof ParametersWithIV) {
ParametersWithIV param = (ParametersWithIV) params;
nonce = param.getIV();
initialAssociatedText = null;
macSize = 16;
keyParam = (KeyParameter) param.getParameters();
} else {
throw new IllegalArgumentException("invalid parameters passed to GCM");
}
int bufLength = forEncryption ? BLOCK_SIZE : (BLOCK_SIZE + macSize);
this.bufBlock = new byte[bufLength];
if (nonce == null || nonce.length < 1) {
throw new IllegalArgumentException("IV must be at least 1 byte");
}
// if keyParam is null we're reusing the last key.
if (keyParam != null) {
cipher.init(true, keyParam);
this.H = new byte[BLOCK_SIZE];
cipher.processBlock(H, 0, H, 0);
// GCMMultiplier tables don't change unless the key changes (and are expensive to init)
multiplier.init(H);
exp = null;
}
this.J0 = new byte[BLOCK_SIZE];
if (nonce.length == 12) {
System.arraycopy(nonce, 0, J0, 0, nonce.length);
this.J0[BLOCK_SIZE - 1] = 0x01;
} else {
gHASH(J0, nonce, nonce.length);
byte[] X = new byte[BLOCK_SIZE];
Pack.longToBigEndian((long) nonce.length * 8, X, 8);
gHASHBlock(J0, X);
}
this.S = new byte[BLOCK_SIZE];
this.S_at = new byte[BLOCK_SIZE];
this.S_atPre = new byte[BLOCK_SIZE];
this.atBlock = new byte[BLOCK_SIZE];
this.atBlockPos = 0;
this.atLength = 0;
this.atLengthPre = 0;
this.counter = Arrays.clone(J0);
this.bufOff = 0;
this.totalLength = 0;
if (initialAssociatedText != null) {
processAADBytes(initialAssociatedText, 0, initialAssociatedText.length);
}
}
Aggregations