o
    Bd\R41  ã                   @   sp   d Z ddlZejd dkrejd dkrd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|  ƒ‚|| }|p|S )zFFind a parameter in tuple and dictionary arguments a function receivesz!Parameter '%s' is specified twice)ÚgetÚlenÚ
ValueError)ÚnameÚindexÚargsÚkwargsÚdefaultÚparam© r   úI/var/www/html/env/lib/python3.10/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r'|j|g|¢R i |¤Ž| _| jj| _d S d| _d| _	t dd||ƒ| _| js<t
dƒ‚|j|ttdƒ| j | jd	 d
�}t| jƒ| jkrt| | j| jdd …  tdƒ| jd   ¡d | jd … | _nLt| jƒ| jd kr´| j| _| | jtdƒ| jd   ¡d | jd … | _| jdd … | jdd… kr«t
dƒ‚| jd d… | _nt
d| j| jd f ƒ‚|j|t| j| j d … | jd	 d
�| _d S )NÚmoder   )r   FÚivr   zMODE_OPENPGP requires an IVú é   )Úsegment_sizeéþÿÿÿr   éüÿÿÿ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Ú_cipherÚIVÚ_done_first_blockÚ_done_last_blockr   ÚMODE_CFBÚbr   ÚencryptÚ_encrypted_IVÚdecrypt)ÚselfÚfactoryÚkeyr   r   Ú	IV_cipherr   r   r   Ú__init__ˆ   sN   
	þÿÿýÿþÿþzBlockAlgo.__init__c                 C   s    | j tkrJ| jt|ƒ| j  | j }|dkr7| jrtd| jƒ‚d| _|tdƒ|  }| j |¡dt|ƒ… }n| j |¡}| j	sH| 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   úFOnly 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+   Ú	plaintextÚpadding_lengthÚpaddedÚresr   r   r   r(   »   s    
'ÿ
zBlockAlgo.encryptc                 C   sŒ   | j tkr@| jt|ƒ| j  | j }|dkr8| jrtd| jƒ‚d| _|tdƒ|  }| j |¡dt|ƒ… }|S | 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   r0   Tr   N)	r   r    r   r   r%   r   r'   r"   r*   )r+   Ú
ciphertextr2   r3   r4   r   r   r   r*   ö   s   
"ÿÿzBlockAlgo.decryptN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r/   r(   r*   r   r   r   r   r   …   s
    3;r   )N)r9   ÚsysÚversion_infoÚCrypto.Util.py21compatÚCrypto.Util.py3compatr   ÚMODE_CBCr&   ÚMODE_PGPÚMODE_OFBÚMODE_CTRr    r   r   r   r   r   r   Ú<module>   s   
	