Current File : //usr/lib64/python2.7/site-packages/Crypto/Cipher/PKCS1_OAEP.pyc
�
Bd\Rc@s�dZddlmZdZddgZddlZddlZddlTddl	Zddl	m
Z
dd	lmZdd
d
��YZ
dded�d�ZdS(s{RSA encryption protocol according to PKCS#1 OAEP

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

This scheme is more properly called ``RSAES-OAEP``.

As an example, a sender may encrypt a message in this way:

        >>> from Crypto.Cipher import PKCS1_OAEP
        >>> from Crypto.PublicKey import RSA
        >>>
        >>> message = 'To be encrypted'
        >>> key = RSA.importKey(open('pubkey.der').read())
        >>> cipher = PKCS1_OAEP.new(key)
        >>> ciphertext = cipher.encrypt(message)

At the receiver side, decryption can be done using the private part of
the RSA key:

        >>> key = RSA.importKey(open('privkey.der').read())
        >>> cipher = PKCS1_OAP.new(key)
        >>> message = cipher.decrypt(ciphertext)

:undocumented: __revision__, __package__

.. __: http://www.ietf.org/rfc/rfc3447.txt
.. __: http://www.rsa.com/rsalabs/node.asp?id=2125.
i����(t
nested_scopess$Id$tnewtPKCS1OAEP_CipherN(t*(tceil_div(tstrxorcBs;eZdZd�Zd�Zd�Zd�Zd�ZRS(sBThis cipher can perform PKCS#1 v1.5 OAEP encryption or decryption.cs[|�_|r|�_ntjj�_|r<|�_n�fd��_|�_dS(s�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 `Crypto.Hash`
                or an existing hash object created from any of such modules. If not specified,
                `Crypto.Hash.SHA` (that is, SHA-1) 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 is used (a safe choice).
         label : string
                A label to apply to this particular encryption. If not specified,
                an empty string is used. Specifying a label does not improve
                security.
 
        :attention: Modify the mask generation function only if you know what you are doing.
                    Sender and receiver must use the same one.
        cstjjj||�j�S(N(tCryptot	Signaturet	PKCS1_PSStMGF1t_hashObj(txty(tself(s>/usr/lib64/python2.7/site-packages/Crypto/Cipher/PKCS1_OAEP.pyt<lambda>esN(t_keyR
RtHashtSHAt_mgft_label(R
tkeythashAlgotmgfunctlabel((R
s>/usr/lib64/python2.7/site-packages/Crypto/Cipher/PKCS1_OAEP.pyt__init__Ds	cCs
|jj�S(s?Return True/1 if this cipher object can be used for encryption.(Rtcan_encrypt(R
((s>/usr/lib64/python2.7/site-packages/Crypto/Cipher/PKCS1_OAEP.pyRiscCs
|jj�S(s?Return True/1 if this cipher object can be used for decryption.(Rtcan_decrypt(R
((s>/usr/lib64/python2.7/site-packages/Crypto/Cipher/PKCS1_OAEP.pyRmscCsg|jj}tjjj|jj�}t|d�}|jj	}t
|�}||d|d}|dkrtd��n|jj|j
�j�}td�|}	||	td�|}
||�}|j|||d�}t|
|�}
|j|
|�}t||�}td�||
}|jj|d�d}td�|t
|�|}|S(s�Produce the PKCS#1 OAEP encryption of a message.
    
        This function is named ``RSAES-OAEP-ENCRYPT``, and is specified in
        section 7.1.1 of RFC3447.
    
        :Parameters:
         message : string
                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.
   
        :Return: A string, the ciphertext in which the message is encrypted.
            It is as long as the RSA modulus (in bytes).
        :Raise ValueError:
            If the RSA key length is not sufficiently long to deal with the given
            message.
        iiisPlaintext is too long.i(Rt	_randfuncRtUtiltnumbertsizetnRR
tdigest_sizetlent
ValueErrorRRtdigesttbchrRRtencrypt(R
tmessagetrandFunctmodBitstkthLentmLentps_lentlHashtpstdbtrostdbMasktmaskedDBtseedMaskt
maskedSeedtemtmtc((s>/usr/lib64/python2.7/site-packages/Crypto/Cipher/PKCS1_OAEP.pyR%qs(cCs�tjjj|jj�}t|d�}|jj}t	|�|ksX||dkrgt
d��n|jj|�}td�|t	|�|}|jj
|j�j�}|d}|d|d!}	||d}
|j|
|�}t|	|�}|j|||d�}
t|
|
�}d}||jtd��}|| }||krcd}n|dkrxd}nt|�dkr�d}n|s�t
d��n|||dS(sDecrypt a PKCS#1 OAEP ciphertext.
    
        This function is named ``RSAES-OAEP-DECRYPT``, and is specified in
        section 7.1.2 of RFC3447.
    
        :Parameters:
         ct : string
                The ciphertext that contains the message to recover.
   
        :Return: A string, the original message.
        :Raise ValueError:
            If the ciphertext length is incorrect, or if the decryption does not
            succeed.
        :Raise TypeError:
            If the RSA key has no private half.
        iis!Ciphertext with incorrect length.iisIncorrect decryption.(RRRRRRRR
R R!R"tdecryptR$RRR#RRtfindtbord(R
tctR(R)R*R6R5R-RR4R2R3tseedR1R/tvalidtonetlHash1((s>/usr/lib64/python2.7/site-packages/Crypto/Cipher/PKCS1_OAEP.pyR8�s6"

			(t__name__t
__module__t__doc__RRRR%R8(((s>/usr/lib64/python2.7/site-packages/Crypto/Cipher/PKCS1_OAEP.pyRAs	%			8tcCst||||�S(sMReturn a cipher object `PKCS1OAEP_Cipher` that can be used to perform PKCS#1 OAEP encryption or decryption.

    :Parameters:
     key : RSA key object
      The key to use to encrypt or decrypt the message. This is a `Crypto.PublicKey.RSA` object.
      Decryption is only possible if *key* is a private RSA key.
     hashAlgo : hash object
      The hash function to use. This can be a module under `Crypto.Hash`
      or an existing hash object created from any of such modules. If not specified,
      `Crypto.Hash.SHA` (that is, SHA-1) 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 is used (a safe choice).
     label : string
      A label to apply to this particular encryption. If not specified,
      an empty string is used. Specifying a label does not improve
      security.
 
    :attention: Modify the mask generation function only if you know what you are doing.
      Sender and receiver must use the same one.
    (R(RRRR((s>/usr/lib64/python2.7/site-packages/Crypto/Cipher/PKCS1_OAEP.pyR�s((RBt
__future__Rt__revision__t__all__tCrypto.Signature.PKCS1_PSSRtCrypto.Hash.SHAtCrypto.Util.py3compattCrypto.Util.numberRtCrypto.Util.strxorRRtNonetbR(((s>/usr/lib64/python2.7/site-packages/Crypto/Cipher/PKCS1_OAEP.pyt<module>2s
