o
    Þý°jª  ã                   @   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_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 |ƒƒ¡}|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_poly1305Úpoly1305_initÚ
address_ofr   r   r
   ÚgetÚpoly1305_destroyÚ_stateÚupdate)ÚselfÚrÚsÚdataÚstateÚresult© r!   ú‹/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-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 )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   Ú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 )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   Úpoly1305_digestr   r   r   r   r   r   )r   Ú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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   é    )Údigest_bitsÚkeyr   zMAC check failedN)r   r   Únewr+   r   )r   Úmac_tagÚsecretÚmac1Ú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)r;   r   r   )r   Úhex_mac_tagr!   r!   r"   Ú	hexverifyŸ   s   zPoly1305_MAC.hexverifyN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Údigest_sizer#   r   r(   r+   r2   r;   r=   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 ChaCha20r5   zYou must pass a parameter 'key'Únoncer   zUnknown parameters: )	ÚpopÚhasattrr   r$   ÚstrrD   r   r   rE   )ÚkwargsrC   Ú
cipher_keyrE   r   r   r   Únew_macr!   r!   r"   r6   °   s   
r6   N)Úbinasciir   ÚCryptodome.Util.py3compatr   r   r   ÚCryptodome.Hashr   ÚCryptodome.Randomr   ÚCryptodome.Util._raw_apir   r	   r
   r   r   r   r   r   Úobjectr   r6   r!   r!   r!   r"   Ú<module>   s   $ÿ{