os/ossrv/ssl/libcrypto/src/crypto/dsa/dsa_lib.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/dsa/dsa_lib.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
sl@0
    59
/* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
sl@0
    60
/*
sl@0
    61
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    62
 */
sl@0
    63
sl@0
    64
sl@0
    65
#include <stdio.h>
sl@0
    66
#include "cryptlib.h"
sl@0
    67
#include <openssl/bn.h>
sl@0
    68
#include <openssl/dsa.h>
sl@0
    69
#include <openssl/asn1.h>
sl@0
    70
#ifndef OPENSSL_NO_ENGINE
sl@0
    71
#include <openssl/engine.h>
sl@0
    72
#endif
sl@0
    73
#ifndef OPENSSL_NO_DH
sl@0
    74
#include <openssl/dh.h>
sl@0
    75
#endif
sl@0
    76
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    77
#include "libcrypto_wsd_macros.h"
sl@0
    78
#include "libcrypto_wsd.h"
sl@0
    79
#endif
sl@0
    80
sl@0
    81
const char DSA_version[]="DSA" OPENSSL_VERSION_PTEXT;
sl@0
    82
sl@0
    83
#ifndef EMULATOR
sl@0
    84
static const DSA_METHOD *default_DSA_method = NULL;
sl@0
    85
#else
sl@0
    86
GET_STATIC_VAR_FROM_TLS(default_DSA_method,dsa_lib,const DSA_METHOD *)
sl@0
    87
#define default_DSA_method (*GET_WSD_VAR_NAME(default_DSA_method,dsa_lib, s)())
sl@0
    88
#endif
sl@0
    89
sl@0
    90
EXPORT_C void DSA_set_default_method(const DSA_METHOD *meth)
sl@0
    91
	{
sl@0
    92
	default_DSA_method = meth;
sl@0
    93
	}
sl@0
    94
sl@0
    95
EXPORT_C const DSA_METHOD *DSA_get_default_method(void)
sl@0
    96
	{
sl@0
    97
	if(!default_DSA_method)
sl@0
    98
		default_DSA_method = DSA_OpenSSL();
sl@0
    99
	return default_DSA_method;
sl@0
   100
	}
sl@0
   101
sl@0
   102
EXPORT_C DSA *DSA_new(void)
sl@0
   103
	{
sl@0
   104
	return DSA_new_method(NULL);
sl@0
   105
	}
sl@0
   106
sl@0
   107
EXPORT_C int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
sl@0
   108
	{
sl@0
   109
	/* NB: The caller is specifically setting a method, so it's not up to us
sl@0
   110
	 * to deal with which ENGINE it comes from. */
sl@0
   111
        const DSA_METHOD *mtmp;
sl@0
   112
        mtmp = dsa->meth;
sl@0
   113
        if (mtmp->finish) mtmp->finish(dsa);
sl@0
   114
#ifndef OPENSSL_NO_ENGINE
sl@0
   115
	if (dsa->engine)
sl@0
   116
		{
sl@0
   117
		ENGINE_finish(dsa->engine);
sl@0
   118
		dsa->engine = NULL;
sl@0
   119
		}
sl@0
   120
#endif
sl@0
   121
        dsa->meth = meth;
sl@0
   122
        if (meth->init) meth->init(dsa);
sl@0
   123
        return 1;
sl@0
   124
	}
sl@0
   125
sl@0
   126
