Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions asyncssh/crypto/dh.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,42 @@

"""A shim around PyCA for Diffie Hellman key exchange"""

from cryptography.hazmat.primitives.asymmetric import dh
import warnings
from cryptography.utils import CryptographyDeprecationWarning

try:
from cryptography.hazmat.primitives.asymmetric import dh
except ImportError:
from cryptography.hazmat.decrepit.asymmetric import dh


class DH:
"""A shim around PyCA for Diffie Hellman key exchange"""

def __init__(self, g: int, p: int):
self._pn = dh.DHParameterNumbers(p, g)
self._priv_key = self._pn.parameters().generate_private_key()
with warnings.catch_warnings():
warnings.simplefilter('ignore', CryptographyDeprecationWarning)

self._pn = dh.DHParameterNumbers(p, g)
self._priv_key = self._pn.parameters().generate_private_key()

def get_public(self) -> int:
"""Return the public key to send in the handshake"""

pub_key = self._priv_key.public_key()
with warnings.catch_warnings():
warnings.simplefilter('ignore', CryptographyDeprecationWarning)

pub_key = self._priv_key.public_key()

return pub_key.public_numbers().y
return pub_key.public_numbers().y

def get_shared(self, peer_public: int) -> int:
"""Return the shared key from the peer's public key"""

peer_key = dh.DHPublicNumbers(peer_public, self._pn).public_key()
shared_key = self._priv_key.exchange(peer_key)
with warnings.catch_warnings():
warnings.simplefilter('ignore', CryptographyDeprecationWarning)

peer_key = dh.DHPublicNumbers(peer_public, self._pn).public_key()
shared_key = self._priv_key.exchange(peer_key)

return int.from_bytes(shared_key, 'big')