Coverage Report

Created: 2022-12-08 06:09

/src/libgcrypt/cipher/ecc-ecdsa.c
Line
Count
Source (jump to first uncovered line)
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
0
{
45
0
  gpg_err_code_t rc = 0;
46
0
  int extraloops = 0;
47
0
  gcry_mpi_t k, dr, sum, k_1, x;
48
0
  mpi_point_struct I;
49
0
  gcry_mpi_t hash;
50
0
  const void *abuf;
51
0
  unsigned int abits, qbits;
52
0
  gcry_mpi_t b;                /* Random number needed for blinding.  */
53
0
  gcry_mpi_t bi;               /* multiplicative inverse of B.        */
54
0
  gcry_mpi_t hash_computed_internally = NULL;
55
56
0
  if (DBG_CIPHER)
57
0
    log_mpidump ("ecdsa sign hash  ", input );
58
59
0
  qbits = mpi_get_nbits (ec->n);
60
61
0
  if ((flags & PUBKEY_FLAG_PREHASH))
62
0
    {
63
0
      rc = _gcry_dsa_compute_hash (&hash_computed_internally, input, hashalgo);
64
0
      if (rc)
65
0
        return rc;
66
0
      input = hash_computed_internally;
67
0
    }
68
69
  /* Convert the INPUT into an MPI if needed.  */
70
0
  rc = _gcry_dsa_normalize_hash (input, &hash, qbits);
71
72
0
  if (rc)
73
0
    {
74
0
      mpi_free (hash_computed_internally);
75
0
      return rc;
76
0
    }
77
78
0
  b  = mpi_snew (qbits);
79
0
  bi = mpi_snew (qbits);
80
0
  do
81
0
    {
82
0
      _gcry_mpi_randomize (b, qbits, GCRY_WEAK_RANDOM);
83
0
      mpi_mod (b, b, ec->n);
84
0
    }
85
0
  while (!mpi_invm (bi, b, ec->n));
86
87
0
  k = NULL;
88
0
  dr = mpi_alloc (0);
89
0
  sum = mpi_alloc (0);
90
0
  k_1 = mpi_alloc (0);
91
0
  x = mpi_alloc (0);
92
0
  point_init (&I);
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
0
  while (1)
98
0
    {
99
0
      while (1)
100
0
        {
101
0
          if (k_supplied)
102
0
            k = k_supplied;
103
0
          else
104
0
            {
105
0
              mpi_free (k);
106
0
              k = NULL;
107
0
              if ((flags & PUBKEY_FLAG_RFC6979) && hashalgo)
108
0
                {
109
                  /* Use Pornin's method for deterministic DSA.  If this
110
                     flag is set, it is expected that HASH is an opaque
111
                     MPI with the to be signed hash.  That hash is also
112
                     used as h1 from 3.2.a.  */
113
0
                  if (!mpi_is_opaque (input))
114
0
                    {
115
0
                      rc = GPG_ERR_CONFLICT;
116
0
                      goto leave;
117
0
                    }
118
119
0
                  abuf = mpi_get_opaque (input, &abits);
120
0
                  rc = _gcry_dsa_gen_rfc6979_k (&k, ec->n, ec->d,
121
0
                                                abuf, (abits+7)/8,
122
0
                                                hashalgo, extraloops);
123
0
                  if (rc)
124
0
                    goto leave;
125
0
                  extraloops++;
126
0
                }
127
0
              else
128
0
                k = _gcry_dsa_gen_k (ec->n, GCRY_STRONG_RANDOM);
129
0
            }
130
131
0
          mpi_invm (k_1, k, ec->n);     /* k_1 = k^(-1) mod n  */
132
133
0
          _gcry_dsa_modify_k (k, ec->n, qbits);
134
135
0
          _gcry_mpi_ec_mul_point (&I, k, ec->G, ec);
136
0
          if (_gcry_mpi_ec_get_affine (x, NULL, &I, ec))
137
0
            {
138
0
              if (DBG_CIPHER)
139
0
                log_debug ("ecc sign: Failed to get affine coordinates\n");
140
0
              rc = GPG_ERR_BAD_SIGNATURE;
141
0
              goto leave;
142
0
            }
143
0
          mpi_mod (r, x, ec->n);  /* r = x mod n */
144
145
0
          if (mpi_cmp_ui (r, 0))
146
0
            break;
147
148
0
          if (k_supplied)
149
0
            {
150
0
              rc = GPG_ERR_INV_VALUE;
151
0
              goto leave;
152
0
            }
153
0
        }
154
155
      /* Computation of dr, sum, and s are blinded with b.  */
156
0
      mpi_mulm (dr, b, ec->d, ec->n);
157
0
      mpi_mulm (dr, dr, r, ec->n);      /* dr = d*r mod n */
158
0
      mpi_mulm (sum, b, hash, ec->n);
159
0
      mpi_addm (sum, sum, dr, ec->n);   /* sum = hash + (d*r) mod n */
160
0
      mpi_mulm (s, k_1, sum, ec->n);    /* s = k^(-1)*(hash+(d*r)) mod n */
161
      /* Undo blinding by b^-1 */
162
0
      mpi_mulm (s, bi, s, ec->n);
163
0
      if (mpi_cmp_ui (s, 0))
164
0
        break;
165
166
0
      if (k_supplied)
167
0
        {
168
0
          rc = GPG_ERR_INV_VALUE;
169
0
          break;
170
0
        }
171
0
    }
172
173
0
  if (DBG_CIPHER)
174
0
    {
175
0
      log_mpidump ("ecdsa sign result r ", r);
176
0
      log_mpidump ("ecdsa sign result s ", s);
177
0
    }
178
179
0
 leave:
180
0
  mpi_free (b);
181
0
  mpi_free (bi);
182
0
  point_free (&I);
183
0
  mpi_free (x);
184
0
  mpi_free (k_1);
185
0
  mpi_free (sum);
186
0
  mpi_free (dr);
187
0
  if (!k_supplied)
188
0
    mpi_free (k);
189
190
0
  if (hash != input)
191
0
    mpi_free (hash);
192
0
  mpi_free (hash_computed_internally);
193
194
0
  return rc;
195
0
}
196
197
198
/* Verify an ECDSA signature.
199
 * Check if R and S verifies INPUT.
200
 */