EXPORT_C DSA *DSA_new_method(ENGINE *engine)
sl@0
   127
	{
sl@0
   128
	DSA *ret;
sl@0
   129
sl@0
   130
	ret=(DSA *)OPENSSL_malloc(sizeof(DSA));
sl@0
   131
	if (ret == NULL)
sl@0
   132
		{
sl@0
   133
		DSAerr(DSA_F_DSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
sl@0
   134
		return(NULL);
sl@0
   135
		}
sl@0
   136
	ret->meth = DSA_get_default_method();
sl@0
   137
#ifndef OPENSSL_NO_ENGINE
sl@0
   138
	if (engine)
sl@0
   139
		{
sl@0
   140
		if (!ENGINE_init(engine))
sl@0
   141
			{
sl@0
   142
			DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
sl@0
   143
			OPENSSL_free(ret);
sl@0
   144
			return NULL;
sl@0
   145
			}
sl@0
   146
		ret->engine = engine;
sl@0
   147
		}
sl@0
   148
	else
sl@0
   149
		ret->engine = ENGINE_get_default_DSA();
sl@0
   150
	if(ret->engine)
sl@0
   151
		{
sl@0
   152
		ret->meth = ENGINE_get_DSA(ret->engine);
sl@0
   153
		if(!ret->meth)
sl@0
   154
			{
sl@0
   155
			DSAerr(DSA_F_DSA_NEW_METHOD,
sl@0
   156
				ERR_R_ENGINE_LIB);
sl@0
   157
			ENGINE_finish(ret->engine);
sl@0
   158
			OPENSSL_free(ret);
sl@0
   159
			return NULL;
sl@0
   160
			}
sl@0
   161
		}
sl@0
   162
#endif
sl@0
   163
sl@0
   164
	ret->pad=0;
sl@0
   165
	ret->version=0;
sl@0
   166
	ret->write_params=1;
sl@0
   167
	ret->p=NULL;
sl@0
   168
	ret->q=NULL;
sl@0
   169
	ret->g=NULL;
sl@0
   170
sl@0
   171
	ret->pub_key=NULL;
sl@0
   172
	ret->priv_key=NULL;
sl@0
   173
sl@0
   174
	ret->kinv=NULL;
sl@0
   175
	ret->r=NULL;
sl@0
   176
	ret->method_mont_p=NULL;
sl@0
   177
sl@0
   178
	ret->references=1;
sl@0
   179
	ret->flags=ret->meth->flags;
sl@0
   180
	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
sl@0
   181
	if ((ret->meth->init != NULL) && !ret->meth->init(ret))
sl@0
   182
		{
sl@0
   183
#ifndef OPENSSL_NO_ENGINE
sl@0
   184
		if (ret->engine)
sl@0
   185
			ENGINE_finish(ret->engine);
sl@0
   186
#endif
sl@0
   187
		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
sl@0
   188
		OPENSSL_free(ret);
sl@0
   189
		ret=NULL;
sl@0
   190
		}
sl@0
   191
	
sl@0
   192
	return(ret);
sl@0
   193
	}
sl@0
   194
sl@0
   195
EXPORT_C void DSA_free(DSA *r)
sl@0
   196
	{
sl@0
   197
	int i;
sl@0
   198
sl@0
   199
	if (r == NULL) return;
sl@0
   200
sl@0
   201
	i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_DSA);
sl@0
   202
#ifdef REF_PRINT
sl@0
   203
	REF_PRINT("DSA",r);
sl@0
   204
#endif
sl@0
   205
	if (i > 0) return;
sl@0
   206
#ifdef REF_CHECK
sl@0
   207
	if (i < 0)
sl@0
   208
		{
sl@0
   209
		fprintf(stderr,"DSA_free, bad reference count\n");
sl@0
   210
		abort();
sl@0
   211
		}
sl@0
   212
#endif
sl@0
   213
sl@0
   214
	if(r->meth->finish)
sl@0
   215
		r->meth->finish(r);
sl@0
   216
#ifndef OPENSSL_NO_ENGINE
sl@0
   217
	if(r->engine)
sl@0
   218
		ENGINE_finish(r->engine);
sl@0
   219
#endif
sl@0
   220
sl@0
   221
	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
sl@0
   222
sl@0
   223
	if (r->p != NULL) BN_clear_free(r->p);
sl@0
   224
	if (r->q != NULL) BN_clear_free(r->q);
sl@0
   225
	if (r->g != NULL) BN_clear_free(r->g);
sl@0
   226
	if (r->pub_key != NULL) BN_clear_free(r->pub_key);
sl@0
   227
	if (r->priv_key != NULL) BN_clear_free(r->priv_key);
sl@0
   228
	if (r->kinv != NULL) BN_clear_free(r->kinv);
sl@0
   229
	if (r->r != NULL) BN_clear_free(r->r);
sl@0
   230
	OPENSSL_free(r);
sl@0
   231
	}
sl@0
   232
sl@0
   233
EXPORT_C int DSA_up_ref(DSA *r)
sl@0
   234
	{
sl@0
   235
	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA);
sl@0
   236
#ifdef REF_PRINT
sl@0
   237
	REF_PRINT("DSA",r);
