Apply %restore_fcommon macro for Address Sanitizer
[platform/upstream/nettle.git] / eddsa-sign.c
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
40 #include "ecc.h"
41 #include "ecc-internal.h"
42 #include "nettle-meta.h"
43
44 mp_size_t
45 _eddsa_sign_itch (const struct ecc_curve *ecc)
46 {
47   return 5*ecc->p.size + ecc->mul_g_itch;
48 }
49
50 void
51 _eddsa_sign (const struct ecc_curve *ecc,
52              const struct nettle_hash *H,
53              const uint8_t *pub,
54              void *ctx,
55              const mp_limb_t *k2,
56              size_t length,
57              const uint8_t *msg,
58              uint8_t *signature,
59              mp_limb_t *scratch)
60 {
61   mp_size_t size;
62   size_t nbytes;
63 #define rp scratch
64 #define hp (scratch + size)
65 #define P (scratch + 2*size)
66 #define sp (scratch + 2*size)
67 #define hash ((uint8_t *) (scratch + 3*size))
68 #define scratch_out (scratch + 5*size)
69
70   size = ecc->p.size;
71   nbytes = 1 + ecc->p.bit_size / 8;
72
73   assert (H->digest_size >= 2 * nbytes);
74
75   H->update (ctx, length, msg);
76   H->digest (ctx, 2*nbytes, hash);
77   _eddsa_hash (&ecc->q, rp, hash);
78   ecc->mul_g (ecc, P, rp, scratch_out);
79   _eddsa_compress (ecc, signature, P, scratch_out);
80
81   H->update (ctx, nbytes, signature);
82   H->update (ctx, nbytes, pub);
83   H->update (ctx, length, msg);
84   H->digest (ctx, 2*nbytes, hash);
85   _eddsa_hash (&ecc->q, hp, hash);
86
87   ecc_modq_mul (ecc, sp, hp, k2);
88   ecc_modq_add (ecc, sp, sp, rp); /* FIXME: Can be plain add */
89   /* FIXME: Special code duplicated in ecc_25519_modq and ecc_eh_to_a.
90      Define a suitable method? */
91   {
92     unsigned shift;
93     mp_limb_t cy;
94     assert (ecc->p.bit_size == 255);
95     shift = 252 - GMP_NUMB_BITS * (ecc->p.size - 1);
96     cy = mpn_submul_1 (sp, ecc->q.m, ecc->p.size,
97                        sp[ecc->p.size-1] >> shift);
98     assert (cy < 2);
99     cnd_add_n (cy, sp, ecc->q.m, ecc->p.size);
100   }
101   mpn_get_base256_le (signature + nbytes, nbytes, sp, ecc->q.size);
102 #undef rp
103 #undef hp
104 #undef P
105 #undef sp
106 #undef hash
107 }