ec-field-test.c: Make the field-element type use internal format.
[secnet] / x448.c
CommitLineData
8a7654a6
MW
1/* -*-c-*-
2 *
3 * The X448 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 "fgoldi.h"
53#include "x448.h"
54
55/*----- Important constants -----------------------------------------------*/
56
57const octet x448_base[56] = { 5, 0, /* ... */ };
58
59#define A0 39081
60
61/*----- Main code ---------------------------------------------------------*/
62
63/* --- @x448@ --- *
64 *
65 * Arguments: @octet zz[X448_OUTSZ]@ = where to put the result
66 * @const octet k[X448_KEYSZ]@ = pointer to private key
67 * @const octet qx[X448_PUBSZ]@ = pointer to public value
68 *
69 * Returns: ---
70 *
71 * Use: Calculates X448 of @k@ and @qx@.
72 */
73
74void x448(octet zz[X448_OUTSZ],
75 const octet k[X448_KEYSZ],
76 const octet qx[X448_PUBSZ])
77{
78 uint32 kw[14];
79 fgoldi x1;
80 unsigned i;
81
82 /* Load and clamp the key. The low bits are cleared to kill the small
83 * subgroups on the curve and its twist, and a high bit is set to guard
84 * against careless implementations, though this isn't one of those.
85 */
86 for (i = 0; i < 14; i++) kw[i] = LOAD32_L(k + 4*i);
87 kw[0] &= 0xfffffffc; kw[13] |= 0x80000000;
88
89 /* And run the ladder. */
90 fgoldi_load(&x1, qx);
91#define MULA0(z, x) do { fgoldi_mulconst((z), (x), A0); } while (0)
92 MONT_LADDER(fgoldi, MULA0, kw, 14, 32, &x1, &x1);
93#undef MULA0
94 fgoldi_store(zz, &x1);
95}
96
97/*----- That's all, folks -------------------------------------------------*/