
    ,h=                         d Z dgZddlZddlmZmZmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZ ddlmZ  edd	      Z G d
 de      Zd Zy)z
Counter (CTR) mode.
CtrMode    N)load_pycryptodome_raw_libVoidPointercreate_string_bufferget_raw_bufferSmartPointerc_size_tc_uint8_ptris_writeable_buffer)get_random_bytes)_copy_bytesis_native_int)long_to_byteszCrypto.Cipher._raw_ctra  
                    int CTR_start_operation(void *cipher,
                                            uint8_t   initialCounterBlock[],
                                            size_t    initialCounterBlock_len,
                                            size_t    prefix_len,
                                            unsigned  counter_len,
                                            unsigned  littleEndian,
                                            void **pResult);
                    int CTR_encrypt(void *ctrState,
                                    const uint8_t *in,
                                    uint8_t *out,
                                    size_t data_len);
                    int CTR_decrypt(void *ctrState,
                                    const uint8_t *in,
                                    uint8_t *out,
                                    size_t data_len);
                    int CTR_stop_operation(void *ctrState);c                   &    e Zd ZdZd ZddZddZy)r   a*  *CounTeR (CTR)* mode.

    This mode is very similar to ECB, in that
    encryption of one block is done independently of all other blocks.

    Unlike ECB, the block *position* contributes to the encryption
    and no information leaks about symbol frequency.

    Each message block is associated to a *counter* which
    must be unique across all messages that get encrypted
    with the same key (not just within the same message).
    The counter is as big as the block size.

    Counters can be generated in several ways. The most
    straightword one is to choose an *initial counter block*
    (which can be made public, similarly to the *IV* for the
    other modes) and increment its lowest **m** bits by one
    (modulo *2^m*) for each block. In most cases, **m** is
    chosen to be half the block size.

    See `NIST SP800-38A`_, Section 6.5 (for the mode) and
    Appendix B (for how to manage the *initial counter block*).

    .. _`NIST SP800-38A` : http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf

    :undocumented: __init__
    c           
         t        |      ||z   k(  rt        d||      | _        	 t               | _        t
        j                  |j                         t        |      t        t        |            t        |      ||| j                  j                               }|rt        d|z        t        | j                  j                         t
        j                        | _        |j                          t        |      | _        	 ddg| _        y)aB  Create a new block cipher, configured in CTR mode.

        :Parameters:
          block_cipher : C pointer
            A smart pointer to the low-level block cipher instance.

          initial_counter_block : bytes/bytearray/memoryview
            The initial plaintext to use to generate the key stream.

            It is as large as the cipher block, and it embeds
            the initial value of the counter.

            This value must not be reused.
            It shall contain a nonce or a random component.
            Reusing the *initial counter block* for encryptions
            performed with the same key compromises confidentiality.

          prefix_len : integer
            The amount of bytes at the beginning of the counter block
            that never change.

          counter_len : integer
            The length in bytes of the counter embedded in the counter
            block.

          little_endian : boolean
            True if the counter in the counter block is an integer encoded
            in little endian mode. If False, it is big endian.
        Nz)Error %X while instantiating the CTR modeencryptdecrypt)lenr   noncer   _stateraw_ctr_libCTR_start_operationgetr
   r	   
address_of
ValueErrorr   CTR_stop_operationrelease
block_size_next)selfblock_cipherinitial_counter_block
prefix_lencounter_lenlittle_endianresults          Y/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/Crypto/Cipher/_mode_ctr.py__init__zCtrMode.__init__Y   s    @ $%k)AA$T:7LMDJA!m001A1A1C1<=R1S19#>S:T1U19*1E1<1>151G1G1IK H%& ' '
 #4;;??#4#.#A#AC
 	34@+
    Nc           
         d| j                   vrt        d      dg| _         |t        t        |            }nF|}t	        |      st        d      t        |      t        |      k7  rt        dt        |      z        t        j                  | j                  j                         t        |      t        |      t        t        |                  }|r|dk(  rt        d      t        d|z        |t        |      S y)	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 is returned as ``bytes``.
          Otherwise, ``None``.
        r   z*encrypt() cannot be called after decrypt()N4output must be a bytearray or a writeable memoryview9output must have the same length as the input  (%d bytes)  *The counter has wrapped around in CTR modez%Error %X while encrypting in CTR mode)r   	TypeErrorr   r   r   r   r   CTR_encryptr   r   r
   r	   OverflowErrorr   )r    	plaintextoutput
