diff options
author | djm <> | 2010-10-01 22:54:21 +0000 |
---|---|---|
committer | djm <> | 2010-10-01 22:54:21 +0000 |
commit | 829fd51d4f8dde4a7f3bf54754f3c1d1a502f5e2 (patch) | |
tree | e03b9f1bd051e844b971936729e9df549a209130 /src/lib/libcrypto/dsa/dsa_gen.c | |
parent | e6b755d2a53d3cac7a344dfdd6bf7c951cac754c (diff) | |
download | openbsd-829fd51d4f8dde4a7f3bf54754f3c1d1a502f5e2.tar.gz openbsd-829fd51d4f8dde4a7f3bf54754f3c1d1a502f5e2.tar.bz2 openbsd-829fd51d4f8dde4a7f3bf54754f3c1d1a502f5e2.zip |
import OpenSSL-1.0.0a
Diffstat (limited to 'src/lib/libcrypto/dsa/dsa_gen.c')
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_gen.c | 124 |
1 files changed, 72 insertions, 52 deletions
diff --git a/src/lib/libcrypto/dsa/dsa_gen.c b/src/lib/libcrypto/dsa/dsa_gen.c index 6f1728e3cf..0fcd25f8b0 100644 --- a/src/lib/libcrypto/dsa/dsa_gen.c +++ b/src/lib/libcrypto/dsa/dsa_gen.c | |||
@@ -74,69 +74,88 @@ | |||
74 | #ifndef OPENSSL_NO_SHA | 74 | #ifndef OPENSSL_NO_SHA |
75 | 75 | ||
76 | #include <stdio.h> | 76 | #include <stdio.h> |
77 | #include <time.h> | ||
78 | #include "cryptlib.h" | 77 | #include "cryptlib.h" |
79 | #include <openssl/evp.h> | 78 | #include <openssl/evp.h> |
80 | #include <openssl/bn.h> | 79 | #include <openssl/bn.h> |
81 | #include <openssl/dsa.h> | ||
82 | #include <openssl/rand.h> | 80 | #include <openssl/rand.h> |
83 | #include <openssl/sha.h> | 81 | #include <openssl/sha.h> |
84 | 82 | #include "dsa_locl.h" | |
85 | #ifndef OPENSSL_FIPS | ||
86 | |||
87 | static int dsa_builtin_paramgen(DSA *ret, int bits, | ||
88 | unsigned char *seed_in, int seed_len, | ||
89 | int *counter_ret, unsigned long *h_ret, BN_GENCB *cb); | ||
90 | 83 | ||
91 | int DSA_generate_parameters_ex(DSA *ret, int bits, | 84 | int DSA_generate_parameters_ex(DSA *ret, int bits, |
92 | unsigned char *seed_in, int seed_len, | 85 | const unsigned char *seed_in, int seed_len, |
93 | int *counter_ret, unsigned long *h_ret, BN_GENCB *cb) | 86 | int *counter_ret, unsigned long *h_ret, BN_GENCB *cb) |
94 | { | 87 | { |
95 | if(ret->meth->dsa_paramgen) | 88 | if(ret->meth->dsa_paramgen) |
96 | return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len, | 89 | return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len, |
97 | counter_ret, h_ret, cb); | 90 | counter_ret, h_ret, cb); |
98 | return dsa_builtin_paramgen(ret, bits, seed_in, seed_len, | 91 | else |
99 | counter_ret, h_ret, cb); | 92 | { |
93 | const EVP_MD *evpmd; | ||
94 | size_t qbits = bits >= 2048 ? 256 : 160; | ||
95 | |||
96 | if (bits >= 2048) | ||
97 | { | ||
98 | qbits = 256; | ||
99 | evpmd = EVP_sha256(); | ||
100 | } | ||
101 | else | ||
102 | { | ||
103 | qbits = 160; | ||
104 | evpmd = EVP_sha1(); | ||
105 | } | ||
106 | |||
107 | return dsa_builtin_paramgen(ret, bits, qbits, evpmd, | ||
108 | seed_in, seed_len, counter_ret, h_ret, cb); | ||
109 | } | ||
100 | } | 110 | } |
101 | 111 | ||
102 | static int dsa_builtin_paramgen(DSA *ret, int bits, | 112 | int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits, |
103 | unsigned char *seed_in, int seed_len, | 113 | const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len, |
104 | int *counter_ret, unsigned long *h_ret, BN_GENCB *cb) | 114 | int *counter_ret, unsigned long *h_ret, BN_GENCB *cb) |
105 | { | 115 | { |
106 | int ok=0; | 116 | int ok=0; |
107 | unsigned char seed[SHA_DIGEST_LENGTH]; | 117 | unsigned char seed[SHA256_DIGEST_LENGTH]; |
108 | unsigned char md[SHA_DIGEST_LENGTH]; | 118 | unsigned char md[SHA256_DIGEST_LENGTH]; |
109 | unsigned char buf[SHA_DIGEST_LENGTH],buf2[SHA_DIGEST_LENGTH]; | 119 | unsigned char buf[SHA256_DIGEST_LENGTH],buf2[SHA256_DIGEST_LENGTH]; |
110 | BIGNUM *r0,*W,*X,*c,*test; | 120 | BIGNUM *r0,*W,*X,*c,*test; |
111 | BIGNUM *g=NULL,*q=NULL,*p=NULL; | 121 | BIGNUM *g=NULL,*q=NULL,*p=NULL; |
112 | BN_MONT_CTX *mont=NULL; | 122 | BN_MONT_CTX *mont=NULL; |
113 | int k,n=0,i,b,m=0; | 123 | int i, k,n=0,b,m=0, qsize = qbits >> 3; |
114 | int counter=0; | 124 | int counter=0; |
115 | int r=0; | 125 | int r=0; |
116 | BN_CTX *ctx=NULL; | 126 | BN_CTX *ctx=NULL; |
117 | unsigned int h=2; | 127 | unsigned int h=2; |
118 | 128 | ||
119 | if (bits < 512) bits=512; | 129 | if (qsize != SHA_DIGEST_LENGTH && qsize != SHA224_DIGEST_LENGTH && |
120 | bits=(bits+63)/64*64; | 130 | qsize != SHA256_DIGEST_LENGTH) |
131 | /* invalid q size */ | ||
132 | return 0; | ||
133 | |||
134 | if (evpmd == NULL) | ||
135 | /* use SHA1 as default */ | ||
136 | evpmd = EVP_sha1(); | ||
137 | |||
138 | if (bits < 512) | ||
139 | bits = 512; | ||
140 | |||
141 | bits = (bits+63)/64*64; | ||
121 | 142 | ||
122 | /* NB: seed_len == 0 is special case: copy generated seed to | 143 | /* NB: seed_len == 0 is special case: copy generated seed to |
123 | * seed_in if it is not NULL. | 144 | * seed_in if it is not NULL. |
124 | */ | 145 | */ |
125 | if (seed_len && (seed_len < 20)) | 146 | if (seed_len && (seed_len < (size_t)qsize)) |
126 | seed_in = NULL; /* seed buffer too small -- ignore */ | 147 | seed_in = NULL; /* seed buffer too small -- ignore */ |
127 | if (seed_len > 20) | 148 | if (seed_len > (size_t)qsize) |
128 | seed_len = 20; /* App. 2.2 of FIPS PUB 186 allows larger SEED, | 149 | seed_len = qsize; /* App. 2.2 of FIPS PUB 186 allows larger SEED, |
129 | * but our internal buffers are restricted to 160 bits*/ | 150 | * but our internal buffers are restricted to 160 bits*/ |
130 | if ((seed_in != NULL) && (seed_len == 20)) | 151 | if (seed_in != NULL) |
131 | { | 152 | memcpy(seed, seed_in, seed_len); |
132 | memcpy(seed,seed_in,seed_len); | 153 | |
133 | /* set seed_in to NULL to avoid it being copied back */ | 154 | if ((ctx=BN_CTX_new()) == NULL) |
134 | seed_in = NULL; | 155 | goto err; |
135 | } | ||
136 | |||
137 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
138 | 156 | ||
139 | if ((mont=BN_MONT_CTX_new()) == NULL) goto err; | 157 | if ((mont=BN_MONT_CTX_new()) == NULL) |
158 | goto err; | ||
140 | 159 | ||
141 | BN_CTX_start(ctx); | 160 | BN_CTX_start(ctx); |
142 | r0 = BN_CTX_get(ctx); | 161 | r0 = BN_CTX_get(ctx); |
@@ -163,7 +182,7 @@ static int dsa_builtin_paramgen(DSA *ret, int bits, | |||
163 | 182 | ||
164 | if (!seed_len) | 183 | if (!seed_len) |
165 | { | 184 | { |
166 | RAND_pseudo_bytes(seed,SHA_DIGEST_LENGTH); | 185 | RAND_pseudo_bytes(seed, qsize); |
167 | seed_is_random = 1; | 186 | seed_is_random = 1; |
168 | } | 187 | } |
169 | else | 188 | else |
@@ -171,25 +190,27 @@ static int dsa_builtin_paramgen(DSA *ret, int bits, | |||
171 | seed_is_random = 0; | 190 | seed_is_random = 0; |
172 | seed_len=0; /* use random seed if 'seed_in' turns out to be bad*/ | 191 | seed_len=0; /* use random seed if 'seed_in' turns out to be bad*/ |
173 | } | 192 | } |
174 | memcpy(buf,seed,SHA_DIGEST_LENGTH); | 193 | memcpy(buf , seed, qsize); |
175 | memcpy(buf2,seed,SHA_DIGEST_LENGTH); | 194 | memcpy(buf2, seed, qsize); |
176 | /* precompute "SEED + 1" for step 7: */ | 195 | /* precompute "SEED + 1" for step 7: */ |
177 | for (i=SHA_DIGEST_LENGTH-1; i >= 0; i--) | 196 | for (i = qsize-1; i >= 0; i--) |
178 | { | 197 | { |
179 | buf[i]++; | 198 | buf[i]++; |
180 | if (buf[i] != 0) break; | 199 | if (buf[i] != 0) |
200 | break; | ||
181 | } | 201 | } |
182 | 202 | ||
183 | /* step 2 */ | 203 | /* step 2 */ |
184 | EVP_Digest(seed,SHA_DIGEST_LENGTH,md,NULL,HASH, NULL); | 204 | EVP_Digest(seed, qsize, md, NULL, evpmd, NULL); |
185 | EVP_Digest(buf,SHA_DIGEST_LENGTH,buf2,NULL,HASH, NULL); | 205 | EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL); |
186 | for (i=0; i<SHA_DIGEST_LENGTH; i++) | 206 | for (i = 0; i < qsize; i++) |
187 | md[i]^=buf2[i]; | 207 | md[i]^=buf2[i]; |
188 | 208 | ||
189 | /* step 3 */ | 209 | /* step 3 */ |
190 | md[0]|=0x80; | 210 | md[0] |= 0x80; |
191 | md[SHA_DIGEST_LENGTH-1]|=0x01; | 211 | md[qsize-1] |= 0x01; |
192 | if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,q)) goto err; | 212 | if (!BN_bin2bn(md, qsize, q)) |
213 | goto err; | ||
193 | 214 | ||
194 | /* step 4 */ | 215 | /* step 4 */ |
195 | r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, | 216 | r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, |
@@ -224,18 +245,19 @@ static int dsa_builtin_paramgen(DSA *ret, int bits, | |||
224 | for (k=0; k<=n; k++) | 245 | for (k=0; k<=n; k++) |
225 | { | 246 | { |
226 | /* obtain "SEED + offset + k" by incrementing: */ | 247 | /* obtain "SEED + offset + k" by incrementing: */ |
227 | for (i=SHA_DIGEST_LENGTH-1; i >= 0; i--) | 248 | for (i = qsize-1; i >= 0; i--) |
228 | { | 249 | { |
229 | buf[i]++; | 250 | buf[i]++; |
230 | if (buf[i] != 0) break; | 251 | if (buf[i] != 0) |
252 | break; | ||
231 | } | 253 | } |
232 | 254 | ||
233 | EVP_Digest(buf,SHA_DIGEST_LENGTH,md,NULL,HASH, NULL); | 255 | EVP_Digest(buf, qsize, md ,NULL, evpmd, NULL); |
234 | 256 | ||
235 | /* step 8 */ | 257 | /* step 8 */ |
236 | if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,r0)) | 258 | if (!BN_bin2bn(md, qsize, r0)) |
237 | goto err; | 259 | goto err; |
238 | if (!BN_lshift(r0,r0,160*k)) goto err; | 260 | if (!BN_lshift(r0,r0,(qsize << 3)*k)) goto err; |
239 | if (!BN_add(W,W,r0)) goto err; | 261 | if (!BN_add(W,W,r0)) goto err; |
240 | } | 262 | } |
241 | 263 | ||
@@ -309,7 +331,6 @@ err: | |||
309 | ok=0; | 331 | ok=0; |
310 | goto err; | 332 | goto err; |
311 | } | 333 | } |
312 | if (seed_in != NULL) memcpy(seed_in,seed,20); | ||
313 | if (counter_ret != NULL) *counter_ret=counter; | 334 | if (counter_ret != NULL) *counter_ret=counter; |
314 | if (h_ret != NULL) *h_ret=h; | 335 | if (h_ret != NULL) *h_ret=h; |
315 | } | 336 | } |
@@ -322,4 +343,3 @@ err: | |||
322 | return ok; | 343 | return ok; |
323 | } | 344 | } |
324 | #endif | 345 | #endif |
325 | #endif | ||