os/ossrv/ssl/tsrc/BC/libcrypto/crypto_test/src/exptest.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* crypto/bn/exptest.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
/*
sl@0
    59
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    60
 */
sl@0
    61
#include <stdio.h>
sl@0
    62
#include <stdlib.h>
sl@0
    63
#include <string.h>
sl@0
    64
#ifndef SYMBIAN
sl@0
    65
#include "../e_os.h"
sl@0
    66
#else
sl@0
    67
#include "e_os.h"
sl@0
    68
#endif
sl@0
    69
sl@0
    70
#include <openssl/bio.h>
sl@0
    71
#include <openssl/bn.h>
sl@0
    72
#include <openssl/rand.h>
sl@0
    73
#include <openssl/err.h>
sl@0
    74
#ifdef SYMBIAN
sl@0
    75
#ifdef stdin
sl@0
    76
#undef stdin
sl@0
    77
#endif
sl@0
    78
#ifdef stdout
sl@0
    79
#undef stdout
sl@0
    80
#endif
sl@0
    81
#ifdef stderr
sl@0
    82
#undef stderr
sl@0
    83
#endif
sl@0
    84
sl@0
    85
#define stdin fp_stdin
sl@0
    86
#define stdout fp_stdout
sl@0
    87
#define stderr fp_stderr
sl@0
    88
sl@0
    89
extern FILE *fp_stdout;
sl@0
    90
extern FILE *fp_stderr;
sl@0
    91
#endif
sl@0
    92
sl@0
    93
sl@0
    94
#define NUM_BITS	(BN_BITS*2)
sl@0
    95
sl@0
    96
static const char rnd_seed[] = "string to make the random number generator think it has entropy";
sl@0
    97
#ifndef SYMBIAN
sl@0
    98
int main(int argc, char *argv[])
sl@0
    99
#else
sl@0
   100
int exp_main(int argc, char *argv[])
sl@0
   101
