σ
/<υ\c           @` sΚ   d  d l  m Z m Z m Z d  d l m Z d  d l m Z m Z m	 Z	 d  d l
 m Z d  d l m Z e j e j  e j e j  e j e j  e j e j  d e f d     Y    Z d S(	   i    (   t   absolute_importt   divisiont   print_function(   t   utils(   t
   InvalidTagt   UnsupportedAlgorithmt   _Reasons(   t   ciphers(   t   modest   _CipherContextc           B` sY   e  Z d  Z d Z d   Z d   Z d   Z d   Z d   Z d   Z	 e
 j d  Z RS(	   i   i    c         C` sh  | |  _  | |  _ | |  _ | |  _ d  |  _ t |  j t j  rX |  j j	 d |  _
 n	 d |  _
 |  j  j j   } |  j  j j | |  j  j j  } |  j  j } y  | t |  t |  f } WnA t k
 rt d j | j | rξ | j n |  t j   n X| |  j  | |  } | |  j  j j k rd j |  }	 | d  k	 r_|	 d j |  7}	 n  |	 d j |  j  j    7}	 t |	 t j   n  t | t j  r½|  j  j j | j  }
 n t | t j  rκ|  j  j j | j  }
 ni t | t j   r|  j  j j | j!  }
 n< t | t j   rD|  j  j j | j!  }
 n |  j  j j }
 |  j  j j" | | |  j  j j |  j  j j |  j  j j |  } |  j  j# | d k  |  j  j j$ | t% | j&   } |  j  j# | d k  t | t j'  rη|  j  j j( | |  j  j j) t% |
  |  j  j j  } |  j  j# | d k  | j* d  k	 r€|  j  j j( | |  j  j j+ t% | j*  | j*  } |  j  j# | d k  | j* |  _ qη|  j |  j, k rη|  j  j j- rη|  j  j j. rηt/ d   qηn  |  j  j j" | |  j  j j |  j  j j |  j  j j | j&  |
 |  } |  j  j# | d k  |  j  j j0 | d  | |  _1 d  S(	   Ni   i   s6   cipher {} in {} mode is not supported by this backend.s   cipher {0.name} s   in {0.name} mode s_   is not supported by this backend (Your version of OpenSSL may be too old. Current version: {}.)i    s_   delayed passing of GCM tag requires OpenSSL >= 1.0.2. To use this feature please update OpenSSL(2   t   _backendt   _ciphert   _modet
   _operationt   Nonet   _tagt
   isinstanceR   t   BlockCipherAlgorithmt
   block_sizet   _block_size_bytest   _libt   EVP_CIPHER_CTX_newt   _ffit   gct   EVP_CIPHER_CTX_freet   _cipher_registryt   typet   KeyErrorR   t   formatt   nameR   t   UNSUPPORTED_CIPHERt   NULLt   openssl_version_textR   t   ModeWithInitializationVectort   from_buffert   initialization_vectort   ModeWithTweakt   tweakt   ModeWithNoncet   noncet   EVP_CipherInit_ext   openssl_assertt   EVP_CIPHER_CTX_set_key_lengtht   lent   keyt   GCMt   EVP_CIPHER_CTX_ctrlt   EVP_CTRL_AEAD_SET_IVLENt   tagt   EVP_CTRL_AEAD_SET_TAGt   _DECRYPTt"   CRYPTOGRAPHY_OPENSSL_LESS_THAN_102t   CRYPTOGRAPHY_IS_LIBRESSLt   NotImplementedErrort   EVP_CIPHER_CTX_set_paddingt   _ctx(   t   selft   backendt   ciphert   modet	   operationt   ctxt   registryt   adaptert
   evp_ciphert   msgt   iv_noncet   res(    (    sK   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/ciphers.pyt   __init__   s    						 		c         C` s=   t  t |  |  j d  } |  j | |  } t | |   S(   Ni   (   t	   bytearrayR+   R   t   update_intot   bytes(   R8   t   datat   buft   n(    (    sK   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/ciphers.pyt   updatez   s    c         C` sί   t  |  t  |  |  j d k  rL t d j t  |  |  j d    n  |  j j j d |  j j j |   } |  j j j d  } |  j j	 j
 |  j | | |  j j j |  t  |   } |  j j | d k  | d S(   Ni   s1   buffer must be at least {} bytes for this payloads   unsigned char *s   int *i    (   R+   R   t
   ValueErrorR   R
   R   t   castR"   t   newR   t   EVP_CipherUpdateR7   R)   (   R8   RH   RI   t   outlenRC   (    (    sK   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/ciphers.pyRF      s    # !c         C` s#  t  |  j t j  r% |  j d  n  |  j |  j k rj t  |  j t j  rj |  j d  k rj t
 d   n  |  j j j d |  j  } |  j j j d  } |  j j j |  j | |  } | d k r9|  j j   } | rψ t  |  j t j  rψ t  n  |  j j | d j |  j j j |  j j j   t
 d   n  t  |  j t j  rΪ|  j |  j k rΪ|  j j j d |  j  } |  j j j |  j |  j j j |  j |  } |  j j | d k  |  j j j |  |  _ n  |  j j j |  j  } |  j j | d k  |  j j j |  | d  S(   Nt    s4   Authentication tag must be provided when decrypting.s   unsigned char[]s   int *i    sF   The length of the provided data is not a multiple of the block length.i   (   R   R   R   R-   RK   R   R2   t   ModeWithAuthenticationTagR0   R   RL   R
   R   RN   R   R   t   EVP_CipherFinal_exR7   t   _consume_errorsR   R)   t   _lib_reason_matcht   ERR_LIB_EVPt'   EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTHt   _ENCRYPTR.   t   EVP_CTRL_AEAD_GET_TAGt   bufferR   t   EVP_CIPHER_CTX_cleanup(   R8   RI   RP   RC   t   errorst   tag_buf(    (    sK   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/ciphers.pyt   finalize   s@    		
c         C` s½   |  j  j j r. |  j  j j r. t d   n  t |  |  j j k  rd t d j	 |  j j    n  |  j  j j
 |  j |  j  j j t |  |  } |  j  j | d k  | |  _ |  j   S(   NsU   finalize_with_tag requires OpenSSL >= 1.0.2. To use this method please update OpenSSLs.   Authentication tag must be {} bytes or longer.i    (   R
   R   R3   R4   R5   R+   R   t   _min_tag_lengthRL   R   R.   R7   R1   R)   R   R^   (   R8   R0   RC   (    (    sK   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/ciphers.pyt   finalize_with_tagΗ   s    	c         C` sq   |  j  j j d  } |  j  j j |  j |  j  j j | |  j  j j |  t |   } |  j  j	 | d k  d  S(   Ns   int *i    (
   R
   R   RN   R   RO   R7   R   R"   R+   R)   (   R8   RH   RP   RC   (    (    sK   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/ciphers.pyt   authenticate_additional_dataέ   s
    !R   (   t   __name__t
   __module__RX   R2   RD   RK   RF   R^   R`   Ra   R   t   read_only_propertyR0   (    (    (    sK   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/ciphers.pyR	      s   	e			6		N(   t
   __future__R    R   R   t   cryptographyR   t   cryptography.exceptionsR   R   R   t   cryptography.hazmat.primitivesR   t&   cryptography.hazmat.primitives.ciphersR   t   register_interfacet   CipherContextt   AEADCipherContextt   AEADEncryptionContextt   AEADDecryptionContextt   objectR	   (    (    (    sK   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/ciphers.pyt   <module>   s   