1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/tsrc/crypto_test/src/dsatest.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,474 @@
1.4 +/* crypto/dsa/dsatest.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 +
1.72 +#include <stdio.h>
1.73 +#include <stdlib.h>
1.74 +#include <string.h>
1.75 +#include <sys/types.h>
1.76 +#include <sys/stat.h>
1.77 +
1.78 +#ifndef SYMBIAN
1.79 +#include "../e_os.h"
1.80 +#else
1.81 +#include "e_os.h"
1.82 +#endif
1.83 +
1.84 +#include <openssl/crypto.h>
1.85 +#include <openssl/rand.h>
1.86 +#include <openssl/bio.h>
1.87 +#include <openssl/err.h>
1.88 +#include <openssl/bn.h>
1.89 +#ifdef SYMBIAN
1.90 +#ifdef stdin
1.91 +#undef stdin
1.92 +#endif
1.93 +#ifdef stdout
1.94 +#undef stdout
1.95 +#endif
1.96 +#ifdef stderr
1.97 +#undef stderr
1.98 +#endif
1.99 +
1.100 +#define stdin fp_stdin
1.101 +#define stdout fp_stdout
1.102 +#define stderr fp_stderr
1.103 +
1.104 +extern FILE *fp_stdout;
1.105 +extern FILE *fp_stderr;
1.106 +#endif
1.107 +#ifdef OPENSSL_NO_DSA
1.108 +int main(int argc, char *argv[])
1.109 +{
1.110 + fprintf(stdout,"No DSA support\n");
1.111 + return(0);
1.112 +}
1.113 +#else
1.114 +#include <openssl/dsa.h>
1.115 +
1.116 +#ifdef OPENSSL_SYS_WIN16
1.117 +#define MS_CALLBACK _far _loadds
1.118 +#else
1.119 +#define MS_CALLBACK
1.120 +#endif
1.121 +
1.122 +static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *arg);
1.123 +
1.124 +/* seed, out_p, out_q, out_g are taken from the updated Appendix 5 to
1.125 + * FIPS PUB 186 and also appear in Appendix 5 to FIPS PIB 186-1 */
1.126 +static unsigned char seed[20]={
1.127 + 0xd5,0x01,0x4e,0x4b,0x60,0xef,0x2b,0xa8,0xb6,0x21,0x1b,0x40,
1.128 + 0x62,0xba,0x32,0x24,0xe0,0x42,0x7d,0xd3,
1.129 + };
1.130 +
1.131 +static unsigned char out_p[]={
1.132 + 0x8d,0xf2,0xa4,0x94,0x49,0x22,0x76,0xaa,
1.133 + 0x3d,0x25,0x75,0x9b,0xb0,0x68,0x69,0xcb,
1.134 + 0xea,0xc0,0xd8,0x3a,0xfb,0x8d,0x0c,0xf7,
1.135 + 0xcb,0xb8,0x32,0x4f,0x0d,0x78,0x82,0xe5,
1.136 + 0xd0,0x76,0x2f,0xc5,0xb7,0x21,0x0e,0xaf,
1.137 + 0xc2,0xe9,0xad,0xac,0x32,0xab,0x7a,0xac,
1.138 + 0x49,0x69,0x3d,0xfb,0xf8,0x37,0x24,0xc2,
1.139 + 0xec,0x07,0x36,0xee,0x31,0xc8,0x02,0x91,
1.140 + };
1.141 +
1.142 +static unsigned char out_q[]={
1.143 + 0xc7,0x73,0x21,0x8c,0x73,0x7e,0xc8,0xee,
1.144 + 0x99,0x3b,0x4f,0x2d,0xed,0x30,0xf4,0x8e,
1.145 + 0xda,0xce,0x91,0x5f,
1.146 + };
1.147 +
1.148 +static unsigned char out_g[]={
1.149 + 0x62,0x6d,0x02,0x78,0x39,0xea,0x0a,0x13,
1.150 + 0x41,0x31,0x63,0xa5,0x5b,0x4c,0xb5,0x00,
1.151 + 0x29,0x9d,0x55,0x22,0x95,0x6c,0xef,0xcb,
1.152 + 0x3b,0xff,0x10,0xf3,0x99,0xce,0x2c,0x2e,
1.153 + 0x71,0xcb,0x9d,0xe5,0xfa,0x24,0xba,0xbf,
1.154 + 0x58,0xe5,0xb7,0x95,0x21,0x92,0x5c,0x9c,
1.155 + 0xc4,0x2e,0x9f,0x6f,0x46,0x4b,0x08,0x8c,
1.156 + 0xc5,0x72,0xaf,0x53,0xe6,0xd7,0x88,0x02,
1.157 + };
1.158 +
1.159 +static const unsigned char str1[]="12345678901234567890";
1.160 +
1.161 +static const char rnd_seed[] = "string to make the random number generator think it has entropy";
1.162 +
1.163 +static BIO *bio_err=NULL;
1.164 +#ifndef SYMBIAN
1.165 +int main(int argc, char **argv)
1.166 +#else
1.167 +int dsa_main(int argc, char **argv)
1.168 +#endif
1.169 + {
1.170 + BN_GENCB cb;
1.171 + DSA *dsa=NULL;
1.172 + int counter,ret=0,i,j;
1.173 + unsigned char buf[256];
1.174 + unsigned long h;
1.175 + unsigned char sig[256];
1.176 + unsigned int siglen;
1.177 +
1.178 + if (bio_err == NULL)
1.179 + bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
1.180 + if( bio_err == NULL&& errno==ENOMEM)
1.181 + {
1.182 + return 1;
1.183 + }
1.184 + /*CRYPTO_malloc_debug_init();
1.185 + if( bio_err == NULL&& errno==ENOMEM)
1.186 + {
1.187 + return 1;
1.188 + }
1.189 + CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
1.190 + if( bio_err == NULL&& errno==ENOMEM)
1.191 + {
1.192 + return 1;
1.193 + }
1.194 + CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
1.195 + if( bio_err == NULL&& errno==ENOMEM)
1.196 + {
1.197 + return 1;
1.198 + }
1.199 +*/
1.200 + ERR_load_crypto_strings();
1.201 + RAND_seed(rnd_seed, sizeof rnd_seed);
1.202 + if(errno==ENOMEM)
1.203 + {
1.204 + return 1;
1.205 + }
1.206 +
1.207 + BIO_printf(bio_err,"test generation of DSA parameters\n");
1.208 + if(errno==ENOMEM)
1.209 + {
1.210 + return 1;
1.211 + }
1.212 +
1.213 + BN_GENCB_set(&cb, dsa_cb, bio_err);
1.214 + if(errno==ENOMEM)
1.215 + {
1.216 + return 1;
1.217 + }
1.218 + if(((dsa = DSA_new()) == NULL))
1.219 + {
1.220 + if(errno==ENOMEM)
1.221 + {
1.222 + return 1;
1.223 + }
1.224 +
1.225 + }
1.226 + if(!DSA_generate_parameters_ex(dsa, 512,
1.227 + seed, 20, &counter, &h, &cb))
1.228 + {
1.229 + if(errno==ENOMEM)
1.230 + {
1.231 + if (dsa != NULL)
1.232 + {
1.233 + DSA_free(dsa);
1.234 +
1.235 + }
1.236 + return 1;
1.237 + }
1.238 + goto end;
1.239 + }
1.240 + BIO_printf(bio_err,"seed\n");
1.241 + if(errno==ENOMEM)
1.242 + {
1.243 + return 1;
1.244 + }
1.245 + for (i=0; i<20; i+=4)
1.246 + {
1.247 + BIO_printf(bio_err,"%02X%02X%02X%02X ",
1.248 + seed[i],seed[i+1],seed[i+2],seed[i+3]);
1.249 + if(errno==ENOMEM)
1.250 + {
1.251 + return 1;
1.252 + }
1.253 +
1.254 + }
1.255 + BIO_printf(bio_err,"\ncounter=%d h=%ld\n",counter,h);
1.256 + if(errno==ENOMEM)
1.257 + {
1.258 + return 1;
1.259 + }
1.260 +
1.261 +
1.262 + if (dsa == NULL) goto end;
1.263 + DSA_print(bio_err,dsa,0);
1.264 + if(errno==ENOMEM)
1.265 + {
1.266 + return 1;
1.267 + }
1.268 +
1.269 + if (counter != 105)
1.270 + {
1.271 + BIO_printf(bio_err,"counter should be 105\n");
1.272 + if(errno==ENOMEM)
1.273 + {
1.274 + return 1;
1.275 + }
1.276 + goto end;
1.277 + }
1.278 + if (h != 2)
1.279 + {
1.280 + BIO_printf(bio_err,"h should be 2\n");
1.281 + if(errno==ENOMEM)
1.282 + {
1.283 + return 1;
1.284 + }
1.285 + goto end;
1.286 + }
1.287 +
1.288 + i=BN_bn2bin(dsa->q,buf);
1.289 + if(errno==ENOMEM)
1.290 + {
1.291 + return 1;
1.292 + }
1.293 +
1.294 + j=sizeof(out_q);
1.295 + if ((i != j) || (memcmp(buf,out_q,i) != 0))
1.296 + {
1.297 + if(errno==ENOMEM)
1.298 + {
1.299 + return 1;
1.300 + }
1.301 + BIO_printf(bio_err,"q value is wrong\n");
1.302 + if(errno==ENOMEM)
1.303 + {
1.304 + return 1;
1.305 + }
1.306 + goto end;
1.307 + }
1.308 +
1.309 + i=BN_bn2bin(dsa->p,buf);
1.310 + if(errno==ENOMEM)
1.311 + {
1.312 + return 1;
1.313 + }
1.314 + j=sizeof(out_p);
1.315 + if ((i != j) || (memcmp(buf,out_p,i) != 0))
1.316 + {
1.317 + if(errno==ENOMEM)
1.318 + {
1.319 + return 1;
1.320 + }
1.321 + BIO_printf(bio_err,"p value is wrong\n");
1.322 + if(errno==ENOMEM)
1.323 + {
1.324 + return 1;
1.325 + }
1.326 + goto end;
1.327 + }
1.328 +
1.329 + i=BN_bn2bin(dsa->g,buf);
1.330 + if(errno==ENOMEM)
1.331 + {
1.332 + return 1;
1.333 + }
1.334 +
1.335 + j=sizeof(out_g);
1.336 + if ((i != j) || (memcmp(buf,out_g,i) != 0))
1.337 + {
1.338 + if(errno==ENOMEM)
1.339 + {
1.340 + return 1;
1.341 + }
1.342 + BIO_printf(bio_err,"g value is wrong\n");
1.343 + if(errno==ENOMEM)
1.344 + {
1.345 + return 1;
1.346 + }
1.347 + goto end;
1.348 + }
1.349 +
1.350 + dsa->flags |= DSA_FLAG_NO_EXP_CONSTTIME;
1.351 + DSA_generate_key(dsa);
1.352 + if(errno==ENOMEM)
1.353 + {
1.354 + return 1;
1.355 + }
1.356 + DSA_sign(0, str1, 20, sig, &siglen, dsa);
1.357 + if(errno==ENOMEM)
1.358 + {
1.359 + return 1;
1.360 + }
1.361 + if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1)
1.362 + ret=1;
1.363 + if(errno==ENOMEM)
1.364 + {
1.365 + return 1;
1.366 + }
1.367 +
1.368 + dsa->flags &= ~DSA_FLAG_NO_EXP_CONSTTIME;
1.369 + DSA_generate_key(dsa);
1.370 + if(errno==ENOMEM)
1.371 + {
1.372 + return 1;
1.373 + }
1.374 +
1.375 + DSA_sign(0, str1, 20, sig, &siglen, dsa);
1.376 + if(errno==ENOMEM)
1.377 + {
1.378 + return 1;
1.379 + }
1.380 +
1.381 + if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1)
1.382 + ret=1;
1.383 + if(errno==ENOMEM)
1.384 + {
1.385 + return 1;
1.386 + }
1.387 +
1.388 +end:
1.389 + if (!ret)
1.390 + {
1.391 + ERR_print_errors(bio_err);
1.392 + if(errno==ENOMEM)
1.393 + {
1.394 + return 1;
1.395 + }
1.396 +
1.397 + }
1.398 + if (dsa != NULL)
1.399 + {
1.400 + DSA_free(dsa);
1.401 + if(errno==ENOMEM)
1.402 + {
1.403 + return 1;
1.404 + }
1.405 + }
1.406 +
1.407 + CRYPTO_mem_leaks(bio_err);
1.408 + if(errno==ENOMEM)
1.409 + {
1.410 + return 1;
1.411 + }
1.412 + if (bio_err != NULL)
1.413 + {
1.414 + BIO_free(bio_err);
1.415 + bio_err = NULL;
1.416 + if(errno==ENOMEM)
1.417 + {
1.418 + return 1;
1.419 + }
1.420 + }
1.421 +
1.422 +#ifdef SYMBIAN
1.423 + CRYPTO_cleanup_all_ex_data();
1.424 + if(errno==ENOMEM)
1.425 + {
1.426 + return 1;
1.427 + }
1.428 +
1.429 +#endif
1.430 +
1.431 + ERR_remove_state(0);
1.432 + if(errno==ENOMEM)
1.433 + {
1.434 + return 1;
1.435 + }
1.436 + ERR_free_strings();
1.437 + if(errno==ENOMEM)
1.438 + {
1.439 + return 1;
1.440 + }
1.441 +
1.442 +#ifdef OPENSSL_SYS_NETWARE
1.443 + if (!ret) fprintf(stdout,"ERROR\n");
1.444 +#endif
1.445 + fprintf(stdout,"Test Case Passed!\n");
1.446 + fprintf(stderr,"Test Case Passed!\n");
1.447 + //EXIT(!ret);
1.448 + return(0);
1.449 + }
1.450 +
1.451 +static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *arg)
1.452 + {
1.453 + char c='*';
1.454 + static int ok=0,num=0;
1.455 +
1.456 + if (p == 0) { c='.'; num++; };
1.457 + if (p == 1) c='+';
1.458 + if (p == 2) { c='*'; ok++; }
1.459 + if (p == 3) c='\n';
1.460 + BIO_write(arg->arg,&c,1);
1.461 + if(errno==ENOMEM)
1.462 + {
1.463 + return 0;
1.464 + }
1.465 + (void)BIO_flush(arg->arg);
1.466 + if(errno==ENOMEM)
1.467 + {
1.468 + return 0;
1.469 + }
1.470 + if (!ok && (p == 0) && (num > 1))
1.471 + {
1.472 + BIO_printf((BIO *)arg,"error in dsatest\n");
1.473 + return 0;
1.474 + }
1.475 + return 1;
1.476 + }
1.477 +#endif