1 /* elgamal.c - ElGamal Public Key encryption
2 * Copyright (C) 1998 Free Software Foundation, Inc.
4 * For a description of the algorithm, see:
5 * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1996.
6 * ISBN 0-471-11709-9. Pages 476 ff.
8 * This file is part of GnuPG.
10 * GnuPG is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * GnuPG is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
28 #include "constants.h"
32 #include "gcryptfix.h"
34 /* #include <config.h> */
42 /* #include "util.h" */
43 /* #include "mpi.h" */
44 /* #include "cipher.h" */
51 MPI g; /* group generator */
52 MPI y; /* g^x mod p */
58 MPI g; /* group generator */
59 MPI y; /* g^x mod p */
60 MPI x; /* secret exponent */
64 static void test_keys( ELG_secret_key *sk, unsigned nbits );
65 static MPI gen_k( MPI p );
66 static void generate( ELG_secret_key *sk, unsigned nbits, MPI **factors );
67 static int check_secret_key( ELG_secret_key *sk );
68 static void encrypt(MPI a, MPI b, MPI input, ELG_public_key *pkey );
69 static void decrypt(MPI output, MPI a, MPI b, ELG_secret_key *skey );
70 static void sign(MPI a, MPI b, MPI input, ELG_secret_key *skey);
71 static int verify(MPI a, MPI b, MPI input, ELG_public_key *pkey);
82 test_keys( ELG_secret_key *sk, unsigned nbits )
85 MPI test = mpi_alloc( 0 );
86 MPI out1_a = mpi_alloc( nbits / BITS_PER_MPI_LIMB );
87 MPI out1_b = mpi_alloc( nbits / BITS_PER_MPI_LIMB );
88 MPI out2 = mpi_alloc( nbits / BITS_PER_MPI_LIMB );
94 /*mpi_set_bytes( test, nbits, get_random_byte, 0 );*/
95 { char *p = get_random_bits( nbits, 0, 0 );
96 mpi_set_buffer( test, p, (nbits+7)/8, 0 );
100 encrypt( out1_a, out1_b, test, &pk );
101 decrypt( out2, out1_a, out1_b, sk );
102 if( mpi_cmp( test, out2 ) )
103 log_fatal("ElGamal operation: encrypt, decrypt failed\n");
105 sign( out1_a, out1_b, test, sk );
106 if( !verify( out1_a, out1_b, test, &pk ) )
107 log_fatal("ElGamal operation: sign, verify failed\n");
117 * generate a random secret exponent k from prime p, so
118 * that k is relatively prime to p-1
123 MPI k = mpi_alloc_secure( 0 );
124 MPI temp = mpi_alloc( mpi_get_nlimbs(p) );
125 MPI p_1 = mpi_copy(p);
126 unsigned int nbits = mpi_get_nbits(p);
127 unsigned int nbytes = (nbits+7)/8;
131 log_debug("choosing a random k ");
132 mpi_sub_ui( p_1, p, 1);
136 if( !rndbuf || nbits < 32 ) {
138 rndbuf = get_random_bits( nbits, 1, 1 );
140 else { /* change only some of the higher bits */
141 /* we could imporove this by directly requesting more memory
142 * at the first call to get_random_bits() and use this the here
143 * maybe it is easier to do this directly in random.c */
144 char *pp = get_random_bits( 32, 1, 1 );
145 memcpy( rndbuf,pp, 4 );
148 mpi_set_buffer( k, rndbuf, nbytes, 0 );
151 /* make sure that the number is of the exact lenght */
152 if( mpi_test_bit( k, nbits-1 ) )
153 mpi_set_highbit( k, nbits-1 );
155 mpi_set_highbit( k, nbits-1 );
156 mpi_clear_bit( k, nbits-1 );
158 if( !(mpi_cmp( k, p_1 ) < 0) ) { /* check: k < (p-1) */
163 if( !(mpi_cmp_ui( k, 0 ) > 0) ) { /* check: k > 0 */
168 if( mpi_gcd( temp, k, p_1 ) )
169 goto found; /* okay, k is relatively prime to (p-1) */
170 mpi_add_ui( k, k, 1 );
184 * Generate a key pair with a key of size NBITS
185 * Returns: 2 structures filles with all needed values
186 * and an array with n-1 factors of (p-1)
189 generate( ELG_secret_key *sk, unsigned nbits, MPI **ret_factors )
191 MPI p; /* the prime */
194 MPI x; /* the secret exponent */
200 p_min1 = mpi_alloc( (nbits+BITS_PER_MPI_LIMB-1)/BITS_PER_MPI_LIMB );
201 temp = mpi_alloc( (nbits+BITS_PER_MPI_LIMB-1)/BITS_PER_MPI_LIMB );
204 else if( nbits <= 1024 )
206 else if( nbits <= 2048 )
211 p = generate_elg_prime( 0, nbits, qbits, g, ret_factors );
212 mpi_sub_ui(p_min1, p, 1);
215 /* select a random number which has these properties:
217 * This must be a very good random number because this is the
218 * secret part. The prime is public and may be shared anyway,
219 * so a random generator level of 1 is used for the prime.
221 x = mpi_alloc_secure( nbits/BITS_PER_MPI_LIMB );
223 log_debug("choosing a random x ");
228 if( rndbuf ) { /* change only some of the higher bits */
229 if( nbits < 16 ) {/* should never happen ... */
231 rndbuf = get_random_bits( nbits, 2, 1 );
234 char *r = get_random_bits( 16, 2, 1 );
235 memcpy(rndbuf, r, 16/8 );
240 rndbuf = get_random_bits( nbits, 2, 1 );
241 mpi_set_buffer( x, rndbuf, (nbits+7)/8, 0 );
242 mpi_clear_highbit( x, nbits+1 );
243 } while( !( mpi_cmp_ui( x, 0 )>0 && mpi_cmp( x, p_min1 )<0 ) );
246 y = mpi_alloc(nbits/BITS_PER_MPI_LIMB);
247 mpi_powm( y, g, x, p );
251 log_mpidump("elg p= ", p );
252 log_mpidump("elg g= ", g );
253 log_mpidump("elg y= ", y );
254 log_mpidump("elg x= ", x );
257 /* copy the stuff to the key structures */
263 /* now we can test our keys (this should never fail!) */
264 test_keys( sk, nbits - 64 );
272 * Test whether the secret key is valid.
273 * Returns: if this is a valid key.
276 check_secret_key( ELG_secret_key *sk )
279 MPI y = mpi_alloc( mpi_get_nlimbs(sk->y) );
281 mpi_powm( y, sk->g, sk->x, sk->p );
282 rc = !mpi_cmp( y, sk->y );
289 encrypt(MPI a, MPI b, MPI input, ELG_public_key *pkey )
293 /* Note: maybe we should change the interface, so that it
294 * is possible to check that input is < p and return an
298 k = gen_k( pkey->p );
299 mpi_powm( a, pkey->g, k, pkey->p );
300 /* b = (y^k * input) mod p
301 * = ((y^k mod p) * (input mod p)) mod p
302 * and because input is < p
303 * = ((y^k mod p) * input) mod p
305 mpi_powm( b, pkey->y, k, pkey->p );
306 mpi_mulm( b, b, input, pkey->p );
309 log_mpidump("elg encrypted y= ", pkey->y);
310 log_mpidump("elg encrypted p= ", pkey->p);
311 log_mpidump("elg encrypted k= ", k);
312 log_mpidump("elg encrypted M= ", input);
313 log_mpidump("elg encrypted a= ", a);
314 log_mpidump("elg encrypted b= ", b);
324 decrypt(MPI output, MPI a, MPI b, ELG_secret_key *skey )
326 MPI t1 = mpi_alloc_secure( mpi_get_nlimbs( skey->p ) );
328 /* output = b/(a^x) mod p */
330 mpi_powm( t1, a, skey->x, skey->p );
331 mpi_invm( t1, t1, skey->p );
332 mpi_mulm( output, b, t1, skey->p );
335 log_mpidump("elg decrypted x= ", skey->x);
336 log_mpidump("elg decrypted p= ", skey->p);
337 log_mpidump("elg decrypted a= ", a);
338 log_mpidump("elg decrypted b= ", b);
339 log_mpidump("elg decrypted M= ", output);
347 * Make an Elgamal signature out of INPUT
351 sign(MPI a, MPI b, MPI input, ELG_secret_key *skey )
354 MPI t = mpi_alloc( mpi_get_nlimbs(a) );
355 MPI inv = mpi_alloc( mpi_get_nlimbs(a) );
356 MPI p_1 = mpi_copy(skey->p);
359 * b = (t * inv) mod (p-1)
360 * b = (t * inv(k,(p-1),(p-1)) mod (p-1)
361 * b = (((M-x*a) mod (p-1)) * inv(k,(p-1),(p-1))) mod (p-1)
364 mpi_sub_ui(p_1, p_1, 1);
365 k = gen_k( skey->p );
366 mpi_powm( a, skey->g, k, skey->p );
367 mpi_mul(t, skey->x, a );
368 mpi_subm(t, input, t, p_1 );
369 while( mpi_is_neg(t) )
371 mpi_invm(inv, k, p_1 );
372 mpi_mulm(b, t, inv, p_1 );
376 log_mpidump("elg sign p= ", skey->p);
377 log_mpidump("elg sign g= ", skey->g);
378 log_mpidump("elg sign y= ", skey->y);
379 log_mpidump("elg sign x= ", skey->x);
380 log_mpidump("elg sign k= ", k);
381 log_mpidump("elg sign M= ", input);
382 log_mpidump("elg sign a= ", a);
383 log_mpidump("elg sign b= ", b);
394 * Returns true if the signature composed of A and B is valid.
397 verify(MPI a, MPI b, MPI input, ELG_public_key *pkey )
405 if( !(mpi_cmp_ui( a, 0 ) > 0 && mpi_cmp( a, pkey->p ) < 0) )
406 return 0; /* assertion 0 < a < p failed */
408 t1 = mpi_alloc( mpi_get_nlimbs(a) );
409 t2 = mpi_alloc( mpi_get_nlimbs(a) );
412 /* t1 = (y^a mod p) * (a^b mod p) mod p */
413 mpi_powm( t1, pkey->y, a, pkey->p );
414 mpi_powm( t2, a, b, pkey->p );
415 mpi_mulm( t1, t1, t2, pkey->p );
417 /* t2 = g ^ input mod p */
418 mpi_powm( t2, pkey->g, input, pkey->p );
420 rc = !mpi_cmp( t1, t2 );
422 /* t1 = (y^a mod p) * (a^b mod p) mod p */
423 base[0] = pkey->y; exp[0] = a;
424 base[1] = a; exp[1] = b;
425 base[2] = NULL; exp[2] = NULL;
426 mpi_mulpowm( t1, base, exp, pkey->p );
428 /* t2 = g ^ input mod p */
429 mpi_powm( t2, pkey->g, input, pkey->p );
431 rc = !mpi_cmp( t1, t2 );
433 /* t1 = g ^ - input * y ^ a * a ^ b mod p */
434 mpi_invm(t2, pkey->g, pkey->p );
435 base[0] = t2 ; exp[0] = input;
436 base[1] = pkey->y; exp[1] = a;
437 base[2] = a; exp[2] = b;
438 base[3] = NULL; exp[3] = NULL;
439 mpi_mulpowm( t1, base, exp, pkey->p );
440 rc = !mpi_cmp_ui( t1, 1 );
449 /*********************************************
450 ************** interface ******************
451 *********************************************/
454 elg_generate( int algo, unsigned nbits, MPI *skey, MPI **retfactors )
458 if( !is_ELGAMAL(algo) )
459 return G10ERR_PUBKEY_ALGO;
461 generate( &sk, nbits, retfactors );
471 elg_check_secret_key( int algo, MPI *skey )
475 if( !is_ELGAMAL(algo) )
476 return G10ERR_PUBKEY_ALGO;
477 if( !skey[0] || !skey[1] || !skey[2] || !skey[3] )
478 return G10ERR_BAD_MPI;
484 if( !check_secret_key( &sk ) )
485 return G10ERR_BAD_SECKEY;
493 elg_encrypt( int algo, MPI *resarr, MPI data, MPI *pkey )
497 if( !is_ELGAMAL(algo) )
498 return G10ERR_PUBKEY_ALGO;
499 if( !data || !pkey[0] || !pkey[1] || !pkey[2] )
500 return G10ERR_BAD_MPI;
505 resarr[0] = mpi_alloc( mpi_get_nlimbs( pk.p ) );
506 resarr[1] = mpi_alloc( mpi_get_nlimbs( pk.p ) );
507 encrypt( resarr[0], resarr[1], data, &pk );
512 elg_decrypt( int algo, MPI *result, MPI *data, MPI *skey )
516 if( !is_ELGAMAL(algo) )
517 return G10ERR_PUBKEY_ALGO;
518 if( !data[0] || !data[1]
519 || !skey[0] || !skey[1] || !skey[2] || !skey[3] )
520 return G10ERR_BAD_MPI;
526 *result = mpi_alloc_secure( mpi_get_nlimbs( sk.p ) );
527 decrypt( *result, data[0], data[1], &sk );
532 elg_sign( int algo, MPI *resarr, MPI data, MPI *skey )
536 if( !is_ELGAMAL(algo) )
537 return G10ERR_PUBKEY_ALGO;
538 if( !data || !skey[0] || !skey[1] || !skey[2] || !skey[3] )
539 return G10ERR_BAD_MPI;
545 resarr[0] = mpi_alloc( mpi_get_nlimbs( sk.p ) );
546 resarr[1] = mpi_alloc( mpi_get_nlimbs( sk.p ) );
547 sign( resarr[0], resarr[1], data, &sk );
552 elg_verify( int algo, MPI hash, MPI *data, MPI *pkey,
553 int (*cmp)(void *, MPI) UNUSED, void *opaquev UNUSED)
557 if( !is_ELGAMAL(algo) )
558 return G10ERR_PUBKEY_ALGO;
559 if( !data[0] || !data[1] || !hash
560 || !pkey[0] || !pkey[1] || !pkey[2] )
561 return G10ERR_BAD_MPI;
566 if( !verify( data[0], data[1], hash, &pk ) )
567 return G10ERR_BAD_SIGN;
574 elg_get_nbits( int algo, MPI *pkey )
576 if( !is_ELGAMAL(algo) )
578 return mpi_get_nbits( pkey[0] );
583 * Return some information about the algorithm. We need algo here to
584 * distinguish different flavors of the algorithm.
585 * Returns: A pointer to string describing the algorithm or NULL if
586 * the ALGO is invalid.
587 * Usage: Bit 0 set : allows signing
588 * 1 set : allows encryption
589 * NOTE: This function allows signing also for ELG-E, which is not
590 * okay but a bad hack to allow to work with old gpg keys. The real check
591 * is done in the gnupg ocde depending on the packet version.
594 elg_get_info( int algo, int *npkey, int *nskey, int *nenc, int *nsig,
603 case PUBKEY_ALGO_ELGAMAL:
604 *use = PUBKEY_USAGE_SIG|PUBKEY_USAGE_ENC;
606 case PUBKEY_ALGO_ELGAMAL_E:
607 *use = PUBKEY_USAGE_SIG|PUBKEY_USAGE_ENC;
609 default: *use = 0; return NULL;