Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sm2签名时z值不参与如何处理 #78

Closed
Jeery-Wang opened this issue Nov 10, 2023 · 5 comments
Closed

sm2签名时z值不参与如何处理 #78

Jeery-Wang opened this issue Nov 10, 2023 · 5 comments

Comments

@Jeery-Wang
Copy link

Jeery-Wang commented Nov 10, 2023

硬件密码介质,如密码机等,在签名过程中z值不参与,请教下基于bc库如何实现呢?
签名结果不带z值的验签,可参考
https://i.goto327.top/CryptTools/SM2.aspx
进行不带Z值签名验证。
请大佬指导下。

@ZZMarquis
Copy link
Owner

这个参考网址打不开

@Jeery-Wang
Copy link
Author

Jeery-Wang commented Nov 13, 2023

更新了下地址,应该是url带着汉字的问题,大佬再试下看。
签名时候,后面有个选项是否计算z值,默认是带的,和用bc库签出来的结果可以互相验签。
当z值不参与运算时,用bc库应该怎么签名和验签呢,请帮忙看下。

@ZZMarquis
Copy link
Owner

好,我周末有空看下,平时没时间哈。你要着急的话也可以自己先研究一下。

@Jeery-Wang
Copy link
Author

嗯感谢大佬

@ZZMarquis
Copy link
Owner

ZZMarquis commented Feb 4, 2024

看了下BC的代码(最新的1.70版本),基于BC可能控制不了要不要算Z,它的实现里没有提供控制这个行为的参数。
不过想做的话应该也简单,SM2Signer的代码还是相对牵扯其他东西较少的,可以拷贝出来自己改改应该就可以。

    // org.bouncycastle.crypto.signers.SM2Signer

    public void init(boolean forSigning, CipherParameters param)
    {
        CipherParameters baseParam;
        byte[] userID;

        if (param instanceof ParametersWithID)
        {
            baseParam = ((ParametersWithID)param).getParameters();
            userID = ((ParametersWithID)param).getID();

            if (userID.length >= 8192)
            {
                throw new IllegalArgumentException("SM2 user ID must be less than 2^16 bits long");
            }
        }
        else
        {
            baseParam = param;
            // the default value, string value is "1234567812345678"
            userID = Hex.decodeStrict("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();
        }
        
        // TODO : 应该在这里加个if判断决定要不要执行后面的两行代码即可
        z = getZ(userID);
        digest.update(z, 0, z.length);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants