Skip to content

Commit af875c9

Browse files
committed
rsa: retry keygen if the private exponent can't be calculated
1 parent 29b7ebe commit af875c9

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

tlslite/utils/python_rsakey.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,21 @@ def generate(bits, key_type="rsa"):
113113
114114
key_type can be "rsa" for a universal rsaEncryption key or
115115
"rsa-pss" for a key that can be used only for RSASSA-PSS."""
116+
# p, q, and t are standard names for the variables in RSA, so
117+
# ignore the fact those are one character long variable names
118+
# pylint: disable=invalid-name
116119
key = Python_RSAKey()
117-
p = getRandomPrime(bits//2, False)
118-
q = getRandomPrime(bits//2, False)
119-
if gmpyLoaded or GMPY2_LOADED:
120-
p = mpz(p)
121-
q = mpz(q)
122-
t = lcm(p-1, q-1)
120+
while True:
121+
p = getRandomPrime(bits//2, False)
122+
q = getRandomPrime(bits//2, False)
123+
if gmpyLoaded or GMPY2_LOADED:
124+
p = mpz(p)
125+
q = mpz(q)
126+
t = lcm(p-1, q-1)
127+
# since we need to calculate inverse of 65537 mod t, they
128+
# must be relatively prime (coprime)
129+
if gcd(t, 65537) == 1:
130+
break
123131
key.n = p * q
124132
if gmpyLoaded or GMPY2_LOADED:
125133
key.e = mpz(65537)
@@ -132,6 +140,7 @@ def generate(bits, key_type="rsa"):
132140
key.dQ = key.d % (q-1)
133141
key.qInv = invMod(q, p)
134142
key.key_type = key_type
143+
# pylint: enable=invalid-name
135144
return key
136145

137146
@staticmethod

0 commit comments

Comments
 (0)