3
¬ �e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.
    é    ZEd25519é9   ZEd448zNot an EdDSA key (%d bytes))ÚcurveZpoint_xZpoint_y)Úlenr	   r
   Ú
ValueErrorr   )ÚencodedÚxÚyÚ
curve_name© r   ú]/var/www/html/wholesaler-api-python-env/lib/python3.6/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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Ú_AÚ_curveÚorderÚ_order)ÚselfÚkeyÚcontextr   r   r   Ú__init__i   s    
zEdDSASigScheme.__init__c             C   s
   | j 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 jƒ stdƒ‚| j jjdkrHt|tjƒ}|p6t|ƒs@tdƒ‚| j	}n>| j jjdkr~t|t
jƒ}|plt|ƒsvt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   z-'msg_or_hash' must be bytes of a SHA-512 hashr   z.'msg_or_hash' must be bytes of a SHAKE256 hashzIncorrect curve for EdDSA)r   r'   Ú	TypeErrorr    ÚnameÚ
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|jƒ n|}tj|| jj | ƒjƒ }t	j
|dƒ| j }t|| jjj d�jƒ }tj|| | j | ƒjƒ }	t	j
|	dƒ| j }
||
| jj  | j }||jddƒ S )Ns    SigEd25519 no Ed25519 collisionsó    Úlittle)Úpointr   )r   Úintr   r   Údigestr   Únewr   Ú_prefixr   Ú
from_bytesr"   r   r    ÚGr   r   ÚdÚto_bytes)r#   r0   r1   Ú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|jdƒn|}tj|| jj | ƒjdƒ}t	j
|dƒ| j }t|| jjj d�jƒ }tj|| | j | ƒjdƒ}	t	j
|	dƒ| j }
||
| jj  | j }||jddƒ S )Ns   SigEd448é@   ér   r4   )r5   r   )r6   r   r   r   Úreadr   r8   r   r9   r   r:   r"   r   r    r;   r   r   r<   r=   )r#   r0   r1   r>   Údom4r@   rA   rB   rC   rD   rE   rF   r   r   r   r/   ¹   s     zEdDSASigScheme._sign_ed448c             C   s€   | j jjdkr6t|tjƒ}|p$t|ƒs.tdƒ‚| j}n>| j jjdkrlt|t	j
ƒ}|pZt|ƒsdt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   z-'msg_or_hash' must be bytes of a SHA-512 hashr   z.'msg_or_hash' must be bytes of a SHAKE256 hashzIncorrect curve for EdDSA)r   r    r*   r+   r   r,   r   r)   Ú_verify_ed25519r   r.   Ú_verify_ed448r   )r#   r0   Ú	signaturer1   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|jƒ n|}yt|d d… ƒj}W n tk
rŽ   tdƒ‚Y nX tj	|dd … dƒ}|| j
kr¶tdƒ‚tj||d d…  | j | ƒjƒ }	tj	|	dƒ| j
 }
|d	 | jjj }d	| |
d	 | jj  }||k�r(td
ƒ‚d S )NrG   z'The signature is not authentic (length)s    SigEd25519 no Ed25519 collisionsr3   r   z"The signature is not authentic (R)r4   z"The signature is not authentic (S)é   zThe signature is not authentic)r   r   r   r6   r   r7   r   ÚpointQr   r:   r"   r   r8   r   r   r    r;   )r#   r0   rM   r1   r>   r?   r@   ÚRrF   rD   rE   Úpoint1Úpoint2r   r   r   rK   ô   s(    
"
$
zEdDSASigScheme._verify_ed25519c             C   s   t |ƒdkrtdƒ‚t|ƒ}dt|ƒ tt | jƒƒ | j }|rJ|jdƒn|}yt|d d… ƒj}W n tk
r€   tdƒ‚Y nX tj	|dd … dƒ}|| j
kr¨tdƒ‚tj||d d…  | j | ƒjdƒ}	tj	|	dƒ| j
 }
|d	 | jjj }d	| |
d	 | jj  }||k�rtd
ƒ‚d S )NrH   z'The signature is not authentic (length)s   SigEd448rG   r   z"The signature is not authentic (R)r4   z"The signature is not authentic (S)rO   zThe signature is not authentic)r   r   r6   r   r   rI   r   rP   r   r:   r"   r   r8   r   r   r    r;   )r#   r0   rM   r1   r>   rJ   r@   rQ   rF   rD   rE   rR   rS   r   r   r   rL     s$     
&
zEdDSASigScheme._verify_ed448N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r&   r(   r2   r-   r/   rN   rK   rL   r   r   r   r   r   c   s   ###r   Nc             C   sZ   t | tƒ s| jƒ  rtdƒ‚|dkr.tdƒ‚|dkr<d}nt|ƒdkrPt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.
    z&EdDSA can only be used with EdDSA keysZrfc8032zMode must be 'rfc8032'Nr3   éÿ   z3Context for EdDSA must not be longer than 255 bytes)r+   r   Z	_is_eddsar   r   r   )r$   Úmoder%   r   r   r   r8   9  s    r8   )N)ZCrypto.Math.Numbersr   ZCrypto.Hashr   r   ZCrypto.Util.py3compatr   r   ZCrypto.PublicKey.ECCr   r   r	   r
   r   r   Úobjectr   r8   r   r   r   r   Ú<module>   s    W