5541d9757ae35827b33ba0b781124fbcbc579c86
[platform/upstream/nettle.git] / eddsa-verify.c
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
40 #include "ecc.h"
41 #include "ecc-internal.h"
42 #include "nettle-meta.h"
43
44 /* Checks if x1/z1 == x2/z2 (mod p). Assumes z1 and z2 are
45    non-zero. */
46 static int
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,
50          mp_limb_t *scratch)
51 {
52 #define t0 scratch
53 #define t1 (scratch + p->size)
54
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);
58
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);
62
63   return mpn_cmp (t0, t1, p->size) == 0;
64
65 #undef t0
66 #undef t1
67 }
68
69 mp_size_t
70 _eddsa_verify_itch (const struct ecc_curve *ecc)
71 {
72   return 8*ecc->p.size + ecc->mul_itch;
73 }
74
75 int
76 _eddsa_verify (const struct ecc_curve *ecc,
77                const struct nettle_hash *H,
78                const uint8_t *pub,
79                const mp_limb_t *A,
80                void *ctx,
81                size_t length,
82                const uint8_t *msg,
83                const uint8_t *signature,
84                mp_limb_t *scratch)
85 {
86   size_t nbytes;
87 #define R scratch
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)
92 #define S R
93 #define hash ((uint8_t *) P)
94
95   nbytes = 1 + ecc->p.bit_size / 8;
96
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))
100     return 0;
101
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)
105     return 0;
106
107   H->init (ctx);
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);
113
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);
120
121   return equal_h (&ecc->p,
122                    P, P + 2*ecc->p.size,
123                    S, S + 2*ecc->p.size, scratch_out)
124     && equal_h (&ecc->p,
125                 P + ecc->p.size, P + 2*ecc->p.size,
126                 S + ecc->p.size, S + 2*ecc->p.size, scratch_out);
127
128 #undef R
129 #undef sp
130 #undef hp
131 #undef P
132 #undef S
133 }