blob: 37631e439bca942bc0420ee3327e701691f6bb7b [file] [log] [blame]
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001/* crypto/bn/bntest.c */
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00002/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
Bodo Möller17e3dd11999-05-20 21:59:20 +000062
Bodo Möller7e701811999-05-21 11:16:48 +000063#include "openssl/e_os.h"
Bodo Möller17e3dd11999-05-20 21:59:20 +000064
Bodo Möllerec577821999-04-23 22:13:45 +000065#include <openssl/bio.h>
66#include <openssl/bn.h>
67#include <openssl/rand.h>
68#include <openssl/x509.h>
69#include <openssl/err.h>
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000070
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000071#ifdef WINDOWS
72#include "../bio/bss_file.c"
73#endif
74
Dr. Stephen Hensonf07fb9b2000-02-08 01:34:59 +000075const int num0 = 100; /* number of tests */
76const int num1 = 50; /* additional tests for some functions */
77const int num2 = 5; /* number of tests for slow functions */
Ulf Möllercae55bf2000-02-06 15:56:59 +000078
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000079int test_add(BIO *bp);
80int test_sub(BIO *bp);
81int test_lshift1(BIO *bp);
Ben Laurie4da88a81999-05-29 14:10:58 +000082int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000083int test_rshift1(BIO *bp);
Ben Laurie62bad771999-05-15 15:59:28 +000084int test_rshift(BIO *bp,BN_CTX *ctx);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +000085int test_div(BIO *bp,BN_CTX *ctx);
86int test_div_recp(BIO *bp,BN_CTX *ctx);
87int test_mul(BIO *bp);
88int test_sqr(BIO *bp,BN_CTX *ctx);
89int test_mont(BIO *bp,BN_CTX *ctx);
90int test_mod(BIO *bp,BN_CTX *ctx);
91int test_mod_mul(BIO *bp,BN_CTX *ctx);
92int test_mod_exp(BIO *bp,BN_CTX *ctx);
Mark J. Coxa0a54071999-01-28 10:40:38 +000093int test_exp(BIO *bp,BN_CTX *ctx);
Bodo Möllerc7820892000-11-28 06:41:05 +000094int test_kron(BIO *bp,BN_CTX *ctx);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000095int rand_neg(void);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000096static int results=0;
97
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000098#ifdef NO_STDIO
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000099#define APPS_WIN16
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000100#include "bss_file.c"
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000101#endif
102
Ulf Möllerb25c8db2000-01-21 00:00:12 +0000103static unsigned char lst[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
Ben Laurie4da88a81999-05-29 14:10:58 +0000104"\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
105
Bodo Möller0c50e022000-01-14 17:55:37 +0000106static const char rnd_seed[] = "string to make the random number generator think it has entropy";
107
Ralf S. Engelschall667ac4e2000-02-11 09:47:18 +0000108static void message(BIO *out, char *m)
Ulf Möllercae55bf2000-02-06 15:56:59 +0000109 {
110 fprintf(stderr, "test %s\n", m);
Ulf Möller4a263292000-02-23 22:11:07 +0000111#if defined(linux) || defined(__FreeBSD__) /* can we use GNU bc features? */
Ulf Möllercae55bf2000-02-06 15:56:59 +0000112 BIO_puts(out, "print \"test ");
113 BIO_puts(out, m);
114 BIO_puts(out, "\\n\"\n");
115#endif
116 }
117
Ulf Möller6b691a51999-04-19 21:31:43 +0000118int main(int argc, char *argv[])
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000119 {
120 BN_CTX *ctx;
121 BIO *out;
122 char *outfile=NULL;
123
Bodo Möller79875771999-10-25 19:36:01 +0000124 results = 0;
125
Bodo Möller0c50e022000-01-14 17:55:37 +0000126 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we don't
127 * even check its return value
128 * (which we should) */
129
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000130 argc--;
131 argv++;
132 while (argc >= 1)
133 {
134 if (strcmp(*argv,"-results") == 0)
135 results=1;
136 else if (strcmp(*argv,"-out") == 0)
137 {
138 if (--argc < 1) break;
139 outfile= *(++argv);
140 }
141 argc--;
142 argv++;
143 }
144
145
146 ctx=BN_CTX_new();
147 if (ctx == NULL) exit(1);
148
149 out=BIO_new(BIO_s_file());
150 if (out == NULL) exit(1);
151 if (outfile == NULL)
152 {
153 BIO_set_fp(out,stdout,BIO_NOCLOSE);
154 }
155 else
156 {
157 if (!BIO_write_filename(out,outfile))
158 {
159 perror(outfile);
160 exit(1);
161 }
162 }
163
164 if (!results)
165 BIO_puts(out,"obase=16\nibase=16\n");
166
Ulf Möllercae55bf2000-02-06 15:56:59 +0000167 message(out,"BN_add");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000168 if (!test_add(out)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000169 BIO_flush(out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000170
Ulf Möllercae55bf2000-02-06 15:56:59 +0000171 message(out,"BN_sub");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000172 if (!test_sub(out)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000173 BIO_flush(out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000174
Ulf Möllercae55bf2000-02-06 15:56:59 +0000175 message(out,"BN_lshift1");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000176 if (!test_lshift1(out)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000177 BIO_flush(out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000178
Ulf Möllercae55bf2000-02-06 15:56:59 +0000179 message(out,"BN_lshift (fixed)");
Ulf Möllerb25c8db2000-01-21 00:00:12 +0000180 if (!test_lshift(out,ctx,BN_bin2bn(lst,sizeof(lst)-1,NULL)))
Ben Laurie4da88a81999-05-29 14:10:58 +0000181 goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000182 BIO_flush(out);
Ben Laurie4da88a81999-05-29 14:10:58 +0000183
Ulf Möllercae55bf2000-02-06 15:56:59 +0000184 message(out,"BN_lshift");
Ben Laurie4da88a81999-05-29 14:10:58 +0000185 if (!test_lshift(out,ctx,NULL)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000186 BIO_flush(out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000187
Ulf Möllercae55bf2000-02-06 15:56:59 +0000188 message(out,"BN_rshift1");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000189 if (!test_rshift1(out)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000190 BIO_flush(out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000191
Ulf Möllercae55bf2000-02-06 15:56:59 +0000192 message(out,"BN_rshift");
Ben Laurie62bad771999-05-15 15:59:28 +0000193 if (!test_rshift(out,ctx)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000194 BIO_flush(out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000195
Ulf Möllercae55bf2000-02-06 15:56:59 +0000196 message(out,"BN_sqr");
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000197 if (!test_sqr(out,ctx)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000198 BIO_flush(out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000199
Ulf Möllercae55bf2000-02-06 15:56:59 +0000200 message(out,"BN_mul");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000201 if (!test_mul(out)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000202 BIO_flush(out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000203
Ulf Möllercae55bf2000-02-06 15:56:59 +0000204 message(out,"BN_div");
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000205 if (!test_div(out,ctx)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000206 BIO_flush(out);
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000207
Ulf Möllercae55bf2000-02-06 15:56:59 +0000208 message(out,"BN_div_recp");
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000209 if (!test_div_recp(out,ctx)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000210 BIO_flush(out);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000211
Ulf Möllercae55bf2000-02-06 15:56:59 +0000212 message(out,"BN_mod");
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000213 if (!test_mod(out,ctx)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000214 BIO_flush(out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000215
Ulf Möllercae55bf2000-02-06 15:56:59 +0000216 message(out,"BN_mod_mul");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000217 if (!test_mod_mul(out,ctx)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000218 BIO_flush(out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000219
Ulf Möllercae55bf2000-02-06 15:56:59 +0000220 message(out,"BN_mont");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000221 if (!test_mont(out,ctx)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000222 BIO_flush(out);
Bodo Möller6e0cad82000-01-27 11:26:28 +0000223
Ulf Möllercae55bf2000-02-06 15:56:59 +0000224 message(out,"BN_mod_exp");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000225 if (!test_mod_exp(out,ctx)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000226 BIO_flush(out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000227
Ulf Möllercae55bf2000-02-06 15:56:59 +0000228 message(out,"BN_exp");
Mark J. Coxa0a54071999-01-28 10:40:38 +0000229 if (!test_exp(out,ctx)) goto err;
Richard Levittef36e02b2000-02-25 20:40:12 +0000230 BIO_flush(out);
Mark J. Coxa0a54071999-01-28 10:40:38 +0000231
Bodo Möllerc7820892000-11-28 06:41:05 +0000232 message(out,"BN_kronecker");
233 if (!test_kron(out,ctx)) goto err;
234 BIO_flush(out);
235
Bodo Möller79875771999-10-25 19:36:01 +0000236 BN_CTX_free(ctx);
237 BIO_free(out);
238
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000239/**/
240 exit(0);
241err:
Bodo Möllerdf83eeb2000-03-02 22:08:30 +0000242 BIO_puts(out,"1\n"); /* make sure the Perl script fed by bc notices
243 * the failure, see test_bn in test/Makefile.ssl*/
Richard Levittef36e02b2000-02-25 20:40:12 +0000244 BIO_flush(out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000245 ERR_load_crypto_strings();
Ulf Möllercae55bf2000-02-06 15:56:59 +0000246 ERR_print_errors_fp(stderr);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000247 exit(1);
248 return(1);
249 }
250
Ulf Möller6b691a51999-04-19 21:31:43 +0000251int test_add(BIO *bp)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000252 {
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000253 BIGNUM a,b,c;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000254 int i;
255 int j;
256
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000257 BN_init(&a);
258 BN_init(&b);
259 BN_init(&c);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000260
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000261 BN_rand(&a,512,0,0);
Ulf Möllercae55bf2000-02-06 15:56:59 +0000262 for (i=0; i<num0; i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000263 {
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000264 BN_rand(&b,450+i,0,0);
265 a.neg=rand_neg();
266 b.neg=rand_neg();
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000267 if (bp == NULL)
268 for (j=0; j<10000; j++)
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000269 BN_add(&c,&a,&b);
270 BN_add(&c,&a,&b);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000271 if (bp != NULL)
272 {
273 if (!results)
274 {
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000275 BN_print(bp,&a);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000276 BIO_puts(bp," + ");
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000277 BN_print(bp,&b);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000278 BIO_puts(bp," - ");
279 }
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000280 BN_print(bp,&c);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000281 BIO_puts(bp,"\n");
282 }
Ben Laurie62bad771999-05-15 15:59:28 +0000283 a.neg=!a.neg;
284 b.neg=!b.neg;
285 BN_add(&c,&c,&b);
286 BN_add(&c,&c,&a);
287 if(!BN_is_zero(&c))
288 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000289 fprintf(stderr,"Add test failed!\n");
Ben Laurie62bad771999-05-15 15:59:28 +0000290 return 0;
291 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000292 }
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000293 BN_free(&a);
294 BN_free(&b);
295 BN_free(&c);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000296 return(1);
297 }
298
Ulf Möller6b691a51999-04-19 21:31:43 +0000299int test_sub(BIO *bp)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000300 {
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000301 BIGNUM a,b,c;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000302 int i;
303 int j;
304
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000305 BN_init(&a);
306 BN_init(&b);
307 BN_init(&c);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000308
Ulf Möllercae55bf2000-02-06 15:56:59 +0000309 for (i=0; i<num0+num1; i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000310 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000311 if (i < num1)
312 {
313 BN_rand(&a,512,0,0);
314 BN_copy(&b,&a);
315 if (BN_set_bit(&a,i)==0) return(0);
316 BN_add_word(&b,i);
317 }
318 else
319 {
320 BN_rand(&b,400+i-num1,0,0);
321 a.neg=rand_neg();
322 b.neg=rand_neg();
323 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000324 if (bp == NULL)
325 for (j=0; j<10000; j++)
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000326 BN_sub(&c,&a,&b);
327 BN_sub(&c,&a,&b);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000328 if (bp != NULL)
329 {
330 if (!results)
331 {
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000332 BN_print(bp,&a);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000333 BIO_puts(bp," - ");
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000334 BN_print(bp,&b);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000335 BIO_puts(bp," - ");
336 }
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000337 BN_print(bp,&c);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000338 BIO_puts(bp,"\n");
339 }
Ben Laurie62bad771999-05-15 15:59:28 +0000340 BN_add(&c,&c,&b);
341 BN_sub(&c,&c,&a);
342 if(!BN_is_zero(&c))
343 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000344 fprintf(stderr,"Subtract test failed!\n");
Ben Laurie62bad771999-05-15 15:59:28 +0000345 return 0;
346 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000347 }
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000348 BN_free(&a);
349 BN_free(&b);
350 BN_free(&c);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000351 return(1);
352 }
353
Ulf Möller6b691a51999-04-19 21:31:43 +0000354int test_div(BIO *bp, BN_CTX *ctx)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000355 {
Ben Laurie62bad771999-05-15 15:59:28 +0000356 BIGNUM a,b,c,d,e;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000357 int i;
358 int j;
359
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000360 BN_init(&a);
361 BN_init(&b);
362 BN_init(&c);
363 BN_init(&d);
Ben Laurie62bad771999-05-15 15:59:28 +0000364 BN_init(&e);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000365
Ulf Möllercae55bf2000-02-06 15:56:59 +0000366 for (i=0; i<num0+num1; i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000367 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000368 if (i < num1)
369 {
370 BN_rand(&a,400,0,0);
371 BN_copy(&b,&a);
372 BN_lshift(&a,&a,i);
373 BN_add_word(&a,i);
374 }
375 else
376 BN_rand(&b,50+3*(i-num1),0,0);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000377 a.neg=rand_neg();
378 b.neg=rand_neg();
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000379 if (bp == NULL)
380 for (j=0; j<100; j++)
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000381 BN_div(&d,&c,&a,&b,ctx);
382 BN_div(&d,&c,&a,&b,ctx);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000383 if (bp != NULL)
384 {
385 if (!results)
386 {
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000387 BN_print(bp,&a);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000388 BIO_puts(bp," / ");
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000389 BN_print(bp,&b);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000390 BIO_puts(bp," - ");
391 }
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000392 BN_print(bp,&d);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000393 BIO_puts(bp,"\n");
394
395 if (!results)
396 {
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000397 BN_print(bp,&a);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000398 BIO_puts(bp," % ");
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000399 BN_print(bp,&b);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000400 BIO_puts(bp," - ");
401 }
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000402 BN_print(bp,&c);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000403 BIO_puts(bp,"\n");
404 }
Ben Laurie62bad771999-05-15 15:59:28 +0000405 BN_mul(&e,&d,&b,ctx);
406 BN_add(&d,&e,&c);
407 BN_sub(&d,&d,&a);
408 if(!BN_is_zero(&d))
409 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000410 fprintf(stderr,"Division test failed!\n");
Ben Laurie62bad771999-05-15 15:59:28 +0000411 return 0;
412 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000413 }
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000414 BN_free(&a);
415 BN_free(&b);
416 BN_free(&c);
417 BN_free(&d);
Ben Laurie62bad771999-05-15 15:59:28 +0000418 BN_free(&e);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000419 return(1);
420 }
421
Ulf Möller6b691a51999-04-19 21:31:43 +0000422int test_div_recp(BIO *bp, BN_CTX *ctx)
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000423 {
Ben Laurie62bad771999-05-15 15:59:28 +0000424 BIGNUM a,b,c,d,e;
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000425 BN_RECP_CTX recp;
426 int i;
427 int j;
428
429 BN_RECP_CTX_init(&recp);
430 BN_init(&a);
431 BN_init(&b);
432 BN_init(&c);
433 BN_init(&d);
Ben Laurie62bad771999-05-15 15:59:28 +0000434 BN_init(&e);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000435
Ulf Möllercae55bf2000-02-06 15:56:59 +0000436 for (i=0; i<num0+num1; i++)
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000437 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000438 if (i < num1)
439 {
440 BN_rand(&a,400,0,0);
441 BN_copy(&b,&a);
442 BN_lshift(&a,&a,i);
443 BN_add_word(&a,i);
444 }
445 else
446 BN_rand(&b,50+3*(i-num1),0,0);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000447 a.neg=rand_neg();
448 b.neg=rand_neg();
449 BN_RECP_CTX_set(&recp,&b,ctx);
450 if (bp == NULL)
451 for (j=0; j<100; j++)
452 BN_div_recp(&d,&c,&a,&recp,ctx);
453 BN_div_recp(&d,&c,&a,&recp,ctx);
454 if (bp != NULL)
455 {
456 if (!results)
457 {
458 BN_print(bp,&a);
459 BIO_puts(bp," / ");
460 BN_print(bp,&b);
461 BIO_puts(bp," - ");
462 }
463 BN_print(bp,&d);
464 BIO_puts(bp,"\n");
465
466 if (!results)
467 {
468 BN_print(bp,&a);
469 BIO_puts(bp," % ");
470 BN_print(bp,&b);
471 BIO_puts(bp," - ");
472 }
473 BN_print(bp,&c);
474 BIO_puts(bp,"\n");
475 }
Ben Laurie62bad771999-05-15 15:59:28 +0000476 BN_mul(&e,&d,&b,ctx);
477 BN_add(&d,&e,&c);
478 BN_sub(&d,&d,&a);
479 if(!BN_is_zero(&d))
480 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000481 fprintf(stderr,"Reciprocal division test failed!\n");
482 fprintf(stderr,"a=");
483 BN_print_fp(stderr,&a);
484 fprintf(stderr,"\nb=");
485 BN_print_fp(stderr,&b);
486 fprintf(stderr,"\n");
Ben Laurie62bad771999-05-15 15:59:28 +0000487 return 0;
488 }
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000489 }
490 BN_free(&a);
491 BN_free(&b);
492 BN_free(&c);
493 BN_free(&d);
Ben Laurie62bad771999-05-15 15:59:28 +0000494 BN_free(&e);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000495 BN_RECP_CTX_free(&recp);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000496 return(1);
497 }
498
Ulf Möller6b691a51999-04-19 21:31:43 +0000499int test_mul(BIO *bp)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000500 {
Ben Laurie62bad771999-05-15 15:59:28 +0000501 BIGNUM a,b,c,d,e;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000502 int i;
503 int j;
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000504 BN_CTX ctx;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000505
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000506 BN_CTX_init(&ctx);
507 BN_init(&a);
508 BN_init(&b);
509 BN_init(&c);
Ben Laurie62bad771999-05-15 15:59:28 +0000510 BN_init(&d);
511 BN_init(&e);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000512
Ulf Möllercae55bf2000-02-06 15:56:59 +0000513 for (i=0; i<num0+num1; i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000514 {
Dr. Stephen Henson587bb0e2000-02-27 17:34:30 +0000515 if (i <= num1)
Ulf Möllercae55bf2000-02-06 15:56:59 +0000516 {
517 BN_rand(&a,100,0,0);
518 BN_rand(&b,100,0,0);
519 }
520 else
521 BN_rand(&b,i-num1,0,0);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000522 a.neg=rand_neg();
523 b.neg=rand_neg();
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000524 if (bp == NULL)
525 for (j=0; j<100; j++)
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000526 BN_mul(&c,&a,&b,&ctx);
527 BN_mul(&c,&a,&b,&ctx);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000528 if (bp != NULL)
529 {
530 if (!results)
531 {
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000532 BN_print(bp,&a);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000533 BIO_puts(bp," * ");
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000534 BN_print(bp,&b);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000535 BIO_puts(bp," - ");
536 }
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000537 BN_print(bp,&c);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000538 BIO_puts(bp,"\n");
539 }
Ben Laurie62bad771999-05-15 15:59:28 +0000540 BN_div(&d,&e,&c,&a,&ctx);
541 BN_sub(&d,&d,&b);
542 if(!BN_is_zero(&d) || !BN_is_zero(&e))
543 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000544 fprintf(stderr,"Multiplication test failed!\n");
Ben Laurie62bad771999-05-15 15:59:28 +0000545 return 0;
546 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000547 }
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000548 BN_free(&a);
549 BN_free(&b);
550 BN_free(&c);
Ben Laurie62bad771999-05-15 15:59:28 +0000551 BN_free(&d);
552 BN_free(&e);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000553 BN_CTX_free(&ctx);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000554 return(1);
555 }
556
Ulf Möller6b691a51999-04-19 21:31:43 +0000557int test_sqr(BIO *bp, BN_CTX *ctx)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000558 {
Ben Laurie62bad771999-05-15 15:59:28 +0000559 BIGNUM a,c,d,e;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000560 int i;
561 int j;
562
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000563 BN_init(&a);
564 BN_init(&c);
Ben Laurie62bad771999-05-15 15:59:28 +0000565 BN_init(&d);
566 BN_init(&e);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000567
Ulf Möllercae55bf2000-02-06 15:56:59 +0000568 for (i=0; i<num0; i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000569 {
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000570 BN_rand(&a,40+i*10,0,0);
571 a.neg=rand_neg();
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000572 if (bp == NULL)
573 for (j=0; j<100; j++)
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000574 BN_sqr(&c,&a,ctx);
575 BN_sqr(&c,&a,ctx);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000576 if (bp != NULL)
577 {
578 if (!results)
579 {
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000580 BN_print(bp,&a);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000581 BIO_puts(bp," * ");
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000582 BN_print(bp,&a);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000583 BIO_puts(bp," - ");
584 }
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000585 BN_print(bp,&c);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000586 BIO_puts(bp,"\n");
587 }
Ben Laurie62bad771999-05-15 15:59:28 +0000588 BN_div(&d,&e,&c,&a,ctx);
589 BN_sub(&d,&d,&a);
590 if(!BN_is_zero(&d) || !BN_is_zero(&e))
591 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000592 fprintf(stderr,"Square test failed!\n");
Ben Laurie62bad771999-05-15 15:59:28 +0000593 return 0;
594 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000595 }
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000596 BN_free(&a);
597 BN_free(&c);
Ben Laurie62bad771999-05-15 15:59:28 +0000598 BN_free(&d);
599 BN_free(&e);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000600 return(1);
601 }
602
Ulf Möller6b691a51999-04-19 21:31:43 +0000603int test_mont(BIO *bp, BN_CTX *ctx)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000604 {
Ben Laurie62bad771999-05-15 15:59:28 +0000605 BIGNUM a,b,c,d,A,B;
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000606 BIGNUM n;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000607 int i;
608 int j;
609 BN_MONT_CTX *mont;
610
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000611 BN_init(&a);
612 BN_init(&b);
613 BN_init(&c);
Ben Laurie62bad771999-05-15 15:59:28 +0000614 BN_init(&d);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000615 BN_init(&A);
616 BN_init(&B);
617 BN_init(&n);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000618
619 mont=BN_MONT_CTX_new();
620
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000621 BN_rand(&a,100,0,0); /**/
622 BN_rand(&b,100,0,0); /**/
Ulf Möllercae55bf2000-02-06 15:56:59 +0000623 for (i=0; i<num2; i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000624 {
Andy Polyakovea96c4b2000-02-15 13:50:02 +0000625 int bits = (200*(i+1))/num2;
Bodo Möller6e0cad82000-01-27 11:26:28 +0000626
627 if (bits == 0)
628 continue;
629 BN_rand(&n,bits,0,1);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000630 BN_MONT_CTX_set(mont,&n,ctx);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000631
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000632 BN_to_montgomery(&A,&a,mont,ctx);
633 BN_to_montgomery(&B,&b,mont,ctx);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000634
635 if (bp == NULL)
636 for (j=0; j<100; j++)
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000637 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
638 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
639 BN_from_montgomery(&A,&c,mont,ctx);/**/
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000640 if (bp != NULL)
641 {
642 if (!results)
643 {
644#ifdef undef
645fprintf(stderr,"%d * %d %% %d\n",
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000646BN_num_bits(&a),
647BN_num_bits(&b),
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000648BN_num_bits(mont->N));
649#endif
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000650 BN_print(bp,&a);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000651 BIO_puts(bp," * ");
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000652 BN_print(bp,&b);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000653 BIO_puts(bp," % ");
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000654 BN_print(bp,&(mont->N));
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000655 BIO_puts(bp," - ");
656 }
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000657 BN_print(bp,&A);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000658 BIO_puts(bp,"\n");
659 }
Ben Laurie62bad771999-05-15 15:59:28 +0000660 BN_mod_mul(&d,&a,&b,&n,ctx);
661 BN_sub(&d,&d,&A);
662 if(!BN_is_zero(&d))
663 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000664 fprintf(stderr,"Montgomery multiplication test failed!\n");
Ben Laurie62bad771999-05-15 15:59:28 +0000665 return 0;
666 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000667 }
668 BN_MONT_CTX_free(mont);
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +0000669 BN_free(&a);
670 BN_free(&b);
671 BN_free(&c);
Ben Laurie62bad771999-05-15 15:59:28 +0000672 BN_free(&d);
673 BN_free(&A);
674 BN_free(&B);
675 BN_free(&n);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000676 return(1);
677 }
678
Ulf Möller6b691a51999-04-19 21:31:43 +0000679int test_mod(BIO *bp, BN_CTX *ctx)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000680 {
Ben Laurie62bad771999-05-15 15:59:28 +0000681 BIGNUM *a,*b,*c,*d,*e;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000682 int i;
683 int j;
684
685 a=BN_new();
686 b=BN_new();
687 c=BN_new();
Ben Laurie62bad771999-05-15 15:59:28 +0000688 d=BN_new();
689 e=BN_new();
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000690
691 BN_rand(a,1024,0,0); /**/
Ulf Möllercae55bf2000-02-06 15:56:59 +0000692 for (i=0; i<num0; i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000693 {
694 BN_rand(b,450+i*10,0,0); /**/
695 a->neg=rand_neg();
696 b->neg=rand_neg();
697 if (bp == NULL)
698 for (j=0; j<100; j++)
699 BN_mod(c,a,b,ctx);/**/
700 BN_mod(c,a,b,ctx);/**/
701 if (bp != NULL)
702 {
703 if (!results)
704 {
705 BN_print(bp,a);
706 BIO_puts(bp," % ");
707 BN_print(bp,b);
708 BIO_puts(bp," - ");
709 }
710 BN_print(bp,c);
711 BIO_puts(bp,"\n");
712 }
Ben Laurie62bad771999-05-15 15:59:28 +0000713 BN_div(d,e,a,b,ctx);
714 BN_sub(e,e,c);
715 if(!BN_is_zero(e))
716 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000717 fprintf(stderr,"Modulo test failed!\n");
Ben Laurie62bad771999-05-15 15:59:28 +0000718 return 0;
719 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000720 }
721 BN_free(a);
722 BN_free(b);
723 BN_free(c);
Ben Laurie62bad771999-05-15 15:59:28 +0000724 BN_free(d);
725 BN_free(e);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000726 return(1);
727 }
728
Ulf Möller6b691a51999-04-19 21:31:43 +0000729int test_mod_mul(BIO *bp, BN_CTX *ctx)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000730 {
731 BIGNUM *a,*b,*c,*d,*e;
732 int i;
733
734 a=BN_new();
735 b=BN_new();
736 c=BN_new();
737 d=BN_new();
738 e=BN_new();
739
740 BN_rand(c,1024,0,0); /**/
Ulf Möllercae55bf2000-02-06 15:56:59 +0000741 for (i=0; i<num0; i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000742 {
Ulf Möllerfcb20be2000-02-06 16:35:28 +0000743 BN_rand(a,475+i*10,0,0); /**/
744 BN_rand(b,425+i*11,0,0); /**/
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000745 a->neg=rand_neg();
746 b->neg=rand_neg();
747 /* if (bp == NULL)
748 for (j=0; j<100; j++)
749 BN_mod_mul(d,a,b,c,ctx);*/ /**/
750
751 if (!BN_mod_mul(e,a,b,c,ctx))
752 {
753 unsigned long l;
754
755 while ((l=ERR_get_error()))
756 fprintf(stderr,"ERROR:%s\n",
757 ERR_error_string(l,NULL));
758 exit(1);
759 }
760 if (bp != NULL)
761 {
762 if (!results)
763 {
764 BN_print(bp,a);
765 BIO_puts(bp," * ");
766 BN_print(bp,b);
767 BIO_puts(bp," % ");
768 BN_print(bp,c);
Bodo Möller5acaa492000-11-26 18:31:32 +0000769 if ((a->neg ^ b->neg) && !BN_is_zero(e))
770 {
771 /* If (a*b) % c is negative, c must be added
772 * in order to obtain the normalized remainder
773 * (new with OpenSSL 0.9.7, previous versions of
774 * BN_mod_mul could generate negative results)
775 */
776 BIO_puts(bp," + ");
777 BN_print(bp,c);
778 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000779 BIO_puts(bp," - ");
780 }
781 BN_print(bp,e);
782 BIO_puts(bp,"\n");
783 }
Ben Laurie62bad771999-05-15 15:59:28 +0000784 BN_mul(d,a,b,ctx);
785 BN_sub(d,d,e);
786 BN_div(a,b,d,c,ctx);
787 if(!BN_is_zero(b))
788 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000789 fprintf(stderr,"Modulo multiply test failed!\n");
Richard Levitte020fc822000-11-06 21:15:54 +0000790 ERR_print_errors_fp(stderr);
Ben Laurie62bad771999-05-15 15:59:28 +0000791 return 0;
792 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000793 }
794 BN_free(a);
795 BN_free(b);
796 BN_free(c);
797 BN_free(d);
798 BN_free(e);
799 return(1);
800 }
801
Ulf Möller6b691a51999-04-19 21:31:43 +0000802int test_mod_exp(BIO *bp, BN_CTX *ctx)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000803 {
804 BIGNUM *a,*b,*c,*d,*e;
805 int i;
806
807 a=BN_new();
808 b=BN_new();
809 c=BN_new();
810 d=BN_new();
811 e=BN_new();
812
813 BN_rand(c,30,0,1); /* must be odd for montgomery */
Ulf Möllercae55bf2000-02-06 15:56:59 +0000814 for (i=0; i<num2; i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000815 {
816 BN_rand(a,20+i*5,0,0); /**/
817 BN_rand(b,2+i,0,0); /**/
818
819 if (!BN_mod_exp(d,a,b,c,ctx))
820 return(00);
821
822 if (bp != NULL)
823 {
824 if (!results)
825 {
826 BN_print(bp,a);
827 BIO_puts(bp," ^ ");
828 BN_print(bp,b);
829 BIO_puts(bp," % ");
830 BN_print(bp,c);
831 BIO_puts(bp," - ");
832 }
833 BN_print(bp,d);
834 BIO_puts(bp,"\n");
835 }
Ben Laurie62bad771999-05-15 15:59:28 +0000836 BN_exp(e,a,b,ctx);
837 BN_sub(e,e,d);
838 BN_div(a,b,e,c,ctx);
839 if(!BN_is_zero(b))
840 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000841 fprintf(stderr,"Modulo exponentiation test failed!\n");
Ben Laurie62bad771999-05-15 15:59:28 +0000842 return 0;
843 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000844 }
845 BN_free(a);
846 BN_free(b);
847 BN_free(c);
848 BN_free(d);
849 BN_free(e);
850 return(1);
851 }
852
Ulf Möller6b691a51999-04-19 21:31:43 +0000853int test_exp(BIO *bp, BN_CTX *ctx)
Mark J. Coxa0a54071999-01-28 10:40:38 +0000854 {
Ben Laurie62bad771999-05-15 15:59:28 +0000855 BIGNUM *a,*b,*d,*e,*one;
Mark J. Coxa0a54071999-01-28 10:40:38 +0000856 int i;
857
858 a=BN_new();
859 b=BN_new();
860 d=BN_new();
861 e=BN_new();
Ben Laurie62bad771999-05-15 15:59:28 +0000862 one=BN_new();
863 BN_one(one);
Mark J. Coxa0a54071999-01-28 10:40:38 +0000864
Ulf Möllercae55bf2000-02-06 15:56:59 +0000865 for (i=0; i<num2; i++)
Mark J. Coxa0a54071999-01-28 10:40:38 +0000866 {
867 BN_rand(a,20+i*5,0,0); /**/
868 BN_rand(b,2+i,0,0); /**/
869
870 if (!BN_exp(d,a,b,ctx))
871 return(00);
872
873 if (bp != NULL)
874 {
875 if (!results)
876 {
877 BN_print(bp,a);
878 BIO_puts(bp," ^ ");
879 BN_print(bp,b);
880 BIO_puts(bp," - ");
881 }
882 BN_print(bp,d);
883 BIO_puts(bp,"\n");
884 }
Ben Laurie62bad771999-05-15 15:59:28 +0000885 BN_one(e);
886 for( ; !BN_is_zero(b) ; BN_sub(b,b,one))
887 BN_mul(e,e,a,ctx);
888 BN_sub(e,e,d);
889 if(!BN_is_zero(e))
890 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000891 fprintf(stderr,"Exponentiation test failed!\n");
Ben Laurie62bad771999-05-15 15:59:28 +0000892 return 0;
893 }
Mark J. Coxa0a54071999-01-28 10:40:38 +0000894 }
895 BN_free(a);
896 BN_free(b);
897 BN_free(d);
898 BN_free(e);
Ben Laurie62bad771999-05-15 15:59:28 +0000899 BN_free(one);
Mark J. Coxa0a54071999-01-28 10:40:38 +0000900 return(1);
901 }
902
Bodo Möllerc7820892000-11-28 06:41:05 +0000903int test_kron(BIO *bp, BN_CTX *ctx)
904 {
905 /* XXX */
906 return(1);
907 }
908
Ben Laurie4da88a81999-05-29 14:10:58 +0000909int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000910 {
Ben Laurie62bad771999-05-15 15:59:28 +0000911 BIGNUM *a,*b,*c,*d;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000912 int i;
913
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000914 b=BN_new();
915 c=BN_new();
Ben Laurie62bad771999-05-15 15:59:28 +0000916 d=BN_new();
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000917 BN_one(c);
918
Ben Laurie4da88a81999-05-29 14:10:58 +0000919 if(a_)
920 a=a_;
921 else
922 {
923 a=BN_new();
924 BN_rand(a,200,0,0); /**/
925 a->neg=rand_neg();
926 }
Ulf Möllercae55bf2000-02-06 15:56:59 +0000927 for (i=0; i<num0; i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000928 {
929 BN_lshift(b,a,i+1);
930 BN_add(c,c,c);
931 if (bp != NULL)
932 {
933 if (!results)
934 {
935 BN_print(bp,a);
936 BIO_puts(bp," * ");
937 BN_print(bp,c);
938 BIO_puts(bp," - ");
939 }
940 BN_print(bp,b);
941 BIO_puts(bp,"\n");
942 }
Ben Laurie62bad771999-05-15 15:59:28 +0000943 BN_mul(d,a,c,ctx);
944 BN_sub(d,d,b);
945 if(!BN_is_zero(d))
946 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000947 fprintf(stderr,"Left shift test failed!\n");
948 fprintf(stderr,"a=");
949 BN_print_fp(stderr,a);
950 fprintf(stderr,"\nb=");
951 BN_print_fp(stderr,b);
952 fprintf(stderr,"\nc=");
953 BN_print_fp(stderr,c);
954 fprintf(stderr,"\nd=");
955 BN_print_fp(stderr,d);
956 fprintf(stderr,"\n");
Ben Laurie62bad771999-05-15 15:59:28 +0000957 return 0;
958 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000959 }
960 BN_free(a);
961 BN_free(b);
962 BN_free(c);
Ben Laurie62bad771999-05-15 15:59:28 +0000963 BN_free(d);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000964 return(1);
965 }
966
Ulf Möller6b691a51999-04-19 21:31:43 +0000967int test_lshift1(BIO *bp)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000968 {
Ben Laurie62bad771999-05-15 15:59:28 +0000969 BIGNUM *a,*b,*c;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000970 int i;
971
972 a=BN_new();
973 b=BN_new();
Ben Laurie62bad771999-05-15 15:59:28 +0000974 c=BN_new();
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000975
976 BN_rand(a,200,0,0); /**/
977 a->neg=rand_neg();
Ulf Möllercae55bf2000-02-06 15:56:59 +0000978 for (i=0; i<num0; i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000979 {
980 BN_lshift1(b,a);
981 if (bp != NULL)
982 {
983 if (!results)
984 {
985 BN_print(bp,a);
986 BIO_puts(bp," * 2");
987 BIO_puts(bp," - ");
988 }
989 BN_print(bp,b);
990 BIO_puts(bp,"\n");
991 }
Ben Laurie62bad771999-05-15 15:59:28 +0000992 BN_add(c,a,a);
993 BN_sub(a,b,c);
994 if(!BN_is_zero(a))
995 {
Ulf Möllercae55bf2000-02-06 15:56:59 +0000996 fprintf(stderr,"Left shift one test failed!\n");
Ben Laurie62bad771999-05-15 15:59:28 +0000997 return 0;
998 }
999
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001000 BN_copy(a,b);
1001 }
1002 BN_free(a);
1003 BN_free(b);
Ben Laurie62bad771999-05-15 15:59:28 +00001004 BN_free(c);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001005 return(1);
1006 }
1007
Ben Laurie62bad771999-05-15 15:59:28 +00001008int test_rshift(BIO *bp,BN_CTX *ctx)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001009 {
Ben Laurie62bad771999-05-15 15:59:28 +00001010 BIGNUM *a,*b,*c,*d,*e;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001011 int i;
1012
1013 a=BN_new();
1014 b=BN_new();
1015 c=BN_new();
Ben Laurie62bad771999-05-15 15:59:28 +00001016 d=BN_new();
1017 e=BN_new();
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001018 BN_one(c);
1019
1020 BN_rand(a,200,0,0); /**/
1021 a->neg=rand_neg();
Ulf Möllercae55bf2000-02-06 15:56:59 +00001022 for (i=0; i<num0; i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001023 {
1024 BN_rshift(b,a,i+1);
1025 BN_add(c,c,c);
1026 if (bp != NULL)
1027 {
1028 if (!results)
1029 {
1030 BN_print(bp,a);
1031 BIO_puts(bp," / ");
1032 BN_print(bp,c);
1033 BIO_puts(bp," - ");
1034 }
1035 BN_print(bp,b);
1036 BIO_puts(bp,"\n");
1037 }
Ben Laurie62bad771999-05-15 15:59:28 +00001038 BN_div(d,e,a,c,ctx);
1039 BN_sub(d,d,b);
1040 if(!BN_is_zero(d))
1041 {
Ulf Möllercae55bf2000-02-06 15:56:59 +00001042 fprintf(stderr,"Right shift test failed!\n");
Ben Laurie62bad771999-05-15 15:59:28 +00001043 return 0;
1044 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001045 }
1046 BN_free(a);
1047 BN_free(b);
1048 BN_free(c);
Ben Laurie62bad771999-05-15 15:59:28 +00001049 BN_free(d);
1050 BN_free(e);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001051 return(1);
1052 }
1053
Ulf Möller6b691a51999-04-19 21:31:43 +00001054int test_rshift1(BIO *bp)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001055 {
Ben Laurie62bad771999-05-15 15:59:28 +00001056 BIGNUM *a,*b,*c;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001057 int i;
1058
1059 a=BN_new();
1060 b=BN_new();
Ben Laurie62bad771999-05-15 15:59:28 +00001061 c=BN_new();
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001062
1063 BN_rand(a,200,0,0); /**/
1064 a->neg=rand_neg();
Ulf Möllercae55bf2000-02-06 15:56:59 +00001065 for (i=0; i<num0; i++)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001066 {
1067 BN_rshift1(b,a);
1068 if (bp != NULL)
1069 {
1070 if (!results)
1071 {
1072 BN_print(bp,a);
1073 BIO_puts(bp," / 2");
1074 BIO_puts(bp," - ");
1075 }
1076 BN_print(bp,b);
1077 BIO_puts(bp,"\n");
1078 }
Ben Laurie62bad771999-05-15 15:59:28 +00001079 BN_sub(c,a,b);
1080 BN_sub(c,c,b);
1081 if(!BN_is_zero(c) && !BN_is_one(c))
1082 {
Ulf Möllercae55bf2000-02-06 15:56:59 +00001083 fprintf(stderr,"Right shift one test failed!\n");
Ben Laurie62bad771999-05-15 15:59:28 +00001084 return 0;
1085 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001086 BN_copy(a,b);
1087 }
1088 BN_free(a);
1089 BN_free(b);
Ben Laurie62bad771999-05-15 15:59:28 +00001090 BN_free(c);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001091 return(1);
1092 }
1093
Ulf Möller6b691a51999-04-19 21:31:43 +00001094int rand_neg(void)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001095 {
1096 static unsigned int neg=0;
1097 static int sign[8]={0,0,0,1,1,0,1,1};
1098
1099 return(sign[(neg++)%8]);
1100 }