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

SM9密文顺序问题 #4

Closed
entarohydralisk opened this issue May 21, 2024 · 8 comments
Closed

SM9密文顺序问题 #4

entarohydralisk opened this issue May 21, 2024 · 8 comments

Comments

@entarohydralisk
Copy link

图片
我看代码里面return的是C1, C2, C3,C2是不定长的,放在中间无法校验密文的合法性。

@ww-rm
Copy link
Owner

ww-rm commented May 21, 2024

最终的密文构造是按 C1,C3,C2顺序返回的

    def encrypt(self, plain: bytes, uid: bytes) -> bytes:
        """Encrypt.

        Args:
            plain: Plain data.
            uid: ID of another user.

        Returns:
            bytes: Cipher data.

        Raises:
            RequireArgumentError: Missing some required arguments.
        """

        if not self.can_encrypt:
            raise RequireArgumentError("encrypt", "hid_e", "mpk_e", "mac_klen")

        C1, C2, C3 = self._core.encrypt(self._hid_e, self._mpk_e, plain, uid, self._mac_klen)

        cipher = bytearray()
        cipher.extend(point_to_bytes_1(C1, self._pc_mode))
        cipher.extend(C3)
        cipher.extend(C2)

        return bytes(cipher)

return bytes(cipher)

@entarohydralisk
Copy link
Author

entarohydralisk commented May 21, 2024

图片
解密测试C3验不过,去掉04也一样

@entarohydralisk
Copy link
Author

图片
我用另一个算法验证工具产生的加密数据,拿来解密,也解不了

@ww-rm
Copy link
Owner

ww-rm commented May 21, 2024

图片 解密测试C3验不过,去掉04也一样

这个是仅仅MAC验证失败,但是明文解密出来了吗?

@entarohydralisk
Copy link
Author

entarohydralisk commented May 21, 2024

两个工具都解不出来,MAC是保证完整性的,MAC验不过,解密过程不会发生。

@ww-rm
Copy link
Owner

ww-rm commented May 21, 2024

方便提供一下你使用的两个验证工具吗?

两个工具都解不出来,MAC是保证完整性的,MAC验不过,解密过程不会发生。

不过完整性校验发生在解密之后,不知道验证工具是否给出了解密后的明文,再提示MAC校验失败,这样我可以排查是密文算错了还是单独MAC算错了

另外你安装的版本是最新的吗?

@entarohydralisk
Copy link
Author

entarohydralisk commented May 21, 2024

@ww-rm
Copy link
Owner

ww-rm commented May 21, 2024

两个工具我都测试了没问题,应该是数据格式的问题

“”算法测试工具——Yaoyuan“都不需要PC字节(主公钥、密文、用户私钥),

”SM9算法验证工具“的密文不能有PC标识字节,并且只需要提供主私钥,并没有用到主公钥和加密私钥(乱填都能解密,因此格式未知)

你可以再试试,应该都是前面那个”04“字节导致的问题,有时候需要有时候不需要,但是 gmalg 里面凡是涉及椭圆曲线点转成字节串的地方都需要PC标识字节。

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