3 lá˜_þ(ã @sTddlmZmZmZddlZyddlmZWn ek rLddlmZYnXddl Z ddl m Z ddl m Z mZddlmZddlmZe jejƒGdd „d eƒƒZe jejƒGd d „d eƒƒZe jejƒGd d „d eƒƒZeZd%dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Z dd„Z!dZ"dd „Z#Gd!d"„d"eƒZ$Gd#d$„d$eƒZ%dS)&é)Úabsolute_importÚdivisionÚprint_functionN)Úgcd)Úutils)ÚUnsupportedAlgorithmÚ_Reasons)Ú _get_backend)Ú RSABackendc@sReZdZejdd„ƒZejdd„ƒZejdd„ƒZejdd„ƒZ ejd d „ƒZ d S) Ú RSAPrivateKeycCsdS)zN Returns an AsymmetricSignatureContext used for signing data. N©)ÚselfÚpaddingÚ algorithmr r ú/usr/lib64/python3.6/rsa.pyÚsignerszRSAPrivateKey.signercCsdS)z3 Decrypts the provided ciphertext. Nr )r Z ciphertextrr r rÚdecryptszRSAPrivateKey.decryptcCsdS)z7 The bit length of the public modulus. Nr )r r r rÚkey_size%szRSAPrivateKey.key_sizecCsdS)zD The RSAPublicKey associated with this private key. Nr )r r r rÚ public_key+szRSAPrivateKey.public_keycCsdS)z! Signs the data. Nr )r Údatarrr r rÚsign1szRSAPrivateKey.signN) Ú__name__Ú __module__Ú __qualname__ÚabcÚabstractmethodrrÚabstractpropertyrrrr r r rr s r c@s(eZdZejdd„ƒZejdd„ƒZdS)ÚRSAPrivateKeyWithSerializationcCsdS)z/ Returns an RSAPrivateNumbers. Nr )r r r rÚprivate_numbers:sz.RSAPrivateKeyWithSerialization.private_numberscCsdS)z6 Returns the key serialized as bytes. Nr )r ÚencodingÚformatZencryption_algorithmr r rÚ private_bytes@sz,RSAPrivateKeyWithSerialization.private_bytesN)rrrrrrr!r r r rr8src@s`eZdZejdd„ƒZejdd„ƒZejdd„ƒZejdd„ƒZ ejd d „ƒZ ejd d „ƒZ d S)Ú RSAPublicKeycCsdS)zY Returns an AsymmetricVerificationContext used for verifying signatures. Nr )r Ú signaturerrr r rÚverifierIszRSAPublicKey.verifiercCsdS)z/ Encrypts the given plaintext. Nr )r Z plaintextrr r rÚencryptOszRSAPublicKey.encryptcCsdS)z7 The bit length of the public modulus. Nr )r r r rrUszRSAPublicKey.key_sizecCsdS)z- Returns an RSAPublicNumbers Nr )r r r rÚpublic_numbers[szRSAPublicKey.public_numberscCsdS)z6 Returns the key serialized as bytes. Nr )r rr r r rÚ public_bytesaszRSAPublicKey.public_bytescCsdS)z5 Verifies the signature of the data. Nr )r r#rrrr r rÚverifygszRSAPublicKey.verifyN) rrrrrr$r%rrr&r'r(r r r rr"Gs r"cCs4t|ƒ}t|tƒstdtjƒ‚t||ƒ|j||ƒS)Nz-Backend object does not implement RSABackend.)r Ú isinstancer rrZBACKEND_MISSING_INTERFACEÚ_verify_rsa_parametersZgenerate_rsa_private_key)Úpublic_exponentrÚbackendr r rÚgenerate_private_keyqs  r-cCs$|dkrtdƒ‚|dkr tdƒ‚dS)Néézopublic_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.)r.r/)Ú ValueError)r+rr r rr*}s r*cCsÜ|dkrtdƒ‚||kr tdƒ‚||kr0tdƒ‚||kr@tdƒ‚||krPtdƒ‚||kr`tdƒ‚||krptdƒ‚|dks€||krˆtd ƒ‚|d @d krœtd ƒ‚|d @d kr°td ƒ‚|d @d krÄtdƒ‚|||krØtdƒ‚dS)Nr.zmodulus 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.)r0)ÚpÚqÚprivate_exponentÚdmp1Údmq1Úiqmpr+Úmodulusr r rÚ_check_private_key_componentsˆs0    r9cCs@|dkrtdƒ‚|dks ||kr(tdƒ‚|d@dkr= 3.ze must be >= 3 and < n.r1rze must be odd.)r0)ÚeÚnr r rÚ_check_public_key_components°s  r<c CsVd\}}||}}x:|dkrLt||ƒ\}}|||}||||f\}}}}qW||S)zO Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1 r1r)r1r)Údivmod) r:ÚmZx1Zx2ÚaÚbr3ÚrZxnr r rÚ_modinv»s   rBcCs t||ƒS)zF Compute the CRT (q ** -1) % p value from RSA primes p and q. )rB)r2r3r r rÚ rsa_crt_iqmpÈsrCcCs ||dS)zg Compute the CRT private_exponent % (p - 1) value from the RSA private_exponent (d) and p. r1r )r4r2r r rÚ rsa_crt_dmp1ÏsrDcCs ||dS)zg Compute the CRT private_exponent % (q - 1) value from the RSA private_exponent (d) and q. r1r )r4r3r r rÚ rsa_crt_dmq1×srEièc Csî||d}|}x|ddkr(|d}qWd}d}xx| rª|tkrª|}xX||kržt|||ƒ}|dkr”||dkr”t|d|ƒdkr”t|d|ƒ} d}P|d9}qHW|d7}q4W|s¸tdƒ‚t|| ƒ\} } | dksÒt‚t| | 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. r1érFTz2Unable to compute factors p and q from exponent d.)Úreverse)Ú_MAX_RECOVERY_ATTEMPTSÚpowrr0r=ÚAssertionErrorÚsorted) r;r:ÚdZktotÚtZspottedr?ÚkZcandr2r3rAr r rÚrsa_recover_prime_factorsås,    $   rOc@s|eZdZdd„ZejdƒZejdƒZejdƒZejdƒZ ejdƒZ ejdƒZ ejd ƒZ dd d „Z d d„Zdd„Zdd„Zd S)ÚRSAPrivateNumberscCsœt|tjƒ sTt|tjƒ sTt|tjƒ sTt|tjƒ sTt|tjƒ sTt|tjƒ r\tdƒ‚t|tƒsntdƒ‚||_||_||_||_||_ ||_ ||_ dS)NzNRSAPrivateNumbers p, q, d, dmp1, dmq1, iqmp arguments must all be an integers.zFRSAPrivateNumbers public_numbers must be an RSAPublicNumbers instance.) r)ÚsixÚ integer_typesÚ TypeErrorÚRSAPublicNumbersÚ_pÚ_qÚ_dÚ_dmp1Ú_dmq1Ú_iqmpÚ_public_numbers)r r2r3rLr5r6r7r&r r rÚ__init__s$ zRSAPrivateNumbers.__init__rUrVrWrXrYrZr[NcCst|ƒ}|j|ƒS)N)r Zload_rsa_private_numbers)r r,r r rÚ private_key5szRSAPrivateNumbers.private_keycCsbt|tƒstS|j|jko`|j|jko`|j|jko`|j|jko`|j|jko`|j|jko`|j |j kS)N) r)rPÚNotImplementedr2r3rLr5r6r7r&)r Úotherr r rÚ__eq__9s       zRSAPrivateNumbers.__eq__cCs ||k S)Nr )r r_r r rÚ__ne__GszRSAPrivateNumbers.__ne__cCs$t|j|j|j|j|j|j|jfƒS)N)Úhashr2r3rLr5r6r7r&)r r r rÚ__hash__JszRSAPrivateNumbers.__hash__)N)rrrr\rÚread_only_propertyr2r3rLr5r6r7r&r]r`rarcr r r rrPs        rPc@sReZdZdd„ZejdƒZejdƒZddd„Zdd „Z d d „Z d d „Z dd„Z dS)rTcCs4t|tjƒ st|tjƒ r$tdƒ‚||_||_dS)Nz,RSAPublicNumbers arguments must be integers.)r)rQrRrSÚ_eÚ_n)r r:r;r r rr\Ys  zRSAPublicNumbers.__init__rerfNcCst|ƒ}|j|ƒS)N)r Zload_rsa_public_numbers)r r,r r rreszRSAPublicNumbers.public_keycCs dj|ƒS)Nz$)r )r r r rÚ__repr__iszRSAPublicNumbers.__repr__cCs&t|tƒstS|j|jko$|j|jkS)N)r)rTr^r:r;)r r_r r rr`ls zRSAPublicNumbers.__eq__cCs ||k S)Nr )r r_r r rrarszRSAPublicNumbers.__ne__cCst|j|jfƒS)N)rbr:r;)r r r rrcuszRSAPublicNumbers.__hash__)N) rrrr\rrdr:r;rrgr`rarcr r r rrTXs   rT)N)&Z __future__rrrrZmathrÚ ImportErrorZ fractionsrQZ cryptographyrZcryptography.exceptionsrrZcryptography.hazmat.backendsr Z'cryptography.hazmat.backends.interfacesr Z add_metaclassÚABCMetaÚobjectr rr"ZRSAPublicKeyWithSerializationr-r*r9r<rBrCrDrErHrOrPrTr r r rÚs:    &  (   +H