§
    m:Rfˆ*  ã                   óŠ   — d Z dgZddlm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  edd¦  «        Z G d„ de¦  «        Zd	„ Zd
S )z'
Ciphertext Block Chaining (CBC) mode.
ÚCbcModeé    )Ú_copy_bytes)Úload_pycryptodome_raw_libÚVoidPointerÚcreate_string_bufferÚget_raw_bufferÚSmartPointerÚc_size_tÚc_uint8_ptrÚis_writeable_buffer)Úget_random_byteszCrypto.Cipher._raw_cbca®  
                int CBC_start_operation(void *cipher,
                                        const uint8_t iv[],
                                        size_t iv_len,
                                        void **pResult);
                int CBC_encrypt(void *cbcState,
                                const uint8_t *in,
                                uint8_t *out,
                                size_t data_len);
                int CBC_decrypt(void *cbcState,
                                const uint8_t *in,
                                uint8_t *out,
                                size_t data_len);
                int CBC_stop_operation(void *state);
                c                   ó(   — e Zd ZdZd„ Zdd„Zdd„ZdS )r   a[  *Cipher-Block Chaining (CBC)*.

    Each of the ciphertext blocks depends on the current
    and all previous plaintext blocks.

    An Initialization Vector (*IV*) is required.

    See `NIST SP800-38A`_ , Section 6.2 .

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

    :undocumented: __init__
    c           	      óF  — t          ¦   «         | _        t                               |                     ¦   «         t          |¦  «        t          t          |¦  «        ¦  «        | j                             ¦   «         ¦  «        }|rt          d|z  ¦  «        ‚t          | j                             ¦   «         t          j        ¦  «        | _        |                     ¦   «          t          |¦  «        | _        	 t          dd|¦  «        | _        	 | j        | _        	 ddg| _        dS )a  Create a new block cipher, configured in CBC mode.

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

          iv : bytes/bytearray/memoryview
            The initialization vector to use for encryption or decryption.
            It is as long as the cipher block.

            **The IV must be unpredictable**. Ideally it is picked randomly.

            Reusing the *IV* for encryptions performed with the same key
            compromises confidentiality.
        z)Error %d while instantiating the CBC modeNÚencryptÚdecrypt)r   Ú_stateÚraw_cbc_libÚCBC_start_operationÚgetr   r
   ÚlenÚ
address_ofÚ
ValueErrorr	   ÚCBC_stop_operationÚreleaseÚ
block_sizer   ÚivÚIVÚ_next)ÚselfÚblock_cipherr   Úresults       úJ/var/www/bsim/venv/lib/python3.11/site-packages/Crypto/Cipher/_mode_cbc.pyÚ__init__zCbcMode.__init__N   s	  € õ" "‘m”mˆŒÝ×0Ò0°×1AÒ1AÑ1CÔ1CÝ1<¸R±´Ý19½#¸b¹'¼'Ñ1BÔ1BØ15´×1GÒ1GÑ1IÔ1IñKô Kˆð ð 	'ÝÐHØ%ñ&ñ 'ô 'ð 'õ
 # 4¤;§?¢?Ñ#4Ô#4Ý#.Ô#AñCô CˆŒð
 	×ÒÑÔÐå˜b™'œ'ˆŒØ@å˜d D¨"Ñ-Ô-ˆŒð	&ð ”'ˆŒØà Ð+ˆŒ
ˆ
ˆ
ó    Nc           
      ó–  — d| j         vrt          d¦  «        ‚dg| _         |€t          t          |¦  «        ¦  «        }n_|}t	          |¦  «        st          d¦  «        ‚t          |¦  «        t          |¦  «        k    rt          dt          |¦  «        z  ¦  «        ‚t                               | j         	                    ¦   «         t          |¦  «        t          |¦  «        t          t          |¦  «        ¦  «        ¦  «        }|r/|dk    rt          d| j        z  ¦  «        ‚t          d|z  ¦  «        ‚|€t          |¦  «        S d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)

        That also means that you cannot reuse an object for encrypting
        or decrypting other data with the same key.

        This function does not add any padding to the plaintext.

        :Parameters:
          plaintext : bytes/bytearray/memoryview
            The piece of data to encrypt.
            Its lenght must be multiple of the cipher block size.
        :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()Nú4output must be a bytearray or a writeable memoryviewú9output must have the same length as the input  (%d bytes)é   ú3Data must be padded to %d byte boundary in CBC modez%Error %d while encrypting in CBC mode)r   Ú	TypeErrorr   r   r   r   r   ÚCBC_encryptr   r   r   r
   r   r   )r   Ú	plaintextÚoutputÚ
