o
    6h+                     @   s   d Z ddlmZ ddlmZ ddlmZmZ g dZG dd de	Z
G dd	 d	e	ZG d
d de	ZG dd de	ZG dd deZdS )zG
Class for performing Elliptic-curve Diffie-Hellman (ECDH) operations.
   )number_to_string)INFINITY)
SigningKeyVerifyingKey)ECDH
NoKeyErrorNoCurveErrorInvalidCurveErrorInvalidSharedSecretErrorc                   @      e Zd ZdZdS )r   z3ECDH. Key not found but it is needed for operation.N__name__
__module____qualname____doc__ r   r   ^/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/ecdsa/ecdh.pyr          r   c                   @   r   )r   z3ECDH. Curve not set but it is needed for operation.Nr   r   r   r   r   r      r   r   c                   @   r   )r	   zP
    ECDH. Raised in case the public and private keys use different curves.
    Nr   r   r   r   r   r	      s    r	   c                   @   r   )r
   zBECDH. Raised in case the shared secret we obtained is an INFINITY.Nr   r   r   r   r   r
   '   r   r
   c                   @   s   e Zd ZdZd!ddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Z	d"ddZdd Zdd Zdd Zdd  ZdS )#r   z
    Elliptic-curve Diffie-Hellman (ECDH). A key agreement protocol.

    Allows two parties, each having an elliptic-curve public-private key
    pair, to establish a shared secret over an insecure channel
    Nc                 C   s6   || _ d| _d| _|r| | |r| | dS dS )a
  
        ECDH init.

        Call can be initialised without parameters, then the first operation
        (loading either key) will set the used curve.
        All parameters must be ultimately set before shared secret
        calculation will be allowed.

        :param curve: curve for operations
        :type curve: Curve
        :param private_key: `my` private key for ECDH
        :type private_key: SigningKey
        :param public_key:  `their` public key for ECDH
        :type public_key: VerifyingKey
        N)curveprivate_key
public_keyload_private_keyload_received_public_key)selfr   r   r   r   r   r   __init__5   s   
zECDH.__init__c                 C   sr   | j std| jstd| j j| j  kr|jks$td td|jj| j jj }|t	kr5t
d| S )Nz3Private key needs to be set to create shared secretz2Public key needs to be set to create shared secretz3Curves for public key and private key is not equal.z!Invalid shared secret (INFINITY).)r   r   r   r   r	   pubkeypointprivkeysecret_multiplierr   r
   x)r   remote_public_keyresultr   r   r   _get_shared_secretM   s.   zECDH._get_shared_secretc                 C   s
   || _ dS )z
        Set the working curve for ecdh operations.

        :param key_curve: curve from `curves` module
        :type key_curve: Curve
        Nr   )r   	key_curver   r   r   	set_curveg   s   
zECDH.set_curvec                 C   s"   | j std| tj| j dS )z
        Generate local private key for ecdh operation with curve that was set.

        :raises NoCurveError: Curve must be set before key generation.

        :return: public (verifying) key from this private key.
        :rtype: VerifyingKey
        z*Curve must be set prior to key generation.r#   )r   r   r   r   generater   r   r   r   generate_private_keyp   s   	zECDH.generate_private_keyc                 C   s2   | j s|j | _ | j |j krtd|| _| j S )a  
        Load private key from SigningKey (keys.py) object.

        Needs to have the same curve as was set with set_curve method.
        If curve is not set - it sets from this SigningKey

        :param private_key: Initialised SigningKey class
        :type private_key: SigningKey

        :raises InvalidCurveError: private_key curve not the same as self.curve

        :return: public (verifying) key from this private key.
        :rtype: VerifyingKey
        Curve mismatch.)r   r	   r   get_verifying_keyr   r   r   r   r   r   }   s   
