ecc-ecdsa.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* ecc-ecdsa.c - Elliptic Curve ECDSA signatures
  2. * Copyright (C) 2007, 2008, 2010, 2011 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 <errno.h>
  25. #include "g10lib.h"
  26. #include "mpi.h"
  27. #include "cipher.h"
  28. #include "context.h"
  29. #include "ec-context.h"
  30. #include "pubkey-internal.h"
  31. #include "ecc-common.h"
  32. /* Compute an ECDSA signature.
  33. * Return the signature struct (r,s) from the message hash. The caller
  34. * must have allocated R and S.
  35. */
  36. gpg_err_code_t
  37. _gcry_ecc_ecdsa_sign (gcry_mpi_t input, gcry_mpi_t k_supplied, mpi_ec_t ec,
  38. gcry_mpi_t r, gcry_mpi_t s,
  39. int flags, int hashalgo)
  40. {
  41. gpg_err_code_t rc = 0;
  42. int extraloops = 0;
  43. gcry_mpi_t k, dr, sum, k_1, x;
  44. mpi_point_struct I;
  45. gcry_mpi_t hash;
  46. const void *abuf;
  47. unsigned int abits, qbits;
  48. gcry_mpi_t b; /* Random number needed for blinding. */
  49. gcry_mpi_t bi; /* multiplicative inverse of B. */
  50. gcry_mpi_t hash_computed_internally = NULL;
  51. if (DBG_CIPHER)
  52. log_mpidump ("ecdsa sign hash ", input );
  53. qbits = mpi_get_nbits (ec->n);
  54. if ((flags & PUBKEY_FLAG_PREHASH))
  55. {
  56. rc = _gcry_dsa_compute_hash (&hash_computed_internally, input, hashalgo);
  57. if (rc)
  58. return rc;
  59. input = hash_computed_internally;
  60. }
  61. /* Convert the INPUT into an MPI if needed. */
  62. rc = _gcry_dsa_normalize_hash (input, &hash, qbits);
  63. if (rc)
  64. {
  65. mpi_free (hash_computed_internally);
  66. return rc;
  67. }
  68. b = mpi_snew (qbits);
  69. bi = mpi_snew (qbits);
  70. do
  71. {
  72. _gcry_mpi_randomize (b, qbits, GCRY_WEAK_RANDOM);
  73. mpi_mod (b, b, ec->n);
  74. }
  75. while (!mpi_invm (bi, b, ec->n));
  76. k = NULL;
  77. dr = mpi_alloc (0);
  78. sum = mpi_alloc (0);
  79. k_1 = mpi_alloc (0);
  80. x = mpi_alloc (0);
  81. point_init (&I);
  82. /* Two loops to avoid R or S are zero. This is more of a joke than
  83. a real demand because the probability of them being zero is less
  84. than any hardware failure. Some specs however require it. */
  85. while (1)
  86. {
  87. while (1)
  88. {
  89. if (k_supplied)
  90. k = k_supplied;
  91. else
  92. {
  93. mpi_free (k);
  94. k = NULL;
  95. if ((flags & PUBKEY_FLAG_RFC6979) && hashalgo)
  96. {
  97. if (fips_mode () &&
  98. (hashalgo == GCRY_MD_SHAKE128
  99. || hashalgo == GCRY_MD_SHAKE256))
  100. {
  101. rc = GPG_ERR_DIGEST_ALGO;
  102. goto leave;
  103. }
  104. /* Use Pornin's method for deterministic DSA. If this
  105. flag is set, it is expected that HASH is an opaque
  106. MPI with the to be signed hash. That hash is also
  107. used as h1 from 3.2.a. */
  108. if (!mpi_is_opaque (input))
  109. {
  110. rc = GPG_ERR_CONFLICT;
  111. goto leave;
  112. }
  113. abuf = mpi_get_opaque (input, &abits);
  114. rc = _gcry_dsa_gen_rfc6979_k (&k, ec->n, ec->d,
  115. abuf, (abits+7)/8,
  116. hashalgo, extraloops);
  117. if (rc)
  118. goto leave;
  119. extraloops++;
  120. }
  121. else
  122. k = _gcry_dsa_gen_k (ec->n, GCRY_STRONG_RANDOM);
  123. }
  124. mpi_invm (k_1, k, ec->n); /* k_1 = k^(-1) mod n */
  125. _gcry_dsa_modify_k (k, ec->n, qbits);
  126. _gcry_mpi_ec_mul_point (&I, k, ec->G, ec);
  127. if (_gcry_mpi_ec_get_affine (x, NULL, &I, ec))
  128. {
  129. if (DBG_CIPHER)
  130. log_debug ("ecc sign: Failed to get affine coordinates\n");
  131. rc = GPG_ERR_BAD_SIGNATURE;
  132. goto leave;
  133. }
  134. mpi_mod (r, x, ec->n); /* r = x mod n */
  135. if (mpi_cmp_ui (r, 0))
  136. break;
  137. if (k_supplied)
  138. {
  139. rc = GPG_ERR_INV_VALUE;
  140. goto leave;
  141. }
  142. }
  143. /* Computation of dr, sum, and s are blinded with b. */
  144. mpi_mulm (dr, b, ec->d, ec->n);
  145. mpi_mulm (dr, dr, r, ec->n); /* dr = d*r mod n */
  146. mpi_mulm (sum, b, hash, ec->n);
  147. mpi_addm (sum, sum, dr, ec->n); /* sum = hash + (d*r) mod n */
  148. mpi_mulm (s, k_1, sum, ec->n); /* s = k^(-1)*(hash+(d*r)) mod n */
  149. /* Undo blinding by b^-1 */
  150. mpi_mulm (s, bi, s, ec->n);
  151. if (mpi_cmp_ui (s, 0))
  152. break;
  153. if (k_supplied)
  154. {
  155. rc = GPG_ERR_INV_VALUE;
  156. break;
  157. }
  158. }
  159. if (DBG_CIPHER)
  160. {
  161. log_mpidump ("ecdsa sign result r ", r);
  162. log_mpidump ("ecdsa sign result s ", s);
  163. }
  164. leave:
  165. mpi_free (b);
  166. mpi_free (bi);
  167. point_free (&I);
  168. mpi_free (x);
  169. mpi_free (k_1);
  170. mpi_free (sum);
  171. mpi_free (dr);
  172. if (!k_supplied)
  173. mpi_free (k);
  174. if (hash != input)
  175. mpi_free (hash);
  176. mpi_free (hash_computed_internally);
  177. return rc;
  178. }
  179. /* Verify an ECDSA signature.
  180. * Check if R and S verifies INPUT.
  181. */
  182. gpg_err_code_t
  183. _gcry_ecc_ecdsa_verify (gcry_mpi_t input, mpi_ec_t ec,
  184. gcry_mpi_t r, gcry_mpi_t s, int flags, int hashalgo)
  185. {
  186. gpg_err_code_t err = 0;
  187. gcry_mpi_t hash, h, h1, h2, x;
  188. mpi_point_struct Q, Q1, Q2;
  189. unsigned int nbits;
  190. gcry_mpi_t hash_computed_internally = NULL;
  191. if (!_gcry_mpi_ec_curve_point (ec->Q, ec))
  192. return GPG_ERR_BROKEN_PUBKEY;
  193. if( !(mpi_cmp_ui (r, 0) > 0 && mpi_cmp (r, ec->n) < 0) )
  194. return GPG_ERR_BAD_SIGNATURE; /* Assertion 0 < r < n failed. */
  195. if( !(mpi_cmp_ui (s, 0) > 0 && mpi_cmp (s, ec->n) < 0) )
  196. return GPG_ERR_BAD_SIGNATURE; /* Assertion 0 < s < n failed. */
  197. nbits = mpi_get_nbits (ec->n);
  198. if ((flags & PUBKEY_FLAG_PREHASH))
  199. {
  200. err = _gcry_dsa_compute_hash (&hash_computed_internally, input,
  201. hashalgo);
  202. if (err)
  203. return err;
  204. input = hash_computed_internally;
  205. }
  206. err = _gcry_dsa_normalize_hash (input, &hash, nbits);
  207. if (err)
  208. {
  209. mpi_free (hash_computed_internally);
  210. return err;
  211. }
  212. h = mpi_alloc (0);
  213. h1 = mpi_alloc (0);
  214. h2 = mpi_alloc (0);
  215. x = mpi_alloc (0);
  216. point_init (&Q);
  217. point_init (&Q1);
  218. point_init (&Q2);
  219. /* h = s^(-1) (mod n) */
  220. mpi_invm (h, s, ec->n);
  221. /* h1 = hash * s^(-1) (mod n) */
  222. mpi_mulm (h1, hash, h, ec->n);
  223. /* Q1 = [ hash * s^(-1) ]G */
  224. _gcry_mpi_ec_mul_point (&Q1, h1, ec->G, ec);
  225. /* h2 = r * s^(-1) (mod n) */
  226. mpi_mulm (h2, r, h, ec->n);
  227. /* Q2 = [ r * s^(-1) ]Q */
  228. _gcry_mpi_ec_mul_point (&Q2, h2, ec->Q, ec);
  229. /* Q = ([hash * s^(-1)]G) + ([r * s^(-1)]Q) */
  230. _gcry_mpi_ec_add_points (&Q, &Q1, &Q2, ec);
  231. if (!mpi_cmp_ui (Q.z, 0))
  232. {
  233. if (DBG_CIPHER)
  234. log_debug ("ecc verify: Rejected\n");
  235. err = GPG_ERR_BAD_SIGNATURE;
  236. goto leave;
  237. }
  238. if (_gcry_mpi_ec_get_affine (x, NULL, &Q, ec))
  239. {
  240. if (DBG_CIPHER)
  241. log_debug ("ecc verify: Failed to get affine coordinates\n");
  242. err = GPG_ERR_BAD_SIGNATURE;
  243. goto leave;
  244. }
  245. mpi_mod (x, x, ec->n); /* x = x mod E_n */
  246. if (mpi_cmp (x, r)) /* x != r */
  247. {
  248. if (DBG_CIPHER)
  249. {
  250. log_mpidump (" x", x);
  251. log_mpidump (" r", r);
  252. log_mpidump (" s", s);
  253. }
  254. err = GPG_ERR_BAD_SIGNATURE;
  255. goto leave;
  256. }
  257. leave:
  258. point_free (&Q2);
  259. point_free (&Q1);
  260. point_free (&Q);
  261. mpi_free (x);
  262. mpi_free (h2);
  263. mpi_free (h1);
  264. mpi_free (h);
  265. if (hash != input)
  266. mpi_free (hash);
  267. mpi_free (hash_computed_internally);
  268. return err;
  269. }