ó
Ő{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 m Z d  d l m Z d  d l m Z m Z m Z m Z e j e j  d e f d     Y Z 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   cipherst   constant_time(   t   modes(   t   CFBt   CFB8t   CTRt   OFBt   _CipherContextc           B` s#   e  Z d    Z d   Z d   Z RS(   c         C` s  | |  _  | |  _ | |  _ | |  _ d |  _ t | t j  rn t | t t	 t
 t f  rn | j d |  _ n	 d |  _ |  j  j } y& | t |  t |  f \ } } WnA t k
 rě t d j | j | r× | j n |  t j   n X|  j  j j d  } |  j  j j | |  j  j  } t | t j  r>| j }	 n- t | t j  r\| j }	 n |  j  j j }	 t | t  r|  j  j  j! }
 n d }
 |  j  j  j" | | | |  j  j  j# |	 | j$ t% | j$  |  j  j j d d |
 |  } |  j  j& |  | |  _' d  S(   Ni    i   i   s8   cipher {0} in {1} mode is not supported by this backend.s   CCCryptorRef *((   t   _backendt   _ciphert   _modet
   _operationt   _bytes_processedt
   isinstanceR   t   BlockCipherAlgorithmR   R
   R   R   t
   block_sizet   _byte_block_sizet   _cipher_registryt   typet   KeyErrorR   t   formatt   nameR   t   UNSUPPORTED_CIPHERt   _ffit   newt   gct   _release_cipher_ctxR	   t   ModeWithInitializationVectort   initialization_vectort   ModeWithNoncet   noncet   NULLt   _libt   kCCModeOptionCTR_BEt   CCCryptorCreateWithModet   ccNoPaddingt   keyt   lent   _check_cipher_responset   _ctx(   t   selft   backendt   ciphert   modet	   operationt   registryt   cipher_enumt	   mode_enumt   ctxt   iv_noncet   mode_optiont   res(    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/ciphers.pyt   __init__   sF    				
		&c         C` sŔ   |  j  t |  7_  |  j j j d t |  |  j d  } |  j j j d  } |  j j j |  j d | t |  | t |  |  j d |  } |  j j	 |  |  j j j
 |  | d  S(   Ns   unsigned char[]i   s   size_t *i    (   R   R,   R   R   R   R   R'   t   CCCryptorUpdateR.   R-   t   buffer(   R/   t   datat   buft   outlenR:   (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/ciphers.pyt   updateM   s    c         C` s¸   |  j  |  j r t d   n  |  j j j d |  j  } |  j j j d  } |  j j j |  j d | t	 |  |  } |  j j
 |  |  j j |  j  |  j j j |  | d  S(   NsF   The length of the provided data is not a multiple of the block length.s   unsigned char[]s   size_t *i    (   R   R   t
   ValueErrorR   R   R   R'   t   CCCryptorFinalR.   R,   R-   R!   R=   (   R/   R?   R@   R:   (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/ciphers.pyt   finalizeY   s    (   t   __name__t
   __module__R;   RA   RD   (    (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/ciphers.pyR      s   	9	t   _GCMCipherContextc           B` s;   e  Z d    Z d   Z d   Z d   Z e j d  Z RS(   c   
      C` s  | |  _  | |  _ | |  _ | |  _ d  |  _ |  j  j } y& | t |  t |  f \ } } WnA t k
 r˘ t	 d j
 | j | r | j n |  t j   n X|  j  j j d  } |  j  j j | |  j  j  } | |  _ |  j  j j | | | |  j  j j |  j  j j | j t | j  |  j  j j d d d |  j  }	 |  j  j |	  |  j  j j |  j d | j t | j   }	 |  j  j |	  |  j d  d  S(   Ns8   cipher {0} in {1} mode is not supported by this backend.s   CCCryptorRef *i    t    (   R   R   R   R   t   Nonet   _tagR   R   R   R   R   R   R   R   R   R   R    R!   R.   R'   R)   R*   R&   R+   R,   R-   t   CCCryptorGCMAddIVR#   t   authenticate_additional_data(
   R/   R0   R1   R2   R3   R4   R5   R6   R7   R:   (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/ciphers.pyR;   l   s<    					&	!
c         C` sŚ   |  j  j j d t |   } |  j d | t |  | f } |  j |  j  j j k rm |  j  j j |   } n |  j  j j	 |   } |  j  j
 |  |  j  j j |  S(   Ns   unsigned char[]i    (   R   R   R   R,   R.   R   R'   t
   kCCEncryptt   CCCryptorGCMEncryptt   CCCryptorGCMDecryptR-   R=   (   R/   R>   R?   t   argsR:   (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/ciphers.pyRA      s    c         C` sü   |  j  d  |  j j d } |  j j j d |  } |  j j j d |  } |  j j j |  j d | |  } |  j j	 |  |  j j
 |  j  |  j j j |  |  _ |  j |  j j j k rř t j |  j t |  j j   |  j j  rř t  n  d S(   NRH   i   s   unsigned char[]s   size_t *i    (   RA   R   R   R   R   R   R'   t   CCCryptorGCMFinalR.   R-   R!   R=   RJ   R   t
   kCCDecryptR   t   bytes_eqR,   R   t   tagR   (   R/   t   tag_sizet   tag_buft   tag_lenR:   (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/ciphers.pyRD   ¤   s    &	c         C` s<   |  j  j j |  j d | t |   } |  j  j |  d  S(   Ni    (   R   R'   t   CCCryptorGCMAddAADR.   R,   R-   (   R/   R>   R:   (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/ciphers.pyRL   ť   s    RJ   (	   RE   RF   R;   RA   RD   RL   R   t   read_only_propertyRT   (    (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/ciphers.pyRG   i   s
   	-			N(   t
   __future__R    R   R   t   cryptographyR   t   cryptography.exceptionsR   R   R   t   cryptography.hazmat.primitivesR   R   t&   cryptography.hazmat.primitives.ciphersR	   t,   cryptography.hazmat.primitives.ciphers.modesR
   R   R   R   t   register_interfacet   CipherContextt   objectR   t   AEADCipherContextt   AEADEncryptionContextRG   (    (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/ciphers.pyt   <module>   s   "V