Search in sources :

Example 11 with ParametersWithIV

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

the class PKCS12ParametersGenerator 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.
     */
public CipherParameters generateDerivedParameters(int keySize, int ivSize) {
    keySize = keySize / 8;
    ivSize = ivSize / 8;
    byte[] dKey = generateDerivedKey(KEY_MATERIAL, keySize);
    byte[] iv = generateDerivedKey(IV_MATERIAL, ivSize);
    return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), iv, 0, ivSize);
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) KeyParameter(org.bouncycastle.crypto.params.KeyParameter)

Example 12 with ParametersWithIV

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

the class PKCS5S2ParametersGenerator 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.
     */
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)

Example 13 with ParametersWithIV

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

the class CBCBlockCipher method init.

/**
     * Initialise the cipher and, possibly, the initialisation vector (IV).
     * If an IV isn't passed as part of the parameter, the IV will be all zeros.
     *
     * @param encrypting if true the cipher is initialised for
     *  encryption, if false for decryption.
     * @param params the key and other data required by the cipher.
     * @exception IllegalArgumentException if the params argument is
     * inappropriate.
     */
public void init(boolean encrypting, CipherParameters params) throws IllegalArgumentException {
    this.encrypting = encrypting;
    if (params instanceof ParametersWithIV) {
        ParametersWithIV ivParam = (ParametersWithIV) params;
        byte[] iv = ivParam.getIV();
        if (iv.length != blockSize) {
            throw new IllegalArgumentException("initialisation vector must be the same length as block size");
        }
        System.arraycopy(iv, 0, IV, 0, iv.length);
        reset();
        cipher.init(encrypting, ivParam.getParameters());
    } else {
        reset();
        cipher.init(encrypting, params);
    }
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV)

Example 14 with ParametersWithIV

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

the class CCMBlockCipher method init.

public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
    this.forEncryption = forEncryption;
    if (params instanceof AEADParameters) {
        AEADParameters param = (AEADParameters) params;
        nonce = param.getNonce();
        associatedText = param.getAssociatedText();
        macSize = param.getMacSize() / 8;
        keyParam = param.getKey();
    } else if (params instanceof ParametersWithIV) {
        ParametersWithIV param = (ParametersWithIV) params;
        nonce = param.getIV();
        associatedText = null;
        macSize = macBlock.length / 2;
        keyParam = param.getParameters();
    } else {
        throw new IllegalArgumentException("invalid parameters passed to CCM");
    }
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) AEADParameters(org.bouncycastle.crypto.params.AEADParameters)

Example 15 with ParametersWithIV

use of org.bouncycastle.crypto.params.ParametersWithIV 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)

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