summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/dsa/dsa_gen.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/dsa/dsa_gen.c')
-rw-r--r--src/lib/libcrypto/dsa/dsa_gen.c111
1 files changed, 47 insertions, 64 deletions
diff --git a/src/lib/libcrypto/dsa/dsa_gen.c b/src/lib/libcrypto/dsa/dsa_gen.c
index ca0b86a6cf..e40afeea51 100644
--- a/src/lib/libcrypto/dsa/dsa_gen.c
+++ b/src/lib/libcrypto/dsa/dsa_gen.c
@@ -69,8 +69,6 @@
69#define HASH EVP_sha1() 69#define HASH EVP_sha1()
70#endif 70#endif
71 71
72#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_SHA is defined */
73
74#ifndef OPENSSL_NO_SHA 72#ifndef OPENSSL_NO_SHA
75 73
76#include <stdio.h> 74#include <stdio.h>
@@ -82,24 +80,12 @@
82#include <openssl/rand.h> 80#include <openssl/rand.h>
83#include <openssl/sha.h> 81#include <openssl/sha.h>
84 82
85static int dsa_builtin_paramgen(DSA *ret, int bits, 83#ifndef OPENSSL_FIPS
86 unsigned char *seed_in, int seed_len, 84DSA *DSA_generate_parameters(int bits,
87 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb);
88
89int DSA_generate_parameters_ex(DSA *ret, int bits,
90 unsigned char *seed_in, int seed_len,
91 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
92 {
93 if(ret->meth->dsa_paramgen)
94 return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len,
95 counter_ret, h_ret, cb);
96 return dsa_builtin_paramgen(ret, bits, seed_in, seed_len,
97 counter_ret, h_ret, cb);
98 }
99
100static int dsa_builtin_paramgen(DSA *ret, int bits,
101 unsigned char *seed_in, int seed_len, 85 unsigned char *seed_in, int seed_len,
102 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb) 86 int *counter_ret, unsigned long *h_ret,
87 void (*callback)(int, int, void *),
88 void *cb_arg)
103 { 89 {
104 int ok=0; 90 int ok=0;
105 unsigned char seed[SHA_DIGEST_LENGTH]; 91 unsigned char seed[SHA_DIGEST_LENGTH];
@@ -111,43 +97,40 @@ static int dsa_builtin_paramgen(DSA *ret, int bits,
111 int k,n=0,i,b,m=0; 97 int k,n=0,i,b,m=0;
112 int counter=0; 98 int counter=0;
113 int r=0; 99 int r=0;
114 BN_CTX *ctx=NULL; 100 BN_CTX *ctx=NULL,*ctx2=NULL,*ctx3=NULL;
115 unsigned int h=2; 101 unsigned int h=2;
102 DSA *ret=NULL;
116 103
117 if (bits < 512) bits=512; 104 if (bits < 512) bits=512;
118 bits=(bits+63)/64*64; 105 bits=(bits+63)/64*64;
119 106
120 /* NB: seed_len == 0 is special case: copy generated seed to 107 if (seed_len < 20)
121 * seed_in if it is not NULL.
122 */
123 if (seed_len && (seed_len < 20))
124 seed_in = NULL; /* seed buffer too small -- ignore */ 108 seed_in = NULL; /* seed buffer too small -- ignore */
125 if (seed_len > 20) 109 if (seed_len > 20)
126 seed_len = 20; /* App. 2.2 of FIPS PUB 186 allows larger SEED, 110 seed_len = 20; /* App. 2.2 of FIPS PUB 186 allows larger SEED,
127 * but our internal buffers are restricted to 160 bits*/ 111 * but our internal buffers are restricted to 160 bits*/
128 if ((seed_in != NULL) && (seed_len == 20)) 112 if ((seed_in != NULL) && (seed_len == 20))
129 {
130 memcpy(seed,seed_in,seed_len); 113 memcpy(seed,seed_in,seed_len);
131 /* set seed_in to NULL to avoid it being copied back */
132 seed_in = NULL;
133 }
134 114
135 if ((ctx=BN_CTX_new()) == NULL) goto err; 115 if ((ctx=BN_CTX_new()) == NULL) goto err;
116 if ((ctx2=BN_CTX_new()) == NULL) goto err;
117 if ((ctx3=BN_CTX_new()) == NULL) goto err;
118 if ((ret=DSA_new()) == NULL) goto err;
136 119
137 if ((mont=BN_MONT_CTX_new()) == NULL) goto err; 120 if ((mont=BN_MONT_CTX_new()) == NULL) goto err;
138 121
139 BN_CTX_start(ctx); 122 BN_CTX_start(ctx2);
140 r0 = BN_CTX_get(ctx); 123 r0 = BN_CTX_get(ctx2);
141 g = BN_CTX_get(ctx); 124 g = BN_CTX_get(ctx2);
142 W = BN_CTX_get(ctx); 125 W = BN_CTX_get(ctx2);
143 q = BN_CTX_get(ctx); 126 q = BN_CTX_get(ctx2);
144 X = BN_CTX_get(ctx); 127 X = BN_CTX_get(ctx2);
145 c = BN_CTX_get(ctx); 128 c = BN_CTX_get(ctx2);
146 p = BN_CTX_get(ctx); 129 p = BN_CTX_get(ctx2);
147 test = BN_CTX_get(ctx); 130 test = BN_CTX_get(ctx2);
131 if (test == NULL) goto err;
148 132
149 if (!BN_lshift(test,BN_value_one(),bits-1)) 133 if (!BN_lshift(test,BN_value_one(),bits-1)) goto err;
150 goto err;
151 134
152 for (;;) 135 for (;;)
153 { 136 {
@@ -156,8 +139,7 @@ static int dsa_builtin_paramgen(DSA *ret, int bits,
156 int seed_is_random; 139 int seed_is_random;
157 140
158 /* step 1 */ 141 /* step 1 */
159 if(!BN_GENCB_call(cb, 0, m++)) 142 if (callback != NULL) callback(0,m++,cb_arg);
160 goto err;
161 143
162 if (!seed_len) 144 if (!seed_len)
163 { 145 {
@@ -190,8 +172,7 @@ static int dsa_builtin_paramgen(DSA *ret, int bits,
190 if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,q)) goto err; 172 if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,q)) goto err;
191 173
192 /* step 4 */ 174 /* step 4 */
193 r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, 175 r = BN_is_prime_fasttest(q, DSS_prime_checks, callback, ctx3, cb_arg, seed_is_random);
194 seed_is_random, cb);
195 if (r > 0) 176 if (r > 0)
196 break; 177 break;
197 if (r != 0) 178 if (r != 0)
@@ -201,8 +182,8 @@ static int dsa_builtin_paramgen(DSA *ret, int bits,
201 /* step 5 */ 182 /* step 5 */
202 } 183 }
203 184
204 if(!BN_GENCB_call(cb, 2, 0)) goto err; 185 if (callback != NULL) callback(2,0,cb_arg);
205 if(!BN_GENCB_call(cb, 3, 0)) goto err; 186 if (callback != NULL) callback(3,0,cb_arg);
206 187
207 /* step 6 */ 188 /* step 6 */
208 counter=0; 189 counter=0;
@@ -213,11 +194,11 @@ static int dsa_builtin_paramgen(DSA *ret, int bits,
213 194
214 for (;;) 195 for (;;)
215 { 196 {
216 if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) 197 if (callback != NULL && counter != 0)
217 goto err; 198 callback(0,counter,cb_arg);
218 199
219 /* step 7 */ 200 /* step 7 */
220 BN_zero(W); 201 if (!BN_zero(W)) goto err;
221 /* now 'buf' contains "SEED + offset - 1" */ 202 /* now 'buf' contains "SEED + offset - 1" */
222 for (k=0; k<=n; k++) 203 for (k=0; k<=n; k++)
223 { 204 {
@@ -252,8 +233,7 @@ static int dsa_builtin_paramgen(DSA *ret, int bits,
252 if (BN_cmp(p,test) >= 0) 233 if (BN_cmp(p,test) >= 0)
253 { 234 {
254 /* step 11 */ 235 /* step 11 */
255 r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, 236 r = BN_is_prime_fasttest(p, DSS_prime_checks, callback, ctx3, cb_arg, 1);
256 ctx, 1, cb);
257 if (r > 0) 237 if (r > 0)
258 goto end; /* found it */ 238 goto end; /* found it */
259 if (r != 0) 239 if (r != 0)
@@ -269,8 +249,7 @@ static int dsa_builtin_paramgen(DSA *ret, int bits,
269 } 249 }
270 } 250 }
271end: 251end:
272 if(!BN_GENCB_call(cb, 2, 1)) 252 if (callback != NULL) callback(2,1,cb_arg);
273 goto err;
274 253
275 /* We now need to generate g */ 254 /* We now need to generate g */
276 /* Set r0=(p-1)/q */ 255 /* Set r0=(p-1)/q */
@@ -289,16 +268,16 @@ end:
289 h++; 268 h++;
290 } 269 }
291 270
292 if(!BN_GENCB_call(cb, 3, 1)) 271 if (callback != NULL) callback(3,1,cb_arg);
293 goto err;
294 272
295 ok=1; 273 ok=1;
296err: 274err:
297 if (ok) 275 if (!ok)
276 {
277 if (ret != NULL) DSA_free(ret);
278 }
279 else
298 { 280 {
299 if(ret->p) BN_free(ret->p);
300 if(ret->q) BN_free(ret->q);
301 if(ret->g) BN_free(ret->g);
302 ret->p=BN_dup(p); 281 ret->p=BN_dup(p);
303 ret->q=BN_dup(q); 282 ret->q=BN_dup(q);
304 ret->g=BN_dup(g); 283 ret->g=BN_dup(g);
@@ -307,16 +286,20 @@ err:
307 ok=0; 286 ok=0;
308 goto err; 287 goto err;
309 } 288 }
310 if (seed_in != NULL) memcpy(seed_in,seed,20); 289 if ((m > 1) && (seed_in != NULL)) memcpy(seed_in,seed,20);
311 if (counter_ret != NULL) *counter_ret=counter; 290 if (counter_ret != NULL) *counter_ret=counter;
312 if (h_ret != NULL) *h_ret=h; 291 if (h_ret != NULL) *h_ret=h;
313 } 292 }
314 if(ctx) 293 if (ctx != NULL) BN_CTX_free(ctx);
294 if (ctx2 != NULL)
315 { 295 {
316 BN_CTX_end(ctx); 296 BN_CTX_end(ctx2);
317 BN_CTX_free(ctx); 297 BN_CTX_free(ctx2);
318 } 298 }
299 if (ctx3 != NULL) BN_CTX_free(ctx3);
319 if (mont != NULL) BN_MONT_CTX_free(mont); 300 if (mont != NULL) BN_MONT_CTX_free(mont);
320 return ok; 301 return(ok?ret:NULL);
321 } 302 }
322#endif 303#endif /* ndef OPENSSL_FIPS */
304#endif /* ndef OPENSSL_NO_SHA */
305