ciphertextr&   s        r'   r   zCtrMode.encrypt   s   B DJJ&HII[
>-c)n=JJ&v. VWW9~V,  "025i."A B B (():)4Y)?)4Z)@)1#i.)AC  # %0 1 1DvMNN>!*--r)   c           
         d| j                   vrt        d      dg| _         |t        t        |            }nF|}t	        |      st        d      t        |      t        |      k7  rt        dt        |      z        t        j                  | j                  j                         t        |      t        |      t        t        |                  }|r|dk(  rt        d      t        d|z        |t        |      S y)	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 is returned as ``bytes``.
          Otherwise, ``None``.
        r   z*decrypt() cannot be called after encrypt()Nr+   r,   r-   r.   z%Error %X while decrypting in CTR mode)r   r/   r   r   r   r   r   CTR_decryptr   r   r
   r	   r1   r   )r    r4   r3   r2   r&   s        r'   r   zCtrMode.decrypt   s   B DJJ&HII[
>,S_=II&v. VWW:#f+-  "025i."A B B (():)4Z)@)4Y)?)1#j/)BD  # %0 1 1DvMNN>!),,r)   )N)__name__
__module____qualname____doc__r(   r   r    r)   r'   r   r   <   s    8<,|>@>r)   c           	      b   | j                  |      }|j                  dd      }|j                  dd      }|j                  dd      }|rt        dt        |      z        |||fdk7  rt        d      ||3| j                  dk  rt        d	      t        | j                  d
z        }n#t        |      | j                  k\  rt        d      | j                  t        |      z
  }|d}t        |      r)d|dz  z  dz
  |k  rt        d      |t        ||      z   }n,t        |      |k7  rt        dt        |      |fz        ||z   }t        ||t        |      |d      S t        |      }	 |j                  d      }|j                  d      }	|j                  d      }
|j                  d      }|j                  d      }g }|dkD  r3|j                  t        j                  d|dz               |dz  }|dkD  r3|dgt!        d|t        |      z
        z  z  }|s|j#                          |	dj%                  |      z   |
z   }t        |      | j                  k7  r#t        dt        |      | j                  fz        t        ||t        |	      ||      S # t        $ r t        d      w xY w)a4  Instantiate a cipher object that performs CTR encryption/decryption.

    :Parameters:
      factory : module
        The underlying block cipher, a module from ``Crypto.Cipher``.

    :Keywords:
      nonce : bytes/bytearray/memoryview
        The fixed part at the beginning of the counter block - the rest is
        the counter number that gets increased when processing the next block.
        The nonce must be such that no two messages are encrypted under the
        same key and the same nonce.

        The nonce must be shorter than the block size (it can have
        zero length; the counter is then as long as the block).

        If this parameter is not present, a random nonce will be created with
        length equal to half the block size. No random nonce shorter than
        64 bits will be created though - you must really think through all
        security consequences of using such a short block size.

      initial_value : posive integer or bytes/bytearray/memoryview
        The initial value for the counter. If not present, the cipher will
        start counting from 0. The value is incremented by one for each block.
        The counter number is encoded in big endian mode.

      counter : object
        Instance of ``Crypto.Util.Counter``, which allows full customization
        of the counter block. This parameter is incompatible to both ``nonce``
        and ``initial_value``.

    Any other keyword will be passed to the underlying block cipher.
    See the relevant documentation for details (at least ``key`` will need
    to be present).
    counterNr   initial_valuez#Invalid parameters for CTR mode: %s)NNz<'counter' and 'nonce'/'initial_value' are mutually exclusive   z7Impossible to create a safe nonce for short block sizes   zNonce is too longr         z"Initial counter value is too largez@Incorrect length for counter byte string (%d bytes, expected %d)Fr$   prefixsuffixr%   z6Incorrect counter object (use Crypto.Util.Counter.new)B       r)   z?Size of the counter block (%d bytes) must match block size (%d))_create_base_cipherpopr/   strr   r   r   r   r   r   r   dictKeyErrorappendstructpackmaxreversejoin)factorykwargscipher_stater=   r   r>   r$   r"   _counterrC   rD   r%   wordss                r'   _create_ctr_cipherrX     s   J ..v6LjjD)GJJw%EJJ5M=FKLL}5E 2 3 	3 =!!B& !/ 0 0$W%7%71%<=E5zW/// !455 ((3u:5 M'kAo&!+m; !EFF$)M-,U$U!=![0 !c"%m"4k!B"C D D$)M$9!|,5z"	 	 G}H:ll=1h'h' _5 _5 E
!
V[[mc&9:;! !
 
gYQc%j 8999E"SXXe_4v=
 !W%7%77 ,/23H/I/6/A/A/CC D 	D <!6v;]< <'  : 9 : 	::s   /AJ J.)r:   __all__rN   Crypto.Util._raw_apir   r   r   r   r   r	   r
   r   Crypto.Randomr   Crypto.Util.py3compatr   r   Crypto.Util.numberr   r   objectr   rX   r;   r)   r'   <module>r_      sX   . + 7 7 7
 + < ,'(@ C? *(Yf Yxq<r)   