]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_apple_csp/open_ssl/dh/dh_lib.c
Security-59306.101.1.tar.gz
[apple/security.git] / OSX / libsecurity_apple_csp / open_ssl / dh / dh_lib.c
1 /*
2 * Copyright (c) 2000-2002,2011,2014 Apple Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18 /* crypto/dh/dh_lib.c */
19 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
20 * All rights reserved.
21 *
22 * This package is an SSL implementation written
23 * by Eric Young (eay@cryptsoft.com).
24 * The implementation was written so as to conform with Netscapes SSL.
25 *
26 * This library is free for commercial and non-commercial use as long as
27 * the following conditions are aheared to. The following conditions
28 * apply to all code found in this distribution, be it the RC4, RSA,
29 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
30 * included with this distribution is covered by the same copyright terms
31 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
32 *
33 * Copyright remains Eric Young's, and as such any Copyright notices in
34 * the code are not to be removed.
35 * If this package is used in a product, Eric Young should be given attribution
36 * as the author of the parts of the library used.
37 * This can be in the form of a textual message at program startup or
38 * in documentation (online or textual) provided with the package.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * "This product includes cryptographic software written by
51 * Eric Young (eay@cryptsoft.com)"
52 * The word 'cryptographic' can be left out if the rouines from the library
53 * being used are not cryptographic related :-).
54 * 4. If you include any Windows specific code (or a derivative thereof) from
55 * the apps directory (application code) you must include an acknowledgement:
56 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
57 *
58 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * The licence and distribution terms for any publically available version or
71 * derivative of this code cannot be changed. i.e. this code cannot simply be
72 * copied and put under another distribution licence
73 * [including the GNU Public Licence.]
74 */
75
76 #include <stdio.h>
77 #include "cryptlib.h"
78 #include <openssl/bn_legacy.h>
79 #include <openssl/dh_legacy.h>
80
81 const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT;
82
83 static DH_METHOD *default_DH_method;
84 static int dh_meth_num = 0;
85 static STACK_OF(CRYPTO_EX_DATA_FUNCS) *dh_meth = NULL;
86
87 void DH_set_default_method(DH_METHOD *meth)
88 {
89 default_DH_method = meth;
90 }
91
92 DH_METHOD *DH_get_default_method(void)
93 {
94 if(!default_DH_method) default_DH_method = DH_OpenSSL();
95 return default_DH_method;
96 }
97
98 DH_METHOD *DH_set_method(DH *dh, DH_METHOD *meth)
99 {
100 DH_METHOD *mtmp;
101 mtmp = dh->meth;
102 if (mtmp->finish) mtmp->finish(dh);
103 dh->meth = meth;
104 if (meth->init) meth->init(dh);
105 return mtmp;
106 }
107
108 DH *DH_new(void)
109 {
110 return DH_new_method(NULL);
111 }
112
113 DH *DH_new_method(DH_METHOD *meth)
114 {
115 DH *ret;
116 ret=(DH *)Malloc(sizeof(DH));
117
118 if (ret == NULL)
119 {
120 DHerr(DH_F_DH_NEW,ERR_R_MALLOC_FAILURE);
121 return(NULL);
122 }
123 if(meth) ret->meth = meth;
124 else ret->meth = DH_get_default_method();
125 ret->pad=0;
126 ret->version=0;
127 ret->p=NULL;
128 ret->g=NULL;
129 ret->length=0;
130 ret->pub_key=NULL;
131 ret->priv_key=NULL;
132 ret->q=NULL;
133 ret->j=NULL;
134 ret->seed = NULL;
135 ret->seedlen = 0;
136 ret->counter = NULL;
137 ret->method_mont_p=NULL;
138 ret->references = 1;
139 ret->flags=ret->meth->flags;
140 CRYPTO_new_ex_data(dh_meth,ret,&ret->ex_data);
141 if ((ret->meth->init != NULL) && !ret->meth->init(ret))
142 {
143 CRYPTO_free_ex_data(dh_meth,ret,&ret->ex_data);
144 Free(ret);
145 ret=NULL;
146 }
147 return(ret);
148 }
149
150 void DH_free(DH *r)
151 {
152 int i;
153 if(r == NULL) return;
154 i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH);
155 #ifdef REF_PRINT
156 REF_PRINT("DH",r);
157 #endif
158 if (i > 0) return;
159 #ifdef REF_CHECK
160 if (i < 0)
161 {
162 fprintf(stderr,"DH_free, bad reference count\n");
163 abort();
164 }
165 #endif
166
167 if(r->meth->finish) r->meth->finish(r);
168
169 CRYPTO_free_ex_data(dh_meth, r, &r->ex_data);
170
171 if (r->p != NULL) BN_clear_free(r->p);
172 if (r->g != NULL) BN_clear_free(r->g);
173 if (r->q != NULL) BN_clear_free(r->q);
174 if (r->j != NULL) BN_clear_free(r->j);
175 if (r->seed) Free(r->seed);
176 if (r->counter != NULL) BN_clear_free(r->counter);
177 if (r->pub_key != NULL) BN_clear_free(r->pub_key);
178 if (r->priv_key != NULL) BN_clear_free(r->priv_key);
179 Free(r);
180 }
181
182 int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
183 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
184 {
185 dh_meth_num++;
186 return(CRYPTO_get_ex_new_index(dh_meth_num-1,
187 &dh_meth,argl,argp,new_func,dup_func,free_func));
188 }
189
190 int DH_set_ex_data(DH *d, int idx, void *arg)
191 {
192 return(CRYPTO_set_ex_data(&d->ex_data,idx,arg));
193 }
194
195 void *DH_get_ex_data(DH *d, int idx)
196 {
197 return(CRYPTO_get_ex_data(&d->ex_data,idx));
198 }
199
200 int DH_size(DH *dh)
201 {
202 return(BN_num_bytes(dh->p));
203 }