progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / pub / ed25519.c
CommitLineData
d56fd9d1
MW
1/* -*-c-*-
2 *
3 * The Ed25519 signature scheme
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 <string.h>
31
141c1284
MW
32#include <mLib/macros.h>
33
d56fd9d1
MW
34#include "f25519.h"
35#include "ed25519.h"
36#include "scaf.h"
581ac808 37#include "scmul.h"
d56fd9d1
MW
38#include "sha512.h"
39
40/*----- Key fetching ------------------------------------------------------*/
41
42const key_fetchdef ed25519_pubfetch[] = {
43 { "pub", offsetof(ed25519_pub, pub), KENC_BINARY, 0 },
44 { 0, 0, 0, 0 }
45};
46
47static const key_fetchdef priv[] = {
48 { "priv", offsetof(ed25519_priv, priv), KENC_BINARY, 0 },
49 { 0, 0, 0, 0 }
50};
51
52const key_fetchdef ed25519_privfetch[] = {
53 { "pub", offsetof(ed25519_priv, pub), KENC_BINARY, 0 },
54 { "private", 0, KENC_STRUCT, priv },
55 { 0, 0, 0, 0 }
56};
57
58/*----- A number of magic numbers -----------------------------------------*/
59
60#if SCAF_IMPL == 32
61# define PIECEWD 24
62 static const scaf_piece l[] = {
63 0xf5d3ed, 0x631a5c, 0xd65812, 0xa2f79c, 0xdef9de, 0x000014,
64 0x000000, 0x000000, 0x000000, 0x000000, 0x001000
65 };
66 static const scaf_piece mu[] = {
67 0x1b3994, 0x0a2c13, 0x9ce5a3, 0x29a7ed, 0x5d0863, 0x210621,
68 0xffffeb, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x000fff
69 };
70#endif
71
72#if SCAF_IMPL == 16
73# define PIECEWD 12
74 static const scaf_piece l[] = {
75 0x3ed, 0xf5d, 0xa5c, 0x631, 0x812, 0xd65,
76 0x79c, 0xa2f, 0x9de, 0xdef, 0x014, 0x000,
77 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,
78 0x000, 0x000, 0x000, 0x001
79 };
80 static const scaf_piece mu[] = {
81 0x994, 0x1b3, 0xc13, 0x0a2, 0x5a3, 0x9ce,
82 0x7ed, 0x29a, 0x863, 0x5d0, 0x621, 0x210,
83 0xfeb, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff,
84 0xfff, 0xfff, 0xfff, 0xfff, 0xfff
85 };
86#endif
87
88#define NPIECE SCAF_NPIECE(255, PIECEWD)
89
90#if F25519_IMPL == 26
91# define P p26
9c077904 92 static const f25519_piece bx_pieces[] = {
d56fd9d1
MW
93 -14297830, -7645148, 16144683, -16471763, 27570974,
94 -2696100, -26142465, 8378389, 20764389, 8758491
95 }, by_pieces[] = {
a81dbb3f
MW
96 -26843541, -6710886, 13421773, -13421773, 26843546,
97 6710886, -13421773, 13421773, -26843546, -6710886
d56fd9d1 98 }, d_pieces[] = {
a81dbb3f
MW
99 -10913610, 13857413, -15372611, 6949391, 114729,
100 -8787816, -6275908, -3247719, -18696448, -12055116
d56fd9d1
MW
101 };
102#endif
103#if F25519_IMPL == 10
104# define P p10
9c077904 105 static const f25519_piece bx_pieces[] = {
d56fd9d1
MW
106 282, 373, 242, 386, -467, 86, -423, 318, -437,
107 75, 236, -308, 421, 92, 439, -35, 400, 452,
108 82, -40, 160, 441, -51, 437, -365, 134
109 }, by_pieces[] = {
a81dbb3f 110 -405, 410, -410, 410, -410, -102, 205, -205, 205,
d56fd9d1 111 -205, 205, -410, 410, -410, 410, 102, -205, 205,
a81dbb3f 112 -205, 205, -205, 410, -410, 410, -410, -102
d56fd9d1 113 }, d_pieces[] = {
a81dbb3f 114 182, -418, 310, -216, -178, -133, 367, -315, -380,
d56fd9d1 115 -351, -182, -255, 2, 152, -390, -136, -52, -383,
a81dbb3f 116 -412, -398, -12, 448, -469, -196, 55, -184
d56fd9d1
MW
117 };
118#endif
119
9c077904 120static const f25519_piece bz_pieces[NPIECE] = { 1, 0, /* ... */ };
d56fd9d1
MW
121#define BX ((const f25519 *)bx_pieces)
122#define BY ((const f25519 *)by_pieces)
123#define BZ ((const f25519 *)bz_pieces)
124#define D ((const f25519 *)d_pieces)
125
126/*----- Point encoding and decoding ---------------------------------------*/
127
128static void ptencode(octet q[32],
129 const f25519 *X, const f25519 *Y, const f25519 *Z)
130{
131 f25519 x, y, t;
132 octet b[32];
133
134 f25519_inv(&t, Z); f25519_mul(&x, X, &t); f25519_mul(&y, Y, &t);
135 f25519_store(q, &y); f25519_store(b, &x); q[31] |= (b[0]&1u) << 7;
136}
137
138static int ptdecode(f25519 *X, f25519 *Y, f25519 *Z, const octet q[32])
139{
140 octet b[32];
accdbbc9 141 unsigned i, a;
d56fd9d1
MW
142 f25519 t, u;
143 uint32 m;
accdbbc9 144 int rc = 0;
d56fd9d1 145
accdbbc9 146 /* Load the y-coordinate. */
d56fd9d1 147 memcpy(b, q, 32); b[31] &= 0x7fu; f25519_load(Y, b);
accdbbc9
MW
148
149 /* Check that the coordinate was in range. If we store it, we'll get a
150 * canonical version which we can compare against Q; be careful not to
151 * check the top bit.
152 */
153 f25519_store(b, Y);
154 for (i = a = 0; i < 31; i++) a |= b[i] ^ q[i];
155 a |= (b[31] ^ q[31])&0x7fu;
156 a = ((a - 1) >> 8)&0x01u; /* 0 |-> 1, non-0 |-> 0 */
157 rc |= (int)a - 1;
158
159 /* Decompress the x-coordinate. */
d56fd9d1 160 f25519_sqr(&t, Y); f25519_mul(&u, &t, D); t.P[0] -= 1; u.P[0] += 1;
accdbbc9
MW
161 rc |= f25519_quosqrt(X, &t, &u);
162 f25519_store(b, X); m = -(uint32)(((q[31] >> 7) ^ b[0])&0x1u);
d56fd9d1 163 f25519_condneg(X, X, m);
accdbbc9
MW
164
165 /* Set Z. */
d56fd9d1 166 f25519_set(Z, 1);
accdbbc9
MW
167
168 /* And we're done. */
d56fd9d1
MW
169 return (rc);
170}
171
172/*----- Edwards curve arithmetic ------------------------------------------*/
173
174static void ptadd(f25519 *X, f25519 *Y, f25519 *Z,
175 const f25519 *X0, const f25519 *Y0, const f25519 *Z0,
176 const f25519 *X1, const f25519 *Y1, const f25519 *Z1)
177{
3709f795 178 f25519 t0, t1, t2, t3;
d56fd9d1
MW
179
180 /* Bernstein, Birkner, Joye, Lange, and Peters, `Twisted Edwards Curves',
181 * 2008-03-13, https://cr.yp.to/newelliptic/twisted-20080313.pdf shows the
182 * formulae as:
183 *
184 * A = Z1 Z2; B = A^2; C = X1 X2; D = Y1 Y2;
185 * E = d C D; F = B - E; G = B + E;
186 * X3 = A F ((X1 + Y1) (X2 + Y2) - C - D);
187 * Y3 = A G (D - a C); Z3 = F G.
188 *
189 * Note that a = -1, which things easier.
190 */
191
192 f25519_mul(&t0, Z0, Z1); /* t0 = A = Z0 Z1 */
3709f795
MW
193 f25519_add(&t1, X0, Y0); /* t1 = X0 + Y0 */
194 f25519_add(&t2, X1, Y1); /* t2 = X1 + Y1 */
195 f25519_mul(&t1, &t1, &t2); /* t1 = (X0 + Y0) (X1 + Y1) */
d56fd9d1
MW
196 f25519_mul(&t2, X0, X1); /* t2 = C = X0 X1 */
197 f25519_mul(&t3, Y0, Y1); /* t3 = D = Y0 Y1 */
3709f795
MW
198 f25519_add(Y, &t2, &t3); /* Y = C + D = D - a C */
199 f25519_sub(X, &t1, Y); /* X = (X0 + Y0) (X1 + Y1) - C - D */
200 f25519_mul(X, X, &t0); /* X = A ((X0 + Y0) (X1 + Y1) - C - D) */
201 f25519_mul(Y, Y, &t0); /* Y = A (D - a C) */
202 f25519_sqr(&t0, &t0); /* t0 = B = A^2 */
203 f25519_mul(&t1, &t2, &t3); /* t1 = C D */
204 f25519_mul(&t1, &t1, D); /* t1 = E = d C D */
205 f25519_sub(&t2, &t0, &t1); /* t2 = F = B - E */
206 f25519_add(&t1, &t0, &t1); /* t1 = G = B + E */
207 f25519_mul(X, X, &t2); /* X = A F ((X0 + Y0) (X1 + Y1) - C - D) */
208 f25519_mul(Y, Y, &t1); /* Y = A G (D - a C) */
209 f25519_mul(Z, &t1, &t2); /* Z = F G */
d56fd9d1
MW
210}
211
212static void ptdbl(f25519 *X, f25519 *Y, f25519 *Z,
213 const f25519 *X0, const f25519 *Y0, const f25519 *Z0)
214{
215 f25519 t0, t1, t2;
216
217 /* Bernstein, Birkner, Joye, Lange, and Peters, `Twisted Edwards Curves',
218 * 2008-03-13, https://cr.yp.to/newelliptic/twisted-20080313.pdf shows the
219 * formulae as:
220 *
221 * B = (X1 + Y1)^2; C = X1^2; D = Y1^2; E = a C;
222 * F = E + D; H = Z1^2; J = F - 2 H;
223 * X3 = (B - C - D) J; Y3 = F (E - D); Z3 = F J.
224 *
225 * Note that a = -1, which things easier.
226 */
227
228 f25519_add(&t0, X0, Y0); /* t0 = X0 + Y0 */
229 f25519_sqr(&t0, &t0); /* t0 = B = (X0 + Y0)^2 */
230 f25519_sqr(&t1, X0); /* t1 = C = X0^2 */
231 f25519_sqr(&t2, Y0); /* t2 = D = Y0^2 */
232 f25519_add(Y, &t1, &t2); /* Y = C + D = -(E - D) */
233 f25519_sub(X, &t0, Y); /* X = B - C - D */
234 /* (E = a C = -C) */
235 f25519_sub(&t0, &t2, &t1); /* t0 = F = D - C = E + D */
236 f25519_sqr(&t1, Z0); /* t1 = H = Z0^2 */
6baf2bea 237 f25519_add(&t1, &t1, &t1); /* t1 = 2 H */
d56fd9d1
MW
238 f25519_sub(&t1, &t0, &t1); /* t1 = J = F - 2 H */
239 f25519_mul(X, X, &t1); /* X = (B - C - D) J */
240 f25519_mul(Y, Y, &t0); /* Y = -F (E - D) */
241 f25519_neg(Y, Y); /* Y = F (E - D) */
242 f25519_mul(Z, &t0, &t1); /* Z = F J */
243}
244
581ac808
MW
245static DEFINE_SCMUL(ptmul, f25519, 4, PIECEWD, NPIECE, ptadd, ptdbl)
246static DEFINE_SCSIMMUL(ptsimmul, f25519, 2, PIECEWD, NPIECE, ptadd, ptdbl)
d56fd9d1
MW
247
248/*----- Key derivation utilities ------------------------------------------*/
249
250static void unpack_key(scaf_piece a[NPIECE], octet h1[32],
251 const octet *k, size_t ksz)
252{
253 sha512_ctx h;
254 octet b[SHA512_HASHSZ];
255
256 sha512_init(&h); sha512_hash(&h, k, ksz); sha512_done(&h, b);
257 b[0] &= 0xf8u; b[31] = (b[31]&0x3f) | 0x40;
258 scaf_load(a, b, 32, NPIECE, PIECEWD);
cb2f9158 259 if (h1) memcpy(h1, b + 32, 32);
d56fd9d1
MW
260}
261
a7aa36f2
MW
262#define PREFIX_BUFSZ 290
263static size_t prefix(octet b[PREFIX_BUFSZ],
264 int phflag, const octet *p, size_t psz)
265{
266 if (phflag < 0) return (0);
267 memcpy(b, "SigEd25519 no Ed25519 collisions", 32);
268 b[32] = phflag;
269 assert(psz < ED25519_MAXPERSOSZ); b[33] = psz; memcpy(b + 34, p, psz);
270 return (psz + 34);
271}
272
d56fd9d1
MW
273/*----- Main code ---------------------------------------------------------*/
274
275/* --- @ed25519_pubkey@ --- *
276 *
277 * Arguments: @octet K[ED25519_PUBSZ]@ = where to put the public key
278 * @const void *k@ = private key
279 * @size_t ksz@ = length of private key
280 *
281 * Returns: ---
282 *
283 * Use: Derives the public key from a private key.
284 */
285
286void ed25519_pubkey(octet K[ED25519_PUBSZ], const void *k, size_t ksz)
287{
288 scaf_piece a[NPIECE];
289 f25519 AX, AY, AZ;
d56fd9d1 290
cb2f9158 291 unpack_key(a, 0, k, ksz);
d56fd9d1
MW
292 ptmul(&AX, &AY, &AZ, a, BX, BY, BZ);
293 ptencode(K, &AX, &AY, &AZ);
294}
295
a7aa36f2 296/* --- @ed25519_sign@, @ed25519ctx_sign@ --- *
d56fd9d1
MW
297 *
298 * Arguments: @octet sig[ED25519_SIGSZ]@ = where to put the signature
299 * @const void *k@ = private key
300 * @size_t ksz@ = length of private key
301 * @const octet K[ED25519_PUBSZ]@ = public key
a7aa36f2
MW
302 * @int phflag@ = whether the `message' has been hashed already
303 * @const void *p@ = personalization string
304 * @size_t psz@ = length of personalization string
d56fd9d1
MW
305 * @const void *m@ = message to sign
306 * @size_t msz@ = length of message
307 *
308 * Returns: ---
309 *
310 * Use: Signs a message.
a7aa36f2
MW
311 *
312 * In @ed25519ctx_sign@, if @phflag@ is @-1@ then you get plain
313 * old Ed25519: the personalization string pointer @p@ will be
314 * ignored. If @phflag > 0@ then the `message' @m@ should be a
315 * SHA512 hash of the actual message.
d56fd9d1
MW
316 */
317
a7aa36f2
MW
318void ed25519ctx_sign(octet sig[ED25519_SIGSZ],
319 const void *k, size_t ksz, const octet K[ED25519_PUBSZ],
320 int phflag, const void *p, size_t psz,
321 const void *m, size_t msz)
d56fd9d1
MW
322{
323 sha512_ctx h;
e057fe08 324 scaf_piece a[NPIECE], r[NPIECE], t[NPIECE], scratch[3*NPIECE];
d56fd9d1
MW
325 scaf_dblpiece tt[2*NPIECE];
326 f25519 RX, RY, RZ;
a7aa36f2 327 octet h1[32], pb[PREFIX_BUFSZ], rb[SHA512_HASHSZ];
d56fd9d1
MW
328 unsigned i;
329
330 /* Get my private key. */
331 unpack_key(a, h1, k, ksz);
332
a7aa36f2
MW
333 /* Determine the prefix string. */
334 psz = prefix(pb, phflag, p, psz);
335
d56fd9d1
MW
336 /* Select the nonce and the vector part. */
337 sha512_init(&h);
a7aa36f2 338 sha512_hash(&h, pb, psz);
d56fd9d1
MW
339 sha512_hash(&h, h1, 32);
340 sha512_hash(&h, m, msz);
a7aa36f2
MW
341 sha512_done(&h, rb);
342 scaf_loaddbl(tt, rb, 64, 2*NPIECE, PIECEWD);
d56fd9d1
MW
343 scaf_reduce(r, tt, l, mu, NPIECE, PIECEWD, scratch);
344 ptmul(&RX, &RY, &RZ, r, BX, BY, BZ);
345 ptencode(sig, &RX, &RY, &RZ);
346
347 /* Calculate the scalar part. */
348 sha512_init(&h);
a7aa36f2 349 sha512_hash(&h, pb, psz);
d56fd9d1
MW
350 sha512_hash(&h, sig, 32);
351 sha512_hash(&h, K, 32);
352 sha512_hash(&h, m, msz);
a7aa36f2
MW
353 sha512_done(&h, rb);
354 scaf_loaddbl(tt, rb, 64, 2*NPIECE, PIECEWD);
d56fd9d1
MW
355 scaf_reduce(t, tt, l, mu, NPIECE, PIECEWD, scratch);
356 scaf_mul(tt, t, a, NPIECE);
357 for (i = 0; i < NPIECE; i++) tt[i] += r[i];
358 scaf_reduce(t, tt, l, mu, NPIECE, PIECEWD, scratch);
359 scaf_store(sig + 32, 32, t, NPIECE, PIECEWD);
360}
361
a7aa36f2
MW
362void ed25519_sign(octet sig[ED25519_SIGSZ],
363 const void *k, size_t ksz, const octet K[ED25519_PUBSZ],
364 const void *m, size_t msz)
365 { ed25519ctx_sign(sig, k, ksz, K, -1, 0, 0, m, msz); }
366
367/* --- @ed25519_verify@, @ed25519ctx_verify@ --- *
d56fd9d1
MW
368 *
369 * Arguments: @const octet K[ED25519_PUBSZ]@ = public key
a7aa36f2
MW
370 * @int phflag@ = whether the `message' has been hashed already
371 * @const void *p@ = personalization string
372 * @size_t psz@ = length of personalization string
d56fd9d1
MW
373 * @const void *m@ = message to sign
374 * @size_t msz@ = length of message
375 * @const octet sig[ED25519_SIGSZ]@ = signature
376 *
377 * Returns: Zero if OK, negative on failure.
378 *
379 * Use: Verify a signature.
a7aa36f2
MW
380 *
381 * In @ed25519ctx_verify@, if @phflag@ is @-1@ then you get
382 * plain old Ed25519: the personalization string pointer @p@
383 * will be ignored. If @phflag > 0@ then the `message' @m@
384 * should be a SHA512 hash of the actual message.
d56fd9d1
MW
385 */
386
a7aa36f2
MW
387int ed25519ctx_verify(const octet K[ED25519_PUBSZ],
388 int phflag, const void *p, size_t psz,
389 const void *m, size_t msz,
390 const octet sig[ED25519_SIGSZ])
d56fd9d1
MW
391{
392 sha512_ctx h;
e057fe08 393 scaf_piece s[NPIECE], t[NPIECE], scratch[3*NPIECE];
d56fd9d1
MW
394 scaf_dblpiece tt[2*NPIECE];
395 f25519 AX, AY, AZ, RX, RY, RZ;
a7aa36f2 396 octet b[PREFIX_BUFSZ];
d56fd9d1
MW
397
398 /* Unpack the public key. Negate it: we're meant to subtract the term
399 * involving the public key point, and this is easier than negating the
400 * scalar.
401 */
402 if (ptdecode(&AX, &AY, &AZ, K)) return (-1);
403 f25519_neg(&AX, &AX);
404
accdbbc9
MW
405 /* Load the scalar and check that it's in range. The easy way is to store
406 * it again and see if the two match.
407 */
408 scaf_loaddbl(tt, sig + 32, 32, 2*NPIECE, PIECEWD);
409 scaf_reduce(s, tt, l, mu, NPIECE, PIECEWD, scratch);
410 scaf_store(b, 32, s, NPIECE, PIECEWD);
141c1284 411 if (MEMCMP(b, !=, sig + 32, 32)) return (-1);
accdbbc9 412
d56fd9d1 413 /* Check the signature. */
a7aa36f2 414 psz = prefix(b, phflag, p, psz);
d56fd9d1 415 sha512_init(&h);
a7aa36f2 416 sha512_hash(&h, b, psz);
d56fd9d1
MW
417 sha512_hash(&h, sig, 32);
418 sha512_hash(&h, K, 32);
419 sha512_hash(&h, m, msz);
420 sha512_done(&h, b);
d56fd9d1
MW
421 scaf_loaddbl(tt, b, 64, 2*NPIECE, PIECEWD);
422 scaf_reduce(t, tt, l, mu, NPIECE, PIECEWD, scratch);
423 ptsimmul(&RX, &RY, &RZ, s, BX, BY, BZ, t, &AX, &AY, &AZ);
424 ptencode(b, &RX, &RY, &RZ);
141c1284 425 if (MEMCMP(b, !=, sig, 32)) return (-1);
d56fd9d1
MW
426
427 /* All is good. */
428 return (0);
429}
430
a7aa36f2
MW
431int ed25519_verify(const octet K[ED25519_PUBSZ],
432 const void *m, size_t msz,
433 const octet sig[ED25519_SIGSZ])
434 { return (ed25519ctx_verify(K, -1, 0, 0, m, msz, sig)); }
435
d56fd9d1
MW
436/*----- Test rig ----------------------------------------------------------*/
437
438#ifdef TEST_RIG
439
440#include <stdio.h>
441#include <string.h>
442
443#include <mLib/report.h>
444#include <mLib/testrig.h>
445
1aaccf40
MW
446#include "ct.h"
447
d56fd9d1
MW
448static int vrf_pubkey(dstr dv[])
449{
450 dstr dpub = DSTR_INIT;
451 int ok = 1;
452
1b59808c 453 if (dv[1].len != ED25519_PUBSZ) die(1, "bad pub length");
d56fd9d1 454
1aaccf40 455 ct_poison(dv[0].buf, dv[0].len);
1b59808c 456 dstr_ensure(&dpub, ED25519_PUBSZ); dpub.len = ED25519_PUBSZ;
d56fd9d1 457 ed25519_pubkey((octet *)dpub.buf, dv[0].buf, dv[0].len);
1aaccf40 458 ct_remedy(dpub.buf, dpub.len);
141c1284 459 if (MEMCMP(dpub.buf, !=, dv[1].buf, ED25519_PUBSZ)) {
d56fd9d1
MW
460 ok = 0;
461 fprintf(stderr, "failed!");
462 fprintf(stderr, "\n\tpriv = "); type_hex.dump(&dv[0], stderr);
463 fprintf(stderr, "\n\tcalc = "); type_hex.dump(&dpub, stderr);
464 fprintf(stderr, "\n\twant = "); type_hex.dump(&dv[1], stderr);
465 fprintf(stderr, "\n");
466 }
467
468 dstr_destroy(&dpub);
469 return (ok);
470}
471
a7aa36f2
MW
472static int vrf_sign(dstr *priv, int phflag, dstr *perso,
473 dstr *msg, dstr *want)
d56fd9d1 474{
a7aa36f2 475 sha512_ctx h;
d56fd9d1 476 octet K[ED25519_PUBSZ];
a7aa36f2 477 dstr d = DSTR_INIT, dsig = DSTR_INIT, *m;
d56fd9d1
MW
478 int ok = 1;
479
a7aa36f2 480 if (want->len != ED25519_SIGSZ) die(1, "bad result length");
d56fd9d1 481
1aaccf40 482 ct_poison(priv->buf, priv->len);
1b59808c 483 dstr_ensure(&dsig, ED25519_SIGSZ); dsig.len = ED25519_SIGSZ;
a7aa36f2
MW
484 if (phflag <= 0)
485 m = msg;
486 else {
487 dstr_ensure(&d, SHA512_HASHSZ); d.len = SHA512_HASHSZ;
488 sha512_init(&h);
489 sha512_hash(&h, msg->buf, msg->len);
490 sha512_done(&h, d.buf);
491 m = &d;
492 }
493 ed25519_pubkey(K, priv->buf, priv->len);
494 ed25519ctx_sign((octet *)dsig.buf, priv->buf, priv->len, K,
495 phflag, perso ? perso->buf : 0, perso ? perso->len : 0,
496 m->buf, m->len);
1aaccf40 497 ct_remedy(dsig.buf, dsig.len);
141c1284 498 if (MEMCMP(dsig.buf, !=, want->buf, ED25519_SIGSZ)) {
d56fd9d1
MW
499 ok = 0;
500 fprintf(stderr, "failed!");
a7aa36f2
MW
501 fprintf(stderr, "\n\tpriv = "); type_hex.dump(priv, stderr);
502 if (phflag >= 0) {
503 fprintf(stderr, "\n\t ph = %d", phflag);
504 fprintf(stderr, "\n\tpers = "); type_hex.dump(perso, stderr);
505 }
506 fprintf(stderr, "\n\t msg = "); type_hex.dump(msg, stderr);
507 if (phflag > 0)
508 { fprintf(stderr, "\n\thash = "); type_hex.dump(m, stderr); }
d56fd9d1 509 fprintf(stderr, "\n\tcalc = "); type_hex.dump(&dsig, stderr);
a7aa36f2 510 fprintf(stderr, "\n\twant = "); type_hex.dump(want, stderr);
d56fd9d1
MW
511 fprintf(stderr, "\n");
512 }
513
514 dstr_destroy(&dsig);
515 return (ok);
516}
517
a7aa36f2
MW
518static int vrf_sign_trad(dstr *dv)
519 { return (vrf_sign(&dv[0], -1, 0, &dv[1], &dv[2])); }
520
521static int vrf_sign_ctx(dstr *dv)
522 { return (vrf_sign(&dv[0], *(int *)dv[1].buf, &dv[2], &dv[3], &dv[4])); }
523
524static int vrf_verify(dstr *pub, int phflag, dstr *perso,
525 dstr *msg, dstr *sig, int rc_want)
d56fd9d1 526{
a7aa36f2
MW
527 sha512_ctx h;
528 int rc_calc;
529 dstr d = DSTR_INIT, *m;
d56fd9d1
MW
530 int ok = 1;
531
a7aa36f2
MW
532 if (pub->len != ED25519_PUBSZ) die(1, "bad pub length");
533 if (sig->len != ED25519_SIGSZ) die(1, "bad sig length");
534
535 if (phflag <= 0)
536 m = msg;
537 else {
538 dstr_ensure(&d, SHA512_HASHSZ); d.len = SHA512_HASHSZ;
539 sha512_init(&h);
540 sha512_hash(&h, msg->buf, msg->len);
541 sha512_done(&h, d.buf);
542 m = &d;
543 }
544 rc_calc = ed25519ctx_verify((const octet *)pub->buf,
545 phflag, perso ? perso->buf : 0,
546 perso ? perso->len : 0,
547 m->buf, m->len,
548 (const octet *)sig->buf);
d56fd9d1
MW
549 if (!rc_want != !rc_calc) {
550 ok = 0;
551 fprintf(stderr, "failed!");
a7aa36f2
MW
552 fprintf(stderr, "\n\t pub = "); type_hex.dump(pub, stderr);
553 if (phflag >= 0) {
554 fprintf(stderr, "\n\t ph = %d", phflag);
555 fprintf(stderr, "\n\tpers = "); type_hex.dump(perso, stderr);
556 }
557 fprintf(stderr, "\n\t msg = "); type_hex.dump(msg, stderr);
558 if (phflag > 0)
559 { fprintf(stderr, "\n\thash = "); type_hex.dump(m, stderr); }
560 fprintf(stderr, "\n\t sig = "); type_hex.dump(sig, stderr);
d56fd9d1
MW
561 fprintf(stderr, "\n\tcalc = %d", rc_calc);
562 fprintf(stderr, "\n\twant = %d", rc_want);
563 fprintf(stderr, "\n");
564 }
565
566 return (ok);
567}
568
a7aa36f2
MW
569static int vrf_verify_trad(dstr *dv)
570 { return (vrf_verify(&dv[0], -1, 0, &dv[1], &dv[2], *(int *)dv[3].buf)); }
571
572static int vrf_verify_ctx(dstr *dv)
573{
574 return (vrf_verify(&dv[0], *(int *)dv[1].buf, &dv[2],
575 &dv[3], &dv[4], *(int *)dv[5].buf));
576}
577
d56fd9d1 578static test_chunk tests[] = {
a7aa36f2
MW
579 { "pubkey", vrf_pubkey,
580 { &type_hex, &type_hex } },
581 { "sign", vrf_sign_trad,
582 { &type_hex, &type_hex, &type_hex } },
583 { "verify", vrf_verify_trad,
584 { &type_hex, &type_hex, &type_hex, &type_int } },
585 { "sign-ctx", vrf_sign_ctx,
586 { &type_hex, &type_int, &type_hex, &type_hex, &type_hex } },
587 { "verify-ctx", vrf_verify_ctx,
588 { &type_hex, &type_int, &type_hex, &type_hex, &type_hex, &type_int } },
d56fd9d1
MW
589 { 0, 0, { 0 } }
590};
591
592int main(int argc, char *argv[])
593{
594 test_run(argc, argv, tests, SRCDIR "/t/ed25519");
595 return (0);
596}
597
598#endif
599
600/*----- That's all, folks -------------------------------------------------*/