use of org.bouncycastle.crypto.params.ParametersWithRandom in project web3sdk by FISCO-BCOS.
the class SM2Signer method init.
@Override
public void init(boolean forSigning, CipherParameters param) {
CipherParameters baseParam;
byte[] userID;
if (param instanceof ParametersWithID) {
baseParam = ((ParametersWithID) param).getParameters();
userID = ((ParametersWithID) param).getID();
} else {
baseParam = param;
// the default value
userID = Hex.decode("31323334353637383132333435363738");
}
if (forSigning) {
if (baseParam instanceof ParametersWithRandom) {
ParametersWithRandom rParam = (ParametersWithRandom) baseParam;
ecKey = (ECKeyParameters) rParam.getParameters();
ecParams = ecKey.getParameters();
kCalculator.init(ecParams.getN(), rParam.getRandom());
} else {
ecKey = (ECKeyParameters) baseParam;
ecParams = ecKey.getParameters();
kCalculator.init(ecParams.getN(), CryptoServicesRegistrar.getSecureRandom());
}
pubPoint = createBasePointMultiplier().multiply(ecParams.getG(), ((ECPrivateKeyParameters) ecKey).getD()).normalize();
} else {
ecKey = (ECKeyParameters) baseParam;
ecParams = ecKey.getParameters();
pubPoint = ((ECPublicKeyParameters) ecKey).getQ();
}
z = getZ(userID);
digest.update(z, 0, z.length);
}
use of org.bouncycastle.crypto.params.ParametersWithRandom in project hutool by looly.
the class SM2 method sign.
/**
* 用私钥对信息生成数字签名,签名格式为ASN1<br>
* 在硬件签名中,返回结果为R+S,可以通过调用{@link cn.hutool.crypto.SmUtil#rsAsn1ToPlain(byte[])}方法转换之。
*
* @param data 被签名的数据数据
* @param id 可以为null,若为null,则默认withId为字节数组:"1234567812345678".getBytes()
* @return 签名
*/
public byte[] sign(byte[] data, byte[] id) {
lock.lock();
final SM2Signer signer = getSigner();
try {
CipherParameters param = new ParametersWithRandom(getCipherParameters(KeyType.PrivateKey));
if (id != null) {
param = new ParametersWithID(param, id);
}
signer.init(true, param);
signer.update(data, 0, data.length);
return signer.generateSignature();
} catch (org.bouncycastle.crypto.CryptoException e) {
throw new CryptoException(e);
} finally {
lock.unlock();
}
}
use of org.bouncycastle.crypto.params.ParametersWithRandom in project robovm by robovm.
the class DESedeWrapEngine method init.
/**
* Method init
*
* @param forWrapping
* @param param
*/
public void init(boolean forWrapping, CipherParameters param) {
this.forWrapping = forWrapping;
this.engine = new CBCBlockCipher(new DESedeEngine());
SecureRandom sr;
if (param instanceof ParametersWithRandom) {
ParametersWithRandom pr = (ParametersWithRandom) param;
param = pr.getParameters();
sr = pr.getRandom();
} else {
sr = new SecureRandom();
}
if (param instanceof KeyParameter) {
this.param = (KeyParameter) param;
if (this.forWrapping) {
// Hm, we have no IV but we want to wrap ?!?
// well, then we have to create our own IV.
this.iv = new byte[8];
sr.nextBytes(iv);
this.paramPlusIV = new ParametersWithIV(this.param, this.iv);
}
} else if (param instanceof ParametersWithIV) {
this.paramPlusIV = (ParametersWithIV) param;
this.iv = this.paramPlusIV.getIV();
this.param = (KeyParameter) this.paramPlusIV.getParameters();
if (this.forWrapping) {
if ((this.iv == null) || (this.iv.length != 8)) {
throw new IllegalArgumentException("IV is not 8 octets");
}
} else {
throw new IllegalArgumentException("You should not supply an IV for unwrapping");
}
}
}
use of org.bouncycastle.crypto.params.ParametersWithRandom in project robovm by robovm.
the class RSABlindedEngine method init.
/**
* initialise the RSA engine.
*
* @param forEncryption true if we are encrypting, false otherwise.
* @param param the necessary RSA key parameters.
*/
public void init(boolean forEncryption, CipherParameters param) {
core.init(forEncryption, param);
if (param instanceof ParametersWithRandom) {
ParametersWithRandom rParam = (ParametersWithRandom) param;
key = (RSAKeyParameters) rParam.getParameters();
random = rParam.getRandom();
} else {
key = (RSAKeyParameters) param;
random = new SecureRandom();
}
}
use of org.bouncycastle.crypto.params.ParametersWithRandom in project XobotOS by xamarin.
the class JCERSACipher 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) {
throw new InvalidKeyException("mode 1 requires RSAPrivateKey");
}
param = RSAUtil.generatePublicKeyParameter((RSAPublicKey) key);
} else if (key instanceof RSAPrivateKey) {
if (publicKeyOnly) {
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 = JCEDigestUtil.getDigest(spec.getDigestAlgorithm());
if (digest == null) {
throw new InvalidAlgorithmParameterException("no match on digest algorithm: " + spec.getDigestAlgorithm());
}
MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) spec.getMGFParameters();
Digest mgfDigest = JCEDigestUtil.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());
}
}
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");
}
}
Aggregations