elgamal.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /* Elgamal.c - Elgamal Public Key encryption
  2. * Copyright (C) 1998, 2000, 2001, 2002, 2003,
  3. * 2008 Free Software Foundation, Inc.
  4. *
  5. * This file is part of Libgcrypt.
  6. *
  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.
  11. *
  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.
  16. *
  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/>.
  19. *
  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.
  23. */
  24. #include <config.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "g10lib.h"
  29. #include "mpi.h"
  30. #include "cipher.h"
  31. typedef struct
  32. {
  33. gcry_mpi_t p; /* prime */
  34. gcry_mpi_t g; /* group generator */
  35. gcry_mpi_t y; /* g^x mod p */
  36. } ELG_public_key;
  37. typedef struct
  38. {
  39. gcry_mpi_t p; /* prime */
  40. gcry_mpi_t g; /* group generator */
  41. gcry_mpi_t y; /* g^x mod p */
  42. gcry_mpi_t x; /* secret exponent */
  43. } ELG_secret_key;
  44. static int test_keys (ELG_secret_key *sk, unsigned int nbits, int nodie);
  45. static gcry_mpi_t gen_k (gcry_mpi_t p, int small_k);
  46. static void generate (ELG_secret_key *sk, unsigned nbits, gcry_mpi_t **factors);
  47. static int check_secret_key (ELG_secret_key *sk);
  48. static void do_encrypt (gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input,
  49. ELG_public_key *pkey);
  50. static void decrypt (gcry_mpi_t output, gcry_mpi_t a, gcry_mpi_t b,
  51. ELG_secret_key *skey);
  52. static void sign (gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input,
  53. ELG_secret_key *skey);
  54. static int verify (gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input,
  55. ELG_public_key *pkey);
  56. static void (*progress_cb) (void *, const char *, int, int, int);
  57. static void *progress_cb_data;
  58. void
  59. _gcry_register_pk_elg_progress (void (*cb) (void *, const char *,
  60. int, int, int),
  61. void *cb_data)
  62. {
  63. progress_cb = cb;
  64. progress_cb_data = cb_data;
  65. }
  66. static void
  67. progress (int c)
  68. {
  69. if (progress_cb)
  70. progress_cb (progress_cb_data, "pk_elg", c, 0, 0);
  71. }
  72. /****************
  73. * Michael Wiener's table on subgroup sizes to match field sizes.
  74. * (floating around somewhere, probably based on the paper from
  75. * Eurocrypt 96, page 332)
  76. */
  77. static unsigned int
  78. wiener_map( unsigned int n )
  79. {
  80. static struct { unsigned int p_n, q_n; } t[] =
  81. { /* p q attack cost */
  82. { 512, 119 }, /* 9 x 10^17 */
  83. { 768, 145 }, /* 6 x 10^21 */
  84. { 1024, 165 }, /* 7 x 10^24 */
  85. { 1280, 183 }, /* 3 x 10^27 */
  86. { 1536, 198 }, /* 7 x 10^29 */
  87. { 1792, 212 }, /* 9 x 10^31 */
  88. { 2048, 225 }, /* 8 x 10^33 */
  89. { 2304, 237 }, /* 5 x 10^35 */
  90. { 2560, 249 }, /* 3 x 10^37 */
  91. { 2816, 259 }, /* 1 x 10^39 */
  92. { 3072, 269 }, /* 3 x 10^40 */
  93. { 3328, 279 }, /* 8 x 10^41 */
  94. { 3584, 288 }, /* 2 x 10^43 */
  95. { 3840, 296 }, /* 4 x 10^44 */
  96. { 4096, 305 }, /* 7 x 10^45 */
  97. { 4352, 313 }, /* 1 x 10^47 */
  98. { 4608, 320 }, /* 2 x 10^48 */
  99. { 4864, 328 }, /* 2 x 10^49 */
  100. { 5120, 335 }, /* 3 x 10^50 */
  101. { 0, 0 }
  102. };
  103. int i;
  104. for(i=0; t[i].p_n; i++ )
  105. {
  106. if( n <= t[i].p_n )
  107. return t[i].q_n;
  108. }
  109. /* Not in table - use an arbitrary high number. */
  110. return n / 8 + 200;
  111. }
  112. static int
  113. test_keys ( ELG_secret_key *sk, unsigned int nbits, int nodie )
  114. {
  115. ELG_public_key pk;
  116. gcry_mpi_t test = gcry_mpi_new ( 0 );
  117. gcry_mpi_t out1_a = gcry_mpi_new ( nbits );
  118. gcry_mpi_t out1_b = gcry_mpi_new ( nbits );
  119. gcry_mpi_t out2 = gcry_mpi_new ( nbits );
  120. int failed = 0;
  121. pk.p = sk->p;
  122. pk.g = sk->g;
  123. pk.y = sk->y;
  124. gcry_mpi_randomize ( test, nbits, GCRY_WEAK_RANDOM );
  125. do_encrypt ( out1_a, out1_b, test, &pk );
  126. decrypt ( out2, out1_a, out1_b, sk );
  127. if ( mpi_cmp( test, out2 ) )
  128. failed |= 1;
  129. sign ( out1_a, out1_b, test, sk );
  130. if ( !verify( out1_a, out1_b, test, &pk ) )
  131. failed |= 2;
  132. gcry_mpi_release ( test );
  133. gcry_mpi_release ( out1_a );
  134. gcry_mpi_release ( out1_b );
  135. gcry_mpi_release ( out2 );
  136. if (failed && !nodie)
  137. log_fatal ("Elgamal test key for %s %s failed\n",
  138. (failed & 1)? "encrypt+decrypt":"",
  139. (failed & 2)? "sign+verify":"");
  140. if (failed && DBG_CIPHER)
  141. log_debug ("Elgamal test key for %s %s failed\n",
  142. (failed & 1)? "encrypt+decrypt":"",
  143. (failed & 2)? "sign+verify":"");
  144. return failed;
  145. }
  146. /****************
  147. * Generate a random secret exponent k from prime p, so that k is
  148. * relatively prime to p-1. With SMALL_K set, k will be selected for
  149. * better encryption performance - this must never be used signing!
  150. */
  151. static gcry_mpi_t
  152. gen_k( gcry_mpi_t p, int small_k )
  153. {
  154. gcry_mpi_t k = mpi_alloc_secure( 0 );
  155. gcry_mpi_t temp = mpi_alloc( mpi_get_nlimbs(p) );
  156. gcry_mpi_t p_1 = mpi_copy(p);
  157. unsigned int orig_nbits = mpi_get_nbits(p);
  158. unsigned int nbits, nbytes;
  159. char *rndbuf = NULL;
  160. if (small_k)
  161. {
  162. /* Using a k much lesser than p is sufficient for encryption and
  163. * it greatly improves the encryption performance. We use
  164. * Wiener's table and add a large safety margin. */
  165. nbits = wiener_map( orig_nbits ) * 3 / 2;
  166. if( nbits >= orig_nbits )
  167. BUG();
  168. }
  169. else
  170. nbits = orig_nbits;
  171. nbytes = (nbits+7)/8;
  172. if( DBG_CIPHER )
  173. log_debug("choosing a random k ");
  174. mpi_sub_ui( p_1, p, 1);
  175. for(;;)
  176. {
  177. if( !rndbuf || nbits < 32 )
  178. {
  179. gcry_free(rndbuf);
  180. rndbuf = gcry_random_bytes_secure( nbytes, GCRY_STRONG_RANDOM );
  181. }
  182. else
  183. {
  184. /* Change only some of the higher bits. We could improve
  185. this by directly requesting more memory at the first call
  186. to get_random_bytes() and use this the here maybe it is
  187. easier to do this directly in random.c Anyway, it is
  188. highly inlikely that we will ever reach this code. */
  189. char *pp = gcry_random_bytes_secure( 4, GCRY_STRONG_RANDOM );
  190. memcpy( rndbuf, pp, 4 );
  191. gcry_free(pp);
  192. }
  193. _gcry_mpi_set_buffer( k, rndbuf, nbytes, 0 );
  194. for(;;)
  195. {
  196. if( !(mpi_cmp( k, p_1 ) < 0) ) /* check: k < (p-1) */
  197. {
  198. if( DBG_CIPHER )
  199. progress('+');
  200. break; /* no */
  201. }
  202. if( !(mpi_cmp_ui( k, 0 ) > 0) ) /* check: k > 0 */
  203. {
  204. if( DBG_CIPHER )
  205. progress('-');
  206. break; /* no */
  207. }
  208. if (gcry_mpi_gcd( temp, k, p_1 ))
  209. goto found; /* okay, k is relative prime to (p-1) */
  210. mpi_add_ui( k, k, 1 );
  211. if( DBG_CIPHER )
  212. progress('.');
  213. }
  214. }
  215. found:
  216. gcry_free(rndbuf);
  217. if( DBG_CIPHER )
  218. progress('\n');
  219. mpi_free(p_1);
  220. mpi_free(temp);
  221. return k;
  222. }
  223. /****************
  224. * Generate a key pair with a key of size NBITS
  225. * Returns: 2 structures filled with all needed values
  226. * and an array with n-1 factors of (p-1)
  227. */
  228. static void
  229. generate ( ELG_secret_key *sk, unsigned int nbits, gcry_mpi_t **ret_factors )
  230. {
  231. gcry_mpi_t p; /* the prime */
  232. gcry_mpi_t p_min1;
  233. gcry_mpi_t g;
  234. gcry_mpi_t x; /* the secret exponent */
  235. gcry_mpi_t y;
  236. unsigned int qbits;
  237. unsigned int xbits;
  238. byte *rndbuf;
  239. p_min1 = gcry_mpi_new ( nbits );
  240. qbits = wiener_map( nbits );
  241. if( qbits & 1 ) /* better have a even one */
  242. qbits++;
  243. g = mpi_alloc(1);
  244. p = _gcry_generate_elg_prime( 0, nbits, qbits, g, ret_factors );
  245. mpi_sub_ui(p_min1, p, 1);
  246. /* Select a random number which has these properties:
  247. * 0 < x < p-1
  248. * This must be a very good random number because this is the
  249. * secret part. The prime is public and may be shared anyway,
  250. * so a random generator level of 1 is used for the prime.
  251. *
  252. * I don't see a reason to have a x of about the same size
  253. * as the p. It should be sufficient to have one about the size
  254. * of q or the later used k plus a large safety margin. Decryption
  255. * will be much faster with such an x.
  256. */
  257. xbits = qbits * 3 / 2;
  258. if( xbits >= nbits )
  259. BUG();
  260. x = gcry_mpi_snew ( xbits );
  261. if( DBG_CIPHER )
  262. log_debug("choosing a random x of size %u", xbits );
  263. rndbuf = NULL;
  264. do
  265. {
  266. if( DBG_CIPHER )
  267. progress('.');
  268. if( rndbuf )
  269. { /* Change only some of the higher bits */
  270. if( xbits < 16 ) /* should never happen ... */
  271. {
  272. gcry_free(rndbuf);
  273. rndbuf = gcry_random_bytes_secure( (xbits+7)/8,
  274. GCRY_VERY_STRONG_RANDOM );
  275. }
  276. else
  277. {
  278. char *r = gcry_random_bytes_secure( 2,
  279. GCRY_VERY_STRONG_RANDOM );
  280. memcpy(rndbuf, r, 2 );
  281. gcry_free(r);
  282. }
  283. }
  284. else
  285. {
  286. rndbuf = gcry_random_bytes_secure( (xbits+7)/8,
  287. GCRY_VERY_STRONG_RANDOM );
  288. }
  289. _gcry_mpi_set_buffer( x, rndbuf, (xbits+7)/8, 0 );
  290. mpi_clear_highbit( x, xbits+1 );
  291. }
  292. while( !( mpi_cmp_ui( x, 0 )>0 && mpi_cmp( x, p_min1 )<0 ) );
  293. gcry_free(rndbuf);
  294. y = gcry_mpi_new (nbits);
  295. gcry_mpi_powm( y, g, x, p );
  296. if( DBG_CIPHER )
  297. {
  298. progress('\n');
  299. log_mpidump("elg p= ", p );
  300. log_mpidump("elg g= ", g );
  301. log_mpidump("elg y= ", y );
  302. log_mpidump("elg x= ", x );
  303. }
  304. /* Copy the stuff to the key structures */
  305. sk->p = p;
  306. sk->g = g;
  307. sk->y = y;
  308. sk->x = x;
  309. gcry_mpi_release ( p_min1 );
  310. /* Now we can test our keys (this should never fail!) */
  311. test_keys ( sk, nbits - 64, 0 );
  312. }
  313. /* Generate a key pair with a key of size NBITS not using a random
  314. value for the secret key but the one given as X. This is useful to
  315. implement a passphrase based decryption for a public key based
  316. encryption. It has appliactions in backup systems.
  317. Returns: A structure filled with all needed values and an array
  318. with n-1 factors of (p-1). */
  319. static gcry_err_code_t
  320. generate_using_x (ELG_secret_key *sk, unsigned int nbits, gcry_mpi_t x,
  321. gcry_mpi_t **ret_factors )
  322. {
  323. gcry_mpi_t p; /* The prime. */
  324. gcry_mpi_t p_min1; /* The prime minus 1. */
  325. gcry_mpi_t g; /* The generator. */
  326. gcry_mpi_t y; /* g^x mod p. */
  327. unsigned int qbits;
  328. unsigned int xbits;
  329. sk->p = NULL;
  330. sk->g = NULL;
  331. sk->y = NULL;
  332. sk->x = NULL;
  333. /* Do a quick check to see whether X is suitable. */
  334. xbits = mpi_get_nbits (x);
  335. if ( xbits < 64 || xbits >= nbits )
  336. return GPG_ERR_INV_VALUE;
  337. p_min1 = gcry_mpi_new ( nbits );
  338. qbits = wiener_map ( nbits );
  339. if ( (qbits & 1) ) /* Better have an even one. */
  340. qbits++;
  341. g = mpi_alloc (1);
  342. p = _gcry_generate_elg_prime ( 0, nbits, qbits, g, ret_factors );
  343. mpi_sub_ui (p_min1, p, 1);
  344. if (DBG_CIPHER)
  345. log_debug ("using a supplied x of size %u", xbits );
  346. if ( !(mpi_cmp_ui ( x, 0 ) > 0 && mpi_cmp ( x, p_min1 ) <0 ) )
  347. {
  348. gcry_mpi_release ( p_min1 );
  349. gcry_mpi_release ( p );
  350. gcry_mpi_release ( g );
  351. return GPG_ERR_INV_VALUE;
  352. }
  353. y = gcry_mpi_new (nbits);
  354. gcry_mpi_powm ( y, g, x, p );
  355. if ( DBG_CIPHER )
  356. {
  357. progress ('\n');
  358. log_mpidump ("elg p= ", p );
  359. log_mpidump ("elg g= ", g );
  360. log_mpidump ("elg y= ", y );
  361. log_mpidump ("elg x= ", x );
  362. }
  363. /* Copy the stuff to the key structures */
  364. sk->p = p;
  365. sk->g = g;
  366. sk->y = y;
  367. sk->x = gcry_mpi_copy (x);
  368. gcry_mpi_release ( p_min1 );
  369. /* Now we can test our keys. */
  370. if ( test_keys ( sk, nbits - 64, 1 ) )
  371. {
  372. gcry_mpi_release ( sk->p ); sk->p = NULL;
  373. gcry_mpi_release ( sk->g ); sk->g = NULL;
  374. gcry_mpi_release ( sk->y ); sk->y = NULL;
  375. gcry_mpi_release ( sk->x ); sk->x = NULL;
  376. return GPG_ERR_BAD_SECKEY;
  377. }
  378. return 0;
  379. }
  380. /****************
  381. * Test whether the secret key is valid.
  382. * Returns: if this is a valid key.
  383. */
  384. static int
  385. check_secret_key( ELG_secret_key *sk )
  386. {
  387. int rc;
  388. gcry_mpi_t y = mpi_alloc( mpi_get_nlimbs(sk->y) );
  389. gcry_mpi_powm( y, sk->g, sk->x, sk->p );
  390. rc = !mpi_cmp( y, sk->y );
  391. mpi_free( y );
  392. return rc;
  393. }
  394. static void
  395. do_encrypt(gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input, ELG_public_key *pkey )
  396. {
  397. gcry_mpi_t k;
  398. /* Note: maybe we should change the interface, so that it
  399. * is possible to check that input is < p and return an
  400. * error code.
  401. */
  402. k = gen_k( pkey->p, 1 );
  403. gcry_mpi_powm( a, pkey->g, k, pkey->p );
  404. /* b = (y^k * input) mod p
  405. * = ((y^k mod p) * (input mod p)) mod p
  406. * and because input is < p
  407. * = ((y^k mod p) * input) mod p
  408. */
  409. gcry_mpi_powm( b, pkey->y, k, pkey->p );
  410. gcry_mpi_mulm( b, b, input, pkey->p );
  411. #if 0
  412. if( DBG_CIPHER )
  413. {
  414. log_mpidump("elg encrypted y= ", pkey->y);
  415. log_mpidump("elg encrypted p= ", pkey->p);
  416. log_mpidump("elg encrypted k= ", k);
  417. log_mpidump("elg encrypted M= ", input);
  418. log_mpidump("elg encrypted a= ", a);
  419. log_mpidump("elg encrypted b= ", b);
  420. }
  421. #endif
  422. mpi_free(k);
  423. }
  424. static void
  425. decrypt(gcry_mpi_t output, gcry_mpi_t a, gcry_mpi_t b, ELG_secret_key *skey )
  426. {
  427. gcry_mpi_t t1 = mpi_alloc_secure( mpi_get_nlimbs( skey->p ) );
  428. /* output = b/(a^x) mod p */
  429. gcry_mpi_powm( t1, a, skey->x, skey->p );
  430. mpi_invm( t1, t1, skey->p );
  431. mpi_mulm( output, b, t1, skey->p );
  432. #if 0
  433. if( DBG_CIPHER )
  434. {
  435. log_mpidump("elg decrypted x= ", skey->x);
  436. log_mpidump("elg decrypted p= ", skey->p);
  437. log_mpidump("elg decrypted a= ", a);
  438. log_mpidump("elg decrypted b= ", b);
  439. log_mpidump("elg decrypted M= ", output);
  440. }
  441. #endif
  442. mpi_free(t1);
  443. }
  444. /****************
  445. * Make an Elgamal signature out of INPUT
  446. */
  447. static void
  448. sign(gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input, ELG_secret_key *skey )
  449. {
  450. gcry_mpi_t k;
  451. gcry_mpi_t t = mpi_alloc( mpi_get_nlimbs(a) );
  452. gcry_mpi_t inv = mpi_alloc( mpi_get_nlimbs(a) );
  453. gcry_mpi_t p_1 = mpi_copy(skey->p);
  454. /*
  455. * b = (t * inv) mod (p-1)
  456. * b = (t * inv(k,(p-1),(p-1)) mod (p-1)
  457. * b = (((M-x*a) mod (p-1)) * inv(k,(p-1),(p-1))) mod (p-1)
  458. *
  459. */
  460. mpi_sub_ui(p_1, p_1, 1);
  461. k = gen_k( skey->p, 0 /* no small K ! */ );
  462. gcry_mpi_powm( a, skey->g, k, skey->p );
  463. mpi_mul(t, skey->x, a );
  464. mpi_subm(t, input, t, p_1 );
  465. mpi_invm(inv, k, p_1 );
  466. mpi_mulm(b, t, inv, p_1 );
  467. #if 0
  468. if( DBG_CIPHER )
  469. {
  470. log_mpidump("elg sign p= ", skey->p);
  471. log_mpidump("elg sign g= ", skey->g);
  472. log_mpidump("elg sign y= ", skey->y);
  473. log_mpidump("elg sign x= ", skey->x);
  474. log_mpidump("elg sign k= ", k);
  475. log_mpidump("elg sign M= ", input);
  476. log_mpidump("elg sign a= ", a);
  477. log_mpidump("elg sign b= ", b);
  478. }
  479. #endif
  480. mpi_free(k);
  481. mpi_free(t);
  482. mpi_free(inv);
  483. mpi_free(p_1);
  484. }
  485. /****************
  486. * Returns true if the signature composed of A and B is valid.
  487. */
  488. static int
  489. verify(gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input, ELG_public_key *pkey )
  490. {
  491. int rc;
  492. gcry_mpi_t t1;
  493. gcry_mpi_t t2;
  494. gcry_mpi_t base[4];
  495. gcry_mpi_t ex[4];
  496. if( !(mpi_cmp_ui( a, 0 ) > 0 && mpi_cmp( a, pkey->p ) < 0) )
  497. return 0; /* assertion 0 < a < p failed */
  498. t1 = mpi_alloc( mpi_get_nlimbs(a) );
  499. t2 = mpi_alloc( mpi_get_nlimbs(a) );
  500. #if 0
  501. /* t1 = (y^a mod p) * (a^b mod p) mod p */
  502. gcry_mpi_powm( t1, pkey->y, a, pkey->p );
  503. gcry_mpi_powm( t2, a, b, pkey->p );
  504. mpi_mulm( t1, t1, t2, pkey->p );
  505. /* t2 = g ^ input mod p */
  506. gcry_mpi_powm( t2, pkey->g, input, pkey->p );
  507. rc = !mpi_cmp( t1, t2 );
  508. #elif 0
  509. /* t1 = (y^a mod p) * (a^b mod p) mod p */
  510. base[0] = pkey->y; ex[0] = a;
  511. base[1] = a; ex[1] = b;
  512. base[2] = NULL; ex[2] = NULL;
  513. mpi_mulpowm( t1, base, ex, pkey->p );
  514. /* t2 = g ^ input mod p */
  515. gcry_mpi_powm( t2, pkey->g, input, pkey->p );
  516. rc = !mpi_cmp( t1, t2 );
  517. #else
  518. /* t1 = g ^ - input * y ^ a * a ^ b mod p */
  519. mpi_invm(t2, pkey->g, pkey->p );
  520. base[0] = t2 ; ex[0] = input;
  521. base[1] = pkey->y; ex[1] = a;
  522. base[2] = a; ex[2] = b;
  523. base[3] = NULL; ex[3] = NULL;
  524. mpi_mulpowm( t1, base, ex, pkey->p );
  525. rc = !mpi_cmp_ui( t1, 1 );
  526. #endif
  527. mpi_free(t1);
  528. mpi_free(t2);
  529. return rc;
  530. }
  531. /*********************************************
  532. ************** interface ******************
  533. *********************************************/
  534. static gpg_err_code_t
  535. elg_generate_ext (int algo, unsigned int nbits, unsigned long evalue,
  536. const gcry_sexp_t genparms,
  537. gcry_mpi_t *skey, gcry_mpi_t **retfactors,
  538. gcry_sexp_t *r_extrainfo)
  539. {
  540. gpg_err_code_t ec;
  541. ELG_secret_key sk;
  542. gcry_mpi_t xvalue = NULL;
  543. gcry_sexp_t l1;
  544. (void)algo;
  545. (void)evalue;
  546. (void)r_extrainfo;
  547. if (genparms)
  548. {
  549. /* Parse the optional xvalue element. */
  550. l1 = gcry_sexp_find_token (genparms, "xvalue", 0);
  551. if (l1)
  552. {
  553. xvalue = gcry_sexp_nth_mpi (l1, 1, 0);
  554. gcry_sexp_release (l1);
  555. if (!xvalue)
  556. return GPG_ERR_BAD_MPI;
  557. }
  558. }
  559. if (xvalue)
  560. ec = generate_using_x (&sk, nbits, xvalue, retfactors);
  561. else
  562. {
  563. generate (&sk, nbits, retfactors);
  564. ec = 0;
  565. }
  566. skey[0] = sk.p;
  567. skey[1] = sk.g;
  568. skey[2] = sk.y;
  569. skey[3] = sk.x;
  570. return ec;
  571. }
  572. static gcry_err_code_t
  573. elg_generate (int algo, unsigned int nbits, unsigned long evalue,
  574. gcry_mpi_t *skey, gcry_mpi_t **retfactors)
  575. {
  576. ELG_secret_key sk;
  577. (void)algo;
  578. (void)evalue;
  579. generate (&sk, nbits, retfactors);
  580. skey[0] = sk.p;
  581. skey[1] = sk.g;
  582. skey[2] = sk.y;
  583. skey[3] = sk.x;
  584. return GPG_ERR_NO_ERROR;
  585. }
  586. static gcry_err_code_t
  587. elg_check_secret_key (int algo, gcry_mpi_t *skey)
  588. {
  589. gcry_err_code_t err = GPG_ERR_NO_ERROR;
  590. ELG_secret_key sk;
  591. (void)algo;
  592. if ((! skey[0]) || (! skey[1]) || (! skey[2]) || (! skey[3]))
  593. err = GPG_ERR_BAD_MPI;
  594. else
  595. {
  596. sk.p = skey[0];
  597. sk.g = skey[1];
  598. sk.y = skey[2];
  599. sk.x = skey[3];
  600. if (! check_secret_key (&sk))
  601. err = GPG_ERR_BAD_SECKEY;
  602. }
  603. return err;
  604. }
  605. static gcry_err_code_t
  606. elg_encrypt (int algo, gcry_mpi_t *resarr,
  607. gcry_mpi_t data, gcry_mpi_t *pkey, int flags)
  608. {
  609. gcry_err_code_t err = GPG_ERR_NO_ERROR;
  610. ELG_public_key pk;
  611. (void)algo;
  612. (void)flags;
  613. if ((! data) || (! pkey[0]) || (! pkey[1]) || (! pkey[2]))
  614. err = GPG_ERR_BAD_MPI;
  615. else
  616. {
  617. pk.p = pkey[0];
  618. pk.g = pkey[1];
  619. pk.y = pkey[2];
  620. resarr[0] = mpi_alloc (mpi_get_nlimbs (pk.p));
  621. resarr[1] = mpi_alloc (mpi_get_nlimbs (pk.p));
  622. do_encrypt (resarr[0], resarr[1], data, &pk);
  623. }
  624. return err;
  625. }
  626. static gcry_err_code_t
  627. elg_decrypt (int algo, gcry_mpi_t *result,
  628. gcry_mpi_t *data, gcry_mpi_t *skey, int flags)
  629. {
  630. gcry_err_code_t err = GPG_ERR_NO_ERROR;
  631. ELG_secret_key sk;
  632. (void)algo;
  633. (void)flags;
  634. if ((! data[0]) || (! data[1])
  635. || (! skey[0]) || (! skey[1]) || (! skey[2]) || (! skey[3]))
  636. err = GPG_ERR_BAD_MPI;
  637. else
  638. {
  639. sk.p = skey[0];
  640. sk.g = skey[1];
  641. sk.y = skey[2];
  642. sk.x = skey[3];
  643. *result = mpi_alloc_secure (mpi_get_nlimbs (sk.p));
  644. decrypt (*result, data[0], data[1], &sk);
  645. }
  646. return err;
  647. }
  648. static gcry_err_code_t
  649. elg_sign (int algo, gcry_mpi_t *resarr, gcry_mpi_t data, gcry_mpi_t *skey)
  650. {
  651. gcry_err_code_t err = GPG_ERR_NO_ERROR;
  652. ELG_secret_key sk;
  653. (void)algo;
  654. if ((! data)
  655. || (! skey[0]) || (! skey[1]) || (! skey[2]) || (! skey[3]))
  656. err = GPG_ERR_BAD_MPI;
  657. else
  658. {
  659. sk.p = skey[0];
  660. sk.g = skey[1];
  661. sk.y = skey[2];
  662. sk.x = skey[3];
  663. resarr[0] = mpi_alloc (mpi_get_nlimbs (sk.p));
  664. resarr[1] = mpi_alloc (mpi_get_nlimbs (sk.p));
  665. sign (resarr[0], resarr[1], data, &sk);
  666. }
  667. return err;
  668. }
  669. static gcry_err_code_t
  670. elg_verify (int algo, gcry_mpi_t hash, gcry_mpi_t *data, gcry_mpi_t *pkey,
  671. int (*cmp) (void *, gcry_mpi_t), void *opaquev)
  672. {
  673. gcry_err_code_t err = GPG_ERR_NO_ERROR;
  674. ELG_public_key pk;
  675. (void)algo;
  676. (void)cmp;
  677. (void)opaquev;
  678. if ((! data[0]) || (! data[1]) || (! hash)
  679. || (! pkey[0]) || (! pkey[1]) || (! pkey[2]))
  680. err = GPG_ERR_BAD_MPI;
  681. else
  682. {
  683. pk.p = pkey[0];
  684. pk.g = pkey[1];
  685. pk.y = pkey[2];
  686. if (! verify (data[0], data[1], hash, &pk))
  687. err = GPG_ERR_BAD_SIGNATURE;
  688. }
  689. return err;
  690. }
  691. static unsigned int
  692. elg_get_nbits (int algo, gcry_mpi_t *pkey)
  693. {
  694. (void)algo;
  695. return mpi_get_nbits (pkey[0]);
  696. }
  697. static const char *elg_names[] =
  698. {
  699. "elg",
  700. "openpgp-elg",
  701. "openpgp-elg-sig",
  702. NULL,
  703. };
  704. gcry_pk_spec_t _gcry_pubkey_spec_elg =
  705. {
  706. "ELG", elg_names,
  707. "pgy", "pgyx", "ab", "rs", "pgy",
  708. GCRY_PK_USAGE_SIGN | GCRY_PK_USAGE_ENCR,
  709. elg_generate,
  710. elg_check_secret_key,
  711. elg_encrypt,
  712. elg_decrypt,
  713. elg_sign,
  714. elg_verify,
  715. elg_get_nbits
  716. };
  717. pk_extra_spec_t _gcry_pubkey_extraspec_elg =
  718. {
  719. NULL,
  720. elg_generate_ext,
  721. NULL
  722. };