B
    
h*                 @   sp   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 deZd	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               @   s,   e Zd ZdZdd Zd	ddZd
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             C   s   t  | _t| t|tt|| j }|r>t	d| t
| j tj| _|  t|| _tdd|| _| j| _| j| j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)r   _stateraw_cbc_libZCBC_start_operationgetr
   r	   len
address_of
ValueErrorr   ZCBC_stop_operationrelease
block_sizer   ivIVencryptdecrypt_next)selfblock_cipherr   result r   TC:\Users\sanjo\AppData\Local\Qlobot\Launcher\ext_packages\Crypto\Cipher\_mode_cbc.py__init__N   s    




zCbcMode.__init__Nc             C   s   | j | jkrtd| j g| _|dkr4tt|}n4|}t|sHtdt|t|krhtdt| t| j	
 t|t|tt|}|r|dkrtd| j td| |dkrt|S d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``.
        z*encrypt() cannot be called after decrypt()Nz4output must be a bytearray or a writeable memoryviewz9output must have the same length as the input  (%d bytes)   z3Data must be padded to %d byte boundary in CBC modez%Error %d while encrypting in CBC mode)r   r   	TypeErrorr   r   r   r   r   ZCBC_encryptr   r   r
   r	   r   r   )r   	plaintextoutput
ciphertextr   r   r   r   r   }   s,    $
zCbcMode.encryptc             C   s   | j | jkrtd| j g| _|dkr4tt|}n4|}t|sHtdt|t|krhtdt| t| j	
 t|t|tt|}|r|dkrtd| j td| |dkrt|S d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``.
        z*decrypt() cannot be called after encrypt()Nz4output must be a bytearray or a writeable memoryviewz9output must have the same length as the input  (%d bytes)r    z3Data must be padded to %d byte boundary in CBC modez%Error %d while decrypting in CBC mode)r   r   r!   r   r   r   r   r   ZCBC_decryptr   r   r
   r	   r   r   )r   r$   r#   r"   r   r   r   r   r      s,    !
zCbcMode.decrypt)N)N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   ?   s   /
Bc             K   s   |  |}|dd}|dd}d||fkr8t| j}|dk	rR|dk	rVtdn|}t|| jkrrtd| j |rtdt| 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   r   r   r   _create_cbc_cipher   s    



r/   N)r(   __all__Crypto.Util.py3compatr   Crypto.Util._raw_apir   r   r   r   r   r	   r
   r   ZCrypto.Randomr   r   objectr   r/   r   r   r   r   <module>!   s   ( A