o
    Þý°j  ã                   @   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ÚCurve448é8   )
ÚpointQÚdÚis_point_at_infinityÚ
ValueErrorÚcurveÚ	bytearrayÚxÚto_bytesr   Úsize_in_bytes)Úkey_privÚkey_pubÚpointPÚz© r   ú‰/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/Cryptodome/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:`Cryptodome.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:`Cryptodome.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:`Cryptodome.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:`Cryptodome.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‹|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_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)ÚCryptodome.Util.numberr   ÚCryptodome.PublicKey.ECCr   r   r   r   r   r"   r)   r+   r,   rD   r   r   r   r   Ú<module>   s    