U
    ÈZjô  ã                   @   sX   d dl mZ d dlmZmZmZmZ dd„ Zdd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ ZdS )é    )Úlong_to_bytes)ÚEccKeyÚ	constructÚ_import_curve25519_public_keyÚ_import_curve448_public_keyc                 C   sp   |j | j }| ¡ rtdƒ‚| jdkr<t|jjddd�ƒ}n0| jdkr\t|jjddd�ƒ}nt|j| 	¡ ƒ}|S )NzInvalid ECDH pointÚ
Curve25519é    Úlittle)Ú	byteorderÚCurve448é8   )
ZpointQÚdZis_point_at_infinityÚ
ValueErrorÚcurveÚ	bytearrayÚxÚto_bytesr   Zsize_in_bytes)Zkey_privZkey_pubZpointPÚz© r   úU/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/Crypto/Protocol/DH.pyÚ_compute_ecdh   s    

r   c                 C   s   t | ƒ}td|d�S )a€  Create a new X25519 public key object,
    starting from the key encoded as raw ``bytes``,
    in the format described in RFC7748.

    Args:
      encoded (bytes):
        The x25519 public key to import.
        It must be 32 bytes.

    Returns:
      :class:`Crypto.PublicKey.EccKey` : a new ECC key object.

    Raises:
      ValueError: when the given key cannot be parsed.
    r   ©r   Zpoint_x)r   r   ©Úencodedr   r   r   r   Úimport_x25519_public_key   s    r   c                 C   s   t | dd�S )a‚  Create a new X25519 private key object,
    starting from the key encoded as raw ``bytes``,
    in the format described in RFC7748.

    Args:
      encoded (bytes):
        The X25519 private key to import.
        It must be 32 bytes.

    Returns:
      :class:`Crypto.PublicKey.EccKey` : a new ECC key object.

    Raises:
      ValueError: when the given key cannot be parsed.
    r   ©Úseedr   ©r   ©r   r   r   r   Úimport_x25519_private_key,   s    r   c                 C   s   t | ƒ}td|d�S )a|  Create a new X448 public key object,
    starting from the key encoded as raw ``bytes``,
    in the format described in RFC7748.

    Args:
      encoded (bytes):
        The x448 public key to import.
        It must be 56 bytes.

    Returns:
      :class:`Crypto.PublicKey.EccKey` : a new ECC key object.

    Raises:
      ValueError: when the given key cannot be parsed.
    r   r   )r   r   r   r   r   r   Úimport_x448_public_key@   s    r    c                 C   s   t | dd�S )a~  Create a new X448 private key object,
    starting from the key encoded as raw ``bytes``,
    in the format described in RFC7748.

    Args:
      encoded (bytes):
        The X448 private key to import.
        It must be 56 bytes.

    Returns:
      :class:`Crypto.PublicKey.EccKey` : a new ECC key object.

    Raises:
      ValueError: when the given key cannot be parsed.
    r   r   r   r   r   r   r   Úimport_x448_private_keyU   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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   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_privater   )r   ÚkeyÚnameÚprivater   r   r   Úcheck_curve�   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_agreementi   sN    

r5   N)ZCrypto.Util.numberr   ZCrypto.PublicKey.ECCr   r   r   r   r   r   r   r    r!   r5   r   r   r   r   Ú<module>   s   