M2Crypto:验证 DSA 签名
我在使用 Python/M2Crypto 验证 DSA 签名时遇到问题。签名是在 Java 中使用标准 java.security.Signature 类以及 Sun 的加密提供程序和 SHA1withDSA 算法指定生成的。
下面是一些 shell 输出:
>>> pk
<M2Crypto.DSA.DSA_pub instance at 0x20b6a28>
>>> sig = '302c02141c4bbb218215ebfec57288059ce814dc430d849502144dd0c581bf2213aff79d17eb37c939e120a97bd2'.decode('hex')
>>> data ='0501...9794'.decode('hex')
>>> pk.verify_asn1(sig, data)
------------------------------------------------------------
Traceback (most recent call last):
...
DSAError: wrong tag
签名值对我来说似乎没问题,它看起来像是两个整数的正确 ASN.1 编码序列(0x302c 指定 44 字节序列,0x0214 指定 20 字节整数),这是标准编码DSA 签名。
由于 DSA_pub.verify_asn1 方法甚至没有记录,我也尝试使用记录的 DSA_pub.verify 方法,但仍然没有雪茄:
>>> r = sig[4:24]
>>> s = sig[26:]
>>> md = sha1(data).digest()
>>> pk.verify(md, r, s)
------------------------------------------------------------
Traceback (most recent call last):
...
DSAError: encoding error
文档声明所有参数应该是“字节字符串”,但 verify 方法以某种方式设法提高编码错误。 我还尝试反转 r 和 s,以检查潜在的字节顺序问题,但这没有帮助。
我做错了什么?
I'm having trouble verifying DSA signatures using Python/M2Crypto. The signatures are generated in Java, using standard java.security.Signature class, with Sun's crypto provider and SHA1withDSA algorithm designation.
Here's some shell output:
>>> pk
<M2Crypto.DSA.DSA_pub instance at 0x20b6a28>
>>> sig = '302c02141c4bbb218215ebfec57288059ce814dc430d849502144dd0c581bf2213aff79d17eb37c939e120a97bd2'.decode('hex')
>>> data ='0501...9794'.decode('hex')
>>> pk.verify_asn1(sig, data)
------------------------------------------------------------
Traceback (most recent call last):
...
DSAError: wrong tag
The signature value seems OK to me, it looks like a proper ASN.1 encoded sequence of two integers (0x302c designates a 44-byte sequence, and 0x0214 designates a 20-byte integer), which is the standard encoding of DSA signatures.
Since the DSA_pub.verify_asn1 method isn't even documented, I also tried using the documented DSA_pub.verify method, but still no cigar:
>>> r = sig[4:24]
>>> s = sig[26:]
>>> md = sha1(data).digest()
>>> pk.verify(md, r, s)
------------------------------------------------------------
Traceback (most recent call last):
...
DSAError: encoding error
The docs state that all the parameters should be "byte strings", but the verify method somehow manages to raise an encoding error.
I also tried reversing r and s, to check for potential endianness problems but that didn't help.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在测试中找到了解决方案: http://svn.osafoundation.org/m2crypto /trunk/tests/test_dsa.py
verify_asn1 方法应按如下方式使用:
Found a solution in the tests: http://svn.osafoundation.org/m2crypto/trunk/tests/test_dsa.py
The verify_asn1 method should be used as follows: