/src/nettle-with-mini-gmp/curve448-mul-g.c
Line  | Count  | Source  | 
1  |  | /* curve448-mul-g.c  | 
2  |  |  | 
3  |  |    Copyright (C) 2017 Daiki Ueno  | 
4  |  |    Copyright (C) 2017 Red Hat, Inc.  | 
5  |  |  | 
6  |  |    This file is part of GNU Nettle.  | 
7  |  |  | 
8  |  |    GNU Nettle is free software: you can redistribute it and/or  | 
9  |  |    modify it under the terms of either:  | 
10  |  |  | 
11  |  |      * the GNU Lesser General Public License as published by the Free  | 
12  |  |        Software Foundation; either version 3 of the License, or (at your  | 
13  |  |        option) any later version.  | 
14  |  |  | 
15  |  |    or  | 
16  |  |  | 
17  |  |      * the GNU General Public License as published by the Free  | 
18  |  |        Software Foundation; either version 2 of the License, or (at your  | 
19  |  |        option) any later version.  | 
20  |  |  | 
21  |  |    or both in parallel, as here.  | 
22  |  |  | 
23  |  |    GNU Nettle is distributed in the hope that it will be useful,  | 
24  |  |    but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
25  |  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
26  |  |    General Public License for more details.  | 
27  |  |  | 
28  |  |    You should have received copies of the GNU General Public License and  | 
29  |  |    the GNU Lesser General Public License along with this program.  If  | 
30  |  |    not, see http://www.gnu.org/licenses/.  | 
31  |  | */  | 
32  |  |  | 
33  |  | #if HAVE_CONFIG_H  | 
34  |  | # include "config.h"  | 
35  |  | #endif  | 
36  |  |  | 
37  |  | #include <string.h>  | 
38  |  |  | 
39  |  | #include "curve448.h"  | 
40  |  |  | 
41  |  | #include "ecc.h"  | 
42  |  | #include "ecc-internal.h"  | 
43  |  |  | 
44  |  | /* Intended to be compatible with NaCl's crypto_scalarmult_base. */  | 
45  |  | void  | 
46  |  | curve448_mul_g (uint8_t *r, const uint8_t *n)  | 
47  | 49  | { | 
48  | 49  |   const struct ecc_curve *ecc = &_nettle_curve448;  | 
49  | 49  |   uint8_t t[CURVE448_SIZE];  | 
50  | 49  |   mp_limb_t *scratch;  | 
51  | 49  |   mp_size_t itch;  | 
52  |  |  | 
53  | 98  | #define ng scratch  | 
54  | 196  | #define x (scratch + 3*ecc->p.size)  | 
55  | 98  | #define scratch_out (scratch + 4*ecc->p.size)  | 
56  |  |  | 
57  | 49  |   memcpy (t, n, sizeof(t));  | 
58  | 49  |   t[0] &= ~3;  | 
59  | 49  |   t[CURVE448_SIZE-1] = (t[CURVE448_SIZE-1] & 0x7f) | 0x80;  | 
60  |  |  | 
61  | 49  |   itch = 5*ecc->p.size + ecc->mul_g_itch;  | 
62  | 49  |   scratch = gmp_alloc_limbs (itch);  | 
63  |  |  | 
64  | 49  |   mpn_set_base256_le (x, ecc->p.size, t, CURVE448_SIZE);  | 
65  |  |  | 
66  | 49  |   ecc_mul_g_eh (ecc, ng, x, scratch_out);  | 
67  | 49  |   curve448_eh_to_x (x, ng, scratch_out);  | 
68  |  |  | 
69  | 49  |   mpn_get_base256_le (r, CURVE448_SIZE, x, ecc->p.size);  | 
70  | 49  |   gmp_free_limbs (scratch, itch);  | 
71  | 49  | #undef ng  | 
72  | 49  | #undef x  | 
73  | 49  | #undef scratch_out  | 
74  | 49  | }  |