/src/nettle/eddsa-decompress.c
Line  | Count  | Source (jump to first uncovered line)  | 
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 <assert.h>  | 
37  |  |  | 
38  |  | #include "eddsa.h"  | 
39  |  | #include "eddsa-internal.h"  | 
40  |  |  | 
41  |  | #include "ecc-internal.h"  | 
42  |  | #include "gmp-glue.h"  | 
43  |  |  | 
44  |  | mp_size_t  | 
45  |  | _eddsa_decompress_itch (const struct ecc_curve *ecc)  | 
46  | 0  | { | 
47  | 0  |   return 4*ecc->p.size + ecc->p.sqrt_ratio_itch;  | 
48  | 0  | }  | 
49  |  |  | 
50  |  | int  | 
51  |  | _eddsa_decompress (const struct ecc_curve *ecc, mp_limb_t *p,  | 
52  |  |        const uint8_t *cp,  | 
53  |  |        mp_limb_t *scratch)  | 
54  | 0  | { | 
55  | 0  |   mp_limb_t sign, cy;  | 
56  | 0  |   mp_size_t nlimbs;  | 
57  | 0  |   size_t nbytes;  | 
58  | 0  |   int res;  | 
59  |  | 
  | 
60  | 0  | #define xp p  | 
61  | 0  | #define yp (p + ecc->p.size)  | 
62  |  | 
  | 
63  | 0  | #define y2 scratch  | 
64  | 0  | #define vp (scratch + ecc->p.size)  | 
65  | 0  | #define up scratch  | 
66  | 0  | #define tp (scratch + 2*ecc->p.size)  | 
67  | 0  | #define scratch_out (scratch + 4*ecc->p.size)  | 
68  |  | 
  | 
69  | 0  |   nbytes = 1 + ecc->p.bit_size / 8;  | 
70  |  |   /* By RFC 8032, sign bit is always the most significant bit of the  | 
71  |  |      last byte. */  | 
72  | 0  |   sign = cp[nbytes-1] >> 7;  | 
73  |  |  | 
74  |  |   /* May need an extra limb. */  | 
75  | 0  |   nlimbs = (nbytes * 8 + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS;  | 
76  | 0  |   assert (nlimbs <= ecc->p.size + 1);  | 
77  | 0  |   mpn_set_base256_le (scratch, nlimbs, cp, nbytes);  | 
78  |  |  | 
79  |  |   /* Clear out the sign bit */  | 
80  | 0  |   scratch[nlimbs - 1] &=  | 
81  | 0  |     ((mp_limb_t) 1 << ((nbytes * 8 - 1) % GMP_NUMB_BITS)) - 1;  | 
82  | 0  |   mpn_copyi (yp, scratch, ecc->p.size);  | 
83  |  |  | 
84  |  |   /* Check range. */  | 
85  | 0  |   if (nlimbs > ecc->p.size)  | 
86  | 0  |     res = is_zero_limb (scratch[nlimbs - 1]);  | 
87  | 0  |   else  | 
88  | 0  |     res = 1;  | 
89  |  |  | 
90  |  |   /* For a valid input, y < p, so subtraction should underflow. */  | 
91  | 0  |   res &= mpn_sub_n (scratch, scratch, ecc->p.m, ecc->p.size);  | 
92  |  | 
  | 
93  | 0  |   ecc_mod_sqr (&ecc->p, y2, yp, y2);  | 
94  | 0  |   ecc_mod_mul (&ecc->p, vp, y2, ecc->b, vp);  | 
95  | 0  |   ecc_mod_sub (&ecc->p, vp, vp, ecc->unit);  | 
96  |  |   /* The sign is different between curve25519 and curve448.  */  | 
97  | 0  |   if (ecc->p.bit_size == 255)  | 
98  | 0  |     ecc_mod_sub (&ecc->p, up, ecc->unit, y2);  | 
99  | 0  |   else  | 
100  | 0  |     ecc_mod_sub (&ecc->p, up, y2, ecc->unit);  | 
101  | 0  |   res &= ecc->p.sqrt_ratio (&ecc->p, tp, up, vp, scratch_out);  | 
102  |  | 
  | 
103  | 0  |   cy = mpn_sub_n (xp, tp, ecc->p.m, ecc->p.size);  | 
104  | 0  |   cnd_copy (cy, xp, tp, ecc->p.size);  | 
105  | 0  |   sign ^= xp[0] & 1;  | 
106  | 0  |   mpn_sub_n (tp, ecc->p.m, xp, ecc->p.size);  | 
107  | 0  |   cnd_copy (sign, xp, tp, ecc->p.size);  | 
108  |  |   /* Fails if the square root is zero but (original) sign was 1 */  | 
109  | 0  |   res &= mpn_sub_n (tp, xp, ecc->p.m, ecc->p.size);  | 
110  | 0  |   return res;  | 
111  | 0  | }  |