Coverage Report

Created: 2023-02-22 06:14

/src/nettle-with-mini-gmp/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
2.18k
{
49
2.18k
  return !mpn_zero_p (xp, ecc->p.size)
50
2.18k
    && mpn_cmp (xp, ecc->q.m, ecc->p.size) < 0;
51
2.18k
}
52
53
mp_size_t
54
ecc_ecdsa_verify_itch (const struct ecc_curve *ecc)
55
1.17k
{
56
  /* Largest storage need is for the ecc_mul_a call. */
57
1.17k
  return 5*ecc->p.size + ECC_MUL_A_ITCH (ecc->p.size);
58
1.17k
}
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
1.10k
{
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
3.88k
#define P2 scratch
85
4.04k
#define u1 (scratch + 3*ecc->p.size)
86
4.20k
#define u2 (scratch + 4*ecc->p.size)
87
88
6.72k
#define P1 (scratch + 4*ecc->p.size)
89
4.20k
#define sinv (scratch)
90
2.10k
#define hp (scratch + ecc->p.size)
91
92
1.10k
  if (! (ecdsa_in_range (ecc, rp)
93
1.10k
   && ecdsa_in_range (ecc, sp)))
94
56
    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
1.05k
  ecc->q.invert (&ecc->q, sinv, sp, sinv + ecc->p.size);
102
103
  /* u1 = h / s, P1 = u1 * G */
104
1.05k
  ecc_hash (&ecc->q, hp, length, digest);
105
1.05k
  ecc_mod_mul_canonical (&ecc->q, u1, hp, sinv, u1);
106
107
  /* u2 = r / s, P2 = u2 * Y */
108
1.05k
  ecc_mod_mul_canonical (&ecc->q, u2, rp, sinv, u2);
109
110
   /* Total storage: 5*ecc->p.size + ECC_MUL_A_ITCH */
111
1.05k
  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
1.05k
  if (!mpn_zero_p (u1, ecc->p.size))
116
893
    {
117
      /* Total storage: 7*ecc->p.size + ECC_MUL_G_ITCH */
118
893
      ecc_mul_g (ecc, P1, u1, P1 + 3*ecc->p.size);
119
120
      /* Total storage: 6*ecc->p.size + ECC_ADD_JJJ_ITCH */
121
893
      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
893
    }
125
  /* x coordinate only, modulo q */
126
1.05k
  ecc_j_to_a (ecc, 2, P1, P2, P1 + 3*ecc->p.size);
127
128
1.05k
  return (mpn_cmp (rp, P1, ecc->p.size) == 0);
129
1.05k
#undef P2
130
1.05k
#undef P1
131
1.05k
#undef sinv
132
1.05k
#undef u2
133
1.05k
#undef hp
134
1.05k
#undef u1
135
1.05k
}