os/ossrv/ssl/tsrc/crypto_test/src/exptest.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/tsrc/crypto_test/src/exptest.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,433 @@
     1.4 +/* crypto/bn/exptest.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +/*
    1.62 + © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    1.63 + */
    1.64 +#include <stdio.h>
    1.65 +#include <stdlib.h>
    1.66 +#include <string.h>
    1.67 +#ifndef SYMBIAN
    1.68 +#include "../e_os.h"
    1.69 +#else
    1.70 +#include "e_os.h"
    1.71 +#endif
    1.72 +
    1.73 +#include <openssl/bio.h>
    1.74 +#include <openssl/bn.h>
    1.75 +#include <openssl/rand.h>
    1.76 +#include <openssl/err.h>
    1.77 +#ifdef SYMBIAN
    1.78 +#ifdef stdin
    1.79 +#undef stdin
    1.80 +#endif
    1.81 +#ifdef stdout
    1.82 +#undef stdout
    1.83 +#endif
    1.84 +#ifdef stderr
    1.85 +#undef stderr
    1.86 +#endif
    1.87 +
    1.88 +#define stdin fp_stdin
    1.89 +#define stdout fp_stdout
    1.90 +#define stderr fp_stderr
    1.91 +
    1.92 +extern FILE *fp_stdout;
    1.93 +extern FILE *fp_stderr;
    1.94 +#endif
    1.95 +
    1.96 +
    1.97 +#define NUM_BITS	(BN_BITS*2)
    1.98 +
    1.99 +static const char rnd_seed[] = "string to make the random number generator think it has entropy";
   1.100 +#ifndef SYMBIAN
   1.101 +int main(int argc, char *argv[])
   1.102 +#else
   1.103 +int exp_main(int argc, char *argv[])
   1.104 +#endif
   1.105 +	{
   1.106 +	BN_CTX *ctx;
   1.107 +	BIO *out=NULL;
   1.108 +	int i,ret;
   1.109 +	unsigned char c;
   1.110 +	BIGNUM *r_mont,*r_mont_const,*r_recp,*r_simple,*a,*b,*m;
   1.111 +  
   1.112 +    
   1.113 +	RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we don't
   1.114 +	                                       * even check its return value
   1.115 +	                                       * (which we should) */
   1.116 +  if(errno==ENOMEM)
   1.117 +  {
   1.118 +	  return 1;
   1.119 +  }	                                       
   1.120 +
   1.121 +	ERR_load_BN_strings();
   1.122 +	if(errno==ENOMEM)
   1.123 +    {
   1.124 +	  return 1;
   1.125 +	}	    
   1.126 +
   1.127 +	ctx=BN_CTX_new();
   1.128 +	if (ctx == NULL)
   1.129 +	{
   1.130 +	 if(errno==ENOMEM)
   1.131 +     {
   1.132 +	  return 1;
   1.133 +	 }
   1.134 +	 return 1;	    
   1.135 +	} 
   1.136 +	r_mont=BN_new();
   1.137 +	if(r_mont==NULL&&errno==ENOMEM)
   1.138 +    {
   1.139 +	  return 1;
   1.140 +	}	    
   1.141 +	r_mont_const=BN_new();
   1.142 +	if(r_mont_const==NULL&&errno==ENOMEM)
   1.143 +  {
   1.144 +	  return 1;
   1.145 +	}	    
   1.146 +
   1.147 +	r_recp=BN_new();
   1.148 +	if(r_recp==NULL&&errno==ENOMEM)
   1.149 +   {
   1.150 +	  return 1;
   1.151 +	}	    
   1.152 +
   1.153 +	r_simple=BN_new();
   1.154 +	if(r_simple==NULL&&errno==ENOMEM)
   1.155 +  {
   1.156 +	  return 1;
   1.157 +	}	    
   1.158 +
   1.159 +	a=BN_new();
   1.160 +	if(a==NULL&&errno==ENOMEM)
   1.161 +  {
   1.162 +	  return 1;
   1.163 +	}	    
   1.164 +
   1.165 +	b=BN_new();
   1.166 +	if(b==NULL&&errno==ENOMEM)
   1.167 +  {
   1.168 +	  return 1;
   1.169 +	}	    
   1.170 +	
   1.171 +	m=BN_new();
   1.172 +	if(m==NULL&&errno==ENOMEM)
   1.173 +  {
   1.174 +	  return 1;
   1.175 +	}	    
   1.176 +	
   1.177 +	if (	(r_mont == NULL) || (r_recp == NULL) ||
   1.178 +		(a == NULL) || (b == NULL))
   1.179 +		goto err;
   1.180 +
   1.181 +	out=BIO_new(BIO_s_file());
   1.182 +	if(out==NULL&&errno==ENOMEM)
   1.183 +  {
   1.184 +	  return 1;
   1.185 +	}	    
   1.186 +	if (out == NULL) 
   1.187 +	return 1;
   1.188 +	BIO_set_fp(out,stdout,BIO_NOCLOSE);
   1.189 +	if(errno==ENOMEM)
   1.190 +  {
   1.191 +	  return 1;
   1.192 +	}	    
   1.193 +
   1.194 +	for (i=0; i<200; i++)
   1.195 +		{
   1.196 +		RAND_bytes(&c,1);
   1.197 +  	if(errno==ENOMEM)
   1.198 +    {
   1.199 +	    return 1;
   1.200 +	  }	    
   1.201 +
   1.202 +		c=(c%BN_BITS)-BN_BITS2;
   1.203 +		BN_rand(a,NUM_BITS+c,0,0);
   1.204 +		if(errno==ENOMEM)
   1.205 +    {
   1.206 +	   return 1;
   1.207 +	  }	    
   1.208 +		RAND_bytes(&c,1);
   1.209 +  	if(errno==ENOMEM)
   1.210 +    {
   1.211 +	    return 1;
   1.212 +	  }	    
   1.213 +		c=(c%BN_BITS)-BN_BITS2;
   1.214 +		BN_rand(b,NUM_BITS+c,0,0);
   1.215 +  	if(errno==ENOMEM)
   1.216 +    {
   1.217 +	    return 1;
   1.218 +	  }	    
   1.219 +		RAND_bytes(&c,1);
   1.220 +		if(errno==ENOMEM)
   1.221 +    {
   1.222 +	   return 1;
   1.223 +	  }	    
   1.224 +
   1.225 +		c=(c%BN_BITS)-BN_BITS2;
   1.226 +		BN_rand(m,NUM_BITS+c,0,1);
   1.227 +		if(errno==ENOMEM)
   1.228 +    {
   1.229 +	   return 1;
   1.230 +	  }	    
   1.231 +
   1.232 +		BN_mod(a,a,m,ctx);
   1.233 +		if(errno==ENOMEM)
   1.234 +    {
   1.235 +	   return 1;
   1.236 +	  }	    
   1.237 +	
   1.238 +		BN_mod(b,b,m,ctx);
   1.239 +		if(errno==ENOMEM)
   1.240 +      {
   1.241 +	   return 1;
   1.242 +	  }	    
   1.243 +
   1.244 +		ret=BN_mod_exp_mont(r_mont,a,b,m,ctx,NULL);
   1.245 +		if (ret <= 0)
   1.246 +			{
   1.247 +			if(errno==ENOMEM)
   1.248 +      {
   1.249 +	     return 1;
   1.250 +	    }	    
   1.251 +			fprintf(stdout,"BN_mod_exp_mont() problems\n");
   1.252 +			ERR_print_errors(out);
   1.253 +  		if(errno==ENOMEM)
   1.254 +      {
   1.255 +	     return 1;
   1.256 +	    }	    
   1.257 +			return 1;
   1.258 +			}
   1.259 +
   1.260 +		ret=BN_mod_exp_recp(r_recp,a,b,m,ctx);
   1.261 +		if (ret <= 0)
   1.262 +			{
   1.263 +  		if(errno==ENOMEM)
   1.264 +       {
   1.265 +	       return 1;
   1.266 +	     }	    
   1.267 +			fprintf(stdout,"BN_mod_exp_recp() problems\n");
   1.268 +			ERR_print_errors(out);
   1.269 +			if(errno==ENOMEM)
   1.270 +      {
   1.271 +	    return 1;
   1.272 +	    }	    
   1.273 +			return 1;
   1.274 +			}
   1.275 +
   1.276 +		ret=BN_mod_exp_simple(r_simple,a,b,m,ctx);
   1.277 +		if (ret <= 0)
   1.278 +			{
   1.279 +  		if(errno==ENOMEM)
   1.280 +       {
   1.281 +	       return 1;
   1.282 +	     }	    
   1.283 +			fprintf(stdout,"BN_mod_exp_simple() problems\n");
   1.284 +			ERR_print_errors(out);
   1.285 +  		if(errno==ENOMEM)
   1.286 +       {
   1.287 +	       return 1;
   1.288 +	     }	    
   1.289 +			return 1;
   1.290 +			}
   1.291 +
   1.292 +		ret=BN_mod_exp_mont_consttime(r_mont_const,a,b,m,ctx,NULL);
   1.293 +		if (ret <= 0)
   1.294 +			{
   1.295 +  		if(errno==ENOMEM)
   1.296 +       {
   1.297 +	       return 1;
   1.298 +	     }	    
   1.299 +			fprintf(stdout,"BN_mod_exp_mont_consttime() problems\n");
   1.300 +			ERR_print_errors(out);
   1.301 +  		if(errno==ENOMEM)
   1.302 +       {
   1.303 +	       return 1;
   1.304 +	     }	    
   1.305 +			return 1;
   1.306 +			}
   1.307 +
   1.308 +		if (BN_cmp(r_simple, r_mont) == 0
   1.309 +		    && BN_cmp(r_simple,r_recp) == 0
   1.310 +			&& BN_cmp(r_simple,r_mont_const) == 0)
   1.311 +			{
   1.312 +  		if(errno==ENOMEM)
   1.313 +       {
   1.314 +	       return 1;
   1.315 +	     }	    
   1.316 +      fprintf(stdout,".");
   1.317 +			fflush(stdout);
   1.318 +			}
   1.319 +		else
   1.320 +		  	{
   1.321 +			if (BN_cmp(r_simple,r_mont) != 0)
   1.322 +       {
   1.323 + 	  		if(errno==ENOMEM)
   1.324 +        {
   1.325 +	        return 1;
   1.326 +	      }	    
   1.327 +				fprintf(stdout,"\nsimple and mont results differ\n");
   1.328 +       }				
   1.329 +			if (BN_cmp(r_simple,r_mont) != 0)
   1.330 +			{
   1.331 + 	  		if(errno==ENOMEM)
   1.332 +        {
   1.333 +	        return 1;
   1.334 +	      }	    
   1.335 +				fprintf(stdout,"\nsimple and mont const time results differ\n");
   1.336 +			}	
   1.337 +			if (BN_cmp(r_simple,r_recp) != 0)
   1.338 +			{
   1.339 + 	  		if(errno==ENOMEM)
   1.340 +        {
   1.341 +	        return 1;
   1.342 +	      }	    
   1.343 +				fprintf(stdout,"\nsimple and recp results differ\n");
   1.344 +      }
   1.345 +			fprintf(stdout,"a (%3d) = ",BN_num_bits(a));   BN_print(out,a);
   1.346 +  		if(errno==ENOMEM)
   1.347 +      {
   1.348 +	       return 1;
   1.349 +      }	    
   1.350 +			fprintf(stdout,"\nb (%3d) = ",BN_num_bits(b)); BN_print(out,b);
   1.351 + 	  	if(errno==ENOMEM)
   1.352 +      {
   1.353 +	       return 1;
   1.354 +      }	    
   1.355 +			fprintf(stdout,"\nm (%3d) = ",BN_num_bits(m)); BN_print(out,m);
   1.356 + 	  	if(errno==ENOMEM)
   1.357 +      {
   1.358 +	       return 1;
   1.359 +      }	    
   1.360 +			fprintf(stdout,"\nsimple   =");	BN_print(out,r_simple);
   1.361 + 	  	if(errno==ENOMEM)
   1.362 +      {
   1.363 +	       return 1;
   1.364 +      }	    
   1.365 +			fprintf(stdout,"\nrecp     =");	BN_print(out,r_recp);
   1.366 + 	  	if(errno==ENOMEM)
   1.367 +      {
   1.368 +	       return 1;
   1.369 +      }	    
   1.370 +			fprintf(stdout,"\nmont     ="); BN_print(out,r_mont);
   1.371 + 	  	if(errno==ENOMEM)
   1.372 +      {
   1.373 +	       return 1;
   1.374 +      }	    
   1.375 +			fprintf(stdout,"\nmont_ct  ="); BN_print(out,r_mont_const);
   1.376 + 	  	if(errno==ENOMEM)
   1.377 +      {
   1.378 +	       return 1;
   1.379 +      }	    
   1.380 +			fprintf(stdout,"\n");
   1.381 +			return 1;
   1.382 +			}
   1.383 +		}
   1.384 +	BN_free(r_mont);
   1.385 +	BN_free(r_mont_const);
   1.386 +	BN_free(r_recp);
   1.387 +	BN_free(r_simple);
   1.388 +	BN_free(a);
   1.389 +	BN_free(b);
   1.390 +	BN_free(m);
   1.391 +	BN_CTX_free(ctx);
   1.392 +	ERR_remove_state(0);
   1.393 +	if(errno==ENOMEM)
   1.394 +  {
   1.395 +    return 1;
   1.396 +  }	    
   1.397 +	CRYPTO_mem_leaks(out);
   1.398 +	if(errno==ENOMEM)
   1.399 +  {
   1.400 +    return 1;
   1.401 +  }	    
   1.402 +	BIO_free(out);
   1.403 +	if(errno==ENOMEM)
   1.404 +   {
   1.405 +	  return 1;
   1.406 +	 }    
   1.407 + 
   1.408 +    CRYPTO_cleanup_all_ex_data();
   1.409 +   if(errno==ENOMEM)
   1.410 +   {
   1.411 +	  return 1;
   1.412 +	 }    
   1.413 + 
   1.414 +	fprintf(stdout," done\n");
   1.415 +	fprintf(stdout," Test case passed\n");
   1.416 +	return 0;
   1.417 +err:
   1.418 +	ERR_load_crypto_strings();
   1.419 +	if(errno==ENOMEM)
   1.420 +  {
   1.421 +    return 1;
   1.422 +  }	    
   1.423 +	
   1.424 +	ERR_print_errors(out);
   1.425 +	if(errno==ENOMEM)
   1.426 +  {
   1.427 +    return 1;
   1.428 +  }	    
   1.429 +	
   1.430 +	
   1.431 +#ifdef OPENSSL_SYS_NETWARE
   1.432 +    fprintf(stdout,"ERROR\n");
   1.433 +#endif
   1.434 +	return(1);
   1.435 +	}
   1.436 +