3
¬ �eŠ  ã               @   s‚   d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZmZmZmZ eddƒZG dd	„ d	eƒZd
d„ ZdS )é    )Ú	unhexlify)ÚbordÚtobytesÚ_copy_bytes)ÚBLAKE2s)Úget_random_bytes)Úload_pycryptodome_raw_libÚVoidPointerÚSmartPointerÚcreate_string_bufferÚget_raw_bufferÚc_size_tÚc_uint8_ptrzCrypto.Hash._poly1305aÛ  
                        int poly1305_init(void **state,
                                          const uint8_t *r,
                                          size_t r_len,
                                          const uint8_t *s,
                                          size_t s_len);
                        int poly1305_destroy(void *state);
                        int poly1305_update(void *state,
                                            const uint8_t *in,
                                            size_t len);
                        int poly1305_digest(const void *state,
                                            uint8_t *digest,
                                            size_t len);
                        c               @   sL   e Zd 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S )ÚPoly1305_MACzÀAn Poly1305 MAC object.
    Do not instantiate directly. Use the :func:`new` function.

    :ivar digest_size: the size in bytes of the resulting MAC tag
    :vartype digest_size: integer
    é   c             C   s–   t |ƒdkrtdƒ‚t |ƒdkr(tdƒ‚d | _tƒ }tj|jƒ t|ƒtt |ƒƒt|ƒtt |ƒƒƒ}|rrtd| ƒ‚t	|j
ƒ tjƒ| _|r’| j|ƒ d S )Nr   z Parameter r is not 16 bytes longz Parameter s is not 16 bytes longz%Error %d while instantiating Poly1305)ÚlenÚ
ValueErrorÚ_mac_tagr	   Ú_raw_poly1305Zpoly1305_initZ
address_ofr   r   r
   ÚgetZpoly1305_destroyÚ_stateÚupdate)ÚselfÚrÚsÚdataÚstateÚresult© r   ú[/var/www/html/wholesaler-api-python-env/lib/python3.6/site-packages/Crypto/Hash/Poly1305.pyÚ__init__?   s"    


zPoly1305_MAC.__init__c             C   sB   | j rtdƒ‚tj| jjƒ t|ƒtt|ƒƒƒ}|r>t	d| ƒ‚| S )zŒAuthenticate the next chunk of message.

        Args:
            data (byte string/byte array/memoryview): The next chunk of data
        z8You can only call 'digest' or 'hexdigest' on this objectz$Error %d while hashing Poly1305 data)
r   Ú	TypeErrorr   Zpoly1305_updater   r   r   r   r   r   )r   r   r   r   r   r   r   V   s    zPoly1305_MAC.updatec             C   s
   t ƒ ‚d S )N)ÚNotImplementedError)r   r   r   r   Úcopyg   s    zPoly1305_MAC.copyc             C   sP   | j r| j S tdƒ}tj| jjƒ |tt|ƒƒƒ}|r@td| ƒ‚t	|ƒ| _ | j S )zìReturn the **binary** (non-printable) MAC tag of the message
        authenticated so far.

        :return: The MAC tag digest, computed over the data processed so far.
                 Binary form.
        :rtype: byte string
        r   z'Error %d while creating Poly1305 digest)
r   r   r   Zpoly1305_digestr   r   r   r   r   r   )r   Zbfrr   r   r   r   Údigestj   s    	
zPoly1305_MAC.digestc             C   s   dj dd„ t| jƒ ƒD ƒƒS )zÓReturn the **printable** MAC tag of the message authenticated so far.

        :return: The MAC tag, computed over the data processed so far.
                 Hexadecimal encoded.
        :rtype: string
        Ú c             S   s   g | ]}d t |ƒ ‘qS )z%02x)r   )Ú.0Úxr   r   r   ú
<listcomp>ˆ   s   z*Poly1305_MAC.hexdigest.<locals>.<listcomp>)ÚjoinÚtupler$   )r   r   r   r   Ú	hexdigest€   s    
zPoly1305_MAC.hexdigestc             C   sH   t dƒ}tjd||d�}tjd|| jƒ d�}|jƒ |jƒ krDtdƒ‚dS )ah  Verify that a given **binary** MAC (computed by another party)
        is valid.

        Args:
          mac_tag (byte string/byte string/memoryview): the expected MAC of the message.

        Raises:
            ValueError: if the MAC does not match. It means that the message
                has been tampered with or that the MAC key is incorrect.
        r   é    )Zdigest_bitsÚkeyr   zMAC check failedN)r   r   Únewr$   r   )r   Zmac_tagZsecretZmac1Zmac2r   r   r   Úverify‹   s
    zPoly1305_MAC.verifyc             C   s   | j tt|ƒƒƒ dS )a~  Verify that a given **printable** MAC (computed by another party)
        is valid.

        Args:
            hex_mac_tag (string): the expected MAC of the message,
                as a hexadecimal string.

        Raises:
            ValueError: if the MAC does not match. It means that the message
                has been tampered with or that the MAC key is incorrect.
        N)r/   r   r   )r   Zhex_mac_tagr   r   r   Ú	hexverifyŸ   s    zPoly1305_MAC.hexverifyN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Údigest_sizer    r   r#   r$   r+   r/   r0   r   r   r   r   r   5   s   r   c              K   s–   | j ddƒ}t|dƒstdƒ‚| j ddƒ}|dkr:tdƒ‚| j ddƒ}| j ddƒ}| rftd	t| ƒ ƒ‚|j||ƒ\}}}t|||ƒ}tdd|ƒ|_|S )
a°  Create a new Poly1305 MAC object.

    Args:
        key (bytes/bytearray/memoryview):
            The 32-byte key for the Poly1305 object.
        cipher (module from ``Crypto.Cipher``):
            The cipher algorithm to use for deriving the Poly1305
            key pair *(r, s)*.
            It can only be ``Crypto.Cipher.AES`` or ``Crypto.Cipher.ChaCha20``.
        nonce (bytes/bytearray/memoryview):
            Optional. The non-repeatable value to use for the MAC of this message.
            It must be 16 bytes long for ``AES`` and 8 or 12 bytes for ``ChaCha20``.
            If not passed, a random nonce is created; you will find it in the
            ``nonce`` attribute of the new object.
        data (bytes/bytearray/memoryview):
            Optional. The very first chunk of the message to authenticate.
            It is equivalent to an early call to ``update()``.

    Returns:
        A :class:`Poly1305_MAC` object
    ÚcipherNÚ_derive_Poly1305_key_pairz*Parameter 'cipher' must be AES or ChaCha20r-   zYou must pass a parameter 'key'Únoncer   zUnknown parameters: )	ÚpopÚhasattrr   r!   Ústrr7   r   r   r8   )Úkwargsr6   Z
cipher_keyr8   r   r   r   Znew_macr   r   r   r.   °   s    
r.   N)Úbinasciir   ZCrypto.Util.py3compatr   r   r   ZCrypto.Hashr   ZCrypto.Randomr   ZCrypto.Util._raw_apir   r	   r
   r   r   r   r   r   Úobjectr   r.   r   r   r   r   Ú<module>   s   ${