σ
Υ{PXc           @` 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  d e f d     Y   Z 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` sG   e  Z d  Z d Z d   Z d   Z d   Z d   Z e j	 d  Z
 RS(   i   i    c         C` sf  | |  _  | |  _ | |  _ | |  _ 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 rat d j | j | rL| j n |  t j   n  t | t j  r| j }	 n- t | t j  r| 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& k rτ|  j  j j$ | |  j  j j' t! | j(  | j(  }
 |  j  j |
 d k  qτn  |  j  j j | |  j  j j |  j  j j | j" |	 |  }
 |  j  j |
 d k  |  j  j j) | d  | |  _* d  S(   Ni   i   s8   cipher {0} in {1} mode is not supported by this backend.i    (+   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   NULLR   t   ModeWithInitializationVectort   initialization_vectort   ModeWithNoncet   noncet   EVP_CipherInit_ext   openssl_assertt   EVP_CIPHER_CTX_set_key_lengtht   lent   keyt   GCMt   EVP_CIPHER_CTX_ctrlt   EVP_CTRL_GCM_SET_IVLENt   _DECRYPTt   EVP_CTRL_GCM_SET_TAGt   tagt   EVP_CIPHER_CTX_set_paddingt   _ctx(   t   selft   backendt   ciphert   modet	   operationt   ctxt   registryt   adaptert
   evp_ciphert   iv_noncet   res(    (    sP   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/openssl/ciphers.pyt   __init__   sv    						 		c         C` s   |  j  j j d t |  |  j d  } |  j  j j d  } |  j  j j |  j | | | t |   } |  j  j | d k  |  j  j j	 |  | d  S(   Ns   unsigned char[]i   s   int *i    (
   R
   R   t   newR'   R   R   t   EVP_CipherUpdateR0   R%   t   buffer(   R1   t   datat   buft   outlenR;   (    (    sP   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/openssl/ciphers.pyt   updateg   s    c         C` s&  t  |  j t j  r% |  j d  n  |  j j j d |  j  } |  j j j d  } |  j j	 j
 |  j | |  } | d k r<|  j j   } | r³ t  |  j t j  r³ t  n  |  j j | d d |  j j	 j |  j j	 j |  j j	 j f k p)| d d |  j j	 j |  j j	 j |  j j	 j f k  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    s   unsigned char[]s   int *i    i   sF   The length of the provided data is not a multiple of the block length.(   R   R   R   R)   RC   R
   R   R=   R   R   t   EVP_CipherFinal_exR0   t   _consume_errorsR   R%   t   ERR_LIB_EVPt   EVP_F_EVP_ENCRYPTFINAL_EXt'   EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTHt   EVP_F_EVP_DECRYPTFINAL_EXt
   ValueErrorR   t   _ENCRYPTR*   t   EVP_CTRL_GCM_GET_TAGR?   R   t   EVP_CIPHER_CTX_cleanup(   R1   RA   RB   R;   t   errorst   tag_buf(    (    sP   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/openssl/ciphers.pyt   finalizep   s@    		c         C` sb   |  j  j j d  } |  j  j j |  j |  j  j j | | t |   } |  j  j | d k  d  S(   Ns   int *i    (	   R
   R   R=   R   R>   R0   R   R'   R%   (   R1   R@   RB   R;   (    (    sP   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/openssl/ciphers.pyt   authenticate_additional_data’   s    'R   (   t   __name__t
   __module__RL   R,   R<   RC   RQ   RR   R   t   read_only_propertyR.   (    (    (    sP   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/openssl/ciphers.pyR	      s   	S			2	t   _AESCTRCipherContextc           B` s)   e  Z d  Z d   Z d   Z d   Z RS(   s―   
    This is needed to provide support for AES CTR mode in OpenSSL 1.0.0. It can
    be removed when we drop 1.0.0 support (RHEL 6.4 is the only thing that
    ships it).
    c         C` s½   | |  _  |  j  j j d  |  _ |  j  j j | j t | j  d |  j  } |  j  j | d k  |  j  j j d d  |  _	 |  j  j j d | j
  |  _ |  j  j j d d  |  _ d  S(   Ns	   AES_KEY *i   i    s   unsigned char[]i   s   unsigned char[16]s   unsigned int *(   R
   R   R=   t   _keyR   t   AES_set_encrypt_keyR(   R'   R%   t   _ecountR#   t   _noncet   _num(   R1   R2   R3   R4   R;   (    (    sP   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/openssl/ciphers.pyR<   ³   s    	"c         C` si   |  j  j j d t |   } |  j  j j | | t |  |  j |  j |  j |  j	  |  j  j j
 |  S(   Ns   unsigned char[](   R
   R   R=   R'   R   t   AES_ctr128_encryptRW   RZ   RY   R[   R?   (   R1   R@   RA   (    (    sP   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/openssl/ciphers.pyRC   Ώ   s
    c         C` s(   d  |  _ d  |  _ d  |  _ d  |  _ d S(   NRD   (   R   RW   RY   RZ   R[   (   R1   (    (    sP   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/openssl/ciphers.pyRQ   Η   s
    				(   RS   RT   t   __doc__R<   RC   RQ   (    (    (    sP   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/openssl/ciphers.pyRV   ¬   s   		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   objectR	   RV   (    (    (    sP   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/openssl/ciphers.pyt   <module>   s   