1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/dh/dh_lib.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,259 @@
1.4 +/* crypto/dh/dh_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 + © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
1.63 + */
1.64 +
1.65 +#include <stdio.h>
1.66 +#include "cryptlib.h"
1.67 +#include <openssl/bn.h>
1.68 +#include <openssl/dh.h>
1.69 +#ifndef OPENSSL_NO_ENGINE
1.70 +#include <openssl/engine.h>
1.71 +#endif
1.72 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
1.73 +#include "libcrypto_wsd_macros.h"
1.74 +#include "libcrypto_wsd.h"
1.75 +#endif
1.76 +
1.77 +const char DH_version[]="Diffie-Hellman" OPENSSL_VERSION_PTEXT;
1.78 +
1.79 +#ifndef EMULATOR
1.80 +static const DH_METHOD *default_DH_method = NULL;
1.81 +#else
1.82 +GET_STATIC_VAR_FROM_TLS(default_DH_method,dh_lib,const DH_METHOD *)
1.83 +#define default_DH_method (*GET_WSD_VAR_NAME(default_DH_method,dh_lib, s)())
1.84 +#endif
1.85 +
1.86 +EXPORT_C void DH_set_default_method(const DH_METHOD *meth)
1.87 + {
1.88 + default_DH_method = meth;
1.89 + }
1.90 +
1.91 +EXPORT_C const DH_METHOD *DH_get_default_method(void)
1.92 + {
1.93 + if(!default_DH_method)
1.94 + default_DH_method = DH_OpenSSL();
1.95 + return default_DH_method;
1.96 + }
1.97 +
1.98 +EXPORT_C int DH_set_method(DH *dh, const DH_METHOD *meth)
1.99 + {
1.100 + /* NB: The caller is specifically setting a method, so it's not up to us
1.101 + * to deal with which ENGINE it comes from. */
1.102 + const DH_METHOD *mtmp;
1.103 + mtmp = dh->meth;
1.104 + if (mtmp->finish) mtmp->finish(dh);
1.105 +#ifndef OPENSSL_NO_ENGINE
1.106 + if (dh->engine)
1.107 + {
1.108 + ENGINE_finish(dh->engine);
1.109 + dh->engine = NULL;
1.110 + }
1.111 +#endif
1.112 + dh->meth = meth;
1.113 + if (meth->init) meth->init(dh);
1.114 + return 1;
1.115 + }
1.116 +
1.117 +EXPORT_C DH *DH_new(void)
1.118 + {
1.119 + return DH_new_method(NULL);
1.120 + }
1.121 +
1.122 +EXPORT_C DH *DH_new_method(ENGINE *engine)
1.123 + {
1.124 + DH *ret;
1.125 +
1.126 + ret=(DH *)OPENSSL_malloc(sizeof(DH));
1.127 + if (ret == NULL)
1.128 + {
1.129 + DHerr(DH_F_DH_NEW_METHOD,ERR_R_MALLOC_FAILURE);
1.130 + return(NULL);
1.131 + }
1.132 +
1.133 + ret->meth = DH_get_default_method();
1.134 +#ifndef OPENSSL_NO_ENGINE
1.135 + if (engine)
1.136 + {
1.137 + if (!ENGINE_init(engine))
1.138 + {
1.139 + DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
1.140 + OPENSSL_free(ret);
1.141 + return NULL;
1.142 + }
1.143 + ret->engine = engine;
1.144 + }
1.145 + else
1.146 + ret->engine = ENGINE_get_default_DH();
1.147 + if(ret->engine)
1.148 + {
1.149 + ret->meth = ENGINE_get_DH(ret->engine);
1.150 + if(!ret->meth)
1.151 + {
1.152 + DHerr(DH_F_DH_NEW_METHOD,ERR_R_ENGINE_LIB);
1.153 + ENGINE_finish(ret->engine);
1.154 + OPENSSL_free(ret);
1.155 + return NULL;
1.156 + }
1.157 + }
1.158 +#endif
1.159 +
1.160 + ret->pad=0;
1.161 + ret->version=0;
1.162 + ret->p=NULL;
1.163 + ret->g=NULL;
1.164 + ret->length=0;
1.165 + ret->pub_key=NULL;
1.166 + ret->priv_key=NULL;
1.167 + ret->q=NULL;
1.168 + ret->j=NULL;
1.169 + ret->seed = NULL;
1.170 + ret->seedlen = 0;
1.171 + ret->counter = NULL;
1.172 + ret->method_mont_p=NULL;
1.173 + ret->references = 1;
1.174 + ret->flags=ret->meth->flags;
1.175 + CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
1.176 + if ((ret->meth->init != NULL) && !ret->meth->init(ret))
1.177 + {
1.178 +#ifndef OPENSSL_NO_ENGINE
1.179 + if (ret->engine)
1.180 + ENGINE_finish(ret->engine);
1.181 +#endif
1.182 + CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
1.183 + OPENSSL_free(ret);
1.184 + ret=NULL;
1.185 + }
1.186 + return(ret);
1.187 + }
1.188 +
1.189 +EXPORT_C void DH_free(DH *r)
1.190 + {
1.191 + int i;
1.192 + if(r == NULL) return;
1.193 + i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH);
1.194 +#ifdef REF_PRINT
1.195 + REF_PRINT("DH",r);
1.196 +#endif
1.197 + if (i > 0) return;
1.198 +#ifdef REF_CHECK
1.199 + if (i < 0)
1.200 + {
1.201 + fprintf(stderr,"DH_free, bad reference count\n");
1.202 + abort();
1.203 + }
1.204 +#endif
1.205 +
1.206 + if (r->meth->finish)
1.207 + r->meth->finish(r);
1.208 +#ifndef OPENSSL_NO_ENGINE
1.209 + if (r->engine)
1.210 + ENGINE_finish(r->engine);
1.211 +#endif
1.212 +
1.213 + CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
1.214 +
1.215 + if (r->p != NULL) BN_clear_free(r->p);
1.216 + if (r->g != NULL) BN_clear_free(r->g);
1.217 + if (r->q != NULL) BN_clear_free(r->q);
1.218 + if (r->j != NULL) BN_clear_free(r->j);
1.219 + if (r->seed) OPENSSL_free(r->seed);
1.220 + if (r->counter != NULL) BN_clear_free(r->counter);
1.221 + if (r->pub_key != NULL) BN_clear_free(r->pub_key);
1.222 + if (r->priv_key != NULL) BN_clear_free(r->priv_key);
1.223 + OPENSSL_free(r);
1.224 + }
1.225 +
1.226 +EXPORT_C int DH_up_ref(DH *r)
1.227 + {
1.228 + int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH);
1.229 +#ifdef REF_PRINT
1.230 + REF_PRINT("DH",r);
1.231 +#endif
1.232 +#ifdef REF_CHECK
1.233 + if (i < 2)
1.234 + {
1.235 + fprintf(stderr, "DH_up, bad reference count\n");
1.236 + abort();
1.237 + }
1.238 +#endif
1.239 + return ((i > 1) ? 1 : 0);
1.240 + }
1.241 +
1.242 +EXPORT_C int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
1.243 + CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
1.244 + {
1.245 + return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp,
1.246 + new_func, dup_func, free_func);
1.247 + }
1.248 +
1.249 +EXPORT_C int DH_set_ex_data(DH *d, int idx, void *arg)
1.250 + {
1.251 + return(CRYPTO_set_ex_data(&d->ex_data,idx,arg));
1.252 + }
1.253 +
1.254 +EXPORT_C void *DH_get_ex_data(DH *d, int idx)
1.255 + {
1.256 + return(CRYPTO_get_ex_data(&d->ex_data,idx));
1.257 + }
1.258 +
1.259 +EXPORT_C int DH_size(const DH *dh)
1.260 + {
1.261 + return(BN_num_bytes(dh->p));
1.262 + }