U
    Mfm                     @   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)ZpointQdZis_point_at_infinity
ValueErrorr   xZsize_in_bytes)Zkey_privZkey_pubZpointPz r   :/tmp/pip-unpacked-wheel-l_0d1exj/Cryptodome/Protocol/DH.py_compute_ecdh   s
    r
   c                  K   s  |  dd}|  dd}|  dd}|  dd}|  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rt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   sX   t |tstd| |r.| s.td| | d kr>|j} n| |jkrTt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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   Z
count_privZ	count_pubr   r   ZsZZeZr   r   r	   key_agreement   sN    

r   N)ZCryptodome.Util.numberr   ZCryptodome.PublicKey.ECCr   r
   r   r   r   r   r	   <module>   s   	