
    Eh/                         d Z ddlmZ dZddgZddl ej                  d   dk(  rej                  d   dk(  rddl dd	l	Z
dd
l	mZmZmZ ddlmZ  G d d      Zd Zd Zd ZddZy	)a  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
    )nested_scopesz$Id$newPSS_SigScheme)*      N)
ceil_shiftceil_divlong_to_bytes)strxorc                   (    e Zd ZdZd Zd Zd Zd Zy)r   zKThis signature scheme can perform PKCS#1 PSS RSA signature or verification.c                 .    || _         || _        || _        y)a!  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)_key_saltLen_mgfunc)selfkeymgfuncsaltLens       [/var/www/html/bid_assistant/venv/lib/python3.12/site-packages/Crypto/Signature/PKCS1_PSS.py__init__zPSS_SigScheme.__init__O   s     	    c                 6    | j                   j                         S )zCReturn True if this cipher object can be used for signing messages.)r   has_private)r   s    r   can_signzPSS_SigScheme.can_sign`   s    yy$$&&r   c                    | j                   j                  }| j                  dk(  rj                  }n| j                  }| j                  r| j                  }nfd}t
        j                  j                  j                  | j                   j                        }t        |d      }t        |dz
  |||      }| j                   j                  |      }t        d      |t        |      z
  z  |z   }	|	S )aB  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.
        Nc                     t        | |      S NMGF1xymhashs     r   <lambda>z$PSS_SigScheme.sign.<locals>.<lambda>   s    Qq r      r   r   )r   	_randfuncr   digest_sizer   CryptoUtilnumbersizenr
   EMSA_PSS_ENCODEdecryptbchrlen)
r   r$   randfuncsLenmgfmodBitskemmSs
    `        r   signzPSS_SigScheme.signd   s    0 99&& ==D $$D==D<<,,C/S++$$))$))++6 WQUGAIxdCIIb!J#a&!A%r   c                    | j                   dk(  rj                  }n| j                   }| j                  r| j                  }nfd}t        j                  j
                  j                  | j                  j                        }t        |d      }t        |      |k7  ry| j                  j                  |d      d   }t        |dz
  d      }t        d      |t        |      z
  z  |z   }	 t        ||dz
  ||      }	|	S # t        $ r Y yw xY w)a  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.
        Nc                     t        | |      S r   r   r!   s     r   r%   z&PSS_SigScheme.verify.<locals>.<lambda>   s    tAa r   r&   Fr   r   )r   r(   r   r)   r*   r+   r,   r   r-   r
   r1   encryptr0   EMSA_PSS_VERIFY
ValueError)
r   r$   r9   r3   r4   r5   r6   r7   emLenresults
    `        r   verifyzPSS_SigScheme.verify   s    * ==D $$D==D<<,,C.C++$$))$))++6 WQq6Q;
 YYq!$Q'1%$Zs2w'",	$UB	3EF   		s   ,D   	DDN)__name__
__module____qualname____doc__r   r   r:   rB    r   r   r   r   L   s    U"'.`3r   c                     t        d      }t        t        ||j                              D ]3  }t	        |d      }||j                  | |z         j                         z   }5 t        |      |k\  sJ |d| S )z,Mask Generation Function, described in B.2.1    N)bxranger
   r(   r   r   digestr1   )mgfSeedmaskLenhashTcountercs         r   r    r       sv    	"A(7D,<,<=> /'1%1%,,../ q67??Xg;r   c                 z   t        |d      }d}t        d|z  |z
        D ]
  }|dz	  dz  } || j                  |z   dz   k  rt        d      t	        d      }|r|dkD  r ||      }| j                  t        d      dz  | j                         z   |z         }	t        d      ||z
  | j                  z
  dz
  z  t        d      z   |z   }
 ||	j                         || j                  z
  dz
        }t        |
|      }t        t        |d         | z        |dd z   }||	j                         z   t        d	      z   }|S )
a%  
    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.
    r&   r   r      r   z6Digest or salt length are too long for given key size.rI   N   )
r
   rL   r(   r?   rK   r   r0   rM   r   bord)r$   emBitsrandFuncr4   r3   r@   lmaskisalthdbdbMaskmaskedDBr7   s                 r   r.   r.      sH   < VAE EAeGFN#  q4 
 u  %a''QRRR5DDF~		$t*Q,/$67A	dU4Z 1 11!3	4tDz	AD	HBU5#4#44Q67Fb HD!%./(12,>H	AHHJ	d	+BIr   c                    t        |d      }d}t        d|z  |z
        D ]
  }|dz	  dz  } || j                  |z   dz   k  ryt        |dd       d	k7  ry|d|| j                  z
  dz
   }||| j                  z
  dz
  d }	|t	        |d         z  ry ||	|| j                  z
  dz
        }
t        ||
      }t        t	        |d         | z        |dd z   }|j                  t        d      || j                  z
  |z
  dz
  z  t        d      z         syt        d
      }|r|| d }| j                  t        d      dz  | j                         z   |z         j                         }|	|k7  ryy)a  
    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.
    r&   r   r   rU   r   FNrV   rI   T)r
   rL   r(   ordrW   r   r0   
startswithrK   r   rM   )r$   r7   rX   r4   r3   r@   rZ   r[   r`   r]   r_   r^   r\   hps                 r   r>   r>     s   : VAE EAeGFN#  q4 
 u  %a''
2bc7|T,5***1,-H
5"""1$R(AtBqE{E%+++A-.F	&	!B	d2a5kUF"	#bf	,B==dU5+<+<%<T%A!%CDtDzQRR5DBuvJT	4:a<%,,.047	8	?	?	AB"ur   c                     t        | ||      S )a  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   s      r   r   r   Q  s    " fg..r   )NN)rF   
__future__r   __revision____all__Crypto.Util.py3compatsysversion_infoCrypto.Util.py21compatCrypto.Util.numberr)   r	   r
   r   Crypto.Util.strxorr   r   r    r.   r>   r   rG   r   r   <module>rp      sz   .%R %?
$ #A! 0 0 3q 8(  B B %{ {z9vBH/r   