HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/env/lib/python3.10/site-packages/Crypto/Hash/__pycache__/CMAC.cpython-310.pyc
o

weo(�@s�ddlmZddlmZddlmZddlmZmZddl	m
Z
mZmZddl
mZdZddd	�ZGd
d�de�Z		dd
d�ZdS)�)�	unhexlify)�BLAKE2s)�strxor)�
long_to_bytes�
bytes_to_long)�bord�tobytes�_copy_bytes)�get_random_bytesNcCs,t|�d>|A}t|t|��t|�d�S)N�)rr�len)�bs�xor_lsb�num�r�I/home/arjun/projects/env/lib/python3.10/site-packages/Crypto/Hash/CMAC.py�_shift_bytes$src@sTeZdZdZdZdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�ZdS)�CMACz�A 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
    NcCsD||_tdd|�|_||_||_|j|_}d|_||_|dkr&d}d|_	n|dkr0d}d|_	nt
d|��d|}	|j||jfi|j��|_
|j
�|	�}
t|
d	�d
@r\t|
|�|_nt|
�|_t|jd	�d
@rrt|j|�|_nt|j�|_|j||j|	fi|j��|_t|�|_d	|_|	|_d|_d	|_|r�|�|�dSdS)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�new�MODE_ECB�_ecb�encryptrr�_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
�const_Rb�
zero_block�Lrrr�__init__3sV���
��
�z
CMAC.__init__cCs|jdur|jstd��|jt|�7_|j}|jdkrUt||jt|��}|d|�|j|j|j|�<|j|7_|j|krD|St	|�|d�}|�
|j�d|_t|�|}|dkrv|�
|d|��||d�|jd|�<n|�
|�||_|S)z�Authenticate 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#r1rrr.�minr-�
memoryview�_update)r3r5r
�filler�remainrrrr2os(


zCMAC.updatecCs�|j}t|�|dks
J�t|�dkrdS|j�|�}t|�|kr%|j}n
||d|�}||d�|_t|||d��|_dS)z,Update a block aligned to the block boundaryrN�)rrr+r'r/rr0)r3�
data_blockr
�ct�second_lastrrrr@�szCMAC._updatecCs^|�t�}|j��|_|jj|j|jj|jfi|j	��|_
|jdd�|_|jdd�|_|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__�copyrr$rr*r/rr+r-)r3�objrrrrI�s

��z	CMAC.copycCs�|j}|jdur|js|jS|j|jkrtd��|jdkr*|jdkr*t|j|j	�}n!|j
dd�}dd||jd||jd�<tt|j|�|j�}|j
�|�d|j�|_|jS)z�Return 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
        NzMAC is unsafe for this messager��rr)rr r!r1r"�
ValueErrorr.rr0r(r-r/r)r&r'r)r3r
�pt�partialrrr�digest�s	 zCMAC.digestcCsd�dd�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
        �cSsg|]}dt|��qS)z%02x)r)�.0�xrrr�
<listcomp>�s�z"CMAC.hexdigest.<locals>.<listcomp>)�join�tuplerO)r3rrr�	hexdigest�s

�zCMAC.hexdigestcCsHtd�}tjd||d�}tjd||��d�}|��|��kr"td��dS)agVerify 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�)�digest_bitsr4�datazMAC check failedN)r
rr$rOrL)r3�mac_tag�secret�mac1�mac2rrr�verify�s�zCMAC.verifycCs|�tt|���dS)rPN)r_rr)r3�hex_mac_tagrrr�	hexverify�szCMAC.hexverify)
�__name__�
__module__�__qualname__�__doc__rr=r2r@rIrOrWr_rarrrrr)s<"rFcCsl|durtd��|durint|�}|dur|j}|dkr!td��||jkr-td|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:`Crypto.Cipher`.
            The cipher's block size has to be 128 bits,
            like :mod:`Crypto.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#�dictrrLr)r4r5r6r7r8r9rrrr$�s 
�r$)r)NNNNF)�binasciir�Crypto.Hashr�Crypto.Util.strxorr�Crypto.Util.numberrr�Crypto.Util.py3compatrrr	�
Crypto.Randomr
rr�objectrr$rrrr�<module>s
W