1
/* primegen.c - prime number generator
2
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
6
* GnuPG is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* GnuPG is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20
* ***********************************************************************
21
* The algorithm used to generate practically save primes is due to
22
* Lim and Lee as described in the CRYPTO '97 proceedings (ISBN3540633847)
35
static int no_of_small_prime_numbers;
36
static MPI gen_prime( unsigned nbits, int mode, int randomlevel );
37
static int check_prime( MPI prime, MPI val_2 );
38
static int is_prime( MPI n, int steps, int *count );
39
static void m_out_of_n( char *array, int m, int n );
41
static void (*progress_cb) ( void *, int );
42
static void *progress_cb_data;
45
register_primegen_progress ( void (*cb)( void *, int), void *cb_data )
48
progress_cb_data = cb_data;
56
progress_cb ( progress_cb_data, c );
63
* Generate a prime number (stored in secure memory)
66
generate_secret_prime( unsigned nbits )
70
prime = gen_prime( nbits, 1, 2 );
76
generate_public_prime( unsigned nbits )
80
prime = gen_prime( nbits, 0, 2 );
87
* We do not need to use the strongest RNG because we gain no extra
88
* security from it - The prime number is public and we could also
89
* offer the factors for those who are willing to check that it is
90
* indeed a strong prime.
93
* 1: Make sure that at least one factor is of size qbits.
96
generate_elg_prime( int mode, unsigned pbits, unsigned qbits,
97
MPI g, MPI **ret_factors )
99
int n; /* number of factors */
100
int m; /* number of primes in pool */
101
unsigned fbits; /* length of prime factors */
102
MPI *factors; /* current factors */
103
MPI *pool; /* pool of primes */
104
MPI q; /* first prime factor (variable)*/
105
MPI prime; /* prime test value */
106
MPI q_factor; /* used for mode 1 */
111
unsigned req_qbits = qbits; /* the requested q bits size */
112
MPI val_2 = mpi_alloc_set_ui( 2 );
114
/* find number of needed prime factors */
115
for(n=1; (pbits - qbits - 1) / n >= qbits; n++ )
118
if( !n || (mode==1 && n < 2) )
119
log_fatal("can't gen prime with pbits=%u qbits=%u\n", pbits, qbits );
122
fbits = (pbits - 2*req_qbits -1) / n;
123
qbits = pbits - req_qbits - n*fbits;
126
fbits = (pbits - req_qbits -1) / n;
127
qbits = pbits - n*fbits;
130
log_debug("gen prime: pbits=%u qbits=%u fbits=%u/%u n=%d\n",
131
pbits, req_qbits, qbits, fbits, n );
132
prime = mpi_alloc( (pbits + BITS_PER_MPI_LIMB - 1) / BITS_PER_MPI_LIMB );
133
q = gen_prime( qbits, 0, 0 );
134
q_factor = mode==1? gen_prime( req_qbits, 0, 0 ) : NULL;
136
/* allocate an array to hold the factors + 2 for later usage */
137
factors = m_alloc_clear( (n+2) * sizeof *factors );
139
/* make a pool of 3n+5 primes (this is an arbitrary value) */
142
m += 5; /* need some more for DSA */
145
pool = m_alloc_clear( m * sizeof *pool );
147
/* permutate over the pool of primes */
152
/* allocate new primes */
153
for(i=0; i < m; i++ ) {
157
/* init m_out_of_n() */
158
perms = m_alloc_clear( m );
159
for(i=0; i < n; i++ ) {
161
pool[i] = gen_prime( fbits, 0, 0 );
162
factors[i] = pool[i];
166
m_out_of_n( perms, n, m );
167
for(i=j=0; i < m && j < n ; i++ )
170
pool[i] = gen_prime( fbits, 0, 0 );
171
factors[j++] = pool[i];
174
m_free(perms); perms = NULL;
176
goto next_try; /* allocate new primes */
181
mpi_mul_ui( prime, prime, 2 );
183
mpi_mul( prime, prime, q_factor );
184
for(i=0; i < n; i++ )
185
mpi_mul( prime, prime, factors[i] );
186
mpi_add_ui( prime, prime, 1 );
187
nprime = mpi_get_nbits(prime);
188
if( nprime < pbits ) {
189
if( ++count1 > 20 ) {
194
q = gen_prime( qbits, 0, 0 );
200
if( nprime > pbits ) {
201
if( ++count2 > 20 ) {
206
q = gen_prime( qbits, 0, 0 );
212
} while( !(nprime == pbits && check_prime( prime, val_2 )) );
216
log_mpidump( "prime : ", prime );
217
log_mpidump( "factor q: ", q );
219
log_mpidump( "factor q0: ", q_factor );
220
for(i=0; i < n; i++ )
221
log_mpidump( "factor pi: ", factors[i] );
222
log_debug("bit sizes: prime=%u, q=%u", mpi_get_nbits(prime), mpi_get_nbits(q) );
224
fprintf(stderr, ", q0=%u", mpi_get_nbits(q_factor) );
225
for(i=0; i < n; i++ )
226
fprintf(stderr, ", p%d=%u", i, mpi_get_nbits(factors[i]) );
230
if( ret_factors ) { /* caller wants the factors */
231
*ret_factors = m_alloc_clear( (n+2) * sizeof **ret_factors);
234
(*ret_factors)[i++] = mpi_copy( q_factor );
236
(*ret_factors)[i] = mpi_copy( factors[i-1] );
240
(*ret_factors)[i] = mpi_copy( factors[i] );
244
if( g ) { /* create a generator (start with 3)*/
245
MPI tmp = mpi_alloc( mpi_get_nlimbs(prime) );
246
MPI b = mpi_alloc( mpi_get_nlimbs(prime) );
247
MPI pmin1 = mpi_alloc( mpi_get_nlimbs(prime) );
250
BUG(); /* not yet implemented */
252
factors[n+1] = mpi_alloc_set_ui(2);
253
mpi_sub_ui( pmin1, prime, 1 );
258
log_debug("checking g: ");
259
mpi_print( stderr, g, 1 );
263
for(i=0; i < n+2; i++ ) {
264
/*fputc('~', stderr);*/
265
mpi_fdiv_q(tmp, pmin1, factors[i] );
266
/* (no mpi_pow(), but it is okay to use this with mod prime) */
267
mpi_powm(b, g, tmp, prime );
268
if( !mpi_cmp_ui(b, 1) )
274
mpi_free(factors[n+1]);
282
m_free( factors ); /* (factors are shallow copies) */
283
for(i=0; i < m; i++ )
295
gen_prime( unsigned nbits, int secret, int randomlevel )
298
MPI prime, ptest, pminus1, val_2, val_3, result;
304
if( 0 && DBG_CIPHER )
305
log_debug("generate a prime of %u bits ", nbits );
307
if( !no_of_small_prime_numbers ) {
308
for(i=0; small_prime_numbers[i]; i++ )
309
no_of_small_prime_numbers++;
311
mods = m_alloc( no_of_small_prime_numbers * sizeof *mods );
312
/* make nbits fit into MPI implementation */
313
nlimbs = (nbits + BITS_PER_MPI_LIMB - 1) / BITS_PER_MPI_LIMB;
314
val_2 = mpi_alloc_set_ui( 2 );
315
val_3 = mpi_alloc_set_ui( 3);
316
prime = secret? mpi_alloc_secure( nlimbs ): mpi_alloc( nlimbs );
317
result = mpi_alloc_like( prime );
318
pminus1= mpi_alloc_like( prime );
319
ptest = mpi_alloc_like( prime );
321
for(;;) { /* try forvever */
324
/* generate a random number */
325
{ char *p = get_random_bits( nbits, randomlevel, secret );
326
mpi_set_buffer( prime, p, (nbits+7)/8, 0 );
330
/* Set high order bit to 1, set low order bit to 0.
331
If we are generating a secret prime we are most probably
332
doing that for RSA, to make sure that the modulus does have
333
the requested keysize we set the 2 high order bits */
334
mpi_set_highbit( prime, nbits-1 );
336
mpi_set_bit (prime, nbits-2);
337
mpi_set_bit( prime, 0 );
339
/* calculate all remainders */
340
for(i=0; (x = small_prime_numbers[i]); i++ )
341
mods[i] = mpi_fdiv_r_ui(NULL, prime, x);
343
/* now try some primes starting with prime */
344
for(step=0; step < 20000; step += 2 ) {
345
/* check against all the small primes we have in mods */
347
for(i=0; (x = small_prime_numbers[i]); i++ ) {
348
while( mods[i] + step >= x )
350
if( !(mods[i] + step) )
354
continue; /* found a multiple of an already known prime */
356
mpi_add_ui( ptest, prime, step );
358
/* do a faster Fermat test */
360
mpi_sub_ui( pminus1, ptest, 1);
361
mpi_powm( result, val_2, pminus1, ptest );
362
if( !mpi_cmp_ui( result, 1 ) ) { /* not composite */
363
/* perform stronger tests */
364
if( is_prime(ptest, 5, &count2 ) ) {
365
if( !mpi_test_bit( ptest, nbits-1 ) ) {
367
log_debug("overflow in prime generation\n");
368
break; /* step loop, continue with a new prime */
380
if( ++dotcount == 10 ) {
385
progress(':'); /* restart with a new random value */
390
* Returns: true if this may be a prime
393
check_prime( MPI prime, MPI val_2 )
399
/* check against small primes */
400
for(i=0; (x = small_prime_numbers[i]); i++ ) {
401
if( mpi_divisible_ui( prime, x ) )
405
/* a quick fermat test */
407
MPI result = mpi_alloc_like( prime );
408
MPI pminus1 = mpi_alloc_like( prime );
409
mpi_sub_ui( pminus1, prime, 1);
410
mpi_powm( result, val_2, pminus1, prime );
412
if( mpi_cmp_ui( result, 1 ) ) { /* if composite */
420
/* perform stronger tests */
421
if( is_prime(prime, 5, &count ) )
422
return 1; /* is probably a prime */
429
* Return true if n is probably a prime
432
is_prime( MPI n, int steps, int *count )
434
MPI x = mpi_alloc( mpi_get_nlimbs( n ) );
435
MPI y = mpi_alloc( mpi_get_nlimbs( n ) );
436
MPI z = mpi_alloc( mpi_get_nlimbs( n ) );
437
MPI nminus1 = mpi_alloc( mpi_get_nlimbs( n ) );
438
MPI a2 = mpi_alloc_set_ui( 2 );
442
unsigned nbits = mpi_get_nbits( n );
444
mpi_sub_ui( nminus1, n, 1 );
446
/* find q and k, so that n = 1 + 2^k * q */
447
q = mpi_copy( nminus1 );
448
k = mpi_trailing_zeros( q );
449
mpi_tdiv_q_2exp(q, q, k);
451
for(i=0 ; i < steps; i++ ) {
457
/*mpi_set_bytes( x, nbits-1, get_random_byte, 0 );*/
458
{ char *p = get_random_bits( nbits, 0, 0 );
459
mpi_set_buffer( x, p, (nbits+7)/8, 0 );
462
/* make sure that the number is smaller than the prime
463
* and keep the randomness of the high bit */
464
if( mpi_test_bit( x, nbits-2 ) ) {
465
mpi_set_highbit( x, nbits-2 ); /* clear all higher bits */
468
mpi_set_highbit( x, nbits-2 );
469
mpi_clear_bit( x, nbits-2 );
471
assert( mpi_cmp( x, nminus1 ) < 0 && mpi_cmp_ui( x, 1 ) > 0 );
473
mpi_powm( y, x, q, n);
474
if( mpi_cmp_ui(y, 1) && mpi_cmp( y, nminus1 ) ) {
475
for( j=1; j < k && mpi_cmp( y, nminus1 ); j++ ) {
476
mpi_powm(y, y, a2, n);
477
if( !mpi_cmp_ui( y, 1 ) )
478
goto leave; /* not a prime */
480
if( mpi_cmp( y, nminus1 ) )
481
goto leave; /* not a prime */
485
rc = 1; /* may be a prime */
499
m_out_of_n( char *array, int m, int n )
501
int i=0, i1=0, j=0, jp=0, j1=0, k1=0, k2=0;
506
if( m == 1 ) { /* special case */
507
for(i=0; i < n; i++ )
518
for(j=1; j < n; j++ ) {
519
if( array[n-1] == array[n-j-1] )
525
if( m & 1 ) { /* m is odd */
541
else if( array[k2] && array[k2-1] )
546
else { /* m is even */
562
for(i=1; i <= jp; i++ ) {
580
array[k1-1] = !array[k1-1];
581
array[k2-1] = !array[k2-1];