os/ossrv/ssl/tsrc/crypto_test/src/dhtest.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/dh/dhtest.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
/*
sl@0
    59
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    60
 */
sl@0
    61
/* Until the key-gen callbacks are modified to use newer prototypes, we allow
sl@0
    62
 * deprecated functions for openssl-internal code */
sl@0
    63
 #ifndef SYMBIAN
sl@0
    64
#ifdef OPENSSL_NO_DEPRECATED
sl@0
    65
#undef OPENSSL_NO_DEPRECATED
sl@0
    66
#endif
sl@0
    67
#endif
sl@0
    68
#include <stdio.h>
sl@0
    69
#include <stdlib.h>
sl@0
    70
#include <string.h>
sl@0
    71
#ifndef SYMBIAN
sl@0
    72
#include "../e_os.h"
sl@0
    73
#else
sl@0
    74
#include "e_os.h"
sl@0
    75
#endif
sl@0
    76
sl@0
    77
#include <openssl/crypto.h>
sl@0
    78
#include <openssl/bio.h>
sl@0
    79
#include <openssl/bn.h>
sl@0
    80
#include <openssl/rand.h>
sl@0
    81
#include <openssl/err.h>
sl@0
    82
sl@0
    83
#ifdef SYMBIAN
sl@0
    84
#ifdef stdin
sl@0
    85
#undef stdin
sl@0
    86
#endif
sl@0
    87
#ifdef stdout
sl@0
    88
#undef stdout
sl@0
    89
#endif
sl@0
    90
#ifdef stderr
sl@0
    91
#undef stderr
sl@0
    92
#endif
sl@0
    93
sl@0
    94
#define stdin fp_stdin
sl@0
    95
#define stdout fp_stdout
sl@0
    96
#define stderr fp_stderr
sl@0
    97
sl@0
    98
extern FILE *fp_stdout;
sl@0
    99
extern FILE *fp_stderr;
sl@0
   100
#endif
sl@0
   101
#ifdef OPENSSL_NO_DH
sl@0
   102
int main(int argc, char *argv[])
sl@0
   103
{
sl@0
   104
    fprintf(stdout,"No DH support\n");
sl@0
   105
    return(0);
sl@0
   106
}
sl@0
   107
#else
sl@0
   108
#include <openssl/dh.h>
sl@0
   109
sl@0
   110
#ifdef OPENSSL_SYS_WIN16
sl@0
   111
#define MS_CALLBACK	_far _loadds
sl@0
   112
#else
sl@0
   113
#define MS_CALLBACK
sl@0
   114
#endif
sl@0
   115
sl@0
   116
static int MS_CALLBACK cb(int p, int n, BN_GENCB *arg);
sl@0
   117
sl@0
   118
static const char rnd_seed[] = "string to make the random number generator think it has entropy";
sl@0
   119
sl@0
   120
#ifndef SYMBIAN
sl@0
   121
int main(int argc, char *argv[])
sl@0
   122
#else
sl@0
   123
int dh_main(int argc, char *argv[])
sl@0
   124
