cryptography/docs/hazmat/primitives
Daniel Lenski 8a7f27be3d
Add rsa_recover_private_exponent function (#11193)
Given the RSA public exponent (`e`), and the RSA primes (`p`, `q`), it is possible
to calculate the corresponding private exponent `d = e⁻¹ mod λ(n)` where
`λ(n) = lcm(p-1, q-1)`.

With this function added, it becomes possible to use the library to reconstruct an RSA
private key given *only* `p`, `q`, and `e`:

    from cryptography.hazmat.primitives.asymmetric import rsa

    n = p * q
    d = rsa.rsa_recover_private_exponent(e, p, q)  # newly-added piece
    iqmp = rsa.rsa_crt_iqmp(p, q)                  # preexisting
    dmp1 = rsa.rsa_crt_dmp1(d, p)                  # preexisting
    dmq1 = rsa.rsa_crt_dmq1(d, q)                  # preexisting

    assert rsa.rsa_recover_prime_factors(n, e, d) in ((p, q), (q, p))  # verify consistency

    privk = rsa.RSAPrivateNumbers(p, q, d, dmp1, dmq1, iqmp, rsa.RSAPublicNumbers(e, n)).private_key()

Older RSA implementations, including the original RSA paper, often used the
Euler totient function `ɸ(n) = (p-1) * (q-1)` instead of `λ(n)`.  The
private exponents generated by that method work equally well, but may be
larger than strictly necessary (`λ(n)` always divides `ɸ(n)`).  This commit
additionally implements `_rsa_recover_euler_private_exponent`, so that tests
of the internal structure of RSA private keys can allow for either the Euler
or the Carmichael versions of the private exponents.

It makes sense to expose only the more modern version (using the Carmichael
totient function) for public usage, given that it is slightly more
computationally efficient to use the keys in this form, and that some
standards like FIPS 186-4 require this form.  (See
https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf#page=63)
2024-07-05 15:14:04 -07:00
..
asymmetric Add rsa_recover_private_exponent function (#11193) 2024-07-05 15:14:04 -07:00
mac update MAC docs (#8777) 2023-04-21 16:04:45 -06:00
aead.rst Update various links in the docs for permanent redirects (#10098) 2023-12-31 17:56:51 -03:00
constant-time.rst As always, recommend cryptoing while you crypto 2016-02-02 21:27:57 -05:00
cryptographic-hashes.rst Update various links in the docs for permanent redirects (#10098) 2023-12-31 17:56:51 -03:00
index.rst increase toctree depth on primitives (#10282) 2024-01-28 15:40:02 -05:00
key-derivation-functions.rst Update various links in the docs for permanent redirects (#10109) 2024-01-02 21:00:20 -03:00
keywrap.rst deprecate backend part 4 of n (#6522) 2021-11-02 17:49:25 -04:00
padding.rst Convert PKCS7PaddingContext to Rust (#11089) 2024-06-10 12:22:36 -04:00
symmetric-encryption.rst migrate ARC4 and TripleDES to decrepit (#10286) 2024-01-29 19:42:21 -05:00
twofactor.rst Make Union type aliases a documented public API (#8168) 2023-03-07 19:20:32 +08:00