Search in sources :

Example 21 with ParametersWithRandom

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);
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithID(org.bouncycastle.crypto.params.ParametersWithID) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom)

Example 22 with ParametersWithRandom

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();
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithID(org.bouncycastle.crypto.params.ParametersWithID) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) CryptoException(cn.hutool.crypto.CryptoException) SM2Signer(org.bouncycastle.crypto.signers.SM2Signer)

Example 23 with ParametersWithRandom

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");
        }
    }
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher)

Example 24 with ParametersWithRandom

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();
    }
}
Also used : ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) SecureRandom(java.security.SecureRandom)

Example 25 with ParametersWithRandom

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

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