1 /* elgamal.c - elgamal Public Key encryption
2 * Copyright (C) 1998, 2000, 2001, 2003,
3 * 2004 Free Software Foundation, Inc.
5 * For a description of the algorithm, see:
6 * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1996.
7 * ISBN 0-471-11709-9. Pages 476 ff.
9 * This file is part of GnuPG.
11 * GnuPG is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * GnuPG is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
37 MPI g; /* group generator */
38 MPI y; /* g^x mod p */
44 MPI g; /* group generator */
45 MPI y; /* g^x mod p */
46 MPI x; /* secret exponent */
50 static void test_keys( ELG_secret_key *sk, unsigned nbits );
51 static MPI gen_k( MPI p, int small_k );
52 static void generate( ELG_secret_key *sk, unsigned nbits, MPI **factors );
53 static int check_secret_key( ELG_secret_key *sk );
54 static void do_encrypt(MPI a, MPI b, MPI input, ELG_public_key *pkey );
55 static void decrypt(MPI output, MPI a, MPI b, ELG_secret_key *skey );
58 static void (*progress_cb) ( void *, int );
59 static void *progress_cb_data;
62 register_pk_elg_progress ( void (*cb)( void *, int), void *cb_data )
65 progress_cb_data = cb_data;
73 progress_cb ( progress_cb_data, c );
80 * Michael Wiener's table about subgroup sizes to match field sizes
81 * (floating around somewhere - Fixme: need a reference)
84 wiener_map( unsigned int n )
86 static struct { unsigned int p_n, q_n; } t[] =
87 { /* p q attack cost */
88 { 512, 119 }, /* 9 x 10^17 */
89 { 768, 145 }, /* 6 x 10^21 */
90 { 1024, 165 }, /* 7 x 10^24 */
91 { 1280, 183 }, /* 3 x 10^27 */
92 { 1536, 198 }, /* 7 x 10^29 */
93 { 1792, 212 }, /* 9 x 10^31 */
94 { 2048, 225 }, /* 8 x 10^33 */
95 { 2304, 237 }, /* 5 x 10^35 */
96 { 2560, 249 }, /* 3 x 10^37 */
97 { 2816, 259 }, /* 1 x 10^39 */
98 { 3072, 269 }, /* 3 x 10^40 */
99 { 3328, 279 }, /* 8 x 10^41 */
100 { 3584, 288 }, /* 2 x 10^43 */
101 { 3840, 296 }, /* 4 x 10^44 */
102 { 4096, 305 }, /* 7 x 10^45 */
103 { 4352, 313 }, /* 1 x 10^47 */
104 { 4608, 320 }, /* 2 x 10^48 */
105 { 4864, 328 }, /* 2 x 10^49 */
106 { 5120, 335 }, /* 3 x 10^50 */
111 for(i=0; t[i].p_n; i++ ) {
115 /* not in table - use some arbitrary high number ;-) */
120 test_keys( ELG_secret_key *sk, unsigned nbits )
123 MPI test = mpi_alloc( 0 );
124 MPI out1_a = mpi_alloc( nbits / BITS_PER_MPI_LIMB );
125 MPI out1_b = mpi_alloc( nbits / BITS_PER_MPI_LIMB );
126 MPI out2 = mpi_alloc( nbits / BITS_PER_MPI_LIMB );
132 /*mpi_set_bytes( test, nbits, get_random_byte, 0 );*/
133 { char *p = get_random_bits( nbits, 0, 0 );
134 mpi_set_buffer( test, p, (nbits+7)/8, 0 );
138 do_encrypt( out1_a, out1_b, test, &pk );
139 decrypt( out2, out1_a, out1_b, sk );
140 if( mpi_cmp( test, out2 ) )
141 log_fatal("Elgamal operation: encrypt, decrypt failed\n");
151 * Generate a random secret exponent k from prime p, so that k is
152 * relatively prime to p-1. With SMALL_K set, k will be selected for
153 * better encryption performance - this must never bee used signing!
156 gen_k( MPI p, int small_k )
158 MPI k = mpi_alloc_secure( 0 );
159 MPI temp = mpi_alloc( mpi_get_nlimbs(p) );
160 MPI p_1 = mpi_copy(p);
161 unsigned int orig_nbits = mpi_get_nbits(p);
168 /* Using a k much lesser than p is sufficient for encryption and
169 * it greatly improves the encryption performance. We use
170 * Wiener's table and add a large safety margin.
172 nbits = wiener_map( orig_nbits ) * 3 / 2;
173 if( nbits >= orig_nbits )
179 nbytes = (nbits+7)/8;
181 log_debug("choosing a random k of %u bits", nbits);
182 mpi_sub_ui( p_1, p, 1);
184 if( !rndbuf || nbits < 32 ) {
186 rndbuf = get_random_bits( nbits, 1, 1 );
188 else { /* Change only some of the higher bits. */
189 /* We could impprove this by directly requesting more memory
190 * at the first call to get_random_bits() and use this the here
191 * maybe it is easier to do this directly in random.c
192 * Anyway, it is highly inlikely that we will ever reach this code
194 char *pp = get_random_bits( 32, 1, 1 );
195 memcpy( rndbuf,pp, 4 );
198 mpi_set_buffer( k, rndbuf, nbytes, 0 );
201 if( !(mpi_cmp( k, p_1 ) < 0) ) { /* check: k < (p-1) */
206 if( !(mpi_cmp_ui( k, 0 ) > 0) ) { /* check: k > 0 */
211 if( mpi_gcd( temp, k, p_1 ) )
212 goto found; /* okay, k is relatively prime to (p-1) */
213 mpi_add_ui( k, k, 1 );
229 * Generate a key pair with a key of size NBITS
230 * Returns: 2 structures filles with all needed values
231 * and an array with n-1 factors of (p-1)
234 generate( ELG_secret_key *sk, unsigned int nbits, MPI **ret_factors )
236 MPI p; /* the prime */
239 MPI x; /* the secret exponent */
246 p_min1 = mpi_alloc( (nbits+BITS_PER_MPI_LIMB-1)/BITS_PER_MPI_LIMB );
247 temp = mpi_alloc( (nbits+BITS_PER_MPI_LIMB-1)/BITS_PER_MPI_LIMB );
248 qbits = wiener_map( nbits );
249 if( qbits & 1 ) /* better have a even one */
252 p = generate_elg_prime( 0, nbits, qbits, g, ret_factors );
253 mpi_sub_ui(p_min1, p, 1);
256 /* select a random number which has these properties:
258 * This must be a very good random number because this is the
259 * secret part. The prime is public and may be shared anyway,
260 * so a random generator level of 1 is used for the prime.
262 * I don't see a reason to have a x of about the same size as the
263 * p. It should be sufficient to have one about the size of q or
264 * the later used k plus a large safety margin. Decryption will be
265 * much faster with such an x. Note that this is not optimal for
266 * signing keys becuase it makes an attack using accidential small
267 * K values even easier. Well, one should not use ElGamal signing
270 xbits = qbits * 3 / 2;
273 x = mpi_alloc_secure( xbits/BITS_PER_MPI_LIMB );
275 log_debug("choosing a random x of size %u", xbits );
280 if( rndbuf ) { /* change only some of the higher bits */
281 if( xbits < 16 ) {/* should never happen ... */
283 rndbuf = get_random_bits( xbits, 2, 1 );
286 char *r = get_random_bits( 16, 2, 1 );
287 memcpy(rndbuf, r, 16/8 );
292 rndbuf = get_random_bits( xbits, 2, 1 );
293 mpi_set_buffer( x, rndbuf, (xbits+7)/8, 0 );
294 mpi_clear_highbit( x, xbits+1 );
295 } while( !( mpi_cmp_ui( x, 0 )>0 && mpi_cmp( x, p_min1 )<0 ) );
298 y = mpi_alloc(nbits/BITS_PER_MPI_LIMB);
299 mpi_powm( y, g, x, p );
303 log_mpidump("elg p= ", p );
304 log_mpidump("elg g= ", g );
305 log_mpidump("elg y= ", y );
306 log_mpidump("elg x= ", x );
309 /* copy the stuff to the key structures */
315 /* now we can test our keys (this should never fail!) */
316 test_keys( sk, nbits - 64 );
324 * Test whether the secret key is valid.
325 * Returns: if this is a valid key.
328 check_secret_key( ELG_secret_key *sk )
331 MPI y = mpi_alloc( mpi_get_nlimbs(sk->y) );
333 mpi_powm( y, sk->g, sk->x, sk->p );
334 rc = !mpi_cmp( y, sk->y );
341 do_encrypt(MPI a, MPI b, MPI input, ELG_public_key *pkey )
345 /* Note: maybe we should change the interface, so that it
346 * is possible to check that input is < p and return an
350 k = gen_k( pkey->p, 1 );
351 mpi_powm( a, pkey->g, k, pkey->p );
352 /* b = (y^k * input) mod p
353 * = ((y^k mod p) * (input mod p)) mod p
354 * and because input is < p
355 * = ((y^k mod p) * input) mod p
357 mpi_powm( b, pkey->y, k, pkey->p );
358 mpi_mulm( b, b, input, pkey->p );
361 log_mpidump("elg encrypted y= ", pkey->y);
362 log_mpidump("elg encrypted p= ", pkey->p);
363 log_mpidump("elg encrypted k= ", k);
364 log_mpidump("elg encrypted M= ", input);
365 log_mpidump("elg encrypted a= ", a);
366 log_mpidump("elg encrypted b= ", b);
374 decrypt(MPI output, MPI a, MPI b, ELG_secret_key *skey )
376 MPI t1 = mpi_alloc_secure( mpi_get_nlimbs( skey->p ) );
378 /* output = b/(a^x) mod p */
379 mpi_powm( t1, a, skey->x, skey->p );
380 mpi_invm( t1, t1, skey->p );
381 mpi_mulm( output, b, t1, skey->p );
384 log_mpidump("elg decrypted x= ", skey->x);
385 log_mpidump("elg decrypted p= ", skey->p);
386 log_mpidump("elg decrypted a= ", a);
387 log_mpidump("elg decrypted b= ", b);
388 log_mpidump("elg decrypted M= ", output);
395 /*********************************************
396 ************** interface ******************
397 *********************************************/
400 elg_generate( int algo, unsigned nbits, MPI *skey, MPI **retfactors )
404 if( !is_ELGAMAL(algo) )
405 return G10ERR_PUBKEY_ALGO;
407 generate( &sk, nbits, retfactors );
417 elg_check_secret_key( int algo, MPI *skey )
421 if( !is_ELGAMAL(algo) )
422 return G10ERR_PUBKEY_ALGO;
423 if( !skey[0] || !skey[1] || !skey[2] || !skey[3] )
424 return G10ERR_BAD_MPI;
430 if( !check_secret_key( &sk ) )
431 return G10ERR_BAD_SECKEY;
438 elg_encrypt( int algo, MPI *resarr, MPI data, MPI *pkey )
442 if( !is_ELGAMAL(algo) )
443 return G10ERR_PUBKEY_ALGO;
444 if( !data || !pkey[0] || !pkey[1] || !pkey[2] )
445 return G10ERR_BAD_MPI;
450 resarr[0] = mpi_alloc( mpi_get_nlimbs( pk.p ) );
451 resarr[1] = mpi_alloc( mpi_get_nlimbs( pk.p ) );
452 do_encrypt( resarr[0], resarr[1], data, &pk );
457 elg_decrypt( int algo, MPI *result, MPI *data, MPI *skey )
461 if( !is_ELGAMAL(algo) )
462 return G10ERR_PUBKEY_ALGO;
463 if( !data[0] || !data[1]
464 || !skey[0] || !skey[1] || !skey[2] || !skey[3] )
465 return G10ERR_BAD_MPI;
471 *result = mpi_alloc_secure( mpi_get_nlimbs( sk.p ) );
472 decrypt( *result, data[0], data[1], &sk );
478 elg_get_nbits( int algo, MPI *pkey )
480 if( !is_ELGAMAL(algo) )
482 return mpi_get_nbits( pkey[0] );
487 * Return some information about the algorithm. We need algo here to
488 * distinguish different flavors of the algorithm.
489 * Returns: A pointer to string describing the algorithm or NULL if
490 * the ALGO is invalid.
491 * Usage: Bit 0 set : allows signing
492 * 1 set : allows encryption
495 elg_get_info( int algo, int *npkey, int *nskey, int *nenc, int *nsig,
504 case PUBKEY_ALGO_ELGAMAL_E:
505 *use = PUBKEY_USAGE_ENC;
507 default: *use = 0; return NULL;