U q`. @sddlZddlZddlmZddlmZddlmZmZddl m Z ddl m Z ddl mZmZddlmZdd lmZmZmZGd d d ejd ZeZGd ddejd ZeZd.eeedddZeeddddZeeeeeeeedd ddZeeddddZeeedddZ eeedddZ!eeed d!d"Z"eeed#d$d%Z#d&Z$eeeej%eefd'd(d)Z&Gd*d+d+e'Z(Gd,d-d-e'Z)dS)/N)gcd)utils)UnsupportedAlgorithm_Reasons) _get_backend) RSABackend)_serializationhashes)AsymmetricPadding)AsymmetricSignatureContextAsymmetricVerificationContextrc@seZdZejeejedddZ eje ee dddZ ej e ddd Zejd dd d Zeje eejejejfe d ddZejddddZejejejeje dddZdS) RSAPrivateKey)padding algorithmreturncCsdS)zN Returns an AsymmetricSignatureContext used for signing data. N)selfrrrrT/tmp/pip-target-nv4zd3e_/lib/python/cryptography/hazmat/primitives/asymmetric/rsa.pysignerszRSAPrivateKey.signer) ciphertextrrcCsdS)z3 Decrypts the provided ciphertext. Nr)rrrrrrdecrypt szRSAPrivateKey.decryptrcCsdSz7 The bit length of the public modulus. Nrrrrrkey_size&szRSAPrivateKey.key_size RSAPublicKeycCsdS)zD The RSAPublicKey associated with this private key. Nrrrrr public_key,szRSAPrivateKey.public_key)datarrrcCsdS)z! Signs the data. Nr)rrrrrrrsign2szRSAPrivateKey.signRSAPrivateNumberscCsdS)z/ Returns an RSAPrivateNumbers. Nrrrrrprivate_numbers=szRSAPrivateKey.private_numbers)encodingformatencryption_algorithmrcCsdSz6 Returns the key serialized as bytes. Nr)rr!r"r#rrr private_bytesCszRSAPrivateKey.private_bytesN)__name__ __module__ __qualname__abcabstractmethodr r HashAlgorithmr rbytesrabstractpropertyintrrtypingUnion asym_utils Prehashedrr rEncodingZ PrivateFormatZKeySerializationEncryptionr%rrrrr s2 r ) metaclassc@seZdZejeeeje dddZ ejeeedddZ ej e ddd Zejd dd d Zejejejed ddZejeeeejejejfddddZejeeejejedddZdS)r) signaturerrrcCsdS)zY Returns an AsymmetricVerificationContext used for verifying signatures. Nrrr5rrrrrverifierSszRSAPublicKey.verifier) plaintextrrcCsdS)z/ Encrypts the given plaintext. Nr)rr8rrrrencrypt^szRSAPublicKey.encryptrcCsdSrrrrrrrdszRSAPublicKey.key_sizeRSAPublicNumberscCsdS)z- Returns an RSAPublicNumbers Nrrrrrpublic_numbersjszRSAPublicKey.public_numbers)r!r"rcCsdSr$r)rr!r"rrr public_bytespszRSAPublicKey.public_bytesN)r5rrrrcCsdS)z5 Verifies the signature of the data. Nr)rr5rrrrrrverifyzszRSAPublicKey.verifycCsdS)z@ Recovers the original data from the signature. Nrr6rrrrecover_data_from_signaturesz(RSAPublicKey.recover_data_from_signature)r&r'r(r)r*r,r r r+r r7r9r-r.rr;rr3Z PublicFormatr<r/r0r1r2r=Optionalr>rrrrrRs<    r)public_exponentrrcCs4t|}t|tstdtjt|||||S)Nz-Backend object does not implement RSABackend.)r isinstancerrrZBACKEND_MISSING_INTERFACE_verify_rsa_parametersZgenerate_rsa_private_key)r@rbackendrrrgenerate_private_keys  rDcCs$|dkrtd|dkr tddS)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)r@rrrrrBs rB) pqprivate_exponentdmp1dmq1iqmpr@modulusrcCs|dkrtd||kr td||kr0td||kr@td||krPtd||kr`td||krptd|dks||krtd |d @d krtd |d @d krtd |d @d krtd|||krtddS)NrEzmodulus 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.rF)rHrIrJrKrLrMr@rNrrr_check_private_key_componentss0     rP)enrcCs@|dkrtd|dks ||kr(td|d@dkr= 3.ze must be >= 3 and < n.rOrze must be odd.rFrQrRrrr_check_public_key_componentss  rT)rQmrc CsRd\}}||}}|dkrJt||\}}|||}||||f\}}}}q||S)zO Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1 )rOrr)divmod) rQrUx1Zx2abrIrZxnrrr_modinvs  r[)rHrIrcCs t||S)zF Compute the CRT (q ** -1) % p value from RSA primes p and q. )r[)rHrIrrr rsa_crt_iqmpsr\)rJrHrcCs ||dS)zg Compute the CRT private_exponent % (p - 1) value from the RSA private_exponent (d) and p. rOr)rJrHrrr rsa_crt_dmp1sr])rJrIrcCs ||dS)zg Compute the CRT private_exponent % (q - 1) value from the RSA private_exponent (d) and q. rOr)rJrIrrr rsa_crt_dmq1sr^i)rRrQdrc Cs||d}|}|ddkr&|d}qd}d}|s|tkr|}||krt|||}|dkr||dkrt|d|dkrt|d|} d}q|d9}q>|d7}q.|stdt|| \} } | dkstt| | 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. rOrFTz2Unable to compute factors p and q from exponent d.)reverse)_MAX_RECOVERY_ATTEMPTSpowrrGrVAssertionErrorsorted) rRrQr_ZktottZspottedrXkcandrHrIrZrrrrsa_recover_prime_factorss,     $   ric@seZdZeeeeeeddddZedZedZedZ edZ ed Z ed Z ed Z ded ddZddZddZddZd S)rr:)rHrIr_rKrLrMr;cCst|tr)r"rrrr__repr__szRSAPublicNumbers.__repr__cCs&t|tstS|j|jko$|j|jkSrs)rAr:rvrQrRrwrrrrys zRSAPublicNumbers.__eq__cCs ||k SrsrrwrrrrzszRSAPublicNumbers.__ne__cCst|j|jfSrs)r{rQrRrrrrr|szRSAPublicNumbers.__hash__)N)r&r'r(r.rrrr}rQrRrrrryrzr|rrrrr:s  r:)N)*r)r/mathrZ cryptographyrZcryptography.exceptionsrrZcryptography.hazmat.backendsrZ'cryptography.hazmat.backends.interfacesrZcryptography.hazmat.primitivesrr Z*cryptography.hazmat.primitives._asymmetricr Z)cryptography.hazmat.primitives.asymmetricr r r1ABCMetar ZRSAPrivateKeyWithSerializationrZRSAPublicKeyWithSerializationr.rDrBrPrTr[r\r]r^rbTupleriobjectrr:rrrrsV     8@   /    -Q