ó Bd\Rc@s¥dZddlmZdZddgZddlZddlZddlTddl Zddl m Z dd l m 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.pytesN(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.(Rt can_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.(Rt can_decrypt(R ((s>/usr/lib64/python2.7/site-packages/Crypto/Cipher/PKCS1_OAEP.pyRmscCsg|jj}tjjj|jjƒ}t|dƒ}|jj }t |ƒ}||d|d}|dkrt dƒ‚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 t digest_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.pyt2s     ¦