o
    5h0                     @   sr   d dl mZ d dlmZmZ d dlmZmZ d dlm	Z	m
Z
mZmZ dd Zdd ZG d	d
 d
eZdddZdS )    )Integer)SHA512SHAKE256)bchris_bytes)EccKey	construct_import_ed25519_public_key_import_ed448_public_keyc                 C   sZ   t | dkrt| \}}d}nt | dkrt| \}}d}ntdt |  t|||dS )a  Create a new Ed25519 or Ed448 public key object,
    starting from the key encoded as raw ``bytes``,
    in the format described in RFC8032.

    Args:
      encoded (bytes):
        The EdDSA public key to import.
        It must be 32 bytes for Ed25519, and 57 bytes for Ed448.

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

    Raises:
      ValueError: when the given key cannot be parsed.
        Ed255199   Ed448zNot an EdDSA key (%d bytes))curvepoint_xpoint_y)lenr	   r
   
ValueErrorr   )encodedxy
curve_name r   j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/Crypto/Signature/eddsa.pyimport_public_key)   s   r   c                 C   s8   t | dkr	d}nt | dkrd}ntdt| |dS )a  Create a new Ed25519 or Ed448 private key object,
    starting from the key encoded as raw ``bytes``,
    in the format described in RFC8032.

    Args:
      encoded (bytes):
        The EdDSA private key to import.
        It must be 32 bytes for Ed25519, and 57 bytes for Ed448.

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

    Raises:
      ValueError: when the given key cannot be parsed.
    r   ed25519r   ed448z8Incorrect length. Only EdDSA private keys are supported.)seedr   )r   r   r   )r   r   r   r   r   import_private_keyE   s   r   c                   @   sP   e Zd Z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S )EdDSASigSchemezlAn EdDSA signature object.
    Do not instantiate directly.
    Use :func:`Crypto.Signature.eddsa.new`.
    c                 C   s$   || _ || _| | _|jj| _dS )zCreate a new EdDSA object.

        Do not instantiate this object directly,
        use `Crypto.Signature.DSS.new` instead.
        N)_key_context_export_eddsa_public_A_curveorder_order)selfkeycontextr   r   r   __init__i   s   
zEdDSASigScheme.__init__c                 C   s
   | j  S )zRReturn ``True`` if this signature object can be used
        for signing messages.)r    has_private)r'   r   r   r   can_signu   s   
zEdDSASigScheme.can_signc                 C   s   | j  s	td| j jdkr#t|tj}|st|std| j}n| j jdkr=t|t	j
}|s9t|s9td| j}ntd|||S )aJ  Compute the EdDSA signature of a message.

        Args:
          msg_or_hash (bytes or a hash object):
            The message to sign (``bytes``, in case of *PureEdDSA*) or
            the hash that was carried out over the message (hash object, for *HashEdDSA*).

            The hash object must be :class:`Crypto.Hash.SHA512` for Ed25519,
            and :class:`Crypto.Hash.SHAKE256` object for Ed448.

        :return: The signature as ``bytes``. It is always 64 bytes for Ed25519, and 114 bytes for Ed448.
        :raise TypeError: if the EdDSA key has no private half
        zPrivate key is needed to signr   -'msg_or_hash' must be bytes of a SHA-512 hashr   .'msg_or_hash' must be bytes of a SHAKE256 hashIncorrect curve for EdDSA)r    r+   	TypeErrorr   
isinstancer   
SHA512Hashr   _sign_ed25519r   SHAKE256_XOF_sign_ed448r   )r'   msg_or_hashpheddsa_sign_methodr   r   r   sign{   s   

zEdDSASigScheme.signc                 C   s   | j s|rt|}dt| tt| j  | j  }nd}|r"| n|}t|| jj |  }t	
|d| j }t|| jjj d }t|| | j |  }	t	
|	d| j }
||
| jj  | j }||dd S )N    SigEd25519 no Ed25519 collisions    littlepointr   )r!   intr   r   digestr   newr    _prefixr   
from_bytesr&   r   r$   Gr"   r#   dto_bytes)r'   r6   r7   flagdom2PHMr_hashrR_pkk_hashksr   r   r   r3      s    

