summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/dsa/dsa_gen.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/dsa/dsa_gen.c (renamed from src/lib/libssl/src/fips/dsa/fips_dsa_gen.c)137
1 files changed, 71 insertions, 66 deletions
diff --git a/src/lib/libssl/src/fips/dsa/fips_dsa_gen.c b/src/lib/libcrypto/dsa/dsa_gen.c
index 0cecf34ab2..cb0b4538a4 100644
--- a/src/lib/libssl/src/fips/dsa/fips_dsa_gen.c
+++ b/src/lib/libcrypto/dsa/dsa_gen.c
@@ -74,83 +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> 77#include "cryptlib.h"
78#include <string.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#include <openssl/err.h> 82#include "dsa_locl.h"
85
86#ifdef OPENSSL_FIPS
87
88static int dsa_builtin_paramgen(DSA *ret, int bits,
89 unsigned char *seed_in, int seed_len,
90 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb);
91 83
92int DSA_generate_parameters_ex(DSA *ret, int bits, 84int DSA_generate_parameters_ex(DSA *ret, int bits,
93 unsigned char *seed_in, int seed_len, 85 const unsigned char *seed_in, int seed_len,
94 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb) 86 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
95 { 87 {
96 if(ret->meth->dsa_paramgen) 88 if(ret->meth->dsa_paramgen)
97 return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len, 89 return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len,
98 counter_ret, h_ret, cb); 90 counter_ret, h_ret, cb);
99 return dsa_builtin_paramgen(ret, bits, seed_in, seed_len, 91 else
100 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 }
101 } 110 }
102 111
103static int dsa_builtin_paramgen(DSA *ret, int bits, 112int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
104 unsigned char *seed_in, int seed_len, 113 const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len,
105 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb) 114 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
106 { 115 {
107 int ok=0; 116 int ok=0;
108 unsigned char seed[SHA_DIGEST_LENGTH]; 117 unsigned char seed[SHA256_DIGEST_LENGTH];
109 unsigned char md[SHA_DIGEST_LENGTH]; 118 unsigned char md[SHA256_DIGEST_LENGTH];
110 unsigned char buf[SHA_DIGEST_LENGTH],buf2[SHA_DIGEST_LENGTH]; 119 unsigned char buf[SHA256_DIGEST_LENGTH],buf2[SHA256_DIGEST_LENGTH];
111 BIGNUM *r0,*W,*X,*c,*test; 120 BIGNUM *r0,*W,*X,*c,*test;
112 BIGNUM *g=NULL,*q=NULL,*p=NULL; 121 BIGNUM *g=NULL,*q=NULL,*p=NULL;
113 BN_MONT_CTX *mont=NULL; 122 BN_MONT_CTX *mont=NULL;
114 int k,n=0,i,b,m=0; 123 int i, k, n=0, m=0, qsize = qbits >> 3;
115 int counter=0; 124 int counter=0;
116 int r=0; 125 int r=0;
117 BN_CTX *ctx=NULL; 126 BN_CTX *ctx=NULL;
118 unsigned int h=2; 127 unsigned int h=2;
119 128
120 if(FIPS_selftest_failed()) 129 if (qsize != SHA_DIGEST_LENGTH && qsize != SHA224_DIGEST_LENGTH &&
121 { 130 qsize != SHA256_DIGEST_LENGTH)
122 FIPSerr(FIPS_F_DSA_BUILTIN_PARAMGEN, 131 /* invalid q size */
123 FIPS_R_FIPS_SELFTEST_FAILED); 132 return 0;
124 goto err;
125 }
126 133
127 if (FIPS_mode() && (bits < OPENSSL_DSA_FIPS_MIN_MODULUS_BITS)) 134 if (evpmd == NULL)
128 { 135 /* use SHA1 as default */
129 DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN, DSA_R_KEY_SIZE_TOO_SMALL); 136 evpmd = EVP_sha1();
130 goto err;
131 }
132 137
133 if (bits < 512) bits=512; 138 if (bits < 512)
134 bits=(bits+63)/64*64; 139 bits = 512;
140
141 bits = (bits+63)/64*64;
135 142
136 /* NB: seed_len == 0 is special case: copy generated seed to 143 /* NB: seed_len == 0 is special case: copy generated seed to
137 * seed_in if it is not NULL. 144 * seed_in if it is not NULL.
138 */ 145 */
139 if (seed_len && (seed_len < 20)) 146 if (seed_len && (seed_len < (size_t)qsize))
140 seed_in = NULL; /* seed buffer too small -- ignore */ 147 seed_in = NULL; /* seed buffer too small -- ignore */
141 if (seed_len > 20) 148 if (seed_len > (size_t)qsize)
142 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,
143 * but our internal buffers are restricted to 160 bits*/ 150 * but our internal buffers are restricted to 160 bits*/
144 if ((seed_in != NULL) && (seed_len == 20)) 151 if (seed_in != NULL)
145 { 152 memcpy(seed, seed_in, seed_len);
146 memcpy(seed,seed_in,seed_len); 153
147 /* set seed_in to NULL to avoid it being copied back */ 154 if ((ctx=BN_CTX_new()) == NULL)
148 seed_in = NULL; 155 goto err;
149 }
150
151 if ((ctx=BN_CTX_new()) == NULL) goto err;
152 156
153 if ((mont=BN_MONT_CTX_new()) == NULL) goto err; 157 if ((mont=BN_MONT_CTX_new()) == NULL)
158 goto err;
154 159
155 BN_CTX_start(ctx); 160 BN_CTX_start(ctx);
156 r0 = BN_CTX_get(ctx); 161 r0 = BN_CTX_get(ctx);
@@ -177,7 +182,7 @@ static int dsa_builtin_paramgen(DSA *ret, int bits,
177 182
178 if (!seed_len) 183 if (!seed_len)
179 { 184 {
180 RAND_pseudo_bytes(seed,SHA_DIGEST_LENGTH); 185 RAND_pseudo_bytes(seed, qsize);
181 seed_is_random = 1; 186 seed_is_random = 1;
182 } 187 }
183 else 188 else
@@ -185,25 +190,27 @@ static int dsa_builtin_paramgen(DSA *ret, int bits,
185 seed_is_random = 0; 190 seed_is_random = 0;
186 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*/
187 } 192 }
188 memcpy(buf,seed,SHA_DIGEST_LENGTH); 193 memcpy(buf , seed, qsize);
189 memcpy(buf2,seed,SHA_DIGEST_LENGTH); 194 memcpy(buf2, seed, qsize);
190 /* precompute "SEED + 1" for step 7: */ 195 /* precompute "SEED + 1" for step 7: */
191 for (i=SHA_DIGEST_LENGTH-1; i >= 0; i--) 196 for (i = qsize-1; i >= 0; i--)
192 { 197 {
193 buf[i]++; 198 buf[i]++;
194 if (buf[i] != 0) break; 199 if (buf[i] != 0)
200 break;
195 } 201 }
196 202
197 /* step 2 */ 203 /* step 2 */
198 EVP_Digest(seed,SHA_DIGEST_LENGTH,md,NULL,HASH, NULL); 204 EVP_Digest(seed, qsize, md, NULL, evpmd, NULL);
199 EVP_Digest(buf,SHA_DIGEST_LENGTH,buf2,NULL,HASH, NULL); 205 EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL);
200 for (i=0; i<SHA_DIGEST_LENGTH; i++) 206 for (i = 0; i < qsize; i++)
201 md[i]^=buf2[i]; 207 md[i]^=buf2[i];
202 208
203 /* step 3 */ 209 /* step 3 */
204 md[0]|=0x80; 210 md[0] |= 0x80;
205 md[SHA_DIGEST_LENGTH-1]|=0x01; 211 md[qsize-1] |= 0x01;
206 if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,q)) goto err; 212 if (!BN_bin2bn(md, qsize, q))
213 goto err;
207 214
208 /* step 4 */ 215 /* step 4 */
209 r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, 216 r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
@@ -225,7 +232,6 @@ static int dsa_builtin_paramgen(DSA *ret, int bits,
225 /* "offset = 2" */ 232 /* "offset = 2" */
226 233
227 n=(bits-1)/160; 234 n=(bits-1)/160;
228 b=(bits-1)-n*160;
229 235
230 for (;;) 236 for (;;)
231 { 237 {
@@ -238,18 +244,19 @@ static int dsa_builtin_paramgen(DSA *ret, int bits,
238 for (k=0; k<=n; k++) 244 for (k=0; k<=n; k++)
239 { 245 {
240 /* obtain "SEED + offset + k" by incrementing: */ 246 /* obtain "SEED + offset + k" by incrementing: */
241 for (i=SHA_DIGEST_LENGTH-1; i >= 0; i--) 247 for (i = qsize-1; i >= 0; i--)
242 { 248 {
243 buf[i]++; 249 buf[i]++;
244 if (buf[i] != 0) break; 250 if (buf[i] != 0)
251 break;
245 } 252 }
246 253
247 EVP_Digest(buf,SHA_DIGEST_LENGTH,md,NULL,HASH, NULL); 254 EVP_Digest(buf, qsize, md ,NULL, evpmd, NULL);
248 255
249 /* step 8 */ 256 /* step 8 */
250 if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,r0)) 257 if (!BN_bin2bn(md, qsize, r0))
251 goto err; 258 goto err;
252 if (!BN_lshift(r0,r0,160*k)) goto err; 259 if (!BN_lshift(r0,r0,(qsize << 3)*k)) goto err;
253 if (!BN_add(W,W,r0)) goto err; 260 if (!BN_add(W,W,r0)) goto err;
254 } 261 }
255 262
@@ -323,7 +330,6 @@ err:
323 ok=0; 330 ok=0;
324 goto err; 331 goto err;
325 } 332 }
326 if (seed_in != NULL) memcpy(seed_in,seed,20);
327 if (counter_ret != NULL) *counter_ret=counter; 333 if (counter_ret != NULL) *counter_ret=counter;
328 if (h_ret != NULL) *h_ret=h; 334 if (h_ret != NULL) *h_ret=h;
329 } 335 }
@@ -336,4 +342,3 @@ err:
336 return ok; 342 return ok;
337 } 343 }
338#endif 344#endif
339#endif