]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_apple_csp/open_ssl/dh/dh_gen.c
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_apple_csp / open_ssl / dh / dh_gen.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_gen.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 /* We generate DH parameters as follows
82 * find a prime q which is prime_len/2 bits long.
83 * p=(2*q)+1 or (p-1)/2 = q
84 * For this case, g is a generator if
85 * g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1.
86 * Since the factors of p-1 are q and 2, we just need to check
87 * g^2 mod p != 1 and g^q mod p != 1.
88 *
89 * Having said all that,
90 * there is another special case method for the generators 2, 3 and 5.
91 * for 2, p mod 24 == 11
92 * for 3, p mod 12 == 5 <<<<< does not work for safe primes.
93 * for 5, p mod 10 == 3 or 7
94 *
95 * Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the
96 * special generators and for answering some of my questions.
97 *
98 * I've implemented the second simple method :-).
99 * Since DH should be using a safe prime (both p and q are prime),
100 * this generator function can take a very very long time to run.
101 */
102
103 DH *DH_generate_parameters(int prime_len, int generator,
104 void (*callback)(int,int,void *), void *cb_arg)
105 {
106 BIGNUM *p=NULL,*t1,*t2;
107 DH *ret=NULL;
108 int g,ok= -1;
109 BN_CTX *ctx=NULL;
110
111 ret=DH_new();
112 if (ret == NULL) goto err;
113 ctx=BN_CTX_new();
114 if (ctx == NULL) goto err;
115 BN_CTX_start(ctx);
116 t1 = BN_CTX_get(ctx);
117 t2 = BN_CTX_get(ctx);
118 if (t1 == NULL || t2 == NULL) goto err;
119
120 if (generator == DH_GENERATOR_2)
121 {
122 BN_set_word(t1,24);
123 BN_set_word(t2,11);
124 g=2;
125 }
126 #ifdef undef /* does not work for safe primes */
127 else if (generator == DH_GENERATOR_3)
128 {
129 BN_set_word(t1,12);
130 BN_set_word(t2,5);
131 g=3;
132 }
133 #endif
134 else if (generator == DH_GENERATOR_5)
135 {
136 BN_set_word(t1,10);
137 BN_set_word(t2,3);
138 /* BN_set_word(t3,7); just have to miss
139 * out on these ones :-( */
140 g=5;
141 }
142 else
143 g=generator;
144
145 p=BN_generate_prime(NULL,prime_len,1,t1,t2,callback,cb_arg);
146 if (p == NULL) goto err;
147 if (callback != NULL) callback(3,0,cb_arg);
148 ret->p=p;
149 ret->g=BN_new();
150 if (!BN_set_word(ret->g,g)) goto err;
151 ok=1;
152 err:
153 if (ok == -1)
154 {
155 DHerr(DH_F_DH_GENERATE_PARAMETERS,ERR_R_BN_LIB);
156 ok=0;
157 }
158
159 if (ctx != NULL)
160 {
161 BN_CTX_end(ctx);
162 BN_CTX_free(ctx);
163 }
164 if (!ok && (ret != NULL))
165 {
166 DH_free(ret);
167 ret=NULL;
168 }
169 return(ret);
170 }