3 Copyright (C) 2013, 2014 Niels Möller
5 This file is part of GNU Nettle.
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
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.
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.
20 or both in parallel, as here.
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.
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/.
32 /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
42 #include "ecc-internal.h"
44 /* Low-level ECDSA verify */
46 /* FIXME: Use mpn_zero_p. */
48 zero_p (const mp_limb_t *xp, mp_size_t n)
57 ecdsa_in_range (const struct ecc_curve *ecc, const mp_limb_t *xp)
59 return !zero_p (xp, ecc->p.size)
60 && mpn_cmp (xp, ecc->q.m, ecc->p.size) < 0;
64 ecc_ecdsa_verify_itch (const struct ecc_curve *ecc)
66 /* Largest storage need is for the ecc->mul call. */
67 return 5*ecc->p.size + ecc->mul_itch;
70 /* FIXME: Use faster primitives, not requiring side-channel silence. */
72 ecc_ecdsa_verify (const struct ecc_curve *ecc,
73 const mp_limb_t *pp, /* Public key */
74 size_t length, const uint8_t *digest,
75 const mp_limb_t *rp, const mp_limb_t *sp,
78 /* Procedure, according to RFC 6090, "KT-I". q denotes the group
81 1. Check 0 < r, s < q.
83 2. s' <-- s^{-1} (mod q)
85 3. u1 <-- h * s' (mod q)
87 4. u2 <-- r * s' (mod q)
91 6. Signature is valid if R_x = r (mod q).
95 #define u1 (scratch + 3*ecc->p.size)
96 #define u2 (scratch + 4*ecc->p.size)
98 #define P1 (scratch + 4*ecc->p.size)
99 #define sinv (scratch)
100 #define hp (scratch + ecc->p.size)
102 if (! (ecdsa_in_range (ecc, rp)
103 && ecdsa_in_range (ecc, sp)))
106 /* FIXME: Micro optimizations: Either simultaneous multiplication.
107 Or convert to projective coordinates (can be done without
108 division, I think), and write an ecc_add_ppp. */
111 ecc->q.invert (&ecc->q, sinv, sp, sinv + 2*ecc->p.size);
113 /* u1 = h / s, P1 = u1 * G */
114 ecc_hash (&ecc->q, hp, length, digest);
115 ecc_modq_mul (ecc, u1, hp, sinv);
117 /* u2 = r / s, P2 = u2 * Y */
118 ecc_modq_mul (ecc, u2, rp, sinv);
120 /* Total storage: 5*ecc->p.size + ecc->mul_itch */
121 ecc->mul (ecc, P2, u2, pp, u2 + ecc->p.size);
123 /* u = 0 can happen only if h = 0 or h = q, which is extremely
125 if (!zero_p (u1, ecc->p.size))
127 /* Total storage: 7*ecc->p.size + ecc->mul_g_itch (ecc->p.size) */
128 ecc->mul_g (ecc, P1, u1, P1 + 3*ecc->p.size);
130 /* NOTE: ecc_add_jjj and/or ecc_j_to_a will produce garbage in
131 case u1 G = +/- u2 V. However, anyone who gets his or her
132 hands on a signature where this happens during verification,
133 can also get the private key as z = +/- u1 / u_2 (mod q). And
134 then it doesn't matter very much if verification of
135 signatures with that key succeeds or fails.
137 u1 G = - u2 V can never happen for a correctly generated
138 signature, since it implies k = 0.
140 u1 G = u2 V is possible, if we are unlucky enough to get h /
141 s_1 = z. Hitting that is about as unlikely as finding the
142 private key by guessing.
144 /* Total storage: 6*ecc->p.size + ecc->add_hhh_itch */
145 ecc->add_hhh (ecc, P1, P1, P2, P1 + 3*ecc->p.size);
147 /* x coordinate only, modulo q */
148 ecc->h_to_a (ecc, 2, P2, P1, P1 + 3*ecc->p.size);
150 return (mpn_cmp (rp, P2, ecc->p.size) == 0);