3 Copyright (C) 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/.
41 #include "ecc-internal.h"
42 #include "nettle-meta.h"
44 /* Checks if x1/z1 == x2/z2 (mod p). Assumes z1 and z2 are
47 equal_h (const struct ecc_modulo *p,
48 const mp_limb_t *x1, const mp_limb_t *z1,
49 const mp_limb_t *x2, const mp_limb_t *z2,
53 #define t1 (scratch + p->size)
55 ecc_mod_mul (p, t0, x1, z2);
56 if (mpn_cmp (t0, p->m, p->size) >= 0)
57 mpn_sub_n (t0, t0, p->m, p->size);
59 ecc_mod_mul (p, t1, x2, z1);
60 if (mpn_cmp (t1, p->m, p->size) >= 0)
61 mpn_sub_n (t1, t1, p->m, p->size);
63 return mpn_cmp (t0, t1, p->size) == 0;
70 _eddsa_verify_itch (const struct ecc_curve *ecc)
72 return 8*ecc->p.size + ecc->mul_itch;
76 _eddsa_verify (const struct ecc_curve *ecc,
77 const struct nettle_hash *H,
83 const uint8_t *signature,
88 #define sp (scratch + 2*ecc->p.size)
89 #define hp (scratch + 3*ecc->p.size)
90 #define P (scratch + 5*ecc->p.size)
91 #define scratch_out (scratch + 8*ecc->p.size)
93 #define hash ((uint8_t *) P)
95 nbytes = 1 + ecc->p.bit_size / 8;
97 /* Could maybe save some storage by delaying the R and S operations,
98 but it makes sense to check them for validity up front. */
99 if (!_eddsa_decompress (ecc, R, signature, R+2*ecc->p.size))
102 mpn_set_base256_le (sp, ecc->q.size, signature + nbytes, nbytes);
103 /* Check that s < q */
104 if (mpn_cmp (sp, ecc->q.m, ecc->q.size) >= 0)
108 H->update (ctx, nbytes, signature);
109 H->update (ctx, nbytes, pub);
110 H->update (ctx, length, msg);
111 H->digest (ctx, 2*nbytes, hash);
112 _eddsa_hash (&ecc->q, hp, hash);
114 /* Compute h A + R - s G, which should be the neutral point */
115 ecc->mul (ecc, P, hp, A, scratch_out);
116 ecc_add_eh (ecc, P, P, R, scratch_out);
117 /* Move out of the way. */
118 mpn_copyi (hp, sp, ecc->q.size);
119 ecc->mul_g (ecc, S, hp, scratch_out);
121 return equal_h (&ecc->p,
122 P, P + 2*ecc->p.size,
123 S, S + 2*ecc->p.size, scratch_out)
125 P + ecc->p.size, P + 2*ecc->p.size,
126 S + ecc->p.size, S + 2*ecc->p.size, scratch_out);