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