use of org.bouncycastle.crypto.params.ParametersWithIV in project XobotOS by xamarin.
the class PKCS12ParametersGenerator 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.
*/
public CipherParameters generateDerivedParameters(int keySize, int ivSize) {
keySize = keySize / 8;
ivSize = ivSize / 8;
byte[] dKey = generateDerivedKey(KEY_MATERIAL, keySize);
byte[] iv = generateDerivedKey(IV_MATERIAL, ivSize);
return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), iv, 0, ivSize);
}
use of org.bouncycastle.crypto.params.ParametersWithIV in project XobotOS by xamarin.
the class PKCS5S2ParametersGenerator 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.
*/
public CipherParameters generateDerivedParameters(int keySize, int ivSize) {
keySize = keySize / 8;
ivSize = ivSize / 8;
byte[] dKey = generateDerivedKey(keySize + ivSize);
return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), dKey, keySize, ivSize);
}
use of org.bouncycastle.crypto.params.ParametersWithIV in project XobotOS by xamarin.
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 {
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();
cipher.init(encrypting, ivParam.getParameters());
} else {
reset();
cipher.init(encrypting, params);
}
}
use of org.bouncycastle.crypto.params.ParametersWithIV in project XobotOS by xamarin.
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();
associatedText = param.getAssociatedText();
macSize = param.getMacSize() / 8;
keyParam = param.getKey();
} else if (params instanceof ParametersWithIV) {
ParametersWithIV param = (ParametersWithIV) params;
nonce = param.getIV();
associatedText = null;
macSize = macBlock.length / 2;
keyParam = param.getParameters();
} else {
throw new IllegalArgumentException("invalid parameters passed to CCM");
}
}
use of org.bouncycastle.crypto.params.ParametersWithIV in project XobotOS by xamarin.
the class CCMBlockCipher method processPacket.
public byte[] processPacket(byte[] in, int inOff, int inLen) throws IllegalStateException, InvalidCipherTextException {
if (keyParam == null) {
throw new IllegalStateException("CCM cipher unitialized.");
}
BlockCipher ctrCipher = new SICBlockCipher(cipher);
byte[] iv = new byte[blockSize];
byte[] out;
iv[0] = (byte) (((15 - nonce.length) - 1) & 0x7);
System.arraycopy(nonce, 0, iv, 1, nonce.length);
ctrCipher.init(forEncryption, new ParametersWithIV(keyParam, iv));
if (forEncryption) {
int index = inOff;
int outOff = 0;
out = new byte[inLen + macSize];
calculateMac(in, inOff, inLen, macBlock);
// S0
ctrCipher.processBlock(macBlock, 0, macBlock, 0);
while (// S1...
index < inLen - blockSize) {
ctrCipher.processBlock(in, index, out, outOff);
outOff += blockSize;
index += blockSize;
}
byte[] block = new byte[blockSize];
System.arraycopy(in, index, block, 0, inLen - index);
ctrCipher.processBlock(block, 0, block, 0);
System.arraycopy(block, 0, out, outOff, inLen - index);
outOff += inLen - index;
System.arraycopy(macBlock, 0, out, outOff, out.length - outOff);
} else {
int index = inOff;
int outOff = 0;
out = new byte[inLen - macSize];
System.arraycopy(in, inOff + inLen - macSize, macBlock, 0, macSize);
ctrCipher.processBlock(macBlock, 0, macBlock, 0);
for (int i = macSize; i != macBlock.length; i++) {
macBlock[i] = 0;
}
while (outOff < out.length - blockSize) {
ctrCipher.processBlock(in, index, out, outOff);
outOff += blockSize;
index += blockSize;
}
byte[] block = new byte[blockSize];
System.arraycopy(in, index, block, 0, out.length - outOff);
ctrCipher.processBlock(block, 0, block, 0);
System.arraycopy(block, 0, out, outOff, out.length - outOff);
byte[] calculatedMacBlock = new byte[blockSize];
calculateMac(out, 0, out.length, calculatedMacBlock);
if (!Arrays.constantTimeAreEqual(macBlock, calculatedMacBlock)) {
throw new InvalidCipherTextException("mac check in CCM failed");
}
}
return out;
}
Aggregations