o
    n~b(                     @   s   d dl mZ d dlmZ d dlmZ d dlmZmZ d dl	m
Z
mZmZ d dlmZ dZddd	ZG d
d deZ		dddZdS )    )	unhexlify)BLAKE2s)strxor)long_to_bytesbytes_to_long)bordtobytes_copy_bytes)get_random_bytesNc                 C   s,   t | d> |A }t|t| t|  d  S )N   )r   r   len)bsZxor_lsbnum r   ?/usr/local/lib/python3.10/dist-packages/Cryptodome/Hash/CMAC.py_shift_bytes$   s   r   c                   @   sT   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d ZdS )CMACzA CMAC hash 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
    Nc                 C   sD  || _ td d || _|| _|| _|j | _}d | _|| _|dkr&d}d| _	n|dkr0d}d| _	nt
d| d| }	|j||jfi | j| _| j|	}
t|
d	 d
@ r\t|
|| _nt|
| _t| jd	 d
@ rrt| j|| _nt| j| _|j||j|	fi | j| _t|| _d	| _|	| _d | _d	| _|r| | d S d S )N      i         l          zACMAC requires a cipher with a block size of 8 or 16 bytes, not %d    r      )digest_sizer	   _key_factory_cipher_params
block_size_block_size_mac_tag_update_after_digest	_max_size	TypeErrornewZMODE_ECB_ecbencryptr   r   _k1_k2MODE_CBC_cbc	bytearray_cache_cache_n_last_ct_last_pt
_data_sizeupdate)selfkeymsg	ciphermodcipher_paramsmac_lenupdate_after_digestr   Zconst_RbZ
zero_blockLr   r   r   __init__3   sV   

zCMAC.__init__c                 C   s   | j dur| jstd|  jt|7  _| j}| jdkrUt|| j t|}|d| | j| j| j| < |  j|7  _| j|k rD| S t	||d }| 
| j d| _t|| }|dkrv| 
|d|   || d | jd|< n| 
| || _| S )zAuthenticate the next chunk of message.

        Args:
            data (byte string/byte array/memoryview): The next chunk of data
        Nz4update() cannot be called after digest() or verify()r   )r   r    r"   r/   r   r   r,   minr+   
memoryview_update)r1   r3   r   fillerZremainr   r   r   r0   o   s(   


zCMAC.updatec                 C   s   | j }t|| dksJ t|dkrdS | j|}t||kr%| j}n
|| d |  }|| d | _t||| d | _dS )z,Update a block aligned to the block boundaryr   N   )r   r   r)   r%   r-   r   r.   )r1   Z
data_blockr   ctZsecond_lastr   r   r   r<      s   zCMAC._updatec                 C   s^   |  t}| j |_| jj| j| jj| jfi | j	|_
| jdd |_| jdd |_|S )a+  Return a copy ("clone") of the CMAC object.

        The copy will have the same internal state as the original CMAC
        object.
        This can be used to efficiently compute the MAC tag of byte
        strings that share a common initial substring.

        :return: An :class:`CMAC`
        N)__new__r   __dict__copyr   r#   r   r(   r-   r   r)   r+   )r1   objr   r   r   rB      s   

z	CMAC.copyc                 C   s   | j }| jdur| js| jS | j| jkrtd| jdkr*| jdkr*t| j| j	}n!| j
dd }dd|| j d   || jd< tt| j|| j}| j|d| j | _| jS )zReturn the **binary** (non-printable) MAC tag of the message
        that has been authenticated so far.

        :return: The MAC tag, computed over the data processed so far.
                 Binary form.
        :rtype: byte string
        NzMAC is unsafe for this messager      r   r   )r   r   r    r/   r!   
ValueErrorr,   r   r.   r&   r+   r-   r'   r$   r%   r   )r1   r   ptpartialr   r   r   digest   s   	 zCMAC.digestc                 C   s   d dd t|  D S )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"CMAC.hexdigest.<locals>.<listcomp>)jointuplerH   )r1   r   r   r   	hexdigest   s   

zCMAC.hexdigestc                 C   sH   t d}tjd||d}tjd||  d}| | kr"tddS )ag  Verify that a given **binary** MAC (computed by another party)
        is valid.

        Args:
          mac_tag (byte string/byte array/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_bitsr2   datazMAC check failedN)r
   r   r#   rH   rE   )r1   Zmac_tagsecretZmac1Zmac2r   r   r   verify   s   zCMAC.verifyc                 C   s   |  tt| dS )rI   N)rT   r   r   )r1   Zhex_mac_tagr   r   r   	hexverify   s   zCMAC.hexverify)__name__
__module____qualname____doc__r   r9   r0   r<   rB   rH   rP   rT   rU   r   r   r   r   r   )   s    <"r   Fc                 C   sl   |du rt d|du ri nt|}|du r|j}|dk r!td||jkr-td|j t| |||||S )a  Create a new MAC object.

    Args:
        key (byte string/byte array/memoryview):
            key for the CMAC object.
            The key must be valid for the underlying cipher algorithm.
            For instance, it must be 16 bytes long for AES-128.
        ciphermod (module):
            A cipher module from :mod:`Cryptodome.Cipher`.
            The cipher's block size has to be 128 bits,
            like :mod:`Cryptodome.Cipher.AES`, to reduce the probability
            of collisions.
        msg (byte string/byte array/memoryview):
            Optional. The very first chunk of the message to authenticate.
            It is equivalent to an early call to `CMAC.update`. Optional.
        cipher_params (dict):
            Optional. A set of parameters to use when instantiating a cipher
            object.
        mac_len (integer):
            Length of the MAC, in bytes.
            It must be at least 4 bytes long.
            The default (and recommended) length matches the size of a cipher block.
        update_after_digest (boolean):
            Optional. By default, a hash object cannot be updated anymore after
            the digest is computed. When this flag is ``True``, such check
            is no longer enforced.
    Returns:
        A :class:`CMAC` object
    Nz%ciphermod must be specified (try AES)   z,MAC tag length must be at least 4 bytes longz>MAC tag length cannot be larger than a cipher block (%d) bytes)r"   dictr   rE   r   )r2   r3   r4   r5   r6   r7   r   r   r   r#      s    
r#   )r   )NNNNF)binasciir   ZCryptodome.Hashr   ZCryptodome.Util.strxorr   ZCryptodome.Util.numberr   r   ZCryptodome.Util.py3compatr   r   r	   ZCryptodome.Randomr
   r   r   objectr   r#   r   r   r   r   <module>   s   
 W