Search in sources :

Example 1 with ParametersWithID

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

the class SM2Signer method initWithCache.

/**
 * The same as init method with better performance by adding the cache for the z value
 * corresponding to the privateKey value
 *
 * @param forSigning
 * @param param
 */
public void initWithCache(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());
        }
        BigInteger privateKey = ((ECPrivateKeyParameters) ecKey).getD();
        /**
         * First find z value from zValueCache
         */
        z = zValueCache.get(privateKey);
        if (Objects.isNull(z)) {
            // z value of privateKey not exist, calculate it and set it to the cache
            pubPoint = createBasePointMultiplier().multiply(ecParams.getG(), ((ECPrivateKeyParameters) ecKey).getD()).normalize();
            z = getZ(userID);
            zValueCache.put(privateKey, z);
            logger.info(" privateKey: {} z value not exist, caculate z: {}", privateKey, Hex.toHexString(z));
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug(" privateKey: {} z value, z: {}", privateKey, Hex.toHexString(z));
            }
        }
        digest.update(z, 0, z.length);
    } 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) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) ParametersWithID(org.bouncycastle.crypto.params.ParametersWithID) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) BigInteger(java.math.BigInteger)

Example 2 with ParametersWithID

use of org.bouncycastle.crypto.params.ParametersWithID 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 3 with ParametersWithID

use of org.bouncycastle.crypto.params.ParametersWithID 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 4 with ParametersWithID

use of org.bouncycastle.crypto.params.ParametersWithID in project hutool by looly.

the class SM2 method verify.

/**
 * 用公钥检验数字签名的合法性
 *
 * @param data 数据签名后的数据
 * @param sign 签名
 * @param id   可以为null,若为null,则默认withId为字节数组:"1234567812345678".getBytes()
 * @return 是否验证通过
 */
public boolean verify(byte[] data, byte[] sign, byte[] id) {
    lock.lock();
    final SM2Signer signer = getSigner();
    try {
        CipherParameters param = getCipherParameters(KeyType.PublicKey);
        if (id != null) {
            param = new ParametersWithID(param, id);
        }
        signer.init(false, param);
        signer.update(data, 0, data.length);
        return signer.verifySignature(sign);
    } finally {
        lock.unlock();
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithID(org.bouncycastle.crypto.params.ParametersWithID) SM2Signer(org.bouncycastle.crypto.signers.SM2Signer)

Example 5 with ParametersWithID

use of org.bouncycastle.crypto.params.ParametersWithID 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)

Aggregations

ParametersWithID (org.bouncycastle.crypto.params.ParametersWithID)5 CipherParameters (org.bouncycastle.crypto.CipherParameters)4 ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)4 BigInteger (java.math.BigInteger)2 ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)2 SM2Signer (org.bouncycastle.crypto.signers.SM2Signer)2 CryptoException (cn.hutool.crypto.CryptoException)1 CryptoException (org.bouncycastle.crypto.CryptoException)1 SM3Digest (org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest)1