aboutsummaryrefslogtreecommitdiffstats
path: root/cryptography
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2014-04-20 09:17:44 -0500
committerPaul Kehrer <paul.l.kehrer@gmail.com>2014-04-20 09:17:44 -0500
commitb8ba9e0fe330e213a93bd32b1368bf839d4d9ec7 (patch)
tree87f91edb5db118c6ca02cdf09b787791cd467f51 /cryptography
parent27723ca8ac79569d9ae60a93856cb574a7e38a24 (diff)
parent5a79ee4f9b831fd11c6f25bc9636d069cc243c75 (diff)
downloadcryptography-b8ba9e0fe330e213a93bd32b1368bf839d4d9ec7.tar.gz
cryptography-b8ba9e0fe330e213a93bd32b1368bf839d4d9ec7.tar.bz2
cryptography-b8ba9e0fe330e213a93bd32b1368bf839d4d9ec7.zip
Merge pull request #935 from public/rsa-crt
Utility methods for RSA CRT params
Diffstat (limited to 'cryptography')
-rw-r--r--cryptography/hazmat/primitives/asymmetric/rsa.py36
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,