Search in sources :

Example 36 with CipherParameters

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

the class JDKDigestSignature method engineInitVerify.

protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    if (!(publicKey instanceof RSAPublicKey)) {
        throw new InvalidKeyException("Supplied key (" + getType(publicKey) + ") is not a RSAPublicKey instance");
    }
    CipherParameters param = RSAUtil.generatePublicKeyParameter((RSAPublicKey) publicKey);
    digest.reset();
    cipher.init(false, param);
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) RSAPublicKey(java.security.interfaces.RSAPublicKey) InvalidKeyException(java.security.InvalidKeyException)

Example 37 with CipherParameters

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

the class WrapCipherSpi method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (key instanceof JCEPBEKey) {
        JCEPBEKey k = (JCEPBEKey) key;
        if (params instanceof PBEParameterSpec) {
            param = PBE.Util.makePBEParameters(k, params, wrapEngine.getAlgorithmName());
        } else if (k.getParam() != null) {
            param = k.getParam();
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
    } else {
        param = new KeyParameter(key.getEncoded());
    }
    if (params instanceof javax.crypto.spec.IvParameterSpec) {
        IvParameterSpec iv = (IvParameterSpec) params;
        param = new ParametersWithIV(param, iv.getIV());
    }
    if (param instanceof KeyParameter && ivSize != 0) {
        iv = new byte[ivSize];
        random.nextBytes(iv);
        param = new ParametersWithIV(param, iv);
    }
    switch(opmode) {
        case Cipher.WRAP_MODE:
            wrapEngine.init(true, param);
            break;
        case Cipher.UNWRAP_MODE:
            wrapEngine.init(false, param);
            break;
        case Cipher.ENCRYPT_MODE:
        case Cipher.DECRYPT_MODE:
            throw new IllegalArgumentException("engine only valid for wrapping");
        default:
            System.out.println("eeek!");
    }
}
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) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 38 with CipherParameters

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

the class KeyAgreement method engineDoPhase.

protected Key engineDoPhase(Key key, boolean lastPhase) throws InvalidKeyException, IllegalStateException {
    if (parameters == null) {
        throw new IllegalStateException(kaAlgorithm + " not initialised.");
    }
    if (!lastPhase) {
        throw new IllegalStateException(kaAlgorithm + " can only be between two parties.");
    }
    CipherParameters pubKey;
    // BEGIN android-removed
    // if (agreement instanceof ECMQVBasicAgreement)
    // {
    //     if (!(key instanceof MQVPublicKey))
    //     {
    //         throw new InvalidKeyException(kaAlgorithm + " key agreement requires "
    //             + getSimpleName(MQVPublicKey.class) + " for doPhase");
    //     }
    //
    //     MQVPublicKey mqvPubKey = (MQVPublicKey)key;
    //     ECPublicKeyParameters staticKey = (ECPublicKeyParameters)
    //         ECUtil.generatePublicKeyParameter(mqvPubKey.getStaticKey());
    //     ECPublicKeyParameters ephemKey = (ECPublicKeyParameters)
    //         ECUtil.generatePublicKeyParameter(mqvPubKey.getEphemeralKey());
    //
    //     pubKey = new MQVPublicParameters(staticKey, ephemKey);
    //
    //     // TODO Validate that all the keys are using the same parameters?
    // }
    // else
    // END android-removed
    {
        if (!(key instanceof ECPublicKey)) {
            throw new InvalidKeyException(kaAlgorithm + " key agreement requires " + getSimpleName(ECPublicKey.class) + " for doPhase");
        }
        pubKey = ECUtil.generatePublicKeyParameter((PublicKey) key);
    // TODO Validate that all the keys are using the same parameters?
    }
    result = agreement.calculateAgreement(pubKey);
    return null;
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ECPublicKey(org.bouncycastle.jce.interfaces.ECPublicKey) InvalidKeyException(java.security.InvalidKeyException)

Example 39 with CipherParameters

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

the class Signature method engineInitVerify.

protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    CipherParameters param;
    if (publicKey instanceof ECPublicKey) {
        param = ECUtil.generatePublicKeyParameter(publicKey);
    } else {
        try {
            byte[] bytes = publicKey.getEncoded();
            publicKey = JDKKeyFactory.createPublicKeyFromDERStream(bytes);
            if (publicKey instanceof ECPublicKey) {
                param = ECUtil.generatePublicKeyParameter(publicKey);
            } else {
                throw new InvalidKeyException("can't recognise key type in ECDSA based signer");
            }
        } catch (Exception e) {
            throw new InvalidKeyException("can't recognise key type in ECDSA based signer");
        }
    }
    digest.reset();
    signer.init(false, param);
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ECPublicKey(java.security.interfaces.ECPublicKey) InvalidKeyException(java.security.InvalidKeyException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException)

Example 40 with CipherParameters

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

the class DSASigner method engineInitSign.

protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    CipherParameters param;
    param = DSAUtil.generatePrivateKeyParameter(privateKey);
    if (random != null) {
        param = new ParametersWithRandom(param, random);
    }
    digest.reset();
    signer.init(true, param);
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom)

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