dsa-common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /* dsa-common.c - Common code for DSA
  2. * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
  3. * Copyright (C) 2013 g10 Code GmbH
  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. #include <config.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "g10lib.h"
  25. #include "mpi.h"
  26. #include "cipher.h"
  27. #include "pubkey-internal.h"
  28. /*
  29. * Modify K, so that computation time difference can be small,
  30. * by making K large enough.
  31. *
  32. * Originally, (EC)DSA computation requires k where 0 < k < q. Here,
  33. * we add q (the order), to keep k in a range: q < k < 2*q (or,
  34. * addming more q, to keep k in a range: 2*q < k < 3*q), so that
  35. * timing difference of the EC multiply (or exponentiation) operation
  36. * can be small. The result of (EC)DSA computation is same.
  37. */
  38. void
  39. _gcry_dsa_modify_k (gcry_mpi_t k, gcry_mpi_t q, int qbits)
  40. {
  41. gcry_mpi_t k1 = mpi_new (qbits+2);
  42. mpi_resize (k, (qbits+2+BITS_PER_MPI_LIMB-1) / BITS_PER_MPI_LIMB);
  43. k->nlimbs = k->alloced;
  44. mpi_add (k, k, q);
  45. mpi_add (k1, k, q);
  46. mpi_set_cond (k, k1, !mpi_test_bit (k, qbits));
  47. mpi_free (k1);
  48. }
  49. /*
  50. * Generate a random secret exponent K less than Q.
  51. * Note that ECDSA uses this code also to generate D.
  52. */
  53. gcry_mpi_t
  54. _gcry_dsa_gen_k (gcry_mpi_t q, int security_level)
  55. {
  56. gcry_mpi_t k = mpi_alloc_secure (mpi_get_nlimbs (q));
  57. unsigned int nbits = mpi_get_nbits (q);
  58. unsigned int nbytes = (nbits+7)/8;
  59. char *rndbuf = NULL;
  60. /* To learn why we don't use mpi_mod to get the requested bit size,
  61. read the paper: "The Insecurity of the Digital Signature
  62. Algorithm with Partially Known Nonces" by Nguyen and Shparlinski.
  63. Journal of Cryptology, New York. Vol 15, nr 3 (2003) */
  64. if (DBG_CIPHER)
  65. log_debug ("choosing a random k of %u bits at seclevel %d\n",
  66. nbits, security_level);
  67. for (;;)
  68. {
  69. if ( !rndbuf || nbits < 32 )
  70. {
  71. xfree (rndbuf);
  72. rndbuf = _gcry_random_bytes_secure (nbytes, security_level);
  73. }
  74. else
  75. { /* Change only some of the higher bits. We could improve
  76. this by directly requesting more memory at the first call
  77. to get_random_bytes() and use these extra bytes here.
  78. However the required management code is more complex and
  79. thus we better use this simple method. */
  80. char *pp = _gcry_random_bytes_secure (4, security_level);
  81. memcpy (rndbuf, pp, 4);
  82. xfree (pp);
  83. }
  84. _gcry_mpi_set_buffer (k, rndbuf, nbytes, 0);
  85. /* Make sure we have the requested number of bits. This code
  86. looks a bit funny but it is easy to understand if you
  87. consider that mpi_set_highbit clears all higher bits. We
  88. don't have a clear_highbit, thus we first set the high bit
  89. and then clear it again. */
  90. if (mpi_test_bit (k, nbits-1))
  91. mpi_set_highbit (k, nbits-1);
  92. else
  93. {
  94. mpi_set_highbit (k, nbits-1);
  95. mpi_clear_bit (k, nbits-1);
  96. }
  97. if (!(mpi_cmp (k, q) < 0)) /* check: k < q */
  98. {
  99. if (DBG_CIPHER)
  100. log_debug ("\tk too large - again\n");
  101. continue; /* no */
  102. }
  103. if (!(mpi_cmp_ui (k, 0) > 0)) /* check: k > 0 */
  104. {
  105. if (DBG_CIPHER)
  106. log_debug ("\tk is zero - again\n");
  107. continue; /* no */
  108. }
  109. break; /* okay */
  110. }
  111. xfree (rndbuf);
  112. return k;
  113. }
  114. /* Turn VALUE into an octet string and store it in an allocated buffer
  115. at R_FRAME. If the resulting octet string is shorter than NBYTES
  116. the result will be left padded with zeroes. If VALUE does not fit
  117. into NBYTES an error code is returned. */
  118. static gpg_err_code_t
  119. int2octets (unsigned char **r_frame, gcry_mpi_t value, size_t nbytes)
  120. {
  121. gpg_err_code_t rc;
  122. size_t nframe, noff, n;
  123. unsigned char *frame;
  124. rc = _gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0, &nframe, value);
  125. if (rc)
  126. return rc;
  127. if (nframe > nbytes)
  128. return GPG_ERR_TOO_LARGE; /* Value too long to fit into NBYTES. */
  129. noff = (nframe < nbytes)? nbytes - nframe : 0;
  130. n = nframe + noff;
  131. frame = mpi_is_secure (value)? xtrymalloc_secure (n) : xtrymalloc (n);
  132. if (!frame)
  133. return gpg_err_code_from_syserror ();
  134. if (noff)
  135. memset (frame, 0, noff);
  136. nframe += noff;
  137. rc = _gcry_mpi_print (GCRYMPI_FMT_USG, frame+noff, nframe-noff, NULL, value);
  138. if (rc)
  139. {
  140. xfree (frame);
  141. return rc;
  142. }
  143. *r_frame = frame;
  144. return 0;
  145. }
  146. /* Connert the bit string BITS of length NBITS into an octet string
  147. with a length of (QBITS+7)/8 bytes. On success store the result at
  148. R_FRAME. */
  149. static gpg_err_code_t
  150. bits2octets (unsigned char **r_frame,
  151. const void *bits, unsigned int nbits,
  152. gcry_mpi_t q, unsigned int qbits)
  153. {
  154. gpg_err_code_t rc;
  155. gcry_mpi_t z1;
  156. /* z1 = bits2int (b) */
  157. rc = _gcry_mpi_scan (&z1, GCRYMPI_FMT_USG, bits, (nbits+7)/8, NULL);
  158. if (rc)
  159. return rc;
  160. if (nbits > qbits)
  161. mpi_rshift (z1, z1, nbits - qbits);
  162. /* z2 - z1 mod q */
  163. if (mpi_cmp (z1, q) >= 0)
  164. mpi_sub (z1, z1, q);
  165. /* Convert to an octet string. */
  166. rc = int2octets (r_frame, z1, (qbits+7)/8);
  167. mpi_free (z1);
  168. return rc;
  169. }
  170. /*
  171. * Generate a deterministic secret exponent K less than DSA_Q. H1 is
  172. * the to be signed digest with a length of HLEN bytes. HALGO is the
  173. * algorithm used to create the hash. On success the value for K is
  174. * stored at R_K.
  175. */
  176. gpg_err_code_t
  177. _gcry_dsa_gen_rfc6979_k (gcry_mpi_t *r_k,
  178. gcry_mpi_t dsa_q, gcry_mpi_t dsa_x,
  179. const unsigned char *h1, unsigned int hlen,
  180. int halgo, unsigned int extraloops)
  181. {
  182. gpg_err_code_t rc;
  183. unsigned char *V = NULL;
  184. unsigned char *K = NULL;
  185. unsigned char *x_buf = NULL;
  186. unsigned char *h1_buf = NULL;
  187. gcry_md_hd_t hd = NULL;
  188. unsigned char *t = NULL;
  189. gcry_mpi_t k = NULL;
  190. unsigned int tbits, qbits;
  191. int i;
  192. qbits = mpi_get_nbits (dsa_q);
  193. if (!qbits || !h1 || !hlen)
  194. return GPG_ERR_EINVAL;
  195. if (_gcry_md_get_algo_dlen (halgo) != hlen)
  196. return GPG_ERR_DIGEST_ALGO;
  197. /* Step b: V = 0x01 0x01 0x01 ... 0x01 */
  198. V = xtrymalloc (hlen);
  199. if (!V)
  200. {
  201. rc = gpg_err_code_from_syserror ();
  202. goto leave;
  203. }
  204. for (i=0; i < hlen; i++)
  205. V[i] = 1;
  206. /* Step c: K = 0x00 0x00 0x00 ... 0x00 */
  207. K = xtrycalloc (1, hlen);
  208. if (!K)
  209. {
  210. rc = gpg_err_code_from_syserror ();
  211. goto leave;
  212. }
  213. rc = int2octets (&x_buf, dsa_x, (qbits+7)/8);
  214. if (rc)
  215. goto leave;
  216. rc = bits2octets (&h1_buf, h1, hlen*8, dsa_q, qbits);
  217. if (rc)
  218. goto leave;
  219. /* Create a handle to compute the HMACs. */
  220. rc = _gcry_md_open (&hd, halgo, (GCRY_MD_FLAG_SECURE | GCRY_MD_FLAG_HMAC));
  221. if (rc)
  222. goto leave;
  223. /* Step d: K = HMAC_K(V || 0x00 || int2octets(x) || bits2octets(h1) */
  224. rc = _gcry_md_setkey (hd, K, hlen);
  225. if (rc)
  226. goto leave;
  227. _gcry_md_write (hd, V, hlen);
  228. _gcry_md_write (hd, "", 1);
  229. _gcry_md_write (hd, x_buf, (qbits+7)/8);
  230. _gcry_md_write (hd, h1_buf, (qbits+7)/8);
  231. memcpy (K, _gcry_md_read (hd, 0), hlen);
  232. /* Step e: V = HMAC_K(V) */
  233. rc = _gcry_md_setkey (hd, K, hlen);
  234. if (rc)
  235. goto leave;
  236. _gcry_md_write (hd, V, hlen);
  237. memcpy (V, _gcry_md_read (hd, 0), hlen);
  238. /* Step f: K = HMAC_K(V || 0x01 || int2octets(x) || bits2octets(h1) */
  239. rc = _gcry_md_setkey (hd, K, hlen);
  240. if (rc)
  241. goto leave;
  242. _gcry_md_write (hd, V, hlen);
  243. _gcry_md_write (hd, "\x01", 1);
  244. _gcry_md_write (hd, x_buf, (qbits+7)/8);
  245. _gcry_md_write (hd, h1_buf, (qbits+7)/8);
  246. memcpy (K, _gcry_md_read (hd, 0), hlen);
  247. /* Step g: V = HMAC_K(V) */
  248. rc = _gcry_md_setkey (hd, K, hlen);
  249. if (rc)
  250. goto leave;
  251. _gcry_md_write (hd, V, hlen);
  252. memcpy (V, _gcry_md_read (hd, 0), hlen);
  253. /* Step h. */
  254. t = xtrymalloc_secure ((qbits+7)/8+hlen);
  255. if (!t)
  256. {
  257. rc = gpg_err_code_from_syserror ();
  258. goto leave;
  259. }
  260. again:
  261. for (tbits = 0; tbits < qbits;)
  262. {
  263. /* V = HMAC_K(V) */
  264. rc = _gcry_md_setkey (hd, K, hlen);
  265. if (rc)
  266. goto leave;
  267. _gcry_md_write (hd, V, hlen);
  268. memcpy (V, _gcry_md_read (hd, 0), hlen);
  269. /* T = T || V */
  270. memcpy (t+(tbits+7)/8, V, hlen);
  271. tbits += 8*hlen;
  272. }
  273. /* k = bits2int (T) */
  274. mpi_free (k);
  275. k = NULL;
  276. rc = _gcry_mpi_scan (&k, GCRYMPI_FMT_USG, t, (tbits+7)/8, NULL);
  277. if (rc)
  278. goto leave;
  279. if (tbits > qbits)
  280. mpi_rshift (k, k, tbits - qbits);
  281. /* Check: k < q and k > 1 */
  282. if (!(mpi_cmp (k, dsa_q) < 0 && mpi_cmp_ui (k, 0) > 0))
  283. {
  284. /* K = HMAC_K(V || 0x00) */
  285. rc = _gcry_md_setkey (hd, K, hlen);
  286. if (rc)
  287. goto leave;
  288. _gcry_md_write (hd, V, hlen);
  289. _gcry_md_write (hd, "", 1);
  290. memcpy (K, _gcry_md_read (hd, 0), hlen);
  291. /* V = HMAC_K(V) */
  292. rc = _gcry_md_setkey (hd, K, hlen);
  293. if (rc)
  294. goto leave;
  295. _gcry_md_write (hd, V, hlen);
  296. memcpy (V, _gcry_md_read (hd, 0), hlen);
  297. goto again;
  298. }
  299. /* The caller may have requested that we introduce some extra loops.
  300. This is for example useful if the caller wants another value for
  301. K because the last returned one yielded an R of 0. Because this
  302. is very unlikely we implement it in a straightforward way. */
  303. if (extraloops)
  304. {
  305. extraloops--;
  306. /* K = HMAC_K(V || 0x00) */
  307. rc = _gcry_md_setkey (hd, K, hlen);
  308. if (rc)
  309. goto leave;
  310. _gcry_md_write (hd, V, hlen);
  311. _gcry_md_write (hd, "", 1);
  312. memcpy (K, _gcry_md_read (hd, 0), hlen);
  313. /* V = HMAC_K(V) */
  314. rc = _gcry_md_setkey (hd, K, hlen);
  315. if (rc)
  316. goto leave;
  317. _gcry_md_write (hd, V, hlen);
  318. memcpy (V, _gcry_md_read (hd, 0), hlen);
  319. goto again;
  320. }
  321. /* log_mpidump (" k", k); */
  322. leave:
  323. xfree (t);
  324. _gcry_md_close (hd);
  325. xfree (h1_buf);
  326. xfree (x_buf);
  327. xfree (K);
  328. xfree (V);
  329. if (rc)
  330. mpi_free (k);
  331. else
  332. *r_k = k;
  333. return rc;
  334. }
  335. /*
  336. * For DSA/ECDSA, as prehash function, compute hash with HASHALGO for
  337. * INPUT. Result hash value is returned in R_HASH as an opaque MPI.
  338. * Returns error code.
  339. */
  340. gpg_err_code_t
  341. _gcry_dsa_compute_hash (gcry_mpi_t *r_hash, gcry_mpi_t input, int hashalgo)
  342. {
  343. gpg_err_code_t rc = 0;
  344. size_t hlen;
  345. void *hashbuf;
  346. void *abuf;
  347. unsigned int abits;
  348. unsigned int n;
  349. hlen = _gcry_md_get_algo_dlen (hashalgo);
  350. hashbuf = xtrymalloc (hlen);
  351. if (!hashbuf)
  352. {
  353. rc = gpg_err_code_from_syserror ();
  354. return rc;
  355. }
  356. if (mpi_is_opaque (input))
  357. {
  358. abuf = mpi_get_opaque (input, &abits);
  359. n = (abits+7)/8;
  360. _gcry_md_hash_buffer (hashalgo, hashbuf, abuf, n);
  361. }
  362. else
  363. {
  364. abits = mpi_get_nbits (input);
  365. n = (abits+7)/8;
  366. abuf = xtrymalloc (n);
  367. if (!abuf)
  368. {
  369. rc = gpg_err_code_from_syserror ();
  370. xfree (hashbuf);
  371. return rc;
  372. }
  373. _gcry_mpi_to_octet_string (NULL, abuf, input, n);
  374. _gcry_md_hash_buffer (hashalgo, hashbuf, abuf, n);
  375. xfree (abuf);
  376. }
  377. *r_hash = mpi_set_opaque (NULL, hashbuf, hlen*8);
  378. if (!*r_hash)
  379. rc = GPG_ERR_INV_OBJ;
  380. return rc;
  381. }
  382. /*
  383. * Truncate opaque hash value to qbits for DSA.
  384. * Non-opaque input is not truncated, in hope that user
  385. * knows what is passed. It is not possible to correctly
  386. * trucate non-opaque inputs.
  387. */
  388. gpg_err_code_t
  389. _gcry_dsa_normalize_hash (gcry_mpi_t input,
  390. gcry_mpi_t *out,
  391. unsigned int qbits)
  392. {
  393. gpg_err_code_t rc = 0;
  394. const void *abuf;
  395. unsigned int abits;
  396. gcry_mpi_t hash;
  397. if (mpi_is_opaque (input))
  398. {
  399. abuf = mpi_get_opaque (input, &abits);
  400. rc = _gcry_mpi_scan (&hash, GCRYMPI_FMT_USG, abuf, (abits+7)/8, NULL);
  401. if (rc)
  402. return rc;
  403. if (abits > qbits)
  404. mpi_rshift (hash, hash, abits - qbits);
  405. }
  406. else
  407. hash = input;
  408. *out = hash;
  409. return rc;
  410. }