1 /* eddsa-verify.c
2 
3    Copyright (C) 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 #if HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35 
36 #include <assert.h>
37 
38 #include "eddsa.h"
39 #include "eddsa-internal.h"
40 
41 #include <nettle/ecc.h>
42 #include "ecc-internal.h"
43 #include <nettle/nettle-meta.h>
44 
45 /* Checks if x1/z1 == x2/z2 (mod p). Assumes z1 and z2 are
46    non-zero. */
47 static int
equal_h(const struct ecc_modulo * p,const mp_limb_t * x1,const mp_limb_t * z1,const mp_limb_t * x2,const mp_limb_t * z2,mp_limb_t * scratch)48 equal_h (const struct ecc_modulo *p,
49 	 const mp_limb_t *x1, const mp_limb_t *z1,
50 	 const mp_limb_t *x2, const mp_limb_t *z2,
51 	 mp_limb_t *scratch)
52 {
53 #define t0 scratch
54 #define t1 (scratch + p->size)
55 
56   ecc_mod_mul (p, t0, x1, z2);
57   if (mpn_cmp (t0, p->m, p->size) >= 0)
58     mpn_sub_n (t0, t0, p->m, p->size);
59 
60   ecc_mod_mul (p, t1, x2, z1);
61   if (mpn_cmp (t1, p->m, p->size) >= 0)
62     mpn_sub_n (t1, t1, p->m, p->size);
63 
64   return mpn_cmp (t0, t1, p->size) == 0;
65 
66 #undef t0
67 #undef t1
68 }
69 
70 mp_size_t
_eddsa_verify_itch(const struct ecc_curve * ecc)71 _eddsa_verify_itch (const struct ecc_curve *ecc)
72 {
73   assert (_eddsa_decompress_itch (ecc) <= ecc->mul_itch);
74   return 8*ecc->p.size + ecc->mul_itch;
75 }
76 
77 int
_eddsa_verify(const struct ecc_curve * ecc,const struct ecc_eddsa * eddsa,const uint8_t * pub,const mp_limb_t * A,void * ctx,size_t length,const uint8_t * msg,const uint8_t * signature,mp_limb_t * scratch)78 _eddsa_verify (const struct ecc_curve *ecc,
79 	       const struct ecc_eddsa *eddsa,
80 	       const uint8_t *pub,
81 	       const mp_limb_t *A,
82 	       void *ctx,
83 	       size_t length,
84 	       const uint8_t *msg,
85 	       const uint8_t *signature,
86 	       mp_limb_t *scratch)
87 {
88   size_t nbytes;
89 #define R scratch
90 #define sp (scratch + 2*ecc->p.size)
91 #define hp (scratch + 3*ecc->p.size)
92 #define P (scratch + 5*ecc->p.size)
93 #define scratch_out (scratch + 8*ecc->p.size)
94 #define S R
95 #define hash ((uint8_t *) P)
96 
97   nbytes = 1 + ecc->p.bit_size / 8;
98 
99   /* Could maybe save some storage by delaying the R and S operations,
100      but it makes sense to check them for validity up front. */
101   if (!_eddsa_decompress (ecc, R, signature, R+2*ecc->p.size))
102     return 0;
103 
104   mpn_set_base256_le (sp, ecc->q.size, signature + nbytes, nbytes);
105   /* Check that s < q */
106   if (mpn_cmp (sp, ecc->q.m, ecc->q.size) >= 0)
107     return 0;
108 
109   eddsa->dom (ctx);
110   eddsa->update (ctx, nbytes, signature);
111   eddsa->update (ctx, nbytes, pub);
112   eddsa->update (ctx, length, msg);
113   eddsa->digest (ctx, 2*nbytes, hash);
114   _eddsa_hash (&ecc->q, hp, 2*nbytes, hash);
115 
116   /* Compute h A + R - s G, which should be the neutral point */
117   ecc->mul (ecc, P, hp, A, scratch_out);
118   ecc->add_hh (ecc, P, P, R, scratch_out);
119   /* Move out of the way. */
120   mpn_copyi (hp, sp, ecc->q.size);
121   ecc->mul_g (ecc, S, hp, scratch_out);
122 
123   return equal_h (&ecc->p,
124 		   P, P + 2*ecc->p.size,
125 		   S, S + 2*ecc->p.size, scratch_out)
126     && equal_h (&ecc->p,
127 		P + ecc->p.size, P + 2*ecc->p.size,
128 		S + ecc->p.size, S + 2*ecc->p.size, scratch_out);
129 
130 #undef R
131 #undef sp
132 #undef hp
133 #undef P
134 #undef S
135 }
136