o
    Þý°jÅ"  ã                   @   sX   d dl Zd dl mZmZmZ d dlmZmZmZm	Z	 G dd„ dƒZ
ddd„Zd	d
„ ZdS )é    N)Úceil_divÚbytes_to_longÚlong_to_bytes)ÚDerSequenceÚDerNullÚDerOctetStringÚDerObjectIdc                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚPKCS115_SigSchemez†A signature object for ``RSASSA-PKCS1-v1_5``.
    Do not instantiate directly.
    Use :func:`Cryptodome.Signature.pkcs1_15.new`.
    c                 C   s
   || _ dS )a  Initialize this PKCS#1 v1.5 signature scheme object.

        :Parameters:
          rsa_key : an RSA key object
            Creation of signatures is only possible if this is a *private*
            RSA key. Verification of signatures is always possible.
        N)Ú_key)ÚselfÚrsa_key© r   ú�/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/Cryptodome/Signature/pkcs1_15.pyÚ__init__)   s   
zPKCS115_SigScheme.__init__c                 C   s
   | j  ¡ S )z<Return ``True`` if this object can be used to sign messages.)r
   Úhas_private)r   r   r   r   Úcan_sign3   s   
zPKCS115_SigScheme.can_signc                 C   sb   t jj | jj¡}t|dƒ}t||ƒ}t|ƒ}| j 	|¡}|t
t|ƒ| jj| jjƒkr/tdƒ‚|S )a„  Create the PKCS#1 v1.5 signature of a message.

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

        :parameter msg_hash:
            This is an object from the :mod:`Cryptodome.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.
        é   z+Fault detected in RSA private key operation)Ú
CryptodomeÚUtilÚnumberÚsizer
   Únr   Ú_EMSA_PKCS1_V1_5_ENCODEr   Ú_decrypt_to_bytesÚpowÚeÚ
ValueError)r   Úmsg_hashÚmodBitsÚkÚemÚem_intÚ	signaturer   r   r   Úsign7   s   

zPKCS115_SigScheme.signc           
      C   sÊ   t jj | jj¡}t|dƒ}t|ƒ|krtdƒ‚t	|ƒ}| j 
|¡}t||ƒ}z(t||dƒg}z|j d¡}	W n tyB   d}	Y nw |	sN| t||dƒ¡ W n tyZ   tdƒ‚w ||vrctdƒ‚dS )a|  Check if the  PKCS#1 v1.5 signature over a message is valid.

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

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

        :parameter signature:
            The signature that needs to be validated.
        :type signature: byte string

        :raise ValueError: if the signature is not valid.
        r   zInvalid signatureTz1.2.840.113549.2.FN)r   r   r   r   r
   r   r   Úlenr   r   Ú_encryptr   r   ÚoidÚ
startswithÚAttributeErrorÚappend)
r   r   r"   r   r   Úsignature_intr!   Úem1Úpossible_em1Úalgorithm_is_mdr   r   r   ÚverifyW   s.   

ÿ€ÿzPKCS115_SigScheme.verifyN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r#   r.   r   r   r   r   r	   #   s    
 r	   Tc                 C   s�   t t| jƒ ¡ gƒ}|r| tƒ  ¡ ¡ t|  ¡ ƒ}t | ¡ | ¡ gƒ ¡ }|t|ƒd k r6t	dt|ƒ ƒ‚d|t|ƒ d  }d| d | 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:
     msg_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.
     with_hash_parameters : bool
            If True (default), include NULL parameters for the hash
            algorithm in the ``digestAlgorithm`` SEQUENCE.

    :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.

    :Return: An ``emLen`` byte long string that encodes the hash.
    é   z3DigestInfo is too long for this RSA key (%d bytes).ó   ÿé   s    ó    )
r   r   r&   Úencoder)   r   r   Údigestr$   Ú	TypeError)r   ÚemLenÚwith_hash_parametersÚ
digestAlgor8   Ú
digestInfoÚPSr   r   r   r   Ž   s   2þýr   c                 C   s   t | ƒS )aŠ  Create a signature object for creating
    or verifying PKCS#1 v1.5 signatures.

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

    :return: a :class:`PKCS115_SigScheme` signature object
    )r	   )r   r   r   r   ÚnewÒ   s   r?   )T)ÚCryptodome.Util.numberr   r   r   r   ÚCryptodome.Util.asn1r   r   r   r   r	   r   r?   r   r   r   r   Ú<module>   s   
kD