1*1ec0d311Stb /* $OpenBSD: dhtest.c,v 1.14 2023/08/20 22:21:00 tb Exp $ */
23c6bd008Smiod /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33c6bd008Smiod * All rights reserved.
43c6bd008Smiod *
53c6bd008Smiod * This package is an SSL implementation written
63c6bd008Smiod * by Eric Young (eay@cryptsoft.com).
73c6bd008Smiod * The implementation was written so as to conform with Netscapes SSL.
83c6bd008Smiod *
93c6bd008Smiod * This library is free for commercial and non-commercial use as long as
103c6bd008Smiod * the following conditions are aheared to. The following conditions
113c6bd008Smiod * apply to all code found in this distribution, be it the RC4, RSA,
123c6bd008Smiod * lhash, DES, etc., code; not just the SSL code. The SSL documentation
133c6bd008Smiod * included with this distribution is covered by the same copyright terms
143c6bd008Smiod * except that the holder is Tim Hudson (tjh@cryptsoft.com).
153c6bd008Smiod *
163c6bd008Smiod * Copyright remains Eric Young's, and as such any Copyright notices in
173c6bd008Smiod * the code are not to be removed.
183c6bd008Smiod * If this package is used in a product, Eric Young should be given attribution
193c6bd008Smiod * as the author of the parts of the library used.
203c6bd008Smiod * This can be in the form of a textual message at program startup or
213c6bd008Smiod * in documentation (online or textual) provided with the package.
223c6bd008Smiod *
233c6bd008Smiod * Redistribution and use in source and binary forms, with or without
243c6bd008Smiod * modification, are permitted provided that the following conditions
253c6bd008Smiod * are met:
263c6bd008Smiod * 1. Redistributions of source code must retain the copyright
273c6bd008Smiod * notice, this list of conditions and the following disclaimer.
283c6bd008Smiod * 2. Redistributions in binary form must reproduce the above copyright
293c6bd008Smiod * notice, this list of conditions and the following disclaimer in the
303c6bd008Smiod * documentation and/or other materials provided with the distribution.
313c6bd008Smiod * 3. All advertising materials mentioning features or use of this software
323c6bd008Smiod * must display the following acknowledgement:
333c6bd008Smiod * "This product includes cryptographic software written by
343c6bd008Smiod * Eric Young (eay@cryptsoft.com)"
353c6bd008Smiod * The word 'cryptographic' can be left out if the rouines from the library
363c6bd008Smiod * being used are not cryptographic related :-).
373c6bd008Smiod * 4. If you include any Windows specific code (or a derivative thereof) from
383c6bd008Smiod * the apps directory (application code) you must include an acknowledgement:
393c6bd008Smiod * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
403c6bd008Smiod *
413c6bd008Smiod * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
423c6bd008Smiod * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
433c6bd008Smiod * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
443c6bd008Smiod * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
453c6bd008Smiod * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
463c6bd008Smiod * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
473c6bd008Smiod * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
483c6bd008Smiod * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
493c6bd008Smiod * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
503c6bd008Smiod * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
513c6bd008Smiod * SUCH DAMAGE.
523c6bd008Smiod *
533c6bd008Smiod * The licence and distribution terms for any publically available version or
543c6bd008Smiod * derivative of this code cannot be changed. i.e. this code cannot simply be
553c6bd008Smiod * copied and put under another distribution licence
563c6bd008Smiod * [including the GNU Public Licence.]
573c6bd008Smiod */
583c6bd008Smiod
599129a5c4Stb #include <err.h>
603c6bd008Smiod #include <stdio.h>
613c6bd008Smiod #include <stdlib.h>
623c6bd008Smiod #include <string.h>
633c6bd008Smiod
643c6bd008Smiod #include <openssl/crypto.h>
653c6bd008Smiod #include <openssl/bio.h>
663c6bd008Smiod #include <openssl/bn.h>
673c6bd008Smiod #include <openssl/err.h>
683c6bd008Smiod
693c6bd008Smiod #include <openssl/dh.h>
703c6bd008Smiod
719129a5c4Stb static int
cb(int p,int n,BN_GENCB * arg)729129a5c4Stb cb(int p, int n, BN_GENCB *arg)
731b2bbd6aSbcook {
741b2bbd6aSbcook char c = '*';
751b2bbd6aSbcook
761b2bbd6aSbcook if (p == 0)
771b2bbd6aSbcook c = '.';
781b2bbd6aSbcook if (p == 1)
791b2bbd6aSbcook c = '+';
801b2bbd6aSbcook if (p == 2)
811b2bbd6aSbcook c = '*';
821b2bbd6aSbcook if (p == 3)
831b2bbd6aSbcook c = '\n';
84ebcc569cStb printf("%c", c);
85ebcc569cStb fflush(stdout);
861b2bbd6aSbcook return 1;
871b2bbd6aSbcook }
883c6bd008Smiod
899129a5c4Stb int
main(int argc,char * argv[])909129a5c4Stb main(int argc, char *argv[])
913c6bd008Smiod {
929129a5c4Stb BN_GENCB *_cb;
93fc0704cbStb DH *dh = NULL;
94b46ee948Stb unsigned char *buf = NULL;
9527d42ba3Stb int flags, buf_len, secret_len;
9627d42ba3Stb int i;
979129a5c4Stb int ret = 1;
983c6bd008Smiod
999129a5c4Stb if ((_cb = BN_GENCB_new()) == NULL)
1009129a5c4Stb err(1, "BN_GENCB_new");
1019129a5c4Stb
102ebcc569cStb BN_GENCB_set(_cb, &cb, NULL);
103fc0704cbStb if ((dh = DH_new()) == NULL)
1049129a5c4Stb goto err;
1059129a5c4Stb
106c75241f9Stb #ifdef OPENSSL_NO_ENGINE
107fc0704cbStb if (DH_get0_engine(dh) != NULL) {
108c75241f9Stb fprintf(stderr, "ENGINE was not NULL\n");
109c75241f9Stb goto err;
110c75241f9Stb }
111c75241f9Stb #endif
112c75241f9Stb
113fc0704cbStb if (!DH_generate_parameters_ex(dh, 64, DH_GENERATOR_5, _cb))
1143c6bd008Smiod goto err;
1153c6bd008Smiod
11627d42ba3Stb if (!DH_check(dh, &flags))
1171b2bbd6aSbcook goto err;
11827d42ba3Stb if (flags & DH_CHECK_P_NOT_PRIME)
119cde7d4afStb printf("p value is not prime\n");
12027d42ba3Stb if (flags & DH_CHECK_P_NOT_SAFE_PRIME)
121cde7d4afStb printf("p value is not a safe prime\n");
12227d42ba3Stb if (flags & DH_UNABLE_TO_CHECK_GENERATOR)
123cde7d4afStb printf("unable to check the generator value\n");
12427d42ba3Stb if (flags & DH_NOT_SUITABLE_GENERATOR)
125cde7d4afStb printf("the g value is not a generator\n");
1263c6bd008Smiod
127ebcc569cStb printf("\np = ");
128fc0704cbStb if (!BN_print_fp(stdout, DH_get0_p(dh)))
129ebcc569cStb goto err;
130ebcc569cStb printf("\ng = ");
131fc0704cbStb if (!BN_print_fp(stdout, DH_get0_g(dh)))
132ebcc569cStb goto err;
133ebcc569cStb printf("\n");
1343c6bd008Smiod
135fc0704cbStb if (!DH_generate_key(dh))
1361b2bbd6aSbcook goto err;
137ebcc569cStb printf("pri1 = ");
138fc0704cbStb if (!BN_print_fp(stdout, DH_get0_priv_key(dh)))
139ebcc569cStb goto err;
140ebcc569cStb printf("\npub1 = ");
141fc0704cbStb if (!BN_print_fp(stdout, DH_get0_pub_key(dh)))
142ebcc569cStb goto err;
143ebcc569cStb printf("\n");
1443c6bd008Smiod
145b46ee948Stb buf_len = DH_size(dh);
146b46ee948Stb if ((buf = malloc(buf_len)) == NULL)
1479129a5c4Stb err(1, "malloc");
148b46ee948Stb secret_len = DH_compute_key(buf, DH_get0_pub_key(dh), dh);
1493c6bd008Smiod
150ebcc569cStb printf("key1 = ");
151b46ee948Stb for (i = 0; i < secret_len; i++) {
152b46ee948Stb printf("%02X", buf[i]);
1533c6bd008Smiod }
154ebcc569cStb printf("\n");
1553c6bd008Smiod
156b46ee948Stb if (secret_len < 4) {
1573c6bd008Smiod fprintf(stderr, "Error in DH routines\n");
1589129a5c4Stb goto err;
1599129a5c4Stb }
1609129a5c4Stb
1613c6bd008Smiod ret = 0;
1623c6bd008Smiod err:
1633c6bd008Smiod ERR_print_errors_fp(stderr);
1643c6bd008Smiod
165b46ee948Stb free(buf);
166fc0704cbStb DH_free(dh);
1679129a5c4Stb BN_GENCB_free(_cb);
1689129a5c4Stb
1699129a5c4Stb return (ret);
1703c6bd008Smiod }
171