]>
git.saurik.com Git - apple/security.git/blob - AppleCSP/open_ssl/dh/dh_lib.c
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All Rights Reserved.
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
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.
18 /* crypto/dh/dh_lib.c */
19 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
20 * All rights reserved.
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.
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).
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.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
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)"
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
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.]
78 #include <openssl/bn.h>
79 #include <openssl/dh.h>
81 const char *DH_version
="Diffie-Hellman" OPENSSL_VERSION_PTEXT
;
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
;
87 void DH_set_default_method(DH_METHOD
*meth
)
89 default_DH_method
= meth
;
92 DH_METHOD
*DH_get_default_method(void)
94 if(!default_DH_method
) default_DH_method
= DH_OpenSSL();
95 return default_DH_method
;
98 DH_METHOD
*DH_set_method(DH
*dh
, DH_METHOD
*meth
)
102 if (mtmp
->finish
) mtmp
->finish(dh
);
104 if (meth
->init
) meth
->init(dh
);
110 return DH_new_method(NULL
);
113 DH
*DH_new_method(DH_METHOD
*meth
)
116 ret
=(DH
*)Malloc(sizeof(DH
));
120 DHerr(DH_F_DH_NEW
,ERR_R_MALLOC_FAILURE
);
123 if(meth
) ret
->meth
= meth
;
124 else ret
->meth
= DH_get_default_method();
137 ret
->method_mont_p
=NULL
;
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
))
143 CRYPTO_free_ex_data(dh_meth
,ret
,&ret
->ex_data
);
153 if(r
== NULL
) return;
154 i
= CRYPTO_add(&r
->references
, -1, CRYPTO_LOCK_DH
);
162 fprintf(stderr
,"DH_free, bad reference count\n");
167 if(r
->meth
->finish
) r
->meth
->finish(r
);
169 CRYPTO_free_ex_data(dh_meth
, r
, &r
->ex_data
);
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
);
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
)
186 return(CRYPTO_get_ex_new_index(dh_meth_num
-1,
187 &dh_meth
,argl
,argp
,new_func
,dup_func
,free_func
));
190 int DH_set_ex_data(DH
*d
, int idx
, void *arg
)
192 return(CRYPTO_set_ex_data(&d
->ex_data
,idx
,arg
));
195 void *DH_get_ex_data(DH
*d
, int idx
)
197 return(CRYPTO_get_ex_data(&d
->ex_data
,idx
));
202 return(BN_num_bytes(dh
->p
));