Search in sources :

Example 6 with DataLengthException

use of org.bouncycastle.crypto.DataLengthException in project XobotOS by xamarin.

the class CTSBlockCipher method doFinal.

/**
     * Process the last block in the buffer.
     *
     * @param out the array the block currently being held is copied into.
     * @param outOff the offset at which the copying starts.
     * @return the number of output bytes copied to out.
     * @exception DataLengthException if there is insufficient space in out for
     * the output.
     * @exception IllegalStateException if the underlying cipher is not
     * initialised.
     * @exception InvalidCipherTextException if cipher text decrypts wrongly (in
     * case the exception will never get thrown).
     */
public int doFinal(byte[] out, int outOff) throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    if (bufOff + outOff > out.length) {
        throw new DataLengthException("output buffer to small in doFinal");
    }
    int blockSize = cipher.getBlockSize();
    int len = bufOff - blockSize;
    byte[] block = new byte[blockSize];
    if (forEncryption) {
        cipher.processBlock(buf, 0, block, 0);
        if (bufOff < blockSize) {
            throw new DataLengthException("need at least one block of input for CTS");
        }
        for (int i = bufOff; i != buf.length; i++) {
            buf[i] = block[i - blockSize];
        }
        for (int i = blockSize; i != bufOff; i++) {
            buf[i] ^= block[i - blockSize];
        }
        if (cipher instanceof CBCBlockCipher) {
            BlockCipher c = ((CBCBlockCipher) cipher).getUnderlyingCipher();
            c.processBlock(buf, blockSize, out, outOff);
        } else {
            cipher.processBlock(buf, blockSize, out, outOff);
        }
        System.arraycopy(block, 0, out, outOff + blockSize, len);
    } else {
        byte[] lastBlock = new byte[blockSize];
        if (cipher instanceof CBCBlockCipher) {
            BlockCipher c = ((CBCBlockCipher) cipher).getUnderlyingCipher();
            c.processBlock(buf, 0, block, 0);
        } else {
            cipher.processBlock(buf, 0, block, 0);
        }
        for (int i = blockSize; i != bufOff; i++) {
            lastBlock[i - blockSize] = (byte) (block[i - blockSize] ^ buf[i]);
        }
        System.arraycopy(buf, blockSize, block, 0, len);
        cipher.processBlock(block, 0, out, outOff);
        System.arraycopy(lastBlock, 0, out, outOff + blockSize, len);
    }
    int offset = bufOff;
    reset();
    return offset;
}
Also used : BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) BlockCipher(org.bouncycastle.crypto.BlockCipher) DataLengthException(org.bouncycastle.crypto.DataLengthException)

Example 7 with DataLengthException

use of org.bouncycastle.crypto.DataLengthException in project 360-Engine-for-Android by 360.

the class RSACoreEngine method convertInput.

public BigInteger convertInput(byte[] in, int inOff, int inLen) {
    if (inLen > (getInputBlockSize() + 1)) {
        throw new DataLengthException("input too large for RSA cipher.");
    } else if (inLen == (getInputBlockSize() + 1) && !forEncryption) {
        throw new DataLengthException("input too large for RSA cipher.");
    }
    byte[] block;
    if (inOff != 0 || inLen != in.length) {
        block = new byte[inLen];
        System.arraycopy(in, inOff, block, 0, inLen);
    } else {
        block = in;
    }
    BigInteger res = new BigInteger(1, block);
    if (res.compareTo(key.getModulus()) >= 0) {
        throw new DataLengthException("input too large for RSA cipher.");
    }
    return res;
}
Also used : DataLengthException(org.bouncycastle.crypto.DataLengthException) BigInteger(java.math.BigInteger)

Example 8 with DataLengthException

use of org.bouncycastle.crypto.DataLengthException in project robovm by robovm.

the class BaseBlockCipher method engineDoFinal.

protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
    int len = 0;
    byte[] tmp = new byte[engineGetOutputSize(inputLen)];
    if (inputLen != 0) {
        len = cipher.processBytes(input, inputOffset, inputLen, tmp, 0);
    }
    try {
        len += cipher.doFinal(tmp, len);
    } catch (DataLengthException e) {
        throw new IllegalBlockSizeException(e.getMessage());
    } catch (InvalidCipherTextException e) {
        throw new BadPaddingException(e.getMessage());
    }
    if (len == tmp.length) {
        return tmp;
    }
    byte[] out = new byte[len];
    System.arraycopy(tmp, 0, out, 0, len);
    return out;
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) DataLengthException(org.bouncycastle.crypto.DataLengthException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException)

