3
¬ �ee  ã               @   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 }|jƒ rtdƒ‚t|j|jƒ ƒ}|S )NzInvalid ECDH point)ZpointQÚdZis_point_at_infinityÚ
ValueErrorr   ÚxZsize_in_bytes)Zkey_privZkey_pubZpointPÚz© r   úY/var/www/html/wholesaler-api-python-env/lib/python3.6/site-packages/Crypto/Protocol/DH.pyÚ_compute_ecdh   s
    r
   c              K   sŠ  | j ddƒ}| j ddƒ}| j ddƒ}| j ddƒ}| j ddƒ}|dkrLtdƒ‚d}d}d}d	d
„ }	|dk	r~|	||ddƒ}|d7 }|dk	rœ|	||ddƒ}|d7 }|dk	rº|	||ddƒ}|d7 }|dk	rØ|	||ddƒ}|d7 }|| dk sô|dksô|dkrütdƒ‚d}
d}|�r|�rt||ƒ}
|�rL|�rLt|ƒt|ƒk�r@tdƒ‚t||ƒ}n.|�rd|�rdt||ƒ}n|�rz|�rzt||ƒ}||
 }||ƒ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| ƒ‚|r0|jƒ  r0td| ƒ‚| d kr@|j} n| |jkrVtd| ƒ‚| S )Nz'%s' must be an ECC keyz'%s' must be a private ECC keyz('%s' is defined on an incompatible curve)Ú
isinstancer   Ú	TypeErrorZhas_privateÚcurve)r   ÚkeyÚnameZ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   Z
count_privZ	count_pubr   r   ZZsZZeÚZr   r   r	   Úkey_agreement   sN    

r   N)ZCrypto.Util.numberr   ZCrypto.PublicKey.ECCr   r
   r   r   r   r   r	   Ú<module>   s   	