
    i5                     x    S SK JrJrJr  S SKrS SKJrJrJr  S SK	J
r
  S SKJr   " S S5      rS rS	 rS
 rS rg)    )bchrbord
iter_rangeN)ceil_divlong_to_bytesbytes_to_long)strxor)Randomc                   0    \ rS rSrSrS rS rS rS rSr	g)	PSS_SigScheme)   zjA signature object for ``RSASSA-PSS``.
Do not instantiate directly.
Use :func:`Crypto.Signature.pss.new`.
c                 4    Xl         X0l        X l        X@l        g)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 : integer
    Length of the salt, in bytes.
  randfunc : callable
    A function that returns random bytes.
N)_key_saltLen_mgfunc	_randfunc)selfkeymgfuncsaltLenrandfuncs        O/var/www/html/trading/venv/lib/python3.13/site-packages/Crypto/Signature/pss.py__init__PSS_SigScheme.__init__/   s    $ 	!    c                 6    U R                   R                  5       $ )z<Return ``True`` if this object can be used to sign messages.)r   has_private)r   s    r   can_signPSS_SigScheme.can_signF   s    yy$$&&r   c                 B  ^ U R                   c  TR                  nOU R                   nU R                  c  U4S jnOU R                  n[        R                  R
                  R                  U R                  R                  5      n[        US5      n[        TUS-
  U R                  X25      n[        U5      nU R                  R                  U5      nU[        [        U5      U R                  R                  U R                  R                  5      :w  a  [!        S5      eU$ )a&  Create the PKCS#1 PSS signature of a message.

This function is also called ``RSASSA-PSS-SIGN`` and
it is specified in
`section 8.1.1 of RFC8017 <https://tools.ietf.org/html/rfc8017#section-8.1.1>`_.

:parameter msg_hash:
    This is an object from the :mod:`Crypto.Hash` package.
    It has been used to digest the message to sign.
:type msg_hash: hash object

:return: the signature encoded as a *byte string*.
:raise ValueError: if the RSA key is not long enough for the given hash algorithm.
:raise TypeError: if the RSA key has no private half.
c                    > [        XT5      $ NMGF1xymsg_hashs     r   <lambda>$PSS_SigScheme.sign.<locals>.<lambda>b       tA(3r         z+Fault detected in RSA private key operation)r   digest_sizer   CryptoUtilnumbersizer   nr   _EMSA_PSS_ENCODEr   r   _decrypt_to_bytespowe
ValueError)	r   r(   sLenmgfmodBitskemem_int	signatures	    `       r   signPSS_SigScheme.signJ   s    $ == ''D==D<<3C,,C++$$))$))++6 Wa h	4>>3Mr"II//7	Sy1499;;		LLJKKr   c                    ^ U R                   c  TR                  nOU R                   nU R                  (       a  U R                  nOU4S jn[        R                  R
                  R                  U R                  R                  5      n[        US5      n[        U5      U:w  a  [        S5      e[        U5      nU R                  R                  U5      n[        US-
  S5      n	[        X5      n
[        TXS-
  XC5        g)a  Check if the  PKCS#1 PSS signature over a message is valid.

This function is also called ``RSASSA-PSS-VERIFY`` and
it is specified in
`section 8.1.2 of RFC8037 <https://tools.ietf.org/html/rfc8017#section-8.1.2>`_.

:parameter msg_hash:
    The hash that was carried out over the message. This is an object
    belonging to the :mod:`Crypto.Hash` module.
:type parameter: hash object

:parameter signature:
    The signature that needs to be validated.
:type signature: bytes

:raise ValueError: if the signature is not valid.
Nc                    > [        XT5      $ r"   r#   r%   s     r   r)   &PSS_SigScheme.verify.<locals>.<lambda>   r+   r   r,   Incorrect signaturer-   )r   r.   r   r/   r0   r1   r2   r   r3   r   lenr8   r   _encryptr   _EMSA_PSS_VERIFY)r   r(   r?   r9   r:   r;   r<   signature_intr>   emLenr=   s    `         r   verifyPSS_SigScheme.verifyu   s    ( == ''D==D<<,,C3C++$$))$))++6 Wa y>Q233%i0##M21a(6)2qy#<r   )r   r   r   r   N)
__name__
__module____qualname____firstlineno____doc__r   r   r@   rK   __static_attributes__ r   r   r   r   )   s    
".')V,=r   r   c                     Sn[        [        XR                  5      5       HD  n[        US5      nUR	                  5       nUR                  X-   5        X6R                  5       -   nMF     [        U5      U:  d   eUSU $ )a  Mask Generation Function, described in `B.2.1 of RFC8017
<https://tools.ietf.org/html/rfc8017>`_.

:param mfgSeed:
    seed from which the mask is generated
:type mfgSeed: byte string

:param maskLen:
    intended length in bytes of the mask
:type maskLen: integer

:param hash_gen:
    A module or a hash object from :mod:`Crypto.Hash`
:type hash_object:

:return: the mask, as a *byte string*
r      N)r   r   r.   r   newupdatedigestrF   )mgfSeedmaskLenhash_genTcounterchobjs          r   r$   r$      sw    & 	Ahw0D0DEF'1%||~GK 	 G
 q6WXg;r   c                 z   [        US5      nSn[        SU-  U-
  5       H  nUS-	  S-  nM     XPR                  U-   S-   :  a  [        S5      eU" U5      n[	        S5      S-  U R                  5       -   U-   n	U R                  5       n
U
R                  U	5        [	        S5      XT-
  U R                  -
  S-
  -  nU[	        S5      -   U-   nU" U
R                  5       XPR                  -
  S-
  5      n[        X5      n[	        [        US   5      U) -  5      USS -   nXR                  5       -   [	        S5      -   nU$ )	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-         z6Digest or salt length are too long for given key size.N   )
r   r   r.   r8   r   rX   rV   rW   r	   r   )mhashemBitsrandFuncr:   r9   rJ   lmaskisaltm_primehpsdbdbMaskmaskedDBr=   s                   r   r4   r4      sB   < VQE E%'
T! (
   %a'' 0 1 	1 D>D1gai%,,.(4/G		AHHW	a%*U...q0	1B	d1g	BU#4#44Q67Fb!HD!%./(12,>H	HHJ	d	+BIr   c                 l   [        US5      nSn[        SU-  U-
  5       H  nUS-	  S-  nM     XPR                  U-   S-   :  a  [        S5      e[	        USS 5      S	:w  a  [        S5      eUSXPR                  -
  S-
   nXU R                  -
  S-
  S n	U[        US   5      -  (       a  [        S5      eU" XU R                  -
  S-
  5      n
[        X5      n[        [        US   5      U) -  5      USS -   nUR                  [        S5      XPR                  -
  U-
  S-
  -  [        S5      -   5      (       d  [        S5      eUS:  a  X* S nOS
n[        S5      S-  U R                  5       -   U-   nU R                  5       nUR                  U5        UR                  5       nX:w  a  [        S5      eg)aS  
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.

:Raise ValueError:
    When the encoding is inconsistent, or the digest or salt lengths
    are too big.
r,   r   r-   ra   rb   rE   Nrc   r   )r   r   r.   r8   ordr   r	   r   
startswithrX   rV   rW   )rd   r=   re   r:   r9   rJ   rg   rh   ro   rk   rn   rm   ri   rj   r_   hps                   r   rH   rH     s   8 VQE E%'
T! (
   %a''.//
2bc7|t.//,5***1,-H
"""1$R(AtBqE{.//%+++A-.F		!B	d2a5kUF"	#bf	,B==a%(9(9"9$">q"@ADGKLL.//ax%&z1gai%,,.(4/G99;DKK	Bw.// r   c                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUc  [        R                  nU(       a%  [        S[	        UR                  5       5      -   5      e[        XX45      $ )av  Create an object for making or verifying PKCS#1 PSS signatures.

:parameter rsa_key:
  The RSA key to use for signing or verifying the message.
  This is a :class:`Crypto.PublicKey.RSA` object.
  Signing is only possible when ``rsa_key`` is a **private** RSA key.
:type rsa_key: RSA object

:Keyword Arguments:

    *   *mask_func* (``callable``) --
        A function that returns the mask (as `bytes`).
        It must accept two parameters: a seed (as `bytes`)
        and the length of the data to return.

        If not specified, it will be the function :func:`MGF1` defined in
        `RFC8017 <https://tools.ietf.org/html/rfc8017#page-67>`_ and
        combined with the same hash algorithm applied to the
        message to sign or verify.

        If you want to use a different function, for instance still :func:`MGF1`
        but together with another hash, you can do::

            from Crypto.Hash import SHA256
            from Crypto.Signature.pss import MGF1
            mgf = lambda x, y: MGF1(x, y, SHA256)

    *   *salt_bytes* (``integer``) --
        Length of the salt, in bytes.
        It is a value between 0 and ``emLen - hLen - 2``, where ``emLen``
        is the size of the RSA modulus and ``hLen`` is the size of the digest
        applied to the message to sign or verify.

        The salt is generated internally, you don't need to provide it.

        If not specified, the salt length will be ``hLen``.
        If it is zero, the signature scheme becomes deterministic.

        Note that in some implementations such as OpenSSL the default
        salt length is ``emLen - hLen - 2`` (even though it is not more
        secure than ``hLen``).

    *   *rand_func* (``callable``) --
        A function that returns random ``bytes``, of the desired length.
        The default is :func:`Crypto.Random.get_random_bytes`.

:return: a :class:`PSS_SigScheme` signature object
	mask_funcN
salt_bytes	rand_funczUnknown keywords: )popr
   get_random_bytesr8   strkeysr   )rsa_keykwargsrv   salt_lenrx   s        r   rV   rV   J  sr    d 

;-Izz,-H

;-I++	-FKKM0BBCCXAAr   )Crypto.Util.py3compatr   r   r   Crypto.Util.numberr/   r   r   r   Crypto.Util.strxorr	   r
   r   r$   r4   rH   rV   rS   r   r   <module>r      sG   > 9 8 " " & x= x=v:=@F0R9Br   