Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* eddsa-sign.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  |  | mp_size_t  | 
46  |  | _eddsa_sign_itch (const struct ecc_curve *ecc)  | 
47  | 0  | { | 
48  | 0  |   assert (ecc->mul_g_itch <= _eddsa_compress_itch (ecc));  | 
49  | 0  |   return 5*ecc->p.size + _eddsa_compress_itch (ecc);  | 
50  | 0  | }  | 
51  |  |  | 
52  |  | void  | 
53  |  | _eddsa_sign (const struct ecc_curve *ecc,  | 
54  |  |        const struct ecc_eddsa *eddsa,  | 
55  |  |        void *ctx,  | 
56  |  |        const uint8_t *pub,  | 
57  |  |        const uint8_t *k1,  | 
58  |  |        const mp_limb_t *k2,  | 
59  |  |        size_t length,  | 
60  |  |        const uint8_t *msg,  | 
61  |  |        uint8_t *signature,  | 
62  |  |        mp_limb_t *scratch)  | 
63  | 0  | { | 
64  | 0  |   mp_size_t size;  | 
65  | 0  |   size_t nbytes;  | 
66  | 0  |   mp_limb_t q, cy;  | 
67  |  | 
  | 
68  | 0  | #define rp scratch  | 
69  | 0  | #define hp (scratch + size)  | 
70  | 0  | #define P (scratch + 2*size)  | 
71  | 0  | #define sp (scratch + 2*size)  | 
72  | 0  | #define hash ((uint8_t *) (scratch + 3*size))  | 
73  | 0  | #define scratch_out (scratch + 5*size)  | 
74  |  | 
  | 
75  | 0  |   size = ecc->p.size;  | 
76  | 0  |   nbytes = 1 + ecc->p.bit_size / 8;  | 
77  |  | 
  | 
78  | 0  |   eddsa->dom (ctx);  | 
79  | 0  |   eddsa->update (ctx, nbytes, k1);  | 
80  | 0  |   eddsa->update (ctx, length, msg);  | 
81  | 0  |   eddsa->digest (ctx, 2*nbytes, hash);  | 
82  | 0  |   _eddsa_hash (&ecc->q, rp, 2*nbytes, hash);  | 
83  |  | 
  | 
84  | 0  |   ecc->mul_g (ecc, P, rp, scratch_out);  | 
85  | 0  |   _eddsa_compress (ecc, signature, P, scratch_out);  | 
86  |  | 
  | 
87  | 0  |   eddsa->dom (ctx);  | 
88  | 0  |   eddsa->update (ctx, nbytes, signature);  | 
89  | 0  |   eddsa->update (ctx, nbytes, pub);  | 
90  | 0  |   eddsa->update (ctx, length, msg);  | 
91  | 0  |   eddsa->digest (ctx, 2*nbytes, hash);  | 
92  | 0  |   _eddsa_hash (&ecc->q, hp, 2*nbytes, hash);  | 
93  |  | 
  | 
94  | 0  |   ecc_mod_mul (&ecc->q, sp, hp, k2, sp);  | 
95  | 0  |   ecc_mod_add (&ecc->q, sp, sp, rp); /* FIXME: Can be plain add */  | 
96  | 0  |   if (ecc->p.bit_size == 255)  | 
97  | 0  |     { | 
98  |  |       /* FIXME: Special code duplicated in ecc_curve25519_modq  | 
99  |  |    Define a suitable method for canonical reduction? */  | 
100  |  |  | 
101  |  |       /* q is slightly larger than 2^252, underflow from below  | 
102  |  |    mpn_submul_1 is unlikely. */  | 
103  | 0  |       unsigned shift = 252 - GMP_NUMB_BITS * (ecc->p.size - 1);  | 
104  | 0  |       q = sp[ecc->p.size-1] >> shift;  | 
105  | 0  |     }  | 
106  | 0  |   else  | 
107  | 0  |     { | 
108  | 0  |       unsigned shift;  | 
109  |  | 
  | 
110  | 0  |       assert (ecc->p.bit_size == 448);  | 
111  |  |       /* q is slightly smaller than 2^446 */  | 
112  | 0  |       shift = 446 - GMP_NUMB_BITS * (ecc->p.size - 1);  | 
113  |  |       /* Add one, then it's possible but unlikely that below  | 
114  |  |    mpn_submul_1 does *not* underflow. */  | 
115  | 0  |       q = (sp[ecc->p.size-1] >> shift) + 1;  | 
116  | 0  |     }  | 
117  |  |  | 
118  | 0  |   cy = mpn_submul_1 (sp, ecc->q.m, ecc->p.size, q);  | 
119  | 0  |   assert_maybe (cy < 2);  | 
120  | 0  |   cy -= mpn_cnd_add_n (cy, sp, sp, ecc->q.m, ecc->p.size);  | 
121  | 0  |   assert_maybe (cy == 0);  | 
122  |  | 
  | 
123  | 0  |   mpn_get_base256_le (signature + nbytes, nbytes, sp, ecc->q.size);  | 
124  | 0  | #undef rp  | 
125  | 0  | #undef hp  | 
126  | 0  | #undef P  | 
127  | 0  | #undef sp  | 
128  | 0  | #undef hash  | 
129  | 0  | }  |