o
    `¨Êh�/  ã                   @   s˜   d Z dZddgZddlT ejd dkrejd dkrddlT ddlZdd	lm	Z	m
Z
mZ dd
lmZ G dd„ dƒZdd„ Zdd„ Zdd„ Zddd„ZdS )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
z$Id$ÚnewÚPSS_SigSchemeé    )Ú*é   é   N)Ú
ceil_shiftÚceil_divÚlong_to_bytes)Ústrxorc                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )r   zKThis signature scheme can perform PKCS#1 PSS RSA signature or verification.c                 C   s   || _ || _|| _dS )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ÚsaltLen© r   úL/var/www/html/env/lib/python3.10/site-packages/Crypto/Signature/PKCS1_PSS.pyÚ__init__O   s   
zPSS_SigScheme.__init__c                 C   s
   | j  ¡ S )zCReturn True if this cipher object can be used for signing messages.)r   Úhas_private)r   r   r   r   Úcan_sign`   s   
zPSS_SigScheme.can_signc           
         s’   | j j}| jdkrˆ j}n| j}| jr| j}n‡ fdd„}tjj | j j	¡}t
|dƒ}tˆ |d |||ƒ}| j  |¡}tdƒ|t|ƒ  | }	|	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©Úmhashr   r   Ú<lambda>†   ó    z$PSS_SigScheme.sign.<locals>.<lambda>é   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ÚSr   r   r   Úsignd   s   

zPSS_SigScheme.signc           
         sÆ   | j dkr	ˆ j}n| j }| jr| j}n‡ fdd„}tjj | jj¡}t	|dƒ}t
|ƒ|kr/dS | j |d¡d }t	|d dƒ}tdƒ|t
|ƒ  | }ztˆ ||d ||ƒ}	W |	S  tyb   Y dS 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                    r   r   r   r   r   r   r   r    °   r!   z&PSS_SigScheme.verify.<locals>.<lambda>r"   Fr   r   )r   r$   r   r%   r&   r'   r(   r   r)   r   r-   Úencryptr,   ÚEMSA_PSS_VERIFYÚ
ValueError)
r   r   r5   r/   r0   r1   r2   r3   ÚemLenÚresultr   r   r   Úverify”   s&   

ýÿzPSS_SigScheme.verifyN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r6   r<   r   r   r   r   r   L   s    0c                 C   sZ   t dƒ}tt||jƒƒD ]}t|dƒ}|| | | ¡ ¡  }qt|ƒ|ks'J ‚|d|… S )z,Mask Generation Function, described in B.2.1Ú é   N)ÚbÚranger   r$   r	   r   Údigestr-   )ÚmgfSeedÚmaskLenÚhashÚTÚcounterÚcr   r   r   r   É   s   
r   c                 C   s  t |dƒ}d}td| | ƒD ]}|d? dB }q|| j| d k r%tdƒ‚tdƒ}|r3|dkr3||ƒ}|  tdƒd |  ¡  | ¡}	tdƒ|| | j d  tdƒ | }
||	 ¡ || j d ƒ}t|
|ƒ}tt	|d ƒ| @ ƒ|dd…  }||	 ¡  td	ƒ }|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.rA   Né¼   )
r   rD   r$   r9   rC   r   r,   rE   r
   Úbord)r   ÚemBitsÚrandFuncr0   r/   r:   ÚlmaskÚiÚsaltÚhÚdbÚdbMaskÚmaskedDBr3   r   r   r   r*   Ò   s    
&
"r*   c                 C   sV  t |dƒ}d}td| | ƒD ]}|d? dB }q|| j| d k r#dS t|dd… ƒd	kr/dS |d|| j d … }||| j d d… }	|t|d ƒ@ rOdS ||	|| j d ƒ}
t||
ƒ}tt|d ƒ| @ ƒ|dd…  }| tdƒ|| j | d  tdƒ ¡s…dS td
ƒ}|r’|| d… }|  	tdƒd |  
¡  | ¡ 
¡ }|	|kr©dS dS )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   rL   r   FéÿÿÿÿNrM   rA   T)r   rD   r$   ÚordrN   r
   r,   Ú
startswithrC   r   rE   )r   r3   rO   r0   r/   r:   rQ   rR   rW   rT   rV   rU   rS   Úhpr   r   r   r8     s.   

"("r8   c                 C   s   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   r   r   r   r   Q  s   )NN)r@   Ú__revision__Ú__all__ÚCrypto.Util.py3compatÚsysÚversion_infoÚCrypto.Util.py21compatÚCrypto.Util.numberr%   r   r   r	   ÚCrypto.Util.strxorr
   r   r   r*   r8   r   r   r   r   r   Ú<module>   s   +}	;D