Skip to content

Commit de09036

Browse files
authored
Merge pull request #466 from tlsfuzzer/rsa-fixes
RSA fixes
2 parents d59ac46 + af875c9 commit de09036

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

tlslite/utils/cryptomath.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,15 @@ def getRandomPrime(bits, display=False):
387387
#29 % 30 and keep them there
388388
low = ((2 ** (bits-1)) * 3) // 2
389389
high = 2 ** bits - 30
390-
p = getRandomNumber(low, high)
391-
p += 29 - (p % 30)
392-
while 1:
393-
if display: print(".", end=' ')
394-
p += 30
395-
if p >= high:
396-
p = getRandomNumber(low, high)
397-
p += 29 - (p % 30)
398-
if isPrime(p, display=display):
399-
return p
390+
while True:
391+
if display:
392+
print(".", end=' ')
393+
cand_p = getRandomNumber(low, high)
394+
# make odd
395+
if cand_p % 2 == 0:
396+
cand_p += 1
397+
if isPrime(cand_p, display=display):
398+
return cand_p
400399

401400

402401
#Unused at the moment...

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)