B Bd\R41@spdZddlZejddkr0ejddkr0ddlTddlTdZdZdZdZdZ d Z d Z dd d Z Gd ddZ dS)z4Module with definitions common to all block ciphers.N)*cCs6||}t||kr.|r&td|||}|p4|S)zFFind a parameter in tuple and dictionary arguments a function receivesz!Parameter '%s' is specified twice)getlen ValueError)nameindexargskwargsdefaultZparamrJ/opt/alt/python37/lib64/python3.7/site-packages/Crypto/Cipher/blockalgo.py _getParameter|s    rc@s(eZdZdZddZddZddZdS) BlockAlgoz)Class modelling an abstract block cipher.cOstdd||td|_|j|_|jtkrH|j|f|||_|jj|_n^d|_d|_ tdd|||_|jsrt d|j|t t d|j|jd d }t |j|jkr||j|jd dt d|jd d|jd |_nt |j|jd krf|j|_||jt d|jd d|jd |_|jd d|jd d krTt d|jdd |_nt d|j|jd f|j|t |j|j d|jd d |_dS)Nmoder)rFZivrzMODE_OPENPGP requires an IV)Z segment_sizerz%Failed integrity check for OPENPGP IVz4Length of IV must be %d or %d bytes for MODE_OPENPGP)rMODE_ECBr block_size MODE_OPENPGPnew_cipherZIV_done_first_block_done_last_blockr MODE_CFBbr encrypt _encrypted_IVdecrypt)selffactorykeyrrZ IV_cipherrrr__init__s>   zBlockAlgo.__init__cCs|jtkr|jt||j|j}|dkrn|jr>td|jd|_|td|}|j|dt|}n |j|}|j s|j |}d|_ |S|j|S)aXEncrypt 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. rzFOnly the last chunk is allowed to have length not multiple of %d bytesTrN) rrrr r!r r#rr$r r%)r'Z plaintextpadding_lengthpaddedresrrrr$s'   zBlockAlgo.encryptcCs|jtkr~|jt||j|j}|dkrn|jr>td|jd|_|td|}|j|dt|}n |j|}|S|j|S)aDecrypt 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*). rzFOnly the last chunk is allowed to have length not multiple of %d bytesTrN) rrrr r!r r#rr&)r'Z ciphertextr+r,r-rrrr&s"  zBlockAlgo.decryptN)__name__ __module__ __qualname____doc__r*r$r&rrrrrs3;r)N)r1sys version_infoZCrypto.Util.py21compatZCrypto.Util.py3compatrZMODE_CBCr"ZMODE_PGPZMODE_OFBZMODE_CTRrrrrrrrs