Search in sources :

Example 76 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project xipki by xipki.

the class EmulatorP11Identity method aesGmac.

// TODO: check the correctness
private byte[] aesGmac(P11Params params, byte[] contentToSign) throws P11TokenException {
    if (params == null) {
        throw new P11TokenException("iv must not be null");
    }
    byte[] iv;
    if (params instanceof P11IVParams) {
        iv = ((P11IVParams) params).getIV();
    } else {
        throw new P11TokenException("params must be instanceof P11IVParams");
    }
    GMac gmac = new GMac(new GCMBlockCipher(new AESEngine()));
    ParametersWithIV paramsWithIv = new ParametersWithIV(new KeyParameter(signingKey.getEncoded()), iv);
    gmac.init(paramsWithIv);
    gmac.update(contentToSign, 0, contentToSign.length);
    byte[] signature = new byte[gmac.getMacSize()];
    gmac.doFinal(signature, 0);
    return signature;
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) AESEngine(org.bouncycastle.crypto.engines.AESEngine) P11TokenException(org.xipki.security.exception.P11TokenException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) GMac(org.bouncycastle.crypto.macs.GMac) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) P11IVParams(org.xipki.security.pkcs11.P11IVParams)

Example 77 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project Zom-Android by zom.

the class Downloader method setupInputStream.

public static InputStream setupInputStream(InputStream is, byte[] keyAndIv) {
    if (keyAndIv != null && keyAndIv.length == 48) {
        byte[] key = new byte[32];
        byte[] iv = new byte[16];
        System.arraycopy(keyAndIv, 0, iv, 0, 16);
        System.arraycopy(keyAndIv, 16, key, 0, 32);
        AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
        cipher.init(true, new AEADParameters(new KeyParameter(key), 128, iv));
        return new CipherInputStream(is, cipher);
    } else {
        return is;
    }
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) CipherInputStream(org.bouncycastle.crypto.io.CipherInputStream) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher)

Example 78 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project rxlib by RockyLOMO.

the class CryptBase method setIV.

protected void setIV(byte[] iv, boolean isEncrypt) {
    if (_ivLength == 0) {
        return;
    }
    if (isEncrypt) {
        _encryptIV = new byte[_ivLength];
        System.arraycopy(iv, 0, _encryptIV, 0, _ivLength);
        try {
            encCipher = getCipher(isEncrypt);
            ParametersWithIV parameterIV = new ParametersWithIV(new KeyParameter(_key.getEncoded()), _encryptIV);
            encCipher.init(isEncrypt, parameterIV);
        } catch (InvalidAlgorithmParameterException e) {
            logger.info(e.toString());
        }
    } else {
        _decryptIV = new byte[_ivLength];
        System.arraycopy(iv, 0, _decryptIV, 0, _ivLength);
        try {
            decCipher = getCipher(isEncrypt);
            ParametersWithIV parameterIV = new ParametersWithIV(new KeyParameter(_key.getEncoded()), _decryptIV);
            decCipher.init(isEncrypt, parameterIV);
        } catch (InvalidAlgorithmParameterException e) {
            logger.info(e.toString());
        }
    }
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter)

Example 79 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project photon-model by vmware.

the class EncryptorService method getCipher.

/*
     * Cipher settings
     */
private BufferedBlockCipher getCipher(boolean forEncryption) {
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
    cipher.init(forEncryption, new ParametersWithIV(new KeyParameter(this.keyBytes, IV_LENGTH, this.keyBytes.length - IV_LENGTH), this.keyBytes, 0, IV_LENGTH));
    return cipher;
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.bouncycastle.crypto.engines.AESEngine) PKCS7Padding(org.bouncycastle.crypto.paddings.PKCS7Padding) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher)

Example 80 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project samourai-wallet-android by Samourai-Wallet.

the class AESUtil method encryptWithSetMode.

public static String encryptWithSetMode(String cleartext, CharSequenceX password, int iterations, int mode, @Nullable BlockCipherPadding padding) throws DecryptionException, UnsupportedEncodingException {
    final int AESBlockSize = 4;
    if (password == null) {
        throw new DecryptionException("Password null");
    }
    // Use secure random to generate a 16 byte iv
    SecureRandom random = new SecureRandom();
    byte[] iv = new byte[AESBlockSize * 4];
    random.nextBytes(iv);
    byte[] clearbytes = cleartext.getBytes("UTF-8");
    PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
    generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password.toString().toCharArray()), iv, iterations);
    KeyParameter keyParam = (KeyParameter) generator.generateDerivedParameters(256);
    CipherParameters params = new ParametersWithIV(keyParam, iv);
    BlockCipher cipherMode;
    if (mode == MODE_CBC) {
        cipherMode = new CBCBlockCipher(new AESEngine());
    } else {
        // mode == MODE_OFB
        cipherMode = new OFBBlockCipher(new AESEngine(), 128);
    }
    BufferedBlockCipher cipher;
    if (padding != null) {
        cipher = new PaddedBufferedBlockCipher(cipherMode, padding);
    } else {
        cipher = new BufferedBlockCipher(cipherMode);
    }
    cipher.reset();
    cipher.init(true, params);
    byte[] outBuf = cipherData(cipher, clearbytes);
    // Append to IV to the output
    int len1 = iv.length;
    int len2 = outBuf.length;
    byte[] ivAppended = new byte[len1 + len2];
    System.arraycopy(iv, 0, ivAppended, 0, len1);
    System.arraycopy(outBuf, 0, ivAppended, len1, len2);
    // String ret = Base64.encodeBase64String(ivAppended);
    byte[] raw = Base64.encodeBase64(ivAppended);
    return new String(raw);
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) OFBBlockCipher(org.bouncycastle.crypto.modes.OFBBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) PKCS5S2ParametersGenerator(org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) BlockCipher(org.bouncycastle.crypto.BlockCipher) OFBBlockCipher(org.bouncycastle.crypto.modes.OFBBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) PBEParametersGenerator(org.bouncycastle.crypto.PBEParametersGenerator)

Aggregations

KeyParameter (org.bouncycastle.crypto.params.KeyParameter)119 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