o
    Ìn~bE  ã                   @   sl   d dl mZ d dlmZmZmZ d dlmZ ddlm	Z	m
Z
 ddl	mZmZmZ G dd„ deƒZd	d
„ ZdS )é    )Ú	unhexlify)ÚbordÚtobytesÚis_bytes)Úget_random_bytesé   )Ú	cSHAKE128ÚSHA3_256)Ú_bytepadÚ_encode_strÚ_right_encodec                   @   sH   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S )Ú	KMAC_Hashz[A KMAC hash object.
    Do not instantiate directly.
    Use the :func:`new` function.
    c           	      C   sP   d| | _ || _d | _ttt|ƒƒ|ƒ}| ||d¡| _|r&| j |¡ d S d S )Nz2.16.840.1.101.3.4.2.s   KMAC)	ÚoidÚdigest_sizeÚ_macr
   r   r   Ú_newÚ_cshakeÚupdate)	ÚselfÚdataÚkeyÚmac_lenÚcustomZoid_variantZcshakeZrateZpartial_newX© r   úB/usr/local/lib/python3.10/dist-packages/Cryptodome/Hash/KMAC128.pyÚ__init__.   s   
ÿzKMAC_Hash.__init__c                 C   s   | j rtdƒ‚| j |¡ | S )z©Authenticate the next chunk of message.

        Args:
            data (bytes/bytearray/memoryview): The next chunk of the message to
            authenticate.
        z8You can only call 'digest' or 'hexdigest' on this object)r   Ú	TypeErrorr   r   )r   r   r   r   r   r   =   s   zKMAC_Hash.updatec                 C   s2   | j s| j t| jd ƒ¡ | j | j¡| _ | j S )zŽReturn the **binary** (non-printable) MAC tag of the message.

        :return: The MAC tag. Binary form.
        :rtype: byte string
        é   )r   r   r   r   r   Úread©r   r   r   r   ÚdigestK   s   zKMAC_Hash.digestc                 C   s   d  dd„ t|  ¡ ƒD ƒ¡S )z„Return the **printable** MAC tag of the message.

        :return: The MAC tag. Hexadecimal encoded.
        :rtype: string
        Ú c                 S   s   g | ]}d t |ƒ ‘qS )z%02x)r   )Ú.0Úxr   r   r   Ú
<listcomp>_   s    z'KMAC_Hash.hexdigest.<locals>.<listcomp>)ÚjoinÚtupler    r   r   r   r   Ú	hexdigestX   s   zKMAC_Hash.hexdigestc                 C   sD   t dƒ}t || ¡}t ||  ¡  ¡}| ¡ | ¡ kr tdƒ‚dS )a`  Verify that a given **binary** MAC (computed by another party)
        is valid.

        Args:
          mac_tag (bytes/bytearray/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.
        é   zMAC check failedN)r   r	   Únewr    Ú
ValueError)r   Zmac_tagÚsecretZmac1Zmac2r   r   r   Úverifya   s   ÿzKMAC_Hash.verifyc                 C   s   |   tt|ƒƒ¡ dS )an  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   Ú	hexverifyu   s   zKMAC_Hash.hexverifyc                 K   s    d|vr	| j |d< tdi |¤ŽS )zNReturn a new instance of a KMAC hash object.
        See :func:`new`.
        r   Nr   )r   r)   )r   Úkwargsr   r   r   r)   ƒ   s   
zKMAC_Hash.newN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r'   r,   r-   r)   r   r   r   r   r   (   s    	r   c                  K   sŒ   |   dd¡}t|ƒstdƒ‚t|ƒdk rtdƒ‚|   dd¡}|   dd¡}|d	k r,td
ƒ‚|   dd¡}| r<tdt| ƒ ƒ‚t||||dtdƒS )aµ  Create a new KMAC128 object.

    Args:
        key (bytes/bytearray/memoryview):
            The key to use to compute the MAC.
            It must be at least 128 bits long (16 bytes).
        data (bytes/bytearray/memoryview):
            Optional. The very first chunk of the message to authenticate.
            It is equivalent to an early call to :meth:`KMAC_Hash.update`.
        mac_len (integer):
            Optional. The size of the authentication tag, in bytes.
            Default is 64. Minimum is 8.
        custom (bytes/bytearray/memoryview):
            Optional. A customization byte string (``S`` in SP 800-185).

    Returns:
        A :class:`KMAC_Hash` hash object
    r   NzYou must pass a key to KMAC128r(   z1The key must be at least 128 bits long (16 bytes)r   r   é@   r   z!'mac_len' must be 8 bytes or morer   ó    zUnknown parameters: Z19é¨   )Úpopr   r   Úlenr*   Ústrr   r   )r.   r   r   r   r   r   r   r   r)   Ž   s   r)   N)Úbinasciir   ZCryptodome.Util.py3compatr   r   r   ZCryptodome.Randomr   r!   r   r	   r
   r   r   Úobjectr   r)   r   r   r   r   Ú<module>   s   f