Skip to content

Commit d5bdf61

Browse files
committed
change: nem_ed25519_rust -> ecdsa
all pure-python code
1 parent b39ff1d commit d5bdf61

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

p2p_python/core.py

+25-8
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
from p2p_python.serializer import dumps
44
from p2p_python.tool.traffic import Traffic
55
from p2p_python.tool.utils import AESCipher
6-
from nem_ed25519_rust import generate_keypair, encrypt, decrypt
6+
from ecdsa.keys import SigningKey, VerifyingKey
7+
from ecdsa.curves import NIST256p
78
from threading import Thread, current_thread, RLock, Event
89
from typing import Optional, List
910
from logging import getLogger
1011
from binascii import a2b_hex
1112
from time import time, sleep
1213
from queue import Queue
1314
from io import BytesIO
15+
from hashlib import sha256
1416
import selectors
1517
import json
1618
import random
@@ -154,17 +156,16 @@ def create_connection(self, host, port):
154156
raise PeerToPeerError('timeout on public key receive')
155157
except json.JSONDecodeError:
156158
raise PeerToPeerError('json decode error on public key receive')
157-
other_pk = msg['public-key']
158-
other_pub = a2b_hex(other_pk)
159159
# 4. send public key
160-
send = json.dumps({'public-key': my_pub.hex()}).encode()
160+
send = json.dumps({'public-key': my_pub}).encode()
161161
sock.sendall(send)
162162
self.traffic.put_traffic_up(send)
163163
# 5. Get AES key and header and decrypt
164164
try:
165165
receive = sock.recv(self.buffsize)
166166
self.traffic.put_traffic_down(receive)
167-
dec = decrypt(my_sec, other_pub, receive)
167+
key = generate_shared_key(my_sec, msg['public-key'])
168+
dec = AESCipher.decrypt(key, receive)
168169
data = json.loads(dec.decode())
169170
except socket.timeout:
170171
raise PeerToPeerError('timeout on AES key and header receive')
@@ -298,7 +299,7 @@ def initial_connection_check(self, sock, host_port):
298299
raise ConnectionAbortedError('Same origin connection.')
299300
# 4. send my public key
300301
my_sec, my_pub = generate_keypair()
301-
send = json.dumps({'public-key': my_pub.hex()}).encode()
302+
send = json.dumps({'public-key': my_pub}).encode()
302303
sock.sendall(send)
303304
self.traffic.put_traffic_up(send)
304305
# 5. receive public key
@@ -312,13 +313,13 @@ def initial_connection_check(self, sock, host_port):
312313
raise PeerToPeerError('timeout on public key receive')
313314
except json.JSONDecodeError:
314315
raise PeerToPeerError('json decode error on public key receive')
315-
other_pub = a2b_hex(data['public-key'])
316316
# 6. encrypt and send AES key and header
317317
send = json.dumps({
318318
'aes-key': new_user.aeskey,
319319
'header': self.get_server_header(),
320320
})
321-
encrypted = encrypt(my_sec, other_pub, send.encode())
321+
key = generate_shared_key(my_sec, data['public-key'])
322+
encrypted = AESCipher.encrypt(key, send.encode())
322323
sock.send(encrypted)
323324
self.traffic.put_traffic_up(encrypted)
324325
# 7. receive accept signal
@@ -514,6 +515,22 @@ def host_port2user(self, host_port) -> Optional[User]:
514515
return None
515516

516517

518+
"""ECDH functions
519+
"""
520+
521+
522+
def generate_shared_key(sk, vk_str) -> bytes:
523+
vk = VerifyingKey.from_string(a2b_hex(vk_str), NIST256p)
524+
point = sk.privkey.secret_multiplier * vk.pubkey.point
525+
return sha256(point.x().to_bytes(32, 'big')).digest()
526+
527+
528+
def generate_keypair() -> (SigningKey, str):
529+
sk = SigningKey.generate(NIST256p)
530+
vk = sk.get_verifying_key()
531+
return sk, vk.to_string().hex()
532+
533+
517534
"""socket connection functions
518535
"""
519536

p2p_python/tool/utils.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def is_aes_key(key):
5757
@staticmethod
5858
def encrypt(key, raw):
5959
assert type(raw) == bytes, "input data is bytes"
60-
key = b64decode(key.encode())
60+
if isinstance(key, str):
61+
key = b64decode(key.encode())
6162
raw = pad(raw, AES.block_size)
6263
iv = Random.new().read(AES.block_size)
6364
cipher = AES.new(key, AES.MODE_CBC, iv)
@@ -66,7 +67,8 @@ def encrypt(key, raw):
6667
@staticmethod
6768
def decrypt(key, enc):
6869
assert type(enc) == bytes, 'Encrypt data is bytes'
69-
key = b64decode(key.encode())
70+
if isinstance(key, str):
71+
key = b64decode(key.encode())
7072
iv = enc[:AES.block_size]
7173
cipher = AES.new(key, AES.MODE_CBC, iv)
7274
raw = cipher.decrypt(enc[AES.block_size:])

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ pycryptodomex
33
xmltodict
44
requests
55
pysocks
6-
nem-ed25519-rust
6+
ecdsa>=0.13
77
msgpack>=0.5.6

0 commit comments

Comments
 (0)