U
    ÈZjŽ0  ã                   @   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.
    é    ÚEd25519é9   ÚEd448zNot an EdDSA key (%d bytes))ÚcurveZpoint_xZpoint_y)Úlenr	   r
   Ú
ValueErrorr   )ÚencodedÚxÚyÚ
curve_name© r   úY/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/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   Zed25519r   Z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 )z…Create 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rFt|tjƒ}|s>t|ƒs>tdƒ‚| j}n<| j jdkrzt|t	j
ƒ}|srt|ƒsrt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ÚphZeddsa_sign_methodr   r   r   Úsign{   s    
zEdDSASigScheme.signc                 C   sà   | j s
|r4t|ƒ}dt|ƒ tt| j ƒƒ | j  }nd}|rD| ¡ 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#   r2   r3   ÚflagÚdom2ÚPHMÚr_hashÚrÚR_pkÚk_hashÚkÚsr   r   r   r/   ž   s     

ÿÿzEdDSASigScheme._sign_ed25519c                 C   sÖ   t |ƒ}dt|ƒ tt| jƒƒ | j }|r6| 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   r7   r8   r   )r:   r   r   r   Úreadr   r<   r   r=   r   r>   r"   r   r    r?   r   r   r@   rA   )r#   r2   r3   rB   Údom4rD   rE   rF   rG   rH   rI   rJ   r   r   r   r1   ¹   s    
ÿÿzEdDSASigScheme._sign_ed448c                 C   s|   | j jdkr4t|tjƒ}|s,t|ƒs,tdƒ‚| j}n<| j jdkrht|tj	ƒ}|s`t|ƒs`t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   r-   r   r.   r   r,   Ú_verify_ed25519r   r0   Ú_verify_ed448r   )r#   r2   Ú	signaturer3   Zeddsa_verify_methodr   r   r   ÚverifyÑ   s    zEdDSASigScheme.verifyc                 C   s,  t |ƒdkrtdƒ‚| js|rHt|ƒ}dt|ƒ tt | jƒƒ | j }nd}|rX| ¡ n|}zt|d d… ƒj}W n tk
rŽ   tdƒ‚Y nX t 	|dd … d¡}|| j
kr¶tdƒ‚t ||d d…  | j | ¡ ¡ }	t 	|	d¡| j
 }
|d	 | jjj }d	| |
d	 | jj  }||k�r(td
ƒ‚d S )NrL   ú'The signature is not authentic (length)r5   r6   r   ú"The signature is not authentic (R)r7   ú"The signature is not authentic (S)é   úThe signature is not authentic)r   r   r   r:   r   r;   r   ÚpointQr   r>   r"   r   r<   r   r   r    r?   )r#   r2   rR   r3   rB   rC   rD   ÚRrJ   rH   rI   Úpoint1Úpoint2r   r   r   rP   ô   s0    

ÿÿ
$
zEdDSASigScheme._verify_ed25519c                 C   s   t |ƒdkrtdƒ‚t|ƒ}dt|ƒ tt | jƒƒ | j }|rJ| d¡n|}zt|d d… ƒj}W n tk
r€   tdƒ‚Y nX t 	|dd … d¡}|| j
kr¨tdƒ‚t ||d d…  | j | ¡ d¡}	t 	|	d¡| j
 }
|d	 | jjj }d	| |
d	 | jj  }||k�rtd
ƒ‚d S )NrM   rT   rK   rL   r   rU   r7   rV   rW   rX   )r   r   r:   r   r   rN   r   rY   r   r>   r"   r   r<   r   r   r    r?   )r#   r2   rR   r3   rB   rO   rD   rZ   rJ   rH   rI   r[   r\   r   r   r   rQ     s,    
ÿÿ
&
zEdDSASigScheme._verify_ed448N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r&   r(   r4   r/   r1   rS   rP   rQ   r   r   r   r   r   c   s   ###r   Nc                 C   sX   t | tƒr| jdkrtdƒ‚|dkr,tdƒ‚|dkr:d}nt|ƒdkrNt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 keysZrfc8032zMode must be 'rfc8032'Nr6   éÿ   z3Context for EdDSA must not be longer than 255 bytes)r-   r   r   r   r   r   )r$   Úmoder%   r   r   r   r<   9  s    r<   )N)ZCrypto.Math.Numbersr   ZCrypto.Hashr   r   ZCrypto.Util.py3compatr   r   ZCrypto.PublicKey.ECCr   r   r	   r
   r   r   Úobjectr   r<   r   r   r   r   Ú<module>   s    W