201
gpg_err_code_t
202
_gcry_ecc_ecdsa_verify (gcry_mpi_t input, mpi_ec_t ec,
203
                        gcry_mpi_t r, gcry_mpi_t s, int flags, int hashalgo)
204
0
{
205
0
  gpg_err_code_t err = 0;
206
0
  gcry_mpi_t hash, h, h1, h2, x;
207
0
  mpi_point_struct Q, Q1, Q2;
208
0
  unsigned int nbits;
209
0
  gcry_mpi_t hash_computed_internally = NULL;
210
211
0
  if (!_gcry_mpi_ec_curve_point (ec->Q, ec))
212
0
    return GPG_ERR_BROKEN_PUBKEY;
213
214
0
  if( !(mpi_cmp_ui (r, 0) > 0 && mpi_cmp (r, ec->n) < 0) )
215
0
    return GPG_ERR_BAD_SIGNATURE; /* Assertion  0 < r < n  failed.  */
216
0
  if( !(mpi_cmp_ui (s, 0) > 0 && mpi_cmp (s, ec->n) < 0) )
217
0
    return GPG_ERR_BAD_SIGNATURE; /* Assertion  0 < s < n  failed.  */
218
219
0
  nbits = mpi_get_nbits (ec->n);
220
0
  if ((flags & PUBKEY_FLAG_PREHASH))
221
0
    {
222
0
      err = _gcry_dsa_compute_hash (&hash_computed_internally, input,
223
0
                                    hashalgo);
224
0
      if (err)
225
0
        return err;
226
0
      input = hash_computed_internally;
227
0
    }
228
229
0
  err = _gcry_dsa_normalize_hash (input, &hash, nbits);
230
0
  if (err)
231
0
    {
232
0
      mpi_free (hash_computed_internally);
233
0
      return err;
234
0
    }
235
236
0
  h  = mpi_alloc (0);
237
0
  h1 = mpi_alloc (0);
238
0
  h2 = mpi_alloc (0);
239
0
  x = mpi_alloc (0);
240
0
  point_init (&Q);
241
0
  point_init (&Q1);
242
0
  point_init (&Q2);
243
244
  /* h  = s^(-1) (mod n) */
245
0
  mpi_invm (h, s, ec->n);
246
  /* h1 = hash * s^(-1) (mod n) */
247
0
  mpi_mulm (h1, hash, h, ec->n);
248
  /* Q1 = [ hash * s^(-1) ]G  */
249
0
  _gcry_mpi_ec_mul_point (&Q1, h1, ec->G, ec);
250
  /* h2 = r * s^(-1) (mod n) */
251
0
  mpi_mulm (h2, r, h, ec->n);
252
  /* Q2 = [ r * s^(-1) ]Q */
253
0
  _gcry_mpi_ec_mul_point (&Q2, h2, ec->Q, ec);
254
  /* Q  = ([hash * s^(-1)]G) + ([r * s^(-1)]Q) */
255
0
  _gcry_mpi_ec_add_points (&Q, &Q1, &Q2, ec);
256
257
0
  if (!mpi_cmp_ui (Q.z, 0))
258
0
    {
259
0
      if (DBG_CIPHER)
260
0
          log_debug ("ecc verify: Rejected\n");
261
0
      err = GPG_ERR_BAD_SIGNATURE;
262
0
      goto leave;
263
0
    }
264
0
  if (_gcry_mpi_ec_get_affine (x, NULL, &Q, ec))
265
0
    {
266
0
      if (DBG_CIPHER)
267
0
        log_debug ("ecc verify: Failed to get affine coordinates\n");
268
0
      err = GPG_ERR_BAD_SIGNATURE;
269
0
      goto leave;
270
0
    }
271
0
  mpi_mod (x, x, ec->n); /* x = x mod E_n */
272
0
  if (mpi_cmp (x, r))   /* x != r */
273
0
    {
274
0
      if (DBG_CIPHER)
275
0
        {
276
0
          log_mpidump ("     x", x);
277
0
          log_mpidump ("     r", r);
278
0
          log_mpidump ("     s", s);
279
0
        }
280
0
      err = GPG_ERR_BAD_SIGNATURE;
281
0
      goto leave;
282
0
    }
283
284
0
 leave:
285
0
  point_free (&Q2);
286
0
  point_free (&Q1);
287
0
  point_free (&Q);
288
0
  mpi_free (x);
289
0
  mpi_free (h2);
290
0
  mpi_free (h1);
291
0
  mpi_free (h);
292
0
  if (hash != input)
293
0
    mpi_free (hash);
294
0
  mpi_free (hash_computed_internally);
295
296
0
  return err;
297
0
}