zEdDSASigScheme._sign_ed25519c                 C   s   t |}dt| tt| j | j }|r|dn|}t|| jj | d}t	
|d| j }t|| jjj d }t|| | j | d}	t	
|	d| j }
||
| jj  | j }||dd S )N   SigEd448@   r   r<   r=   r   )r?   r   r   r!   readr   rA   r    rB   r   rC   r&   r   r$   rD   r"   r#   rE   rF   )r'   r6   r7   rG   dom4rI   rJ   rK   rL   rM   rN   rO   r   r   r   r5      s   
zEdDSASigScheme._sign_ed448c                 C   s|   | j jdkrt|tj}|st|std| j}n| j jdkr4t|tj	}|s0t|s0td| j
}ntd||||S )a  Check if an EdDSA signature is authentic.

        Args:
          msg_or_hash (bytes or a hash object):
            The message to verify (``bytes``, in case of *PureEdDSA*) or
            the hash that was carried out over the message (hash object, for *HashEdDSA*).

            The hash object must be :class:`Crypto.Hash.SHA512` object for Ed25519,
            and :class:`Crypto.Hash.SHAKE256` for Ed448.

          signature (``bytes``):
            The signature that needs to be validated.
            It must be 64 bytes for Ed25519, and 114 bytes for Ed448.

        :raise ValueError: if the signature is not authentic
        r   r-   r   r.   r/   )r    r   r1   r   r2   r   r0   _verify_ed25519r   r4   _verify_ed448r   )r'   r6   	signaturer7   eddsa_verify_methodr   r   r   verify   s   zEdDSASigScheme.verifyc                 C   s$  t |dkr
td| js|r$t|}dt| tt | j | j }nd}|r,| n|}zt|d d j}W n tyD   tdw t	|dd  d}|| j
krXtdt||d d  | j |  }	t	|	d| j
 }
|d	 | jjj }d	| |
d	 | jj  }||krtd
d S )NrQ   'The signature is not authentic (length)r:   r;   r   "The signature is not authentic (R)r<   "The signature is not authentic (S)   The signature is not authentic)r   r   r!   r?   r   r@   r   pointQr   rC   r&   r   rA   r#   r    r$   rD   )r'   r6   rW   r7   rG   rH   rI   RrO   rM   rN   point1point2r   r   r   rU      s4   


$zEdDSASigScheme._verify_ed25519c                 C   s  t |dkr
tdt|}dt| tt | j | j }|r%|dn|}zt|d d j}W n ty=   tdw t	|dd  d}|| j
krQtdt||d d  | j | d}	t	|	d| j
 }
|d	 | jjj }d	| |
d	 | jj  }||krtd
d S )NrR   rZ   rP   rQ   r   r[   r<   r\   r]   r^   )r   r   r?   r   r!   rS   r   r_   r   rC   r&   r   rA   r#   r    r$   rD   )r'   r6   rW   r7   rG   rT   rI   r`   rO   rM   rN   ra   rb   r   r   r   rV     s0   

&zEdDSASigScheme._verify_ed448N)__name__
__module____qualname____doc__r*   r,   r9   r3   r5   rY   rU   rV   r   r   r   r   r   c   s    ###r   Nc                 C   sX   t | tr
| jdvrtd|dkrtd|du rd}n
t|dkr'tdt| |S )	a  Create a signature object :class:`EdDSASigScheme` that
    can perform or verify an EdDSA signature.

    Args:
        key (:class:`Crypto.PublicKey.ECC` object):
            The key to use for computing the signature (*private* keys only)
            or for verifying one.
            The key must be on the curve ``Ed25519`` or ``Ed448``.

        mode (string):
            This parameter must be ``'rfc8032'``.

        context (bytes):
            Up to 255 bytes of `context <https://datatracker.ietf.org/doc/html/rfc8032#page-41>`_,
            which is a constant byte string to segregate different protocols or
            different applications of the same key.
    )r   r   z&EdDSA can only be used with EdDSA keysrfc8032zMode must be 'rfc8032'Nr;      z3Context for EdDSA must not be longer than 255 bytes)r1   r   r   r   r   r   )r(   moder)   r   r   r   rA   9  s   
rA   )N)Crypto.Math.Numbersr   Crypto.Hashr   r   Crypto.Util.py3compatr   r   Crypto.PublicKey.ECCr   r   r	   r
   r   r   objectr   rA   r   r   r   r   <module>   s    W