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");
}
}
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);
}
}
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;
}
}
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;
}
}
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);
}
Aggregations