Coverage Report

Created: 2024-06-28 06:39

/src/nettle-with-libgmp/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
1.55k
{
50
1.55k
  return !mpn_zero_p (xp, ecc->p.size)
51
1.55k
    && mpn_cmp (xp, ecc->q.m, ecc->p.size) < 0;
52
1.55k
}
53
54
mp_size_t
55
ecc_ecdsa_verify_itch (const struct ecc_curve *ecc)
56
860
{
57
  /* Largest storage need is for the ecc_mul_a call. */
58
860
  return 5*ecc->p.size + ECC_MUL_A_ITCH (ecc->p.size);
59
860
}
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
787
{
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
2.69k
#define P2 scratch
86
2.82k
#define u1 (scratch + 3*ecc->p.size)
87
2.96k
#define u2 (scratch + 4*ecc->p.size)
88
89
4.63k
#define P1 (scratch + 4*ecc->p.size)
90
2.96k
#define sinv (scratch)
91
1.48k
#define hp (scratch + ecc->p.size)
92
93
787
  if (! (ecdsa_in_range (ecc, rp)
94
787
   && ecdsa_in_range (ecc, sp)))
95
45
    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
742
  ecc->q.invert (&ecc->q, sinv, sp, sinv + ecc->p.size);
103
104
  /* u1 = h / s, P1 = u1 * G */
105
742
  _nettle_dsa_hash (hp, ecc->q.bit_size, length, digest);
106
742
  ecc_mod_mul_canonical (&ecc->q, u1, hp, sinv, u1);
107
108
  /* u2 = r / s, P2 = u2 * Y */
109
742
  ecc_mod_mul_canonical (&ecc->q, u2, rp, sinv, u2);
110
111
   /* Total storage: 5*ecc->p.size + ECC_MUL_A_ITCH */
112
742
  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
742
  if (!mpn_zero_p (u1, ecc->p.size))
117
603
    {
118
      /* Total storage: 7*ecc->p.size + ECC_MUL_G_ITCH */
119
603
      ecc_mul_g (ecc, P1, u1, P1 + 3*ecc->p.size);
120
121
      /* Total storage: 6*ecc->p.size + ECC_ADD_JJJ_ITCH */
122
603
      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
603
    }
126
  /* x coordinate only, modulo q */
127
742
  ecc_j_to_a (ecc, 2, P1, P2, P1 + 3*ecc->p.size);
128
129
742
  return (mpn_cmp (rp, P1, ecc->p.size) == 0);
130
742
#undef P2
131
742
#undef P1
132
742
#undef sinv
133
742
#undef u2
134
742
#undef hp
135
742
#undef u1
136
742
}