ecc-gost.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* ecc-gots.c - Elliptic Curve GOST signatures
  2. * Copyright (C) 2007, 2008, 2010, 2011 Free Software Foundation, Inc.
  3. * Copyright (C) 2013 Dmitry Eremin-Solenikov
  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 "ecc-common.h"
  31. #include "pubkey-internal.h"
  32. /* Compute an GOST R 34.10-01/-12 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_gost_sign (gcry_mpi_t input, mpi_ec_t ec,
  38. gcry_mpi_t r, gcry_mpi_t s)
  39. {
  40. gpg_err_code_t rc = 0;
  41. gcry_mpi_t k, dr, sum, ke, x, e;
  42. mpi_point_struct I;
  43. gcry_mpi_t hash;
  44. unsigned int qbits;
  45. if (DBG_CIPHER)
  46. log_mpidump ("gost sign hash ", input );
  47. qbits = mpi_get_nbits (ec->n);
  48. /* Convert the INPUT into an MPI if needed. */
  49. rc = _gcry_dsa_normalize_hash (input, &hash, qbits);
  50. if (rc)
  51. return rc;
  52. k = NULL;
  53. dr = mpi_alloc (0);
  54. sum = mpi_alloc (0);
  55. ke = mpi_alloc (0);
  56. e = mpi_alloc (0);
  57. x = mpi_alloc (0);
  58. point_init (&I);
  59. mpi_mod (e, input, ec->n); /* e = hash mod n */
  60. if (!mpi_cmp_ui (e, 0))
  61. mpi_set_ui (e, 1);
  62. /* Two loops to avoid R or S are zero. This is more of a joke than
  63. a real demand because the probability of them being zero is less
  64. than any hardware failure. Some specs however require it. */
  65. do
  66. {
  67. do
  68. {
  69. mpi_free (k);
  70. k = _gcry_dsa_gen_k (ec->n, GCRY_STRONG_RANDOM);
  71. _gcry_dsa_modify_k (k, ec->n, qbits);
  72. _gcry_mpi_ec_mul_point (&I, k, ec->G, ec);
  73. if (_gcry_mpi_ec_get_affine (x, NULL, &I, ec))
  74. {
  75. if (DBG_CIPHER)
  76. log_debug ("ecc sign: Failed to get affine coordinates\n");
  77. rc = GPG_ERR_BAD_SIGNATURE;
  78. goto leave;
  79. }
  80. mpi_mod (r, x, ec->n); /* r = x mod n */
  81. }
  82. while (!mpi_cmp_ui (r, 0));
  83. mpi_mulm (dr, ec->d, r, ec->n); /* dr = d*r mod n */
  84. mpi_mulm (ke, k, e, ec->n); /* ke = k*e mod n */
  85. mpi_addm (s, ke, dr, ec->n); /* sum = (k*e+ d*r) mod n */
  86. }
  87. while (!mpi_cmp_ui (s, 0));
  88. if (DBG_CIPHER)
  89. {
  90. log_mpidump ("gost sign result r ", r);
  91. log_mpidump ("gost sign result s ", s);
  92. }
  93. leave:
  94. point_free (&I);
  95. mpi_free (x);
  96. mpi_free (e);
  97. mpi_free (ke);
  98. mpi_free (sum);
  99. mpi_free (dr);
  100. mpi_free (k);
  101. if (hash != input)
  102. mpi_free (hash);
  103. return rc;
  104. }
  105. /* Verify a GOST R 34.10-01/-12 signature.
  106. * Check if R and S verifies INPUT.
  107. */
  108. gpg_err_code_t
  109. _gcry_ecc_gost_verify (gcry_mpi_t input, mpi_ec_t ec,
  110. gcry_mpi_t r, gcry_mpi_t s)
  111. {
  112. gpg_err_code_t err = 0;
  113. gcry_mpi_t e, x, z1, z2, v, rv, zero;
  114. mpi_point_struct Q, Q1, Q2;
  115. if (!_gcry_mpi_ec_curve_point (ec->Q, ec))
  116. return GPG_ERR_BROKEN_PUBKEY;
  117. if( !(mpi_cmp_ui (r, 0) > 0 && mpi_cmp (r, ec->n) < 0) )
  118. return GPG_ERR_BAD_SIGNATURE; /* Assertion 0 < r < n failed. */
  119. if( !(mpi_cmp_ui (s, 0) > 0 && mpi_cmp (s, ec->n) < 0) )
  120. return GPG_ERR_BAD_SIGNATURE; /* Assertion 0 < s < n failed. */
  121. x = mpi_alloc (0);
  122. e = mpi_alloc (0);
  123. z1 = mpi_alloc (0);
  124. z2 = mpi_alloc (0);
  125. v = mpi_alloc (0);
  126. rv = mpi_alloc (0);
  127. zero = mpi_alloc (0);
  128. point_init (&Q);
  129. point_init (&Q1);
  130. point_init (&Q2);
  131. mpi_mod (e, input, ec->n); /* e = hash mod n */
  132. if (!mpi_cmp_ui (e, 0))
  133. mpi_set_ui (e, 1);
  134. mpi_invm (v, e, ec->n); /* v = e^(-1) (mod n) */
  135. mpi_mulm (z1, s, v, ec->n); /* z1 = s*v (mod n) */
  136. mpi_mulm (rv, r, v, ec->n); /* rv = r*v (mod n) */
  137. mpi_subm (z2, zero, rv, ec->n); /* z2 = -r*v (mod n) */
  138. _gcry_mpi_ec_mul_point (&Q1, z1, ec->G, ec);
  139. /* log_mpidump ("Q1.x", Q1.x); */
  140. /* log_mpidump ("Q1.y", Q1.y); */
  141. /* log_mpidump ("Q1.z", Q1.z); */
  142. _gcry_mpi_ec_mul_point (&Q2, z2, ec->Q, ec);
  143. /* log_mpidump ("Q2.x", Q2.x); */
  144. /* log_mpidump ("Q2.y", Q2.y); */
  145. /* log_mpidump ("Q2.z", Q2.z); */
  146. _gcry_mpi_ec_add_points (&Q, &Q1, &Q2, ec);
  147. /* log_mpidump (" Q.x", Q.x); */
  148. /* log_mpidump (" Q.y", Q.y); */
  149. /* log_mpidump (" Q.z", Q.z); */
  150. if (!mpi_cmp_ui (Q.z, 0))
  151. {
  152. if (DBG_CIPHER)
  153. log_debug ("ecc verify: Rejected\n");
  154. err = GPG_ERR_BAD_SIGNATURE;
  155. goto leave;
  156. }
  157. if (_gcry_mpi_ec_get_affine (x, NULL, &Q, ec))
  158. {
  159. if (DBG_CIPHER)
  160. log_debug ("ecc verify: Failed to get affine coordinates\n");
  161. err = GPG_ERR_BAD_SIGNATURE;
  162. goto leave;
  163. }
  164. mpi_mod (x, x, ec->n); /* x = x mod E_n */
  165. if (mpi_cmp (x, r)) /* x != r */
  166. {
  167. if (DBG_CIPHER)
  168. {
  169. log_mpidump (" x", x);
  170. log_mpidump (" r", r);
  171. log_mpidump (" s", s);
  172. log_debug ("ecc verify: Not verified\n");
  173. }
  174. err = GPG_ERR_BAD_SIGNATURE;
  175. goto leave;
  176. }
  177. if (DBG_CIPHER)
  178. log_debug ("ecc verify: Accepted\n");
  179. leave:
  180. point_free (&Q2);
  181. point_free (&Q1);
  182. point_free (&Q);
  183. mpi_free (zero);
  184. mpi_free (rv);
  185. mpi_free (v);
  186. mpi_free (z2);
  187. mpi_free (z1);
  188. mpi_free (x);
  189. mpi_free (e);
  190. return err;
  191. }