#endif
sl@0
   125
	{
sl@0
   126
	BN_GENCB _cb;
sl@0
   127
	DH *a;
sl@0
   128
	DH *b=NULL;
sl@0
   129
	char buf[12];
sl@0
   130
	unsigned char *abuf=NULL,*bbuf=NULL;
sl@0
   131
	int i,alen,blen,aout,bout,ret=1;
sl@0
   132
	BIO *out;
sl@0
   133
    
sl@0
   134
	//CRYPTO_malloc_debug_init();
sl@0
   135
	//CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
sl@0
   136
	//CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
sl@0
   137
sl@0
   138
#ifdef OPENSSL_SYS_WIN32
sl@0
   139
	CRYPTO_malloc_init();
sl@0
   140
#endif
sl@0
   141
sl@0
   142
	RAND_seed(rnd_seed, sizeof rnd_seed);
sl@0
   143
	if(errno==ENOMEM)
sl@0
   144
   	{
sl@0
   145
	  return 1;
sl@0
   146
	}
sl@0
   147
	out=BIO_new(BIO_s_file());
sl@0
   148
	if(	out == NULL&& errno==ENOMEM)
sl@0
   149
   	{
sl@0
   150
	  return 1;
sl@0
   151
	}
sl@0
   152
	if (out == NULL)
sl@0
   153
	return 1;
sl@0
   154
	//EXIT(1);
sl@0
   155
	BIO_set_fp(out,stdout,BIO_NOCLOSE);
sl@0
   156
	if(errno==ENOMEM)
sl@0
   157
   	{
sl@0
   158
	  return 1;
sl@0
   159
	}
sl@0
   160
	
sl@0
   161
	BN_GENCB_set(&_cb, &cb, out);
sl@0
   162
	if(errno==ENOMEM)
sl@0
   163
   	{
sl@0
   164
	  return 1;
sl@0
   165
	}
sl@0
   166
	if(((a = DH_new()) == NULL)) 
sl@0
   167
	{
sl@0
   168
		if(errno==ENOMEM)
sl@0
   169
   	   {
sl@0
   170
	    return 1;
sl@0
   171
	   }
sl@0
   172
		goto err;	
sl@0
   173
	}
sl@0
   174
	if( !DH_generate_parameters_ex(a, 64,
sl@0
   175
				DH_GENERATOR_5, &_cb))
sl@0
   176
 {
sl@0
   177
 	  if(a != NULL) DH_free(a);
sl@0
   178
 		if(errno==ENOMEM)
sl@0
   179
   	 {
sl@0
   180
	    return 1;
sl@0
   181
	   }
sl@0
   182
sl@0
   183
 }	
sl@0
   184
	if (!DH_check(a, &i)) 
sl@0
   185
	{
sl@0
   186
		if(errno==ENOMEM)
sl@0
   187
  	   {
sl@0
   188
	    return 1;
sl@0
   189
	   }
sl@0
   190
		goto err;	
sl@0
   191
	}
sl@0
   192
	if (i & DH_CHECK_P_NOT_PRIME)
sl@0
   193
		BIO_puts(out, "p value is not prime\n");
sl@0
   194
	if(errno==ENOMEM)
sl@0
   195
   	{
sl@0
   196
	  return 1;
sl@0
   197
	}
sl@0
   198
	
sl@0
   199
	if (i & DH_CHECK_P_NOT_SAFE_PRIME)
sl@0
   200
		BIO_puts(out, "p value is not a safe prime\n");
sl@0
   201
	if(errno==ENOMEM)
sl@0
   202
   	{
sl@0
   203
	  return 1;
sl@0
   204
	}
sl@0
   205
	
sl@0
   206
	if (i & DH_UNABLE_TO_CHECK_GENERATOR)
sl@0
   207
		BIO_puts(out, "unable to check the generator value\n");
sl@0
   208
	if(errno==ENOMEM)
sl@0
   209
   	{
sl@0
   210
	  return 1;
sl@0
   211
	}
sl@0
   212
	
sl@0
   213
	if (i & DH_NOT_SUITABLE_GENERATOR)
sl@0
   214
		BIO_puts(out, "the g value is not a generator\n");
sl@0
   215
    if(errno==ENOMEM)
sl@0
   216
   	{
sl@0
   217
	  return 1;
sl@0
   218
	}
sl@0
   219
	
sl@0
   220
	BIO_puts(out,"\np    =");
sl@0
   221
	if(errno==ENOMEM)
sl@0
   222
   	{
sl@0
   223
	  return 1;
sl@0
   224
	}
sl@0
   225
	
sl@0
   226
	BN_print(out,a->p);
sl@0
   227
	if(errno==ENOMEM)
sl@0
   228
   	{
sl@0
   229
	  return 1;
sl@0
   230
	}
sl@0
   231
	
sl@0
   232
	BIO_puts(out,"\ng    =");
sl@0
   233
	if(errno==ENOMEM)
sl@0
   234
   	{
sl@0
   235
	  return 1;
sl@0
   236
	}
sl@0
   237
	
sl@0
   238
	BN_print(out,a->g);
sl@0
   239
	if(errno==ENOMEM)
sl@0
   240
   	{
sl@0
   241
	  return 1;
sl@0
   242
	}
sl@0
   243
	
sl@0
   244
	BIO_puts(out,"\n");
sl@0
   245
	if(errno==ENOMEM)
sl@0
   246
   	{
sl@0
   247
	  return 1;
sl@0
   248
	}
sl@0
   249
	
sl@0
   250
	b=DH_new();
sl@0
   251
	if(	b == NULL&& errno==ENOMEM)
sl@0
   252
   	{
sl@0
   253
	  return 1;
sl@0
   254
	}
sl@0
   255
	if (b == NULL) goto err;
sl@0
   256
sl@0
   257
	b->p=BN_dup(a->p);
sl@0
   258
	if(errno==ENOMEM)
sl@0
   259
   	{
sl@0
   260
	  return 1;
sl@0
   261
	}
sl@0
   262
	b->g=BN_dup(a->g);
sl@0
   263
	if(errno==ENOMEM)
sl@0
   264
   	{
sl@0
   265
	  return 1;
sl@0
   266
	}
sl@0
   267
	if ((b->p == NULL) || (b->g == NULL))
sl@0
   268
	{
sl@0
   269
		if(errno==ENOMEM)
sl@0
   270
   	   {
sl@0
   271
	     return 1;
sl@0
   272
	   }
sl@0
   273
	   goto err;
sl@0
   274
	}
sl@0
   275
sl@0
   276
	/* Set a to run with normal modexp and b to use constant time */
sl@0
   277
	a->flags &= ~DH_FLAG_NO_EXP_CONSTTIME;
sl@0
   278
	b->flags |= DH_FLAG_NO_EXP_CONSTTIME;
sl@0
   279
sl@0
   280
	if (!DH_generate_key(a))
sl@0
   281
	{
sl@0
   282
	   if(errno==ENOMEM)
sl@0
   283
   	   {
sl@0
   284
	     return 1;
sl@0
   285
	   }
sl@0
   286
	   goto err;	
sl@0
   287
	}
sl@0
   288
	BIO_puts(out,"pri 1=");
sl@0
   289
	if(errno==ENOMEM)
sl@0
   290
   	{
sl@0
   291
	  return 1;
sl@0
   292
	}
sl@0
   293
	BN_print(out,a->priv_key);
sl@0
   294
	if(errno==ENOMEM)
sl@0
   295
   	{
sl@0
   296
	  return 1;
sl@0
   297
	}
sl@0
   298
	BIO_puts(out,"\npub 1=");
sl@0
   299
	if(errno==ENOMEM)
sl@0
   300
   	{
sl@0
   301
	  return 1;
sl@0
   302
	}
sl@0
   303
	BN_print(out,a->pub_key);
sl@0
   304
	if(errno==ENOMEM)
sl@0
   305
   	{
sl@0
   306
	  return 1;
sl@0
   307
	}
sl@0
   308
	BIO_puts(out,"\n");
sl@0
   309
    if(errno==ENOMEM)
sl@0
   310
   	{
sl@0
   311
	  return 1;
sl@0
   312
	}
sl@0
   313
	if (!DH_generate_key(b))
sl@0
   314
	{
sl@0
   315
	  if(errno==ENOMEM)
sl@0
   316
   	{
sl@0
   317
	  return 1;
sl@0
   318
	}
sl@0
   319
	  goto err;	
sl@0
   320
	}
sl@0
   321
	BIO_puts(out,"pri 2=");
sl@0
   322
	if(errno==ENOMEM)
sl@0
   323
   	{
sl@0
   324
	  return 1;
sl@0
   325
	}
sl@0
   326
	BN_print(out,b->priv_key);
sl@0
   327
	if(errno==ENOMEM)
sl@0
   328
   	{
sl@0
   329
	  return 1;
sl@0
   330
	}
sl@0
   331
	BIO_puts(out,"\npub 2=");
sl@0
   332
	if(errno==ENOMEM)
sl@0
   333
   	{
sl@0
   334
	  return 1;
sl@0
   335
	}
sl@0
   336
	BN_print(out,b->pub_key);
sl@0
   337
	if(errno==ENOMEM)
sl@0
   338
   	{
sl@0
   339
	  return 1;
sl@0
   340
	}
sl@0
   341
	BIO_puts(out,"\n");
sl@0
   342
	if(errno==ENOMEM)
sl@0
   343
   	{
sl@0
   344
	  return 1;
sl@0
   345
	}
sl@0
   346
sl@0
   347
	alen=DH_size(a);
sl@0
   348
	 if(errno==ENOMEM)
sl@0
   349
   	{
sl@0
   350
	  return 1;
sl@0
   351
	}
sl@0
   352
	abuf=(unsigned char *)OPENSSL_malloc(alen);
sl@0
   353
	 if(abuf== NULL&& errno==ENOMEM)
sl@0
   354
   	{
sl@0
   355
	  return 1;
sl@0
   356
	}
sl@0
   357
	aout=DH_compute_key(abuf,b->pub_key,a);
sl@0
   358
    if(errno==ENOMEM)
sl@0
   359
   	{
sl@0
   360
	  return 1;
sl@0
   361
	}
sl@0
   362
	BIO_puts(out,"key1 =");
sl@0
   363
	if(errno==ENOMEM)
sl@0
   364
   	{
sl@0
   365
	  return 1;
sl@0
   366
	}
sl@0
   367
	for (i=0; i<aout; i++)
sl@0
   368
		{
sl@0
   369
		sprintf(buf,"%02X",abuf[i]);
sl@0
   370
		BIO_puts(out,buf);
sl@0
   371
		if(errno==ENOMEM)
sl@0
   372
   	    {
sl@0
   373
	      return 1;
sl@0
   374
	     }
sl@0
   375
		}
sl@0
   376
	BIO_puts(out,"\n");
sl@0
   377
	if(errno==ENOMEM)
sl@0
   378
   	{
sl@0
   379
	  return 1;
sl@0
   380
	}
sl@0
   381
sl@0
   382
	blen=DH_size(b);
sl@0
   383
	if(errno==ENOMEM)
sl@0
   384
   	{
sl@0
   385
	  return 1;
sl@0
   386
	}
sl@0
   387
	bbuf=(unsigned char *)OPENSSL_malloc(blen);
sl@0
   388
	if(bbuf== NULL&& errno==ENOMEM)
sl@0
   389
   	{
sl@0
   390
	  return 1;
sl@0
   391
	}
sl@0
   392
	bout=DH_compute_key(bbuf,a->pub_key,b);
sl@0
   393
    if(errno==ENOMEM)
sl@0
   394
   	{
sl@0
   395
	  return 1;
sl@0
   396
	}
sl@0
   397
	
sl@0
   398
	BIO_puts(out,"key2 =");
sl@0
   399
	if(errno==ENOMEM)
sl@0
   400
   	{
sl@0
   401
	  return 1;
sl@0
   402
	}
sl@0
   403
	
sl@0
   404
	for (i=0; i<bout; i++)
sl@0
   405
		{
sl@0
   406
		sprintf(buf,"%02X",bbuf[i]);
sl@0
   407
		BIO_puts(out,buf);
sl@0
   408
		if(errno==ENOMEM)
sl@0
   409
   	    {
sl@0
   410
	      return 1;
sl@0
   411
	     }
sl@0
   412
	
sl@0
   413
		}
sl@0
   414
	BIO_puts(out,"\n");
sl@0
   415
	if(errno==ENOMEM)
sl@0
   416
   	{
sl@0
   417
	  return 1;
sl@0
   418
	}
sl@0
   419
	
sl@0
   420
	if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0))
