xref: /onnv-gate/usr/src/common/openssl/crypto/dsa/dsa_lib.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/dsa/dsa_lib.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #include <stdio.h>
620Sstevel@tonic-gate #include "cryptlib.h"
630Sstevel@tonic-gate #include <openssl/bn.h>
640Sstevel@tonic-gate #include <openssl/dsa.h>
650Sstevel@tonic-gate #include <openssl/asn1.h>
660Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
670Sstevel@tonic-gate #include <openssl/engine.h>
680Sstevel@tonic-gate #endif
69*2139Sjp161948 #ifndef OPENSSL_NO_DH
70*2139Sjp161948 #include <openssl/dh.h>
71*2139Sjp161948 #endif
720Sstevel@tonic-gate 
730Sstevel@tonic-gate const char *DSA_version="DSA" OPENSSL_VERSION_PTEXT;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate static const DSA_METHOD *default_DSA_method = NULL;
760Sstevel@tonic-gate 
DSA_set_default_method(const DSA_METHOD * meth)770Sstevel@tonic-gate void DSA_set_default_method(const DSA_METHOD *meth)
780Sstevel@tonic-gate 	{
790Sstevel@tonic-gate 	default_DSA_method = meth;
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 
DSA_get_default_method(void)820Sstevel@tonic-gate const DSA_METHOD *DSA_get_default_method(void)
830Sstevel@tonic-gate 	{
840Sstevel@tonic-gate 	if(!default_DSA_method)
850Sstevel@tonic-gate 		default_DSA_method = DSA_OpenSSL();
860Sstevel@tonic-gate 	return default_DSA_method;
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 
DSA_new(void)890Sstevel@tonic-gate DSA *DSA_new(void)
900Sstevel@tonic-gate 	{
910Sstevel@tonic-gate 	return DSA_new_method(NULL);
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
DSA_set_method(DSA * dsa,const DSA_METHOD * meth)940Sstevel@tonic-gate int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
950Sstevel@tonic-gate 	{
960Sstevel@tonic-gate 	/* NB: The caller is specifically setting a method, so it's not up to us
970Sstevel@tonic-gate 	 * to deal with which ENGINE it comes from. */
980Sstevel@tonic-gate         const DSA_METHOD *mtmp;
990Sstevel@tonic-gate         mtmp = dsa->meth;
1000Sstevel@tonic-gate         if (mtmp->finish) mtmp->finish(dsa);
1010Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1020Sstevel@tonic-gate 	if (dsa->engine)
1030Sstevel@tonic-gate 		{
1040Sstevel@tonic-gate 		ENGINE_finish(dsa->engine);
1050Sstevel@tonic-gate 		dsa->engine = NULL;
1060Sstevel@tonic-gate 		}
1070Sstevel@tonic-gate #endif
1080Sstevel@tonic-gate         dsa->meth = meth;
1090Sstevel@tonic-gate         if (meth->init) meth->init(dsa);
1100Sstevel@tonic-gate         return 1;
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 
DSA_new_method(ENGINE * engine)1130Sstevel@tonic-gate DSA *DSA_new_method(ENGINE *engine)
1140Sstevel@tonic-gate 	{
1150Sstevel@tonic-gate 	DSA *ret;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	ret=(DSA *)OPENSSL_malloc(sizeof(DSA));
1180Sstevel@tonic-gate 	if (ret == NULL)
1190Sstevel@tonic-gate 		{
1200Sstevel@tonic-gate 		DSAerr(DSA_F_DSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
1210Sstevel@tonic-gate 		return(NULL);
1220Sstevel@tonic-gate 		}
1230Sstevel@tonic-gate 	ret->meth = DSA_get_default_method();
1240Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1250Sstevel@tonic-gate 	if (engine)
1260Sstevel@tonic-gate 		{
1270Sstevel@tonic-gate 		if (!ENGINE_init(engine))
1280Sstevel@tonic-gate 			{
1290Sstevel@tonic-gate 			DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
1300Sstevel@tonic-gate 			OPENSSL_free(ret);
1310Sstevel@tonic-gate 			return NULL;
1320Sstevel@tonic-gate 			}
1330Sstevel@tonic-gate 		ret->engine = engine;
1340Sstevel@tonic-gate 		}
1350Sstevel@tonic-gate 	else
1360Sstevel@tonic-gate 		ret->engine = ENGINE_get_default_DSA();
1370Sstevel@tonic-gate 	if(ret->engine)
1380Sstevel@tonic-gate 		{
1390Sstevel@tonic-gate 		ret->meth = ENGINE_get_DSA(ret->engine);
1400Sstevel@tonic-gate 		if(!ret->meth)
1410Sstevel@tonic-gate 			{
1420Sstevel@tonic-gate 			DSAerr(DSA_F_DSA_NEW_METHOD,
1430Sstevel@tonic-gate 				ERR_R_ENGINE_LIB);
1440Sstevel@tonic-gate 			ENGINE_finish(ret->engine);
1450Sstevel@tonic-gate 			OPENSSL_free(ret);
1460Sstevel@tonic-gate 			return NULL;
1470Sstevel@tonic-gate 			}
1480Sstevel@tonic-gate 		}
1490Sstevel@tonic-gate #endif
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	ret->pad=0;
1520Sstevel@tonic-gate 	ret->version=0;
1530Sstevel@tonic-gate 	ret->write_params=1;
1540Sstevel@tonic-gate 	ret->p=NULL;
1550Sstevel@tonic-gate 	ret->q=NULL;
1560Sstevel@tonic-gate 	ret->g=NULL;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	ret->pub_key=NULL;
1590Sstevel@tonic-gate 	ret->priv_key=NULL;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	ret->kinv=NULL;
1620Sstevel@tonic-gate 	ret->r=NULL;
1630Sstevel@tonic-gate 	ret->method_mont_p=NULL;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	ret->references=1;
1660Sstevel@tonic-gate 	ret->flags=ret->meth->flags;
1670Sstevel@tonic-gate 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
1680Sstevel@tonic-gate 	if ((ret->meth->init != NULL) && !ret->meth->init(ret))
1690Sstevel@tonic-gate 		{
1700Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1710Sstevel@tonic-gate 		if (ret->engine)
1720Sstevel@tonic-gate 			ENGINE_finish(ret->engine);
1730Sstevel@tonic-gate #endif
1740Sstevel@tonic-gate 		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
1750Sstevel@tonic-gate 		OPENSSL_free(ret);
1760Sstevel@tonic-gate 		ret=NULL;
1770Sstevel@tonic-gate 		}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	return(ret);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
DSA_free(DSA * r)1820Sstevel@tonic-gate void DSA_free(DSA *r)
1830Sstevel@tonic-gate 	{
1840Sstevel@tonic-gate 	int i;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (r == NULL) return;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_DSA);
1890Sstevel@tonic-gate #ifdef REF_PRINT
1900Sstevel@tonic-gate 	REF_PRINT("DSA",r);
1910Sstevel@tonic-gate #endif
1920Sstevel@tonic-gate 	if (i > 0) return;
1930Sstevel@tonic-gate #ifdef REF_CHECK
1940Sstevel@tonic-gate 	if (i < 0)
1950Sstevel@tonic-gate 		{
1960Sstevel@tonic-gate 		fprintf(stderr,"DSA_free, bad reference count\n");
1970Sstevel@tonic-gate 		abort();
1980Sstevel@tonic-gate 		}
1990Sstevel@tonic-gate #endif
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	if(r->meth->finish)
2020Sstevel@tonic-gate 		r->meth->finish(r);
2030Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
2040Sstevel@tonic-gate 	if(r->engine)
2050Sstevel@tonic-gate 		ENGINE_finish(r->engine);
2060Sstevel@tonic-gate #endif
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	if (r->p != NULL) BN_clear_free(r->p);
2110Sstevel@tonic-gate 	if (r->q != NULL) BN_clear_free(r->q);
2120Sstevel@tonic-gate 	if (r->g != NULL) BN_clear_free(r->g);
2130Sstevel@tonic-gate 	if (r->pub_key != NULL) BN_clear_free(r->pub_key);
2140Sstevel@tonic-gate 	if (r->priv_key != NULL) BN_clear_free(r->priv_key);
2150Sstevel@tonic-gate 	if (r->kinv != NULL) BN_clear_free(r->kinv);
2160Sstevel@tonic-gate 	if (r->r != NULL) BN_clear_free(r->r);
2170Sstevel@tonic-gate 	OPENSSL_free(r);
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 
DSA_up_ref(DSA * r)2200Sstevel@tonic-gate int DSA_up_ref(DSA *r)
2210Sstevel@tonic-gate 	{
2220Sstevel@tonic-gate 	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA);
2230Sstevel@tonic-gate #ifdef REF_PRINT
2240Sstevel@tonic-gate 	REF_PRINT("DSA",r);
2250Sstevel@tonic-gate #endif
2260Sstevel@tonic-gate #ifdef REF_CHECK
2270Sstevel@tonic-gate 	if (i < 2)
2280Sstevel@tonic-gate 		{
2290Sstevel@tonic-gate 		fprintf(stderr, "DSA_up_ref, bad reference count\n");
2300Sstevel@tonic-gate 		abort();
2310Sstevel@tonic-gate 		}
2320Sstevel@tonic-gate #endif
2330Sstevel@tonic-gate 	return ((i > 1) ? 1 : 0);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
DSA_size(const DSA * r)2360Sstevel@tonic-gate int DSA_size(const DSA *r)
2370Sstevel@tonic-gate 	{
2380Sstevel@tonic-gate 	int ret,i;
2390Sstevel@tonic-gate 	ASN1_INTEGER bs;
2400Sstevel@tonic-gate 	unsigned char buf[4];	/* 4 bytes looks really small.
2410Sstevel@tonic-gate 				   However, i2d_ASN1_INTEGER() will not look
2420Sstevel@tonic-gate 				   beyond the first byte, as long as the second
2430Sstevel@tonic-gate 				   parameter is NULL. */
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	i=BN_num_bits(r->q);
2460Sstevel@tonic-gate 	bs.length=(i+7)/8;
2470Sstevel@tonic-gate 	bs.data=buf;
2480Sstevel@tonic-gate 	bs.type=V_ASN1_INTEGER;
2490Sstevel@tonic-gate 	/* If the top bit is set the asn1 encoding is 1 larger. */
2500Sstevel@tonic-gate 	buf[0]=0xff;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	i=i2d_ASN1_INTEGER(&bs,NULL);
2530Sstevel@tonic-gate 	i+=i; /* r and s */
2540Sstevel@tonic-gate 	ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE);
2550Sstevel@tonic-gate 	return(ret);
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
DSA_get_ex_new_index(long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)2580Sstevel@tonic-gate int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2590Sstevel@tonic-gate 	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
2600Sstevel@tonic-gate         {
2610Sstevel@tonic-gate 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp,
2620Sstevel@tonic-gate 				new_func, dup_func, free_func);
2630Sstevel@tonic-gate         }
2640Sstevel@tonic-gate 
DSA_set_ex_data(DSA * d,int idx,void * arg)2650Sstevel@tonic-gate int DSA_set_ex_data(DSA *d, int idx, void *arg)
2660Sstevel@tonic-gate 	{
2670Sstevel@tonic-gate 	return(CRYPTO_set_ex_data(&d->ex_data,idx,arg));
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 
DSA_get_ex_data(DSA * d,int idx)2700Sstevel@tonic-gate void *DSA_get_ex_data(DSA *d, int idx)
2710Sstevel@tonic-gate 	{
2720Sstevel@tonic-gate 	return(CRYPTO_get_ex_data(&d->ex_data,idx));
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
DSA_dup_DH(const DSA * r)2760Sstevel@tonic-gate DH *DSA_dup_DH(const DSA *r)
2770Sstevel@tonic-gate 	{
2780Sstevel@tonic-gate 	/* DSA has p, q, g, optional pub_key, optional priv_key.
2790Sstevel@tonic-gate 	 * DH has p, optional length, g, optional pub_key, optional priv_key.
2800Sstevel@tonic-gate 	 */
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	DH *ret = NULL;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if (r == NULL)
2850Sstevel@tonic-gate 		goto err;
2860Sstevel@tonic-gate 	ret = DH_new();
2870Sstevel@tonic-gate 	if (ret == NULL)
2880Sstevel@tonic-gate 		goto err;
2890Sstevel@tonic-gate 	if (r->p != NULL)
2900Sstevel@tonic-gate 		if ((ret->p = BN_dup(r->p)) == NULL)
2910Sstevel@tonic-gate 			goto err;
2920Sstevel@tonic-gate 	if (r->q != NULL)
2930Sstevel@tonic-gate 		ret->length = BN_num_bits(r->q);
2940Sstevel@tonic-gate 	if (r->g != NULL)
2950Sstevel@tonic-gate 		if ((ret->g = BN_dup(r->g)) == NULL)
2960Sstevel@tonic-gate 			goto err;
2970Sstevel@tonic-gate 	if (r->pub_key != NULL)
2980Sstevel@tonic-gate 		if ((ret->pub_key = BN_dup(r->pub_key)) == NULL)
2990Sstevel@tonic-gate 			goto err;
3000Sstevel@tonic-gate 	if (r->priv_key != NULL)
3010Sstevel@tonic-gate 		if ((ret->priv_key = BN_dup(r->priv_key)) == NULL)
3020Sstevel@tonic-gate 			goto err;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	return ret;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate  err:
3070Sstevel@tonic-gate 	if (ret != NULL)
3080Sstevel@tonic-gate 		DH_free(ret);
3090Sstevel@tonic-gate 	return NULL;
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate #endif
312