Debian Codegraph @

cipher/dsa-common.c

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