Commit | Line | Data |
---|---|---|
8a7654a6 MW |
1 | /* -*-c-*- |
2 | * | |
3 | * The X25519 key-agreement algorithm | |
4 | * | |
5 | * (c) 2017 Straylight/Edgeware | |
6 | */ | |
7 | ||
8 | /*----- Licensing notice --------------------------------------------------* | |
9 | * | |
10 | * This file is part of secnet. | |
11 | * See README for full list of copyright holders. | |
12 | * | |
13 | * secnet is free software; you can redistribute it and/or modify it | |
14 | * under the terms of the GNU General Public License as published by | |
15 | * the Free Software Foundation; either version d of the License, or | |
16 | * (at your option) any later version. | |
17 | * | |
18 | * secnet is distributed in the hope that it will be useful, but | |
19 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
21 | * General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU General Public License | |
24 | * version 3 along with secnet; if not, see | |
25 | * https://www.gnu.org/licenses/gpl.html. | |
26 | * | |
27 | * This file was originally part of Catacomb, but has been automatically | |
28 | * modified for incorporation into secnet: see `import-catacomb-crypto' | |
29 | * for details. | |
30 | * | |
31 | * Catacomb is free software; you can redistribute it and/or modify | |
32 | * it under the terms of the GNU Library General Public License as | |
33 | * published by the Free Software Foundation; either version 2 of the | |
34 | * License, or (at your option) any later version. | |
35 | * | |
36 | * Catacomb is distributed in the hope that it will be useful, | |
37 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
38 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
39 | * GNU Library General Public License for more details. | |
40 | * | |
41 | * You should have received a copy of the GNU Library General Public | |
42 | * License along with Catacomb; if not, write to the Free | |
43 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
44 | * MA 02111-1307, USA. | |
45 | */ | |
46 | ||
47 | /*----- Header files ------------------------------------------------------*/ | |
48 | ||
49 | #include "fake-mLib-bits.h" | |
50 | ||
51 | #include "montladder.h" | |
52 | #include "f25519.h" | |
53 | #include "x25519.h" | |
54 | ||
55 | /*----- Important constants -----------------------------------------------*/ | |
56 | ||
57 | const octet x25519_base[32] = { 9, 0, /* ... */ }; | |
58 | ||
59 | #define A0 121665 | |
60 | ||
61 | /*----- Main code ---------------------------------------------------------*/ | |
62 | ||
63 | /* --- @x25519@ --- * | |
64 | * | |
65 | * Arguments: @octet zz[X25519_OUTSZ]@ = where to put the result | |
66 | * @const octet k[X25519_KEYSZ]@ = pointer to private key | |
67 | * @const octet qx[X25519_PUBSZ]@ = pointer to public value | |
68 | * | |
69 | * Returns: --- | |
70 | * | |
71 | * Use: Calculates X25519 of @k@ and @qx@. | |
72 | * | |
73 | * Note that there is disagreement over whether the most | |
74 | * significant bit of @qx@ (i.e., the value @qx[31]&0x80@) | |
75 | * should be ignored or counted towards the represented value. | |
76 | * Historically implementations respected the bit; later | |
77 | * convention seems to be to ignore it. This implementation | |
78 | * honours the bit: a caller who wants to ignore the bit can | |
79 | * easily clear it, while caller who wants to respect it has a | |
80 | * difficult job if this function ignores it. | |
81 | */ | |
82 | ||
83 | void x25519(octet zz[X25519_OUTSZ], | |
84 | const octet k[X25519_KEYSZ], | |
85 | const octet qx[X25519_PUBSZ]) | |
86 | { | |
87 | uint32 kw[8]; | |
88 | f25519 x1; | |
89 | ||
90 | /* Load and clamp the key. The low bits are cleared to kill the small | |
91 | * subgroups on the curve and its twist, and a high bit is set to guard | |
92 | * against careless implementations, though this isn't one of those. | |
93 | */ | |
94 | kw[0] = LOAD32_L(k + 0); kw[1] = LOAD32_L(k + 4); | |
95 | kw[2] = LOAD32_L(k + 8); kw[3] = LOAD32_L(k + 12); | |
96 | kw[4] = LOAD32_L(k + 16); kw[5] = LOAD32_L(k + 20); | |
97 | kw[6] = LOAD32_L(k + 24); kw[7] = LOAD32_L(k + 28); | |
98 | kw[0] &= 0xfffffff8; kw[7] = (kw[7]&0x3fffffff) | 0x40000000; | |
99 | ||
100 | /* And run the ladder. */ | |
101 | f25519_load(&x1, qx); | |
102 | #define MULA0(z, x) do { f25519_mulconst((z), (x), A0); } while (0) | |
103 | MONT_LADDER(f25519, MULA0, kw, 8, 32, &x1, &x1); | |
104 | #undef MULA0 | |
105 | f25519_store(zz, &x1); | |
106 | } | |
107 | ||
108 | /*----- That's all, folks -------------------------------------------------*/ |