
    Eh!                     f    d Z dZddgZddlZddlmZ ddlmZmZm	Z	 ddl
  G d	 d      Zd
 Zd Zy)ar  
RSA digital signature protocol according to PKCS#1 v1.5

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

This scheme is more properly called ``RSASSA-PKCS1-v1_5``.

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

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

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

        >>> key = RSA.importKey(open('pubkey.der').read())
        >>> h = SHA.new(message)
        >>> verifier = PKCS1_v1_5.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PKCS115_SigScheme    N)ceil_div)DerSequenceDerNullDerOctetString)*c                   (    e Zd ZdZd Zd Zd Zd Zy)r   zLThis signature scheme can perform PKCS#1 v1.5 RSA signature or verification.c                     || _         y)a  Initialize this PKCS#1 v1.5 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.
        N)_key)selfkeys     \/var/www/html/bid_assistant/venv/lib/python3.12/site-packages/Crypto/Signature/PKCS1_v1_5.py__init__zPKCS115_SigScheme.__init__G   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PKCS115_SigScheme.can_signQ   s    yy$$&&r   c                     t         j                  j                  j                  | j                  j
                        }t        |d      }t        ||      }| j                  j                  |      }t        d      |t        |      z
  z  |z   }|S )az  Produce the PKCS#1 v1.5 signature of a message.
    
        This function is named ``RSASSA-PKCS1-V1_5-SIGN``, and is specified in
        section 8.2.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 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.
           r   )CryptoUtilnumbersizer   nr   EMSA_PKCS1_V1_5_ENCODEdecryptbchrlen)r   mhashmodBitskemmSs          r   signzPKCS115_SigScheme.signU   st    * ++$$))$))++6WQ $E1-IIb!J#a&!A%r   c                 l   t         j                  j                  j                  | j                  j
                        }t        |d      }t        |      |k7  ry| j                  j                  |d      d   }t        d      |t        |      z
  z  |z   }	 t        ||      }||k(  S # t        $ r Y yw xY w)a  Verify that a certain PKCS#1 v1.5 signature is authentic.
    
        This function checks if the party holding the private half of the key
        really signed the message.
    
        This function is named ``RSASSA-PKCS1-V1_5-VERIFY``, and is specified in
        section 8.2.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.
        r   r   )r   r   r   r   r   r   r   r   encryptr   r   
ValueError)r   r    r%   r!   r"   r$   em1em2s           r   verifyzPKCS115_SigScheme.verifyu   s    * ++$$))$))++6WQ q6Q;
 IIa#A&4j!CF(#a'	(2C Cx  		s   B' '	B32B3N)__name__
__module____qualname____doc__r   r   r&   r,    r   r   r   r   D   s    V'@,r   c                    t        | j                  t               j                         g      }t	        | j                               }t        |j                         |j                         g      j                         }|t        |      dz   k  rt        dt        |      z        t        d      |t        |      z
  dz
  z  }t        d      |z   t        d      z   |z   S )a@  
    Implement the ``EMSA-PKCS1-V1_5-ENCODE`` function, as defined
    in PKCS#1 v2.1 (RFC3447, 9.2).

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

    :Parameters:
     hash : hash object
            The hash object that holds the digest of the message being signed.
     emLen : int
            The length the final encoding must have, in bytes.

    :attention: the early standard (RFC2313) stated that ``DigestInfo``
        had to be BER-encoded. This means that old signatures
        might have length tags in indefinite form, which
        is not supported in DER. Such encoding cannot be
        reproduced by this function.

    :attention: the same standard defined ``DigestAlgorithm`` to be
        of ``AlgorithmIdentifier`` type, where the PARAMETERS
        item is optional. Encodings for ``MD2/4/5`` without
        ``PARAMETERS`` cannot be reproduced by this function.

    :Return: An ``emLen`` byte long string that encodes the hash.
       z8Selected hash algorith has a too long digest (%d bytes).      z r   )
r   oidr   encoder   digestr   r)   r   b)hashemLen
digestAlgor8   
digestInfoPSs         r   r   r      s    ` txx)9)9);<=J /F%%'MMO  vx  S_RSVYZ`Vaabb	dus:.2	3BZ=2T
*Z77r   c                     t        |       S )aH  Return a signature scheme object `PKCS115_SigScheme` that
    can be used to perform PKCS#1 v1.5 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.

    )r   )r   s    r   r   r      s     S!!r   )r0   __revision____all__Crypto.Util.numberr   r   Crypto.Util.asn1r   r   r   Crypto.Util.py3compatr   r   r   r1   r   r   <module>rE      sD   .#J &
(  ' A A #] ]~<8|
"r   