σ
Υ{PXc           @` s  d  d l  m Z m Z m Z d  d l m Z d  d l m Z d  d l m	 Z	 d  d l
 m Z m Z d  d l m Z d  d l m Z d  d l m Z m Z m Z m Z d  d	 l m Z d  d
 l m Z m Z m Z m Z m Z d  d l m Z m  Z  m! Z! m" Z" m# Z# m$ Z$ m% Z% e d d d d d g  Z& e j' e  e j' e  e j' e  e j' e  d e( f d     Y    Z) e)   Z* d S(   i    (   t   absolute_importt   divisiont   print_function(   t
   namedtuple(   t   utils(   t   InternalError(   t   _CipherContextt   _GCMCipherContext(   t   _HashContext(   t   _HMACContext(   t   CipherBackendt   HMACBackendt   HashBackendt   PBKDF2HMACBackend(   t   Binding(   t   AESt   ARC4t   Blowfisht   CAST5t	   TripleDES(   t   CBCt   CFBt   CFB8t   CTRt   ECBt   GCMt   OFBt   HashMethodst   ctxt	   hash_initt   hash_updatet
   hash_finalt   Backendc           B` s   e  Z d  Z d Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z RS(   s#   
    CommonCrypto API wrapper.
    t   commoncryptoc         C` sΫ  t    |  _ |  j j |  _ |  j j |  _ i  |  _ |  j   i t d |  j j	 |  j j
 |  j j  d 6t d |  j j |  j j |  j j  d 6t d |  j j |  j j |  j j  d 6t d |  j j |  j j |  j j  d 6t d |  j j |  j j |  j j  d	 6t d |  j j |  j j |  j j  d
 6|  _ i |  j j d 6|  j j d 6|  j j d 6|  j j d 6|  j j  d	 6|  j j! d
 6|  _" i |  j j# d 6|  j j$ d 6|  j j% d 6|  j j& d	 6|  j j' d
 6|  _( d  S(   Ns   CC_MD5_CTX *t   md5s   CC_SHA1_CTX *t   sha1s   CC_SHA256_CTX *t   sha224t   sha256s   CC_SHA512_CTX *t   sha384t   sha512()   R   t   _bindingt   ffit   _ffit   libt   _libt   _cipher_registryt   _register_default_ciphersR   t   CC_MD5_Initt   CC_MD5_Updatet   CC_MD5_Finalt   CC_SHA1_Initt   CC_SHA1_Updatet   CC_SHA1_Finalt   CC_SHA224_Initt   CC_SHA224_Updatet   CC_SHA224_Finalt   CC_SHA256_Initt   CC_SHA256_Updatet   CC_SHA256_Finalt   CC_SHA384_Initt   CC_SHA384_Updatet   CC_SHA384_Finalt   CC_SHA512_Initt   CC_SHA512_Updatet   CC_SHA512_Finalt   _hash_mappingt   kCCHmacAlgMD5t   kCCHmacAlgSHA1t   kCCHmacAlgSHA224t   kCCHmacAlgSHA256t   kCCHmacAlgSHA384t   kCCHmacAlgSHA512t   _supported_hmac_algorithmst   kCCPRFHmacAlgSHA1t   kCCPRFHmacAlgSHA224t   kCCPRFHmacAlgSHA256t   kCCPRFHmacAlgSHA384t   kCCPRFHmacAlgSHA512t!   _supported_pbkdf2_hmac_algorithms(   t   self(    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyt   __init__+   sJ    	
c         C` s   | j  |  j k S(   N(   t   nameRA   (   RO   t	   algorithm(    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyt   hash_supported^   s    c         C` s   | j  |  j k S(   N(   RQ   RH   (   RO   RR   (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyt   hmac_supporteda   s    c         C` s   t  |  |  S(   N(   R   (   RO   RR   (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyt   create_hash_ctxd   s    c         C` s   t  |  | |  S(   N(   R	   (   RO   t   keyRR   (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyt   create_hmac_ctxg   s    c         C` sK   t  | t  r( t | j  d k  r( t St |  t |  f |  j k Sd  S(   Ni   (   t
   isinstanceR   t   lenRV   t   Falset   typeR-   (   RO   t   ciphert   mode(    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyt   cipher_supportedj   s    $c         C` sE   t  | t  r( t |  | | |  j j  St |  | | |  j j  Sd  S(   N(   RX   R   R   R,   t
   kCCEncryptR   (   RO   R\   R]   (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyt   create_symmetric_encryption_ctxr   s    c         C` sE   t  | t  r( t |  | | |  j j  St |  | | |  j j  Sd  S(   N(   RX   R   R   R,   t
   kCCDecryptR   (   RO   R\   R]   (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyt   create_symmetric_decryption_ctxz   s    c         C` s   | j  |  j k S(   N(   RQ   RN   (   RO   RR   (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyt   pbkdf2_hmac_supported   s    c   	   
   C` s   |  j  | j } |  j j d |  } |  j j |  j j | t |  | t |  | | | | 	 } |  j |  |  j j	 |  S(   Ns	   uint8_t[](
   RN   RQ   R*   t   newR,   t   CCKeyDerivationPBKDFt	   kCCPBKDF2RY   t   _check_cipher_responset   buffer(	   RO   RR   t   lengtht   saltt
   iterationst   key_materialt   alg_enumt   buft   res(    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyt   derive_pbkdf2_hmac   s    					c         C` sM   | | f |  j  k r0 t d j | |    n  | | f |  j  | | f <d  S(   Ns$   Duplicate registration for: {0} {1}.(   R-   t
   ValueErrort   format(   RO   t
   cipher_clst   cipher_constt   mode_clst
   mode_const(    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyt   _register_cipher_adapter   s
    	c         C` s3  x t  |  j j f t |  j j f t |  j j f t |  j j f t	 |  j j
 f t |  j j f t |  j j f g D]( \ } } |  j t |  j j | |  qp Wx{ t  |  j j f t |  j j f t |  j j f t |  j j f t	 |  j j
 f g D]( \ } } |  j t |  j j | |  qξ Wxl t  |  j j f t |  j j f t |  j j f t	 |  j j
 f g D]( \ } } |  j t |  j j | |  q]Wx{ t  |  j j f t |  j j f t |  j j f t	 |  j j
 f t |  j j f g D]( \ } } |  j t |  j j | |  qΫW|  j t |  j j t d   |  j j  d  S(   N(   R   R,   t
   kCCModeCBCR   t
   kCCModeECBR   t
   kCCModeCFBR   t   kCCModeCFB8R   t
   kCCModeOFBR   t
   kCCModeCTRR   t
   kCCModeGCMRw   R   t   kCCAlgorithmAES128R   t   kCCAlgorithm3DESR   t   kCCAlgorithmBlowfishR   t   kCCAlgorithmCASTR   t   kCCAlgorithmRC4R[   t   Nonet
   kCCModeRC4(   RO   Ru   Rv   (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyR.   ‘   sd    						c         C` sS   | |  j  j k r d  S| |  j  j k r7 t d   n t d j |  |   d  S(   NsF   The length of the provided data is not a multiple of the block length.sH   The backend returned an unknown error, consider filing a bug. Code: {0}.(   R,   t
   kCCSuccesst   kCCAlignmentErrorRq   R   Rr   (   RO   t   response(    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyRg   ή   s    c         C` sP   | d |  j  j k rL |  j j | d  } |  j |  |  j  j | d <n  d S(   sq   
        Called by the garbage collector and used to safely dereference and
        release the context.
        i    N(   R*   t   NULLR,   t   CCCryptorReleaseRg   (   RO   R   Ro   (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyt   _release_cipher_ctxο   s    (   t   __name__t
   __module__t   __doc__RQ   RP   RS   RT   RU   RW   R^   R`   Rb   Rc   Rp   Rw   R.   Rg   R   (    (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyR    !   s    	3												=	N(+   t
   __future__R    R   R   t   collectionsR   t   cryptographyR   t   cryptography.exceptionsR   t1   cryptography.hazmat.backends.commoncrypto.ciphersR   R   t0   cryptography.hazmat.backends.commoncrypto.hashesR   t.   cryptography.hazmat.backends.commoncrypto.hmacR	   t'   cryptography.hazmat.backends.interfacesR
   R   R   R   t1   cryptography.hazmat.bindings.commoncrypto.bindingR   t1   cryptography.hazmat.primitives.ciphers.algorithmsR   R   R   R   R   t,   cryptography.hazmat.primitives.ciphers.modesR   R   R   R   R   R   R   R   t   register_interfacet   objectR    t   backend(    (    (    sU   /usr/lib/python2.7/dist-packages/cryptography/hazmat/backends/commoncrypto/backend.pyt   <module>   s$   "(4Υ