ó |~úVc@sìdZddlZddlZddlmZddlmZmZmZiedƒd6edƒd6ed ƒd 6ed ƒd 6ed ƒd6Z iej d6ej d6ej d 6ej d 6ejd6Zdefd„ƒYZdefd„ƒYZdefd„ƒYZd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zdddd dddgZed!krèd"GHddlZxRed#ƒD]DZ ej!ƒ\Z"Z#e"rºPne r˜e d$d%kr˜d&e GHq˜q˜Wd'GHndS((sbFunctions for PKCS#1 version 1.5 encryption and signing This module implements certain functionality from PKCS#1 version 1.5. For a very clear example, read http://www.di-mgt.com.au/rsa_alg.html#pkcs1schemes At least 8 bytes of random padding is used when encrypting a message. This makes these methods much more secure than the ones in the ``rsa`` module. WARNING: this module leaks information when decryption fails. The exceptions that are raised contain the Python traceback information, which can be used to deduce where in the process the failure occurred. DO NOT PASS SUCH INFORMATION to your users. iÿÿÿÿN(tb(tcommont transformtcores0 0 *†H†÷ tMD5s0!0 +sSHA-1s010  `†He sSHA-256s0A0  `†He0sSHA-384s0Q0  `†He@sSHA-512t CryptoErrorcBseZdZRS(s-Base class for all exceptions in this module.(t__name__t __module__t__doc__(((s4/opt/awscli/lib/python2.7/site-packages/rsa/pkcs1.pyR7stDecryptionErrorcBseZdZRS(sRaised when decryption fails.(RRR(((s4/opt/awscli/lib/python2.7/site-packages/rsa/pkcs1.pyR ;stVerificationErrorcBseZdZRS(sRaised when verification fails.(RRR(((s4/opt/awscli/lib/python2.7/site-packages/rsa/pkcs1.pyR ?scCs|d}t|ƒ}||kr;td||fƒ‚ntdƒ}||d}xet|ƒ|kr¼|t|ƒ}tj|dƒ}|jtdƒtdƒƒ}||| }qXWt|ƒ|ksÕt‚tdƒjtdƒ|tdƒ|gƒS(sPads the message for encryption, returning the padded message. :return: 00 02 RANDOM_DATA 00 MESSAGE >>> block = _pad_for_encryption(b'hello', 16) >>> len(block) 16 >>> block[0:2] b'\x00\x02' >>> block[-6:] b'\x00hello' i s;%i bytes needed for message, but there is only space for %itiiss(tlent OverflowErrorRtosturandomtreplacetAssertionErrortjoin(tmessaget target_lengtht max_msglengtht msglengthtpaddingtpadding_lengtht needed_bytest new_padding((s4/opt/awscli/lib/python2.7/site-packages/rsa/pkcs1.pyt_pad_for_encryptionCs"     cCs~|d}t|ƒ}||kr;td||fƒ‚n||d}tdƒjtdƒ|tdƒtdƒ|gƒS(sjPads the message for signing, returning the padded message. The padding is always a repetition of FF bytes. :return: 00 01 PADDING 00 MESSAGE >>> block = _pad_for_signing(b'hello', 16) >>> len(block) 16 >>> block[0:2] b'\x00\x01' >>> block[-6:] b'\x00hello' >>> block[2:-6] b'\xff\xff\xff\xff\xff\xff\xff\xff' i s;%i bytes needed for message, but there is only space for %iiR ssÿs(R R RR(RRRRR((s4/opt/awscli/lib/python2.7/site-packages/rsa/pkcs1.pyt_pad_for_signingqs     cCsatj|jƒ}t||ƒ}tj|ƒ}tj||j|jƒ}tj ||ƒ}|S(sÂEncrypts the given message using PKCS#1 v1.5 :param message: the message to encrypt. Must be a byte string no longer than ``k-11`` bytes, where ``k`` is the number of bytes needed to encode the ``n`` component of the public key. :param pub_key: the :py:class:`rsa.PublicKey` to encrypt with. :raise OverflowError: when the message is too large to fit in the padded block. >>> from rsa import key, common >>> (pub_key, priv_key) = key.newkeys(256) >>> message = b'hello' >>> crypto = encrypt(message, pub_key) The crypto text should be just as long as the public key 'n' component: >>> len(crypto) == common.byte_size(pub_key.n) True ( Rt byte_sizetnRRt bytes2intRt encrypt_inttet int2bytes(Rtpub_keyt keylengthtpaddedtpayloadt encryptedtblock((s4/opt/awscli/lib/python2.7/site-packages/rsa/pkcs1.pytencrypt“s cCs²tj|jƒ}tj|ƒ}|j|ƒ}tj||ƒ}|dd!tdƒkrjtdƒ‚ny|j tdƒdƒ}Wnt k r¥tdƒ‚nX||dS(saDecrypts the given message using PKCS#1 v1.5 The decryption is considered 'failed' when the resulting cleartext doesn't start with the bytes 00 02, or when the 00 byte between the padding and the message cannot be found. :param crypto: the crypto text as returned by :py:func:`rsa.encrypt` :param priv_key: the :py:class:`rsa.PrivateKey` to decrypt with. :raise DecryptionError: when the decryption fails. No details are given as to why the code thinks the decryption fails, as this would leak information about the private key. >>> import rsa >>> (pub_key, priv_key) = rsa.newkeys(256) It works with strings: >>> crypto = encrypt(b'hello', pub_key) >>> decrypt(crypto, priv_key) b'hello' And with binary data: >>> crypto = encrypt(b'\x00\x00\x00\x00\x01', pub_key) >>> decrypt(crypto, priv_key) b'\x00\x00\x00\x00\x01' Altering the encrypted information will *likely* cause a :py:class:`rsa.pkcs1.DecryptionError`. If you want to be *sure*, use :py:func:`rsa.sign`. .. warning:: Never display the stack trace of a :py:class:`rsa.pkcs1.DecryptionError` exception. It shows where in the code the exception occurred, and thus leaks information about the key. It's only a tiny bit of information, but every bit makes cracking the keys easier. >>> crypto = encrypt(b'hello', pub_key) >>> crypto = crypto[0:5] + b'X' + crypto[6:] # change a byte >>> decrypt(crypto, priv_key) Traceback (most recent call last): ... rsa.pkcs1.DecryptionError: Decryption failed iissDecryption failedsi( RRRRRtblinded_decryptR"RR tindext ValueError(tcryptotpriv_keyt blocksizeR't decryptedt cleartexttsep_idx((s4/opt/awscli/lib/python2.7/site-packages/rsa/pkcs1.pytdecrypt³s3 c Cs—|tkrtd|ƒ‚nt|}t||ƒ}||}tj|jƒ}t||ƒ}tj|ƒ}|j |ƒ}tj ||ƒ} | S(s¼Signs the message with the private key. Hashes the message, then signs the hash with the given key. This is known as a "detached signature", because the message itself isn't altered. :param message: the message to sign. Can be an 8-bit string or a file-like object. If ``message`` has a ``read()`` method, it is assumed to be a file-like object. :param priv_key: the :py:class:`rsa.PrivateKey` to sign with :param hash: the hash method used on the message. Use 'MD5', 'SHA-1', 'SHA-256', 'SHA-384' or 'SHA-512'. :return: a message signature block. :raise OverflowError: if the private key is too small to contain the requested hash. sInvalid hash method: %s( t HASH_ASN1R,t_hashRRRRRRtblinded_encryptR"( RR.thashtasn1codeR1R$R%R&R'R(((s4/opt/awscli/lib/python2.7/site-packages/rsa/pkcs1.pytsignøs   c Cs¥tj|jƒ}tj|ƒ}tj||j|jƒ}tj||ƒ}t |ƒ}t ||ƒ}t ||} t | |ƒ} | |kr¡t dƒ‚ntS(s!Verifies that the signature matches the message. The hash method is detected automatically from the signature. :param message: the signed message. Can be an 8-bit string or a file-like object. If ``message`` has a ``read()`` method, it is assumed to be a file-like object. :param signature: the signature block, as created with :py:func:`rsa.sign`. :param pub_key: the :py:class:`rsa.PublicKey` of the person signing the message. :raise VerificationError: when the signature doesn't match the message. sVerification failed(RRRRRRt decrypt_intR!R"t_find_method_hashR5R4RR tTrue( Rt signatureR#R$R'R0tclearsigt method_namet message_hashR1texpected((s4/opt/awscli/lib/python2.7/site-packages/rsa/pkcs1.pytverifys  cCs§|tkrtd|ƒ‚nt|}|ƒ}t|dƒrt|jdƒrddlm}x7|j|dƒD]}|j|ƒqvWn |j|ƒ|jƒS(s4Returns the message digest. :param message: the signed message. Can be an 8-bit string or a file-like object. If ``message`` has a ``read()`` method, it is assumed to be a file-like object. :param method_name: the hash method, must be a key of :py:const:`HASH_METHODS`. sInvalid hash method: %streadt__call__i(tvarblocki( t HASH_METHODSR,thasattrRCR REtyield_fixedblockstupdatetdigest(RR?tmethodthasherRER(((s4/opt/awscli/lib/python2.7/site-packages/rsa/pkcs1.pyR5@s   ! cCs=x*tjƒD]\}}||kr |Sq Wtdƒ‚dS(s³Finds the hash method. :param clearsig: full padded ASN1 and hash. :return: the used hash method. :raise VerificationFailed: when the hash method cannot be found sVerification failedN(R4titemsR (R>thashnameR8((s4/opt/awscli/lib/python2.7/site-packages/rsa/pkcs1.pyR;_s R)R3R9RBt__main__s'Running doctests 1000x or until failureièidis%i timess Doctests done($RthashlibRt rsa._compatRtrsaRRRR4tmd5tsha1tsha256tsha384tsha512RFt ExceptionRR R RRR)R3R9RBR5R;t__all__Rtdoctesttrangetcountttestmodtfailuresttests(((s4/opt/awscli/lib/python2.7/site-packages/rsa/pkcs1.pytsN            . " E & "