1
/* Elgamal.c - Elgamal Public Key encryption
2
* Copyright (C) 1998, 2000, 2001, 2002, 2003,
3
* 2008 Free Software Foundation, Inc.
5
* This file is part of Libgcrypt.
7
* Libgcrypt is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* Libgcrypt is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
20
* For a description of the algorithm, see:
21
* Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1996.
22
* ISBN 0-471-11709-9. Pages 476 ff.
35
gcry_mpi_t p; /* prime */
36
gcry_mpi_t g; /* group generator */
37
gcry_mpi_t y; /* g^x mod p */
43
gcry_mpi_t p; /* prime */
44
gcry_mpi_t g; /* group generator */
45
gcry_mpi_t y; /* g^x mod p */
46
gcry_mpi_t x; /* secret exponent */
50
static int test_keys (ELG_secret_key *sk, unsigned int nbits, int nodie);
51
static gcry_mpi_t gen_k (gcry_mpi_t p, int small_k);
52
static void generate (ELG_secret_key *sk, unsigned nbits, gcry_mpi_t **factors);
53
static int check_secret_key (ELG_secret_key *sk);
54
static void do_encrypt (gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input,
55
ELG_public_key *pkey);
56
static void decrypt (gcry_mpi_t output, gcry_mpi_t a, gcry_mpi_t b,
57
ELG_secret_key *skey);
58
static void sign (gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input,
59
ELG_secret_key *skey);
60
static int verify (gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input,
61
ELG_public_key *pkey);
64
static void (*progress_cb) (void *, const char *, int, int, int);
65
static void *progress_cb_data;
68
_gcry_register_pk_elg_progress (void (*cb) (void *, const char *,
73
progress_cb_data = cb_data;
81
progress_cb (progress_cb_data, "pk_elg", c, 0, 0);
86
* Michael Wiener's table on subgroup sizes to match field sizes.
87
* (floating around somewhere, probably based on the paper from
88
* Eurocrypt 96, page 332)
91
wiener_map( unsigned int n )
93
static struct { unsigned int p_n, q_n; } t[] =
94
{ /* p q attack cost */
95
{ 512, 119 }, /* 9 x 10^17 */
96
{ 768, 145 }, /* 6 x 10^21 */
97
{ 1024, 165 }, /* 7 x 10^24 */
98
{ 1280, 183 }, /* 3 x 10^27 */
99
{ 1536, 198 }, /* 7 x 10^29 */
100
{ 1792, 212 }, /* 9 x 10^31 */
101
{ 2048, 225 }, /* 8 x 10^33 */
102
{ 2304, 237 }, /* 5 x 10^35 */
103
{ 2560, 249 }, /* 3 x 10^37 */
104
{ 2816, 259 }, /* 1 x 10^39 */
105
{ 3072, 269 }, /* 3 x 10^40 */
106
{ 3328, 279 }, /* 8 x 10^41 */
107
{ 3584, 288 }, /* 2 x 10^43 */
108
{ 3840, 296 }, /* 4 x 10^44 */
109
{ 4096, 305 }, /* 7 x 10^45 */
110
{ 4352, 313 }, /* 1 x 10^47 */
111
{ 4608, 320 }, /* 2 x 10^48 */
112
{ 4864, 328 }, /* 2 x 10^49 */
113
{ 5120, 335 }, /* 3 x 10^50 */
118
for(i=0; t[i].p_n; i++ )
123
/* Not in table - use an arbitrary high number. */
128
test_keys ( ELG_secret_key *sk, unsigned int nbits, int nodie )
131
gcry_mpi_t test = gcry_mpi_new ( 0 );
132
gcry_mpi_t out1_a = gcry_mpi_new ( nbits );
133
gcry_mpi_t out1_b = gcry_mpi_new ( nbits );
134
gcry_mpi_t out2 = gcry_mpi_new ( nbits );
141
gcry_mpi_randomize ( test, nbits, GCRY_WEAK_RANDOM );
143
do_encrypt ( out1_a, out1_b, test, &pk );
144
decrypt ( out2, out1_a, out1_b, sk );
145
if ( mpi_cmp( test, out2 ) )
148
sign ( out1_a, out1_b, test, sk );
149
if ( !verify( out1_a, out1_b, test, &pk ) )
152
gcry_mpi_release ( test );
153
gcry_mpi_release ( out1_a );
154
gcry_mpi_release ( out1_b );
155
gcry_mpi_release ( out2 );
157
if (failed && !nodie)
158
log_fatal ("Elgamal test key for %s %s failed\n",
159
(failed & 1)? "encrypt+decrypt":"",
160
(failed & 2)? "sign+verify":"");
161
if (failed && DBG_CIPHER)
162
log_debug ("Elgamal test key for %s %s failed\n",
163
(failed & 1)? "encrypt+decrypt":"",
164
(failed & 2)? "sign+verify":"");
171
* Generate a random secret exponent k from prime p, so that k is
172
* relatively prime to p-1. With SMALL_K set, k will be selected for
173
* better encryption performance - this must never be used signing!
176
gen_k( gcry_mpi_t p, int small_k )
178
gcry_mpi_t k = mpi_alloc_secure( 0 );
179
gcry_mpi_t temp = mpi_alloc( mpi_get_nlimbs(p) );
180
gcry_mpi_t p_1 = mpi_copy(p);
181
unsigned int orig_nbits = mpi_get_nbits(p);
182
unsigned int nbits, nbytes;
187
/* Using a k much lesser than p is sufficient for encryption and
188
* it greatly improves the encryption performance. We use
189
* Wiener's table and add a large safety margin. */
190
nbits = wiener_map( orig_nbits ) * 3 / 2;
191
if( nbits >= orig_nbits )
198
nbytes = (nbits+7)/8;
200
log_debug("choosing a random k ");
201
mpi_sub_ui( p_1, p, 1);
204
if( !rndbuf || nbits < 32 )
207
rndbuf = gcry_random_bytes_secure( nbytes, GCRY_STRONG_RANDOM );
211
/* Change only some of the higher bits. We could improve
212
this by directly requesting more memory at the first call
213
to get_random_bytes() and use this the here maybe it is
214
easier to do this directly in random.c Anyway, it is
215
highly inlikely that we will ever reach this code. */
216
char *pp = gcry_random_bytes_secure( 4, GCRY_STRONG_RANDOM );
217
memcpy( rndbuf, pp, 4 );
220
_gcry_mpi_set_buffer( k, rndbuf, nbytes, 0 );
224
if( !(mpi_cmp( k, p_1 ) < 0) ) /* check: k < (p-1) */
230
if( !(mpi_cmp_ui( k, 0 ) > 0) ) /* check: k > 0 */
236
if (gcry_mpi_gcd( temp, k, p_1 ))
237
goto found; /* okay, k is relative prime to (p-1) */
238
mpi_add_ui( k, k, 1 );
254
* Generate a key pair with a key of size NBITS
255
* Returns: 2 structures filled with all needed values
256
* and an array with n-1 factors of (p-1)
259
generate ( ELG_secret_key *sk, unsigned int nbits, gcry_mpi_t **ret_factors )
261
gcry_mpi_t p; /* the prime */
264
gcry_mpi_t x; /* the secret exponent */
270
p_min1 = gcry_mpi_new ( nbits );
271
qbits = wiener_map( nbits );
272
if( qbits & 1 ) /* better have a even one */
275
p = _gcry_generate_elg_prime( 0, nbits, qbits, g, ret_factors );
276
mpi_sub_ui(p_min1, p, 1);
279
/* Select a random number which has these properties:
281
* This must be a very good random number because this is the
282
* secret part. The prime is public and may be shared anyway,
283
* so a random generator level of 1 is used for the prime.
285
* I don't see a reason to have a x of about the same size
286
* as the p. It should be sufficient to have one about the size
287
* of q or the later used k plus a large safety margin. Decryption
288
* will be much faster with such an x.
290
xbits = qbits * 3 / 2;
293
x = gcry_mpi_snew ( xbits );
295
log_debug("choosing a random x of size %u", xbits );
302
{ /* Change only some of the higher bits */
303
if( xbits < 16 ) /* should never happen ... */
306
rndbuf = gcry_random_bytes_secure( (xbits+7)/8,
307
GCRY_VERY_STRONG_RANDOM );
311
char *r = gcry_random_bytes_secure( 2,
312
GCRY_VERY_STRONG_RANDOM );
313
memcpy(rndbuf, r, 2 );
319
rndbuf = gcry_random_bytes_secure( (xbits+7)/8,
320
GCRY_VERY_STRONG_RANDOM );
322
_gcry_mpi_set_buffer( x, rndbuf, (xbits+7)/8, 0 );
323
mpi_clear_highbit( x, xbits+1 );
325
while( !( mpi_cmp_ui( x, 0 )>0 && mpi_cmp( x, p_min1 )<0 ) );
328
y = gcry_mpi_new (nbits);
329
gcry_mpi_powm( y, g, x, p );
334
log_mpidump("elg p= ", p );
335
log_mpidump("elg g= ", g );
336
log_mpidump("elg y= ", y );
337
log_mpidump("elg x= ", x );
340
/* Copy the stuff to the key structures */
346
gcry_mpi_release ( p_min1 );
348
/* Now we can test our keys (this should never fail!) */
349
test_keys ( sk, nbits - 64, 0 );
353
/* Generate a key pair with a key of size NBITS not using a random
354
value for the secret key but the one given as X. This is useful to
355
implement a passphrase based decryption for a public key based
356
encryption. It has appliactions in backup systems.
358
Returns: A structure filled with all needed values and an array
359
with n-1 factors of (p-1). */
360
static gcry_err_code_t
361
generate_using_x (ELG_secret_key *sk, unsigned int nbits, gcry_mpi_t x,
362
gcry_mpi_t **ret_factors )
364
gcry_mpi_t p; /* The prime. */
365
gcry_mpi_t p_min1; /* The prime minus 1. */
366
gcry_mpi_t g; /* The generator. */
367
gcry_mpi_t y; /* g^x mod p. */
376
/* Do a quick check to see whether X is suitable. */
377
xbits = mpi_get_nbits (x);
378
if ( xbits < 64 || xbits >= nbits )
379
return GPG_ERR_INV_VALUE;
381
p_min1 = gcry_mpi_new ( nbits );
382
qbits = wiener_map ( nbits );
383
if ( (qbits & 1) ) /* Better have an even one. */
386
p = _gcry_generate_elg_prime ( 0, nbits, qbits, g, ret_factors );
387
mpi_sub_ui (p_min1, p, 1);
390
log_debug ("using a supplied x of size %u", xbits );
391
if ( !(mpi_cmp_ui ( x, 0 ) > 0 && mpi_cmp ( x, p_min1 ) <0 ) )
393
gcry_mpi_release ( p_min1 );
394
gcry_mpi_release ( p );
395
gcry_mpi_release ( g );
396
return GPG_ERR_INV_VALUE;
399
y = gcry_mpi_new (nbits);
400
gcry_mpi_powm ( y, g, x, p );
405
log_mpidump ("elg p= ", p );
406
log_mpidump ("elg g= ", g );
407
log_mpidump ("elg y= ", y );
408
log_mpidump ("elg x= ", x );
411
/* Copy the stuff to the key structures */
415
sk->x = gcry_mpi_copy (x);
417
gcry_mpi_release ( p_min1 );
419
/* Now we can test our keys. */
420
if ( test_keys ( sk, nbits - 64, 1 ) )
422
gcry_mpi_release ( sk->p ); sk->p = NULL;
423
gcry_mpi_release ( sk->g ); sk->g = NULL;
424
gcry_mpi_release ( sk->y ); sk->y = NULL;
425
gcry_mpi_release ( sk->x ); sk->x = NULL;
426
return GPG_ERR_BAD_SECKEY;
434
* Test whether the secret key is valid.
435
* Returns: if this is a valid key.
438
check_secret_key( ELG_secret_key *sk )
441
gcry_mpi_t y = mpi_alloc( mpi_get_nlimbs(sk->y) );
443
gcry_mpi_powm( y, sk->g, sk->x, sk->p );
444
rc = !mpi_cmp( y, sk->y );
451
do_encrypt(gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input, ELG_public_key *pkey )
455
/* Note: maybe we should change the interface, so that it
456
* is possible to check that input is < p and return an
460
k = gen_k( pkey->p, 1 );
461
gcry_mpi_powm( a, pkey->g, k, pkey->p );
462
/* b = (y^k * input) mod p
463
* = ((y^k mod p) * (input mod p)) mod p
464
* and because input is < p
465
* = ((y^k mod p) * input) mod p
467
gcry_mpi_powm( b, pkey->y, k, pkey->p );
468
gcry_mpi_mulm( b, b, input, pkey->p );
472
log_mpidump("elg encrypted y= ", pkey->y);
473
log_mpidump("elg encrypted p= ", pkey->p);
474
log_mpidump("elg encrypted k= ", k);
475
log_mpidump("elg encrypted M= ", input);
476
log_mpidump("elg encrypted a= ", a);
477
log_mpidump("elg encrypted b= ", b);
487
decrypt(gcry_mpi_t output, gcry_mpi_t a, gcry_mpi_t b, ELG_secret_key *skey )
489
gcry_mpi_t t1 = mpi_alloc_secure( mpi_get_nlimbs( skey->p ) );
491
/* output = b/(a^x) mod p */
492
gcry_mpi_powm( t1, a, skey->x, skey->p );
493
mpi_invm( t1, t1, skey->p );
494
mpi_mulm( output, b, t1, skey->p );
498
log_mpidump("elg decrypted x= ", skey->x);
499
log_mpidump("elg decrypted p= ", skey->p);
500
log_mpidump("elg decrypted a= ", a);
501
log_mpidump("elg decrypted b= ", b);
502
log_mpidump("elg decrypted M= ", output);
510
* Make an Elgamal signature out of INPUT
514
sign(gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input, ELG_secret_key *skey )
517
gcry_mpi_t t = mpi_alloc( mpi_get_nlimbs(a) );
518
gcry_mpi_t inv = mpi_alloc( mpi_get_nlimbs(a) );
519
gcry_mpi_t p_1 = mpi_copy(skey->p);
522
* b = (t * inv) mod (p-1)
523
* b = (t * inv(k,(p-1),(p-1)) mod (p-1)
524
* b = (((M-x*a) mod (p-1)) * inv(k,(p-1),(p-1))) mod (p-1)
527
mpi_sub_ui(p_1, p_1, 1);
528
k = gen_k( skey->p, 0 /* no small K ! */ );
529
gcry_mpi_powm( a, skey->g, k, skey->p );
530
mpi_mul(t, skey->x, a );
531
mpi_subm(t, input, t, p_1 );
532
mpi_invm(inv, k, p_1 );
533
mpi_mulm(b, t, inv, p_1 );
538
log_mpidump("elg sign p= ", skey->p);
539
log_mpidump("elg sign g= ", skey->g);
540
log_mpidump("elg sign y= ", skey->y);
541
log_mpidump("elg sign x= ", skey->x);
542
log_mpidump("elg sign k= ", k);
543
log_mpidump("elg sign M= ", input);
544
log_mpidump("elg sign a= ", a);
545
log_mpidump("elg sign b= ", b);
556
* Returns true if the signature composed of A and B is valid.
559
verify(gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input, ELG_public_key *pkey )
567
if( !(mpi_cmp_ui( a, 0 ) > 0 && mpi_cmp( a, pkey->p ) < 0) )
568
return 0; /* assertion 0 < a < p failed */
570
t1 = mpi_alloc( mpi_get_nlimbs(a) );
571
t2 = mpi_alloc( mpi_get_nlimbs(a) );
574
/* t1 = (y^a mod p) * (a^b mod p) mod p */
575
gcry_mpi_powm( t1, pkey->y, a, pkey->p );
576
gcry_mpi_powm( t2, a, b, pkey->p );
577
mpi_mulm( t1, t1, t2, pkey->p );
579
/* t2 = g ^ input mod p */
580
gcry_mpi_powm( t2, pkey->g, input, pkey->p );
582
rc = !mpi_cmp( t1, t2 );
584
/* t1 = (y^a mod p) * (a^b mod p) mod p */
585
base[0] = pkey->y; ex[0] = a;
586
base[1] = a; ex[1] = b;
587
base[2] = NULL; ex[2] = NULL;
588
mpi_mulpowm( t1, base, ex, pkey->p );
590
/* t2 = g ^ input mod p */
591
gcry_mpi_powm( t2, pkey->g, input, pkey->p );
593
rc = !mpi_cmp( t1, t2 );
595
/* t1 = g ^ - input * y ^ a * a ^ b mod p */
596
mpi_invm(t2, pkey->g, pkey->p );
597
base[0] = t2 ; ex[0] = input;
598
base[1] = pkey->y; ex[1] = a;
599
base[2] = a; ex[2] = b;
600
base[3] = NULL; ex[3] = NULL;
601
mpi_mulpowm( t1, base, ex, pkey->p );
602
rc = !mpi_cmp_ui( t1, 1 );
611
/*********************************************
612
************** interface ******************
613
*********************************************/
615
static gpg_err_code_t
616
elg_generate_ext (int algo, unsigned int nbits, unsigned long evalue,
617
const gcry_sexp_t genparms,
618
gcry_mpi_t *skey, gcry_mpi_t **retfactors,
619
gcry_sexp_t *r_extrainfo)
623
gcry_mpi_t xvalue = NULL;
632
/* Parse the optional xvalue element. */
633
l1 = gcry_sexp_find_token (genparms, "xvalue", 0);
636
xvalue = gcry_sexp_nth_mpi (l1, 1, 0);
637
gcry_sexp_release (l1);
639
return GPG_ERR_BAD_MPI;
644
ec = generate_using_x (&sk, nbits, xvalue, retfactors);
647
generate (&sk, nbits, retfactors);
660
static gcry_err_code_t
661
elg_generate (int algo, unsigned int nbits, unsigned long evalue,
662
gcry_mpi_t *skey, gcry_mpi_t **retfactors)
669
generate (&sk, nbits, retfactors);
675
return GPG_ERR_NO_ERROR;
679
static gcry_err_code_t
680
elg_check_secret_key (int algo, gcry_mpi_t *skey)
682
gcry_err_code_t err = GPG_ERR_NO_ERROR;
687
if ((! skey[0]) || (! skey[1]) || (! skey[2]) || (! skey[3]))
688
err = GPG_ERR_BAD_MPI;
696
if (! check_secret_key (&sk))
697
err = GPG_ERR_BAD_SECKEY;
704
static gcry_err_code_t
705
elg_encrypt (int algo, gcry_mpi_t *resarr,
706
gcry_mpi_t data, gcry_mpi_t *pkey, int flags)
708
gcry_err_code_t err = GPG_ERR_NO_ERROR;
714
if ((! data) || (! pkey[0]) || (! pkey[1]) || (! pkey[2]))
715
err = GPG_ERR_BAD_MPI;
721
resarr[0] = mpi_alloc (mpi_get_nlimbs (pk.p));
722
resarr[1] = mpi_alloc (mpi_get_nlimbs (pk.p));
723
do_encrypt (resarr[0], resarr[1], data, &pk);
729
static gcry_err_code_t
730
elg_decrypt (int algo, gcry_mpi_t *result,
731
gcry_mpi_t *data, gcry_mpi_t *skey, int flags)
733
gcry_err_code_t err = GPG_ERR_NO_ERROR;
739
if ((! data[0]) || (! data[1])
740
|| (! skey[0]) || (! skey[1]) || (! skey[2]) || (! skey[3]))
741
err = GPG_ERR_BAD_MPI;
748
*result = mpi_alloc_secure (mpi_get_nlimbs (sk.p));
749
decrypt (*result, data[0], data[1], &sk);
755
static gcry_err_code_t
756
elg_sign (int algo, gcry_mpi_t *resarr, gcry_mpi_t data, gcry_mpi_t *skey)
758
gcry_err_code_t err = GPG_ERR_NO_ERROR;
764
|| (! skey[0]) || (! skey[1]) || (! skey[2]) || (! skey[3]))
765
err = GPG_ERR_BAD_MPI;
772
resarr[0] = mpi_alloc (mpi_get_nlimbs (sk.p));
773
resarr[1] = mpi_alloc (mpi_get_nlimbs (sk.p));
774
sign (resarr[0], resarr[1], data, &sk);
781
static gcry_err_code_t
782
elg_verify (int algo, gcry_mpi_t hash, gcry_mpi_t *data, gcry_mpi_t *pkey,
783
int (*cmp) (void *, gcry_mpi_t), void *opaquev)
785
gcry_err_code_t err = GPG_ERR_NO_ERROR;
792
if ((! data[0]) || (! data[1]) || (! hash)
793
|| (! pkey[0]) || (! pkey[1]) || (! pkey[2]))
794
err = GPG_ERR_BAD_MPI;
800
if (! verify (data[0], data[1], hash, &pk))
801
err = GPG_ERR_BAD_SIGNATURE;
809
elg_get_nbits (int algo, gcry_mpi_t *pkey)
813
return mpi_get_nbits (pkey[0]);
817
static const char *elg_names[] =
826
gcry_pk_spec_t _gcry_pubkey_spec_elg =
829
"pgy", "pgyx", "ab", "rs", "pgy",
830
GCRY_PK_USAGE_SIGN | GCRY_PK_USAGE_ENCR,
832
elg_check_secret_key,
840
pk_extra_spec_t _gcry_pubkey_extraspec_elg =