ó
Bd\Rc           @   sœ   d  Z  d d l Z e j d d k rE e j d d k rE d d l Tn  d d l Td Z d Z d Z d Z d	 Z	 d
 Z
 d Z d d „ Z d d d „  ƒ  YZ d S(   s4   Module with definitions common to all block ciphers.iÿÿÿÿNi    i   i   (   t   *i   i   i   i   i   c         C   sQ   | j  |  ƒ } t | ƒ | k rG | r: t d |  ƒ ‚ n  | | } n  | pP | S(   sF   Find a parameter in tuple and dictionary arguments a function receivess!   Parameter '%s' is specified twice(   t   gett   lent
   ValueError(   t   namet   indext   argst   kwargst   defaultt   param(    (    s6   lib/python2.7/site-packages/Crypto/Cipher/blockalgo.pyt   _getParameter|   s    t	   BlockAlgoc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s)   Class modelling an abstract block cipher.c         O   s  t  d d | | d t ƒ|  _ | j |  _ |  j t k rc | j | | | Ž |  _ |  j j |  _ n¸t |  _	 t |  _
 t  d d | | ƒ |  _ |  j s¥ t d ƒ ‚ n  | j | t t d ƒ |  j d |  j d	 ƒ} t |  j ƒ |  j k r/| j |  j |  j d
 t d ƒ |  j d ƒ |  j d  |  _ n¼ t |  j ƒ |  j d k rË|  j |  _ | j |  j t d ƒ |  j d ƒ |  j d  |  _ |  j d
 |  j d d
 !k r¸t d ƒ ‚ n  |  j d
  |  _ n  t d |  j |  j d f ƒ ‚ | j | t |  j |  j d |  j d	 ƒ|  _ d  S(   Nt   modei    R   t   ivi   s   MODE_OPENPGP requires an IVs    t   segment_sizei   iþÿÿÿi   iüÿÿÿs%   Failed integrity check for OPENPGP IVs4   Length of IV must be %d or %d bytes for MODE_OPENPGP(   R
   t   MODE_ECBR   t
   block_sizet   MODE_OPENPGPt   newt   _ciphert   IVt   Falset   _done_first_blockt   _done_last_blockR   t   MODE_CFBt   bR   t   encryptt   _encrypted_IVt   decrypt(   t   selft   factoryt   keyR   R   t	   IV_cipher(    (    s6   lib/python2.7/site-packages/Crypto/Cipher/blockalgo.pyt   __init__ˆ   s>    				c         C   sÞ   |  j  t k rÎ |  j t | ƒ |  j |  j } | d k r– |  j rZ t d |  j ƒ ‚ n  t |  _ | t d ƒ | } |  j j	 | ƒ t | ƒ  } n |  j j	 | ƒ } |  j
 sÊ |  j | } t |  _
 n  | S|  j j	 | ƒ S(   sX  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.
        i    sF   Only the last chunk is allowed to have length not multiple of %d bytess    (   R   R   R   R   R   R   t   TrueR   R   R   R   R   (   R   t	   plaintextt   padding_lengtht   paddedt   res(    (    s6   lib/python2.7/site-packages/Crypto/Cipher/blockalgo.pyR   »   s    '!			c         C   s¼   |  j  t k r¬ |  j t | ƒ |  j |  j } | d k r– |  j rZ t d |  j ƒ ‚ n  t |  _ | t d ƒ | } |  j j	 | ƒ t | ƒ  } n |  j j	 | ƒ } | S|  j j	 | ƒ S(   sš  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*).
        i    sF   Only the last chunk is allowed to have length not multiple of %d bytess    (
   R   R   R   R   R   R   R"   R   R   R   (   R   t
   ciphertextR$   R%   R&   (    (    s6   lib/python2.7/site-packages/Crypto/Cipher/blockalgo.pyR   ö   s    "!		(   t   __name__t
   __module__t   __doc__R!   R   R   (    (    (    s6   lib/python2.7/site-packages/Crypto/Cipher/blockalgo.pyR   …   s   	3	;(    (   R*   t   syst   version_infot   Crypto.Util.py21compatt   Crypto.Util.py3compatR   t   MODE_CBCR   t   MODE_PGPt   MODE_OFBt   MODE_CTRR   t   NoneR
   R   (    (    (    s6   lib/python2.7/site-packages/Crypto/Cipher/blockalgo.pyt   <module>   s   &
	