o
    5h                     @   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   st   |j | j }| rtd| jdkrt|jjddd}|S | jdkr0t|jjddd}|S t|j|	 }|S )NzInvalid ECDH point
Curve25519    little)	byteorderCurve4488   )
pointQdis_point_at_infinity
ValueErrorcurve	bytearrayxto_bytesr   size_in_bytes)key_privkey_pubpointPz r   f/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/Crypto/Protocol/DH.py_compute_ecdh   s   

r   c                 C      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   point_x)r   r   encodedr   r   r   r   import_x25519_public_key      r"   c                 C      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,      r)   c                 C   r   )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@   r#   r+   c                 C   r$   )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   r*   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|rt||}
|r|rt|t|krtdt||}n|r|rt||}n	|r|rt||}||
 }||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_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/   r0   r1   
count_priv	count_pubr   r8   ZsZeZr   r   r   key_agreementi   sN   

rD   N)Crypto.Util.numberr   Crypto.PublicKey.ECCr   r   r   r   r   r"   r)   r+   r,   rD   r   r   r   r   <module>   s    