Debian Codegraph @

cipher/ecc-ecdsa.c

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