exptest.c 6.9 KB
Newer Older
1
/* crypto/bn/exptest.c */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
 * All rights reserved.
 *
 * This package is an SSL implementation written
 * by Eric Young (eay@cryptsoft.com).
 * The implementation was written so as to conform with Netscapes SSL.
 * 
 * This library is free for commercial and non-commercial use as long as
 * the following conditions are aheared to.  The following conditions
 * apply to all code found in this distribution, be it the RC4, RSA,
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
 * included with this distribution is covered by the same copyright terms
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
 * 
 * Copyright remains Eric Young's, and as such any Copyright notices in
 * the code are not to be removed.
 * If this package is used in a product, Eric Young should be given attribution
 * as the author of the parts of the library used.
 * This can be in the form of a textual message at program startup or
 * in documentation (online or textual) provided with the package.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    "This product includes cryptographic software written by
 *     Eric Young (eay@cryptsoft.com)"
 *    The word 'cryptographic' can be left out if the rouines from the library
 *    being used are not cryptographic related :-).
 * 4. If you include any Windows specific code (or a derivative thereof) from 
 *    the apps directory (application code) you must include an acknowledgement:
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
 * 
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 * The licence and distribution terms for any publically available version or
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
 * copied and put under another distribution licence
 * [including the GNU Public Licence.]
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
62 63 64

#include "../e_os.h"

65 66 67 68
#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/rand.h>
#include <openssl/err.h>
69 70 71

#define NUM_BITS	(BN_BITS*2)

72 73
static const char rnd_seed[] = "string to make the random number generator think it has entropy";

A
Adam Langley 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/* test_exp_mod_zero tests that x**0 mod 1 == 0. It returns zero on success. */
static int test_exp_mod_zero() {
	BIGNUM a, p, m;
	BIGNUM r;
	BN_CTX *ctx = BN_CTX_new();
	int ret = 1;

	BN_init(&m);
	BN_one(&m);

	BN_init(&a);
	BN_one(&a);

	BN_init(&p);
	BN_zero(&p);

	BN_init(&r);
	BN_mod_exp(&r, &a, &p, &m, ctx);
	BN_CTX_free(ctx);

	if (BN_is_zero(&r))
		ret = 0;
	else
		{
		printf("1**0 mod 1 = ");
		BN_print_fp(stdout, &r);
		printf(", should be 0\n");
		}

	BN_free(&r);
	BN_free(&a);
	BN_free(&p);
	BN_free(&m);

	return ret;
}

U
Ulf Möller 已提交
111
int main(int argc, char *argv[])
112 113 114 115 116
	{
	BN_CTX *ctx;
	BIO *out=NULL;
	int i,ret;
	unsigned char c;
117
	BIGNUM *r_mont,*r_mont_const,*r_recp,*r_simple,*a,*b,*m;
118

119 120 121 122
	RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we don't
	                                       * even check its return value
	                                       * (which we should) */

123 124
	ERR_load_BN_strings();

125
	ctx=BN_CTX_new();
126
	if (ctx == NULL) EXIT(1);
127
	r_mont=BN_new();
128
	r_mont_const=BN_new();
129
	r_recp=BN_new();
130
	r_simple=BN_new();
131 132 133 134 135 136 137 138
	a=BN_new();
	b=BN_new();
	m=BN_new();
	if (	(r_mont == NULL) || (r_recp == NULL) ||
		(a == NULL) || (b == NULL))
		goto err;

	out=BIO_new(BIO_s_file());
139

140
	if (out == NULL) EXIT(1);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	BIO_set_fp(out,stdout,BIO_NOCLOSE);

	for (i=0; i<200; i++)
		{
		RAND_bytes(&c,1);
		c=(c%BN_BITS)-BN_BITS2;
		BN_rand(a,NUM_BITS+c,0,0);

		RAND_bytes(&c,1);
		c=(c%BN_BITS)-BN_BITS2;
		BN_rand(b,NUM_BITS+c,0,0);

		RAND_bytes(&c,1);
		c=(c%BN_BITS)-BN_BITS2;
		BN_rand(m,NUM_BITS+c,0,1);

		BN_mod(a,a,m,ctx);
		BN_mod(b,b,m,ctx);

160
		ret=BN_mod_exp_mont(r_mont,a,b,m,ctx,NULL);
161
		if (ret <= 0)
162 163 164
			{
			printf("BN_mod_exp_mont() problems\n");
			ERR_print_errors(out);
165
			EXIT(1);
166
			}
167 168 169

		ret=BN_mod_exp_recp(r_recp,a,b,m,ctx);
		if (ret <= 0)
170 171 172
			{
			printf("BN_mod_exp_recp() problems\n");
			ERR_print_errors(out);
173
			EXIT(1);
174
			}
175 176 177

		ret=BN_mod_exp_simple(r_simple,a,b,m,ctx);
		if (ret <= 0)
178
			{
179 180
			printf("BN_mod_exp_simple() problems\n");
			ERR_print_errors(out);
181
			EXIT(1);
182 183
			}

184 185 186 187 188 189 190 191
		ret=BN_mod_exp_mont_consttime(r_mont_const,a,b,m,ctx,NULL);
		if (ret <= 0)
			{
			printf("BN_mod_exp_mont_consttime() problems\n");
			ERR_print_errors(out);
			EXIT(1);
			}

192
		if (BN_cmp(r_simple, r_mont) == 0
193 194
		    && BN_cmp(r_simple,r_recp) == 0
			&& BN_cmp(r_simple,r_mont_const) == 0)
195 196 197 198 199 200 201 202
			{
			printf(".");
			fflush(stdout);
			}
		else
		  	{
			if (BN_cmp(r_simple,r_mont) != 0)
				printf("\nsimple and mont results differ\n");
A
Andy Polyakov 已提交
203
			if (BN_cmp(r_simple,r_mont_const) != 0)
204
				printf("\nsimple and mont const time results differ\n");
205 206 207
			if (BN_cmp(r_simple,r_recp) != 0)
				printf("\nsimple and recp results differ\n");

208 209 210
			printf("a (%3d) = ",BN_num_bits(a));   BN_print(out,a);
			printf("\nb (%3d) = ",BN_num_bits(b)); BN_print(out,b);
			printf("\nm (%3d) = ",BN_num_bits(m)); BN_print(out,m);
211
			printf("\nsimple   =");	BN_print(out,r_simple);
212 213
			printf("\nrecp     =");	BN_print(out,r_recp);
			printf("\nmont     ="); BN_print(out,r_mont);
214
			printf("\nmont_ct  ="); BN_print(out,r_mont_const);
215
			printf("\n");
216
			EXIT(1);
217 218
			}
		}
219
	BN_free(r_mont);
220
	BN_free(r_mont_const);
221 222 223 224 225 226
	BN_free(r_recp);
	BN_free(r_simple);
	BN_free(a);
	BN_free(b);
	BN_free(m);
	BN_CTX_free(ctx);
227
	ERR_remove_thread_state(NULL);
228
	CRYPTO_mem_leaks(out);
229
	BIO_free(out);
A
Adam Langley 已提交
230 231 232 233 234 235 236
	printf("\n");

	if (test_exp_mod_zero() != 0)
		goto err;

	printf("done\n");

237
	EXIT(0);
238 239 240
err:
	ERR_load_crypto_strings();
	ERR_print_errors(out);
R
Richard Levitte 已提交
241 242 243
#ifdef OPENSSL_SYS_NETWARE
    printf("ERROR\n");
#endif
244
	EXIT(1);
245 246 247
	return(1);
	}