#endif
sl@0
   102
	{
sl@0
   103
	BN_CTX *ctx;
sl@0
   104
	BIO *out=NULL;
sl@0
   105
	int i,ret;
sl@0
   106
	unsigned char c;
sl@0
   107
	BIGNUM *r_mont,*r_mont_const,*r_recp,*r_simple,*a,*b,*m;
sl@0
   108
	//	FILE* temp;
sl@0
   109
  
sl@0
   110
    
sl@0
   111
	RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we don't
sl@0
   112
	                                       * even check its return value
sl@0
   113
	                                       * (which we should) */
sl@0
   114
  if(errno==ENOMEM)
sl@0
   115
  {
sl@0
   116
	  return 1;
sl@0
   117
  }	                                       
sl@0
   118
sl@0
   119
	ERR_load_BN_strings();
sl@0
   120
	if(errno==ENOMEM)
sl@0
   121
    {
sl@0
   122
	  return 1;
sl@0
   123
	}	    
sl@0
   124
sl@0
   125
	ctx=BN_CTX_new();
sl@0
   126
	if (ctx == NULL)
sl@0
   127
	{
sl@0
   128
	 if(errno==ENOMEM)
sl@0
   129
     {
sl@0
   130
	  return 1;
sl@0
   131
	 }
sl@0
   132
	 return 1;	    
sl@0
   133
	} 
sl@0
   134
	r_mont=BN_new();
sl@0
   135
	if(r_mont==NULL&&errno==ENOMEM)
sl@0
   136
    {
sl@0
   137
	  return 1;
sl@0
   138
	}	    
sl@0
   139
	r_mont_const=BN_new();
sl@0
   140
	if(r_mont_const==NULL&&errno==ENOMEM)
sl@0
   141
  {
sl@0
   142
	  return 1;
sl@0
   143
	}	    
sl@0
   144
sl@0
   145
	r_recp=BN_new();
sl@0
   146
	if(r_recp==NULL&&errno==ENOMEM)
sl@0
   147
   {
sl@0
   148
	  return 1;
sl@0
   149
	}	    
sl@0
   150
sl@0
   151
	r_simple=BN_new();
sl@0
   152
	if(r_simple==NULL&&errno==ENOMEM)
sl@0
   153
  {
sl@0
   154
	  return 1;
sl@0
   155
	}	    
sl@0
   156
sl@0
   157
	a=BN_new();
sl@0
   158
	if(a==NULL&&errno==ENOMEM)
sl@0
   159
  {
sl@0
   160
	  return 1;
sl@0
   161
	}	    
sl@0
   162
sl@0
   163
	b=BN_new();
sl@0
   164
	if(b==NULL&&errno==ENOMEM)
sl@0
   165
  {
sl@0
   166
	  return 1;
sl@0
   167
	}	    
sl@0
   168
	
sl@0
   169
	m=BN_new();
sl@0
   170
	if(m==NULL&&errno==ENOMEM)
sl@0
   171
  {
sl@0
   172
	  return 1;
sl@0
   173
	}	    
sl@0
   174
	
sl@0
   175
	if (	(r_mont == NULL) || (r_recp == NULL) ||
sl@0
   176
		(a == NULL) || (b == NULL))
sl@0
   177
		goto err;
sl@0
   178
sl@0
   179
	out=BIO_new(BIO_s_file());
sl@0
   180
	if(out==NULL&&errno==ENOMEM)
sl@0
   181
  {
sl@0
   182
	  return 1;
sl@0
   183
	}	    
sl@0
   184
	if (out == NULL) 
sl@0
   185
	return 1;
sl@0
   186
	BIO_set_fp(out,stdout,BIO_NOCLOSE);
sl@0
   187
	if(errno==ENOMEM)
sl@0
   188
  {
sl@0
   189
	  return 1;
sl@0
   190
	}	    
sl@0
   191
sl@0
   192
// temp = fopen("sanjeev.txt", "w");
sl@0
   193
	
sl@0
   194
	for (i=0; i<200; i++)
sl@0
   195
		{
sl@0
   196
	//	fputc(i,temp);
sl@0
   197
		RAND_bytes(&c,1);
sl@0
   198
  	if(errno==ENOMEM)
sl@0
   199
    {
sl@0
   200
	    return 1;
sl@0
   201
	  }	    
sl@0
   202
sl@0
   203
		c=(c%BN_BITS)-BN_BITS2;
sl@0
   204
		BN_rand(a,NUM_BITS+c,0,0);
sl@0
   205
		if(errno==ENOMEM)
sl@0
   206
    {
sl@0
   207
	   return 1;
sl@0
   208
	  }	    
sl@0
   209
		RAND_bytes(&c,1);
sl@0
   210
  	if(errno==ENOMEM)
sl@0
   211
    {
sl@0
   212
	    return 1;
sl@0
   213
	  }	    
sl@0
   214
		c=(c%BN_BITS)-BN_BITS2;
sl@0
   215
		BN_rand(b,NUM_BITS+c,0,0);
sl@0
   216
  	if(errno==ENOMEM)
sl@0
   217
    {
sl@0
   218
	    return 1;
sl@0
   219
	  }	    
sl@0
   220
		RAND_bytes(&c,1);
sl@0
   221
		if(errno==ENOMEM)
sl@0
   222
    {
sl@0
   223
	   return 1;
sl@0
   224
	  }	    
sl@0
   225
sl@0
   226
		c=(c%BN_BITS)-BN_BITS2;
sl@0
   227
		BN_rand(m,NUM_BITS+c,0,1);
sl@0
   228
		if(errno==ENOMEM)
sl@0
   229
    {
sl@0
   230
	   return 1;
sl@0
   231
	  }	    
sl@0
   232
sl@0
   233
		BN_mod(a,a,m,ctx);
sl@0
   234
		if(errno==ENOMEM)
sl@0
   235
    {
sl@0
   236
	   return 1;
sl@0
   237
	  }	    
sl@0
   238
	
sl@0
   239
		BN_mod(b,b,m,ctx);
sl@0
   240
		if(errno==ENOMEM)
sl@0
   241
      {
sl@0
   242
	   return 1;
sl@0
   243
	  }	    
sl@0
   244
sl@0
   245
		ret=BN_mod_exp_mont(r_mont,a,b,m,ctx,NULL);
sl@0
   246
		if (ret <= 0)
sl@0
   247
			{
sl@0
   248
			if(errno==ENOMEM)
sl@0
   249
      {
sl@0
   250
	     return 1;
sl@0
   251
	    }	    
sl@0
   252
			fprintf(stdout,"BN_mod_exp_mont() problems\n");
sl@0
   253
			ERR_print_errors(out);
sl@0
   254
  		if(errno==ENOMEM)
sl@0
   255
      {
sl@0
   256
	     return 1;
sl@0
   257
	    }	    
sl@0
   258
			return 1;
sl@0
   259
			}
sl@0
   260
sl@0
   261
		ret=BN_mod_exp_recp(r_recp,a,b,m,ctx);
sl@0
   262
		if (ret <= 0)
sl@0
   263
			{
sl@0
   264
  		if(errno==ENOMEM)
sl@0
   265
       {
sl@0
   266
	       return 1;
sl@0
   267
	     }	    
sl@0
   268
			fprintf(stdout,"BN_mod_exp_recp() problems\n");
sl@0
   269
			ERR_print_errors(out);
sl@0
   270
			if(errno==ENOMEM)
sl@0
   271
      {
sl@0
   272
	    return 1;
sl@0
   273
	    }	    
sl@0
   274
			return 1;
sl@0
   275
			}
sl@0
   276
sl@0
   277
		ret=BN_mod_exp_simple(r_simple,a,b,m,ctx);
sl@0
   278
		if (ret <= 0)
sl@0
   279
			{
sl@0
   280
  		if(errno==ENOMEM)
sl@0
   281
       {
sl@0
   282
	       return 1;
sl@0
   283
	     }	    
sl@0
   284
			fprintf(stdout,"BN_mod_exp_simple() problems\n");
sl@0
   285
			ERR_print_errors(out);
sl@0
   286
  		if(errno==ENOMEM)
sl@0
   287
       {
sl@0
   288
	       return 1;
sl@0
   289
	     }	    
sl@0
   290
			return 1;
sl@0
   291
			}
sl@0
   292
sl@0
   293
		ret=BN_mod_exp_mont_consttime(r_mont_const,a,b,m,ctx,NULL);
sl@0
   294
		if (ret <= 0)
sl@0
   295
			{
sl@0
   296
  		if(errno==ENOMEM)
sl@0
   297
       {
sl@0
   298
	       return 1;
sl@0
   299
	     }	    
sl@0
   300
			fprintf(stdout,"BN_mod_exp_mont_consttime() problems\n");
sl@0
   301
			ERR_print_errors(out);
sl@0
   302
  		if(errno==ENOMEM)
sl@0
   303
       {
sl@0
   304
	       return 1;
sl@0
   305
	     }	    
sl@0
   306
			return 1;
sl@0
   307
			}
sl@0
   308
sl@0
   309
		if (BN_cmp(r_simple, r_mont) == 0
sl@0
   310
		    && BN_cmp(r_simple,r_recp) == 0
sl@0
   311
			&& BN_cmp(r_simple,r_mont_const) == 0)
sl@0
   312
			{
sl@0
   313
  		if(errno==ENOMEM)
sl@0
   314
       {
sl@0
   315
	       return 1;
sl@0
   316
	     }	    
sl@0
   317
      fprintf(stdout,".");
sl@0
   318
			fflush(stdout);
sl@0
   319
			}
sl@0
   320
		else
sl@0
   321
		  	{
sl@0
   322
			if (BN_cmp(r_simple,r_mont) != 0)
sl@0
   323
       {
sl@0
   324
 	  		if(errno==ENOMEM)
sl@0
   325
        {
sl@0
   326
	        return 1;
sl@0
   327
	      }	    
sl@0
   328
				fprintf(stdout,"\nsimple and mont results differ\n");
sl@0
   329
       }				
sl@0
   330
			if (BN_cmp(r_simple,r_mont) != 0)
sl@0
   331
			{
sl@0
   332
 	  		if(errno==ENOMEM)
sl@0
   333
        {
sl@0
   334
	        return 1;
sl@0
   335
	      }	    
sl@0
   336
				fprintf(stdout,"\nsimple and mont const time results differ\n");
sl@0
   337
			}	
sl@0
   338
			if (BN_cmp(r_simple,r_recp) != 0)
sl@0
   339
			{
sl@0
   340
 	  		if(errno==ENOMEM)
sl@0
   341
        {
sl@0
   342
	        return 1;
sl@0
   343
	      }	    
sl@0
   344
				fprintf(stdout,"\nsimple and recp results differ\n");
sl@0
   345
      }
sl@0
   346
			fprintf(stdout,"a (%3d) = ",BN_num_bits(a));   BN_print(out,a);
sl@0
   347
  		if(errno==ENOMEM)
sl@0
   348
      {
sl@0
   349
	       return 1;
sl@0
   350
      }	    
sl@0
   351
			fprintf(stdout,"\nb (%3d) = ",BN_num_bits(b)); BN_print(out,b);
sl@0
   352
 	  	if(errno==ENOMEM)
sl@0
   353
      {
sl@0
   354
	       return 1;
sl@0
   355
      }	    
sl@0
   356
			fprintf(stdout,"\nm (%3d) = ",BN_num_bits(m)); BN_print(out,m);
sl@0
   357
 	  	if(errno==ENOMEM)
sl@0
   358
      {
sl@0
   359
	       return 1;
sl@0
   360
      }	    
sl@0
   361
			fprintf(stdout,"\nsimple   =");	BN_print(out,r_simple);
sl@0
   362
 	  	if(errno==ENOMEM)
sl@0
   363
      {
sl@0
   364
	       return 1;
sl@0
   365
      }	    
sl@0
   366
			fprintf(stdout,"\nrecp     =");	BN_print(out,r_recp);
sl@0
   367
 	  	if(errno==ENOMEM)
sl@0
   368
      {
sl@0
   369
	       return 1;
sl@0
   370
      }	    
sl@0
   371
			fprintf(stdout,"\nmont     ="); BN_print(out,r_mont);
sl@0
   372
 	  	if(errno==ENOMEM)
sl@0
   373
      {
sl@0
   374
	       return 1;
sl@0
   375
      }	    
sl@0
   376
			fprintf(stdout,"\nmont_ct  ="); BN_print(out,r_mont_const);
sl@0
   377
 	  	if(errno==ENOMEM)
sl@0
   378
      {
sl@0
   379
	       return 1;
sl@0
   380
      }	    
sl@0
   381
			fprintf(stdout,"\n");
sl@0
   382
			return 1;
sl@0
   383
			}
sl@0
   384
		}
sl@0
   385
	BN_free(r_mont);
sl@0
   386
	BN_free(r_mont_const);
sl@0
   387
	BN_free(r_recp);
sl@0
   388
	BN_free(r_simple);
sl@0
   389
	BN_free(a);
sl@0
   390
	BN_free(b);
sl@0
   391
	BN_free(m);
sl@0
   392
	BN_CTX_free(ctx);
sl@0
   393
	ERR_remove_state(0);
sl@0
   394
	if(errno==ENOMEM)
sl@0
   395
  {
sl@0
   396
    return 1;
sl@0
   397
  }	    
sl@0
   398
	CRYPTO_mem_leaks(out);
sl@0
   399
	if(errno==ENOMEM)
sl@0
   400
  {
sl@0
   401
    return 1;
sl@0
   402
  }	    
sl@0
   403
	BIO_free(out);
sl@0
   404
	if(errno==ENOMEM)
sl@0
   405
   {
sl@0
   406
	  return 1;
sl@0
   407
	 }    
sl@0
   408
 
sl@0
   409
    CRYPTO_cleanup_all_ex_data();
sl@0
   410
   if(errno==ENOMEM)
sl@0
   411
   {
sl@0
   412
	  return 1;
sl@0
   413
	 }    
sl@0
   414
 
sl@0
   415
	fprintf(stdout," done\n");
sl@0
   416
	fprintf(stdout," Test case passed\n");
sl@0
   417
	return 0;
sl@0
   418
err:
sl@0
   419
	ERR_load_crypto_strings();
sl@0
   420
	if(errno==ENOMEM)
sl@0
   421
  {
sl@0
   422
    return 1;
sl@0
   423
  }	    
sl@0
   424
	
sl@0
   425
	ERR_print_errors(out);
sl@0
   426
	if(errno==ENOMEM)
sl@0
   427
  {
sl@0
   428
    return 1;
sl@0
   429
  }	    
sl@0
   430
	
sl@0
   431
	
sl@0
   432
#ifdef OPENSSL_SYS_NETWARE
sl@0
   433
    fprintf(stdout,"ERROR\n");
sl@0
   434
#endif
sl@0
   435
	return(1);
sl@0
   436
	}
sl@0
   437