o
    Ìn~b³"  ã                   @   st   d dl mZ d dlZd dlmZmZ d dlZd dlmZm	Z	m
Z
 d dlmZ d dlmZ G dd„ dƒZdd
d„ZdS )é    )ÚMGF1N)ÚbordÚ_copy_bytes)Úceil_divÚbytes_to_longÚlong_to_bytes)Ústrxor)ÚRandomc                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚPKCS1OAEP_CipherzXCipher object for PKCS#1 v1.5 OAEP.
    Do not create directly: use :func:`new` instead.c                    sN   |ˆ _ |r	|ˆ _ntjjˆ _|r|ˆ _n‡ fdd„ˆ _tdd|ƒˆ _|ˆ _dS )a  Initialize this PKCS#1 OAEP cipher object.

        :Parameters:
         key : an RSA key object
                If a private half is given, both encryption and decryption are possible.
                If a public half is given, only encryption is possible.
         hashAlgo : hash object
                The hash function to use. This can be a module under `Cryptodome.Hash`
                or an existing hash object created from any of such modules. If not specified,
                `Cryptodome.Hash.SHA1` is used.
         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 consistent with ``hashAlgo`` is used (a safe choice).
         label : bytes/bytearray/memoryview
                A label to apply to this particular encryption. If not specified,
                an empty string is used. Specifying a label does not improve
                security.
         randfunc : callable
                A function that returns random bytes.

        :attention: Modify the mask generation function only if you know what you are doing.
                    Sender and receiver must use the same one.
        c                    s   t | |ˆ jƒS )N)r   Ú_hashObj)ÚxÚy©Úself© úG/usr/local/lib/python3.10/dist-packages/Cryptodome/Cipher/PKCS1_OAEP.pyÚ<lambda>G   s    z+PKCS1OAEP_Cipher.__init__.<locals>.<lambda>N)	Ú_keyr   Ú
CryptodomeZHashÚSHA1Ú_mgfr   Ú_labelÚ	_randfunc)r   ÚkeyÚhashAlgoÚmgfuncÚlabelÚrandfuncr   r   r   Ú__init__$   s   

zPKCS1OAEP_Cipher.__init__c                 C   ó
   | j  ¡ S )zVLegacy function to check if you can call :meth:`encrypt`.

        .. deprecated:: 3.0)r   Úcan_encryptr   r   r   r   r    L   ó   
zPKCS1OAEP_Cipher.can_encryptc                 C   r   )zVLegacy function to check if you can call :meth:`decrypt`.

        .. deprecated:: 3.0)r   Úcan_decryptr   r   r   r   r"   R   r!   zPKCS1OAEP_Cipher.can_decryptc                 C   sî   t jj | jj¡}t|dƒ}| jj}t	|ƒ}|| d|  d }|dk r(t
dƒ‚| j | j¡ ¡ }d| }|| d tdd|ƒ }	|  |¡}
|  |
|| d ¡}t|	|ƒ}|  ||¡}t|
|ƒ}d| | }t|ƒ}| j |¡}t||ƒ}|S )	a\  Encrypt a message with PKCS#1 OAEP.

        :param message:
            The message to encrypt, also known as plaintext. It can be of
            variable length, but not longer than the RSA modulus (in bytes)
            minus 2, minus twice the hash output size.
            For instance, if you use RSA 2048 and SHA-256, the longest message
            you can encrypt is 190 byte long.
        :type message: bytes/bytearray/memoryview

        :returns: The ciphertext, as large as the RSA modulus.
        :rtype: bytes

        :raises ValueError:
            if the message is too long.
        é   é   r   zPlaintext is too long.ó    ó   Né   )r   ÚUtilÚnumberÚsizer   Únr   r   Údigest_sizeÚlenÚ
ValueErrorÚnewr   Údigestr   r   r   r   r   Z_encryptr   )r   ÚmessageÚmodBitsÚkÚhLenZmLenZps_lenÚlHashZpsÚdbZrosÚdbMaskÚmaskedDBÚseedMaskÚ
maskedSeedÚemZem_intÚm_intÚcr   r   r   ÚencryptX   s(   




zPKCS1OAEP_Cipher.encryptc                 C   sd  t jj | jj¡}t|dƒ}| jj}t	|ƒ|ks||d k r"t
dƒ‚t|ƒ}| j |¡}t||ƒ}| j | j¡ ¡ }|d }	|d|d … }
||d d… }|  ||¡}t|
|ƒ}|  ||| d ¡}t||ƒ}|||d…  d¡ }|d|… }t|	ƒt||k ƒB }t||ƒ}|D ]}|t|ƒO }qŠ|||… D ]}|t|ƒO }q™|dkrªt
dƒ‚||d d… S )	a5  Decrypt a message with PKCS#1 OAEP.

        :param ciphertext: The encrypted message.
        :type ciphertext: bytes/bytearray/memoryview

        :returns: The original message (plaintext).
        :rtype: bytes

        :raises ValueError:
            if the ciphertext has the wrong length, or if decryption
            fails the integrity check (in which case, the decryption
            key is probably wrong).
        :raises TypeError:
            if the RSA key has no private half (i.e. you are trying
            to decrypt using a public key).
        r#   r$   z!Ciphertext with incorrect length.r   r'   Nr&   zIncorrect decryption.)r   r(   r)   r*   r   r+   r   r   r,   r-   r.   r   Z_decryptr   r/   r   r0   r   r   Úfindr   Úint)r   Z
ciphertextr2   r3   r4   Zct_intr<   r;   r5   r   r:   r8   r9   Úseedr7   r6   Zone_posZlHash1ÚinvalidZhash_comparer   r   r   r   ÚdecryptŽ   s6   




zPKCS1OAEP_Cipher.decryptN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r"   r>   rC   r   r   r   r   r
       s    (6r
   ó    c                 C   s   |du rt j}t| ||||ƒS )a£  Return a cipher object :class:`PKCS1OAEP_Cipher` that can be used to perform PKCS#1 OAEP encryption or decryption.

    :param key:
      The key object to use to encrypt or decrypt the message.
      Decryption is only possible with a private RSA key.
    :type key: RSA key object

    :param hashAlgo:
      The hash function to use. This can be a module under `Cryptodome.Hash`
      or an existing hash object created from any of such modules.
      If not specified, `Cryptodome.Hash.SHA1` is used.
    :type hashAlgo: hash object

    :param mgfunc:
      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 consistent with ``hashAlgo`` is used (a safe choice).
    :type mgfunc: callable

    :param label:
      A label to apply to this particular encryption. If not specified,
      an empty string is used. Specifying a label does not improve
      security.
    :type label: bytes/bytearray/memoryview

    :param randfunc:
      A function that returns random bytes.
      The default is `Random.get_random_bytes`.
    :type randfunc: callable
    N)r	   Zget_random_bytesr
   )r   r   r   r   r   r   r   r   r/   Ì   s    r/   )NNrH   N)ZCryptodome.Signature.pssr   ZCryptodome.Hash.SHA1r   ZCryptodome.Util.py3compatr   r   ZCryptodome.Util.numberr   r   r   ZCryptodome.Util.strxorr   r	   r
   r/   r   r   r   r   Ú<module>   s    -