Coverage Report

Created: 2024-11-25 06:31

/src/nettle/ecc-ecdsa-verify.c
Line
Count
Source (jump to first uncovered line)
1
/* ecc-ecdsa-verify.c
2
3
   Copyright (C) 2013, 2014 Niels Möller
4
5
   This file is part of GNU Nettle.
6
7
   GNU Nettle is free software: you can redistribute it and/or
8
   modify it under the terms of either:
9
10
     * the GNU Lesser General Public License as published by the Free
11
       Software Foundation; either version 3 of the License, or (at your
12
       option) any later version.
13
14
   or
15
16
     * the GNU General Public License as published by the Free
17
       Software Foundation; either version 2 of the License, or (at your
18
       option) any later version.
19
20
   or both in parallel, as here.
21
22
   GNU Nettle is distributed in the hope that it will be useful,
23
   but WITHOUT ANY WARRANTY; without even the implied warranty of
24
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25
   General Public License for more details.
26
27
   You should have received copies of the GNU General Public License and
28
   the GNU Lesser General Public License along with this program.  If
29
   not, see http://www.gnu.org/licenses/.
30
*/
31
32
/* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
33
34
#if HAVE_CONFIG_H
35
# include "config.h"
36
#endif
37
38
#include <assert.h>
39
#include <stdlib.h>
40
41
#include "ecdsa.h"
42
#include "ecc-internal.h"
43
#include "dsa-internal.h"
44
45
/* Low-level ECDSA verify */
46
47
static int
48
ecdsa_in_range (const struct ecc_curve *ecc, const mp_limb_t *xp)
49
20.5k
{
50
20.5k
  return !mpn_zero_p (xp, ecc->p.size)
51
20.5k
    && mpn_cmp (xp, ecc->q.m, ecc->p.size) < 0;
52
20.5k
}
53
54
mp_size_t
55
ecc_ecdsa_verify_itch (const struct ecc_curve *ecc)
56
10.2k
{
57
  /* Largest storage need is for the ecc_mul_a call. */
58
10.2k
  return 5*ecc->p.size + ECC_MUL_A_ITCH (ecc->p.size);
59
10.2k
}
60
61
/* FIXME: Use faster primitives, not requiring side-channel silence. */
62
int
63
ecc_ecdsa_verify (const struct ecc_curve *ecc,
64
      const mp_limb_t *pp, /* Public key */
65
      size_t length, const uint8_t *digest,
66
      const mp_limb_t *rp, const mp_limb_t *sp,
67
      mp_limb_t *scratch)
68
10.2k
{
69
  /* Procedure, according to RFC 6090, "KT-I". q denotes the group
70
     order.
71
72
     1. Check 0 < r, s < q.
73
74
     2. s' <-- s^{-1}  (mod q)
75
76
     3. u1  <-- h * s' (mod q)
77
78
     4. u2  <-- r * s' (mod q)
79
80
     5. R = u1 G + u2 Y
81
82
     6. Signature is valid if R_x = r (mod q).
83
  */
84
85
40.8k
#define P2 scratch
86
40.8k
#define u1 (scratch + 3*ecc->p.size)
87
40.8k
#define u2 (scratch + 4*ecc->p.size)
88
89
71.5k
#define P1 (scratch + 4*ecc->p.size)
90
40.8k
#define sinv (scratch)
91
20.4k
#define hp (scratch + ecc->p.size)
92
93
10.2k
  if (! (ecdsa_in_range (ecc, rp)
94
10.2k
   && ecdsa_in_range (ecc, sp)))
95
66
    return 0;
96
97
  /* FIXME: Micro optimizations: Either simultaneous multiplication.
98
     Or convert to projective coordinates (can be done without
99
     division, I think), and write an ecc_add_ppp. */
100
101
  /* Compute sinv */
102
10.2k
  ecc->q.invert (&ecc->q, sinv, sp, sinv + ecc->p.size);
103
104
  /* u1 = h / s, P1 = u1 * G */
105
10.2k
  _nettle_dsa_hash (hp, ecc->q.bit_size, length, digest);
106
10.2k
  ecc_mod_mul_canonical (&ecc->q, u1, hp, sinv, u1);
107
108
  /* u2 = r / s, P2 = u2 * Y */
109
10.2k
  ecc_mod_mul_canonical (&ecc->q, u2, rp, sinv, u2);
110
111
   /* Total storage: 5*ecc->p.size + ECC_MUL_A_ITCH */
112
10.2k
  ecc_mul_a (ecc, P2, u2, pp, u2 + ecc->p.size);
113
114
  /* u = 0 can happen only if h = 0 or h = q, which is extremely
115
     unlikely. */
116
10.2k
  if (!mpn_zero_p (u1, ecc->p.size))
117
10.2k
    {
118
      /* Total storage: 7*ecc->p.size + ECC_MUL_G_ITCH */
119
10.2k
      ecc_mul_g (ecc, P1, u1, P1 + 3*ecc->p.size);
120
121
      /* Total storage: 6*ecc->p.size + ECC_ADD_JJJ_ITCH */
122
10.2k
      if (!ecc_nonsec_add_jjj (ecc, P2, P2, P1, P1 + 3*ecc->p.size))
123
  /* Infinity point, not a valid signature. */
124
0
  return 0;
125
10.2k
    }
126
  /* x coordinate only, modulo q */
127
10.2k
  ecc_j_to_a (ecc, 2, P1, P2, P1 + 3*ecc->p.size);
128
129
10.2k
  return (mpn_cmp (rp, P1, ecc->p.size) == 0);
130
10.2k
#undef P2
131
10.2k
#undef P1
132
10.2k
#undef sinv
133
10.2k
#undef u2
134
10.2k
#undef hp
135
10.2k
#undef u1
136
10.2k
}