U
    g                      @   sX   d Z dgZddlmZmZmZmZmZmZm	Z	m
Z
 eddZG dd deZdd Zd	S )
z"
Electronic Code Book (ECB) mode.
EcbMode    )load_pycryptodome_raw_libVoidPointercreate_string_bufferget_raw_bufferSmartPointerc_size_tc_uint8_ptris_writeable_bufferzCryptodome.Cipher._raw_ecbak  
                    int ECB_start_operation(void *cipher,
                                            void **pResult);
                    int ECB_encrypt(void *ecbState,
                                    const uint8_t *in,
                                    uint8_t *out,
                                    size_t data_len);
                    int ECB_decrypt(void *ecbState,
                                    const uint8_t *in,
                                    uint8_t *out,
                                    size_t data_len);
                    int ECB_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  *Electronic Code Book (ECB)*.

    This is the simplest encryption mode. Each of the plaintext blocks
    is directly encrypted into a ciphertext block, independently of
    any other block.

    This mode is dangerous because it exposes frequency of symbols
    in your plaintext. Other modes (e.g. *CBC*) should be used instead.

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

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

    :undocumented: __init__
    c                 C   sV   |j | _ t | _t| | j }|r6td| t| j tj	| _|
  dS )zCreate a new block cipher, configured in ECB mode.

        :Parameters:
          block_cipher : C pointer
            A smart pointer to the low-level block cipher instance.
        z)Error %d while instantiating the ECB modeN)
block_sizer   _stateraw_ecb_libZECB_start_operationgetZ
address_of
ValueErrorr   ZECB_stop_operationrelease)selfZblock_cipherresult r   ?/tmp/pip-unpacked-wheel-_q8s9isk/Cryptodome/Cipher/_mode_ecb.py__init__D   s    

zEcbMode.__init__Nc                 C   s   |dkrt t|}n4|}t|s*tdt|t|krJtdt| t| j t	|t	|t
t|}|r|dkrtdtd| |dkrt|S dS dS )ab  Encrypt data with the key set at initialization.

        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.
            The length must be multiple of the cipher block 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``.
        N4output must be a bytearray or a writeable memoryview9output must have the same length as the input  (%d bytes)   2Data must be aligned to block boundary in ECB modez%Error %d while encrypting in ECB mode)r   lenr
   	TypeErrorr   r   ZECB_encryptr   r   r	   r   r   )r   	plaintextoutput
ciphertextr   r   r   r   encrypt^   s*    
zEcbMode.encryptc                 C   s   |dkrt t|}n4|}t|s*tdt|t|krJtdt| t| j t	|t	|t
t|}|r|dkrtdtd| |dkrt|S dS dS )ae  Decrypt data with the key set at initialization.

        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.
            The length must be multiple of the cipher block 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``.
        Nr   r   r   r   z%Error %d while decrypting in ECB mode)r   r   r
   r   r   r   ZECB_decryptr   r   r	   r   r   )r   r   r   r   r   r   r   r   decrypt   s*    
zEcbMode.decrypt)N)N)__name__
__module____qualname____doc__r   r   r    r   r   r   r   r   3   s   
7c                 K   s.   |  |}| j|_|r&tdt| t|S )aQ  Instantiate a cipher object that performs ECB encryption/decryption.

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

    All keywords are passed to the underlying block cipher.
    See the relevant documentation for details (at least ``key`` will need
    to be presentzUnknown parameters for ECB: %s)Z_create_base_cipherr   r   strr   )factorykwargsZcipher_stater   r   r   _create_ecb_cipher   s
    
r(   N)r$   __all__ZCryptodome.Util._raw_apir   r   r   r   r   r   r	   r
   r   objectr   r(   r   r   r   r   <module>   s   (
 