diff options
author | Sybren A. Stüvel <sybren@stuvel.eu> | 2016-03-17 11:50:15 +0100 |
---|---|---|
committer | Sybren A. Stüvel <sybren@stuvel.eu> | 2016-03-17 11:50:15 +0100 |
commit | 6ac63ad2998be9f4f8ed84dd671fba1811be7256 (patch) | |
tree | 6f044abf72274798fd38f25c6dda5dbbf0d86a6b | |
parent | bb74fc918d133f170a40f4f90a3db3bf484214e7 (diff) | |
download | rsa-git-6ac63ad2998be9f4f8ed84dd671fba1811be7256.tar.gz |
Prevent possible infinite loops.
-rw-r--r-- | rsa/prime.py | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/rsa/prime.py b/rsa/prime.py index c377c91..6f23f9d 100644 --- a/rsa/prime.py +++ b/rsa/prime.py @@ -53,10 +53,15 @@ def miller_rabin_primality_testing(n, k): :rtype: bool """ + # prevent potential infinite loop when d = 0 + if n < 2: + return False + # Decompose (n - 1) to write it as (2 ** r) * d # While d is even, divide it by 2 and increase the exponent. d = n - 1 r = 0 + while not (d & 1): r += 1 d >>= 1 @@ -64,9 +69,7 @@ def miller_rabin_primality_testing(n, k): # Test k witnesses. for _ in range(k): # Generate random integer a, where 2 <= a <= (n - 2) - a = 0 - while a < 2: - a = rsa.randnum.randint(n - 2) + a = rsa.randnum.randint(n - 4) + 2 x = pow(a, d, n) if x == 1 or x == n - 1: @@ -134,6 +137,8 @@ def getprime(nbits): True """ + assert nbits > 3 # the loop wil hang on too small numbers + while True: integer = rsa.randnum.read_random_odd_int(nbits) |