Search in sources :

Example 6 with ParametersWithRandom

use of org.bouncycastle.crypto.params.ParametersWithRandom in project robovm by robovm.

the class CipherSpi method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (params == null || params instanceof OAEPParameterSpec) {
        if (key instanceof RSAPublicKey) {
            if (privateKeyOnly && opmode == Cipher.ENCRYPT_MODE) {
                throw new InvalidKeyException("mode 1 requires RSAPrivateKey");
            }
            param = RSAUtil.generatePublicKeyParameter((RSAPublicKey) key);
        } else if (key instanceof RSAPrivateKey) {
            if (publicKeyOnly && opmode == Cipher.ENCRYPT_MODE) {
                throw new InvalidKeyException("mode 2 requires RSAPublicKey");
            }
            param = RSAUtil.generatePrivateKeyParameter((RSAPrivateKey) key);
        } else {
            throw new InvalidKeyException("unknown key type passed to RSA");
        }
        if (params != null) {
            OAEPParameterSpec spec = (OAEPParameterSpec) params;
            paramSpec = params;
            if (!spec.getMGFAlgorithm().equalsIgnoreCase("MGF1") && !spec.getMGFAlgorithm().equals(PKCSObjectIdentifiers.id_mgf1.getId())) {
                throw new InvalidAlgorithmParameterException("unknown mask generation function specified");
            }
            if (!(spec.getMGFParameters() instanceof MGF1ParameterSpec)) {
                throw new InvalidAlgorithmParameterException("unkown MGF parameters");
            }
            Digest digest = DigestFactory.getDigest(spec.getDigestAlgorithm());
            if (digest == null) {
                throw new InvalidAlgorithmParameterException("no match on digest algorithm: " + spec.getDigestAlgorithm());
            }
            MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) spec.getMGFParameters();
            Digest mgfDigest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());
            if (mgfDigest == null) {
                throw new InvalidAlgorithmParameterException("no match on MGF digest algorithm: " + mgfParams.getDigestAlgorithm());
            }
            cipher = new OAEPEncoding(new RSABlindedEngine(), digest, mgfDigest, ((PSource.PSpecified) spec.getPSource()).getValue());
        }
    } else {
        throw new IllegalArgumentException("unknown parameter type.");
    }
    if (!(cipher instanceof RSABlindedEngine)) {
        if (random != null) {
            param = new ParametersWithRandom(param, random);
        } else {
            param = new ParametersWithRandom(param, new SecureRandom());
        }
    }
    bOut.reset();
    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:
            throw new InvalidParameterException("unknown opmode " + opmode + " passed to RSA");
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) Digest(org.bouncycastle.crypto.Digest) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) SecureRandom(java.security.SecureRandom) InvalidKeyException(java.security.InvalidKeyException) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec) CipherParameters(org.bouncycastle.crypto.CipherParameters) InvalidParameterException(java.security.InvalidParameterException) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSABlindedEngine(org.bouncycastle.crypto.engines.RSABlindedEngine) OAEPEncoding(org.bouncycastle.crypto.encodings.OAEPEncoding) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 7 with ParametersWithRandom

use of org.bouncycastle.crypto.params.ParametersWithRandom in project robovm by robovm.

the class PaddedBufferedBlockCipher method init.

/**
     * initialise the cipher.
     *
     * @param forEncryption 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 forEncryption, CipherParameters params) throws IllegalArgumentException {
    this.forEncryption = forEncryption;
    reset();
    if (params instanceof ParametersWithRandom) {
        ParametersWithRandom p = (ParametersWithRandom) params;
        padding.init(p.getRandom());
        cipher.init(forEncryption, p.getParameters());
    } else {
        padding.init(null);
        cipher.init(forEncryption, params);
    }
}
Also used : ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom)

Example 8 with ParametersWithRandom

use of org.bouncycastle.crypto.params.ParametersWithRandom in project robovm by robovm.

the class DSASigner method init.

public void init(boolean forSigning, CipherParameters param) {
    if (forSigning) {
        if (param instanceof ParametersWithRandom) {
            ParametersWithRandom rParam = (ParametersWithRandom) param;
            this.random = rParam.getRandom();
            this.key = (DSAPrivateKeyParameters) rParam.getParameters();
        } else {
            this.random = new SecureRandom();
            this.key = (DSAPrivateKeyParameters) param;
        }
    } else {
        this.key = (DSAPublicKeyParameters) param;
    }
}
Also used : ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) SecureRandom(java.security.SecureRandom)

Example 9 with ParametersWithRandom

use of org.bouncycastle.crypto.params.ParametersWithRandom in project robovm by robovm.

the class ECDSASigner method init.

public void init(boolean forSigning, CipherParameters param) {
    if (forSigning) {
        if (param instanceof ParametersWithRandom) {
            ParametersWithRandom rParam = (ParametersWithRandom) param;
            this.random = rParam.getRandom();
            this.key = (ECPrivateKeyParameters) rParam.getParameters();
        } else {
            this.random = new SecureRandom();
            this.key = (ECPrivateKeyParameters) param;
        }
    } else {
        this.key = (ECPublicKeyParameters) param;
    }
}
Also used : ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) SecureRandom(java.security.SecureRandom)

Example 10 with ParametersWithRandom

use of org.bouncycastle.crypto.params.ParametersWithRandom in project robovm by robovm.

the class RSADigestSigner method init.

/**
     * initialise the signer for signing or verification.
     *
     * @param forSigning
     *            true if for signing, false otherwise
     * @param parameters
     *            necessary parameters.
     */
public void init(boolean forSigning, CipherParameters parameters) {
    this.forSigning = forSigning;
    AsymmetricKeyParameter k;
    if (parameters instanceof ParametersWithRandom) {
        k = (AsymmetricKeyParameter) ((ParametersWithRandom) parameters).getParameters();
    } else {
        k = (AsymmetricKeyParameter) parameters;
    }
    if (forSigning && !k.isPrivate()) {
        throw new IllegalArgumentException("signing requires private key");
    }
    if (!forSigning && k.isPrivate()) {
        throw new IllegalArgumentException("verification requires public key");
    }
    reset();
    rsaEngine.init(forSigning, parameters);
}
Also used : AsymmetricKeyParameter(org.bouncycastle.crypto.params.AsymmetricKeyParameter) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom)

Aggregations

ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)41 SecureRandom (java.security.SecureRandom)20 CipherParameters (org.bouncycastle.crypto.CipherParameters)12 AsymmetricKeyParameter (org.bouncycastle.crypto.params.AsymmetricKeyParameter)8 InvalidKeyException (java.security.InvalidKeyException)7 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)5 InvalidParameterException (java.security.InvalidParameterException)5 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)5 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)5 ParametersWithID (org.bouncycastle.crypto.params.ParametersWithID)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 BadPaddingException (javax.crypto.BadPaddingException)3 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3 SecretKey (javax.crypto.SecretKey)3 ShortBufferException (javax.crypto.ShortBufferException)3 IvParameterSpec (javax.crypto.spec.IvParameterSpec)3 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)3 DataLengthException (org.bouncycastle.crypto.DataLengthException)3 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)3