
    ,hu8                         d Z dgZddlZ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 dd	lmZmZ dd
lmZ  G d de      Zd Zy)z
EAX mode.
EaxMode    N)	unhexlify)byte_stringbord_copy_bytes)	is_buffer)strxor)long_to_bytesbytes_to_long)CMACBLAKE2s)get_random_bytesc                   T    e Zd ZdZd Zd ZddZddZd Zd Z	d	 Z
d
 ZddZddZy)r   a  *EAX* mode.

    This is an Authenticated Encryption with Associated Data
    (`AEAD`_) mode. It provides both confidentiality and authenticity.

    The header of the message may be left in the clear, if needed,
    and it will still be subject to authentication.

    The decryption step tells the receiver if the message comes
    from a source that really knowns the secret key.
    Additionally, decryption detects if any part of the message -
    including the header - has been modified or corrupted.

    This mode requires a *nonce*.

    This mode is only available for ciphers that operate on 64 or
    128 bits blocks.

    There are no official standards defining EAX.
    The implementation is based on `a proposal`__ that
    was presented to NIST.

    .. _AEAD: http://blog.cryptographyengineering.com/2012/05/how-to-choose-authenticated-encryption.html
    .. __: http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/eax/eax-spec.pdf

    :undocumented: __init__
    c                 4   |j                   | _         	 t        dd|      | _        	 || _        d| _        g d| _        d| j                  cxk  r| j                   k  sn t        d| j                   z        t        | j                        dk(  rt        d      t        |      st        d      t        dd      D cg c]B  }t        j                  |d	| j                   d
z
  z  t        j                  d|      z   ||      D c}| _        | j                  d   j!                  | j                         | j                  d
   | _        t%        | j                  d   j'                               } |j                  ||j(                  f|dd|| _        yc c}w )zEAX cipher modeNupdateencryptdecryptdigestverify   z3'mac_len' must be at least 2 and not larger than %dr   z!Nonce cannot be empty in EAX modez,nonce must be bytes, bytearray or memoryview          B)	ciphermodcipher_params    )initial_valuenonce)
block_sizer   r    _mac_len_mac_tag_next
ValueErrorlenr   	TypeErrorranger   newstructpack_omacr   _signerr   r   MODE_CTR_cipher)selffactorykeyr    mac_lenr   icounter_ints           Y/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/Crypto/Cipher/_mode_eax.py__init__zEaxMode.__init__P   sx    ",,@ tU3
=*
 T]]5doo5R#/ 0 0 tzz?a@AAJKK q!
 	  DOOa$786;;sA;NN#*'46
 	

1TZZ(zz!} $DJJqM$8$8$:;"w{{3#*#3#341<),4 &3	4s   =AFc                     d| j                   vrt        d      g d| _         | j                  j                  |       | S )a  Protect associated data

        If there is any associated data, the caller has to invoke
        this function one or more times, before using
        ``decrypt`` or ``encrypt``.

        By *associated data* it is meant any data (e.g. packet headers) that
        will not be encrypted and will be transmitted in the clear.
        However, the receiver is still able to detect any modification to it.

        If there is no associated data, this method must not be called.

        The caller may split associated data in segments of any size, and
        invoke this method multiple times, each time with the next segment.

        :Parameters:
          assoc_data : bytes/bytearray/memoryview
            A piece of associated data. There are no restrictions on its size.
        r   z<update() can only be called immediately after initializationr   )r$   r'   r-   r   )r0   
