diff options
author | Alex Stapleton <alexs@prol.etari.at> | 2014-04-18 16:10:06 +0100 |
---|---|---|
committer | Alex Stapleton <alexs@prol.etari.at> | 2014-04-18 16:26:44 +0100 |
commit | 5a79ee4f9b831fd11c6f25bc9636d069cc243c75 (patch) | |
tree | 87aa3775a50a3fcfb06d2d26e1860887fb3edfc8 /cryptography | |
parent | 9fa31c6353af3ab32edd6f6988725886d0e2aa4c (diff) | |
download | cryptography-5a79ee4f9b831fd11c6f25bc9636d069cc243c75.tar.gz cryptography-5a79ee4f9b831fd11c6f25bc9636d069cc243c75.tar.bz2 cryptography-5a79ee4f9b831fd11c6f25bc9636d069cc243c75.zip |
Utility methods for RSA CRT params
Diffstat (limited to 'cryptography')
-rw-r--r-- | cryptography/hazmat/primitives/asymmetric/rsa.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/cryptography/hazmat/primitives/asymmetric/rsa.py b/cryptography/hazmat/primitives/asymmetric/rsa.py index 94cc4645..5b15350a 100644 --- a/cryptography/hazmat/primitives/asymmetric/rsa.py +++ b/cryptography/hazmat/primitives/asymmetric/rsa.py @@ -73,6 +73,42 @@ class RSAPublicKey(object): return self.modulus +def _modinv(e, m): + """ + Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1 + """ + x1, y1, x2, y2 = 1, 0, 0, 1 + a, b = e, m + while b > 0: + q, r = divmod(a, b) + xn, yn = x1 - q * x2, y1 - q * y2 + a, b, x1, y1, x2, y2 = b, r, x2, y2, xn, yn + return x1 % m + + +def rsa_crt_iqmp(p, q): + """ + Compute the CRT (q ** -1) % p value from RSA primes p and q. + """ + return _modinv(q, p) + + +def rsa_crt_dmp1(private_exponent, p): + """ + Compute the CRT private_exponent % (p - 1) value from the RSA + private_exponent and p. + """ + return private_exponent % (p - 1) + + +def rsa_crt_dmq1(private_exponent, q): + """ + Compute the CRT private_exponent % (q - 1) value from the RSA + private_exponent and q. + """ + return private_exponent % (q - 1) + + @utils.register_interface(interfaces.RSAPrivateKey) class RSAPrivateKey(object): def __init__(self, p, q, private_exponent, dmp1, dmq1, iqmp, |