ssl/tsrc/crypto_test/src/dsatest.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* crypto/dsa/dsatest.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 /* Until the key-gen callbacks are modified to use newer prototypes, we allow
       
    62  * deprecated functions for openssl-internal code */
       
    63 #ifndef SYMBIAN
       
    64 #ifdef OPENSSL_NO_DEPRECATED
       
    65 #undef OPENSSL_NO_DEPRECATED
       
    66 #endif
       
    67 #endif
       
    68 
       
    69 #include <stdio.h>
       
    70 #include <stdlib.h>
       
    71 #include <string.h>
       
    72 #include <sys/types.h>
       
    73 #include <sys/stat.h>
       
    74 
       
    75 #ifndef SYMBIAN
       
    76 #include "../e_os.h"
       
    77 #else
       
    78 #include "e_os.h"
       
    79 #endif
       
    80 
       
    81 #include <openssl/crypto.h>
       
    82 #include <openssl/rand.h>
       
    83 #include <openssl/bio.h>
       
    84 #include <openssl/err.h>
       
    85 #include <openssl/bn.h>
       
    86 #ifdef SYMBIAN
       
    87 #ifdef stdin
       
    88 #undef stdin
       
    89 #endif
       
    90 #ifdef stdout
       
    91 #undef stdout
       
    92 #endif
       
    93 #ifdef stderr
       
    94 #undef stderr
       
    95 #endif
       
    96 
       
    97 #define stdin fp_stdin
       
    98 #define stdout fp_stdout
       
    99 #define stderr fp_stderr
       
   100 
       
   101 extern FILE *fp_stdout;
       
   102 extern FILE *fp_stderr;
       
   103 #endif
       
   104 #ifdef OPENSSL_NO_DSA
       
   105 int main(int argc, char *argv[])
       
   106 {
       
   107     fprintf(stdout,"No DSA support\n");
       
   108     return(0);
       
   109 }
       
   110 #else
       
   111 #include <openssl/dsa.h>
       
   112 
       
   113 #ifdef OPENSSL_SYS_WIN16
       
   114 #define MS_CALLBACK     _far _loadds
       
   115 #else
       
   116 #define MS_CALLBACK
       
   117 #endif
       
   118 
       
   119 static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *arg);
       
   120 
       
   121 /* seed, out_p, out_q, out_g are taken from the updated Appendix 5 to
       
   122  * FIPS PUB 186 and also appear in Appendix 5 to FIPS PIB 186-1 */
       
   123 static unsigned char seed[20]={
       
   124 	0xd5,0x01,0x4e,0x4b,0x60,0xef,0x2b,0xa8,0xb6,0x21,0x1b,0x40,
       
   125 	0x62,0xba,0x32,0x24,0xe0,0x42,0x7d,0xd3,
       
   126 	};
       
   127 
       
   128 static unsigned char out_p[]={
       
   129 	0x8d,0xf2,0xa4,0x94,0x49,0x22,0x76,0xaa,
       
   130 	0x3d,0x25,0x75,0x9b,0xb0,0x68,0x69,0xcb,
       
   131 	0xea,0xc0,0xd8,0x3a,0xfb,0x8d,0x0c,0xf7,
       
   132 	0xcb,0xb8,0x32,0x4f,0x0d,0x78,0x82,0xe5,
       
   133 	0xd0,0x76,0x2f,0xc5,0xb7,0x21,0x0e,0xaf,
       
   134 	0xc2,0xe9,0xad,0xac,0x32,0xab,0x7a,0xac,
       
   135 	0x49,0x69,0x3d,0xfb,0xf8,0x37,0x24,0xc2,
       
   136 	0xec,0x07,0x36,0xee,0x31,0xc8,0x02,0x91,
       
   137 	};
       
   138 
       
   139 static unsigned char out_q[]={
       
   140 	0xc7,0x73,0x21,0x8c,0x73,0x7e,0xc8,0xee,
       
   141 	0x99,0x3b,0x4f,0x2d,0xed,0x30,0xf4,0x8e,
       
   142 	0xda,0xce,0x91,0x5f,
       
   143 	};
       
   144 
       
   145 static unsigned char out_g[]={
       
   146 	0x62,0x6d,0x02,0x78,0x39,0xea,0x0a,0x13,
       
   147 	0x41,0x31,0x63,0xa5,0x5b,0x4c,0xb5,0x00,
       
   148 	0x29,0x9d,0x55,0x22,0x95,0x6c,0xef,0xcb,
       
   149 	0x3b,0xff,0x10,0xf3,0x99,0xce,0x2c,0x2e,
       
   150 	0x71,0xcb,0x9d,0xe5,0xfa,0x24,0xba,0xbf,
       
   151 	0x58,0xe5,0xb7,0x95,0x21,0x92,0x5c,0x9c,
       
   152 	0xc4,0x2e,0x9f,0x6f,0x46,0x4b,0x08,0x8c,
       
   153 	0xc5,0x72,0xaf,0x53,0xe6,0xd7,0x88,0x02,
       
   154 	};
       
   155 
       
   156 static const unsigned char str1[]="12345678901234567890";
       
   157 
       
   158 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
       
   159 
       
   160 static BIO *bio_err=NULL;
       
   161 #ifndef SYMBIAN
       
   162 int main(int argc, char **argv)
       
   163 #else
       
   164 int dsa_main(int argc, char **argv)
       
   165 #endif
       
   166 	{
       
   167 	BN_GENCB cb;
       
   168 	DSA *dsa=NULL;
       
   169 	int counter,ret=0,i,j;
       
   170 	unsigned char buf[256];
       
   171 	unsigned long h;
       
   172 	unsigned char sig[256];
       
   173 	unsigned int siglen;
       
   174     
       
   175 	if (bio_err == NULL)
       
   176 		bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
       
   177   if(	bio_err == NULL&& errno==ENOMEM)
       
   178   {
       
   179 	  return 1;
       
   180 	}
       
   181 	/*CRYPTO_malloc_debug_init();
       
   182 	if(	bio_err == NULL&& errno==ENOMEM)
       
   183   {
       
   184 	  return 1;
       
   185 	}
       
   186 	CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
       
   187 	if(	bio_err == NULL&& errno==ENOMEM)
       
   188   {
       
   189 	  return 1;
       
   190 	}
       
   191 	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
       
   192 	if(	bio_err == NULL&& errno==ENOMEM)
       
   193   {
       
   194 	  return 1;
       
   195 	}
       
   196 */
       
   197 	ERR_load_crypto_strings();
       
   198 	RAND_seed(rnd_seed, sizeof rnd_seed);
       
   199 	if(errno==ENOMEM)
       
   200   {
       
   201 	  return 1;
       
   202 	}
       
   203 
       
   204 	BIO_printf(bio_err,"test generation of DSA parameters\n");
       
   205 	if(errno==ENOMEM)
       
   206   {
       
   207 	  return 1;
       
   208 	}
       
   209 
       
   210 	BN_GENCB_set(&cb, dsa_cb, bio_err);
       
   211 	if(errno==ENOMEM)
       
   212   {
       
   213 	  return 1;
       
   214 	}
       
   215 	if(((dsa = DSA_new()) == NULL))
       
   216   {				
       
   217 	  if(errno==ENOMEM)
       
   218    	{
       
   219 	    return 1;
       
   220 	  }
       
   221 		
       
   222   }
       
   223   if(!DSA_generate_parameters_ex(dsa, 512,
       
   224 				seed, 20, &counter, &h, &cb))
       
   225 	{			
       
   226     if(errno==ENOMEM)
       
   227    	{
       
   228    		if (dsa != NULL)
       
   229 	   {
       
   230 		 DSA_free(dsa);
       
   231 		
       
   232 	   }	 
       
   233 	    return 1;
       
   234 	  }
       
   235 		goto end;
       
   236   } 
       
   237 	BIO_printf(bio_err,"seed\n");
       
   238 	if(errno==ENOMEM)
       
   239   {
       
   240 	  return 1;
       
   241 	}
       
   242 	for (i=0; i<20; i+=4)
       
   243 		{
       
   244 		BIO_printf(bio_err,"%02X%02X%02X%02X ",
       
   245 			seed[i],seed[i+1],seed[i+2],seed[i+3]);
       
   246     if(errno==ENOMEM)
       
   247     {
       
   248 	    return 1;
       
   249 	  }
       
   250 			
       
   251 		}
       
   252 	BIO_printf(bio_err,"\ncounter=%d h=%ld\n",counter,h);
       
   253 	if(errno==ENOMEM)
       
   254   {
       
   255 	  return 1;
       
   256 	}
       
   257 
       
   258 		
       
   259 	if (dsa == NULL) goto end;
       
   260 	DSA_print(bio_err,dsa,0);
       
   261 	if(errno==ENOMEM)
       
   262   {
       
   263 	  return 1;
       
   264 	}
       
   265 
       
   266 	if (counter != 105) 
       
   267 		{
       
   268 		BIO_printf(bio_err,"counter should be 105\n");
       
   269 		if(errno==ENOMEM)
       
   270     {
       
   271 	  return 1;
       
   272 	  }
       
   273 		goto end;
       
   274 		}
       
   275 	if (h != 2)
       
   276 		{
       
   277 		BIO_printf(bio_err,"h should be 2\n");
       
   278 		if(errno==ENOMEM)
       
   279     {
       
   280 	  return 1;
       
   281 	  }
       
   282   	goto end;
       
   283 		}
       
   284 
       
   285 	i=BN_bn2bin(dsa->q,buf);
       
   286 	if(errno==ENOMEM)
       
   287   {
       
   288 	  return 1;
       
   289 	}
       
   290 
       
   291 	j=sizeof(out_q);
       
   292 	if ((i != j) || (memcmp(buf,out_q,i) != 0))
       
   293 	{
       
   294   	if(errno==ENOMEM)
       
   295       {
       
   296 	      return 1;
       
   297 	    }
       
   298 		BIO_printf(bio_err,"q value is wrong\n");
       
   299 		if(errno==ENOMEM)
       
   300     {
       
   301 	    return 1;
       
   302 	  }
       
   303 		goto end;
       
   304 	}
       
   305 
       
   306 	i=BN_bn2bin(dsa->p,buf);
       
   307 	if(errno==ENOMEM)
       
   308   {
       
   309 	  return 1;
       
   310 	}
       
   311 	j=sizeof(out_p);
       
   312 	if ((i != j) || (memcmp(buf,out_p,i) != 0))
       
   313 		{
       
   314 		if(errno==ENOMEM)
       
   315     {
       
   316 	    return 1;
       
   317 	  }
       
   318 		BIO_printf(bio_err,"p value is wrong\n");
       
   319 		if(errno==ENOMEM)
       
   320     {
       
   321 	   return 1;
       
   322 	  }
       
   323 		goto end;
       
   324 		}
       
   325 
       
   326 	i=BN_bn2bin(dsa->g,buf);
       
   327 	if(errno==ENOMEM)
       
   328   {
       
   329 	  return 1;
       
   330 	}
       
   331 
       
   332 	j=sizeof(out_g);
       
   333 	if ((i != j) || (memcmp(buf,out_g,i) != 0))
       
   334 		{
       
   335 		if(errno==ENOMEM)
       
   336     {
       
   337 	    return 1;
       
   338 	  }
       
   339 		BIO_printf(bio_err,"g value is wrong\n");
       
   340 		if(errno==ENOMEM)
       
   341     {
       
   342 	    return 1;
       
   343 	  }
       
   344 		goto end;
       
   345 		}
       
   346 
       
   347 	dsa->flags |= DSA_FLAG_NO_EXP_CONSTTIME;
       
   348 	DSA_generate_key(dsa);
       
   349 	if(errno==ENOMEM)
       
   350   {
       
   351 	  return 1;
       
   352 	}
       
   353 	DSA_sign(0, str1, 20, sig, &siglen, dsa);
       
   354 	if(errno==ENOMEM)
       
   355   {
       
   356 	  return 1;
       
   357 	}
       
   358 	if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1)
       
   359 		ret=1;
       
   360   if(errno==ENOMEM)
       
   361   {
       
   362 	  return 1;
       
   363 	}
       
   364 
       
   365 	dsa->flags &= ~DSA_FLAG_NO_EXP_CONSTTIME;
       
   366 	DSA_generate_key(dsa);
       
   367 	if(errno==ENOMEM)
       
   368   {
       
   369 	  return 1;
       
   370 	}
       
   371 
       
   372 	DSA_sign(0, str1, 20, sig, &siglen, dsa);
       
   373 	if(errno==ENOMEM)
       
   374   {
       
   375 	  return 1;
       
   376 	}
       
   377 
       
   378 	if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1)
       
   379 		ret=1;
       
   380   if(errno==ENOMEM)
       
   381   {
       
   382 	  return 1;
       
   383 	}
       
   384 
       
   385 end:
       
   386 	if (!ret)
       
   387 	{
       
   388 		ERR_print_errors(bio_err);
       
   389 		if(errno==ENOMEM)
       
   390     {
       
   391 	    return 1;
       
   392 	  }
       
   393 
       
   394 	}	
       
   395 	if (dsa != NULL)
       
   396 	{
       
   397 		 DSA_free(dsa);
       
   398 		 if(errno==ENOMEM)
       
   399      {
       
   400 	    return 1;
       
   401 	   }
       
   402 	}	 
       
   403 	
       
   404 	CRYPTO_mem_leaks(bio_err);
       
   405 	if(errno==ENOMEM)
       
   406   {
       
   407 	  return 1;
       
   408 	}
       
   409 	if (bio_err != NULL)
       
   410 		{
       
   411 		BIO_free(bio_err);
       
   412 		bio_err = NULL;
       
   413 		if(errno==ENOMEM)
       
   414     {
       
   415 	   return 1;
       
   416 	  }
       
   417    }
       
   418   	
       
   419 #ifdef SYMBIAN	
       
   420 	CRYPTO_cleanup_all_ex_data();
       
   421 		if(errno==ENOMEM)
       
   422   {
       
   423 	  return 1;
       
   424 	}
       
   425 
       
   426 #endif
       
   427 		 
       
   428 	ERR_remove_state(0);
       
   429 	if(errno==ENOMEM)
       
   430   {
       
   431 	  return 1;
       
   432 	}
       
   433 	ERR_free_strings();
       
   434 	if(errno==ENOMEM)
       
   435   {
       
   436 	  return 1;
       
   437 	}
       
   438 
       
   439 #ifdef OPENSSL_SYS_NETWARE
       
   440     if (!ret) fprintf(stdout,"ERROR\n");
       
   441 #endif
       
   442   fprintf(stdout,"Test Case Passed!\n");
       
   443   fprintf(stderr,"Test Case Passed!\n");
       
   444 	//EXIT(!ret);
       
   445 	return(0);
       
   446 	}
       
   447 
       
   448 static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *arg)
       
   449 	{
       
   450 	char c='*';
       
   451 	static int ok=0,num=0;
       
   452 
       
   453 	if (p == 0) { c='.'; num++; };
       
   454 	if (p == 1) c='+';
       
   455 	if (p == 2) { c='*'; ok++; }
       
   456 	if (p == 3) c='\n';
       
   457 	BIO_write(arg->arg,&c,1);
       
   458 	if(errno==ENOMEM)
       
   459   {
       
   460 	    return 0;
       
   461 	}  
       
   462 	(void)BIO_flush(arg->arg);
       
   463   if(errno==ENOMEM)
       
   464   {
       
   465 	    return 0;
       
   466 	}  
       
   467 	if (!ok && (p == 0) && (num > 1))
       
   468 		{
       
   469 		BIO_printf((BIO *)arg,"error in dsatest\n");
       
   470 		return 0;
       
   471 		}
       
   472 	return 1;
       
   473 	}
       
   474 #endif