Search in sources :

Example 36 with ParametersWithRandom

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

the class SignatureSpi method engineInitSign.

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

Example 37 with ParametersWithRandom

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

the class P11RSAPSSSignatureSpi method engineInitSign.

protected void engineInitSign(PrivateKey privateKey, SecureRandom random) throws InvalidKeyException {
    if (!(privateKey instanceof P11PrivateKey)) {
        throw new InvalidKeyException("privateKey is not instanceof " + P11PrivateKey.class.getName());
    }
    String algo = privateKey.getAlgorithm();
    if (!"RSA".equals(algo)) {
        throw new InvalidKeyException("privateKey is not an RSA private key: " + algo);
    }
    this.signingKey = (P11PrivateKey) privateKey;
    pss = new org.bouncycastle.crypto.signers.PSSSigner(signer, contentDigest, mgfDigest, saltLength, trailer);
    P11RSAKeyParameter p11KeyParam = P11RSAKeyParameter.getInstance(signingKey.getP11CryptService(), signingKey.getIdentityId());
    if (random == null) {
        pss.init(true, p11KeyParam);
    } else {
        pss.init(true, new ParametersWithRandom(p11KeyParam, random));
    }
}
Also used : ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) InvalidKeyException(java.security.InvalidKeyException) P11RSAKeyParameter(org.xipki.security.pkcs11.P11RSAKeyParameter)

Example 38 with ParametersWithRandom

use of org.bouncycastle.crypto.params.ParametersWithRandom in project web3sdk by FISCO-BCOS.

the class SM2Sign method sign2.

/**
 * The new sm2 signature algorithm with better performance
 *
 * @param message
 * @param ecKeyPair
 * @return
 */
public static Sign.SignatureData sign2(byte[] message, ECKeyPair ecKeyPair) {
    SM2Signer sm2Signer = new SM2Signer();
    ECPrivateKeyParameters eCPrivateKeyParameters = new ECPrivateKeyParameters(ecKeyPair.getPrivateKey(), eCDomainParameters);
    sm2Signer.initWithCache(true, new ParametersWithID(new ParametersWithRandom(eCPrivateKeyParameters), identValue));
    org.bouncycastle.crypto.digests.SM3Digest sm3Digest = new org.bouncycastle.crypto.digests.SM3Digest();
    byte[] md = new byte[sm3Digest.getDigestSize()];
    sm3Digest.update(message, 0, message.length);
    sm3Digest.doFinal(md, 0);
    sm2Signer.update(md, 0, md.length);
    byte[] r = null;
    byte[] s = null;
    byte[] pub = null;
    try {
        BigInteger[] bigIntegers = sm2Signer.generateSignature2();
        pub = Numeric.toBytesPadded(ecKeyPair.getPublicKey(), 64);
        r = SM2Algorithm.getEncoded(bigIntegers[0]);
        s = SM2Algorithm.getEncoded(bigIntegers[1]);
    } catch (CryptoException e) {
        throw new RuntimeException(e);
    }
    return new Sign.SignatureData((byte) 0, r, s, pub);
}
Also used : ParametersWithID(org.bouncycastle.crypto.params.ParametersWithID) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) SM3Digest(org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest) BigInteger(java.math.BigInteger) CryptoException(org.bouncycastle.crypto.CryptoException)

Example 39 with ParametersWithRandom

use of org.bouncycastle.crypto.params.ParametersWithRandom in project 360-Engine-for-Android by 360.

the class RSACoreEngine 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) {
    if (param instanceof ParametersWithRandom) {
        ParametersWithRandom rParam = (ParametersWithRandom) param;
        key = (RSAKeyParameters) rParam.getParameters();
    } else {
        key = (RSAKeyParameters) param;
    }
    this.forEncryption = forEncryption;
}
Also used : ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom)

Example 40 with ParametersWithRandom

use of org.bouncycastle.crypto.params.ParametersWithRandom in project Skein3Fish by wernerd.

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)

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