U
    ÈZjŠ  ã                   @   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 | ¡ t|ƒtt |ƒƒt|ƒtt |ƒƒ¡}|rrtd| ƒ‚t	| 
¡ tjƒ| _|r’|  |¡ 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   úW/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/Crypto/Hash/Poly1305.pyÚ__init__?   s&    


üÿzPoly1305_MAC.__init__c                 C   sB   | j rtdƒ‚t | 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 ¡ |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  dd„ t|  ¡ ƒ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||  ¡ d�}| ¡ | ¡ 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_tagÚsecretZmac1Zmac2r   r   r   Úverify‹   s
    zPoly1305_MAC.verifyc                 C   s   |   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)r1   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,   r1   r2   r   r   r   r   r   5   s   r   c                  K   s–   |   dd¡}t|dƒstdƒ‚|   dd¡}|dkr:tdƒ‚|   dd¡}|   dd¡}| rftd	t| ƒ ƒ‚| ||¡\}}}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!   Ústrr9   r   r   r:   )Úkwargsr8   Z
cipher_keyr:   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   $ÿ{