1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/tsrc/BC/libcrypto/crypto_test/src/exptest.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,437 @@
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 + // FILE* temp;
1.112 +
1.113 +
1.114 + RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we don't
1.115 + * even check its return value
1.116 + * (which we should) */
1.117 + if(errno==ENOMEM)
1.118 + {
1.119 + return 1;
1.120 + }
1.121 +
1.122 + ERR_load_BN_strings();
1.123 + if(errno==ENOMEM)
1.124 + {
1.125 + return 1;
1.126 + }
1.127 +
1.128 + ctx=BN_CTX_new();
1.129 + if (ctx == NULL)
1.130 + {
1.131 + if(errno==ENOMEM)
1.132 + {
1.133 + return 1;
1.134 + }
1.135 + return 1;
1.136 + }
1.137 + r_mont=BN_new();
1.138 + if(r_mont==NULL&&errno==ENOMEM)
1.139 + {
1.140 + return 1;
1.141 + }
1.142 + r_mont_const=BN_new();
1.143 + if(r_mont_const==NULL&&errno==ENOMEM)
1.144 + {
1.145 + return 1;
1.146 + }
1.147 +
1.148 + r_recp=BN_new();
1.149 + if(r_recp==NULL&&errno==ENOMEM)
1.150 + {
1.151 + return 1;
1.152 + }
1.153 +
1.154 + r_simple=BN_new();
1.155 + if(r_simple==NULL&&errno==ENOMEM)
1.156 + {
1.157 + return 1;
1.158 + }
1.159 +
1.160 + a=BN_new();
1.161 + if(a==NULL&&errno==ENOMEM)
1.162 + {
1.163 + return 1;
1.164 + }
1.165 +
1.166 + b=BN_new();
1.167 + if(b==NULL&&errno==ENOMEM)
1.168 + {
1.169 + return 1;
1.170 + }
1.171 +
1.172 + m=BN_new();
1.173 + if(m==NULL&&errno==ENOMEM)
1.174 + {
1.175 + return 1;
1.176 + }
1.177 +
1.178 + if ( (r_mont == NULL) || (r_recp == NULL) ||
1.179 + (a == NULL) || (b == NULL))
1.180 + goto err;
1.181 +
1.182 + out=BIO_new(BIO_s_file());
1.183 + if(out==NULL&&errno==ENOMEM)
1.184 + {
1.185 + return 1;
1.186 + }
1.187 + if (out == NULL)
1.188 + return 1;
1.189 + BIO_set_fp(out,stdout,BIO_NOCLOSE);
1.190 + if(errno==ENOMEM)
1.191 + {
1.192 + return 1;
1.193 + }
1.194 +
1.195 +// temp = fopen("sanjeev.txt", "w");
1.196 +
1.197 + for (i=0; i<200; i++)
1.198 + {
1.199 + // fputc(i,temp);
1.200 + RAND_bytes(&c,1);
1.201 + if(errno==ENOMEM)
1.202 + {
1.203 + return 1;
1.204 + }
1.205 +
1.206 + c=(c%BN_BITS)-BN_BITS2;
1.207 + BN_rand(a,NUM_BITS+c,0,0);
1.208 + if(errno==ENOMEM)
1.209 + {
1.210 + return 1;
1.211 + }
1.212 + RAND_bytes(&c,1);
1.213 + if(errno==ENOMEM)
1.214 + {
1.215 + return 1;
1.216 + }
1.217 + c=(c%BN_BITS)-BN_BITS2;
1.218 + BN_rand(b,NUM_BITS+c,0,0);
1.219 + if(errno==ENOMEM)
1.220 + {
1.221 + return 1;
1.222 + }
1.223 + RAND_bytes(&c,1);
1.224 + if(errno==ENOMEM)
1.225 + {
1.226 + return 1;
1.227 + }
1.228 +
1.229 + c=(c%BN_BITS)-BN_BITS2;
1.230 + BN_rand(m,NUM_BITS+c,0,1);
1.231 + if(errno==ENOMEM)
1.232 + {
1.233 + return 1;
1.234 + }
1.235 +
1.236 + BN_mod(a,a,m,ctx);
1.237 + if(errno==ENOMEM)
1.238 + {
1.239 + return 1;
1.240 + }
1.241 +
1.242 + BN_mod(b,b,m,ctx);
1.243 + if(errno==ENOMEM)
1.244 + {
1.245 + return 1;
1.246 + }
1.247 +
1.248 + ret=BN_mod_exp_mont(r_mont,a,b,m,ctx,NULL);
1.249 + if (ret <= 0)
1.250 + {
1.251 + if(errno==ENOMEM)
1.252 + {
1.253 + return 1;
1.254 + }
1.255 + fprintf(stdout,"BN_mod_exp_mont() problems\n");
1.256 + ERR_print_errors(out);
1.257 + if(errno==ENOMEM)
1.258 + {
1.259 + return 1;
1.260 + }
1.261 + return 1;
1.262 + }
1.263 +
1.264 + ret=BN_mod_exp_recp(r_recp,a,b,m,ctx);
1.265 + if (ret <= 0)
1.266 + {
1.267 + if(errno==ENOMEM)
1.268 + {
1.269 + return 1;
1.270 + }
1.271 + fprintf(stdout,"BN_mod_exp_recp() problems\n");
1.272 + ERR_print_errors(out);
1.273 + if(errno==ENOMEM)
1.274 + {
1.275 + return 1;
1.276 + }
1.277 + return 1;
1.278 + }
1.279 +
1.280 + ret=BN_mod_exp_simple(r_simple,a,b,m,ctx);
1.281 + if (ret <= 0)
1.282 + {
1.283 + if(errno==ENOMEM)
1.284 + {
1.285 + return 1;
1.286 + }
1.287 + fprintf(stdout,"BN_mod_exp_simple() problems\n");
1.288 + ERR_print_errors(out);
1.289 + if(errno==ENOMEM)
1.290 + {
1.291 + return 1;
1.292 + }
1.293 + return 1;
1.294 + }
1.295 +
1.296 + ret=BN_mod_exp_mont_consttime(r_mont_const,a,b,m,ctx,NULL);
1.297 + if (ret <= 0)
1.298 + {
1.299 + if(errno==ENOMEM)
1.300 + {
1.301 + return 1;
1.302 + }
1.303 + fprintf(stdout,"BN_mod_exp_mont_consttime() problems\n");
1.304 + ERR_print_errors(out);
1.305 + if(errno==ENOMEM)
1.306 + {
1.307 + return 1;
1.308 + }
1.309 + return 1;
1.310 + }
1.311 +
1.312 + if (BN_cmp(r_simple, r_mont) == 0
1.313 + && BN_cmp(r_simple,r_recp) == 0
1.314 + && BN_cmp(r_simple,r_mont_const) == 0)
1.315 + {
1.316 + if(errno==ENOMEM)
1.317 + {
1.318 + return 1;
1.319 + }
1.320 + fprintf(stdout,".");
1.321 + fflush(stdout);
1.322 + }
1.323 + else
1.324 + {
1.325 + if (BN_cmp(r_simple,r_mont) != 0)
1.326 + {
1.327 + if(errno==ENOMEM)
1.328 + {
1.329 + return 1;
1.330 + }
1.331 + fprintf(stdout,"\nsimple and mont results differ\n");
1.332 + }
1.333 + if (BN_cmp(r_simple,r_mont) != 0)
1.334 + {
1.335 + if(errno==ENOMEM)
1.336 + {
1.337 + return 1;
1.338 + }
1.339 + fprintf(stdout,"\nsimple and mont const time results differ\n");
1.340 + }
1.341 + if (BN_cmp(r_simple,r_recp) != 0)
1.342 + {
1.343 + if(errno==ENOMEM)
1.344 + {
1.345 + return 1;
1.346 + }
1.347 + fprintf(stdout,"\nsimple and recp results differ\n");
1.348 + }
1.349 + fprintf(stdout,"a (%3d) = ",BN_num_bits(a)); BN_print(out,a);
1.350 + if(errno==ENOMEM)
1.351 + {
1.352 + return 1;
1.353 + }
1.354 + fprintf(stdout,"\nb (%3d) = ",BN_num_bits(b)); BN_print(out,b);
1.355 + if(errno==ENOMEM)
1.356 + {
1.357 + return 1;
1.358 + }
1.359 + fprintf(stdout,"\nm (%3d) = ",BN_num_bits(m)); BN_print(out,m);
1.360 + if(errno==ENOMEM)
1.361 + {
1.362 + return 1;
1.363 + }
1.364 + fprintf(stdout,"\nsimple ="); BN_print(out,r_simple);
1.365 + if(errno==ENOMEM)
1.366 + {
1.367 + return 1;
1.368 + }
1.369 + fprintf(stdout,"\nrecp ="); BN_print(out,r_recp);
1.370 + if(errno==ENOMEM)
1.371 + {
1.372 + return 1;
1.373 + }
1.374 + fprintf(stdout,"\nmont ="); BN_print(out,r_mont);
1.375 + if(errno==ENOMEM)
1.376 + {
1.377 + return 1;
1.378 + }
1.379 + fprintf(stdout,"\nmont_ct ="); BN_print(out,r_mont_const);
1.380 + if(errno==ENOMEM)
1.381 + {
1.382 + return 1;
1.383 + }
1.384 + fprintf(stdout,"\n");
1.385 + return 1;
1.386 + }
1.387 + }
1.388 + BN_free(r_mont);
1.389 + BN_free(r_mont_const);
1.390 + BN_free(r_recp);
1.391 + BN_free(r_simple);
1.392 + BN_free(a);
1.393 + BN_free(b);
1.394 + BN_free(m);
1.395 + BN_CTX_free(ctx);
1.396 + ERR_remove_state(0);
1.397 + if(errno==ENOMEM)
1.398 + {
1.399 + return 1;
1.400 + }
1.401 + CRYPTO_mem_leaks(out);
1.402 + if(errno==ENOMEM)
1.403 + {
1.404 + return 1;
1.405 + }
1.406 + BIO_free(out);
1.407 + if(errno==ENOMEM)
1.408 + {
1.409 + return 1;
1.410 + }
1.411 +
1.412 + CRYPTO_cleanup_all_ex_data();
1.413 + if(errno==ENOMEM)
1.414 + {
1.415 + return 1;
1.416 + }
1.417 +
1.418 + fprintf(stdout," done\n");
1.419 + fprintf(stdout," Test case passed\n");
1.420 + return 0;
1.421 +err:
1.422 + ERR_load_crypto_strings();
1.423 + if(errno==ENOMEM)
1.424 + {
1.425 + return 1;
1.426 + }
1.427 +
1.428 + ERR_print_errors(out);
1.429 + if(errno==ENOMEM)
1.430 + {
1.431 + return 1;
1.432 + }
1.433 +
1.434 +
1.435 +#ifdef OPENSSL_SYS_NETWARE
1.436 + fprintf(stdout,"ERROR\n");
1.437 +#endif
1.438 + return(1);
1.439 + }
1.440 +