
    9iu8                         S r S/rSSKrSSKJr  SSKJrJrJr  SSK	J
r
  SSKJr  SSKJrJr  SS	KJrJr  SS
KJr   " S S\5      rS rg)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                   d    \ rS rSrSrS rS rSS jrSS jrS r	S	 r
S
 rS rSS jrSS jrSrg)r   3   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                 F   UR                   U l          [        SSU5      U l         X@l        SU l        / SQU l        SU R                  s=::  a  U R                   ::  d  O  [        SU R                   -  5      e[        U R                  5      S:X  a  [        S5      e[        U5      (       d  [        S5      e[        SS5       Vs/ s HB  n[        R                  " US	U R                   S
-
  -  [        R                  " SU5      -   UUS9PMD     snU l        U R                  S   R!                  U R                  5        U R                  S
   U l        [%        U R                  S   R'                  5       5      nUR                  " UUR(                  4USS.UD6U l        gs  snf )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           W/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/Crypto/Cipher/_mode_eax.py__init__EaxMode.__init__P   s~    ",,@ tU3
=*
 T]]5doo5R#/ 0 0 tzz?a@AAJKK q!
 %A	  DOOa$786;;sA;NN#*'46 %
 	

1TZZ(zz!} $DJJqM$8$8$:;{{3#*#3#341<),4 &3	4s   A	Fc                     SU R                   ;  a  [        S5      e/ SQU l         U R                  R                  U5        U $ )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   )r1   
assoc_datas     r7   r   EaxMode.update   sG    * 4::% D E E*
 	J'r   Nc                    SU R                   ;  a  [        S5      eSS/U l         U R                  R                  XS9nUc   U R                  S   R                  U5        U$ U R                  S   R                  U5        U$ )a9  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(   r0   r   r-   r   )r1   	plaintextr?   cts       r7   r   EaxMode.encrypt   s    B DJJ& = > >*
\\!!)!;>JJqM  $ 	 JJqM  (	r   c                     SU R                   ;  a  [        S5      eSS/U l         U R                  S   R                  U5        U R                  R                  XS9$ )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   r0   r   )r1   
ciphertextr?   s      r7   r   EaxMode.decrypt   s`    B DJJ& C D D*


1Z(||##J#>>r   c                 8   SU R                   ;  a  [        S5      eS/U l         U R                  (       d[  SU R                  -  n[	        S5       H)  n[        XR                  U   R                  5       5      nM+     USU R                   U l        U R                  $ )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#   )r1   tagr5   s      r7   r   EaxMode.digest   s     4::% ; < <Z
}}DOO+C1XS**Q-"6"6"89 /DM}}r   c           	          SR                  U R                  5        Vs/ s H  nS[        U5      -  PM     sn5      $ s  snf )zlCompute the *printable* MAC tag.

This method is like `digest`.

:Return: the MAC, as a hexadecimal string.
 z%02x)joinr   r   )r1   xs     r7   	hexdigestEaxMode.hexdigest
  s4     ww$++-@-Qa(-@AA@s   =c                    SU R                   ;  a  [        S5      eS/U l         U R                  (       d[  SU R                  -  n[	        S5       H)  n[        X R                  U   R                  5       5      nM+     USU R                   U l        [        S5      n[        R                  " SX@R                  S9n[        R                  " SXAS9nUR                  5       UR                  5       :w  a  [        S	5      eg)
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_bitsr3   datazMAC check failed)r%   r(   r$   r"   r)   r	   r-   r   r#   r   r   r*   r&   )r1   received_mac_tagrG   r5   secretmac1mac2s          r7   r   EaxMode.verify  s    " 4::% = > >Z
}}DOO+C1XS**Q-"6"6"89 /DM!"%{{s]]K{{sN;;=DKKM)/00 *r   c                 8    U R                  [        U5      5        g)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   )r1   hex_mac_tags     r7   	hexverifyEaxMode.hexverify7  s     	Ik*+r   c                 @    U R                  XS9U R                  5       4$ )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   )r1   r@   r?   s      r7   encrypt_and_digestEaxMode.encrypt_and_digestF  s     ( ||I|5t{{}DDr   c                 F    U R                  XS9nU R                  U5        U$ )aq  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   )r1   rD   rT   r?   pts        r7   decrypt_and_verifyEaxMode.decrypt_and_verify\  s&    & \\*\4$%	r   )r0   r#   r$   r%   r-   r.   r"   r!   )N)__name__
__module____qualname____firstlineno____doc__r8   r   r   r   r   rM   r   r[   r^   rb   __static_attributes__ r   r7   r   r   3   s?    8-4^>*X&?P0B"1H,E,r   c                 
    UR                  S5      nUR                  SS5      nUc  [        S5      nUR                  SU R                  5      n[        XX4U5      $ ! [         a  n[	        S[        U5      -   5      eSnAff = f)a  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).
r3   r!   NrP   r4   zMissing parameter: )popr   r"   KeyErrorr(   strr   )r2   kwargsr3   r!   r4   es         r7   _create_eax_cipherrq   t  s    68jj

7D)=$R(E**Y(:(:; 788  8-A6778s   AA 
B&A==B)rh   __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   rq   rj   r   r7   <module>r{      sB   > +   @ @ * % ; % *~f ~B
$9r   