Search in sources :

Example 6 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)

Example 7 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 8 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 9 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 10 with InvalidCipherTextException

use of org.bouncycastle.crypto.InvalidCipherTextException in project oxAuth by GluuFederation.

the class JweDecrypterImpl method decryptEncryptionKey.

@Override
public byte[] decryptEncryptionKey(String encodedEncryptedKey) throws InvalidJweException {
    if (getKeyEncryptionAlgorithm() == null) {
        throw new InvalidJweException("The key encryption algorithm is null");
    }
    if (encodedEncryptedKey == null) {
        throw new InvalidJweException("The encoded encryption key is null");
    }
    try {
        if (getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.RSA_OAEP || getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.RSA1_5) {
            if (rsaPrivateKey == null && privateKey == null) {
                throw new InvalidJweException("The RSA private key is null");
            }
            //Cipher cipher = Cipher.getInstance(getKeyEncryptionAlgorithm().getAlgorithm(), "BC");
            Cipher cipher = Cipher.getInstance(getKeyEncryptionAlgorithm().getAlgorithm());
            if (rsaPrivateKey != null) {
                KeyFactory keyFactory = KeyFactory.getInstance(getKeyEncryptionAlgorithm().getFamily(), "BC");
                RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
                java.security.interfaces.RSAPrivateKey privKey = (java.security.interfaces.RSAPrivateKey) keyFactory.generatePrivate(privKeySpec);
                cipher.init(Cipher.DECRYPT_MODE, privKey);
            } else {
                cipher.init(Cipher.DECRYPT_MODE, privateKey);
            }
            byte[] decryptedKey = cipher.doFinal(Base64Util.base64urldecode(encodedEncryptedKey));
            return decryptedKey;
        } else if (getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.A128KW || getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.A256KW) {
            if (sharedSymmetricKey == null) {
                throw new InvalidJweException("The shared symmetric key is null");
            }
            if (sharedSymmetricKey.length != 16) {
                // 128 bit
                MessageDigest sha = MessageDigest.getInstance("SHA-1");
                sharedSymmetricKey = sha.digest(sharedSymmetricKey);
                sharedSymmetricKey = Arrays.copyOf(sharedSymmetricKey, 16);
            }
            byte[] encryptedKey = Base64Util.base64urldecode(encodedEncryptedKey);
            SecretKeySpec keyEncryptionKey = new SecretKeySpec(sharedSymmetricKey, "AES");
            AESWrapEngine aesWrapEngine = new AESWrapEngine();
            CipherParameters params = new KeyParameter(keyEncryptionKey.getEncoded());
            aesWrapEngine.init(false, params);
            byte[] decryptedKey = aesWrapEngine.unwrap(encryptedKey, 0, encryptedKey.length);
            return decryptedKey;
        } else {
            throw new InvalidJweException("The key encryption algorithm is not supported");
        }
    } catch (NoSuchPaddingException e) {
        throw new InvalidJweException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new InvalidJweException(e);
    } catch (IllegalBlockSizeException e) {
        throw new InvalidJweException(e);
    } catch (BadPaddingException e) {
        throw new InvalidJweException(e);
    } catch (NoSuchProviderException e) {
        throw new InvalidJweException(e);
    } catch (InvalidKeyException e) {
        throw new InvalidJweException(e);
    } catch (InvalidKeySpecException e) {
        throw new InvalidJweException(e);
    } catch (InvalidCipherTextException e) {
        throw new InvalidJweException(e);
    }
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) CipherParameters(org.bouncycastle.crypto.CipherParameters) java.security(java.security) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) AESWrapEngine(org.bouncycastle.crypto.engines.AESWrapEngine) BlockCipher(org.bouncycastle.crypto.BlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) RSAPrivateKey(org.xdi.oxauth.model.crypto.signature.RSAPrivateKey) InvalidJweException(org.xdi.oxauth.model.exception.InvalidJweException)

Aggregations

InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)19 GoCipher (com.thoughtworks.go.security.GoCipher)6 Test (org.junit.Test)6 BlockCipher (org.bouncycastle.crypto.BlockCipher)5 UrlArgument (com.thoughtworks.go.util.command.UrlArgument)4 BadPaddingException (javax.crypto.BadPaddingException)4 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)4 SecretKeySpec (javax.crypto.spec.SecretKeySpec)3 CipherParameters (org.bouncycastle.crypto.CipherParameters)3 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)3 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)3 InvalidJweException (org.xdi.oxauth.model.exception.InvalidJweException)3 SvnMaterial (com.thoughtworks.go.config.materials.svn.SvnMaterial)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 IvParameterSpec (javax.crypto.spec.IvParameterSpec)2 DataLengthException (org.bouncycastle.crypto.DataLengthException)2 AESEngine (org.bouncycastle.crypto.engines.AESEngine)2 RSABlindedEngine (org.bouncycastle.crypto.engines.RSABlindedEngine)2