155714Skris/* crypto/dh/dhtest.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8296465Sdelphij *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296465Sdelphij *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22296465Sdelphij *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296465Sdelphij *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52296465Sdelphij *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
59296465Sdelphij/*
60296465Sdelphij * Until the key-gen callbacks are modified to use newer prototypes, we allow
61296465Sdelphij * deprecated functions for openssl-internal code
62296465Sdelphij */
63160814Ssimon#ifdef OPENSSL_NO_DEPRECATED
64296465Sdelphij# undef OPENSSL_NO_DEPRECATED
65160814Ssimon#endif
66160814Ssimon
6755714Skris#include <stdio.h>
6855714Skris#include <stdlib.h>
6955714Skris#include <string.h>
70109998Smarkm
71109998Smarkm#include "../e_os.h"
72109998Smarkm
7355714Skris#include <openssl/crypto.h>
7455714Skris#include <openssl/bio.h>
7555714Skris#include <openssl/bn.h>
7659191Skris#include <openssl/rand.h>
77100928Snectar#include <openssl/err.h>
7855714Skris
79109998Smarkm#ifdef OPENSSL_NO_DH
8055714Skrisint main(int argc, char *argv[])
8155714Skris{
8255714Skris    printf("No DH support\n");
83296465Sdelphij    return (0);
8455714Skris}
8555714Skris#else
86296465Sdelphij# include <openssl/dh.h>
8755714Skris
88296465Sdelphij# ifdef OPENSSL_SYS_WIN16
89296465Sdelphij#  define MS_CALLBACK     _far _loadds
90296465Sdelphij# else
91296465Sdelphij#  define MS_CALLBACK
92296465Sdelphij# endif
9355714Skris
94160814Ssimonstatic int MS_CALLBACK cb(int p, int n, BN_GENCB *arg);
9555714Skris
96296465Sdelphijstatic const char rnd_seed[] =
97296465Sdelphij    "string to make the random number generator think it has entropy";
9855714Skris
9955714Skrisint main(int argc, char *argv[])
100296465Sdelphij{
101296465Sdelphij    BN_GENCB _cb;
102296465Sdelphij    DH *a;
103296465Sdelphij    DH *b = NULL;
104296465Sdelphij    char buf[12];
105296465Sdelphij    unsigned char *abuf = NULL, *bbuf = NULL;
106296465Sdelphij    int i, alen, blen, aout, bout, ret = 1;
107296465Sdelphij    BIO *out;
10855714Skris
109296465Sdelphij    CRYPTO_malloc_debug_init();
110296465Sdelphij    CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
111296465Sdelphij    CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
112109998Smarkm
113296465Sdelphij# ifdef OPENSSL_SYS_WIN32
114296465Sdelphij    CRYPTO_malloc_init();
115296465Sdelphij# endif
11655714Skris
117296465Sdelphij    RAND_seed(rnd_seed, sizeof rnd_seed);
11859191Skris
119296465Sdelphij    out = BIO_new(BIO_s_file());
120296465Sdelphij    if (out == NULL)
121296465Sdelphij        EXIT(1);
122296465Sdelphij    BIO_set_fp(out, stdout, BIO_NOCLOSE);
12355714Skris
124296465Sdelphij    BN_GENCB_set(&_cb, &cb, out);
125296465Sdelphij    if (((a = DH_new()) == NULL) || !DH_generate_parameters_ex(a, 64,
126296465Sdelphij                                                               DH_GENERATOR_5,
127296465Sdelphij                                                               &_cb))
128296465Sdelphij        goto err;
12955714Skris
130296465Sdelphij    if (!DH_check(a, &i))
131296465Sdelphij        goto err;
132296465Sdelphij    if (i & DH_CHECK_P_NOT_PRIME)
133296465Sdelphij        BIO_puts(out, "p value is not prime\n");
134296465Sdelphij    if (i & DH_CHECK_P_NOT_SAFE_PRIME)
135296465Sdelphij        BIO_puts(out, "p value is not a safe prime\n");
136296465Sdelphij    if (i & DH_UNABLE_TO_CHECK_GENERATOR)
137296465Sdelphij        BIO_puts(out, "unable to check the generator value\n");
138296465Sdelphij    if (i & DH_NOT_SUITABLE_GENERATOR)
139296465Sdelphij        BIO_puts(out, "the g value is not a generator\n");
140100928Snectar
141296465Sdelphij    BIO_puts(out, "\np    =");
142296465Sdelphij    BN_print(out, a->p);
143296465Sdelphij    BIO_puts(out, "\ng    =");
144296465Sdelphij    BN_print(out, a->g);
145296465Sdelphij    BIO_puts(out, "\n");
14655714Skris
147296465Sdelphij    b = DH_new();
148296465Sdelphij    if (b == NULL)
149296465Sdelphij        goto err;
15055714Skris
151296465Sdelphij    b->p = BN_dup(a->p);
152296465Sdelphij    b->g = BN_dup(a->g);
153296465Sdelphij    if ((b->p == NULL) || (b->g == NULL))
154296465Sdelphij        goto err;
15555714Skris
156296465Sdelphij    /* Set a to run with normal modexp and b to use constant time */
157296465Sdelphij    a->flags &= ~DH_FLAG_NO_EXP_CONSTTIME;
158296465Sdelphij    b->flags |= DH_FLAG_NO_EXP_CONSTTIME;
159160814Ssimon
160296465Sdelphij    if (!DH_generate_key(a))
161296465Sdelphij        goto err;
162296465Sdelphij    BIO_puts(out, "pri 1=");
163296465Sdelphij    BN_print(out, a->priv_key);
164296465Sdelphij    BIO_puts(out, "\npub 1=");
165296465Sdelphij    BN_print(out, a->pub_key);
166296465Sdelphij    BIO_puts(out, "\n");
16755714Skris
168296465Sdelphij    if (!DH_generate_key(b))
169296465Sdelphij        goto err;
170296465Sdelphij    BIO_puts(out, "pri 2=");
171296465Sdelphij    BN_print(out, b->priv_key);
172296465Sdelphij    BIO_puts(out, "\npub 2=");
173296465Sdelphij    BN_print(out, b->pub_key);
174296465Sdelphij    BIO_puts(out, "\n");
17555714Skris
176296465Sdelphij    alen = DH_size(a);
177296465Sdelphij    abuf = (unsigned char *)OPENSSL_malloc(alen);
178296465Sdelphij    aout = DH_compute_key(abuf, b->pub_key, a);
17955714Skris
180296465Sdelphij    BIO_puts(out, "key1 =");
181296465Sdelphij    for (i = 0; i < aout; i++) {
182296465Sdelphij        sprintf(buf, "%02X", abuf[i]);
183296465Sdelphij        BIO_puts(out, buf);
184296465Sdelphij    }
185296465Sdelphij    BIO_puts(out, "\n");
18655714Skris
187296465Sdelphij    blen = DH_size(b);
188296465Sdelphij    bbuf = (unsigned char *)OPENSSL_malloc(blen);
189296465Sdelphij    bout = DH_compute_key(bbuf, a->pub_key, b);
19055714Skris
191296465Sdelphij    BIO_puts(out, "key2 =");
192296465Sdelphij    for (i = 0; i < bout; i++) {
193296465Sdelphij        sprintf(buf, "%02X", bbuf[i]);
194296465Sdelphij        BIO_puts(out, buf);
195296465Sdelphij    }
196296465Sdelphij    BIO_puts(out, "\n");
197296465Sdelphij    if ((aout < 4) || (bout != aout) || (memcmp(abuf, bbuf, aout) != 0)) {
198296465Sdelphij        fprintf(stderr, "Error in DH routines\n");
199296465Sdelphij        ret = 1;
200296465Sdelphij    } else
201296465Sdelphij        ret = 0;
202296465Sdelphij err:
203296465Sdelphij    ERR_print_errors_fp(stderr);
204100928Snectar
205296465Sdelphij    if (abuf != NULL)
206296465Sdelphij        OPENSSL_free(abuf);
207296465Sdelphij    if (bbuf != NULL)
208296465Sdelphij        OPENSSL_free(bbuf);
209296465Sdelphij    if (b != NULL)
210296465Sdelphij        DH_free(b);
211296465Sdelphij    if (a != NULL)
212296465Sdelphij        DH_free(a);
213296465Sdelphij    BIO_free(out);
214296465Sdelphij# ifdef OPENSSL_SYS_NETWARE
215296465Sdelphij    if (ret)
216296465Sdelphij        printf("ERROR: %d\n", ret);
217296465Sdelphij# endif
218296465Sdelphij    EXIT(ret);
219296465Sdelphij    return (ret);
220296465Sdelphij}
22155714Skris
222160814Ssimonstatic int MS_CALLBACK cb(int p, int n, BN_GENCB *arg)
223296465Sdelphij{
224296465Sdelphij    char c = '*';
22555714Skris
226296465Sdelphij    if (p == 0)
227296465Sdelphij        c = '.';
228296465Sdelphij    if (p == 1)
229296465Sdelphij        c = '+';
230296465Sdelphij    if (p == 2)
231296465Sdelphij        c = '*';
232296465Sdelphij    if (p == 3)
233296465Sdelphij        c = '\n';
234296465Sdelphij    BIO_write(arg->arg, &c, 1);
235296465Sdelphij    (void)BIO_flush(arg->arg);
236296465Sdelphij# ifdef LINT
237296465Sdelphij    p = n;
238296465Sdelphij# endif
239296465Sdelphij    return 1;
240296465Sdelphij}
24155714Skris#endif
242