blob: 8da2feaade37ad969b4b13afe4dd671c1ca798eb [file] [log] [blame]
Damien Millerf7540cd2010-09-24 22:03:24 +10001/* $OpenBSD: schnorr.c,v 1.4 2010/09/20 04:50:53 djm Exp $ */
Damien Miller01ed2272008-11-05 16:20:46 +11002/*
3 * Copyright (c) 2008 Damien Miller. All rights reserved.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/*
19 * Implementation of Schnorr signatures / zero-knowledge proofs, based on
20 * description in:
21 *
22 * F. Hao, P. Ryan, "Password Authenticated Key Exchange by Juggling",
23 * 16th Workshop on Security Protocols, Cambridge, April 2008
24 *
25 * http://grouper.ieee.org/groups/1363/Research/contributions/hao-ryan-2008.pdf
26 */
27
28#include "includes.h"
29
30#include <sys/types.h>
31
32#include <string.h>
33#include <stdarg.h>
34#include <stdio.h>
35
36#include <openssl/evp.h>
37#include <openssl/bn.h>
38
39#include "xmalloc.h"
40#include "buffer.h"
41#include "log.h"
42
Damien Millercee85232009-03-06 00:58:22 +110043#include "schnorr.h"
Damien Miller01ed2272008-11-05 16:20:46 +110044
Darren Tucker8aae6ff2009-03-07 12:01:47 +110045#include "openbsd-compat/openssl-compat.h"
46
Damien Miller01ed2272008-11-05 16:20:46 +110047/* #define SCHNORR_DEBUG */ /* Privacy-violating debugging */
48/* #define SCHNORR_MAIN */ /* Include main() selftest */
49
Damien Miller01ed2272008-11-05 16:20:46 +110050#ifndef SCHNORR_DEBUG
51# define SCHNORR_DEBUG_BN(a)
52# define SCHNORR_DEBUG_BUF(a)
53#else
Damien Millercee85232009-03-06 00:58:22 +110054# define SCHNORR_DEBUG_BN(a) debug3_bn a
55# define SCHNORR_DEBUG_BUF(a) debug3_buf a
Damien Miller01ed2272008-11-05 16:20:46 +110056#endif /* SCHNORR_DEBUG */
57
58/*
59 * Calculate hash component of Schnorr signature H(g || g^v || g^x || id)
Damien Millercee85232009-03-06 00:58:22 +110060 * using the hash function defined by "evp_md". Returns signature as
61 * bignum or NULL on error.
Damien Miller01ed2272008-11-05 16:20:46 +110062 */
63static BIGNUM *
64schnorr_hash(const BIGNUM *p, const BIGNUM *q, const BIGNUM *g,
Damien Millercee85232009-03-06 00:58:22 +110065 const EVP_MD *evp_md, const BIGNUM *g_v, const BIGNUM *g_x,
Damien Miller01ed2272008-11-05 16:20:46 +110066 const u_char *id, u_int idlen)
67{
68 u_char *digest;
69 u_int digest_len;
70 BIGNUM *h;
Damien Miller01ed2272008-11-05 16:20:46 +110071 Buffer b;
72 int success = -1;
73
74 if ((h = BN_new()) == NULL) {
75 error("%s: BN_new", __func__);
76 return NULL;
77 }
78
79 buffer_init(&b);
Damien Miller01ed2272008-11-05 16:20:46 +110080
Damien Miller25918382009-02-21 12:45:18 +110081 /* h = H(g || p || q || g^v || g^x || id) */
Damien Miller01ed2272008-11-05 16:20:46 +110082 buffer_put_bignum2(&b, g);
Damien Miller25918382009-02-21 12:45:18 +110083 buffer_put_bignum2(&b, p);
84 buffer_put_bignum2(&b, q);
Damien Miller01ed2272008-11-05 16:20:46 +110085 buffer_put_bignum2(&b, g_v);
86 buffer_put_bignum2(&b, g_x);
87 buffer_put_string(&b, id, idlen);
88
89 SCHNORR_DEBUG_BUF((buffer_ptr(&b), buffer_len(&b),
90 "%s: hashblob", __func__));
Damien Millercee85232009-03-06 00:58:22 +110091 if (hash_buffer(buffer_ptr(&b), buffer_len(&b), evp_md,
Damien Miller01ed2272008-11-05 16:20:46 +110092 &digest, &digest_len) != 0) {
93 error("%s: hash_buffer", __func__);
94 goto out;
95 }
96 if (BN_bin2bn(digest, (int)digest_len, h) == NULL) {
97 error("%s: BN_bin2bn", __func__);
98 goto out;
99 }
100 success = 0;
101 SCHNORR_DEBUG_BN((h, "%s: h = ", __func__));
102 out:
103 buffer_free(&b);
Damien Miller01ed2272008-11-05 16:20:46 +1100104 bzero(digest, digest_len);
105 xfree(digest);
106 digest_len = 0;
107 if (success == 0)
108 return h;
109 BN_clear_free(h);
110 return NULL;
111}
112
113/*
114 * Generate Schnorr signature to prove knowledge of private value 'x' used
115 * in public exponent g^x, under group defined by 'grp_p', 'grp_q' and 'grp_g'
Damien Millercee85232009-03-06 00:58:22 +1100116 * using the hash function "evp_md".
Damien Miller01ed2272008-11-05 16:20:46 +1100117 * 'idlen' bytes from 'id' will be included in the signature hash as an anti-
118 * replay salt.
Damien Millercee85232009-03-06 00:58:22 +1100119 *
120 * On success, 0 is returned. The signature values are returned as *e_p
121 * (g^v mod p) and *r_p (v - xh mod q). The caller must free these values.
122 * On failure, -1 is returned.
Damien Miller01ed2272008-11-05 16:20:46 +1100123 */
124int
125schnorr_sign(const BIGNUM *grp_p, const BIGNUM *grp_q, const BIGNUM *grp_g,
Damien Millercee85232009-03-06 00:58:22 +1100126 const EVP_MD *evp_md, const BIGNUM *x, const BIGNUM *g_x,
127 const u_char *id, u_int idlen, BIGNUM **r_p, BIGNUM **e_p)
Damien Miller01ed2272008-11-05 16:20:46 +1100128{
129 int success = -1;
Damien Miller01ed2272008-11-05 16:20:46 +1100130 BIGNUM *h, *tmp, *v, *g_v, *r;
131 BN_CTX *bn_ctx;
132
133 SCHNORR_DEBUG_BN((x, "%s: x = ", __func__));
134 SCHNORR_DEBUG_BN((g_x, "%s: g_x = ", __func__));
135
136 /* Avoid degenerate cases: g^0 yields a spoofable signature */
137 if (BN_cmp(g_x, BN_value_one()) <= 0) {
138 error("%s: g_x < 1", __func__);
139 return -1;
140 }
Damien Millerf7540cd2010-09-24 22:03:24 +1000141 if (BN_cmp(g_x, grp_p) >= 0) {
142 error("%s: g_x > g", __func__);
143 return -1;
144 }
Damien Miller01ed2272008-11-05 16:20:46 +1100145
146 h = g_v = r = tmp = v = NULL;
147 if ((bn_ctx = BN_CTX_new()) == NULL) {
148 error("%s: BN_CTX_new", __func__);
149 goto out;
150 }
151 if ((g_v = BN_new()) == NULL ||
152 (r = BN_new()) == NULL ||
153 (tmp = BN_new()) == NULL) {
154 error("%s: BN_new", __func__);
155 goto out;
156 }
157
158 /*
159 * v must be a random element of Zq, so 1 <= v < q
160 * we also exclude v = 1, since g^1 looks dangerous
161 */
162 if ((v = bn_rand_range_gt_one(grp_p)) == NULL) {
163 error("%s: bn_rand_range2", __func__);
164 goto out;
165 }
166 SCHNORR_DEBUG_BN((v, "%s: v = ", __func__));
167
168 /* g_v = g^v mod p */
169 if (BN_mod_exp(g_v, grp_g, v, grp_p, bn_ctx) == -1) {
170 error("%s: BN_mod_exp (g^v mod p)", __func__);
171 goto out;
172 }
173 SCHNORR_DEBUG_BN((g_v, "%s: g_v = ", __func__));
174
175 /* h = H(g || g^v || g^x || id) */
Damien Millercee85232009-03-06 00:58:22 +1100176 if ((h = schnorr_hash(grp_p, grp_q, grp_g, evp_md, g_v, g_x,
Damien Miller01ed2272008-11-05 16:20:46 +1100177 id, idlen)) == NULL) {
178 error("%s: schnorr_hash failed", __func__);
179 goto out;
180 }
181
182 /* r = v - xh mod q */
183 if (BN_mod_mul(tmp, x, h, grp_q, bn_ctx) == -1) {
184 error("%s: BN_mod_mul (tmp = xv mod q)", __func__);
185 goto out;
186 }
187 if (BN_mod_sub(r, v, tmp, grp_q, bn_ctx) == -1) {
188 error("%s: BN_mod_mul (r = v - tmp)", __func__);
189 goto out;
190 }
Damien Millercee85232009-03-06 00:58:22 +1100191 SCHNORR_DEBUG_BN((g_v, "%s: e = ", __func__));
Damien Miller01ed2272008-11-05 16:20:46 +1100192 SCHNORR_DEBUG_BN((r, "%s: r = ", __func__));
193
Damien Millercee85232009-03-06 00:58:22 +1100194 *e_p = g_v;
195 *r_p = r;
196
Damien Miller01ed2272008-11-05 16:20:46 +1100197 success = 0;
198 out:
199 BN_CTX_free(bn_ctx);
200 if (h != NULL)
201 BN_clear_free(h);
202 if (v != NULL)
203 BN_clear_free(v);
Damien Miller01ed2272008-11-05 16:20:46 +1100204 BN_clear_free(tmp);
205
206 return success;
207}
208
209/*
Damien Millercee85232009-03-06 00:58:22 +1100210 * Generate Schnorr signature to prove knowledge of private value 'x' used
211 * in public exponent g^x, under group defined by 'grp_p', 'grp_q' and 'grp_g'
212 * using a SHA256 hash.
213 * 'idlen' bytes from 'id' will be included in the signature hash as an anti-
214 * replay salt.
215 * On success, 0 is returned and *siglen bytes of signature are returned in
216 * *sig (caller to free). Returns -1 on failure.
217 */
218int
219schnorr_sign_buf(const BIGNUM *grp_p, const BIGNUM *grp_q, const BIGNUM *grp_g,
220 const BIGNUM *x, const BIGNUM *g_x, const u_char *id, u_int idlen,
221 u_char **sig, u_int *siglen)
222{
223 Buffer b;
224 BIGNUM *r, *e;
225
226 if (schnorr_sign(grp_p, grp_q, grp_g, EVP_sha256(),
227 x, g_x, id, idlen, &r, &e) != 0)
228 return -1;
229
230 /* Signature is (e, r) */
231 buffer_init(&b);
232 /* XXX sigtype-hash as string? */
233 buffer_put_bignum2(&b, e);
234 buffer_put_bignum2(&b, r);
235 *siglen = buffer_len(&b);
236 *sig = xmalloc(*siglen);
237 memcpy(*sig, buffer_ptr(&b), *siglen);
238 SCHNORR_DEBUG_BUF((buffer_ptr(&b), buffer_len(&b),
239 "%s: sigblob", __func__));
240 buffer_free(&b);
241
242 BN_clear_free(r);
243 BN_clear_free(e);
244
245 return 0;
246}
247
248/*
249 * Verify Schnorr signature { r (v - xh mod q), e (g^v mod p) } against
250 * public exponent g_x (g^x) under group defined by 'grp_p', 'grp_q' and
251 * 'grp_g' using hash "evp_md".
Damien Miller01ed2272008-11-05 16:20:46 +1100252 * Signature hash will be salted with 'idlen' bytes from 'id'.
253 * Returns -1 on failure, 0 on incorrect signature or 1 on matching signature.
254 */
255int
256schnorr_verify(const BIGNUM *grp_p, const BIGNUM *grp_q, const BIGNUM *grp_g,
Damien Millercee85232009-03-06 00:58:22 +1100257 const EVP_MD *evp_md, const BIGNUM *g_x, const u_char *id, u_int idlen,
258 const BIGNUM *r, const BIGNUM *e)
Damien Miller01ed2272008-11-05 16:20:46 +1100259{
260 int success = -1;
Damien Millercee85232009-03-06 00:58:22 +1100261 BIGNUM *h, *g_xh, *g_r, *expected;
Damien Miller01ed2272008-11-05 16:20:46 +1100262 BN_CTX *bn_ctx;
Damien Miller01ed2272008-11-05 16:20:46 +1100263
264 SCHNORR_DEBUG_BN((g_x, "%s: g_x = ", __func__));
265
266 /* Avoid degenerate cases: g^0 yields a spoofable signature */
267 if (BN_cmp(g_x, BN_value_one()) <= 0) {
268 error("%s: g_x < 1", __func__);
269 return -1;
270 }
Damien Millerf7540cd2010-09-24 22:03:24 +1000271 if (BN_cmp(g_x, grp_p) >= 0) {
272 error("%s: g_x >= p", __func__);
273 return -1;
274 }
Damien Miller01ed2272008-11-05 16:20:46 +1100275
Damien Millercee85232009-03-06 00:58:22 +1100276 h = g_xh = g_r = expected = NULL;
Damien Miller01ed2272008-11-05 16:20:46 +1100277 if ((bn_ctx = BN_CTX_new()) == NULL) {
278 error("%s: BN_CTX_new", __func__);
279 goto out;
280 }
Damien Millercee85232009-03-06 00:58:22 +1100281 if ((g_xh = BN_new()) == NULL ||
Damien Miller01ed2272008-11-05 16:20:46 +1100282 (g_r = BN_new()) == NULL ||
283 (expected = BN_new()) == NULL) {
284 error("%s: BN_new", __func__);
285 goto out;
286 }
287
Damien Millercee85232009-03-06 00:58:22 +1100288 SCHNORR_DEBUG_BN((e, "%s: e = ", __func__));
Damien Miller01ed2272008-11-05 16:20:46 +1100289 SCHNORR_DEBUG_BN((r, "%s: r = ", __func__));
290
291 /* h = H(g || g^v || g^x || id) */
Damien Millercee85232009-03-06 00:58:22 +1100292 if ((h = schnorr_hash(grp_p, grp_q, grp_g, evp_md, e, g_x,
Damien Miller01ed2272008-11-05 16:20:46 +1100293 id, idlen)) == NULL) {
294 error("%s: schnorr_hash failed", __func__);
295 goto out;
296 }
297
298 /* g_xh = (g^x)^h */
299 if (BN_mod_exp(g_xh, g_x, h, grp_p, bn_ctx) == -1) {
300 error("%s: BN_mod_exp (g_x^h mod p)", __func__);
301 goto out;
302 }
303 SCHNORR_DEBUG_BN((g_xh, "%s: g_xh = ", __func__));
304
305 /* g_r = g^r */
306 if (BN_mod_exp(g_r, grp_g, r, grp_p, bn_ctx) == -1) {
307 error("%s: BN_mod_exp (g_x^h mod p)", __func__);
308 goto out;
309 }
310 SCHNORR_DEBUG_BN((g_r, "%s: g_r = ", __func__));
311
312 /* expected = g^r * g_xh */
313 if (BN_mod_mul(expected, g_r, g_xh, grp_p, bn_ctx) == -1) {
314 error("%s: BN_mod_mul (expected = g_r mod p)", __func__);
315 goto out;
316 }
317 SCHNORR_DEBUG_BN((expected, "%s: expected = ", __func__));
318
Damien Millercee85232009-03-06 00:58:22 +1100319 /* Check e == expected */
320 success = BN_cmp(expected, e) == 0;
Damien Miller01ed2272008-11-05 16:20:46 +1100321 out:
322 BN_CTX_free(bn_ctx);
323 if (h != NULL)
324 BN_clear_free(h);
Damien Miller01ed2272008-11-05 16:20:46 +1100325 BN_clear_free(g_xh);
326 BN_clear_free(g_r);
327 BN_clear_free(expected);
328 return success;
329}
330
Damien Millercee85232009-03-06 00:58:22 +1100331/*
332 * Verify Schnorr signature 'sig' of length 'siglen' against public exponent
333 * g_x (g^x) under group defined by 'grp_p', 'grp_q' and 'grp_g' using a
334 * SHA256 hash.
335 * Signature hash will be salted with 'idlen' bytes from 'id'.
336 * Returns -1 on failure, 0 on incorrect signature or 1 on matching signature.
337 */
338int
339schnorr_verify_buf(const BIGNUM *grp_p, const BIGNUM *grp_q,
340 const BIGNUM *grp_g,
341 const BIGNUM *g_x, const u_char *id, u_int idlen,
342 const u_char *sig, u_int siglen)
343{
344 Buffer b;
345 int ret = -1;
346 u_int rlen;
347 BIGNUM *r, *e;
348
349 e = r = NULL;
350 if ((e = BN_new()) == NULL ||
351 (r = BN_new()) == NULL) {
352 error("%s: BN_new", __func__);
353 goto out;
354 }
355
356 /* Extract g^v and r from signature blob */
357 buffer_init(&b);
358 buffer_append(&b, sig, siglen);
359 SCHNORR_DEBUG_BUF((buffer_ptr(&b), buffer_len(&b),
360 "%s: sigblob", __func__));
361 buffer_get_bignum2(&b, e);
362 buffer_get_bignum2(&b, r);
363 rlen = buffer_len(&b);
364 buffer_free(&b);
365 if (rlen != 0) {
366 error("%s: remaining bytes in signature %d", __func__, rlen);
367 goto out;
368 }
369
370 ret = schnorr_verify(grp_p, grp_q, grp_g, EVP_sha256(),
371 g_x, id, idlen, r, e);
372 out:
373 BN_clear_free(e);
374 BN_clear_free(r);
375
376 return ret;
377}
378
379/* Helper functions */
380
381/*
382 * Generate uniformly distributed random number in range (1, high).
383 * Return number on success, NULL on failure.
384 */
385BIGNUM *
386bn_rand_range_gt_one(const BIGNUM *high)
387{
388 BIGNUM *r, *tmp;
389 int success = -1;
390
391 if ((tmp = BN_new()) == NULL) {
392 error("%s: BN_new", __func__);
393 return NULL;
394 }
395 if ((r = BN_new()) == NULL) {
396 error("%s: BN_new failed", __func__);
397 goto out;
398 }
399 if (BN_set_word(tmp, 2) != 1) {
400 error("%s: BN_set_word(tmp, 2)", __func__);
401 goto out;
402 }
403 if (BN_sub(tmp, high, tmp) == -1) {
404 error("%s: BN_sub failed (tmp = high - 2)", __func__);
405 goto out;
406 }
407 if (BN_rand_range(r, tmp) == -1) {
408 error("%s: BN_rand_range failed", __func__);
409 goto out;
410 }
411 if (BN_set_word(tmp, 2) != 1) {
412 error("%s: BN_set_word(tmp, 2)", __func__);
413 goto out;
414 }
415 if (BN_add(r, r, tmp) == -1) {
416 error("%s: BN_add failed (r = r + 2)", __func__);
417 goto out;
418 }
419 success = 0;
420 out:
421 BN_clear_free(tmp);
422 if (success == 0)
423 return r;
424 BN_clear_free(r);
425 return NULL;
426}
427
428/*
429 * Hash contents of buffer 'b' with hash 'md'. Returns 0 on success,
430 * with digest via 'digestp' (caller to free) and length via 'lenp'.
431 * Returns -1 on failure.
432 */
433int
434hash_buffer(const u_char *buf, u_int len, const EVP_MD *md,
435 u_char **digestp, u_int *lenp)
436{
437 u_char digest[EVP_MAX_MD_SIZE];
438 u_int digest_len;
439 EVP_MD_CTX evp_md_ctx;
440 int success = -1;
441
442 EVP_MD_CTX_init(&evp_md_ctx);
443
444 if (EVP_DigestInit_ex(&evp_md_ctx, md, NULL) != 1) {
445 error("%s: EVP_DigestInit_ex", __func__);
446 goto out;
447 }
448 if (EVP_DigestUpdate(&evp_md_ctx, buf, len) != 1) {
449 error("%s: EVP_DigestUpdate", __func__);
450 goto out;
451 }
452 if (EVP_DigestFinal_ex(&evp_md_ctx, digest, &digest_len) != 1) {
453 error("%s: EVP_DigestFinal_ex", __func__);
454 goto out;
455 }
456 *digestp = xmalloc(digest_len);
457 *lenp = digest_len;
458 memcpy(*digestp, digest, *lenp);
459 success = 0;
460 out:
461 EVP_MD_CTX_cleanup(&evp_md_ctx);
462 bzero(digest, sizeof(digest));
463 digest_len = 0;
464 return success;
465}
466
467/* print formatted string followed by bignum */
468void
469debug3_bn(const BIGNUM *n, const char *fmt, ...)
470{
471 char *out, *h;
472 va_list args;
473
474 out = NULL;
475 va_start(args, fmt);
476 vasprintf(&out, fmt, args);
477 va_end(args);
478 if (out == NULL)
479 fatal("%s: vasprintf failed", __func__);
480
481 if (n == NULL)
482 debug3("%s(null)", out);
483 else {
484 h = BN_bn2hex(n);
485 debug3("%s0x%s", out, h);
486 free(h);
487 }
488 free(out);
489}
490
491/* print formatted string followed by buffer contents in hex */
492void
493debug3_buf(const u_char *buf, u_int len, const char *fmt, ...)
494{
495 char *out, h[65];
496 u_int i, j;
497 va_list args;
498
499 out = NULL;
500 va_start(args, fmt);
501 vasprintf(&out, fmt, args);
502 va_end(args);
503 if (out == NULL)
504 fatal("%s: vasprintf failed", __func__);
505
506 debug3("%s length %u%s", out, len, buf == NULL ? " (null)" : "");
507 free(out);
508 if (buf == NULL)
509 return;
510
511 *h = '\0';
512 for (i = j = 0; i < len; i++) {
513 snprintf(h + j, sizeof(h) - j, "%02x", buf[i]);
514 j += 2;
515 if (j >= sizeof(h) - 1 || i == len - 1) {
516 debug3(" %s", h);
517 *h = '\0';
518 j = 0;
519 }
520 }
521}
522
523/*
524 * Construct a MODP group from hex strings p (which must be a safe
525 * prime) and g, automatically calculating subgroup q as (p / 2)
526 */
527struct modp_group *
528modp_group_from_g_and_safe_p(const char *grp_g, const char *grp_p)
529{
530 struct modp_group *ret;
531
532 ret = xmalloc(sizeof(*ret));
533 ret->p = ret->q = ret->g = NULL;
534 if (BN_hex2bn(&ret->p, grp_p) == 0 ||
535 BN_hex2bn(&ret->g, grp_g) == 0)
536 fatal("%s: BN_hex2bn", __func__);
537 /* Subgroup order is p/2 (p is a safe prime) */
538 if ((ret->q = BN_new()) == NULL)
539 fatal("%s: BN_new", __func__);
540 if (BN_rshift1(ret->q, ret->p) != 1)
541 fatal("%s: BN_rshift1", __func__);
542
543 return ret;
544}
545
546void
547modp_group_free(struct modp_group *grp)
548{
549 if (grp->g != NULL)
550 BN_clear_free(grp->g);
551 if (grp->p != NULL)
552 BN_clear_free(grp->p);
553 if (grp->q != NULL)
554 BN_clear_free(grp->q);
555 bzero(grp, sizeof(*grp));
556 xfree(grp);
557}
558
559/* main() function for self-test */
560
Damien Miller01ed2272008-11-05 16:20:46 +1100561#ifdef SCHNORR_MAIN
562static void
563schnorr_selftest_one(const BIGNUM *grp_p, const BIGNUM *grp_q,
564 const BIGNUM *grp_g, const BIGNUM *x)
565{
566 BIGNUM *g_x;
567 u_char *sig;
568 u_int siglen;
569 BN_CTX *bn_ctx;
570
571 if ((bn_ctx = BN_CTX_new()) == NULL)
572 fatal("%s: BN_CTX_new", __func__);
573 if ((g_x = BN_new()) == NULL)
574 fatal("%s: BN_new", __func__);
575
576 if (BN_mod_exp(g_x, grp_g, x, grp_p, bn_ctx) == -1)
577 fatal("%s: g_x", __func__);
Damien Millercee85232009-03-06 00:58:22 +1100578 if (schnorr_sign_buf(grp_p, grp_q, grp_g, x, g_x, "junk", 4,
579 &sig, &siglen))
Damien Miller01ed2272008-11-05 16:20:46 +1100580 fatal("%s: schnorr_sign", __func__);
Damien Millercee85232009-03-06 00:58:22 +1100581 if (schnorr_verify_buf(grp_p, grp_q, grp_g, g_x, "junk", 4,
Damien Miller01ed2272008-11-05 16:20:46 +1100582 sig, siglen) != 1)
583 fatal("%s: verify fail", __func__);
Damien Millercee85232009-03-06 00:58:22 +1100584 if (schnorr_verify_buf(grp_p, grp_q, grp_g, g_x, "JUNK", 4,
Damien Miller01ed2272008-11-05 16:20:46 +1100585 sig, siglen) != 0)
586 fatal("%s: verify should have failed (bad ID)", __func__);
587 sig[4] ^= 1;
Damien Millercee85232009-03-06 00:58:22 +1100588 if (schnorr_verify_buf(grp_p, grp_q, grp_g, g_x, "junk", 4,
Damien Miller01ed2272008-11-05 16:20:46 +1100589 sig, siglen) != 0)
590 fatal("%s: verify should have failed (bit error)", __func__);
591 xfree(sig);
592 BN_free(g_x);
593 BN_CTX_free(bn_ctx);
594}
595
596static void
597schnorr_selftest(void)
598{
599 BIGNUM *x;
Damien Millercee85232009-03-06 00:58:22 +1100600 struct modp_group *grp;
Damien Miller01ed2272008-11-05 16:20:46 +1100601 u_int i;
602 char *hh;
603
604 grp = jpake_default_group();
605 if ((x = BN_new()) == NULL)
606 fatal("%s: BN_new", __func__);
607 SCHNORR_DEBUG_BN((grp->p, "%s: grp->p = ", __func__));
608 SCHNORR_DEBUG_BN((grp->q, "%s: grp->q = ", __func__));
609 SCHNORR_DEBUG_BN((grp->g, "%s: grp->g = ", __func__));
610
611 /* [1, 20) */
612 for (i = 1; i < 20; i++) {
613 printf("x = %u\n", i);
614 fflush(stdout);
615 if (BN_set_word(x, i) != 1)
616 fatal("%s: set x word", __func__);
617 schnorr_selftest_one(grp->p, grp->q, grp->g, x);
618 }
619
620 /* 100 x random [0, p) */
621 for (i = 0; i < 100; i++) {
622 if (BN_rand_range(x, grp->p) != 1)
623 fatal("%s: BN_rand_range", __func__);
624 hh = BN_bn2hex(x);
625 printf("x = (random) 0x%s\n", hh);
626 free(hh);
627 fflush(stdout);
628 schnorr_selftest_one(grp->p, grp->q, grp->g, x);
629 }
630
631 /* [q-20, q) */
632 if (BN_set_word(x, 20) != 1)
633 fatal("%s: BN_set_word (x = 20)", __func__);
634 if (BN_sub(x, grp->q, x) != 1)
635 fatal("%s: BN_sub (q - x)", __func__);
636 for (i = 0; i < 19; i++) {
637 hh = BN_bn2hex(x);
638 printf("x = (q - %d) 0x%s\n", 20 - i, hh);
639 free(hh);
640 fflush(stdout);
641 schnorr_selftest_one(grp->p, grp->q, grp->g, x);
642 if (BN_add(x, x, BN_value_one()) != 1)
643 fatal("%s: BN_add (x + 1)", __func__);
644 }
645 BN_free(x);
646}
647
648int
649main(int argc, char **argv)
650{
651 log_init(argv[0], SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_USER, 1);
652
653 schnorr_selftest();
654 return 0;
655}
656#endif
657