o )%a.@sddlZddlZddlmZddlmZddlmZmZddl m Z ddl m Z ddl mZmZddlmZdd lmZmZmZGd d d ejd ZeZGd ddejd ZeZ d4dededefddZdededdfddZdededededededededdfddZded eddfd!d"Zded#edefd$d%Z dededefd&d'Z!dededefd(d)Z"dededefd*d+Z#d,Z$d eded-edej%eeffd.d/Z&Gd0d1d1e'Z(Gd2d3d3e'Z)dS)5N)gcd)utils)UnsupportedAlgorithm_Reasons) _get_backend) RSABackend)_serializationhashes)AsymmetricPadding)AsymmetricSignatureContextAsymmetricVerificationContextrc @seZdZejdedejdefddZ ejde dede fddZ ej de fd d Zejdd d Zejde dedejejejfde fddZejdddZejdejdejdejde fddZdS) RSAPrivateKeypadding algorithmreturncCdS)zN Returns an AsymmetricSignatureContext used for signing data. N)selfrrrrO/usr/lib/python3/dist-packages/cryptography/hazmat/primitives/asymmetric/rsa.pysignerzRSAPrivateKey.signer ciphertextcCr)z3 Decrypts the provided ciphertext. Nr)rrrrrrdecrypt rzRSAPrivateKey.decryptcCrz7 The bit length of the public modulus. Nrrrrrkey_size&rzRSAPrivateKey.key_size RSAPublicKeycCr)zD The RSAPublicKey associated with this private key. Nrrrrr public_key,rzRSAPrivateKey.public_keydatacCr)z! Signs the data. Nr)rrrrrrrsign2rzRSAPrivateKey.signRSAPrivateNumberscCr)z/ Returns an RSAPrivateNumbers. Nrrrrrprivate_numbers=rzRSAPrivateKey.private_numbersencodingformatencryption_algorithmcCrz6 Returns the key serialized as bytes. Nr)rr"r#r$rrr private_bytesCrzRSAPrivateKey.private_bytesN)rr)rr )__name__ __module__ __qualname__abcabstractmethodr r HashAlgorithmr rbytesrabstractpropertyintrrtypingUnion asym_utils Prehashedrr!rEncoding PrivateFormatKeySerializationEncryptionr&rrrrr sJ    r ) metaclassc @seZdZejdededejde fddZ ejdededefdd Z ej de fd d Zejdd dZejdejdejdefddZejdedededejejejfddf ddZejdededejejdefddZdS)r signaturerrrcCr)zY Returns an AsymmetricVerificationContext used for verifying signatures. Nrrr8rrrrrverifierSrzRSAPublicKey.verifier plaintextcCr)z/ Encrypts the given plaintext. Nr)rr;rrrrencrypt^rzRSAPublicKey.encryptcCrrrrrrrrdrzRSAPublicKey.key_sizeRSAPublicNumberscCr)z- Returns an RSAPublicNumbers Nrrrrrpublic_numbersjrzRSAPublicKey.public_numbersr"r#cCrr%r)rr"r#rrr public_bytesprzRSAPublicKey.public_bytesrNcCr)z5 Verifies the signature of the data. Nr)rr8rrrrrrverifyzrzRSAPublicKey.verifycCr)z@ Recovers the original data from the signature. Nrr9rrrrecover_data_from_signaturerz(RSAPublicKey.recover_data_from_signature)rr=)r'r(r)r*r+r-r r r,r r:r<r.r/rr>rr4 PublicFormatr?r0r1r2r3r@OptionalrArrrrrRs^        rpublic_exponentrrcCs4t|}t|tstdtjt|||||S)Nz-Backend object does not implement RSABackend.)r isinstancerrrBACKEND_MISSING_INTERFACE_verify_rsa_parametersgenerate_rsa_private_key)rDrbackendrrrgenerate_private_keys   rJcCs$|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)rDrrrrrGsrGpqprivate_exponentdmp1dmq1iqmpmoduluscCs|dkrtd||krtd||krtd||kr td||kr(td||kr0td||kr8td|dks@||krDtd |d @d krNtd |d @d krXtd |d @d krbtd|||krltddS)NrKzmodulus 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.rL)rNrOrPrQrRrSrDrTrrr_check_private_key_componentss2     rVencCs@|dkrtd|dks||krtd|d@dkrtddS)NrKzn must be >= 3.ze must be >= 3 and < n.rUrze must be odd.rL)rWrXrrr_check_public_key_componentss rYmc CsXd\}}||}}|dkr(t||\}}|||}||||f\}}}}|dks ||S)zO Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1 )rUrr)divmod) rWrZx1x2abrOrxnrrr_modinvs  rbcCs t||S)zF Compute the CRT (q ** -1) % p value from RSA primes p and q. )rb)rNrOrrr rsa_crt_iqmps rccC ||dS)zg Compute the CRT private_exponent % (p - 1) value from the RSA private_exponent (d) and p. rUr)rPrNrrr rsa_crt_dmp1 recCrd)zg Compute the CRT private_exponent % (q - 1) value from the RSA private_exponent (d) and q. rUr)rPrOrrr rsa_crt_dmq1rfrgidc Cs||d}|}|ddkr|d}|ddksd}d}|s\|tkr\|}||krRt|||}|dkrJ||dkrJt|d|dkrJt|d|} d}n|d9}||ks(|d7}|s\|tks"|sbtdt|| \} } | dksoJt| | fdd\} } | | 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. rUrFTz2Unable to compute factors p and q from exponent d.)reverse)_MAX_RECOVERY_ATTEMPTSpowrrMr[sorted) rXrWrhktottspottedr^kcandrNrOr`rrrrsa_recover_prime_factorss2     $  rsc@seZdZdededededededdfd d Zed Zed Zed Z edZ edZ edZ edZ ddefddZddZddZddZdS)r rNrOrhrQrRrSr>r=cCst|trt|trt|trt|trt|trt|ts"tdt|ts+td||_||_||_||_||_||_ ||_ dS)NzNRSAPrivateNumbers p, q, d, dmp1, dmq1, iqmp arguments must all be an integers.zFRSAPrivateNumbers public_numbers must be an RSAPublicNumbers instance.) rEr/ TypeErrorr=_p_q_d_dmp1_dmq1_iqmp_public_numbers)rrNrOrhrQrRrSr>rrr__init__@s4   zRSAPrivateNumbers.__init__rurvrwrxryrzr{NrcCt|}||SN)rload_rsa_private_numbersrrIrrr private_keym zRSAPrivateNumbers.private_keycCsbt|tstS|j|jko0|j|jko0|j|jko0|j|jko0|j|jko0|j|jko0|j |j kSr~) rEr NotImplementedrNrOrhrQrRrSr>rotherrrr__eq__qs        zRSAPrivateNumbers.__eq__cC ||k Sr~rrrrr__ne__ zRSAPrivateNumbers.__ne__cCs$t|j|j|j|j|j|j|jfSr~)hashrNrOrhrQrRrSr>rrrr__hash__szRSAPrivateNumbers.__hash__r~)r'r(r)r/r|rread_only_propertyrNrOrhrQrRrSr>r rrrrrrrrr ?s6 %       r c@sbeZdZdedefddZedZedZdde fd d Z d d Z d dZ ddZ ddZdS)r=rWrXcCs,t|tr t|tstd||_||_dS)Nz,RSAPublicNumbers arguments must be integers.)rEr/rt_e_n)rrWrXrrrr|s zRSAPublicNumbers.__init__rrNrcCr}r~)rload_rsa_public_numbersrrrrrrzRSAPublicNumbers.public_keycCs d|S)Nz$)r#rrrr__repr__rzRSAPublicNumbers.__repr__cCs&t|tstS|j|jko|j|jkSr~)rEr=rrWrXrrrrrs zRSAPublicNumbers.__eq__cCrr~rrrrrrrzRSAPublicNumbers.__ne__cCst|j|jfSr~)rrWrXrrrrrszRSAPublicNumbers.__hash__r~)r'r(r)r/r|rrrWrXrrrrrrrrrrr=s   r=r~)*r*r0mathr cryptographyrcryptography.exceptionsrrcryptography.hazmat.backendsr'cryptography.hazmat.backends.interfacesrcryptography.hazmat.primitivesrr *cryptography.hazmat.primitives._asymmetricr )cryptography.hazmat.primitives.asymmetricr r r2ABCMetar RSAPrivateKeyWithSerializationrRSAPublicKeyWithSerializationr/rJrGrVrYrbrcrergrkTuplersobjectr r=rrrrsv     8@   /     -Q