155714Skris/* crypto/dsa/dsa_lib.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.
855714Skris *
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).
1555714Skris *
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.
2255714Skris *
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 :-).
3755714Skris * 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)"
4055714Skris *
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.
5255714Skris *
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
5955714Skris/* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
6055714Skris
6155714Skris#include <stdio.h>
6255714Skris#include "cryptlib.h"
6355714Skris#include <openssl/bn.h>
6455714Skris#include <openssl/dsa.h>
6555714Skris#include <openssl/asn1.h>
66111147Snectar#ifndef OPENSSL_NO_ENGINE
67109998Smarkm#include <openssl/engine.h>
68111147Snectar#endif
69160814Ssimon#ifndef OPENSSL_NO_DH
70160814Ssimon#include <openssl/dh.h>
71160814Ssimon#endif
7255714Skris
73238405Sjkim#ifdef OPENSSL_FIPS
74238405Sjkim#include <openssl/fips.h>
75238405Sjkim#endif
76238405Sjkim
77167612Ssimonconst char DSA_version[]="DSA" OPENSSL_VERSION_PTEXT;
7855714Skris
79109998Smarkmstatic const DSA_METHOD *default_DSA_method = NULL;
8059191Skris
81109998Smarkmvoid DSA_set_default_method(const DSA_METHOD *meth)
82109998Smarkm	{
8359191Skris	default_DSA_method = meth;
84109998Smarkm	}
8559191Skris
86109998Smarkmconst DSA_METHOD *DSA_get_default_method(void)
87109998Smarkm	{
88109998Smarkm	if(!default_DSA_method)
89238405Sjkim		{
90238405Sjkim#ifdef OPENSSL_FIPS
91238405Sjkim		if (FIPS_mode())
92238405Sjkim			return FIPS_dsa_openssl();
93238405Sjkim		else
94238405Sjkim			return DSA_OpenSSL();
95238405Sjkim#else
96109998Smarkm		default_DSA_method = DSA_OpenSSL();
97238405Sjkim#endif
98238405Sjkim		}
9959191Skris	return default_DSA_method;
100109998Smarkm	}
10159191Skris
10255714SkrisDSA *DSA_new(void)
103109998Smarkm	{
10459191Skris	return DSA_new_method(NULL);
105109998Smarkm	}
10659191Skris
107109998Smarkmint DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
108109998Smarkm	{
109109998Smarkm	/* NB: The caller is specifically setting a method, so it's not up to us
110109998Smarkm	 * to deal with which ENGINE it comes from. */
111109998Smarkm        const DSA_METHOD *mtmp;
11259191Skris        mtmp = dsa->meth;
11359191Skris        if (mtmp->finish) mtmp->finish(dsa);
114111147Snectar#ifndef OPENSSL_NO_ENGINE
115109998Smarkm	if (dsa->engine)
116109998Smarkm		{
117109998Smarkm		ENGINE_finish(dsa->engine);
118109998Smarkm		dsa->engine = NULL;
119109998Smarkm		}
120111147Snectar#endif
12159191Skris        dsa->meth = meth;
12259191Skris        if (meth->init) meth->init(dsa);
123109998Smarkm        return 1;
124109998Smarkm	}
12559191Skris
126109998SmarkmDSA *DSA_new_method(ENGINE *engine)
12755714Skris	{
12855714Skris	DSA *ret;
12955714Skris
13068651Skris	ret=(DSA *)OPENSSL_malloc(sizeof(DSA));
13155714Skris	if (ret == NULL)
13255714Skris		{
133109998Smarkm		DSAerr(DSA_F_DSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
13455714Skris		return(NULL);
13555714Skris		}
136109998Smarkm	ret->meth = DSA_get_default_method();
137111147Snectar#ifndef OPENSSL_NO_ENGINE
138109998Smarkm	if (engine)
139109998Smarkm		{
140109998Smarkm		if (!ENGINE_init(engine))
141109998Smarkm			{
142109998Smarkm			DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
143109998Smarkm			OPENSSL_free(ret);
144109998Smarkm			return NULL;
145109998Smarkm			}
146109998Smarkm		ret->engine = engine;
147109998Smarkm		}
148109998Smarkm	else
149109998Smarkm		ret->engine = ENGINE_get_default_DSA();
150109998Smarkm	if(ret->engine)
151109998Smarkm		{
152109998Smarkm		ret->meth = ENGINE_get_DSA(ret->engine);
153109998Smarkm		if(!ret->meth)
154109998Smarkm			{
155109998Smarkm			DSAerr(DSA_F_DSA_NEW_METHOD,
156109998Smarkm				ERR_R_ENGINE_LIB);
157109998Smarkm			ENGINE_finish(ret->engine);
158109998Smarkm			OPENSSL_free(ret);
159109998Smarkm			return NULL;
160109998Smarkm			}
161109998Smarkm		}
162111147Snectar#endif
163109998Smarkm
16455714Skris	ret->pad=0;
16555714Skris	ret->version=0;
16655714Skris	ret->write_params=1;
16755714Skris	ret->p=NULL;
16855714Skris	ret->q=NULL;
16955714Skris	ret->g=NULL;
17055714Skris
17155714Skris	ret->pub_key=NULL;
17255714Skris	ret->priv_key=NULL;
17355714Skris
17455714Skris	ret->kinv=NULL;
17555714Skris	ret->r=NULL;
17655714Skris	ret->method_mont_p=NULL;
17755714Skris
17855714Skris	ret->references=1;
179205128Ssimon	ret->flags=ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
180109998Smarkm	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
18159191Skris	if ((ret->meth->init != NULL) && !ret->meth->init(ret))
18259191Skris		{
183111147Snectar#ifndef OPENSSL_NO_ENGINE
184109998Smarkm		if (ret->engine)
185109998Smarkm			ENGINE_finish(ret->engine);
186111147Snectar#endif
187109998Smarkm		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
18868651Skris		OPENSSL_free(ret);
18959191Skris		ret=NULL;
19059191Skris		}
19159191Skris
19255714Skris	return(ret);
19355714Skris	}
19455714Skris
19555714Skrisvoid DSA_free(DSA *r)
19655714Skris	{
19755714Skris	int i;
19855714Skris
19955714Skris	if (r == NULL) return;
20055714Skris
20155714Skris	i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_DSA);
20255714Skris#ifdef REF_PRINT
20355714Skris	REF_PRINT("DSA",r);
20455714Skris#endif
20555714Skris	if (i > 0) return;
20655714Skris#ifdef REF_CHECK
20755714Skris	if (i < 0)
20855714Skris		{
20955714Skris		fprintf(stderr,"DSA_free, bad reference count\n");
21055714Skris		abort();
21155714Skris		}
21255714Skris#endif
21355714Skris
214109998Smarkm	if(r->meth->finish)
215109998Smarkm		r->meth->finish(r);
216111147Snectar#ifndef OPENSSL_NO_ENGINE
217109998Smarkm	if(r->engine)
218109998Smarkm		ENGINE_finish(r->engine);
219111147Snectar#endif
22076866Skris
221109998Smarkm	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
22259191Skris
22355714Skris	if (r->p != NULL) BN_clear_free(r->p);
22455714Skris	if (r->q != NULL) BN_clear_free(r->q);
22555714Skris	if (r->g != NULL) BN_clear_free(r->g);
22655714Skris	if (r->pub_key != NULL) BN_clear_free(r->pub_key);
22755714Skris	if (r->priv_key != NULL) BN_clear_free(r->priv_key);
22855714Skris	if (r->kinv != NULL) BN_clear_free(r->kinv);
22955714Skris	if (r->r != NULL) BN_clear_free(r->r);
23068651Skris	OPENSSL_free(r);
23155714Skris	}
23255714Skris
233109998Smarkmint DSA_up_ref(DSA *r)
23455714Skris	{
235109998Smarkm	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA);
236109998Smarkm#ifdef REF_PRINT
237109998Smarkm	REF_PRINT("DSA",r);
238109998Smarkm#endif
239109998Smarkm#ifdef REF_CHECK
240109998Smarkm	if (i < 2)
241109998Smarkm		{
242109998Smarkm		fprintf(stderr, "DSA_up_ref, bad reference count\n");
243109998Smarkm		abort();
244109998Smarkm		}
245109998Smarkm#endif
246109998Smarkm	return ((i > 1) ? 1 : 0);
247109998Smarkm	}
248109998Smarkm
249238405Sjkimint DSA_size(const DSA *r)
250238405Sjkim	{
251238405Sjkim	int ret,i;
252238405Sjkim	ASN1_INTEGER bs;
253238405Sjkim	unsigned char buf[4];	/* 4 bytes looks really small.
254238405Sjkim				   However, i2d_ASN1_INTEGER() will not look
255238405Sjkim				   beyond the first byte, as long as the second
256238405Sjkim				   parameter is NULL. */
257238405Sjkim
258238405Sjkim	i=BN_num_bits(r->q);
259238405Sjkim	bs.length=(i+7)/8;
260238405Sjkim	bs.data=buf;
261238405Sjkim	bs.type=V_ASN1_INTEGER;
262238405Sjkim	/* If the top bit is set the asn1 encoding is 1 larger. */
263238405Sjkim	buf[0]=0xff;
264238405Sjkim
265238405Sjkim	i=i2d_ASN1_INTEGER(&bs,NULL);
266238405Sjkim	i+=i; /* r and s */
267238405Sjkim	ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE);
268238405Sjkim	return(ret);
269238405Sjkim	}
270238405Sjkim
27159191Skrisint DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
27259191Skris	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
27359191Skris        {
274109998Smarkm	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp,
275109998Smarkm				new_func, dup_func, free_func);
27659191Skris        }
27759191Skris
27859191Skrisint DSA_set_ex_data(DSA *d, int idx, void *arg)
27959191Skris	{
28059191Skris	return(CRYPTO_set_ex_data(&d->ex_data,idx,arg));
28159191Skris	}
28259191Skris
28359191Skrisvoid *DSA_get_ex_data(DSA *d, int idx)
28459191Skris	{
28559191Skris	return(CRYPTO_get_ex_data(&d->ex_data,idx));
28659191Skris	}
28759191Skris
288109998Smarkm#ifndef OPENSSL_NO_DH
289109998SmarkmDH *DSA_dup_DH(const DSA *r)
29055714Skris	{
29155714Skris	/* DSA has p, q, g, optional pub_key, optional priv_key.
292238405Sjkim	 * DH has p, optional length, g, optional pub_key, optional priv_key,
293238405Sjkim	 * optional q.
29455714Skris	 */
29555714Skris
29655714Skris	DH *ret = NULL;
29755714Skris
29855714Skris	if (r == NULL)
29955714Skris		goto err;
30055714Skris	ret = DH_new();
30155714Skris	if (ret == NULL)
30255714Skris		goto err;
30355714Skris	if (r->p != NULL)
30455714Skris		if ((ret->p = BN_dup(r->p)) == NULL)
30555714Skris			goto err;
30655714Skris	if (r->q != NULL)
307238405Sjkim		{
30855714Skris		ret->length = BN_num_bits(r->q);
309238405Sjkim		if ((ret->q = BN_dup(r->q)) == NULL)
310238405Sjkim			goto err;
311238405Sjkim		}
31255714Skris	if (r->g != NULL)
31355714Skris		if ((ret->g = BN_dup(r->g)) == NULL)
31455714Skris			goto err;
31555714Skris	if (r->pub_key != NULL)
31655714Skris		if ((ret->pub_key = BN_dup(r->pub_key)) == NULL)
31755714Skris			goto err;
31855714Skris	if (r->priv_key != NULL)
31955714Skris		if ((ret->priv_key = BN_dup(r->priv_key)) == NULL)
32055714Skris			goto err;
32155714Skris
32255714Skris	return ret;
32355714Skris
32455714Skris err:
32555714Skris	if (ret != NULL)
32655714Skris		DH_free(ret);
32755714Skris	return NULL;
32855714Skris	}
32955714Skris#endif
330