o
    n~b                     @   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Cryptodome.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_MACzAn 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 |}|r9td| t	|
 tj| _|rK| | d S 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   C/usr/local/lib/python3.10/dist-packages/Cryptodome/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 )zAuthenticate 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  )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 )zReturn 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 )zReturn 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r"t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u rtd|  dd}|  dd}| r3t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 ``Cryptodome.Cipher``):
            The cipher algorithm to use for deriving the Poly1305
            key pair *(r, s)*.
            It can only be ``Cryptodome.Cipher.AES`` or ``Cryptodome.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Cryptodome.Util.py3compatr   r   r   ZCryptodome.Hashr   ZCryptodome.Randomr   ZCryptodome.Util._raw_apir   r	   r
   r   r   r   r   r   objectr   r/   r   r   r   r   <module>   s   ${