zECDH.load_private_keyc                 C   s$   | j std| tj|| j dS )a  
        Load private key from byte string.

        Uses current curve and checks if the provided key matches
        the curve of ECDH key agreement.
        Key loads via from_string method of SigningKey class

        :param private_key: private key in bytes string format
        :type private_key: :term:`bytes-like object`

        :raises NoCurveError: Curve must be set before loading.

        :return: public (verifying) key from this private key.
        :rtype: VerifyingKey
        z$Curve must be set prior to key load.r#   )r   r   r   r   from_stringr+   r   r   r   load_private_key_bytes   s
   zECDH.load_private_key_bytesc                 C      |  t|S )aC  
        Load private key from DER byte string.

        Compares the curve of the DER-encoded key with the ECDH set curve,
        uses the former if unset.

        Note, the only DER format supported is the RFC5915
        Look at keys.py:SigningKey.from_der()

        :param private_key_der: string with the DER encoding of private ECDSA
            key
        :type private_key_der: string

        :raises InvalidCurveError: private_key curve not the same as self.curve

        :return: public (verifying) key from this private key.
        :rtype: VerifyingKey
        )r   r   from_der)r   private_key_derr   r   r   load_private_key_der      zECDH.load_private_key_derc                 C   r.   )a\  
        Load private key from PEM string.

        Compares the curve of the DER-encoded key with the ECDH set curve,
        uses the former if unset.

        Note, the only PEM format supported is the RFC5915
        Look at keys.py:SigningKey.from_pem()
        it needs to have `EC PRIVATE KEY` section

        :param private_key_pem: string with PEM-encoded private ECDSA key
        :type private_key_pem: string

        :raises InvalidCurveError: private_key curve not the same as self.curve

        :return: public (verifying) key from this private key.
        :rtype: VerifyingKey
        )r   r   from_pem)r   private_key_pemr   r   r   load_private_key_pem   r2   zECDH.load_private_key_pemc                 C   s
   | j  S )z
        Provides a public key that matches the local private key.

        Needs to be sent to the remote party.

        :return: public (verifying) key from local private key.
        :rtype: VerifyingKey
        )r   r*   r'   r   r   r   get_public_key   s   
	zECDH.get_public_keyc                 C   s,   | j s|j | _ | j |j krtd|| _dS )a  
        Load public key from VerifyingKey (keys.py) object.

        Needs to have the same curve as set as current for ecdh operation.
        If curve is not set - it sets it from VerifyingKey.

        :param public_key: Initialised VerifyingKey class
        :type public_key: VerifyingKey

        :raises InvalidCurveError: public_key curve not the same as self.curve
        r)   N)r   r	   r   )r   r   r   r   r   r      s
   
zECDH.load_received_public_keyc                 C   s   |  t|| j|S )a  
        Load public key from byte string.

        Uses current curve and checks if key length corresponds to
        the current curve.
        Key loads via from_string method of VerifyingKey class

        :param public_key_str: public key in bytes string format
        :type public_key_str: :term:`bytes-like object`
        :param valid_encodings: list of acceptable point encoding formats,
            supported ones are: :term:`uncompressed`, :term:`compressed`,
            :term:`hybrid`, and :term:`raw encoding` (specified with ``raw``
            name). All formats by default (specified with ``None``).
        :type valid_encodings: :term:`set-like object`
        )r   r   r,   r   )r   public_key_strvalid_encodingsr   r   r   load_received_public_key_bytes   s
   z#ECDH.load_received_public_key_bytesc                 C   r.   )a  
        Load public key from DER byte string.

        Compares the curve of the DER-encoded key with the ECDH set curve,
        uses the former if unset.

        Note, the only DER format supported is the RFC5912
        Look at keys.py:VerifyingKey.from_der()

        :param public_key_der: string with the DER encoding of public ECDSA key
        :type public_key_der: string

        :raises InvalidCurveError: public_key curve not the same as self.curve
        )r   r   r/   )r   public_key_derr   r   r   load_received_public_key_der     z!ECDH.load_received_public_key_derc                 C   r.   )a  
        Load public key from PEM string.

        Compares the curve of the PEM-encoded key with the ECDH set curve,
        uses the former if unset.

        Note, the only PEM format supported is the RFC5912
        Look at keys.py:VerifyingKey.from_pem()

        :param public_key_pem: string with PEM-encoded public ECDSA key
        :type public_key_pem: string

        :raises InvalidCurveError: public_key curve not the same as self.curve
        )r   r   r3   )r   public_key_pemr   r   r   load_received_public_key_pem  r<   z!ECDH.load_received_public_key_pemc                 C   s   t |  | jjj S )a  
        Generate shared secret from local private key and remote public key.

        The objects needs to have both private key and received public key
        before generation is allowed.

        :raises InvalidCurveError: public_key curve not the same as self.curve
        :raises NoKeyError: public_key or private_key is not set

        :return: shared secret
        :rtype: bytes
        )r   generate_sharedsecretr   r   pr'   r   r   r   generate_sharedsecret_bytes.  s   z ECDH.generate_sharedsecret_bytesc                 C   s   |  | jS )a;  
        Generate shared secret from local private key and remote public key.

        The objects needs to have both private key and received public key
        before generation is allowed.

        It's the same for local and remote party,
        shared secret(local private key, remote public key) ==
        shared secret(local public key, remote private key)

        :raises InvalidCurveError: public_key curve not the same as self.curve
        :raises NoKeyError: public_key or private_key is not set

        :return: shared secret
        :rtype: int
        )r"   r   r'   r   r   r   r?   ?  s   zECDH.generate_sharedsecret)NNN)N)r   r   r   r   r   r"   r%   r(   r   r-   r1   r5   r6   r   r9   r;   r>   rA   r?   r   r   r   r   r   -   s$    
	
r   N)r   utilr   ellipticcurver   keysr   r   __all__	Exceptionr   r   r	   r
   objectr   r   r   r   r   <module>   s    	