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
ecdsa_in_range(const struct ecc_curve * ecc,const mp_limb_t * xp)47 ecdsa_in_range (const struct ecc_curve *ecc, const mp_limb_t *xp)
48 {
49 return !mpn_zero_p (xp, ecc->p.size)
50 && mpn_cmp (xp, ecc->q.m, ecc->p.size) < 0;
51 }
52
53 mp_size_t
ecc_ecdsa_verify_itch(const struct ecc_curve * ecc)54 ecc_ecdsa_verify_itch (const struct ecc_curve *ecc)
55 {
56 /* Largest storage need is for the ecc->mul call. */
57 return 5*ecc->p.size + ecc->mul_itch;
58 }
59
60 /* FIXME: Use faster primitives, not requiring side-channel silence. */
61 int
ecc_ecdsa_verify(const struct ecc_curve * ecc,const mp_limb_t * pp,size_t length,const uint8_t * digest,const mp_limb_t * rp,const mp_limb_t * sp,mp_limb_t * scratch)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 {
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 #define P2 scratch
85 #define u1 (scratch + 3*ecc->p.size)
86 #define u2 (scratch + 4*ecc->p.size)
87
88 #define P1 (scratch + 4*ecc->p.size)
89 #define sinv (scratch)
90 #define hp (scratch + ecc->p.size)
91
92 if (! (ecdsa_in_range (ecc, rp)
93 && ecdsa_in_range (ecc, sp)))
94 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 ecc->q.invert (&ecc->q, sinv, sp, sinv + ecc->p.size);
102
103 /* u1 = h / s, P1 = u1 * G */
104 ecc_hash (&ecc->q, hp, length, digest);
105 ecc_mod_mul_canonical (&ecc->q, u1, hp, sinv, u1);
106
107 /* u2 = r / s, P2 = u2 * Y */
108 ecc_mod_mul_canonical (&ecc->q, u2, rp, sinv, u2);
109
110 /* Total storage: 5*ecc->p.size + ecc->mul_itch */
111 ecc->mul (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 if (!mpn_zero_p (u1, ecc->p.size))
116 {
117 /* Total storage: 7*ecc->p.size + ecc->mul_g_itch (ecc->p.size) */
118 ecc->mul_g (ecc, P1, u1, P1 + 3*ecc->p.size);
119
120 /* NOTE: ecc_add_jjj and/or ecc_j_to_a will produce garbage in
121 case u1 G = +/- u2 V. However, anyone who gets his or her
122 hands on a signature where this happens during verification,
123 can also get the private key as z = +/- u1 / u_2 (mod q). And
124 then it doesn't matter very much if verification of
125 signatures with that key succeeds or fails.
126
127 u1 G = - u2 V can never happen for a correctly generated
128 signature, since it implies k = 0.
129
130 u1 G = u2 V is possible, if we are unlucky enough to get h /
131 s_1 = z. Hitting that is about as unlikely as finding the
132 private key by guessing.
133 */
134 /* Total storage: 6*ecc->p.size + ecc->add_hhh_itch */
135 ecc->add_hhh (ecc, P2, P2, P1, P1 + 3*ecc->p.size);
136 }
137 /* x coordinate only, modulo q */
138 ecc->h_to_a (ecc, 2, P1, P2, P1 + 3*ecc->p.size);
139
140 return (mpn_cmp (rp, P1, ecc->p.size) == 0);
141 #undef P2
142 #undef P1
143 #undef sinv
144 #undef u2
145 #undef hp
146 #undef u1
147 }
148