rsa-keygen.c 4.71 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* rsa-keygen.c
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

   Generation of RSA keypairs

   Copyright (C) 2002 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/
Niels Möller's avatar
Niels Möller committed
33 34

#if HAVE_CONFIG_H
35
# include "config.h"
Niels Möller's avatar
Niels Möller committed
36 37 38 39 40
#endif

#include <assert.h>
#include <stdlib.h>

41
#include "rsa.h"
42
#include "rsa-internal.h"
43 44
#include "bignum.h"

Niels Möller's avatar
Niels Möller committed
45 46 47 48 49 50 51 52 53 54 55 56
#ifndef DEBUG
# define DEBUG 0
#endif

#if DEBUG
# include <stdio.h>
#endif


int
rsa_generate_keypair(struct rsa_public_key *pub,
		     struct rsa_private_key *key,
57 58
		     void *random_ctx, nettle_random_func *random,
		     void *progress_ctx, nettle_progress_func *progress,
Niels Möller's avatar
Niels Möller committed
59 60 61 62 63 64 65 66 67 68 69
		     unsigned n_size,
		     unsigned e_size)
{
  mpz_t p1;
  mpz_t q1;
  mpz_t phi;
  mpz_t tmp;

  if (e_size)
    {
      /* We should choose e randomly. Is the size reasonable? */
70
      if ((e_size < 16) || (e_size >= n_size) )
Niels Möller's avatar
Niels Möller committed
71 72 73 74 75 76 77
	return 0;
    }
  else
    {
      /* We have a fixed e. Check that it makes sense */

      /* It must be odd */
78
      if (!mpz_tstbit(pub->e, 0))
Niels Möller's avatar
Niels Möller committed
79 80 81 82 83
	return 0;

      /* And 3 or larger */
      if (mpz_cmp_ui(pub->e, 3) < 0)
	return 0;
84 85 86 87

      /* And size less than n */
      if (mpz_sizeinbase(pub->e, 2) >= n_size)
	return 0;
Niels Möller's avatar
Niels Möller committed
88
    }
89

Niels Möller's avatar
Niels Möller committed
90 91 92 93 94 95 96 97 98 99 100
  if (n_size < RSA_MINIMUM_N_BITS)
    return 0;
  
  mpz_init(p1); mpz_init(q1); mpz_init(phi); mpz_init(tmp);

  /* Generate primes */
  for (;;)
    {
      /* Generate p, such that gcd(p-1, e) = 1 */
      for (;;)
	{
101
	  nettle_random_prime(key->p, (n_size+1)/2, 1,
Niels Möller's avatar
Niels Möller committed
102 103
			      random_ctx, random,
			      progress_ctx, progress);
104

Niels Möller's avatar
Niels Möller committed
105 106
	  mpz_sub_ui(p1, key->p, 1);
      
107
	  /* If e was given, we must choose p such that p-1 has no factors in
Niels Möller's avatar
Niels Möller committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	   * common with e. */
	  if (e_size)
	    break;
	  
	  mpz_gcd(tmp, pub->e, p1);

	  if (mpz_cmp_ui(tmp, 1) == 0)
	    break;
	  else if (progress) progress(progress_ctx, 'c');
	} 

      if (progress)
	progress(progress_ctx, '\n');
      
      /* Generate q, such that gcd(q-1, e) = 1 */
      for (;;)
	{
125
	  nettle_random_prime(key->q, n_size/2, 1,
Niels Möller's avatar
Niels Möller committed
126 127
			      random_ctx, random,
			      progress_ctx, progress);
128

Niels Möller's avatar
Niels Möller committed
129 130
	  mpz_sub_ui(q1, key->q, 1);
      
131
	  /* If e was given, we must choose q such that q-1 has no factors in
Niels Möller's avatar
Niels Möller committed
132 133 134 135 136 137 138 139 140 141 142 143 144
	   * common with e. */
	  if (e_size)
	    break;
	  
	  mpz_gcd(tmp, pub->e, q1);

	  if (mpz_cmp_ui(tmp, 1) == 0)
	    break;
	  else if (progress) progress(progress_ctx, 'c');
	}

      /* Now we have the primes. Is the product of the right size? */
      mpz_mul(pub->n, key->p, key->q);
145 146 147

      assert (mpz_sizeinbase(pub->n, 2) == n_size);

Niels Möller's avatar
Niels Möller committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
      if (progress)
	progress(progress_ctx, '\n');

      /* c = q^{-1} (mod p) */
      if (mpz_invert(key->c, key->q, key->p))
	/* This should succeed everytime. But if it doesn't,
	 * we try again. */
	break;
      else if (progress) progress(progress_ctx, '?');
    }

  mpz_mul(phi, p1, q1);
  
  /* If we didn't have a given e, generate one now. */
  if (e_size)
163 164 165 166
    {
      int retried = 0;
      for (;;)
	{
Niels Möller's avatar
Niels Möller committed
167 168 169
	  nettle_mpz_random_size(pub->e,
				 random_ctx, random,
				 e_size);
Niels Möller's avatar
Niels Möller committed
170
	
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	  /* Make sure it's odd and that the most significant bit is
	   * set */
	  mpz_setbit(pub->e, 0);
	  mpz_setbit(pub->e, e_size - 1);

	  /* Needs gmp-3, or inverse might be negative. */
	  if (mpz_invert(key->d, pub->e, phi))
	    break;

	  if (progress) progress(progress_ctx, 'e');
	  retried = 1;
	}
      if (retried && progress)
	progress(progress_ctx, '\n');	
    }
Niels Möller's avatar
Niels Möller committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  else
    {
      /* Must always succeed, as we already that e
       * doesn't have any common factor with p-1 or q-1. */
      int res = mpz_invert(key->d, pub->e, phi);
      assert(res);
    }

  /* Done! Almost, we must compute the auxillary private values. */
  /* a = d % (p-1) */
  mpz_fdiv_r(key->a, key->d, p1);

  /* b = d % (q-1) */
  mpz_fdiv_r(key->b, key->d, q1);

  /* c was computed earlier */

203
  pub->size = key->size = (n_size + 7) / 8;
Niels Möller's avatar
Niels Möller committed
204 205 206 207 208 209
  assert(pub->size >= RSA_MINIMUM_N_OCTETS);
  
  mpz_clear(p1); mpz_clear(q1); mpz_clear(phi); mpz_clear(tmp);

  return 1;
}