ciphertextr!   s        r"   r   zCbcMode.encrypt}   sO  € ðH ˜DœJÐ&Ð&ÝÐHÑIÔIÐIØ�[ˆŒ
àˆ>Ý-­c°)©n¬nÑ=Ô=ˆJˆJàˆJå& vÑ.Ô.ð XÝÐ VÑWÔWÐWå�9‰~Œ~¥ V¡¤Ò,Ð,Ý ð "0Ý25°i±.´.ñ"Añ Bô Bð Bõ ×(Ò(¨¬¯ªÑ):Ô):Ý)4°YÑ)?Ô)?Ý)4°ZÑ)@Ô)@Ý)1µ#°i±.´.Ñ)AÔ)AñCô Cˆð ð 	OØ˜Š{ˆ{Ý Ð!VÐY]ÔYhÑ!hÑiÔiÐiÝÐDÀvÑMÑNÔNÐNàˆ>Ý! *Ñ-Ô-Ð-à�4r$   c           
      ó–  — d| j         vrt          d¦  «        ‚dg| _         |€t          t          |¦  «        ¦  «        }n_|}t	          |¦  «        st          d¦  «        ‚t          |¦  «        t          |¦  «        k    rt          dt          |¦  «        z  ¦  «        ‚t                               | j         	                    ¦   «         t          |¦  «        t          |¦  «        t          t          |¦  «        ¦  «        ¦  «        }|r/|dk    rt          d| j        z  ¦  «        ‚t          d|z  ¦  «        ‚|€t          |¦  «        S d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.
            Its length must be multiple of the cipher block size.
        :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 %d while decrypting in CBC mode)r   r*   r   r   r   r   r   ÚCBC_decryptr   r   r   r
   r   r   )r   r.   r-   r,   r!   s        r"   r   zCbcMode.decrypt¿   sO  € ðB ˜DœJÐ&Ð&ÝÐHÑIÔIÐIØ�[ˆŒ
àˆ>Ý,­S°©_¬_Ñ=Ô=ˆIˆIàˆIå& vÑ.Ô.ð XÝÐ VÑWÔWÐWå�:‰Œ¥# f¡+¤+Ò-Ð-Ý ð "0Ý25°i±.´.ñ"Añ Bô Bð Bõ ×(Ò(¨¬¯ªÑ):Ô):Ý)4°ZÑ)@Ô)@Ý)4°YÑ)?Ô)?Ý)1µ#°j±/´/Ñ)BÔ)BñDô Dˆð ð 	OØ˜Š{ˆ{Ý Ð!VÐY]ÔYhÑ!hÑiÔiÐiÝÐDÀvÑMÑNÔNÐNàˆ>Ý! )Ñ,Ô,Ð,à�4r$   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r#   r   r   © r$   r"   r   r   ?   s[   € € € € € ðð ð-,ð -,ð -,ð^@ð @ð @ð @ðD=ð =ð =ð =ð =ð =r$   c                 ó¨  — |                       |¦  «        }|                     dd¦  «        }|                     dd¦  «        }d||fk    rt          | j        ¦  «        }|�|�t	          d¦  «        ‚n|}t          |¦  «        | j        k    rt          d| j        z  ¦  «        ‚|rt	          dt          |¦  «        z  ¦  «        ‚t          ||¦  «        S )aó  Instantiate a cipher object that performs CBC encryption/decryption.

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

    :Keywords:
      iv : bytes/bytearray/memoryview
        The IV to use for CBC.

      IV : bytes/bytearray/memoryview
        Alias for ``iv``.

    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).
    r   Nr   )NNz*You must either use 'iv' or 'IV', not bothz.Incorrect IV length (it must be %d bytes long)zUnknown parameters for CBC: %s)	Ú_create_base_cipherÚpopr   r   r*   r   r   Ústrr   )ÚfactoryÚkwargsÚcipher_stater   r   s        r"   Ú_create_cbc_cipherr=   ÿ   sê   € ð& ×.Ò.¨vÑ6Ô6€LØ	�Š�D˜$Ñ	Ô	€BØ	�Š�D˜$Ñ	Ô	€Bà˜˜B�xÒÐÝ˜gÔ0Ñ1Ô1ˆØ	€~Øˆ>ÝÐHÑIÔIÐIð ð ˆå
ˆ2�w„w�'Ô$Ò$Ð$ÝÐIØ Ô+ñ,ñ -ô -ð 	-ð ð HÝÐ8½3¸v¹;¼;ÑFÑGÔGÐGå�< Ñ$Ô$Ð$r$   N)r4   Ú__all__ÚCrypto.Util.py3compatr   ÚCrypto.Util._raw_apir   r   r   r   r	   r
   r   r   ÚCrypto.Randomr   r   Úobjectr   r=   r5   r$   r"   ú<module>rC      s	  ðð>ð ð ˆ+€à -Ð -Ð -Ð -Ð -Ð -ð7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð
 +Ð *Ð *Ð *Ð *Ð *à'Ð'Ð(@ð Cñ ô €ð$}ð }ð }ð }ð }ˆfñ }ô }ð }ð@&%ð &%ð &%ð &%ð &%r$   