Search in sources :

Example 6 with ParametersWithIV

use of org.bouncycastle.crypto.params.ParametersWithIV in project robovm by robovm.

the class CCMBlockCipher method processPacket.

public byte[] processPacket(byte[] in, int inOff, int inLen) throws IllegalStateException, InvalidCipherTextException {
    // Need to keep the CTR and CBC Mac parts around and reset
    if (keyParam == null) {
        throw new IllegalStateException("CCM cipher unitialized.");
    }
    int n = nonce.length;
    int q = 15 - n;
    if (q < 4) {
        int limitLen = 1 << (8 * q);
        if (inLen >= limitLen) {
            throw new IllegalStateException("CCM packet too large for choice of q.");
        }
    }
    byte[] iv = new byte[blockSize];
    iv[0] = (byte) ((q - 1) & 0x7);
    System.arraycopy(nonce, 0, iv, 1, nonce.length);
    BlockCipher ctrCipher = new SICBlockCipher(cipher);
    ctrCipher.init(forEncryption, new ParametersWithIV(keyParam, iv));
    int index = inOff;
    int outOff = 0;
    byte[] output;
    if (forEncryption) {
        output = 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, output, 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, output, outOff, inLen - index);
        outOff += inLen - index;
        System.arraycopy(macBlock, 0, output, outOff, output.length - outOff);
    } else {
        output = 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 < output.length - blockSize) {
            ctrCipher.processBlock(in, index, output, outOff);
            outOff += blockSize;
            index += blockSize;
        }
        byte[] block = new byte[blockSize];
        System.arraycopy(in, index, block, 0, output.length - outOff);
        ctrCipher.processBlock(block, 0, block, 0);
        System.arraycopy(block, 0, output, outOff, output.length - outOff);
        byte[] calculatedMacBlock = new byte[blockSize];
        calculateMac(output, 0, output.length, calculatedMacBlock);
        if (!Arrays.constantTimeAreEqual(macBlock, calculatedMacBlock)) {
            throw new InvalidCipherTextException("mac check in CCM failed");
        }
    }
    return output;
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) BlockCipher(org.bouncycastle.crypto.BlockCipher)

Example 7 with ParametersWithIV

use of org.bouncycastle.crypto.params.ParametersWithIV in project spring-security by spring-projects.

the class BouncyCastleAesCbcBytesEncryptor method decrypt.

@Override
public byte[] decrypt(byte[] encryptedBytes) {
    byte[] iv = subArray(encryptedBytes, 0, this.ivGenerator.getKeyLength());
    encryptedBytes = subArray(encryptedBytes, this.ivGenerator.getKeyLength(), encryptedBytes.length);
    PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()), new PKCS7Padding());
    blockCipher.init(false, new ParametersWithIV(secretKey, iv));
    return process(blockCipher, encryptedBytes);
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) PKCS7Padding(org.bouncycastle.crypto.paddings.PKCS7Padding) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.bouncycastle.crypto.engines.AESFastEngine)

Example 8 with ParametersWithIV

use of org.bouncycastle.crypto.params.ParametersWithIV in project XobotOS by xamarin.

the class JCEMac method engineInit.

protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (key == null) {
        throw new InvalidKeyException("key is null");
    }
    if (key instanceof JCEPBEKey) {
        JCEPBEKey k = (JCEPBEKey) key;
        if (k.getParam() != null) {
            param = k.getParam();
        } else if (params instanceof PBEParameterSpec) {
            param = PBE.Util.makePBEMacParameters(k, params);
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
    } else if (params instanceof IvParameterSpec) {
        param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
    } else if (params == null) {
        param = new KeyParameter(key.getEncoded());
    } else {
        throw new InvalidAlgorithmParameterException("unknown parameter type.");
    }
    macEngine.init(param);
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) IvParameterSpec(javax.crypto.spec.IvParameterSpec) InvalidKeyException(java.security.InvalidKeyException) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 9 with ParametersWithIV

use of org.bouncycastle.crypto.params.ParametersWithIV in project XobotOS by xamarin.

the class JCEStreamCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    this.pbeSpec = null;
    this.pbeAlgorithm = null;
    this.engineParams = null;
    //
    if (!(key instanceof SecretKey)) {
        throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
    }
    if (key instanceof JCEPBEKey) {
        JCEPBEKey k = (JCEPBEKey) key;
        if (k.getOID() != null) {
            pbeAlgorithm = k.getOID().getId();
        } else {
            pbeAlgorithm = k.getAlgorithm();
        }
        if (k.getParam() != null) {
            param = k.getParam();
            pbeSpec = new PBEParameterSpec(k.getSalt(), k.getIterationCount());
        } else if (params instanceof PBEParameterSpec) {
            param = PBE.Util.makePBEParameters(k, params, cipher.getAlgorithmName());
            pbeSpec = (PBEParameterSpec) params;
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
        if (k.getIvSize() != 0) {
            ivParam = (ParametersWithIV) param;
        }
    } else if (params == null) {
        param = new KeyParameter(key.getEncoded());
    } else if (params instanceof IvParameterSpec) {
        param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
        ivParam = (ParametersWithIV) param;
    } else {
        throw new IllegalArgumentException("unknown parameter type.");
    }
    if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
        SecureRandom ivRandom = random;
        if (ivRandom == null) {
            ivRandom = new SecureRandom();
        }
        if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
            byte[] iv = new byte[ivLength];
            ivRandom.nextBytes(iv);
            param = new ParametersWithIV(param, iv);
            ivParam = (ParametersWithIV) param;
        } else {
            throw new InvalidAlgorithmParameterException("no IV set when one expected");
        }
    }
    switch(opmode) {
        case Cipher.ENCRYPT_MODE:
        case Cipher.WRAP_MODE:
            cipher.init(true, param);
            break;
        case Cipher.DECRYPT_MODE:
        case Cipher.UNWRAP_MODE:
            cipher.init(false, param);
            break;
        default:
            System.out.println("eeek!");
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) SecretKey(javax.crypto.SecretKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) IvParameterSpec(javax.crypto.spec.IvParameterSpec) InvalidKeyException(java.security.InvalidKeyException) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 10 with ParametersWithIV

use of org.bouncycastle.crypto.params.ParametersWithIV in project XobotOS by xamarin.

the class OpenSSLPBEParametersGenerator 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.
     * @exception IllegalArgumentException if keySize + ivSize is larger than the base hash size.
     */
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);
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) KeyParameter(org.bouncycastle.crypto.params.KeyParameter)

Aggregations

ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)42 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)21 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)9 IvParameterSpec (javax.crypto.spec.IvParameterSpec)9 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)9 CipherParameters (org.bouncycastle.crypto.CipherParameters)9 InvalidKeyException (java.security.InvalidKeyException)7 SecureRandom (java.security.SecureRandom)7 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)6 SecretKey (javax.crypto.SecretKey)5 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)4 AEADParameters (org.bouncycastle.crypto.params.AEADParameters)4 ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)4 InvalidParameterException (java.security.InvalidParameterException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 BadPaddingException (javax.crypto.BadPaddingException)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 ShortBufferException (javax.crypto.ShortBufferException)2 BlockCipher (org.bouncycastle.crypto.BlockCipher)2