B cy$@sndZdZddgZddlZddlZddlTddlZddlmZddl m Z Gd ddZ dde d fd dZ dS) a{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. z$Id$newPKCS1OAEP_CipherN)*)ceil_div)strxorc@s8eZdZdZddZddZddZdd Zd d Zd S) rzBThis cipher can perform PKCS#1 v1.5 OAEP encryption or decryption.cs@|_|r|_n tjj_|r(|_nfdd_|_dS)aInitialize 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. cstjj||jS)N)CryptoZ SignatureZ PKCS1_PSSZMGF1_hashObj)xy)selfK/opt/alt/python37/lib64/python3.7/site-packages/Crypto/Cipher/PKCS1_OAEP.pyez+PKCS1OAEP_Cipher.__init__..N)_keyrrZHashZSHA_mgf_label)r keyhashAlgomgfunclabelr )r r __init__Ds zPKCS1OAEP_Cipher.__init__cCs |jS)z?Return True/1 if this cipher object can be used for encryption.)r can_encrypt)r r r r riszPKCS1OAEP_Cipher.can_encryptcCs |jS)z?Return True/1 if this cipher object can be used for decryption.)r can_decrypt)r r r r rmszPKCS1OAEP_Cipher.can_decryptcCs|jj}tjj|jj}t|d}|jj }t |}||d|d}|dkrXt d|j |j }td|} || td|} ||} || ||d} t| | } || |}t| |}td|| }|j|dd}td|t ||}|S)aProduce 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. rzPlaintext is too long.)rZ _randfuncrUtilnumbersizenrr digest_sizelen ValueErrorrrdigestbchrrrencrypt)r messageZrandFuncmodBitskhLenZmLenZps_lenlHashZpsdbZrosdbMaskmaskedDBseedMask maskedSeedemmcr r r r&qs(     zPKCS1OAEP_Cipher.encryptcCsLtjj|jj}t|d}|jj}t ||ks<||dkrDt d|j |}t d|t ||}|j |j}|d}|d|d} ||dd} || |} t| | } || ||d} t| | }d}||dt d}|d|}||kr d}|dkrd}t|dkr*d}|s8t d|||ddS)aDecrypt 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. rrz!Ciphertext with incorrect length.rrNzIncorrect decryption.)rrrrrr rrr!r"r#decryptr%rrr$rrfindZbord)r Zctr(r)r*r2r1r+r r0r.r/Zseedr-r,ZvalidZoneZlHash1r r r r4s6        zPKCS1OAEP_Cipher.decryptN) __name__ __module__ __qualname____doc__rrrr&r4r r r r rAs %8cCst||||S)aMReturn 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)rrrrr r r rs)r9Z __revision____all__ZCrypto.Signature.PKCS1_PSSrZCrypto.Hash.SHAZCrypto.Util.py3compatZCrypto.Util.numberrZCrypto.Util.strxorrrbrr r r r 2s  '