1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/tsrc/crypto_test/src/dhtest.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,489 @@
1.4 +/* crypto/dh/dhtest.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 +/* Until the key-gen callbacks are modified to use newer prototypes, we allow
1.65 + * deprecated functions for openssl-internal code */
1.66 + #ifndef SYMBIAN
1.67 +#ifdef OPENSSL_NO_DEPRECATED
1.68 +#undef OPENSSL_NO_DEPRECATED
1.69 +#endif
1.70 +#endif
1.71 +#include <stdio.h>
1.72 +#include <stdlib.h>
1.73 +#include <string.h>
1.74 +#ifndef SYMBIAN
1.75 +#include "../e_os.h"
1.76 +#else
1.77 +#include "e_os.h"
1.78 +#endif
1.79 +
1.80 +#include <openssl/crypto.h>
1.81 +#include <openssl/bio.h>
1.82 +#include <openssl/bn.h>
1.83 +#include <openssl/rand.h>
1.84 +#include <openssl/err.h>
1.85 +
1.86 +#ifdef SYMBIAN
1.87 +#ifdef stdin
1.88 +#undef stdin
1.89 +#endif
1.90 +#ifdef stdout
1.91 +#undef stdout
1.92 +#endif
1.93 +#ifdef stderr
1.94 +#undef stderr
1.95 +#endif
1.96 +
1.97 +#define stdin fp_stdin
1.98 +#define stdout fp_stdout
1.99 +#define stderr fp_stderr
1.100 +
1.101 +extern FILE *fp_stdout;
1.102 +extern FILE *fp_stderr;
1.103 +#endif
1.104 +#ifdef OPENSSL_NO_DH
1.105 +int main(int argc, char *argv[])
1.106 +{
1.107 + fprintf(stdout,"No DH support\n");
1.108 + return(0);
1.109 +}
1.110 +#else
1.111 +#include <openssl/dh.h>
1.112 +
1.113 +#ifdef OPENSSL_SYS_WIN16
1.114 +#define MS_CALLBACK _far _loadds
1.115 +#else
1.116 +#define MS_CALLBACK
1.117 +#endif
1.118 +
1.119 +static int MS_CALLBACK cb(int p, int n, BN_GENCB *arg);
1.120 +
1.121 +static const char rnd_seed[] = "string to make the random number generator think it has entropy";
1.122 +
1.123 +#ifndef SYMBIAN
1.124 +int main(int argc, char *argv[])
1.125 +#else
1.126 +int dh_main(int argc, char *argv[])
1.127 +#endif
1.128 + {
1.129 + BN_GENCB _cb;
1.130 + DH *a;
1.131 + DH *b=NULL;
1.132 + char buf[12];
1.133 + unsigned char *abuf=NULL,*bbuf=NULL;
1.134 + int i,alen,blen,aout,bout,ret=1;
1.135 + BIO *out;
1.136 +
1.137 + //CRYPTO_malloc_debug_init();
1.138 + //CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
1.139 + //CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
1.140 +
1.141 +#ifdef OPENSSL_SYS_WIN32
1.142 + CRYPTO_malloc_init();
1.143 +#endif
1.144 +
1.145 + RAND_seed(rnd_seed, sizeof rnd_seed);
1.146 + if(errno==ENOMEM)
1.147 + {
1.148 + return 1;
1.149 + }
1.150 + out=BIO_new(BIO_s_file());
1.151 + if( out == NULL&& errno==ENOMEM)
1.152 + {
1.153 + return 1;
1.154 + }
1.155 + if (out == NULL)
1.156 + return 1;
1.157 + //EXIT(1);
1.158 + BIO_set_fp(out,stdout,BIO_NOCLOSE);
1.159 + if(errno==ENOMEM)
1.160 + {
1.161 + return 1;
1.162 + }
1.163 +
1.164 + BN_GENCB_set(&_cb, &cb, out);
1.165 + if(errno==ENOMEM)
1.166 + {
1.167 + return 1;
1.168 + }
1.169 + if(((a = DH_new()) == NULL))
1.170 + {
1.171 + if(errno==ENOMEM)
1.172 + {
1.173 + return 1;
1.174 + }
1.175 + goto err;
1.176 + }
1.177 + if( !DH_generate_parameters_ex(a, 64,
1.178 + DH_GENERATOR_5, &_cb))
1.179 + {
1.180 + if(a != NULL) DH_free(a);
1.181 + if(errno==ENOMEM)
1.182 + {
1.183 + return 1;
1.184 + }
1.185 +
1.186 + }
1.187 + if (!DH_check(a, &i))
1.188 + {
1.189 + if(errno==ENOMEM)
1.190 + {
1.191 + return 1;
1.192 + }
1.193 + goto err;
1.194 + }
1.195 + if (i & DH_CHECK_P_NOT_PRIME)
1.196 + BIO_puts(out, "p value is not prime\n");
1.197 + if(errno==ENOMEM)
1.198 + {
1.199 + return 1;
1.200 + }
1.201 +
1.202 + if (i & DH_CHECK_P_NOT_SAFE_PRIME)
1.203 + BIO_puts(out, "p value is not a safe prime\n");
1.204 + if(errno==ENOMEM)
1.205 + {
1.206 + return 1;
1.207 + }
1.208 +
1.209 + if (i & DH_UNABLE_TO_CHECK_GENERATOR)
1.210 + BIO_puts(out, "unable to check the generator value\n");
1.211 + if(errno==ENOMEM)
1.212 + {
1.213 + return 1;
1.214 + }
1.215 +
1.216 + if (i & DH_NOT_SUITABLE_GENERATOR)
1.217 + BIO_puts(out, "the g value is not a generator\n");
1.218 + if(errno==ENOMEM)
1.219 + {
1.220 + return 1;
1.221 + }
1.222 +
1.223 + BIO_puts(out,"\np =");
1.224 + if(errno==ENOMEM)
1.225 + {
1.226 + return 1;
1.227 + }
1.228 +
1.229 + BN_print(out,a->p);
1.230 + if(errno==ENOMEM)
1.231 + {
1.232 + return 1;
1.233 + }
1.234 +
1.235 + BIO_puts(out,"\ng =");
1.236 + if(errno==ENOMEM)
1.237 + {
1.238 + return 1;
1.239 + }
1.240 +
1.241 + BN_print(out,a->g);
1.242 + if(errno==ENOMEM)
1.243 + {
1.244 + return 1;
1.245 + }
1.246 +
1.247 + BIO_puts(out,"\n");
1.248 + if(errno==ENOMEM)
1.249 + {
1.250 + return 1;
1.251 + }
1.252 +
1.253 + b=DH_new();
1.254 + if( b == NULL&& errno==ENOMEM)
1.255 + {
1.256 + return 1;
1.257 + }
1.258 + if (b == NULL) goto err;
1.259 +
1.260 + b->p=BN_dup(a->p);
1.261 + if(errno==ENOMEM)
1.262 + {
1.263 + return 1;
1.264 + }
1.265 + b->g=BN_dup(a->g);
1.266 + if(errno==ENOMEM)
1.267 + {
1.268 + return 1;
1.269 + }
1.270 + if ((b->p == NULL) || (b->g == NULL))
1.271 + {
1.272 + if(errno==ENOMEM)
1.273 + {
1.274 + return 1;
1.275 + }
1.276 + goto err;
1.277 + }
1.278 +
1.279 + /* Set a to run with normal modexp and b to use constant time */
1.280 + a->flags &= ~DH_FLAG_NO_EXP_CONSTTIME;
1.281 + b->flags |= DH_FLAG_NO_EXP_CONSTTIME;
1.282 +
1.283 + if (!DH_generate_key(a))
1.284 + {
1.285 + if(errno==ENOMEM)
1.286 + {
1.287 + return 1;
1.288 + }
1.289 + goto err;
1.290 + }
1.291 + BIO_puts(out,"pri 1=");
1.292 + if(errno==ENOMEM)
1.293 + {
1.294 + return 1;
1.295 + }
1.296 + BN_print(out,a->priv_key);
1.297 + if(errno==ENOMEM)
1.298 + {
1.299 + return 1;
1.300 + }
1.301 + BIO_puts(out,"\npub 1=");
1.302 + if(errno==ENOMEM)
1.303 + {
1.304 + return 1;
1.305 + }
1.306 + BN_print(out,a->pub_key);
1.307 + if(errno==ENOMEM)
1.308 + {
1.309 + return 1;
1.310 + }
1.311 + BIO_puts(out,"\n");
1.312 + if(errno==ENOMEM)
1.313 + {
1.314 + return 1;
1.315 + }
1.316 + if (!DH_generate_key(b))
1.317 + {
1.318 + if(errno==ENOMEM)
1.319 + {
1.320 + return 1;
1.321 + }
1.322 + goto err;
1.323 + }
1.324 + BIO_puts(out,"pri 2=");
1.325 + if(errno==ENOMEM)
1.326 + {
1.327 + return 1;
1.328 + }
1.329 + BN_print(out,b->priv_key);
1.330 + if(errno==ENOMEM)
1.331 + {
1.332 + return 1;
1.333 + }
1.334 + BIO_puts(out,"\npub 2=");
1.335 + if(errno==ENOMEM)
1.336 + {
1.337 + return 1;
1.338 + }
1.339 + BN_print(out,b->pub_key);
1.340 + if(errno==ENOMEM)
1.341 + {
1.342 + return 1;
1.343 + }
1.344 + BIO_puts(out,"\n");
1.345 + if(errno==ENOMEM)
1.346 + {
1.347 + return 1;
1.348 + }
1.349 +
1.350 + alen=DH_size(a);
1.351 + if(errno==ENOMEM)
1.352 + {
1.353 + return 1;
1.354 + }
1.355 + abuf=(unsigned char *)OPENSSL_malloc(alen);
1.356 + if(abuf== NULL&& errno==ENOMEM)
1.357 + {
1.358 + return 1;
1.359 + }
1.360 + aout=DH_compute_key(abuf,b->pub_key,a);
1.361 + if(errno==ENOMEM)
1.362 + {
1.363 + return 1;
1.364 + }
1.365 + BIO_puts(out,"key1 =");
1.366 + if(errno==ENOMEM)
1.367 + {
1.368 + return 1;
1.369 + }
1.370 + for (i=0; i<aout; i++)
1.371 + {
1.372 + sprintf(buf,"%02X",abuf[i]);
1.373 + BIO_puts(out,buf);
1.374 + if(errno==ENOMEM)
1.375 + {
1.376 + return 1;
1.377 + }
1.378 + }
1.379 + BIO_puts(out,"\n");
1.380 + if(errno==ENOMEM)
1.381 + {
1.382 + return 1;
1.383 + }
1.384 +
1.385 + blen=DH_size(b);
1.386 + if(errno==ENOMEM)
1.387 + {
1.388 + return 1;
1.389 + }
1.390 + bbuf=(unsigned char *)OPENSSL_malloc(blen);
1.391 + if(bbuf== NULL&& errno==ENOMEM)
1.392 + {
1.393 + return 1;
1.394 + }
1.395 + bout=DH_compute_key(bbuf,a->pub_key,b);
1.396 + if(errno==ENOMEM)
1.397 + {
1.398 + return 1;
1.399 + }
1.400 +
1.401 + BIO_puts(out,"key2 =");
1.402 + if(errno==ENOMEM)
1.403 + {
1.404 + return 1;
1.405 + }
1.406 +
1.407 + for (i=0; i<bout; i++)
1.408 + {
1.409 + sprintf(buf,"%02X",bbuf[i]);
1.410 + BIO_puts(out,buf);
1.411 + if(errno==ENOMEM)
1.412 + {
1.413 + return 1;
1.414 + }
1.415 +
1.416 + }
1.417 + BIO_puts(out,"\n");
1.418 + if(errno==ENOMEM)
1.419 + {
1.420 + return 1;
1.421 + }
1.422 +
1.423 + if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0))
1.424 + {
1.425 + fprintf(stderr,"Error in DH routines\n");
1.426 + ret=1;
1.427 + }
1.428 + else
1.429 + ret=0;
1.430 +err:
1.431 + ERR_print_errors_fp(stderr);
1.432 + if(errno==ENOMEM)
1.433 + {
1.434 + return 1;
1.435 + }
1.436 + ERR_remove_state(0);
1.437 + if(errno==ENOMEM)
1.438 + {
1.439 + return 1;
1.440 + }
1.441 + ERR_free_strings();
1.442 + if(errno==ENOMEM)
1.443 + {
1.444 + return 1;
1.445 + }
1.446 + if (abuf != NULL) OPENSSL_free(abuf);
1.447 + if (bbuf != NULL) OPENSSL_free(bbuf);
1.448 + if(b != NULL) DH_free(b);
1.449 + if(a != NULL) DH_free(a);
1.450 + BIO_free(out);
1.451 +#ifdef SYMBIAN
1.452 + CRYPTO_cleanup_all_ex_data();
1.453 + if(errno==ENOMEM)
1.454 + {
1.455 + return 1;
1.456 + }
1.457 +
1.458 +#endif
1.459 +#ifdef OPENSSL_SYS_NETWARE
1.460 + if (ret) fprintf(stdout,"ERROR: %d\n", ret);
1.461 +#endif
1.462 + fprintf(stdout,"Test case passed\n");
1.463 + //EXIT(ret);
1.464 + return(ret);
1.465 + }
1.466 +
1.467 +static int MS_CALLBACK cb(int p, int n, BN_GENCB *arg)
1.468 + {
1.469 + char c='*';
1.470 +
1.471 + if (p == 0) c='.';
1.472 + if (p == 1) c='+';
1.473 + if (p == 2) c='*';
1.474 + if (p == 3) c='\n';
1.475 + BIO_write(arg->arg,&c,1);
1.476 + if(errno==ENOMEM)
1.477 + {
1.478 + return 1;
1.479 + }
1.480 +
1.481 + (void)BIO_flush(arg->arg);
1.482 + if(errno==ENOMEM)
1.483 + {
1.484 + return 1;
1.485 + }
1.486 +
1.487 +#ifdef LINT
1.488 + p=n;
1.489 +#endif
1.490 + return 1;
1.491 + }
1.492 +#endif