Search in sources :

Example 1 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter 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 2 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter 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 3 with KeyParameter

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

Example 4 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter 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 5 with KeyParameter

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

Aggregations

KeyParameter (org.bouncycastle.crypto.params.KeyParameter)117 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)51 AESEngine (org.bouncycastle.crypto.engines.AESEngine)37 CipherParameters (org.bouncycastle.crypto.CipherParameters)35 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)24 AEADParameters (org.bouncycastle.crypto.params.AEADParameters)22 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)21 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)21 IvParameterSpec (javax.crypto.spec.IvParameterSpec)19 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)16 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)16 AEADBlockCipher (org.bouncycastle.crypto.modes.AEADBlockCipher)16 InvalidKeyException (java.security.InvalidKeyException)13 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)12 SecretKeySpec (javax.crypto.spec.SecretKeySpec)12 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)12 PKCS5S2ParametersGenerator (org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator)12 SecureRandom (java.security.SecureRandom)11 SecretKey (javax.crypto.SecretKey)10 BlockCipher (org.bouncycastle.crypto.BlockCipher)8