ó
/<õ\c           @` s{   d  d l  m Z m Z m Z d  d l m Z d Z d  Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z d S(   i    (   t   absolute_importt   divisiont   print_function(   t
   InvalidTagi   c         C` s   d d l  m } m } m } t |  |  r/ d St |  |  ra d j t |  j  d  j d  St |  |  sv t	  d j t |  j  d  j d  Sd  S(   Ni    (   t   AESCCMt   AESGCMt   ChaCha20Poly1305s   chacha20-poly1305s
   aes-{}-ccmi   t   asciis
   aes-{}-gcm(
   t+   cryptography.hazmat.primitives.ciphers.aeadR   R   R   t
   isinstancet   formatt   lent   _keyt   encodet   AssertionError(   t   cipherR   R   R   (    (    sH   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/aead.pyt   _aead_cipher_name   s    #c      	   C` sų  |  j  j |  } |  j | |  j j k  |  j  j   } |  j j | |  j  j  } |  j  j | | |  j j |  j j |  j j t	 | t
 k   }	 |  j |	 d k  |  j  j | t |   }	 |  j |	 d k  |  j  j | |  j  j t |  |  j j  }	 |  j |	 d k  | t k r^|  j  j | |  j  j t |  |  }	 |  j |	 d k  n' |  j  j | |  j  j | |  j j  }	 |  j j |  }
 |  j j |  } |  j  j | |  j j |  j j | |
 t	 | t
 k   }	 |  j |	 d k  | S(   Ni    (   t   _libt   EVP_get_cipherbynamet   openssl_assertt   _ffit   NULLt   EVP_CIPHER_CTX_newt   gct   EVP_CIPHER_CTX_freet   EVP_CipherInit_ext   intt   _ENCRYPTt   EVP_CIPHER_CTX_set_key_lengthR   t   EVP_CIPHER_CTX_ctrlt   EVP_CTRL_AEAD_SET_IVLENt   _DECRYPTt   EVP_CTRL_AEAD_SET_TAGt   from_buffer(   t   backendt   cipher_namet   keyt   noncet   tagt   tag_lent	   operationt
   evp_ciphert   ctxt   rest	   nonce_ptrt   key_ptr(    (    sH   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/aead.pyt   _aead_setup   sD    										c         C` sS   |  j  j d  } |  j j | |  j  j | |  j  j |  } |  j | d k  d  S(   Ns   int *i    (   R   t   newR   t   EVP_CipherUpdateR   R   (   R"   R*   t   data_lent   intptrR+   (    (    sH   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/aead.pyt   _set_lengthG   s    				c         C` sS   |  j  j d  } |  j j | |  j  j | | t |   } |  j | d k  d  S(   Ns   int *i    (   R   R/   R   R0   R   R   R   (   R"   R*   t   associated_datat   outlenR+   (    (    sH   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/aead.pyt   _process_aadS   s    	!c         C` s|   |  j  j d  } |  j  j d t |   } |  j j | | | | t |   } |  j | d k  |  j  j | | d  S(   Ns   int *s   unsigned char[]i    (   R   R/   R   R   R0   R   t   buffer(   R"   R*   t   dataR5   t   bufR+   (    (    sH   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/aead.pyt   _process_data[   s
    $c         C` sE  d d l  m } t |  } t |  | | j | d  | t  } t | |  re t |  | t	 |   n  t
 |  | |  t |  | |  }	 |  j j d  }
 |  j j | |  j j |
  } |  j | d k  |  j |
 d d k  |  j j d |  } |  j j | |  j j | |  } |  j | d k  |  j j |  } |	 | S(   Ni    (   R   s   int *s   unsigned char[](   R   R   R   R.   R   t   NoneR   R	   R3   R   R6   R:   R   R/   R   t   EVP_CipherFinal_exR   R   R   t   EVP_CTRL_AEAD_GET_TAGR7   (   R"   R   R%   R8   R4   t
   tag_lengthR   R#   R*   t   processed_dataR5   R+   t   tag_bufR&   (    (    sH   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/aead.pyt   _encryptc   s$    	c         C` s§  d d l  m } t |  | k  r+ t  n  | | } | |  } t |  } t |  | | j | | | t  }	 t | |  r t	 |  |	 t |   n  t
 |  |	 |  t | |  rB|  j j d  }
 |  j j d t |   } |  j j |	 | |
 | t |   } | d k r%|  j   t  n  |  j j | |
 d  } na t |  |	 |  } |  j j d  }
 |  j j |	 |  j j |
  } | d k rĢ|  j   t  n  | S(   Ni    (   R   s   int *s   unsigned char[]i   (   R   R   R   R   R   R.   R   R   R	   R3   R6   R   R/   R   R0   t   _consume_errorsR7   R:   R<   R   (   R"   R   R%   R8   R4   R>   R   R&   R#   R*   R5   R9   R+   R?   (    (    sH   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/aead.pyt   _decrypt~   s4    	$
	
	N(   t
   __future__R    R   R   t   cryptography.exceptionsR   R   R   R   R.   R3   R6   R:   RA   RC   (    (    (    sH   lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/aead.pyt   <module>   s   		,				