use of org.bouncycastle.crypto.InvalidCipherTextException in project robovm by robovm.
the class CipherSpi method engineDoFinal.
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalBlockSizeException, BadPaddingException {
if (input != null) {
bOut.write(input, inputOffset, inputLen);
}
if (cipher instanceof RSABlindedEngine) {
if (bOut.size() > cipher.getInputBlockSize() + 1) {
throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
}
} else {
if (bOut.size() > cipher.getInputBlockSize()) {
throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
}
}
byte[] out;
try {
byte[] bytes = bOut.toByteArray();
out = cipher.processBlock(bytes, 0, bytes.length);
} catch (InvalidCipherTextException e) {
throw new BadPaddingException(e.getMessage());
} finally {
bOut.reset();
}
for (int i = 0; i != out.length; i++) {
output[outputOffset + i] = out[i];
}
return out.length;
}
use of org.bouncycastle.crypto.InvalidCipherTextException in project XobotOS by xamarin.
the class JCEBlockCipher method engineDoFinal.
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalBlockSizeException, BadPaddingException, ShortBufferException {
// BEGIN android-note
// added ShortBufferException to the throws statement
// END android-note
int len = 0;
// BEGIN android-added
int outputLen = cipher.getOutputSize(inputLen);
if (outputLen + outputOffset > output.length) {
throw new ShortBufferException("need at least " + outputLen + " bytes");
}
if (inputLen != 0) {
len = cipher.processBytes(input, inputOffset, inputLen, output, outputOffset);
}
try {
return (len + cipher.doFinal(output, outputOffset + len));
} catch (DataLengthException e) {
throw new IllegalBlockSizeException(e.getMessage());
} catch (InvalidCipherTextException e) {
throw new BadPaddingException(e.getMessage());
}
}
use of org.bouncycastle.crypto.InvalidCipherTextException in project XobotOS by xamarin.
the class JCERSACipher method engineDoFinal.
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalBlockSizeException, BadPaddingException {
if (input != null) {
bOut.write(input, inputOffset, inputLen);
}
if (cipher instanceof RSABlindedEngine) {
if (bOut.size() > cipher.getInputBlockSize() + 1) {
throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
}
} else {
if (bOut.size() > cipher.getInputBlockSize()) {
throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
}
}
byte[] out;
try {
byte[] bytes = bOut.toByteArray();
bOut.reset();
out = cipher.processBlock(bytes, 0, bytes.length);
} catch (InvalidCipherTextException e) {
throw new BadPaddingException(e.getMessage());
}
for (int i = 0; i != out.length; i++) {
output[outputOffset + i] = out[i];
}
return out.length;
}
use of org.bouncycastle.crypto.InvalidCipherTextException 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;
}
use of org.bouncycastle.crypto.InvalidCipherTextException in project XobotOS by xamarin.
the class ISO9796d1Encoding method decodeBlock.
/**
* @exception InvalidCipherTextException if the decrypted block is not a valid ISO 9796 bit string
*/
private byte[] decodeBlock(byte[] in, int inOff, int inLen) throws InvalidCipherTextException {
byte[] block = engine.processBlock(in, inOff, inLen);
int r = 1;
int t = (bitSize + 13) / 16;
BigInteger iS = new BigInteger(1, block);
BigInteger iR;
if (iS.mod(SIXTEEN).equals(SIX)) {
iR = iS;
} else if ((modulus.subtract(iS)).mod(SIXTEEN).equals(SIX)) {
iR = modulus.subtract(iS);
} else {
throw new InvalidCipherTextException("resulting integer iS or (modulus - iS) is not congruent to 6 mod 16");
}
block = convertOutputDecryptOnly(iR);
if ((block[block.length - 1] & 0x0f) != 0x6) {
throw new InvalidCipherTextException("invalid forcing byte in block");
}
block[block.length - 1] = (byte) (((block[block.length - 1] & 0xff) >>> 4) | ((inverse[(block[block.length - 2] & 0xff) >> 4]) << 4));
block[0] = (byte) ((shadows[(block[1] & 0xff) >>> 4] << 4) | shadows[block[1] & 0x0f]);
boolean boundaryFound = false;
int boundary = 0;
for (int i = block.length - 1; i >= block.length - 2 * t; i -= 2) {
int val = ((shadows[(block[i] & 0xff) >>> 4] << 4) | shadows[block[i] & 0x0f]);
if (((block[i - 1] ^ val) & 0xff) != 0) {
if (!boundaryFound) {
boundaryFound = true;
r = (block[i - 1] ^ val) & 0xff;
boundary = i - 1;
} else {
throw new InvalidCipherTextException("invalid tsums in block");
}
}
}
block[boundary] = 0;
byte[] nblock = new byte[(block.length - boundary) / 2];
for (int i = 0; i < nblock.length; i++) {
nblock[i] = block[2 * i + boundary + 1];
}
padBits = r - 1;
return nblock;
}
Aggregations