Coverage Report

Created: 2023-03-26 08:33

/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
44
/* Low-level ECDSA verify */
45
46
static int
47
ecdsa_in_range (const struct ecc_curve *ecc, const mp_limb_t *xp)
48
0
{
49
0
  return !mpn_zero_p (xp, ecc->p.size)
50
0
    && mpn_cmp (xp, ecc->q.m, ecc->p.size) < 0;
51
0
}
52
53
mp_size_t
54
ecc_ecdsa_verify_itch (const struct ecc_curve *ecc)
55
0
{
56
  /* Largest storage need is for the ecc_mul_a call. */
57
0
  return 5*ecc->p.size + ECC_MUL_A_ITCH (ecc->p.size);
58
0
}
59
60
/* FIXME: Use faster primitives, not requiring side-channel silence. */
61
int
62
ecc_ecdsa_verify (const struct ecc_curve *ecc,
63
      const mp_limb_t *pp, /* Public key */
64
      size_t length, const uint8_t *digest,
65
      const mp_limb_t *rp, const mp_limb_t *sp,
66
      mp_limb_t *scratch)
67
0
{
68
  /* Procedure, according to RFC 6090, "KT-I". q denotes the group
69
     order.
70
71
     1. Check 0 < r, s < q.
72
73
     2. s' <-- s^{-1}  (mod q)
74
75
     3. u1  <-- h * s' (mod q)
76
77
     4. u2  <-- r * s' (mod q)
78
79
     5. R = u1 G + u2 Y
80
81
     6. Signature is valid if R_x = r (mod q).
82
  */
83
84
0
#define P2 scratch
85
0
#define u1 (scratch + 3*ecc->p.size)
86
0
#define u2 (scratch + 4*ecc->p.size)
87
88
0
#define P1 (scratch + 4*ecc->p.size)
89
0
#define sinv (scratch)
90
0
#define hp (scratch + ecc->p.size)
91
92
0
  if (! (ecdsa_in_range (ecc, rp)
93
0
   && ecdsa_in_range (ecc, sp)))
94
0
    return 0;
95
96
  /* FIXME: Micro optimizations: Either simultaneous multiplication.
97
     Or convert to projective coordinates (can be done without
98
     division, I think), and write an ecc_add_ppp. */
99
100
  /* Compute sinv */
101
0
  ecc->q.invert (&ecc->q, sinv, sp, sinv + ecc->p.size);
102
103
  /* u1 = h / s, P1 = u1 * G */
104
0
  ecc_hash (&ecc->q, hp, length, digest);
105
0
  ecc_mod_mul_canonical (&ecc->q, u1, hp, sinv, u1);
106
107
  /* u2 = r / s, P2 = u2 * Y */
108
0
  ecc_mod_mul_canonical (&ecc->q, u2, rp, sinv, u2);
109
110
   /* Total storage: 5*ecc->p.size + ECC_MUL_A_ITCH */
111
0
  ecc_mul_a (ecc, P2, u2, pp, u2 + ecc->p.size);
112
113
  /* u = 0 can happen only if h = 0 or h = q, which is extremely
114
     unlikely. */
115
0
  if (!mpn_zero_p (u1, ecc->p.size))
116
0
    {
117
      /* Total storage: 7*ecc->p.size + ECC_MUL_G_ITCH */
118
0
      ecc_mul_g (ecc, P1, u1, P1 + 3*ecc->p.size);
119
120
      /* Total storage: 6*ecc->p.size + ECC_ADD_JJJ_ITCH */
121
0
      if (!ecc_nonsec_add_jjj (ecc, P2, P2, P1, P1 + 3*ecc->p.size))
122
  /* Infinity point, not a valid signature. */
123
0
  return 0;
124
0
    }
125
  /* x coordinate only, modulo q */
126
0
  ecc_j_to_a (ecc, 2, P1, P2, P1 + 3*ecc->p.size);
127
128
0
  return (mpn_cmp (rp, P1, ecc->p.size) == 0);
129
0
#undef P2
130
0
#undef P1
131
0
#undef sinv
132
0
#undef u2
133
0
#undef hp
134
0
#undef u1
135
0
}