Search in sources :

Example 1 with InvalidCipherTextException

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;
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) RSABlindedEngine(org.bouncycastle.crypto.engines.RSABlindedEngine) BadPaddingException(javax.crypto.BadPaddingException)

Example 2 with InvalidCipherTextException

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());
    }
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) DataLengthException(org.bouncycastle.crypto.DataLengthException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException)

Example 3 with InvalidCipherTextException

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;
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) RSABlindedEngine(org.bouncycastle.crypto.engines.RSABlindedEngine) BadPaddingException(javax.crypto.BadPaddingException)

Example 4 with InvalidCipherTextException

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;
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) BlockCipher(org.bouncycastle.crypto.BlockCipher)

Example 5 with InvalidCipherTextException

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;
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) BigInteger(java.math.BigInteger)

Aggregations

InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)46 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)13 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)13 AESEngine (org.bouncycastle.crypto.engines.AESEngine)12 CipherParameters (org.bouncycastle.crypto.CipherParameters)11 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)9 IOException (java.io.IOException)7 DataLengthException (org.bouncycastle.crypto.DataLengthException)7 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)7 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)7 GoCipher (com.thoughtworks.go.security.GoCipher)6 Test (org.junit.Test)6 BadPaddingException (javax.crypto.BadPaddingException)5 BlockCipher (org.bouncycastle.crypto.BlockCipher)5 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)5 UrlArgument (com.thoughtworks.go.util.command.UrlArgument)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ExtendedInvalidCipherTextException (org.openremote.agent.protocol.bluetooth.mesh.utils.ExtendedInvalidCipherTextException)4 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)3 SecretKeySpec (javax.crypto.spec.SecretKeySpec)3