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.
     1 /* crypto/bn/exptest.c */
     2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     3  * 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  © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    60  */
    61 #include <stdio.h>
    62 #include <stdlib.h>
    63 #include <string.h>
    64 #ifndef SYMBIAN
    65 #include "../e_os.h"
    66 #else
    67 #include "e_os.h"
    68 #endif
    69 
    70 #include <openssl/bio.h>
    71 #include <openssl/bn.h>
    72 #include <openssl/rand.h>
    73 #include <openssl/err.h>
    74 #ifdef SYMBIAN
    75 #ifdef stdin
    76 #undef stdin
    77 #endif
    78 #ifdef stdout
    79 #undef stdout
    80 #endif
    81 #ifdef stderr
    82 #undef stderr
    83 #endif
    84 
    85 #define stdin fp_stdin
    86 #define stdout fp_stdout
    87 #define stderr fp_stderr
    88 
    89 extern FILE *fp_stdout;
    90 extern FILE *fp_stderr;
    91 #endif
    92 
    93 
    94 #define NUM_BITS	(BN_BITS*2)
    95 
    96 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
    97 #ifndef SYMBIAN
    98 int main(int argc, char *argv[])
    99 #else
   100 int exp_main(int argc, char *argv[])
   101 #endif
   102 	{
   103 	BN_CTX *ctx;
   104 	BIO *out=NULL;
   105 	int i,ret;
   106 	unsigned char c;
   107 	BIGNUM *r_mont,*r_mont_const,*r_recp,*r_simple,*a,*b,*m;
   108 	//	FILE* temp;
   109   
   110     
   111 	RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we don't
   112 	                                       * even check its return value
   113 	                                       * (which we should) */
   114   if(errno==ENOMEM)
   115   {
   116 	  return 1;
   117   }	                                       
   118 
   119 	ERR_load_BN_strings();
   120 	if(errno==ENOMEM)
   121     {
   122 	  return 1;
   123 	}	    
   124 
   125 	ctx=BN_CTX_new();
   126 	if (ctx == NULL)
   127 	{
   128 	 if(errno==ENOMEM)
   129      {
   130 	  return 1;
   131 	 }
   132 	 return 1;	    
   133 	} 
   134 	r_mont=BN_new();
   135 	if(r_mont==NULL&&errno==ENOMEM)
   136     {
   137 	  return 1;
   138 	}	    
   139 	r_mont_const=BN_new();
   140 	if(r_mont_const==NULL&&errno==ENOMEM)
   141   {
   142 	  return 1;
   143 	}	    
   144 
   145 	r_recp=BN_new();
   146 	if(r_recp==NULL&&errno==ENOMEM)
   147    {
   148 	  return 1;
   149 	}	    
   150 
   151 	r_simple=BN_new();
   152 	if(r_simple==NULL&&errno==ENOMEM)
   153   {
   154 	  return 1;
   155 	}	    
   156 
   157 	a=BN_new();
   158 	if(a==NULL&&errno==ENOMEM)
   159   {
   160 	  return 1;
   161 	}	    
   162 
   163 	b=BN_new();
   164 	if(b==NULL&&errno==ENOMEM)
   165   {
   166 	  return 1;
   167 	}	    
   168 	
   169 	m=BN_new();
   170 	if(m==NULL&&errno==ENOMEM)
   171   {
   172 	  return 1;
   173 	}	    
   174 	
   175 	if (	(r_mont == NULL) || (r_recp == NULL) ||
   176 		(a == NULL) || (b == NULL))
   177 		goto err;
   178 
   179 	out=BIO_new(BIO_s_file());
   180 	if(out==NULL&&errno==ENOMEM)
   181   {
   182 	  return 1;
   183 	}	    
   184 	if (out == NULL) 
   185 	return 1;
   186 	BIO_set_fp(out,stdout,BIO_NOCLOSE);
   187 	if(errno==ENOMEM)
   188   {
   189 	  return 1;
   190 	}	    
   191 
   192 // temp = fopen("sanjeev.txt", "w");
   193 	
   194 	for (i=0; i<200; i++)
   195 		{
   196 	//	fputc(i,temp);
   197 		RAND_bytes(&c,1);
   198   	if(errno==ENOMEM)
   199     {
   200 	    return 1;
   201 	  }	    
   202 
   203 		c=(c%BN_BITS)-BN_BITS2;
   204 		BN_rand(a,NUM_BITS+c,0,0);
   205 		if(errno==ENOMEM)
   206     {
   207 	   return 1;
   208 	  }	    
   209 		RAND_bytes(&c,1);
   210   	if(errno==ENOMEM)
   211     {
   212 	    return 1;
   213 	  }	    
   214 		c=(c%BN_BITS)-BN_BITS2;
   215 		BN_rand(b,NUM_BITS+c,0,0);
   216   	if(errno==ENOMEM)
   217     {
   218 	    return 1;
   219 	  }	    
   220 		RAND_bytes(&c,1);
   221 		if(errno==ENOMEM)
   222     {
   223 	   return 1;
   224 	  }	    
   225 
   226 		c=(c%BN_BITS)-BN_BITS2;
   227 		BN_rand(m,NUM_BITS+c,0,1);
   228 		if(errno==ENOMEM)
   229     {
   230 	   return 1;
   231 	  }	    
   232 
   233 		BN_mod(a,a,m,ctx);
   234 		if(errno==ENOMEM)
   235     {
   236 	   return 1;
   237 	  }	    
   238 	
   239 		BN_mod(b,b,m,ctx);
   240 		if(errno==ENOMEM)
   241       {
   242 	   return 1;
   243 	  }	    
   244 
   245 		ret=BN_mod_exp_mont(r_mont,a,b,m,ctx,NULL);
   246 		if (ret <= 0)
   247 			{
   248 			if(errno==ENOMEM)
   249       {
   250 	     return 1;
   251 	    }	    
   252 			fprintf(stdout,"BN_mod_exp_mont() problems\n");
   253 			ERR_print_errors(out);
   254   		if(errno==ENOMEM)
   255       {
   256 	     return 1;
   257 	    }	    
   258 			return 1;
   259 			}
   260 
   261 		ret=BN_mod_exp_recp(r_recp,a,b,m,ctx);
   262 		if (ret <= 0)
   263 			{
   264   		if(errno==ENOMEM)
   265        {
   266 	       return 1;
   267 	     }	    
   268 			fprintf(stdout,"BN_mod_exp_recp() problems\n");
   269 			ERR_print_errors(out);
   270 			if(errno==ENOMEM)
   271       {
   272 	    return 1;
   273 	    }	    
   274 			return 1;
   275 			}
   276 
   277 		ret=BN_mod_exp_simple(r_simple,a,b,m,ctx);
   278 		if (ret <= 0)
   279 			{
   280   		if(errno==ENOMEM)
   281        {
   282 	       return 1;
   283 	     }	    
   284 			fprintf(stdout,"BN_mod_exp_simple() problems\n");
   285 			ERR_print_errors(out);
   286   		if(errno==ENOMEM)
   287        {
   288 	       return 1;
   289 	     }	    
   290 			return 1;
   291 			}
   292 
   293 		ret=BN_mod_exp_mont_consttime(r_mont_const,a,b,m,ctx,NULL);
   294 		if (ret <= 0)
   295 			{
   296   		if(errno==ENOMEM)
   297        {
   298 	       return 1;
   299 	     }	    
   300 			fprintf(stdout,"BN_mod_exp_mont_consttime() problems\n");
   301 			ERR_print_errors(out);
   302   		if(errno==ENOMEM)
   303        {
   304 	       return 1;
   305 	     }	    
   306 			return 1;
   307 			}
   308 
   309 		if (BN_cmp(r_simple, r_mont) == 0
   310 		    && BN_cmp(r_simple,r_recp) == 0
   311 			&& BN_cmp(r_simple,r_mont_const) == 0)
   312 			{
   313   		if(errno==ENOMEM)
   314        {
   315 	       return 1;
   316 	     }	    
   317       fprintf(stdout,".");
   318 			fflush(stdout);
   319 			}
   320 		else
   321 		  	{
   322 			if (BN_cmp(r_simple,r_mont) != 0)
   323        {
   324  	  		if(errno==ENOMEM)
   325         {
   326 	        return 1;
   327 	      }	    
   328 				fprintf(stdout,"\nsimple and mont results differ\n");
   329        }				
   330 			if (BN_cmp(r_simple,r_mont) != 0)
   331 			{
   332  	  		if(errno==ENOMEM)
   333         {
   334 	        return 1;
   335 	      }	    
   336 				fprintf(stdout,"\nsimple and mont const time results differ\n");
   337 			}	
   338 			if (BN_cmp(r_simple,r_recp) != 0)
   339 			{
   340  	  		if(errno==ENOMEM)
   341         {
   342 	        return 1;
   343 	      }	    
   344 				fprintf(stdout,"\nsimple and recp results differ\n");
   345       }
   346 			fprintf(stdout,"a (%3d) = ",BN_num_bits(a));   BN_print(out,a);
   347   		if(errno==ENOMEM)
   348       {
   349 	       return 1;
   350       }	    
   351 			fprintf(stdout,"\nb (%3d) = ",BN_num_bits(b)); BN_print(out,b);
   352  	  	if(errno==ENOMEM)
   353       {
   354 	       return 1;
   355       }	    
   356 			fprintf(stdout,"\nm (%3d) = ",BN_num_bits(m)); BN_print(out,m);
   357  	  	if(errno==ENOMEM)
   358       {
   359 	       return 1;
   360       }	    
   361 			fprintf(stdout,"\nsimple   =");	BN_print(out,r_simple);
   362  	  	if(errno==ENOMEM)
   363       {
   364 	       return 1;
   365       }	    
   366 			fprintf(stdout,"\nrecp     =");	BN_print(out,r_recp);
   367  	  	if(errno==ENOMEM)
   368       {
   369 	       return 1;
   370       }	    
   371 			fprintf(stdout,"\nmont     ="); BN_print(out,r_mont);
   372  	  	if(errno==ENOMEM)
   373       {
   374 	       return 1;
   375       }	    
   376 			fprintf(stdout,"\nmont_ct  ="); BN_print(out,r_mont_const);
   377  	  	if(errno==ENOMEM)
   378       {
   379 	       return 1;
   380       }	    
   381 			fprintf(stdout,"\n");
   382 			return 1;
   383 			}
   384 		}
   385 	BN_free(r_mont);
   386 	BN_free(r_mont_const);
   387 	BN_free(r_recp);
   388 	BN_free(r_simple);
   389 	BN_free(a);
   390 	BN_free(b);
   391 	BN_free(m);
   392 	BN_CTX_free(ctx);
   393 	ERR_remove_state(0);
   394 	if(errno==ENOMEM)
   395   {
   396     return 1;
   397   }	    
   398 	CRYPTO_mem_leaks(out);
   399 	if(errno==ENOMEM)
   400   {
   401     return 1;
   402   }	    
   403 	BIO_free(out);
   404 	if(errno==ENOMEM)
   405    {
   406 	  return 1;
   407 	 }    
   408  
   409     CRYPTO_cleanup_all_ex_data();
   410    if(errno==ENOMEM)
   411    {
   412 	  return 1;
   413 	 }    
   414  
   415 	fprintf(stdout," done\n");
   416 	fprintf(stdout," Test case passed\n");
   417 	return 0;
   418 err:
   419 	ERR_load_crypto_strings();
   420 	if(errno==ENOMEM)
   421   {
   422     return 1;
   423   }	    
   424 	
   425 	ERR_print_errors(out);
   426 	if(errno==ENOMEM)
   427   {
   428     return 1;
   429   }	    
   430 	
   431 	
   432 #ifdef OPENSSL_SYS_NETWARE
   433     fprintf(stdout,"ERROR\n");
   434 #endif
   435 	return(1);
   436 	}
   437