σ
/<υ\c           @` s§  d  d l  m Z m Z m Z d  d l Z y d  d l m Z Wn! e k
 r_ d  d l m Z n Xd  d l	 Z	 d  d l
 m Z d  d l m Z m Z d  d l m Z e	 j e j  d e f d     Y Z e	 j e j  d	 e f d
     Y Z e	 j e j  d e f d     Y Z e Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d Z  d   Z! d e f d     YZ" d e f d     YZ# d S(   i    (   t   absolute_importt   divisiont   print_functionN(   t   gcd(   t   utils(   t   UnsupportedAlgorithmt   _Reasons(   t
   RSABackendt   RSAPrivateKeyc           B` sb   e  Z e j d     Z e j d    Z e j d    Z e j d    Z e j d    Z	 RS(   c         C` s   d S(   sN   
        Returns an AsymmetricSignatureContext used for signing data.
        N(    (   t   selft   paddingt	   algorithm(    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   signer   t    c         C` s   d S(   s3   
        Decrypts the provided ciphertext.
        N(    (   R	   t
   ciphertextR
   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   decrypt   R   c         C` s   d S(   s7   
        The bit length of the public modulus.
        N(    (   R	   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   key_size#   R   c         C` s   d S(   sD   
        The RSAPublicKey associated with this private key.
        N(    (   R	   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt
   public_key)   R   c         C` s   d S(   s!   
        Signs the data.
        N(    (   R	   t   dataR
   R   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   sign/   R   (
   t   __name__t
   __module__t   abct   abstractmethodR   R   t   abstractpropertyR   R   R   (    (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyR      s
   t   RSAPrivateKeyWithSerializationc           B` s,   e  Z e j d     Z e j d    Z RS(   c         C` s   d S(   s/   
        Returns an RSAPrivateNumbers.
        N(    (   R	   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   private_numbers8   R   c         C` s   d S(   s6   
        Returns the key serialized as bytes.
        N(    (   R	   t   encodingt   formatt   encryption_algorithm(    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   private_bytes>   R   (   R   R   R   R   R   R   (    (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyR   6   s   t   RSAPublicKeyc           B` st   e  Z e j d     Z e j d    Z e j d    Z e j d    Z e j d    Z	 e j d    Z
 RS(   c         C` s   d S(   sY   
        Returns an AsymmetricVerificationContext used for verifying signatures.
        N(    (   R	   t	   signatureR
   R   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   verifierG   R   c         C` s   d S(   s/   
        Encrypts the given plaintext.
        N(    (   R	   t	   plaintextR
   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   encryptM   R   c         C` s   d S(   s7   
        The bit length of the public modulus.
        N(    (   R	   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyR   S   R   c         C` s   d S(   s-   
        Returns an RSAPublicNumbers
        N(    (   R	   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   public_numbersY   R   c         C` s   d S(   s6   
        Returns the key serialized as bytes.
        N(    (   R	   R   R   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   public_bytes_   R   c         C` s   d S(   s5   
        Verifies the signature of the data.
        N(    (   R	   R    R   R
   R   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   verifye   R   (   R   R   R   R   R!   R#   R   R   R$   R%   R&   (    (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyR   E   s   c         C` sA   t  | t  s$ t d t j   n  t |  |  | j |  |  S(   Ns-   Backend object does not implement RSABackend.(   t
   isinstanceR   R   R   t   BACKEND_MISSING_INTERFACEt   _verify_rsa_parameterst   generate_rsa_private_key(   t   public_exponentR   t   backend(    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   generate_private_keyo   s    c         C` sY   |  d k  r t  d   n  |  d @d k r: t  d   n  | d k  rU t  d   n  d  S(   Ni   s   public_exponent must be >= 3.i   i    s   public_exponent must be odd.i   s#   key_size must be at least 512-bits.(   t
   ValueError(   R+   R   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyR)   z   s    c         C` sd  | d k  r t  d   n  |  | k r6 t  d   n  | | k rQ t  d   n  | | k rl t  d   n  | | k r t  d   n  | | k r’ t  d   n  | | k r½ t  d   n  | d k  sΥ | | k rδ t  d	   n  | d
 @d k rt  d   n  | d
 @d k r"t  d   n  | d
 @d k rAt  d   n  |  | | k r`t  d   n  d  S(   Ni   s   modulus must be >= 3.s   p must be < modulus.s   q must be < modulus.s   dmp1 must be < modulus.s   dmq1 must be < modulus.s   iqmp must be < modulus.s#   private_exponent must be < modulus.s+   public_exponent must be >= 3 and < modulus.i   i    s   public_exponent must be odd.s   dmp1 must be odd.s   dmq1 must be odd.s   p*q must equal modulus.(   R.   (   t   pt   qt   private_exponentt   dmp1t   dmq1t   iqmpR+   t   modulus(    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   _check_private_key_components   s0    c         C` se   | d k  r t  d   n  |  d k  s3 |  | k rB t  d   n  |  d @d k ra t  d   n  d  S(   Ni   s   n must be >= 3.s   e must be >= 3 and < n.i   i    s   e must be odd.(   R.   (   t   et   n(    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   _check_public_key_components¬   s    c         C` s   d \ } } } } |  | } } xl | d k r t  | |  \ } }	 | | | | | | }
 } | |	 | | |
 | f \ } } } } } } q" W| | S(   sO   
    Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1
    i   i    (   i   i    i    i   (   t   divmod(   R7   t   mt   x1t   y1t   x2t   y2t   at   bR0   t   rt   xnt   yn(    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   _modinv·   s    .c         C` s   t  | |   S(   sF   
    Compute the CRT (q ** -1) % p value from RSA primes p and q.
    (   RE   (   R/   R0   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   rsa_crt_iqmpΔ   s    c         C` s   |  | d S(   sg   
    Compute the CRT private_exponent % (p - 1) value from the RSA
    private_exponent (d) and p.
    i   (    (   R1   R/   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   rsa_crt_dmp1Λ   s    c         C` s   |  | d S(   sg   
    Compute the CRT private_exponent % (q - 1) value from the RSA
    private_exponent (d) and q.
    i   (    (   R1   R0   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   rsa_crt_dmq1Σ   s    iθ  c         C` sO  | | d } | } x | d d k r4 | d } q Wt  } d } x§ | rκ | t k  rκ | } x} | | k  rά t | | |   } | d k rΟ | |  d k rΟ t | d |   d k rΟ t | d |   }	 t } Pn  | d 9} q` W| d 7} qD W| s t d   n  t |  |	  \ }
 } | d k s't  t |	 |
 f d t \ }	 }
 |	 |
 f S(   s‘   
    Compute factors p and q from the private exponent d. We assume that n has
    no more than two factors. This function is adapted from code in PyCrypto.
    i   i   i    s2   Unable to compute factors p and q from exponent d.t   reverse(	   t   Falset   _MAX_RECOVERY_ATTEMPTSt   powR   t   TrueR.   R:   t   AssertionErrort   sorted(   R8   R7   t   dt   ktott   tt   spottedR@   t   kt   candR/   R0   RB   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   rsa_recover_prime_factorsα   s,    4t   RSAPrivateNumbersc           B` s   e  Z d    Z e j d  Z e j d  Z e j d  Z e j d  Z e j d  Z	 e j d  Z
 e j d  Z d   Z d	   Z d
   Z d   Z RS(   c         C` sβ   t  | t j  sr t  | t j  sr t  | t j  sr t  | t j  sr t  | t j  sr t  | t j  r t d   n  t  | t  s t d   n  | |  _ | |  _ | |  _ | |  _ | |  _	 | |  _
 | |  _ d  S(   NsN   RSAPrivateNumbers p, q, d, dmp1, dmq1, iqmp arguments must all be an integers.sF   RSAPrivateNumbers public_numbers must be an RSAPublicNumbers instance.(   R'   t   sixt   integer_typest	   TypeErrort   RSAPublicNumberst   _pt   _qt   _dt   _dmp1t   _dmq1t   _iqmpt   _public_numbers(   R	   R/   R0   RP   R2   R3   R4   R$   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   __init__  s$    						R\   R]   R^   R_   R`   Ra   Rb   c         C` s   | j  |   S(   N(   t   load_rsa_private_numbers(   R	   R,   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   private_key2  s    c         C` s   t  | t  s t S|  j | j k o |  j | j k o |  j | j k o |  j | j k o |  j | j k o |  j | j k o |  j	 | j	 k S(   N(
   R'   RW   t   NotImplementedR/   R0   RP   R2   R3   R4   R$   (   R	   t   other(    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   __eq__5  s    c         C` s   |  | k S(   N(    (   R	   Rg   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   __ne__C  s    c         C` s4   t  |  j |  j |  j |  j |  j |  j |  j f  S(   N(   t   hashR/   R0   RP   R2   R3   R4   R$   (   R	   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   __hash__F  s    (   R   R   Rc   R   t   read_only_propertyR/   R0   RP   R2   R3   R4   R$   Re   Rh   Ri   Rk   (    (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyRW     s   				R[   c           B` s\   e  Z d    Z e j d  Z e j d  Z d   Z d   Z d   Z	 d   Z
 d   Z RS(   c         C` sK   t  | t j  s& t  | t j  r5 t d   n  | |  _ | |  _ d  S(   Ns,   RSAPublicNumbers arguments must be integers.(   R'   RX   RY   RZ   t   _et   _n(   R	   R7   R8   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyRc   S  s
    	Rm   Rn   c         C` s   | j  |   S(   N(   t   load_rsa_public_numbers(   R	   R,   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyR   `  s    c         C` s   d j  |   S(   Ns$   <RSAPublicNumbers(e={0.e}, n={0.n})>(   R   (   R	   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   __repr__c  s    c         C` s5   t  | t  s t S|  j | j k o4 |  j | j k S(   N(   R'   R[   Rf   R7   R8   (   R	   Rg   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyRh   f  s    c         C` s   |  | k S(   N(    (   R	   Rg   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyRi   l  s    c         C` s   t  |  j |  j f  S(   N(   Rj   R7   R8   (   R	   (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyRk   o  s    (   R   R   Rc   R   Rl   R7   R8   R   Rp   Rh   Ri   Rk   (    (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyR[   R  s   	
				($   t
   __future__R    R   R   R   t   mathR   t   ImportErrort	   fractionsRX   t   cryptographyR   t   cryptography.exceptionsR   R   t'   cryptography.hazmat.backends.interfacesR   t   add_metaclasst   ABCMetat   objectR   R   R   t   RSAPublicKeyWithSerializationR-   R)   R6   R9   RE   RF   RG   RH   RK   RV   RW   R[   (    (    (    sL   lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyt   <module>   s8    &			'						+F