tongsuo-project / tongsuo-python-sdk Goto Github PK
View Code? Open in Web Editor NEWTongsuo-Python-SDK底层使用Tongsuo密码库,旨在为Python开发者提供密码学原语和安全传输协议的支持,目前以国密算法和协议为主
License: Apache License 2.0
Tongsuo-Python-SDK底层使用Tongsuo密码库,旨在为Python开发者提供密码学原语和安全传输协议的支持,目前以国密算法和协议为主
License: Apache License 2.0
KDF希望能支持SM3、SM4两种生成方式;
另外我看到之前添加的hmac好像只在序列化密钥的时候用到,而且我好像无法单独调用hmac函数,希望能添加CBC-MAC的功能;
SM4的XTS模式好像也不能用,希望能添加CCM的工作模式。
ciphertext = pubkey.encrypt(msg)报错'_EllipticCurvePublicKey' object has no attribute 'encrypt'
这里没有这个方法,包括下面的decrypt
该功能已合入 2 个月,求出一个 pypi 的版本(可以是 rc 等预发布版本)
希望增加导入证书和签名/验签时自定义userid的问题
现在只有签名验证的demo
环境:
运行样例时报错:AttributeError: '_EllipticCurvePublicKey' object has no attribute 'encrypt'
from tongsuopy.crypto import serialization
from tongsuopy.crypto.asymciphers import ec
msg = b"hello"
key = ec.generate_private_key(ec.SM2())
pem = key.public_key().public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
pubkey = serialization.load_pem_public_key(pem)
ciphertext = pubkey.encrypt(msg)
decrypt_text = key.decrypt(ciphertext)
assert decrypt_text == msg
版本:1.0.0
运行 demos 代码:
from tongsuopy.crypto import hashes
from tongsuopy.crypto.asymciphers import ec
msg = b"message digest"
d = b"3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8"
Qx = b"09F9DF311E5421A150DD7D161E4BC5C672179FAD1833FC076BB08FF356F35020"
Qy = b"CCEA490CE26775A52DC6EA718CC1AA600AED05FBF35E084A6632F6072DA9AD13"
key = ec.EllipticCurvePrivateNumbers(
int(d, 16),
ec.EllipticCurvePublicNumbers(int(Qx, 16), int(Qy, 16), ec.SM2()),
).private_key()
signature = key.sign(msg, ec.ECDSA(hashes.SM3()))
pubkey = key.public_key()
pubkey.verify(signature, msg, ec.ECDSA(hashes.SM3()))
报错:
Traceback (most recent call last):
File "/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3524, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-ebc580c1d414>", line 14, in <module>
signature = key.sign(msg, ec.ECDSA(hashes.SM3()))
File "/lib/python3.7/site-packages/tongsuopy/backends/tongsuo/ec.py", line 264, in sign
self._backend, signature_algorithm.algorithm, self, data
File "/lib/python3.7/site-packages/tongsuopy/backends/tongsuo/ec.py", line 104, in _ecdsa_sig_sign
backend._lib.EVP_DigestSignInit,
File "/lib/python3.7/site-packages/tongsuopy/backends/tongsuo/ec.py", line 173, in _ecdsa_sig_setup
raise ValueError("Unable to sign/verify with this key", errors)
ValueError: ('Unable to sign/verify with this key', [<tongsuopy.backends.tongsuo.binding._OpenSSLError object at 0x11680d888>])
def sm2_demo():
msg = b"message"
key = ec.generate_private_key(ec.SM2()).private_numbers().private_key()
# 验证 key.public_key 签名 & 验签
signature = key.sign(msg, ec.ECDSA(hashes.SM3()))
key.public_key().verify(signature, msg, ec.ECDSA(hashes.SM3()))
# 将公钥导出为文本
pem_public_key_str = key.public_key().public_bytes(
encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo
).decode()
# 通过文本加载公钥
pubkey_from_text = serialization.load_pem_public_key(pem_public_key_str.encode())
# 验证公钥等价
assert pubkey_from_text.public_numbers() == key.public_key().public_numbers()
assert pubkey_from_text.public_bytes(
encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo
) == key.public_key().public_bytes(
encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo
)
# 验签
pubkey_from_text.verify(signature, msg, ec.ECDSA(hashes.SM3()))
使用文本载入的公钥验签异常:
InvalidSignature Traceback (most recent call last)
<ipython-input-3-621b0280cfd7> in <module>
----> 1 sm2_demo()
<ipython-input-2-3f024d497b2a> in sm2_demo()
23 )
24 # 验签
---> 25 pubkey_from_text.verify(signature, msg, ec.ECDSA(hashes.SM3()))
/nm-bkdev-venv-36/lib/python3.6/site-packages/tongsuopy/backends/tongsuo/ec.py in verify(self, signature, data, signature_algorithm)
361 _check_signature_algorithm(signature_algorithm)
362 _ecdsa_sig_verify(
--> 363 self._backend, signature_algorithm.algorithm, self, signature, data
364 )
/nm-bkdev-venv-36/lib/python3.6/site-packages/tongsuopy/backends/tongsuo/ec.py in _ecdsa_sig_verify(backend, algorithm, public_key, signature, data)
144 if res == 0:
145 backend._consume_errors()
--> 146 raise InvalidSignature
147
148
其他主流密码学库,例如 pycryptodome
cryptography
均支持 arm 架构
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.