cfb8: Fix decrypt path
[gd/nettle] / eddsa-decompress.c
1 /* eddsa-decompress.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 "eddsa.h"
37 #include "eddsa-internal.h"
38
39 #include "ecc-internal.h"
40 #include "gmp-glue.h"
41
42 mp_size_t
43 _eddsa_decompress_itch (const struct ecc_curve *ecc)
44 {
45   return 4*ecc->p.size + ecc->p.sqrt_itch;
46 }
47
48 int
49 _eddsa_decompress (const struct ecc_curve *ecc, mp_limb_t *p,
50                    const uint8_t *cp,
51                    mp_limb_t *scratch)
52 {
53   mp_limb_t sign, cy;
54   int res;
55
56 #define xp p
57 #define yp (p + ecc->p.size)
58
59 #define y2 scratch
60 #define vp (scratch + ecc->p.size)
61 #define up scratch
62 #define tp (scratch + 2*ecc->p.size)
63 #define scratch_out (scratch + 4*ecc->p.size)
64
65   sign = cp[ecc->p.bit_size / 8] >> (ecc->p.bit_size & 7);
66   if (sign > 1)
67     return 0;
68   mpn_set_base256_le (yp, ecc->p.size, cp, 1 + ecc->p.bit_size / 8);
69   /* Clear out the sign bit (if it fits) */
70   yp[ecc->p.size - 1] &= ~(mp_limb_t) 0
71     >> (ecc->p.size * GMP_NUMB_BITS - ecc->p.bit_size);
72   ecc_modp_sqr (ecc, y2, yp);
73   ecc_modp_mul (ecc, vp, y2, ecc->b);
74   ecc_modp_sub (ecc, vp, vp, ecc->unit);
75   ecc_modp_sub (ecc, up, ecc->unit, y2);
76   res = ecc->p.sqrt (&ecc->p, tp, up, vp, scratch_out);
77
78   cy = mpn_sub_n (xp, tp, ecc->p.m, ecc->p.size);
79   cnd_copy (cy, xp, tp, ecc->p.size);
80   sign ^= xp[0] & 1;
81   mpn_sub_n (tp, ecc->p.m, xp, ecc->p.size);
82   cnd_copy (sign, xp, tp, ecc->p.size);
83   return res;
84 }