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