def _sign(self, M, K): if (not hasattr(self, 'x')): raise TypeError('Private key not available in this object') p1 = self.p - 1 K = Integer(K) if (K.gcd(p1) != 1): raise ValueError('Bad K value: GCD(K,p-1)!=1') a = pow(self.g, K, self.p) t = (Integer(M) - self.x * a) % p1 while t < 0: t = t + p1 b = (t * K.inverse(p1)) % p1 return list(map(int, (a, b)))
def construct(rsa_components, consistency_check=True): r"""Construct an RSA key from a tuple of valid RSA components. The modulus **n** must be the product of two primes. The public exponent **e** must be odd and larger than 1. In case of a private key, the following equations must apply: .. math:: \begin{align} p*q &= n \\ e*d &\equiv 1 ( \text{mod lcm} [(p-1)(q-1)]) \\ p*u &\equiv 1 ( \text{mod } q) \end{align} Args: rsa_components (tuple): A tuple of integers, with at least 2 and no more than 6 items. The items come in the following order: 1. RSA modulus *n*. 2. Public exponent *e*. 3. Private exponent *d*. Only required if the key is private. 4. First factor of *n* (*p*). Optional, but the other factor *q* must also be present. 5. Second factor of *n* (*q*). Optional. 6. CRT coefficient *q*, that is :math:`p^{-1} \text{mod }q`. Optional. consistency_check (boolean): If ``True``, the library will verify that the provided components fulfil the main RSA properties. Raises: ValueError: when the key being imported fails the most basic RSA validity checks. Returns: An RSA key object (:class:`RsaKey`). """ class InputComps(object): pass input_comps = InputComps() for (comp, value) in zip(('n', 'e', 'd', 'p', 'q', 'u'), rsa_components): setattr(input_comps, comp, Integer(value)) n = input_comps.n e = input_comps.e if not hasattr(input_comps, 'd'): key = RsaKey(n=n, e=e) else: d = input_comps.d if hasattr(input_comps, 'q'): p = input_comps.p q = input_comps.q else: # Compute factors p and q from the private exponent d. # We assume that n has no more than two factors. # See 8.2.2(i) in Handbook of Applied Cryptography. ktot = d * e - 1 # The quantity d*e-1 is a multiple of phi(n), even, # and can be represented as t*2^s. t = ktot while t % 2 == 0: t //= 2 # Cycle through all multiplicative inverses in Zn. # The algorithm is non-deterministic, but there is a 50% chance # any candidate a leads to successful factoring. # See "Digitalized Signatures and Public Key Functions as Intractable # as Factorization", M. Rabin, 1979 spotted = False a = Integer(2) while not spotted and a < 100: k = Integer(t) # Cycle through all values a^{t*2^i}=a^k while k < ktot: cand = pow(a, k, n) # Check if a^k is a non-trivial root of unity (mod n) if cand != 1 and cand != (n - 1) and pow(cand, 2, n) == 1: # We have found a number such that (cand-1)(cand+1)=0 (mod n). # Either of the terms divides n. p = Integer(n).gcd(cand + 1) spotted = True break k *= 2 # This value was not any good... let's try another! a += 2 if not spotted: raise ValueError( "Unable to compute factors p and q from exponent d.") # Found ! assert ((n % p) == 0) q = n // p if hasattr(input_comps, 'u'): u = input_comps.u else: u = p.inverse(q) # Build key object key = RsaKey(n=n, e=e, d=d, p=p, q=q, u=u) # Verify consistency of the key if consistency_check: # Modulus and public exponent must be coprime if e <= 1 or e >= n: raise ValueError("Invalid RSA public exponent") if Integer(n).gcd(e) != 1: raise ValueError("RSA public exponent is not coprime to modulus") # For RSA, modulus must be odd if not n & 1: raise ValueError("RSA modulus is not odd") if key.has_private(): # Modulus and private exponent must be coprime if d <= 1 or d >= n: raise ValueError("Invalid RSA private exponent") if Integer(n).gcd(d) != 1: raise ValueError( "RSA private exponent is not coprime to modulus") # Modulus must be product of 2 primes if p * q != n: raise ValueError("RSA factors do not match modulus") if test_probable_prime(p) == COMPOSITE: raise ValueError("RSA factor p is composite") if test_probable_prime(q) == COMPOSITE: raise ValueError("RSA factor q is composite") # See Carmichael theorem phi = (p - 1) * (q - 1) lcm = phi // (p - 1).gcd(q - 1) if (e * d % int(lcm)) != 1: raise ValueError("Invalid RSA condition") if hasattr(key, 'u'): # CRT coefficient if u <= 1 or u >= q: raise ValueError("Invalid RSA component u") if (p * u % q) != 1: raise ValueError("Invalid RSA component u with p") return key
def generate(bits, randfunc=None, e=65537): """Create a new RSA key pair. The algorithm closely follows NIST `FIPS 186-4`_ in its sections B.3.1 and B.3.3. The modulus is the product of two non-strong probable primes. Each prime passes a suitable number of Miller-Rabin tests with random bases and a single Lucas test. Args: bits (integer): Key length, or size (in bits) of the RSA modulus. It must be at least 1024, but **2048 is recommended.** The FIPS standard only defines 1024, 2048 and 3072. randfunc (callable): Function that returns random bytes. The default is :func:`Crypto.Random.get_random_bytes`. e (integer): Public RSA exponent. It must be an odd positive integer. It is typically a small number with very few ones in its binary representation. The FIPS standard requires the public exponent to be at least 65537 (the default). Returns: an RSA key object (:class:`RsaKey`, with private key). .. _FIPS 186-4: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf """ if bits < 1024: raise ValueError("RSA modulus length must be >= 1024") if e % 2 == 0 or e < 3: raise ValueError( "RSA public exponent must be a positive, odd integer larger than 2." ) if randfunc is None: randfunc = Random.get_random_bytes d = n = Integer(1) e = Integer(e) while n.size_in_bits() != bits and d < (1 << (bits // 2)): # Generate the prime factors of n: p and q. # By construciton, their product is always # 2^{bits-1} < p*q < 2^bits. size_q = bits // 2 size_p = bits - size_q min_p = min_q = (Integer(1) << (2 * size_q - 1)).sqrt() if size_q != size_p: min_p = (Integer(1) << (2 * size_p - 1)).sqrt() def filter_p(candidate): return candidate > min_p and (candidate - 1).gcd(e) == 1 p = generate_probable_prime(exact_bits=size_p, randfunc=randfunc, prime_filter=filter_p) min_distance = Integer(1) << (bits // 2 - 100) def filter_q(candidate): return (candidate > min_q and (candidate - 1).gcd(e) == 1 and abs(candidate - p) > min_distance) q = generate_probable_prime(exact_bits=size_q, randfunc=randfunc, prime_filter=filter_q) n = p * q lcm = (p - 1).lcm(q - 1) d = e.inverse(lcm) if p > q: p, q = q, p u = p.inverse(q) return RsaKey(n=n, e=e, d=d, p=p, q=q, u=u)