Search in sources :

Example 11 with CipherParameters

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

the class DigestSignatureSpi method engineInitSign.

protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    if (!(privateKey instanceof RSAPrivateKey)) {
        throw new InvalidKeyException("Supplied key (" + getType(privateKey) + ") is not a RSAPrivateKey instance");
    }
    CipherParameters param = RSAUtil.generatePrivateKeyParameter((RSAPrivateKey) privateKey);
    digest.reset();
    cipher.init(true, param);
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) InvalidKeyException(java.security.InvalidKeyException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 12 with CipherParameters

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

the class Signature method engineInitSign.

protected void engineInitSign(PrivateKey privateKey, SecureRandom random) throws InvalidKeyException {
    CipherParameters param;
    if (privateKey instanceof ECKey) {
        param = ECUtil.generatePrivateKeyParameter(privateKey);
    } else {
        throw new InvalidKeyException("can't recognise key type in ECDSA based signer");
    }
    digest.reset();
    if (random != null) {
        signer.init(true, new ParametersWithRandom(param, random));
    } else {
        signer.init(true, param);
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) ECKey(org.bouncycastle.jce.interfaces.ECKey) InvalidKeyException(java.security.InvalidKeyException)

Example 13 with CipherParameters

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

the class BcKeyStoreSpi method engineLoad.

public void engineLoad(InputStream stream, char[] password) throws IOException {
    table.clear();
    if (// just initialising
    stream == null) {
        return;
    }
    DataInputStream dIn = new DataInputStream(stream);
    int version = dIn.readInt();
    if (version != STORE_VERSION) {
        if (version != 0 && version != 1) {
            throw new IOException("Wrong version of key store.");
        }
    }
    int saltLength = dIn.readInt();
    if (saltLength <= 0) {
        throw new IOException("Invalid salt detected");
    }
    byte[] salt = new byte[saltLength];
    dIn.readFully(salt);
    int iterationCount = dIn.readInt();
    //
    // we only do an integrity check if the password is provided.
    //
    HMac hMac = new HMac(new SHA1Digest());
    if (password != null && password.length != 0) {
        byte[] passKey = PBEParametersGenerator.PKCS12PasswordToBytes(password);
        PBEParametersGenerator pbeGen = new PKCS12ParametersGenerator(new SHA1Digest());
        pbeGen.init(passKey, salt, iterationCount);
        CipherParameters macParams;
        if (version != 2) {
            macParams = pbeGen.generateDerivedMacParameters(hMac.getMacSize());
        } else {
            macParams = pbeGen.generateDerivedMacParameters(hMac.getMacSize() * 8);
        }
        Arrays.fill(passKey, (byte) 0);
        hMac.init(macParams);
        MacInputStream mIn = new MacInputStream(dIn, hMac);
        loadStore(mIn);
        // Finalise our mac calculation
        byte[] mac = new byte[hMac.getMacSize()];
        hMac.doFinal(mac, 0);
        // TODO Should this actually be reading the remainder of the stream?
        // Read the original mac from the stream
        byte[] oldMac = new byte[hMac.getMacSize()];
        dIn.readFully(oldMac);
        if (!Arrays.constantTimeAreEqual(mac, oldMac)) {
            table.clear();
            throw new IOException("KeyStore integrity check failed.");
        }
    } else {
        loadStore(dIn);
        // TODO Should this actually be reading the remainder of the stream?
        // Parse the original mac from the stream too
        byte[] oldMac = new byte[hMac.getMacSize()];
        dIn.readFully(oldMac);
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) MacInputStream(org.bouncycastle.crypto.io.MacInputStream) PKCS12ParametersGenerator(org.bouncycastle.crypto.generators.PKCS12ParametersGenerator) HMac(org.bouncycastle.crypto.macs.HMac) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) PBEParametersGenerator(org.bouncycastle.crypto.PBEParametersGenerator)

Example 14 with CipherParameters

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

the class BaseMac 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 BCPBEKey) {
        BCPBEKey k = (BCPBEKey) 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 15 with CipherParameters

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

the class PBESecretKeyFactory method engineGenerateSecret.

protected SecretKey engineGenerateSecret(KeySpec keySpec) throws InvalidKeySpecException {
    if (keySpec instanceof PBEKeySpec) {
        PBEKeySpec pbeSpec = (PBEKeySpec) keySpec;
        CipherParameters param;
        if (pbeSpec.getSalt() == null) {
            return new BCPBEKey(this.algName, this.algOid, scheme, digest, keySize, ivSize, pbeSpec, null);
        }
        if (forCipher) {
            param = PBE.Util.makePBEParameters(pbeSpec, scheme, digest, keySize, ivSize);
        } else {
            param = PBE.Util.makePBEMacParameters(pbeSpec, scheme, digest, keySize);
        }
        return new BCPBEKey(this.algName, this.algOid, scheme, digest, keySize, ivSize, pbeSpec, param);
    }
    throw new InvalidKeySpecException("Invalid KeySpec");
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) PBEKeySpec(javax.crypto.spec.PBEKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Aggregations

CipherParameters (org.bouncycastle.crypto.CipherParameters)60 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)35 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)24 InvalidKeyException (java.security.InvalidKeyException)21 AESEngine (org.bouncycastle.crypto.engines.AESEngine)16 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)14 IvParameterSpec (javax.crypto.spec.IvParameterSpec)14 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)14 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)12 ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)12 SecureRandom (java.security.SecureRandom)11 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)11 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)9 BlockCipher (org.bouncycastle.crypto.BlockCipher)8 SecretKey (javax.crypto.SecretKey)7 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)7 PBEParametersGenerator (org.bouncycastle.crypto.PBEParametersGenerator)7 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)7 IOException (java.io.IOException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5