assoc_datas     r6   r   zEaxMode.update   sG    * 4::% D E E*
 	J'r   Nc                    d| j                   vrt        d      ddg| _         | j                  j                  ||      }| | j                  d   j                  |       |S | j                  d   j                  |       |S )a  Encrypt data with the key and the parameters set at initialization.

        A cipher object is stateful: once you have encrypted a message
        you cannot encrypt (or decrypt) another message using the same
        object.

        The data to encrypt can be broken up in two or
        more pieces and `encrypt` can be called multiple times.

        That is, the statement:

            >>> c.encrypt(a) + c.encrypt(b)

        is equivalent to:

             >>> c.encrypt(a+b)

        This function does not add any padding to the plaintext.

        :Parameters:
          plaintext : bytes/bytearray/memoryview
            The piece of data to encrypt.
            It can be of any length.
        :Keywords:
          output : bytearray/memoryview
            The location where the ciphertext must be written to.
            If ``None``, the ciphertext is returned.
        :Return:
          If ``output`` is ``None``, the ciphertext as ``bytes``.
          Otherwise, ``None``.
        r   z@encrypt() can only be called after initialization or an update()r   outputr   )r$   r'   r/   r   r,   r   )r0   	plaintextr<   cts       r6   r   zEaxMode.encrypt   s    B DJJ& = > >*
\\!!)F!;>JJqM  $ 	 JJqM  (	r   c                     d| j                   vrt        d      ddg| _         | j                  d   j                  |       | j                  j                  ||      S )a  Decrypt data with the key and the parameters set at initialization.

        A cipher object is stateful: once you have decrypted a message
        you cannot decrypt (or encrypt) another message with the same
        object.

        The data to decrypt can be broken up in two or
        more pieces and `decrypt` can be called multiple times.

        That is, the statement:

            >>> c.decrypt(a) + c.decrypt(b)

        is equivalent to:

             >>> c.decrypt(a+b)

        This function does not remove any padding from the plaintext.

        :Parameters:
          ciphertext : bytes/bytearray/memoryview
            The piece of data to decrypt.
            It can be of any length.
        :Keywords:
          output : bytearray/memoryview
            The location where the plaintext must be written to.
            If ``None``, the plaintext is returned.
        :Return:
          If ``output`` is ``None``, the plaintext as ``bytes``.
          Otherwise, ``None``.
        r   z@decrypt() can only be called after initialization or an update()r   r   r;   )r$   r'   r,   r   r/   r   )r0   
ciphertextr<   s      r6   r   zEaxMode.decrypt   sb    B DJJ& C D D*


1Z(||##Jv#>>r   c                 (   d| j                   vrt        d      dg| _         | j                  sZd| j                  z  }t	        d      D ])  }t        || j                  |   j                               }+ |d| j                   | _        | j                  S )zCompute the *binary* MAC tag.

        The caller invokes this function at the very end.

        This method returns the MAC that shall be sent to the receiver,
        together with the ciphertext.

        :Return: the MAC, as a byte string.
        r   zAdigest() cannot be called when decrypting or validating a messager   r   N)	r$   r'   r#   r!   r(   r	   r,   r   r"   )r0   tagr4   s      r6   r   zEaxMode.digest   s     4::% ; < <Z
}}DOO+C1X :S$**Q-"6"6"89:/DM}}r   c           	      ~    dj                  | j                         D cg c]  }dt        |      z   c}      S c c}w )zCompute the *printable* MAC tag.

        This method is like `digest`.

        :Return: the MAC, as a hexadecimal string.
         z%02x)joinr   r   )r0   xs     r6   	hexdigestzEaxMode.hexdigest
  s0     ww$++-@Qa(@AA@s   :c                    d| j                   vrt        d      dg| _         | j                  sZd| j                  z  }t	        d      D ])  }t        || j                  |   j                               }+ |d| j                   | _        t        d      }t        j                  d|| j                        }t        j                  d||      }|j                         |j                         k7  rt        d	      y)
