OLD | NEW |
1 /* crypto/dsa/dsa_ossl.c */ | 1 /* crypto/dsa/dsa_ossl.c */ |
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * This package is an SSL implementation written | 5 * This package is an SSL implementation written |
6 * by Eric Young (eay@cryptsoft.com). | 6 * by Eric Young (eay@cryptsoft.com). |
7 * The implementation was written so as to conform with Netscapes SSL. | 7 * The implementation was written so as to conform with Netscapes SSL. |
8 * | 8 * |
9 * This library is free for commercial and non-commercial use as long as | 9 * This library is free for commercial and non-commercial use as long as |
10 * the following conditions are aheared to. The following conditions | 10 * the following conditions are aheared to. The following conditions |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 * derivative of this code cannot be changed. i.e. this code cannot simply be | 54 * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 * copied and put under another distribution licence | 55 * copied and put under another distribution licence |
56 * [including the GNU Public Licence.] | 56 * [including the GNU Public Licence.] |
57 */ | 57 */ |
58 | 58 |
59 /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */ | 59 /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */ |
60 | 60 |
61 #include <stdio.h> | 61 #include <stdio.h> |
62 #include "cryptlib.h" | 62 #include "cryptlib.h" |
63 #include <openssl/bn.h> | 63 #include <openssl/bn.h> |
| 64 #include <openssl/sha.h> |
64 #include <openssl/dsa.h> | 65 #include <openssl/dsa.h> |
65 #include <openssl/rand.h> | 66 #include <openssl/rand.h> |
66 #include <openssl/asn1.h> | 67 #include <openssl/asn1.h> |
67 | 68 |
68 #ifndef OPENSSL_FIPS | |
69 | |
70 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa); | 69 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa); |
71 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
; | 70 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
; |
72 static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, | 71 static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, |
73 » » DSA *dsa); | 72 » » » DSA *dsa); |
74 static int dsa_init(DSA *dsa); | 73 static int dsa_init(DSA *dsa); |
75 static int dsa_finish(DSA *dsa); | 74 static int dsa_finish(DSA *dsa); |
76 | 75 |
77 static DSA_METHOD openssl_dsa_meth = { | 76 static DSA_METHOD openssl_dsa_meth = { |
78 "OpenSSL DSA method", | 77 "OpenSSL DSA method", |
79 dsa_do_sign, | 78 dsa_do_sign, |
80 dsa_sign_setup, | 79 dsa_sign_setup, |
81 dsa_do_verify, | 80 dsa_do_verify, |
82 NULL, /* dsa_mod_exp, */ | 81 NULL, /* dsa_mod_exp, */ |
83 NULL, /* dsa_bn_mod_exp, */ | 82 NULL, /* dsa_bn_mod_exp, */ |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 { | 127 { |
129 return &openssl_dsa_meth; | 128 return &openssl_dsa_meth; |
130 } | 129 } |
131 | 130 |
132 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) | 131 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) |
133 { | 132 { |
134 BIGNUM *kinv=NULL,*r=NULL,*s=NULL; | 133 BIGNUM *kinv=NULL,*r=NULL,*s=NULL; |
135 BIGNUM m; | 134 BIGNUM m; |
136 BIGNUM xr; | 135 BIGNUM xr; |
137 BN_CTX *ctx=NULL; | 136 BN_CTX *ctx=NULL; |
138 » int i,reason=ERR_R_BN_LIB; | 137 » int reason=ERR_R_BN_LIB; |
139 DSA_SIG *ret=NULL; | 138 DSA_SIG *ret=NULL; |
140 | 139 |
141 BN_init(&m); | 140 BN_init(&m); |
142 BN_init(&xr); | 141 BN_init(&xr); |
143 | 142 |
144 if (!dsa->p || !dsa->q || !dsa->g) | 143 if (!dsa->p || !dsa->q || !dsa->g) |
145 { | 144 { |
146 reason=DSA_R_MISSING_PARAMETERS; | 145 reason=DSA_R_MISSING_PARAMETERS; |
147 goto err; | 146 goto err; |
148 } | 147 } |
149 | 148 |
150 s=BN_new(); | 149 s=BN_new(); |
151 if (s == NULL) goto err; | 150 if (s == NULL) goto err; |
152 | |
153 i=BN_num_bytes(dsa->q); /* should be 20 */ | |
154 if ((dlen > i) || (dlen > 50)) | |
155 { | |
156 reason=DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE; | |
157 goto err; | |
158 } | |
159 | |
160 ctx=BN_CTX_new(); | 151 ctx=BN_CTX_new(); |
161 if (ctx == NULL) goto err; | 152 if (ctx == NULL) goto err; |
162 | 153 |
163 if ((dsa->kinv == NULL) || (dsa->r == NULL)) | 154 if ((dsa->kinv == NULL) || (dsa->r == NULL)) |
164 { | 155 { |
165 if (!DSA_sign_setup(dsa,ctx,&kinv,&r)) goto err; | 156 if (!DSA_sign_setup(dsa,ctx,&kinv,&r)) goto err; |
166 } | 157 } |
167 else | 158 else |
168 { | 159 { |
169 kinv=dsa->kinv; | 160 kinv=dsa->kinv; |
170 dsa->kinv=NULL; | 161 dsa->kinv=NULL; |
171 r=dsa->r; | 162 r=dsa->r; |
172 dsa->r=NULL; | 163 dsa->r=NULL; |
173 } | 164 } |
174 | 165 |
175 » if (BN_bin2bn(dgst,dlen,&m) == NULL) goto err; | 166 » |
| 167 » if (dlen > BN_num_bytes(dsa->q)) |
| 168 » » /* if the digest length is greater than the size of q use the |
| 169 » » * BN_num_bits(dsa->q) leftmost bits of the digest, see |
| 170 » » * fips 186-3, 4.2 */ |
| 171 » » dlen = BN_num_bytes(dsa->q); |
| 172 » if (BN_bin2bn(dgst,dlen,&m) == NULL) |
| 173 » » goto err; |
176 | 174 |
177 /* Compute s = inv(k) (m + xr) mod q */ | 175 /* Compute s = inv(k) (m + xr) mod q */ |
178 if (!BN_mod_mul(&xr,dsa->priv_key,r,dsa->q,ctx)) goto err;/* s = xr */ | 176 if (!BN_mod_mul(&xr,dsa->priv_key,r,dsa->q,ctx)) goto err;/* s = xr */ |
179 if (!BN_add(s, &xr, &m)) goto err; /* s = m + xr */ | 177 if (!BN_add(s, &xr, &m)) goto err; /* s = m + xr */ |
180 if (BN_cmp(s,dsa->q) > 0) | 178 if (BN_cmp(s,dsa->q) > 0) |
181 » » BN_sub(s,s,dsa->q); | 179 » » if (!BN_sub(s,s,dsa->q)) goto err; |
182 if (!BN_mod_mul(s,s,kinv,dsa->q,ctx)) goto err; | 180 if (!BN_mod_mul(s,s,kinv,dsa->q,ctx)) goto err; |
183 | 181 |
184 ret=DSA_SIG_new(); | 182 ret=DSA_SIG_new(); |
185 if (ret == NULL) goto err; | 183 if (ret == NULL) goto err; |
186 ret->r = r; | 184 ret->r = r; |
187 ret->s = s; | 185 ret->s = s; |
188 | 186 |
189 err: | 187 err: |
190 if (!ret) | 188 if (!ret) |
191 { | 189 { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 if (*kinvp != NULL) BN_clear_free(*kinvp); | 274 if (*kinvp != NULL) BN_clear_free(*kinvp); |
277 *kinvp=kinv; | 275 *kinvp=kinv; |
278 kinv=NULL; | 276 kinv=NULL; |
279 if (*rp != NULL) BN_clear_free(*rp); | 277 if (*rp != NULL) BN_clear_free(*rp); |
280 *rp=r; | 278 *rp=r; |
281 ret=1; | 279 ret=1; |
282 err: | 280 err: |
283 if (!ret) | 281 if (!ret) |
284 { | 282 { |
285 DSAerr(DSA_F_DSA_SIGN_SETUP,ERR_R_BN_LIB); | 283 DSAerr(DSA_F_DSA_SIGN_SETUP,ERR_R_BN_LIB); |
286 » » if (kinv != NULL) BN_clear_free(kinv); | 284 » » if (r != NULL) |
287 » » if (r != NULL) BN_clear_free(r); | 285 » » » BN_clear_free(r); |
288 } | 286 } |
289 if (ctx_in == NULL) BN_CTX_free(ctx); | 287 if (ctx_in == NULL) BN_CTX_free(ctx); |
290 if (kinv != NULL) BN_clear_free(kinv); | |
291 BN_clear_free(&k); | 288 BN_clear_free(&k); |
292 BN_clear_free(&kq); | 289 BN_clear_free(&kq); |
293 return(ret); | 290 return(ret); |
294 } | 291 } |
295 | 292 |
296 static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, | 293 static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, |
297 » » DSA *dsa) | 294 » » » DSA *dsa) |
298 { | 295 { |
299 BN_CTX *ctx; | 296 BN_CTX *ctx; |
300 BIGNUM u1,u2,t1; | 297 BIGNUM u1,u2,t1; |
301 BN_MONT_CTX *mont=NULL; | 298 BN_MONT_CTX *mont=NULL; |
302 » int ret = -1; | 299 » int ret = -1, i; |
303 if (!dsa->p || !dsa->q || !dsa->g) | 300 if (!dsa->p || !dsa->q || !dsa->g) |
304 { | 301 { |
305 DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_MISSING_PARAMETERS); | 302 DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_MISSING_PARAMETERS); |
306 return -1; | 303 return -1; |
307 } | 304 } |
308 | 305 |
309 » if (BN_num_bits(dsa->q) != 160) | 306 » i = BN_num_bits(dsa->q); |
| 307 » /* fips 186-3 allows only different sizes for q */ |
| 308 » if (i != 160 && i != 224 && i != 256) |
310 { | 309 { |
311 DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_BAD_Q_VALUE); | 310 DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_BAD_Q_VALUE); |
312 return -1; | 311 return -1; |
313 } | 312 } |
314 | 313 |
315 if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) | 314 if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) |
316 { | 315 { |
317 DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_MODULUS_TOO_LARGE); | 316 DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_MODULUS_TOO_LARGE); |
318 return -1; | 317 return -1; |
319 } | 318 } |
320 | |
321 BN_init(&u1); | 319 BN_init(&u1); |
322 BN_init(&u2); | 320 BN_init(&u2); |
323 BN_init(&t1); | 321 BN_init(&t1); |
324 | 322 |
325 if ((ctx=BN_CTX_new()) == NULL) goto err; | 323 if ((ctx=BN_CTX_new()) == NULL) goto err; |
326 | 324 |
327 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || | 325 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || |
328 BN_ucmp(sig->r, dsa->q) >= 0) | 326 BN_ucmp(sig->r, dsa->q) >= 0) |
329 { | 327 { |
330 ret = 0; | 328 ret = 0; |
331 goto err; | 329 goto err; |
332 } | 330 } |
333 if (BN_is_zero(sig->s) || BN_is_negative(sig->s) || | 331 if (BN_is_zero(sig->s) || BN_is_negative(sig->s) || |
334 BN_ucmp(sig->s, dsa->q) >= 0) | 332 BN_ucmp(sig->s, dsa->q) >= 0) |
335 { | 333 { |
336 ret = 0; | 334 ret = 0; |
337 goto err; | 335 goto err; |
338 } | 336 } |
339 | 337 |
340 /* Calculate W = inv(S) mod Q | 338 /* Calculate W = inv(S) mod Q |
341 * save W in u2 */ | 339 * save W in u2 */ |
342 if ((BN_mod_inverse(&u2,sig->s,dsa->q,ctx)) == NULL) goto err; | 340 if ((BN_mod_inverse(&u2,sig->s,dsa->q,ctx)) == NULL) goto err; |
343 | 341 |
344 /* save M in u1 */ | 342 /* save M in u1 */ |
| 343 if (dgst_len > (i >> 3)) |
| 344 /* if the digest length is greater than the size of q use the |
| 345 * BN_num_bits(dsa->q) leftmost bits of the digest, see |
| 346 * fips 186-3, 4.2 */ |
| 347 dgst_len = (i >> 3); |
345 if (BN_bin2bn(dgst,dgst_len,&u1) == NULL) goto err; | 348 if (BN_bin2bn(dgst,dgst_len,&u1) == NULL) goto err; |
346 | 349 |
347 /* u1 = M * w mod q */ | 350 /* u1 = M * w mod q */ |
348 if (!BN_mod_mul(&u1,&u1,&u2,dsa->q,ctx)) goto err; | 351 if (!BN_mod_mul(&u1,&u1,&u2,dsa->q,ctx)) goto err; |
349 | 352 |
350 /* u2 = r * w mod q */ | 353 /* u2 = r * w mod q */ |
351 if (!BN_mod_mul(&u2,sig->r,&u2,dsa->q,ctx)) goto err; | 354 if (!BN_mod_mul(&u2,sig->r,&u2,dsa->q,ctx)) goto err; |
352 | 355 |
353 | 356 |
354 if (dsa->flags & DSA_FLAG_CACHE_MONT_P) | 357 if (dsa->flags & DSA_FLAG_CACHE_MONT_P) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 return(1); | 389 return(1); |
387 } | 390 } |
388 | 391 |
389 static int dsa_finish(DSA *dsa) | 392 static int dsa_finish(DSA *dsa) |
390 { | 393 { |
391 if(dsa->method_mont_p) | 394 if(dsa->method_mont_p) |
392 BN_MONT_CTX_free(dsa->method_mont_p); | 395 BN_MONT_CTX_free(dsa->method_mont_p); |
393 return(1); | 396 return(1); |
394 } | 397 } |
395 | 398 |
396 #endif | |
OLD | NEW |