progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / pub / x448.c
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 Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <mLib/bits.h>
31
32 #include "montladder.h"
33 #include "fgoldi.h"
34 #include "x448.h"
35
36 /*----- Important constants -----------------------------------------------*/
37
38 const octet x448_base[56] = { 5, 0, /* ... */ };
39
40 #define A0 39081
41
42 /*----- Key fetching ------------------------------------------------------*/
43
44 const key_fetchdef x448_pubfetch[] = {
45 { "pub", offsetof(x448_pub, pub), KENC_BINARY, 0 },
46 { 0, 0, 0, 0 }
47 };
48
49 static const key_fetchdef priv[] = {
50 { "priv", offsetof(x448_priv, priv), KENC_BINARY, 0 },
51 { 0, 0, 0, 0 }
52 };
53
54 const key_fetchdef x448_privfetch[] = {
55 { "pub", offsetof(x448_priv, pub), KENC_BINARY, 0 },
56 { "private", 0, KENC_STRUCT, priv },
57 { 0, 0, 0, 0 }
58 };
59
60 /*----- Main code ---------------------------------------------------------*/
61
62 /* --- @x448@ --- *
63 *
64 * Arguments: @octet zz[X448_OUTSZ]@ = where to put the result
65 * @const octet k[X448_KEYSZ]@ = pointer to private key
66 * @const octet qx[X448_PUBSZ]@ = pointer to public value
67 *
68 * Returns: ---
69 *
70 * Use: Calculates X448 of @k@ and @qx@.
71 */
72
73 void x448(octet zz[X448_OUTSZ],
74 const octet k[X448_KEYSZ],
75 const octet qx[X448_PUBSZ])
76 {
77 uint32 kw[14];
78 fgoldi x1;
79 unsigned i;
80
81 /* Load and clamp the key. The low bits are cleared to kill the small
82 * subgroups on the curve and its twist, and a high bit is set to guard
83 * against careless implementations, though this isn't one of those.
84 */
85 for (i = 0; i < 14; i++) kw[i] = LOAD32_L(k + 4*i);
86 kw[0] &= 0xfffffffc; kw[13] |= 0x80000000;
87
88 /* And run the ladder. */
89 fgoldi_load(&x1, qx);
90 #define MULA0(z, x) do { fgoldi_mulconst((z), (x), A0); } while (0)
91 MONT_LADDER(fgoldi, MULA0, kw, 14, 32, &x1, &x1);
92 #undef MULA0
93 fgoldi_store(zz, &x1);
94 }
95
96 /*----- Test rig ----------------------------------------------------------*/
97
98 #ifdef TEST_RIG
99
100 #include <mLib/macros.h>
101 #include <mLib/report.h>
102 #include <mLib/testrig.h>
103
104 #include "ct.h"
105
106 static int vrf_x448(dstr dv[])
107 {
108 dstr dz = DSTR_INIT;
109 int ok = 1;
110
111 if (dv[0].len != X448_KEYSZ) die(1, "bad key length");
112 if (dv[1].len != X448_PUBSZ) die(1, "bad public length");
113 if (dv[2].len != X448_OUTSZ) die(1, "bad result length");
114
115 ct_poison(dv[0].buf, dv[0].len);
116 dstr_ensure(&dz, X448_OUTSZ); dz.len = X448_OUTSZ;
117 x448((octet *)dz.buf,
118 (const octet *)dv[0].buf,
119 (const octet *)dv[1].buf);
120 ct_remedy(dz.buf, dz.len);
121 if (MEMCMP(dz.buf, !=, dv[2].buf, X448_OUTSZ)) {
122 ok = 0;
123 fprintf(stderr, "failed!");
124 fprintf(stderr, "\n\t k = "); type_hex.dump(&dv[0], stderr);
125 fprintf(stderr, "\n\t p = "); type_hex.dump(&dv[1], stderr);
126 fprintf(stderr, "\n\twant = "); type_hex.dump(&dv[2], stderr);
127 fprintf(stderr, "\n\tcalc = "); type_hex.dump(&dz, stderr);
128 fprintf(stderr, "\n");
129 }
130
131 dstr_destroy(&dz);
132 return (ok);
133 }
134
135 static int vrf_mct(dstr dv[])
136 {
137 octet b0[X448_OUTSZ], b1[X448_OUTSZ], *k = b0, *x = b1, *t;
138 unsigned long i, niter;
139 dstr d = DSTR_INIT;
140 int ok = 1;
141
142 if (dv[0].len != sizeof(b0)) { fprintf(stderr, "k len\n"); exit(2); }
143 if (dv[1].len != sizeof(b1)) { fprintf(stderr, "x len\n"); exit(2); }
144 if (dv[3].len != sizeof(b0)) { fprintf(stderr, "result len\n"); exit(2); }
145 memcpy(b0, dv[0].buf, sizeof(b0));
146 memcpy(b1, dv[1].buf, sizeof(b1));
147 niter = *(unsigned long *)dv[2].buf;
148 dstr_ensure(&d, X448_OUTSZ); d.len = X448_OUTSZ; t = (octet *)d.buf;
149
150 for (i = 0; i < niter; i++) {
151 x448(x, k, x);
152 t = x; x = k; k = t;
153 }
154 memcpy(d.buf, k, d.len);
155
156 if (MEMCMP(d.buf, !=, dv[3].buf, d.len)) {
157 ok = 0;
158 fprintf(stderr, "failed...");
159 fprintf(stderr, "\n\tinitial k = "); type_hex.dump(&dv[0], stderr);
160 fprintf(stderr, "\n\tinitial x = "); type_hex.dump(&dv[1], stderr);
161 fprintf(stderr, "\n\titerations = %lu", niter);
162 fprintf(stderr, "\n\texpected = "); type_hex.dump(&dv[3], stderr);
163 fprintf(stderr, "\n\tcalculated = "); type_hex.dump(&d, stderr);
164 fputc('\n', stderr);
165 }
166
167 dstr_destroy(&d);
168 return (ok);
169 }
170
171 static test_chunk tests[] = {
172 { "x448", vrf_x448, { &type_hex, &type_hex, &type_hex } },
173 { "x448-mct", vrf_mct,
174 { &type_hex, &type_hex, &type_ulong, &type_hex } },
175 { 0, 0, { 0 } }
176 };
177
178 int main(int argc, char *argv[])
179 {
180 test_run(argc, argv, tests, SRCDIR "/t/x448");
181 return (0);
182 }
183
184 #endif
185
186 /*----- That's all, folks -------------------------------------------------*/