o
    ò§Mfe  ã                   @   s,   d dl mZ d dlmZ dd„ Zdd„ ZdS )é    )Úlong_to_bytes)ÚEccKeyc                 C   s0   |j | j }| ¡ rtdƒ‚t|j| ¡ ƒ}|S )NzInvalid ECDH point)ÚpointQÚdÚis_point_at_infinityÚ
ValueErrorr   ÚxÚsize_in_bytes)Úkey_privÚkey_pubÚpointPÚz© r   úS/var/www/html/humari/django-venv/lib/python3.10/site-packages/Crypto/Protocol/DH.pyÚ_compute_ecdh   s
   r   c                  K   sx  |   dd¡}|   dd¡}|   dd¡}|   dd¡}|   dd¡}|du r&tdƒ‚d}d}d}d	d
„ }	|dur?|	||ddƒ}|d7 }|durN|	||ddƒ}|d7 }|dur]|	||ddƒ}|d7 }|durl|	||ddƒ}|d7 }|| dk sz|dksz|dkr~tdƒ‚d}
d}|r‹|r‹t||ƒ}
|r¡|r¡t|ƒt|ƒkr›tdƒ‚t||ƒ}n|r«|r«t||ƒ}n	|r´|r´t||ƒ}||
 }||ƒS )aÌ  Perform a Diffie-Hellman key agreement.

    Keywords:
      kdf (callable):
        A key derivation function that accepts ``bytes`` as input and returns
        ``bytes``.
      static_priv (EccKey):
        The local static private key. Optional.
      static_pub (EccKey):
        The static public key that belongs to the peer. Optional.
      eph_priv (EccKey):
        The local ephemeral private key, generated for this session. Optional.
      eph_pub (EccKey):
        The ephemeral public key, received from the peer for this session. Optional.

    At least two keys must be passed, of which one is a private key and one
    a public key.

    Returns (bytes):
      The derived secret key material.
    Ústatic_privNÚ
static_pubÚeph_privÚeph_pubÚkdfz'kdf' is mandatoryr   c                 S   sZ   t |tƒstd| ƒ‚|r| ¡ std| ƒ‚| d u r |j} | S | |jkr+td| ƒ‚| S )Nz'%s' must be an ECC keyz'%s' must be a private ECC keyz('%s' is defined on an incompatible curve)Ú
isinstancer   Ú	TypeErrorÚhas_privateÚcurve)r   ÚkeyÚnameÚprivater   r   r   Úcheck_curve2   s   

þz"key_agreement.<locals>.check_curveTé   Fé   z'Too few keys for the ECDH key agreementó    z"DH mode C(2e, 1s) is not supported)Úgetr   r   Úbool)Úkwargsr   r   r   r   r   Ú
count_privÚ	count_pubr   r   ÚZsÚZeÚZr   r   r   Úkey_agreement   sN   

r)   N)ÚCrypto.Util.numberr   ÚCrypto.PublicKey.ECCr   r   r)   r   r   r   r   Ú<module>   s    	