Example 9 with DataLengthException

use of org.bouncycastle.crypto.DataLengthException in project gocd by gocd.

the class EnvironmentVariableConfigTest method shouldErrorOutOnValidateWhenEncryptedValueIsForceChanged.

@Test
public void shouldErrorOutOnValidateWhenEncryptedValueIsForceChanged() throws InvalidCipherTextException {
    String plainText = "secure_value";
    String cipherText = "cipherText";
    when(goCipher.encrypt(plainText)).thenReturn(cipherText);
    when(goCipher.decrypt(cipherText)).thenThrow(new DataLengthException("last block incomplete in decryption"));
    EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(goCipher, "secure_key", plainText, true);
    environmentVariableConfig.validate(null);
    ConfigErrors error = environmentVariableConfig.errors();
    assertThat(error.isEmpty(), is(false));
    assertThat(error.on(EnvironmentVariableConfig.VALUE), is("Encrypted value for variable named 'secure_key' is invalid. This usually happens when the cipher text is modified to have an invalid value."));
}
Also used : DataLengthException(org.bouncycastle.crypto.DataLengthException) ConfigErrors(com.thoughtworks.go.domain.ConfigErrors) Test(org.junit.Test)

Example 10 with DataLengthException

use of org.bouncycastle.crypto.DataLengthException in project inbot-utils by Inbot.

the class AESUtils method encryptBouncyCastle.

private static String encryptBouncyCastle(SecretKey secret, String plainText) {
    try {
        // prepending with md5 hash allows us to do an integrity check on decrypt to prevent returning garbage if the decrypt key is incorrect
        String md5 = HashUtils.md5(plainText);
        plainText = md5 + plainText;
        // the iv acts as a per use salt, this ensures things encrypted with the same key always have a unique salt
        // 128 bit iv because NIST AES is standardized with 128 bit blocks and iv needs to match block size, even when using 256 bit key
        byte[] iv = new byte[16];
        SECURE_RANDOM.nextBytes(iv);
        // setup cipher parameters with key and IV
        byte[] key = secret.getEncoded();
        // setup AES cipher in CBC mode with PKCS7 padding
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
        cipher.reset();
        cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
        byte[] plainTextBuf = plainText.getBytes(StandardCharsets.UTF_8);
        byte[] buf = new byte[cipher.getOutputSize(plainTextBuf.length)];
        int len = cipher.processBytes(plainTextBuf, 0, plainTextBuf.length, buf, 0);
        len += cipher.doFinal(buf, len);
        // copy the encrypted part of the buffer to out
        byte[] out = new byte[len];
        System.arraycopy(buf, 0, out, 0, len);
        // iv$encrypted
        return byteArrayToHexString(iv) + "$" + new String(Base64.encodeBase64URLSafe(out), StandardCharsets.UTF_8);
    } catch (DataLengthException | InvalidCipherTextException e) {
        throw new IllegalStateException("cannot encrypt", e);
    }
}
Also used : PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.bouncycastle.crypto.engines.AESEngine) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) PKCS7Padding(org.bouncycastle.crypto.paddings.PKCS7Padding) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) DataLengthException(org.bouncycastle.crypto.DataLengthException) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher)

Aggregations

DataLengthException (org.bouncycastle.crypto.DataLengthException)10 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)4 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)4 BigInteger (java.math.BigInteger)3 BadPaddingException (javax.crypto.BadPaddingException)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 BlockCipher (org.bouncycastle.crypto.BlockCipher)2 AESEngine (org.bouncycastle.crypto.engines.AESEngine)2 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)2 PKCS7Padding (org.bouncycastle.crypto.paddings.PKCS7Padding)2 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)2 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)2 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)2 ConfigErrors (com.thoughtworks.go.domain.ConfigErrors)1 ShortBufferException (javax.crypto.ShortBufferException)1 CipherParameters (org.bouncycastle.crypto.CipherParameters)1 Test (org.junit.Test)1