ó
j4Vdc           @   sÞ   d  Z  d d l m Z d Z d d g Z d d l Te j d d k re e j d	 d	 k re d d l Tn  d d
 l	 Z
 d d l	 m Z m Z m Z d d l m Z d d d „  ƒ  YZ d „  Z d „  Z d „  Z d
 d
 d „ Z d
 S(   s‘  RSA digital signature protocol with appendix according to PKCS#1 PSS.

See RFC3447__ or the `original RSA Labs specification`__.

This scheme is more properly called ``RSASSA-PSS``.

For example, a sender may authenticate a message using SHA-1 and PSS like
this:

    >>> from Crypto.Signature import PKCS1_PSS
    >>> from Crypto.Hash import SHA
    >>> from Crypto.PublicKey import RSA
    >>> from Crypto import Random
    >>>
    >>> message = 'To be signed'
    >>> key = RSA.importKey(open('privkey.der').read())
    >>> h = SHA.new()
    >>> h.update(message)
    >>> signer = PKCS1_PSS.new(key)
    >>> signature = PKCS1_PSS.sign(key)

At the receiver side, verification can be done like using the public part of
the RSA key:

    >>> key = RSA.importKey(open('pubkey.der').read())
    >>> h = SHA.new()
    >>> h.update(message)
    >>> verifier = PKCS1_PSS.new(key)
    >>> if verifier.verify(h, signature):
    >>>     print "The signature is authentic."
    >>> else:
    >>>     print "The signature is not authentic."

:undocumented: __revision__, __package__

.. __: http://www.ietf.org/rfc/rfc3447.txt
.. __: http://www.rsa.com/rsalabs/node.asp?id=2125
iÿÿÿÿ(   t   nested_scopess   $Id$t   newt   PSS_SigScheme(   t   *i    i   i   N(   t
   ceil_shiftt   ceil_divt   long_to_bytes(   t   strxorc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sK   This signature scheme can perform PKCS#1 PSS RSA signature or verification.c         C   s   | |  _  | |  _ | |  _ d S(   s!  Initialize this PKCS#1 PSS signature scheme object.
        
        :Parameters:
         key : an RSA key object
                If a private half is given, both signature and verification are possible.
                If a public half is given, only verification is possible.
         mgfunc : callable
                A mask generation function that accepts two parameters: a string to
                use as seed, and the lenth of the mask to generate, in bytes.
         saltLen : int
                Length of the salt, in bytes.
        N(   t   _keyt   _saltLent   _mgfunc(   t   selft   keyt   mgfunct   saltLen(    (    sh   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/Crypto/Signature/PKCS1_PSS.pyt   __init__O   s    		c         C   s   |  j  j ƒ  S(   sC   Return True if this cipher object can be used for signing messages.(   R   t   has_private(   R   (    (    sh   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/Crypto/Signature/PKCS1_PSS.pyt   can_sign`   s    c   
         sÎ   |  j  j } |  j d k r' ˆ  j } n	 |  j } |  j rE |  j } n ‡  f d †  } t j j j	 |  j  j
 ƒ } t | d ƒ } t ˆ  | d | | | ƒ } |  j  j | ƒ } t d ƒ | t | ƒ | }	 |	 S(   sB  Produce the PKCS#1 PSS signature of a message.
    
        This function is named ``RSASSA-PSS-SIGN``, and is specified in
        section 8.1.1 of RFC3447.
    
        :Parameters:
         mhash : hash object
                The hash that was carried out over the message. This is an object
                belonging to the `Crypto.Hash` module.
   
        :Return: The PSS signature encoded as a string.
        :Raise ValueError:
            If the RSA key length is not sufficiently long to deal with the given
            hash algorithm.
        :Raise TypeError:
            If the RSA key has no private half.
    
        :attention: Modify the salt length and the mask generation function only
                    if you know what you are doing.
                    The receiver must use the same parameters too.
        c            s   t  |  | ˆ  ƒ S(   N(   t   MGF1(   t   xt   y(   t   mhash(    sh   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/Crypto/Signature/PKCS1_PSS.pyt   <lambda>†   t    i   i   i    N(   R   t	   _randfuncR	   t   Nonet   digest_sizeR
   t   Cryptot   Utilt   numbert   sizet   nR   t   EMSA_PSS_ENCODEt   decryptt   bchrt   len(
   R   R   t   randfunct   sLent   mgft   modBitst   kt   emt   mt   S(    (   R   sh   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/Crypto/Signature/PKCS1_PSS.pyt   signd   s    		c   
         s  |  j  d k r ˆ  j } n	 |  j  } |  j r9 |  j } n ‡  f d †  } t j j j |  j j	 ƒ } t
 | d ƒ } t | ƒ | k rˆ t S|  j j | d ƒ d } t
 | d d ƒ } t d ƒ | t | ƒ | } y  t ˆ  | | d | | ƒ }	 Wn t k
 rt SX|	 S(   s  Verify that a certain PKCS#1 PSS signature is authentic.
    
        This function checks if the party holding the private half of the given
        RSA key has really signed the message.
    
        This function is called ``RSASSA-PSS-VERIFY``, and is specified in section
        8.1.2 of RFC3447.
    
        :Parameters:
         mhash : hash object
                The hash that was carried out over the message. This is an object
                belonging to the `Crypto.Hash` module.
         S : string
                The signature that needs to be validated.
    
        :Return: True if verification is correct. False otherwise.
        c            s   t  |  | ˆ  ƒ S(   N(   R   (   R   R   (   R   (    sh   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/Crypto/Signature/PKCS1_PSS.pyR   °   R   i   i    i   N(   R	   R   R   R
   R   R   R   R   R   R   R   R#   t   Falset   encryptR"   t   EMSA_PSS_VERIFYt
   ValueError(
   R   R   R+   R%   R&   R'   R(   R)   t   emLent   result(    (   R   sh   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/Crypto/Signature/PKCS1_PSS.pyt   verify”   s$    		 (   t   __name__t
   __module__t   __doc__R   R   R,   R3   (    (    (    sh   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/Crypto/Signature/PKCS1_PSS.pyR   L   s
   			0c         C   s{   t  d ƒ } xL t t | | j ƒ ƒ D]2 } t | d ƒ } | | j |  | ƒ j ƒ  } q% Wt | ƒ | k ss t ‚ | |  S(   s,   Mask Generation Function, described in B.2.1R   i   (	   t   bt   xrangeR   R   R   R   t   digestR#   t   AssertionError(   t   mgfSeedt   maskLent   hasht   Tt   countert   c(    (    sh   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/Crypto/Signature/PKCS1_PSS.pyR   É   s    !c         C   sY  t  | d ƒ } d } x* t d | | ƒ D] } | d ?d B} q* W| |  j | d k  rh t d ƒ ‚ n  t d ƒ } | r• | d k r• | | ƒ } n  |  j t d ƒ d |  j ƒ  | ƒ }	 t d ƒ | | |  j d t d ƒ | }
 | |	 j ƒ  | |  j d ƒ } t |
 | ƒ } t t	 | d ƒ | @ƒ | d } | |	 j ƒ  t d ƒ } | S(	   s%  
    Implement the ``EMSA-PSS-ENCODE`` function, as defined
    in PKCS#1 v2.1 (RFC3447, 9.1.1).

    The original ``EMSA-PSS-ENCODE`` actually accepts the message ``M`` as input,
    and hash it internally. Here, we expect that the message has already
    been hashed instead.

    :Parameters:
     mhash : hash object
            The hash object that holds the digest of the message being signed.
     emBits : int
            Maximum length of the final encoding, in bits.
     randFunc : callable
            An RNG function that accepts as only parameter an int, and returns
            a string of random bytes, to be used as salt.
     mgf : callable
            A mask generation function that accepts two parameters: a string to
            use as seed, and the lenth of the mask to generate, in bytes.
     sLen : int
            Length of the salt, in bytes.

    :Return: An ``emLen`` byte long string that encodes the hash
            (with ``emLen = \ceil(emBits/8)``).

    :Raise ValueError:
        When digest or salt length are too big.
    i   i    i   i€   i   s6   Digest or salt length are too long for given key size.R   i¼   (
   R   R8   R   R0   R7   R   R"   R9   R   t   bord(   R   t   emBitst   randFuncR&   R%   R1   t   lmaskt   it   saltt   ht   dbt   dbMaskt   maskedDBR)   (    (    sh   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/Crypto/Signature/PKCS1_PSS.pyR    Ò   s     '- #c         C   sŸ  t  | d ƒ } d } x* t d | | ƒ D] } | d ?d B} q* W| |  j | d k  r] t St | d ƒ d k rw t S| | |  j d  } | | |  j d d !}	 | t | d ƒ @r¼ t S| |	 | |  j d ƒ }
 t | |
 ƒ } t t | d ƒ | @ƒ | d } | j t d ƒ | |  j | d t d ƒ ƒ s>t St	 d ƒ } | r^| | } n  |  j
 t d ƒ d |  j ƒ  | ƒ j ƒ  } |	 | k r›t St S(	   sÜ  
    Implement the ``EMSA-PSS-VERIFY`` function, as defined
    in PKCS#1 v2.1 (RFC3447, 9.1.2).

    ``EMSA-PSS-VERIFY`` actually accepts the message ``M`` as input,
    and hash it internally. Here, we expect that the message has already
    been hashed instead.

    :Parameters:
     mhash : hash object
            The hash object that holds the digest of the message to be verified.
     em : string
            The signature to verify, therefore proving that the sender really signed
            the message that was received.
     emBits : int
            Length of the final encoding (em), in bits.
     mgf : callable
            A mask generation function that accepts two parameters: a string to
            use as seed, and the lenth of the mask to generate, in bytes.
     sLen : int
            Length of the salt, in bytes.

    :Return: 0 if the encoding is consistent, 1 if it is inconsistent.

    :Raise ValueError:
        When digest or salt length are too big.
    i   i    i   i€   i   iÿÿÿÿi¼   R   (   R   R8   R   R-   t   ordRA   R   R"   t
   startswithR7   R   R9   t   True(   R   R)   RB   R&   R%   R1   RD   RE   RJ   RG   RI   RH   RF   t   hp(    (    sh   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/Crypto/Signature/PKCS1_PSS.pyR/     s0    #2 -c         C   s   t  |  | | ƒ S(   sª  Return a signature scheme object `PSS_SigScheme` that
    can be used to perform PKCS#1 PSS signature or verification.

    :Parameters:
     key : RSA key object
        The key to use to sign or verify the message. This is a `Crypto.PublicKey.RSA` object.
        Signing is only possible if *key* is a private RSA key.
     mgfunc : callable
        A mask generation function that accepts two parameters: a string to
        use as seed, and the lenth of the mask to generate, in bytes.
        If not specified, the standard MGF1 is used.
     saltLen : int
        Length of the salt, in bytes. If not specified, it matches the output
        size of the hash function.
 
    (   R   (   R   R   R   (    (    sh   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/Crypto/Signature/PKCS1_PSS.pyR   Q  s    (    (   R6   t
   __future__R    t   __revision__t   __all__t   Crypto.Util.py3compatt   syst   version_infot   Crypto.Util.py21compatt   Crypto.Util.numberR   R   R   R   t   Crypto.Util.strxorR   R   R   R    R/   R   R   (    (    (    sh   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/Crypto/Signature/PKCS1_PSS.pyt   <module><   s   
&}			;	D