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