From b66397300b7f518b6f6b4dc80dfa65383bda3307 Mon Sep 17 00:00:00 2001 From: xuzhenxing Date: Tue, 10 Oct 2023 11:22:36 +0800 Subject: [PATCH 1/2] Fix some errors when cryptography is a newer version --- python/examples/ecdh.py | 5 ++--- python/examples/sign_verify.py | 11 +++++------ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/python/examples/ecdh.py b/python/examples/ecdh.py index 11b8d29..fe4f67e 100644 --- a/python/examples/ecdh.py +++ b/python/examples/ecdh.py @@ -26,7 +26,6 @@ from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.asymmetric import ec from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat -from cryptography.utils import int_from_bytes import time @@ -100,8 +99,8 @@ def ECDH(slot, iface='hid', **kwargs): # Convert device public key to a cryptography public key object device_pub = ec.EllipticCurvePublicNumbers( curve=ec.SECP256R1(), - x=int_from_bytes(device_pub[0:32], byteorder='big'), - y=int_from_bytes(device_pub[32:64], byteorder='big'), + x=int.from_bytes(device_pub[0:32], byteorder='big'), + y=int.from_bytes(device_pub[32:64], byteorder='big'), ).public_key(default_backend()) # Perform the host side ECDH computation diff --git a/python/examples/sign_verify.py b/python/examples/sign_verify.py index 573f68a..be3c90b 100644 --- a/python/examples/sign_verify.py +++ b/python/examples/sign_verify.py @@ -28,7 +28,6 @@ from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import ec, utils from cryptography.exceptions import InvalidSignature -from cryptography.utils import int_from_bytes, int_to_bytes from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat import time @@ -99,7 +98,7 @@ def verify_device(message, signature, public_key): def sign_host(digest, key): signature = key.sign(digest, ec.ECDSA(utils.Prehashed(hashes.SHA256()))) (r,s) = utils.decode_dss_signature(signature) - signature = int_to_bytes(r, 32) + int_to_bytes(s, 32) + signature = int.to_bytes(r, 32) + int.to_bytes(s, 32) return signature @@ -108,14 +107,14 @@ def verify_host(digest, signature, public_key_data): Verify a signature using the host software """ try: - r = int_from_bytes(signature[0:32], byteorder='big', signed=False) - s = int_from_bytes(signature[32:64], byteorder='big', signed=False) + r = int.from_bytes(signature[0:32], byteorder='big', signed=False) + s = int.from_bytes(signature[32:64], byteorder='big', signed=False) sig = utils.encode_dss_signature(r, s) public_key = ec.EllipticCurvePublicNumbers( curve=ec.SECP256R1(), - x=int_from_bytes(public_key_data[0:32], byteorder='big'), - y=int_from_bytes(public_key_data[32:64], byteorder='big'), + x=int.from_bytes(public_key_data[0:32], byteorder='big'), + y=int.from_bytes(public_key_data[32:64], byteorder='big'), ).public_key(default_backend()) public_key.verify(sig, digest, ec.ECDSA(utils.Prehashed(hashes.SHA256()))) return True From 4c70788e5526f16926bd8802238bb3d6db475d05 Mon Sep 17 00:00:00 2001 From: xuzhenxing Date: Fri, 13 Oct 2023 18:01:40 +0800 Subject: [PATCH 2/2] Fix some errors for sign_verify.py --- python/examples/sign_verify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/examples/sign_verify.py b/python/examples/sign_verify.py index be3c90b..03cd520 100644 --- a/python/examples/sign_verify.py +++ b/python/examples/sign_verify.py @@ -98,7 +98,7 @@ def verify_device(message, signature, public_key): def sign_host(digest, key): signature = key.sign(digest, ec.ECDSA(utils.Prehashed(hashes.SHA256()))) (r,s) = utils.decode_dss_signature(signature) - signature = int.to_bytes(r, 32) + int.to_bytes(s, 32) + signature = int.to_bytes(r, 32, byteorder='big') + int.to_bytes(s, 32, byteorder='big') return signature