a.  Validate the *binary* MAC tag.

        The caller invokes this function at the very end.

        This method checks if the decrypted message is indeed valid
        (that is, if the key is correct) and it has not been
        tampered with while in transit.

        :Parameters:
          received_mac_tag : bytes/bytearray/memoryview
            This is the *binary* MAC, as received from the sender.
        :Raises MacMismatchError:
            if the MAC does not match. The message has been tampered with
            or the key is incorrect.
        r   z3verify() cannot be called when encrypting a messager   r   N      )digest_bitsr2   datazMAC check failed)r$   r'   r#   r!   r(   r	   r,   r   r"   r   r   r)   r%   )r0   received_mac_tagrB   r4   secretmac1mac2s          r6   r   zEaxMode.verify  s    " 4::% = > >Z
}}DOO+C1X :S$**Q-"6"6"89:/DM!"%{{sT]]K{{s=MN;;=DKKM)/00 *r   c                 8    | j                  t        |             y)a]  Validate the *printable* MAC tag.

        This method is like `verify`.

        :Parameters:
          hex_mac_tag : string
            This is the *printable* MAC, as received from the sender.
        :Raises MacMismatchError:
            if the MAC does not match. The message has been tampered with
            or the key is incorrect.
        N)r   r   )r0   hex_mac_tags     r6   	hexverifyzEaxMode.hexverify7  s     	Ik*+r   c                 H    | j                  ||      | j                         fS )a\  Perform encrypt() and digest() in one step.

        :Parameters:
          plaintext : bytes/bytearray/memoryview
            The piece of data to encrypt.
        :Keywords:
          output : bytearray/memoryview
            The location where the ciphertext must be written to.
            If ``None``, the ciphertext is returned.
        :Return:
            a tuple with two items:

            - the ciphertext, as ``bytes``
            - the MAC tag, as ``bytes``

            The first item becomes ``None`` when the ``output`` parameter
            specified a location for the result.
        r;   )r   r   )r0   r=   r<   s      r6   encrypt_and_digestzEaxMode.encrypt_and_digestF  s"    ( ||If|5t{{}DDr   c                 N    | j                  ||      }| j                  |       |S )a  Perform decrypt() and verify() in one step.

        :Parameters:
          ciphertext : bytes/bytearray/memoryview
            The piece of data to decrypt.
          received_mac_tag : bytes/bytearray/memoryview
            This is the *binary* MAC, as received from the sender.
        :Keywords:
          output : bytearray/memoryview
            The location where the plaintext must be written to.
            If ``None``, the plaintext is returned.
        :Return: the plaintext as ``bytes`` or ``None`` when the ``output``
            parameter specified a location for the result.
        :Raises MacMismatchError:
            if the MAC does not match. The message has been tampered with
            or the key is incorrect.
        r;   )r   r   )r0   r@   rM   r<   pts        r6   decrypt_and_verifyzEaxMode.decrypt_and_verify\  s(    & \\*V\4$%	r   )N)__name__
__module____qualname____doc__r7   r   r   r   r   rG   r   rS   rU   rX    r   r6   r   r   3   s?    8-4^>*X&?P0B"1H,E,r   c                 
   	 |j                  d      }|j                  dd      }|t        d      }|j                  d| j                        }t        | ||||      S # t        $ r}t	        dt        |      z         d}~ww xY w)aY  Create a new block cipher, configured in EAX mode.

    :Parameters:
      factory : module
        A symmetric cipher module from `Crypto.Cipher` (like
        `Crypto.Cipher.AES`).

    :Keywords:
      key : bytes/bytearray/memoryview
        The secret key to use in the symmetric cipher.

      nonce : bytes/bytearray/memoryview
        A value that must never be reused for any other encryption.
        There are no restrictions on its length, but it is recommended to use
        at least 16 bytes.

        The nonce shall never repeat for two different messages encrypted with
        the same key, but it does not need to be random.

        If not specified, a 16 byte long random string is used.

      mac_len : integer
        Length of the MAC, in bytes. It must be no larger than the cipher
        block bytes (which is the default).
    r2   r    NrI   r3   zMissing parameter: )popr   r!   KeyErrorr'   strr   )r1   kwargsr2   r    r3   es         r6   _create_eax_cipherrd   t  s    68jj

7D)=$R(E**Y(:(:; 7C88  8-A6778s   AA 	B&A==B)r\   __all__r*   binasciir   Crypto.Util.py3compatr   r   r   Crypto.Util._raw_apir   Crypto.Util.strxorr	   Crypto.Util.numberr
   r   Crypto.Hashr   r   Crypto.Randomr   objectr   rd   r]   r   r6   <module>rn      sB   > +   @ @ * % ; % *~f ~B
$9r   