dhtest.c revision 68651
1126007Spjd/* crypto/dh/dhtest.c */ 2142727Spjd/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3126007Spjd * All rights reserved. 4126007Spjd * 5126007Spjd * This package is an SSL implementation written 6126007Spjd * by Eric Young (eay@cryptsoft.com). 7126007Spjd * The implementation was written so as to conform with Netscapes SSL. 8126007Spjd * 9126007Spjd * This library is free for commercial and non-commercial use as long as 10126007Spjd * the following conditions are aheared to. The following conditions 11126007Spjd * apply to all code found in this distribution, be it the RC4, RSA, 12126007Spjd * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13155174Spjd * included with this distribution is covered by the same copyright terms 14126007Spjd * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15126007Spjd * 16126007Spjd * Copyright remains Eric Young's, and as such any Copyright notices in 17126007Spjd * the code are not to be removed. 18126007Spjd * If this package is used in a product, Eric Young should be given attribution 19126007Spjd * as the author of the parts of the library used. 20126007Spjd * This can be in the form of a textual message at program startup or 21126007Spjd * in documentation (online or textual) provided with the package. 22126007Spjd * 23126007Spjd * Redistribution and use in source and binary forms, with or without 24126007Spjd * modification, are permitted provided that the following conditions 25126007Spjd * are met: 26126007Spjd * 1. Redistributions of source code must retain the copyright 27126007Spjd * notice, this list of conditions and the following disclaimer. 28126007Spjd * 2. Redistributions in binary form must reproduce the above copyright 29126007Spjd * notice, this list of conditions and the following disclaimer in the 30126007Spjd * documentation and/or other materials provided with the distribution. 31126007Spjd * 3. All advertising materials mentioning features or use of this software 32126007Spjd * must display the following acknowledgement: 33126007Spjd * "This product includes cryptographic software written by 34126007Spjd * Eric Young (eay@cryptsoft.com)" 35126007Spjd * The word 'cryptographic' can be left out if the rouines from the library 36126007Spjd * being used are not cryptographic related :-). 37223921Sae * 4. If you include any Windows specific code (or a derivative thereof) from 38126007Spjd * the apps directory (application code) you must include an acknowledgement: 39126007Spjd * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40126007Spjd * 41126007Spjd * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42126007Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43219029Snetchild * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44126007Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45151897Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46126007Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47126565Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48227309Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49227309Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50126007Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51134528Spjd * SUCH DAMAGE. 52126007Spjd * 53126007Spjd * The licence and distribution terms for any publically available version or 54126007Spjd * derivative of this code cannot be changed. i.e. this code cannot simply be 55126007Spjd * copied and put under another distribution licence 56126007Spjd * [including the GNU Public Licence.] 57126007Spjd */ 58126007Spjd 59126007Spjd#include <stdio.h> 60126007Spjd#include <stdlib.h> 61126007Spjd#include <string.h> 62126007Spjd#ifdef WINDOWS 63126007Spjd#include "../bio/bss_file.c" 64126007Spjd#endif 65133318Sphk#include <openssl/crypto.h> 66126007Spjd#include <openssl/bio.h> 67126007Spjd#include <openssl/bn.h> 68126007Spjd#include <openssl/rand.h> 69126007Spjd 70126007Spjd#ifdef NO_DH 71126007Spjdint main(int argc, char *argv[]) 72126007Spjd{ 73126773Spjd printf("No DH support\n"); 74126773Spjd return(0); 75126773Spjd} 76126773Spjd#else 77126773Spjd#include <openssl/dh.h> 78126773Spjd 79126773Spjd#ifdef WIN16 80126773Spjd#define MS_CALLBACK _far _loadds 81126773Spjd#else 82126773Spjd#define MS_CALLBACK 83126773Spjd#endif 84126773Spjd 85126773Spjdstatic void MS_CALLBACK cb(int p, int n, void *arg); 86126773Spjd#ifdef NO_STDIO 87126773Spjd#define APPS_WIN16 88126773Spjd#include "bss_file.c" 89126773Spjd#endif 90126773Spjd 91126773Spjdstatic const char rnd_seed[] = "string to make the random number generator think it has entropy"; 92126773Spjd 93126773Spjdint main(int argc, char *argv[]) 94126773Spjd { 95126773Spjd DH *a; 96126773Spjd DH *b=NULL; 97126773Spjd char buf[12]; 98126773Spjd unsigned char *abuf=NULL,*bbuf=NULL; 99126007Spjd int i,alen,blen,aout,bout,ret=1; 100126007Spjd BIO *out; 101126007Spjd 102126007Spjd#ifdef WIN32 103126007Spjd CRYPTO_malloc_init(); 104126007Spjd#endif 105126007Spjd 106126007Spjd RAND_seed(rnd_seed, sizeof rnd_seed); 107126007Spjd 108126565Spjd out=BIO_new(BIO_s_file()); 109126007Spjd if (out == NULL) exit(1); 110126007Spjd BIO_set_fp(out,stdout,BIO_NOCLOSE); 111126007Spjd 112126007Spjd a=DH_generate_parameters(64,DH_GENERATOR_5,cb,out); 113126007Spjd if (a == NULL) goto err; 114126007Spjd 115126007Spjd BIO_puts(out,"\np ="); 116126007Spjd BN_print(out,a->p); 117126007Spjd BIO_puts(out,"\ng ="); 118126007Spjd BN_print(out,a->g); 119126007Spjd BIO_puts(out,"\n"); 120126007Spjd 121227004Smav b=DH_new(); 122126565Spjd if (b == NULL) goto err; 123126007Spjd 124126007Spjd b->p=BN_dup(a->p); 125126007Spjd b->g=BN_dup(a->g); 126227004Smav if ((b->p == NULL) || (b->g == NULL)) goto err; 127227004Smav 128227004Smav if (!DH_generate_key(a)) goto err; 129227004Smav BIO_puts(out,"pri 1="); 130227004Smav BN_print(out,a->priv_key); 131126007Spjd BIO_puts(out,"\npub 1="); 132129478Spjd BN_print(out,a->pub_key); 133227004Smav BIO_puts(out,"\n"); 134227004Smav 135306765Smav if (!DH_generate_key(b)) goto err; 136129478Spjd BIO_puts(out,"pri 2="); 137129478Spjd BN_print(out,b->priv_key); 138126007Spjd BIO_puts(out,"\npub 2="); 139126007Spjd BN_print(out,b->pub_key); 140227004Smav BIO_puts(out,"\n"); 141227004Smav 142126007Spjd alen=DH_size(a); 143126007Spjd abuf=(unsigned char *)OPENSSL_malloc(alen); 144227004Smav aout=DH_compute_key(abuf,b->pub_key,a); 145227004Smav 146227004Smav BIO_puts(out,"key1 ="); 147126007Spjd for (i=0; i<aout; i++) 148126007Spjd { 149126007Spjd sprintf(buf,"%02X",abuf[i]); 150126007Spjd BIO_puts(out,buf); 151126007Spjd } 152126007Spjd BIO_puts(out,"\n"); 153126007Spjd 154126007Spjd blen=DH_size(b); 155126007Spjd bbuf=(unsigned char *)OPENSSL_malloc(blen); 156126007Spjd bout=DH_compute_key(bbuf,a->pub_key,b); 157126007Spjd 158126007Spjd BIO_puts(out,"key2 ="); 159126007Spjd for (i=0; i<bout; i++) 160126007Spjd { 161126007Spjd sprintf(buf,"%02X",bbuf[i]); 162126007Spjd BIO_puts(out,buf); 163126007Spjd } 164126007Spjd BIO_puts(out,"\n"); 165126007Spjd if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0)) 166126007Spjd { 167126007Spjd fprintf(stderr,"Error in DH routines\n"); 168126007Spjd ret=1; 169126007Spjd } 170126007Spjd else 171227004Smav ret=0; 172227004Smaverr: 173126007Spjd if (abuf != NULL) OPENSSL_free(abuf); 174126007Spjd if (bbuf != NULL) OPENSSL_free(bbuf); 175126007Spjd if(b != NULL) DH_free(b); 176227004Smav if(a != NULL) DH_free(a); 177126007Spjd BIO_free(out); 178126007Spjd exit(ret); 179126007Spjd return(ret); 180126007Spjd } 181126007Spjd 182126007Spjdstatic void MS_CALLBACK cb(int p, int n, void *arg) 183132342Spjd { 184126007Spjd char c='*'; 185126007Spjd 186227004Smav if (p == 0) c='.'; 187126007Spjd if (p == 1) c='+'; 188227004Smav if (p == 2) c='*'; 189227004Smav if (p == 3) c='\n'; 190227004Smav BIO_write((BIO *)arg,&c,1); 191227004Smav (void)BIO_flush((BIO *)arg); 192227004Smav#ifdef LINT 193227004Smav p=n; 194126007Spjd#endif 195126007Spjd } 196227004Smav#endif 197126007Spjd