xref: /openbsd-src/lib/libcrypto/dsa/dsa_key.c (revision 7a31e386bf17f78a5df04735a19059c648d96e95)
1*7a31e386Stb /* $OpenBSD: dsa_key.c,v 1.37 2024/05/11 06:43:50 tb Exp $ */
25b37fcf3Sryker /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
35b37fcf3Sryker  * All rights reserved.
45b37fcf3Sryker  *
55b37fcf3Sryker  * This package is an SSL implementation written
65b37fcf3Sryker  * by Eric Young (eay@cryptsoft.com).
75b37fcf3Sryker  * The implementation was written so as to conform with Netscapes SSL.
85b37fcf3Sryker  *
95b37fcf3Sryker  * This library is free for commercial and non-commercial use as long as
105b37fcf3Sryker  * the following conditions are aheared to.  The following conditions
115b37fcf3Sryker  * apply to all code found in this distribution, be it the RC4, RSA,
125b37fcf3Sryker  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
135b37fcf3Sryker  * included with this distribution is covered by the same copyright terms
145b37fcf3Sryker  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
155b37fcf3Sryker  *
165b37fcf3Sryker  * Copyright remains Eric Young's, and as such any Copyright notices in
175b37fcf3Sryker  * the code are not to be removed.
185b37fcf3Sryker  * If this package is used in a product, Eric Young should be given attribution
195b37fcf3Sryker  * as the author of the parts of the library used.
205b37fcf3Sryker  * This can be in the form of a textual message at program startup or
215b37fcf3Sryker  * in documentation (online or textual) provided with the package.
225b37fcf3Sryker  *
235b37fcf3Sryker  * Redistribution and use in source and binary forms, with or without
245b37fcf3Sryker  * modification, are permitted provided that the following conditions
255b37fcf3Sryker  * are met:
265b37fcf3Sryker  * 1. Redistributions of source code must retain the copyright
275b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer.
285b37fcf3Sryker  * 2. Redistributions in binary form must reproduce the above copyright
295b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer in the
305b37fcf3Sryker  *    documentation and/or other materials provided with the distribution.
315b37fcf3Sryker  * 3. All advertising materials mentioning features or use of this software
325b37fcf3Sryker  *    must display the following acknowledgement:
335b37fcf3Sryker  *    "This product includes cryptographic software written by
345b37fcf3Sryker  *     Eric Young (eay@cryptsoft.com)"
355b37fcf3Sryker  *    The word 'cryptographic' can be left out if the rouines from the library
365b37fcf3Sryker  *    being used are not cryptographic related :-).
375b37fcf3Sryker  * 4. If you include any Windows specific code (or a derivative thereof) from
385b37fcf3Sryker  *    the apps directory (application code) you must include an acknowledgement:
395b37fcf3Sryker  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
405b37fcf3Sryker  *
415b37fcf3Sryker  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
425b37fcf3Sryker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
435b37fcf3Sryker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
445b37fcf3Sryker  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
455b37fcf3Sryker  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
465b37fcf3Sryker  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
475b37fcf3Sryker  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
485b37fcf3Sryker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
495b37fcf3Sryker  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
505b37fcf3Sryker  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
515b37fcf3Sryker  * SUCH DAMAGE.
525b37fcf3Sryker  *
535b37fcf3Sryker  * The licence and distribution terms for any publically available version or
545b37fcf3Sryker  * derivative of this code cannot be changed.  i.e. this code cannot simply be
555b37fcf3Sryker  * copied and put under another distribution licence
565b37fcf3Sryker  * [including the GNU Public Licence.]
575b37fcf3Sryker  */
585b37fcf3Sryker 
595b37fcf3Sryker #include <stdio.h>
605b37fcf3Sryker #include <time.h>
618cf4d6a6Sjsing 
628cf4d6a6Sjsing #include <openssl/opensslconf.h>
638cf4d6a6Sjsing 
644fcf65c5Sdjm #ifndef OPENSSL_NO_SHA
65b6ab114eSjsing 
66913ec974Sbeck #include <openssl/bn.h>
67913ec974Sbeck #include <openssl/dsa.h>
68f69303b6Stb 
69c9675a23Stb #include "bn_local.h"
70c9675a23Stb #include "dsa_local.h"
715b37fcf3Sryker 
72e85b1b81Smiod int
DSA_generate_key(DSA * dsa)73e85b1b81Smiod DSA_generate_key(DSA *dsa)
745b37fcf3Sryker {
75c541982eStb 	BIGNUM *pub_key = NULL, *priv_key = NULL;
76f6b6cc33Sjsing 	BN_CTX *ctx = NULL;
77f6b6cc33Sjsing 	int ok = 0;
78f6b6cc33Sjsing 
79f6b6cc33Sjsing 	if ((priv_key = BN_new()) == NULL)
80f6b6cc33Sjsing 		goto err;
81f6b6cc33Sjsing 	if ((pub_key = BN_new()) == NULL)
82f6b6cc33Sjsing 		goto err;
835b37fcf3Sryker 
84e85b1b81Smiod 	if ((ctx = BN_CTX_new()) == NULL)
85e85b1b81Smiod 		goto err;
865b37fcf3Sryker 
8712347e81Stb 	if (!bn_rand_interval(priv_key, 1, dsa->q))
88e85b1b81Smiod 		goto err;
89a5369e1aStb 	if (!BN_mod_exp_ct(pub_key, dsa->g, priv_key, dsa->p, ctx))
90e85b1b81Smiod 		goto err;
915b37fcf3Sryker 
92f6b6cc33Sjsing 	BN_free(dsa->priv_key);
935b37fcf3Sryker 	dsa->priv_key = priv_key;
94f6b6cc33Sjsing 	priv_key = NULL;
95f6b6cc33Sjsing 
96f6b6cc33Sjsing 	BN_free(dsa->pub_key);
975b37fcf3Sryker 	dsa->pub_key = pub_key;
98f6b6cc33Sjsing 	pub_key = NULL;
99f6b6cc33Sjsing 
1005b37fcf3Sryker 	ok = 1;
1015b37fcf3Sryker 
1025b37fcf3Sryker  err:
103e85b1b81Smiod 	BN_free(pub_key);
104e85b1b81Smiod 	BN_free(priv_key);
105e85b1b81Smiod 	BN_CTX_free(ctx);
106f6b6cc33Sjsing 
107e85b1b81Smiod 	return ok;
1085b37fcf3Sryker }
109bd7c3e4dStb LCRYPTO_ALIAS(DSA_generate_key);
110bd7c3e4dStb 
111913ec974Sbeck #endif
112