os/ossrv/ssl/libcrypto/src/crypto/dsa/dsa_lib.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/dsa/dsa_lib.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,324 @@
     1.4 +/* crypto/dsa/dsa_lib.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +
    1.62 +/* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
    1.63 +/*
    1.64 + © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    1.65 + */
    1.66 +
    1.67 +
    1.68 +#include <stdio.h>
    1.69 +#include "cryptlib.h"
    1.70 +#include <openssl/bn.h>
    1.71 +#include <openssl/dsa.h>
    1.72 +#include <openssl/asn1.h>
    1.73 +#ifndef OPENSSL_NO_ENGINE
    1.74 +#include <openssl/engine.h>
    1.75 +#endif
    1.76 +#ifndef OPENSSL_NO_DH
    1.77 +#include <openssl/dh.h>
    1.78 +#endif
    1.79 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
    1.80 +#include "libcrypto_wsd_macros.h"
    1.81 +#include "libcrypto_wsd.h"
    1.82 +#endif
    1.83 +
    1.84 +const char DSA_version[]="DSA" OPENSSL_VERSION_PTEXT;
    1.85 +
    1.86 +#ifndef EMULATOR
    1.87 +static const DSA_METHOD *default_DSA_method = NULL;
    1.88 +#else
    1.89 +GET_STATIC_VAR_FROM_TLS(default_DSA_method,dsa_lib,const DSA_METHOD *)
    1.90 +#define default_DSA_method (*GET_WSD_VAR_NAME(default_DSA_method,dsa_lib, s)())
    1.91 +#endif
    1.92 +
    1.93 +EXPORT_C void DSA_set_default_method(const DSA_METHOD *meth)
    1.94 +	{
    1.95 +	default_DSA_method = meth;
    1.96 +	}
    1.97 +
    1.98 +EXPORT_C const DSA_METHOD *DSA_get_default_method(void)
    1.99 +	{
   1.100 +	if(!default_DSA_method)
   1.101 +		default_DSA_method = DSA_OpenSSL();
   1.102 +	return default_DSA_method;
   1.103 +	}
   1.104 +
   1.105 +EXPORT_C DSA *DSA_new(void)
   1.106 +	{
   1.107 +	return DSA_new_method(NULL);
   1.108 +	}
   1.109 +
   1.110 +EXPORT_C int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
   1.111 +	{
   1.112 +	/* NB: The caller is specifically setting a method, so it's not up to us
   1.113 +	 * to deal with which ENGINE it comes from. */
   1.114 +        const DSA_METHOD *mtmp;
   1.115 +        mtmp = dsa->meth;
   1.116 +        if (mtmp->finish) mtmp->finish(dsa);
   1.117 +#ifndef OPENSSL_NO_ENGINE
   1.118 +	if (dsa->engine)
   1.119 +		{
   1.120 +		ENGINE_finish(dsa->engine);
   1.121 +		dsa->engine = NULL;
   1.122 +		}
   1.123 +#endif
   1.124 +        dsa->meth = meth;
   1.125 +        if (meth->init) meth->init(dsa);
   1.126 +        return 1;
   1.127 +	}
   1.128 +
   1.129 +EXPORT_C DSA *DSA_new_method(ENGINE *engine)
   1.130 +	{
   1.131 +	DSA *ret;
   1.132 +
   1.133 +	ret=(DSA *)OPENSSL_malloc(sizeof(DSA));
   1.134 +	if (ret == NULL)
   1.135 +		{
   1.136 +		DSAerr(DSA_F_DSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
   1.137 +		return(NULL);
   1.138 +		}
   1.139 +	ret->meth = DSA_get_default_method();
   1.140 +#ifndef OPENSSL_NO_ENGINE
   1.141 +	if (engine)
   1.142 +		{
   1.143 +		if (!ENGINE_init(engine))
   1.144 +			{
   1.145 +			DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
   1.146 +			OPENSSL_free(ret);
   1.147 +			return NULL;
   1.148 +			}
   1.149 +		ret->engine = engine;
   1.150 +		}
   1.151 +	else
   1.152 +		ret->engine = ENGINE_get_default_DSA();
   1.153 +	if(ret->engine)
   1.154 +		{
   1.155 +		ret->meth = ENGINE_get_DSA(ret->engine);
   1.156 +		if(!ret->meth)
   1.157 +			{
   1.158 +			DSAerr(DSA_F_DSA_NEW_METHOD,
   1.159 +				ERR_R_ENGINE_LIB);
   1.160 +			ENGINE_finish(ret->engine);
   1.161 +			OPENSSL_free(ret);
   1.162 +			return NULL;
   1.163 +			}
   1.164 +		}
   1.165 +#endif
   1.166 +
   1.167 +	ret->pad=0;
   1.168 +	ret->version=0;
   1.169 +	ret->write_params=1;
   1.170 +	ret->p=NULL;
   1.171 +	ret->q=NULL;
   1.172 +	ret->g=NULL;
   1.173 +
   1.174 +	ret->pub_key=NULL;
   1.175 +	ret->priv_key=NULL;
   1.176 +
   1.177 +	ret->kinv=NULL;
   1.178 +	ret->r=NULL;
   1.179 +	ret->method_mont_p=NULL;
   1.180 +
   1.181 +	ret->references=1;
   1.182 +	ret->flags=ret->meth->flags;
   1.183 +	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
   1.184 +	if ((ret->meth->init != NULL) && !ret->meth->init(ret))
   1.185 +		{
   1.186 +#ifndef OPENSSL_NO_ENGINE
   1.187 +		if (ret->engine)
   1.188 +			ENGINE_finish(ret->engine);
   1.189 +#endif
   1.190 +		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
   1.191 +		OPENSSL_free(ret);
   1.192 +		ret=NULL;
   1.193 +		}
   1.194 +	
   1.195 +	return(ret);
   1.196 +	}
   1.197 +
   1.198 +EXPORT_C void DSA_free(DSA *r)
   1.199 +	{
   1.200 +	int i;
   1.201 +
   1.202 +	if (r == NULL) return;
   1.203 +
   1.204 +	i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_DSA);
   1.205 +#ifdef REF_PRINT
   1.206 +	REF_PRINT("DSA",r);
   1.207 +#endif
   1.208 +	if (i > 0) return;
   1.209 +#ifdef REF_CHECK
   1.210 +	if (i < 0)
   1.211 +		{
   1.212 +		fprintf(stderr,"DSA_free, bad reference count\n");
   1.213 +		abort();
   1.214 +		}
   1.215 +#endif
   1.216 +
   1.217 +	if(r->meth->finish)
   1.218 +		r->meth->finish(r);
   1.219 +#ifndef OPENSSL_NO_ENGINE
   1.220 +	if(r->engine)
   1.221 +		ENGINE_finish(r->engine);
   1.222 +#endif
   1.223 +
   1.224 +	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
   1.225 +
   1.226 +	if (r->p != NULL) BN_clear_free(r->p);
   1.227 +	if (r->q != NULL) BN_clear_free(r->q);
   1.228 +	if (r->g != NULL) BN_clear_free(r->g);
   1.229 +	if (r->pub_key != NULL) BN_clear_free(r->pub_key);
   1.230 +	if (r->priv_key != NULL) BN_clear_free(r->priv_key);
   1.231 +	if (r->kinv != NULL) BN_clear_free(r->kinv);
   1.232 +	if (r->r != NULL) BN_clear_free(r->r);
   1.233 +	OPENSSL_free(r);
   1.234 +	}
   1.235 +
   1.236 +EXPORT_C int DSA_up_ref(DSA *r)
   1.237 +	{
   1.238 +	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA);
   1.239 +#ifdef REF_PRINT
   1.240 +	REF_PRINT("DSA",r);
   1.241 +#endif
   1.242 +#ifdef REF_CHECK
   1.243 +	if (i < 2)
   1.244 +		{
   1.245 +		fprintf(stderr, "DSA_up_ref, bad reference count\n");
   1.246 +		abort();
   1.247 +		}
   1.248 +#endif
   1.249 +	return ((i > 1) ? 1 : 0);
   1.250 +	}
   1.251 +
   1.252 +EXPORT_C int DSA_size(const DSA *r)
   1.253 +	{
   1.254 +	int ret,i;
   1.255 +	ASN1_INTEGER bs;
   1.256 +	unsigned char buf[4];	/* 4 bytes looks really small.
   1.257 +				   However, i2d_ASN1_INTEGER() will not look
   1.258 +				   beyond the first byte, as long as the second
   1.259 +				   parameter is NULL. */
   1.260 +
   1.261 +	i=BN_num_bits(r->q);
   1.262 +	bs.length=(i+7)/8;
   1.263 +	bs.data=buf;
   1.264 +	bs.type=V_ASN1_INTEGER;
   1.265 +	/* If the top bit is set the asn1 encoding is 1 larger. */
   1.266 +	buf[0]=0xff;	
   1.267 +
   1.268 +	i=i2d_ASN1_INTEGER(&bs,NULL);
   1.269 +	i+=i; /* r and s */
   1.270 +	ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE);
   1.271 +	return(ret);
   1.272 +	}
   1.273 +
   1.274 +EXPORT_C int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
   1.275 +	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
   1.276 +        {
   1.277 +	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp,
   1.278 +				new_func, dup_func, free_func);
   1.279 +        }
   1.280 +
   1.281 +EXPORT_C int DSA_set_ex_data(DSA *d, int idx, void *arg)
   1.282 +	{
   1.283 +	return(CRYPTO_set_ex_data(&d->ex_data,idx,arg));
   1.284 +	}
   1.285 +
   1.286 +EXPORT_C void *DSA_get_ex_data(DSA *d, int idx)
   1.287 +	{
   1.288 +	return(CRYPTO_get_ex_data(&d->ex_data,idx));
   1.289 +	}
   1.290 +
   1.291 +#ifndef OPENSSL_NO_DH
   1.292 +EXPORT_C DH *DSA_dup_DH(const DSA *r)
   1.293 +	{
   1.294 +	/* DSA has p, q, g, optional pub_key, optional priv_key.
   1.295 +	 * DH has p, optional length, g, optional pub_key, optional priv_key.
   1.296 +	 */ 
   1.297 +
   1.298 +	DH *ret = NULL;
   1.299 +
   1.300 +	if (r == NULL)
   1.301 +		goto err;
   1.302 +	ret = DH_new();
   1.303 +	if (ret == NULL)
   1.304 +		goto err;
   1.305 +	if (r->p != NULL) 
   1.306 +		if ((ret->p = BN_dup(r->p)) == NULL)
   1.307 +			goto err;
   1.308 +	if (r->q != NULL)
   1.309 +		ret->length = BN_num_bits(r->q);
   1.310 +	if (r->g != NULL)
   1.311 +		if ((ret->g = BN_dup(r->g)) == NULL)
   1.312 +			goto err;
   1.313 +	if (r->pub_key != NULL)
   1.314 +		if ((ret->pub_key = BN_dup(r->pub_key)) == NULL)
   1.315 +			goto err;
   1.316 +	if (r->priv_key != NULL)
   1.317 +		if ((ret->priv_key = BN_dup(r->priv_key)) == NULL)
   1.318 +			goto err;
   1.319 +
   1.320 +	return ret;
   1.321 +
   1.322 + err:
   1.323 +	if (ret != NULL)
   1.324 +		DH_free(ret);
   1.325 +	return NULL;
   1.326 +	}
   1.327 +#endif