sl@0
   421
		{
sl@0
   422
		fprintf(stderr,"Error in DH routines\n");
sl@0
   423
		ret=1;
sl@0
   424
		}
sl@0
   425
	else
sl@0
   426
		ret=0;
sl@0
   427
err:
sl@0
   428
	ERR_print_errors_fp(stderr);
sl@0
   429
    if(errno==ENOMEM)
sl@0
   430
   	{
sl@0
   431
	  return 1;
sl@0
   432
	}
sl@0
   433
	 ERR_remove_state(0);
sl@0
   434
     if(errno==ENOMEM)
sl@0
   435
   {
sl@0
   436
	  return 1;
sl@0
   437
	 }
sl@0
   438
    ERR_free_strings();
sl@0
   439
   if(errno==ENOMEM)
sl@0
   440
   {
sl@0
   441
	  return 1;
sl@0
   442
	 }    
sl@0
   443
	if (abuf != NULL) OPENSSL_free(abuf);
sl@0
   444
	if (bbuf != NULL) OPENSSL_free(bbuf);
sl@0
   445
	if(b != NULL) DH_free(b);
sl@0
   446
	if(a != NULL) DH_free(a);
sl@0
   447
	BIO_free(out);
sl@0
   448
#ifdef SYMBIAN	
sl@0
   449
	CRYPTO_cleanup_all_ex_data();