sl@0
   238
#endif
sl@0
   239
#ifdef REF_CHECK
sl@0
   240
	if (i < 2)
sl@0
   241
		{
sl@0
   242
		fprintf(stderr, "DSA_up_ref, bad reference count\n");
sl@0
   243
		abort();
sl@0
   244
		}
sl@0
   245
#endif
sl@0
   246
	return ((i > 1) ? 1 : 0);
sl@0
   247
	}
sl@0
   248
sl@0
   249
EXPORT_C int DSA_size(const DSA *r)
sl@0
   250
	{
sl@0
   251
	int ret,i;
sl@0
   252
	ASN1_INTEGER bs;
sl@0
   253
	unsigned char buf[4];	/* 4 bytes looks really small.
sl@0
   254
				   However, i2d_ASN1_INTEGER() will not look
sl@0
   255
				   beyond the first byte, as long as the second
sl@0
   256
				   parameter is NULL. */
sl@0
   257
sl@0
   258
	i=BN_num_bits(r->q);
sl@0
   259
	bs.length=(i+7)/8;
sl@0
   260
	bs.data=buf;
sl@0
   261
	bs.type=V_ASN1_INTEGER;
sl@0
   262
	/* If the top bit is set the asn1 encoding is 1 larger. */
sl@0
   263
	buf[0]=0xff;	
sl@0
   264
sl@0
   265
	i=i2d_ASN1_INTEGER(&bs,NULL);
sl@0
   266
	i+=i; /* r and s */
sl@0
   267
	ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE);
sl@0
   268
	return(ret);
sl@0
   269
	}
sl@0
   270
sl@0
   271
EXPORT_C int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
sl@0
   272
	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
sl@0
   273
        {
sl@0
   274
	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp,
sl@0
   275
				new_func, dup_func, free_func);
sl@0
   276
        }
sl@0
   277
sl@0
   278
EXPORT_C int DSA_set_ex_data(DSA *d, int idx, void *arg)
sl@0
   279
	{
sl@0
   280
	return(CRYPTO_set_ex_data(&d->ex_data,idx,arg));
sl@0
   281
	}
sl@0
   282
sl@0
   283
EXPORT_C void *DSA_get_ex_data(DSA *d, int idx)
sl@0
   284
	{
sl@0
   285
	return(CRYPTO_get_ex_data(&d->ex_data,idx));
sl@0
   286
	}
sl@0
   287
sl@0
   288
#ifndef OPENSSL_NO_DH
sl@0
   289
EXPORT_C DH *DSA_dup_DH(const DSA *r)
sl@0
   290
	{
sl@0
   291
	/* DSA has p, q, g, optional pub_key, optional priv_key.
sl@0
   292
	 * DH has p, optional length, g, optional pub_key, optional priv_key.
sl@0
   293
	 */ 
sl@0
   294
sl@0
   295
	DH *ret = NULL;
sl@0
   296
sl@0
   297
	if (r == NULL)
sl@0
   298
		goto err;
sl@0
   299
	ret = DH_new();
sl@0
   300
	if (ret == NULL)
sl@0
   301
		goto err;
sl@0
   302
	if (r->p != NULL) 
sl@0
   303
		if ((ret->p = BN_dup(r->p)) == NULL)
sl@0
   304
			goto err;
sl@0
   305
	if (r->q != NULL)
sl@0
   306
		ret->length = BN_num_bits(r->q);
sl@0
   307
	if (r->g != NULL)
sl@0
   308
		if ((ret->g = BN_dup(r->g)) == NULL)
sl@0
   309
			goto err;
sl@0
   310
	if (r->pub_key != NULL)
sl@0
   311
		if ((ret->pub_key = BN_dup(r->pub_key)) == NULL)
sl@0
   312
			goto err;
sl@0
   313
	if (r->priv_key != NULL)
sl@0
   314
		if ((ret->priv_key = BN_dup(r->priv_key)) == NULL)
sl@0
   315
			goto err;
sl@0
   316
sl@0
   317
	return ret;
sl@0
   318
sl@0
   319
 err:
sl@0
   320
	if (ret != NULL)
sl@0
   321
		DH_free(ret);
sl@0
   322
	return NULL;
sl@0
   323
	}
sl@0
   324
#endif