pg:,ddlZddlZddlmZddlmZmZddlmZddl m Z Gddej Z e ZGd d ej ZeZ d(d ed ed ejde fdZd ed eddfdZdedededededed ededdfdZdededdfdZdededefdZdededefdZdededefdZdededefd Zd!Zdeded"edejeeffd#ZGd$d%ZGd&d'ZdS))N)gcd)_serializationhashes)AsymmetricPadding)utilsc VeZdZejdededefdZejde fdZ ejddZ ejdeded e j ejejfdefd Zejdd Zejd ejdejdejdefdZdS) RSAPrivateKey ciphertextpaddingreturncdS)z3 Decrypts the provided ciphertext. N)selfr r s O/usr/lib/python3/dist-packages/cryptography/hazmat/primitives/asymmetric/rsa.pydecryptzRSAPrivateKey.decryptcdSz7 The bit length of the public modulus. Nrrs rkey_sizezRSAPrivateKey.key_sizerr RSAPublicKeycdS)zD The RSAPublicKey associated with this private key. Nrrs r public_keyzRSAPrivateKey.public_keyrrdata algorithmcdS)z! Signs the data. Nr)rrr rs rsignzRSAPrivateKey.sign$rrRSAPrivateNumberscdS)z/ Returns an RSAPrivateNumbers. Nrrs rprivate_numberszRSAPrivateKey.private_numbers/rrencodingformatencryption_algorithmcdSz6 Returns the key serialized as bytes. Nr)rr"r#r$s r private_byteszRSAPrivateKey.private_bytes5rrN)r r)r r)__name__ __module__ __qualname__abcabstractmethodbytesrrabstractpropertyintrrtypingUnion asym_utils Prehashedr HashAlgorithmrr!rEncoding PrivateFormatKeySerializationEncryptionr'rrrr r sn % 2C       #            # < 4f6J JK               ) , -G         rr ) metaclassc |eZdZejdededefdZejde fdZ ejddZ ejde j d e jdefd Zejd ed eded ejejejfddf dZejd eded ejejdefdZdS)r plaintextr r cdS)z/ Encrypts the given plaintext. Nr)rr:r s rencryptzRSAPublicKey.encryptErrcdSrrrs rrzRSAPublicKey.key_sizeKrrRSAPublicNumberscdS)z- Returns an RSAPublicNumbers Nrrs rpublic_numberszRSAPublicKey.public_numbersQrrr"r#cdSr&r)rr"r#s r public_byteszRSAPublicKey.public_bytesWrr signaturerrNcdS)z5 Verifies the signature of the data. Nr)rrCrr rs rverifyzRSAPublicKey.verifyarrcdS)z@ Recovers the original data from the signature. Nr)rrCr rs rrecover_data_from_signaturez(RSAPublicKey.recover_data_from_signaturemrrr r>)r(r)r*r+r,r-rr<r.r/rr@rr5 PublicFormatrBr0r1r2r3rr4rEOptionalrGrrrrrDs  1B u      #            ) +              #  < 4f6J JK           # ?6#78         rrpublic_exponentrbackendr cZddlm}t|||||SNr)rL),cryptography.hazmat.backends.openssl.backendrL_verify_rsa_parametersgenerate_rsa_private_key)rKrrLossls rgenerate_private_keyrS|s= MLLLLL?H555  ( (( C CCrcV|dvrtd|dkrtddS)N)izopublic_exponent must be either 3 (for legacy compatibility) or 65537. Almost everyone should choose 65537 here!iz#key_size must be at least 512-bits. ValueError)rKrs rrPrPsEj(( ?   #~~>???~rpqprivate_exponentdmp1dmq1iqmpmodulusc"|dkrtd||krtd||krtd||krtd||krtd||krtd||krtd|dks||krtd |d zd krtd |d zd krtd |d zd krtd||z|krtddS)NrUzmodulus must be >= 3.zp must be < modulus.zq must be < modulus.zdmp1 must be < modulus.zdmq1 must be < modulus.ziqmp must be < modulus.z#private_exponent must be < modulus.z+public_exponent must be >= 3 and < modulus.rzpublic_exponent must be odd.zdmp1 must be odd.zdmq1 must be odd.zp*q must equal modulus.rV)rXrYrZr[r\r]rKr^s r_check_private_key_componentsrasO{{0111G||/000G||/000 w2333 w2333 w23337"">???o88FGGGa7888 ax1}},--- ax1}},---1u2333renc|dkrtd|dks||krtd|dzdkrtddS)NrUzn must be >= 3.ze must be >= 3 and < n.r`rze must be odd.rV)rbrcs r_check_public_key_componentsres]1uu*+++1uuQ23331uzz)***zrmcd\}}||}}|dkr,t||\}}|||zz }||||f\}}}}|dk,||zS)zO Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1 )r`rr)divmod) rbrfx1x2abrYrxns r_modinvroshFB aqA a%%a||1 !b&[!R| 1b" a%% 6Mrc"t||S)zF Compute the CRT (q ** -1) % p value from RSA primes p and q. )ro)rXrYs r rsa_crt_iqmprqs 1a==rc||dz zS)zg Compute the CRT private_exponent % (p - 1) value from the RSA private_exponent (d) and p. r`r)rZrXs r rsa_crt_dmp1rs q1u %%rc||dz zS)zg Compute the CRT private_exponent % (q - 1) value from the RSA private_exponent (d) and q. r`r)rZrYs r rsa_crt_dmq1rvrtridc||zdz }|}|dzdkr|dz}|dzdkd}d}|s{|tkrp|}||krVt|||}|dkr4||dz kr+t|d|dkrt|dz|} d}n |dz}||kV|dz }|s |tkp|stdt || \} } | dksJt | | fd\} } | | fS)z Compute factors p and q from the private exponent d. We assume that n has no more than two factors. This function is adapted from code in PyCrypto. r`rFTz2Unable to compute factors p and q from exponent d.)reverse)_MAX_RECOVERY_ATTEMPTSpowrrWrhsorted) rcrbrwktottspottedrkkcandrXrYrms rrsa_recover_prime_factorsrsN q519D A a%1** F a%1**G A!444 $hhq!Q<ct|trit|trTt|tr?t|tr*t|trt|tstdt|tstd||_||_||_||_||_||_ ||_ dS)NzNRSAPrivateNumbers p, q, d, dmp1, dmq1, iqmp arguments must all be an integers.zFRSAPrivateNumbers public_numbers must be an RSAPublicNumbers instance.) isinstancer/ TypeErrorr>_p_q_d_dmp1_dmq1_iqmp_public_numbers)rrXrYrwr[r\r]r@s r__init__zRSAPrivateNumbers.__init__$s1c"" a%% a%% dC((  dC((  dC(( '  .*:;;      -rr c|jSN)rrs rrXzRSAPrivateNumbers.pI wrc|jSr)rrs rrYzRSAPrivateNumbers.qMrrc|jSr)rrs rrwzRSAPrivateNumbers.dQrrc|jSr)rrs rr[zRSAPrivateNumbers.dmp1U zrc|jSr)rrs rr\zRSAPrivateNumbers.dmq1Yrrc|jSr)rrs rr]zRSAPrivateNumbers.iqmp]rrc|jSr)rrs rr@z RSAPrivateNumbers.public_numbersas ##rNrLc8ddlm}||SrN)rOrLload_rsa_private_numbersrrLrRs r private_keyzRSAPrivateNumbers.private_keyes6      ,,T222rotherct|tstS|j|jko_|j|jkoO|j|jko?|j|jko/|j|jko|j|jko|j |j kSr) rrNotImplementedrXrYrwr[r\r]r@rrs r__eq__zRSAPrivateNumbers.__eq__ls%!233 "! ! Feg  <%'! <%'! < UZ' < UZ'  <  UZ'  < #u';; rc tt|j|j|j|j|j|j|jfSr)hashrXrYrwr[r\r]r@rs r__hash__zRSAPrivateNumbers.__hash__zs=   #    rrHr)r(r)r*r/rpropertyrXrYrwr[r\r]r@r0Anyr robjectboolrrrrrrr#s#. #. #.  #.  #.  #.#.+#.#.#.#.J3X3X3XcXcXcX$$$X$336:33333  F  t       #       rrceZdZdedefdZedefdZedefdZddej de fd Z de fd Z d edefd Zdefd ZdS)r>rbrcct|trt|tstd||_||_dS)Nz,RSAPublicNumbers arguments must be integers.)rr/r_e_n)rrbrcs rrzRSAPublicNumbers.__init__sH!S!! LAs);); LJKK Krr c|jSr)rrs rrbzRSAPublicNumbers.errc|jSr)rrs rrczRSAPublicNumbers.nrrNrLc8ddlm}||SrN)rOrLload_rsa_public_numbersrs rrzRSAPublicNumbers.public_keys6      ++D111rc,d|S)Nz$)r#rs r__repr__zRSAPublicNumbers.__repr__s5<rrbrcrs rrzRSAPublicNumbers.__eq__s8%!122 "! !v 6TVuw%66rc8t|j|jfSr)rrbrcrs rrzRSAPublicNumbers.__hash__sTVTV$%%%rr)r(r)r*r/rrrbrcr0rrrstrrrrrrrrrr>r>s ##3X3X22&*2 2222C#CCCC7F7t7777 &#&&&&&&rr>r) r+r0mathrcryptography.hazmat.primitivesrr*cryptography.hazmat.primitives._asymmetricr)cryptography.hazmat.primitives.asymmetricrr2ABCMetar RSAPrivateKeyWithSerializationrRSAPublicKeyWithSerializationr/rrSrPrarerorqrsrvr{Tuplerrr>rrrrsG  AAAAAAAAHHHHHH - - - - - ck- - - - `"/2 2 2 2 2 S[2 2 2 2 j!- DDDDZD DDDD@C@3@4@@@@,4 ,4 ,4,4  ,4  ,4  ,4,4,4 ,4,4,4,4^+C+C+D++++ s s s    CCC&3&3&3&&&&&3&3&3&&&&* *** \#s(****Zb b b b b b b b J!&!&!&!&!&!&!&!&!&!&r