sl@0
   450
		if(errno==ENOMEM)
sl@0
   451
  {
sl@0
   452
	  return 1;
sl@0
   453
	}
sl@0
   454
sl@0
   455
#endif	
sl@0
   456
#ifdef OPENSSL_SYS_NETWARE
sl@0
   457
    if (ret) fprintf(stdout,"ERROR: %d\n", ret);
sl@0
   458
#endif
sl@0
   459
  fprintf(stdout,"Test case passed\n");
sl@0
   460
	//EXIT(ret);
sl@0
   461
	return(ret);
sl@0
   462
	}
sl@0
   463
sl@0
   464
static int MS_CALLBACK cb(int p, int n, BN_GENCB *arg)
sl@0
   465
	{
sl@0
   466
	char c='*';
sl@0
   467
sl@0
   468
	if (p == 0) c='.';
sl@0
   469
	if (p == 1) c='+';
sl@0
   470
	if (p == 2) c='*';
sl@0
   471
	if (p == 3) c='\n';
sl@0
   472
	BIO_write(arg->arg,&c,1);
sl@0
   473
	if(errno==ENOMEM)
sl@0
   474
   	{
sl@0
   475
	  return 1;
sl@0
   476
	}
sl@0
   477
	
sl@0
   478
	(void)BIO_flush(arg->arg);
sl@0
   479
	if(errno==ENOMEM)
sl@0
   480
   	{
sl@0
   481
	  return 1;
sl@0
   482
	}
sl@0
   483
	
sl@0
   484
#ifdef LINT
sl@0
   485
	p=n;
sl@0
   486
#endif
sl@0
   487
	return 1;
sl@0
   488
	}
sl@0
   489
#endif