B
    Bd\R41                 @   sp   d Z ddlZejd dkr0ejd dkr0ddlT ddlT dZdZdZdZdZ	d	Z
d
ZdddZG dd dZdS )z4Module with definitions common to all block ciphers.    N      )*               c             C   s6   | | }t||kr.|r&td|  || }|p4|S )zFFind a parameter in tuple and dictionary arguments a function receivesz!Parameter '%s' is specified twice)getlen
ValueError)nameindexargskwargsdefaultZparam r   6lib/python3.7/site-packages/Crypto/Cipher/blockalgo.py_getParameter|   s    
r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )		BlockAlgoz)Class modelling an abstract block cipher.c             O   s  t dd||td| _|j| _| jtkrH|j|f||| _| jj| _n^d| _d| _	t dd||| _| jsrt
d|j|ttd| j | jd	 d
}t| j| jkr|| j| jdd   td| jd   d | jd  | _nt| j| jd krf| j| _|| jtd| jd   d | jd  | _| jdd  | jdd krTt
d| jd d | _nt
d| j| jd f |j|t| j| j d  | jd	 d
| _d S )Nmoder   )r   FZivr   zMODE_OPENPGP requires an IV    )Zsegment_sizer   z%Failed integrity check for OPENPGP IVz4Length of IV must be %d or %d bytes for MODE_OPENPGP)r   MODE_ECBr   
block_sizeMODE_OPENPGPnew_cipherZIV_done_first_block_done_last_blockr   MODE_CFBbr   encrypt_encrypted_IVdecrypt)selffactorykeyr   r   Z	IV_cipherr   r   r   __init__   s>    
	zBlockAlgo.__init__c             C   s   | j tkr| jt|| j  | j }|dkrn| jr>td| jd| _|td|  }| j|dt| }n| j|}| j	s| j
| }d| _	|S | j|S )aX  Encrypt data with the key and the parameters set at initialization.
        
        The cipher object is stateful; encryption of a long block
        of data can be broken up in two or more calls to `encrypt()`.
        That is, the statement:
            
            >>> c.encrypt(a) + c.encrypt(b)

        is always 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 perform any padding.
       
         - For `MODE_ECB`, `MODE_CBC`, and `MODE_OFB`, *plaintext* length
           (in bytes) must be a multiple of *block_size*.

         - For `MODE_CFB`, *plaintext* length (in bytes) must be a multiple
           of *segment_size*/8.

         - For `MODE_CTR`, *plaintext* can be of any length.

         - For `MODE_OPENPGP`, *plaintext* must be a multiple of *block_size*,
           unless it is the last chunk of the message.

        :Parameters:
          plaintext : byte string
            The piece of data to encrypt.
        :Return:
            the encrypted data, as a byte string. It is as long as
            *plaintext* with one exception: when encrypting the first message
            chunk with `MODE_OPENPGP`, the encypted IV is prepended to the
            returned ciphertext.
        r   zFOnly the last chunk is allowed to have length not multiple of %d bytesTr   N)r   r   r   r   r!   r   r#   r   r$   r    r%   )r'   Z	plaintextpadding_lengthpaddedresr   r   r   r$      s    '

zBlockAlgo.encryptc             C   s   | j tkr~| jt|| j  | j }|dkrn| jr>td| jd| _|td|  }| j|dt| }n| j|}|S | j|S )a  Decrypt data with the key and the parameters set at initialization.
        
        The cipher object is stateful; decryption of a long block
        of data can be broken up in two or more calls to `decrypt()`.
        That is, the statement:
            
            >>> c.decrypt(a) + c.decrypt(b)

        is always equivalent to:

             >>> c.decrypt(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 perform any padding.
       
         - For `MODE_ECB`, `MODE_CBC`, and `MODE_OFB`, *ciphertext* length
           (in bytes) must be a multiple of *block_size*.

         - For `MODE_CFB`, *ciphertext* length (in bytes) must be a multiple
           of *segment_size*/8.

         - For `MODE_CTR`, *ciphertext* can be of any length.

         - For `MODE_OPENPGP`, *plaintext* must be a multiple of *block_size*,
           unless it is the last chunk of the message.

        :Parameters:
          ciphertext : byte string
            The piece of data to decrypt.
        :Return: the decrypted data (byte string, as long as *ciphertext*).
        r   zFOnly the last chunk is allowed to have length not multiple of %d bytesTr   N)	r   r   r   r   r!   r   r#   r   r&   )r'   Z
ciphertextr+   r,   r-   r   r   r   r&      s    "
zBlockAlgo.decryptN)__name__
__module____qualname____doc__r*   r$   r&   r   r   r   r   r      s   3;r   )N)r1   sysversion_infoZCrypto.Util.py21compatZCrypto.Util.py3compatr   ZMODE_CBCr"   ZMODE_PGPZMODE_OFBZMODE_CTRr   r   r   r   r   r   r   <module>   s   
	