diff options
author | jsing <> | 2023-01-11 04:39:42 +0000 |
---|---|---|
committer | jsing <> | 2023-01-11 04:39:42 +0000 |
commit | 74f1269e0cf9abe4f2b70a0ba26461fafac75cd2 (patch) | |
tree | 5fe3fd82569c7f9e8a9a1295ec3fda45956a6144 | |
parent | e4eca83b70194cd4d7b511bbdb16dec48f17bf00 (diff) | |
download | openbsd-74f1269e0cf9abe4f2b70a0ba26461fafac75cd2.tar.gz openbsd-74f1269e0cf9abe4f2b70a0ba26461fafac75cd2.tar.bz2 openbsd-74f1269e0cf9abe4f2b70a0ba26461fafac75cd2.zip |
Clean up and simplify BIGNUM handling in DSA code.
This adds missing BN_CTX_start()/BN_CTX_end() calls, removes NULL checks
before BN_CTX_end()/BN_CTX_free() (since they're NULL safe) and calls
BN_free() instead of BN_clear_free() (which does the same thing).
Also replace stack allocated BIGNUMs with calls to BN_CTX_get(), using the
BN_CTX that is already available.
ok tb@
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_ameth.c | 33 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_gen.c | 14 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_ossl.c | 165 |
3 files changed, 116 insertions, 96 deletions
diff --git a/src/lib/libcrypto/dsa/dsa_ameth.c b/src/lib/libcrypto/dsa/dsa_ameth.c index fb333dda0f..0d3333d92c 100644 --- a/src/lib/libcrypto/dsa/dsa_ameth.c +++ b/src/lib/libcrypto/dsa/dsa_ameth.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: dsa_ameth.c,v 1.38 2022/11/26 16:08:52 tb Exp $ */ | 1 | /* $OpenBSD: dsa_ameth.c,v 1.39 2023/01/11 04:39:42 jsing Exp $ */ |
2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
3 | * project 2006. | 3 | * project 2006. |
4 | */ | 4 | */ |
@@ -192,7 +192,6 @@ dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8) | |||
192 | ASN1_INTEGER *privkey = NULL; | 192 | ASN1_INTEGER *privkey = NULL; |
193 | BN_CTX *ctx = NULL; | 193 | BN_CTX *ctx = NULL; |
194 | DSA *dsa = NULL; | 194 | DSA *dsa = NULL; |
195 | |||
196 | int ret = 0; | 195 | int ret = 0; |
197 | 196 | ||
198 | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) | 197 | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) |
@@ -221,11 +220,14 @@ dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8) | |||
221 | DSAerror(ERR_R_MALLOC_FAILURE); | 220 | DSAerror(ERR_R_MALLOC_FAILURE); |
222 | goto dsaerr; | 221 | goto dsaerr; |
223 | } | 222 | } |
224 | if (!(ctx = BN_CTX_new())) { | 223 | |
224 | if ((ctx = BN_CTX_new()) == NULL) { | ||
225 | DSAerror(ERR_R_MALLOC_FAILURE); | 225 | DSAerror(ERR_R_MALLOC_FAILURE); |
226 | goto dsaerr; | 226 | goto dsaerr; |
227 | } | 227 | } |
228 | 228 | ||
229 | BN_CTX_start(ctx); | ||
230 | |||
229 | if (!BN_mod_exp_ct(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) { | 231 | if (!BN_mod_exp_ct(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) { |
230 | DSAerror(DSA_R_BN_ERROR); | 232 | DSAerror(DSA_R_BN_ERROR); |
231 | goto dsaerr; | 233 | goto dsaerr; |
@@ -242,8 +244,10 @@ decerr: | |||
242 | dsaerr: | 244 | dsaerr: |
243 | DSA_free(dsa); | 245 | DSA_free(dsa); |
244 | done: | 246 | done: |
247 | BN_CTX_end(ctx); | ||
245 | BN_CTX_free(ctx); | 248 | BN_CTX_free(ctx); |
246 | ASN1_INTEGER_free(privkey); | 249 | ASN1_INTEGER_free(privkey); |
250 | |||
247 | return ret; | 251 | return ret; |
248 | } | 252 | } |
249 | 253 | ||
@@ -511,26 +515,31 @@ old_dsa_priv_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) | |||
511 | goto err; | 515 | goto err; |
512 | } | 516 | } |
513 | 517 | ||
514 | ctx = BN_CTX_new(); | 518 | if ((ctx = BN_CTX_new()) == NULL) |
515 | if (ctx == NULL) | ||
516 | goto err; | 519 | goto err; |
517 | 520 | ||
521 | BN_CTX_start(ctx); | ||
522 | |||
518 | /* | 523 | /* |
519 | * Check that p and q are consistent with each other. | 524 | * Check that p and q are consistent with each other. |
520 | */ | 525 | */ |
521 | 526 | if ((j = BN_CTX_get(ctx)) == NULL) | |
522 | j = BN_CTX_get(ctx); | ||
523 | p1 = BN_CTX_get(ctx); | ||
524 | newp1 = BN_CTX_get(ctx); | ||
525 | powg = BN_CTX_get(ctx); | ||
526 | if (j == NULL || p1 == NULL || newp1 == NULL || powg == NULL) | ||
527 | goto err; | 527 | goto err; |
528 | if ((p1 = BN_CTX_get(ctx)) == NULL) | ||
529 | goto err; | ||
530 | if ((newp1 = BN_CTX_get(ctx)) == NULL) | ||
531 | goto err; | ||
532 | if ((powg = BN_CTX_get(ctx)) == NULL) | ||
533 | goto err; | ||
534 | |||
528 | /* p1 = p - 1 */ | 535 | /* p1 = p - 1 */ |
529 | if (BN_sub(p1, dsa->p, BN_value_one()) == 0) | 536 | if (BN_sub(p1, dsa->p, BN_value_one()) == 0) |
530 | goto err; | 537 | goto err; |
538 | |||
531 | /* j = (p - 1) / q */ | 539 | /* j = (p - 1) / q */ |
532 | if (BN_div_ct(j, NULL, p1, dsa->q, ctx) == 0) | 540 | if (BN_div_ct(j, NULL, p1, dsa->q, ctx) == 0) |
533 | goto err; | 541 | goto err; |
542 | |||
534 | /* q * j should == p - 1 */ | 543 | /* q * j should == p - 1 */ |
535 | if (BN_mul(newp1, dsa->q, j, ctx) == 0) | 544 | if (BN_mul(newp1, dsa->q, j, ctx) == 0) |
536 | goto err; | 545 | goto err; |
@@ -561,12 +570,14 @@ old_dsa_priv_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) | |||
561 | goto err; | 570 | goto err; |
562 | } | 571 | } |
563 | 572 | ||
573 | BN_CTX_end(ctx); | ||
564 | BN_CTX_free(ctx); | 574 | BN_CTX_free(ctx); |
565 | 575 | ||
566 | EVP_PKEY_assign_DSA(pkey, dsa); | 576 | EVP_PKEY_assign_DSA(pkey, dsa); |
567 | return 1; | 577 | return 1; |
568 | 578 | ||
569 | err: | 579 | err: |
580 | BN_CTX_end(ctx); | ||
570 | BN_CTX_free(ctx); | 581 | BN_CTX_free(ctx); |
571 | DSA_free(dsa); | 582 | DSA_free(dsa); |
572 | return 0; | 583 | return 0; |
diff --git a/src/lib/libcrypto/dsa/dsa_gen.c b/src/lib/libcrypto/dsa/dsa_gen.c index 9c2b9cfd70..1f91894100 100644 --- a/src/lib/libcrypto/dsa/dsa_gen.c +++ b/src/lib/libcrypto/dsa/dsa_gen.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: dsa_gen.c,v 1.26 2022/11/26 16:08:52 tb Exp $ */ | 1 | /* $OpenBSD: dsa_gen.c,v 1.27 2023/01/11 04:39:42 jsing Exp $ */ |
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 | * |
@@ -142,11 +142,12 @@ dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits, const EVP_MD *evpmd, | |||
142 | else if (seed_len != 0) | 142 | else if (seed_len != 0) |
143 | goto err; | 143 | goto err; |
144 | 144 | ||
145 | if ((mont=BN_MONT_CTX_new()) == NULL) | 145 | if ((mont = BN_MONT_CTX_new()) == NULL) |
146 | goto err; | 146 | goto err; |
147 | 147 | ||
148 | if ((ctx=BN_CTX_new()) == NULL) | 148 | if ((ctx = BN_CTX_new()) == NULL) |
149 | goto err; | 149 | goto err; |
150 | |||
150 | BN_CTX_start(ctx); | 151 | BN_CTX_start(ctx); |
151 | 152 | ||
152 | if ((r0 = BN_CTX_get(ctx)) == NULL) | 153 | if ((r0 = BN_CTX_get(ctx)) == NULL) |
@@ -348,11 +349,10 @@ err: | |||
348 | if (seed_out != NULL) | 349 | if (seed_out != NULL) |
349 | memcpy(seed_out, seed, qsize); | 350 | memcpy(seed_out, seed, qsize); |
350 | } | 351 | } |
351 | if (ctx) { | 352 | BN_CTX_end(ctx); |
352 | BN_CTX_end(ctx); | 353 | BN_CTX_free(ctx); |
353 | BN_CTX_free(ctx); | ||
354 | } | ||
355 | BN_MONT_CTX_free(mont); | 354 | BN_MONT_CTX_free(mont); |
355 | |||
356 | return ok; | 356 | return ok; |
357 | } | 357 | } |
358 | #endif | 358 | #endif |
diff --git a/src/lib/libcrypto/dsa/dsa_ossl.c b/src/lib/libcrypto/dsa/dsa_ossl.c index 102bc44cd7..a242291a62 100644 --- a/src/lib/libcrypto/dsa/dsa_ossl.c +++ b/src/lib/libcrypto/dsa/dsa_ossl.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: dsa_ossl.c,v 1.46 2022/11/26 16:08:52 tb Exp $ */ | 1 | /* $OpenBSD: dsa_ossl.c,v 1.47 2023/01/11 04:39:42 jsing Exp $ */ |
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 | * |
@@ -95,28 +95,35 @@ DSA_OpenSSL(void) | |||
95 | static DSA_SIG * | 95 | static DSA_SIG * |
96 | dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) | 96 | dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) |
97 | { | 97 | { |
98 | BIGNUM b, bm, bxr, binv, m, *kinv = NULL, *r = NULL, *s = NULL; | 98 | BIGNUM *b = NULL, *bm = NULL, *bxr = NULL, *binv = NULL, *m = NULL; |
99 | BIGNUM *kinv = NULL, *r = NULL, *s = NULL; | ||
99 | BN_CTX *ctx = NULL; | 100 | BN_CTX *ctx = NULL; |
100 | int reason = ERR_R_BN_LIB; | 101 | int reason = ERR_R_BN_LIB; |
101 | DSA_SIG *ret = NULL; | 102 | DSA_SIG *ret = NULL; |
102 | int noredo = 0; | 103 | int noredo = 0; |
103 | 104 | ||
104 | BN_init(&b); | 105 | if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) { |
105 | BN_init(&binv); | ||
106 | BN_init(&bm); | ||
107 | BN_init(&bxr); | ||
108 | BN_init(&m); | ||
109 | |||
110 | if (!dsa->p || !dsa->q || !dsa->g) { | ||
111 | reason = DSA_R_MISSING_PARAMETERS; | 106 | reason = DSA_R_MISSING_PARAMETERS; |
112 | goto err; | 107 | goto err; |
113 | } | 108 | } |
114 | 109 | ||
115 | s = BN_new(); | 110 | if ((s = BN_new()) == NULL) |
116 | if (s == NULL) | ||
117 | goto err; | 111 | goto err; |
118 | ctx = BN_CTX_new(); | 112 | |
119 | if (ctx == NULL) | 113 | if ((ctx = BN_CTX_new()) == NULL) |
114 | goto err; | ||
115 | |||
116 | BN_CTX_start(ctx); | ||
117 | |||
118 | if ((b = BN_CTX_get(ctx)) == NULL) | ||
119 | goto err; | ||
120 | if ((binv = BN_CTX_get(ctx)) == NULL) | ||
121 | goto err; | ||
122 | if ((bm = BN_CTX_get(ctx)) == NULL) | ||
123 | goto err; | ||
124 | if ((bxr = BN_CTX_get(ctx)) == NULL) | ||
125 | goto err; | ||
126 | if ((m = BN_CTX_get(ctx)) == NULL) | ||
120 | goto err; | 127 | goto err; |
121 | 128 | ||
122 | /* | 129 | /* |
@@ -126,7 +133,7 @@ dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) | |||
126 | */ | 133 | */ |
127 | if (dlen > BN_num_bytes(dsa->q)) | 134 | if (dlen > BN_num_bytes(dsa->q)) |
128 | dlen = BN_num_bytes(dsa->q); | 135 | dlen = BN_num_bytes(dsa->q); |
129 | if (BN_bin2bn(dgst, dlen, &m) == NULL) | 136 | if (BN_bin2bn(dgst, dlen, m) == NULL) |
130 | goto err; | 137 | goto err; |
131 | 138 | ||
132 | redo: | 139 | redo: |
@@ -153,22 +160,22 @@ dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) | |||
153 | * | 160 | * |
154 | * Where b is a random value in the range [1, q). | 161 | * Where b is a random value in the range [1, q). |
155 | */ | 162 | */ |
156 | if (!bn_rand_interval(&b, BN_value_one(), dsa->q)) | 163 | if (!bn_rand_interval(b, BN_value_one(), dsa->q)) |
157 | goto err; | 164 | goto err; |
158 | if (BN_mod_inverse_ct(&binv, &b, dsa->q, ctx) == NULL) | 165 | if (BN_mod_inverse_ct(binv, b, dsa->q, ctx) == NULL) |
159 | goto err; | 166 | goto err; |
160 | 167 | ||
161 | if (!BN_mod_mul(&bxr, &b, dsa->priv_key, dsa->q, ctx)) /* bx */ | 168 | if (!BN_mod_mul(bxr, b, dsa->priv_key, dsa->q, ctx)) /* bx */ |
162 | goto err; | 169 | goto err; |
163 | if (!BN_mod_mul(&bxr, &bxr, r, dsa->q, ctx)) /* bxr */ | 170 | if (!BN_mod_mul(bxr, bxr, r, dsa->q, ctx)) /* bxr */ |
164 | goto err; | 171 | goto err; |
165 | if (!BN_mod_mul(&bm, &b, &m, dsa->q, ctx)) /* bm */ | 172 | if (!BN_mod_mul(bm, b, m, dsa->q, ctx)) /* bm */ |
166 | goto err; | 173 | goto err; |
167 | if (!BN_mod_add(s, &bxr, &bm, dsa->q, ctx)) /* s = bm + bxr */ | 174 | if (!BN_mod_add(s, bxr, bm, dsa->q, ctx)) /* s = bm + bxr */ |
168 | goto err; | 175 | goto err; |
169 | if (!BN_mod_mul(s, s, kinv, dsa->q, ctx)) /* s = b(m + xr)k^-1 */ | 176 | if (!BN_mod_mul(s, s, kinv, dsa->q, ctx)) /* s = b(m + xr)k^-1 */ |
170 | goto err; | 177 | goto err; |
171 | if (!BN_mod_mul(s, s, &binv, dsa->q, ctx)) /* s = (m + xr)k^-1 */ | 178 | if (!BN_mod_mul(s, s, binv, dsa->q, ctx)) /* s = (m + xr)k^-1 */ |
172 | goto err; | 179 | goto err; |
173 | 180 | ||
174 | /* | 181 | /* |
@@ -196,13 +203,9 @@ dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) | |||
196 | BN_free(r); | 203 | BN_free(r); |
197 | BN_free(s); | 204 | BN_free(s); |
198 | } | 205 | } |
206 | BN_CTX_end(ctx); | ||
199 | BN_CTX_free(ctx); | 207 | BN_CTX_free(ctx); |
200 | BN_clear_free(&b); | 208 | BN_free(kinv); |
201 | BN_clear_free(&bm); | ||
202 | BN_clear_free(&bxr); | ||
203 | BN_clear_free(&binv); | ||
204 | BN_clear_free(&m); | ||
205 | BN_clear_free(kinv); | ||
206 | 209 | ||
207 | return ret; | 210 | return ret; |
208 | } | 211 | } |
@@ -210,39 +213,44 @@ dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) | |||
210 | static int | 213 | static int |
211 | dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) | 214 | dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) |
212 | { | 215 | { |
213 | BN_CTX *ctx; | 216 | BIGNUM *k = NULL, *l = NULL, *m = NULL, *kinv = NULL, *r = NULL; |
214 | BIGNUM k, l, m, *kinv = NULL, *r = NULL; | 217 | BN_CTX *ctx = NULL; |
215 | int q_bits, ret = 0; | 218 | int q_bits; |
219 | int ret = 0; | ||
216 | 220 | ||
217 | if (!dsa->p || !dsa->q || !dsa->g) { | 221 | if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) { |
218 | DSAerror(DSA_R_MISSING_PARAMETERS); | 222 | DSAerror(DSA_R_MISSING_PARAMETERS); |
219 | return 0; | 223 | return 0; |
220 | } | 224 | } |
221 | 225 | ||
222 | BN_init(&k); | 226 | if ((r = BN_new()) == NULL) |
223 | BN_init(&l); | 227 | goto err; |
224 | BN_init(&m); | ||
225 | 228 | ||
226 | if (ctx_in == NULL) { | 229 | if ((ctx = ctx_in) == NULL) |
227 | if ((ctx = BN_CTX_new()) == NULL) | 230 | ctx = BN_CTX_new(); |
228 | goto err; | 231 | if (ctx == NULL) |
229 | } else | 232 | goto err; |
230 | ctx = ctx_in; | ||
231 | 233 | ||
232 | if ((r = BN_new()) == NULL) | 234 | BN_CTX_start(ctx); |
235 | |||
236 | if ((k = BN_CTX_get(ctx)) == NULL) | ||
237 | goto err; | ||
238 | if ((l = BN_CTX_get(ctx)) == NULL) | ||
239 | goto err; | ||
240 | if ((m = BN_CTX_get(ctx)) == NULL) | ||
233 | goto err; | 241 | goto err; |
234 | 242 | ||
235 | /* Preallocate space */ | 243 | /* Preallocate space */ |
236 | q_bits = BN_num_bits(dsa->q); | 244 | q_bits = BN_num_bits(dsa->q); |
237 | if (!BN_set_bit(&k, q_bits) || | 245 | if (!BN_set_bit(k, q_bits) || |
238 | !BN_set_bit(&l, q_bits) || | 246 | !BN_set_bit(l, q_bits) || |
239 | !BN_set_bit(&m, q_bits)) | 247 | !BN_set_bit(m, q_bits)) |
240 | goto err; | 248 | goto err; |
241 | 249 | ||
242 | if (!bn_rand_interval(&k, BN_value_one(), dsa->q)) | 250 | if (!bn_rand_interval(k, BN_value_one(), dsa->q)) |
243 | goto err; | 251 | goto err; |
244 | 252 | ||
245 | BN_set_flags(&k, BN_FLG_CONSTTIME); | 253 | BN_set_flags(k, BN_FLG_CONSTTIME); |
246 | 254 | ||
247 | if (dsa->flags & DSA_FLAG_CACHE_MONT_P) { | 255 | if (dsa->flags & DSA_FLAG_CACHE_MONT_P) { |
248 | if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, | 256 | if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, |
@@ -265,17 +273,17 @@ dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) | |||
265 | * conditional copy. | 273 | * conditional copy. |
266 | */ | 274 | */ |
267 | 275 | ||
268 | if (!BN_add(&l, &k, dsa->q) || | 276 | if (!BN_add(l, k, dsa->q) || |
269 | !BN_add(&m, &l, dsa->q) || | 277 | !BN_add(m, l, dsa->q) || |
270 | !BN_copy(&k, BN_num_bits(&l) > q_bits ? &l : &m)) | 278 | !BN_copy(k, BN_num_bits(l) > q_bits ? l : m)) |
271 | goto err; | 279 | goto err; |
272 | 280 | ||
273 | if (dsa->meth->bn_mod_exp != NULL) { | 281 | if (dsa->meth->bn_mod_exp != NULL) { |
274 | if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, &k, dsa->p, ctx, | 282 | if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, k, dsa->p, ctx, |
275 | dsa->method_mont_p)) | 283 | dsa->method_mont_p)) |
276 | goto err; | 284 | goto err; |
277 | } else { | 285 | } else { |
278 | if (!BN_mod_exp_mont_ct(r, dsa->g, &k, dsa->p, ctx, | 286 | if (!BN_mod_exp_mont_ct(r, dsa->g, k, dsa->p, ctx, |
279 | dsa->method_mont_p)) | 287 | dsa->method_mont_p)) |
280 | goto err; | 288 | goto err; |
281 | } | 289 | } |
@@ -284,13 +292,14 @@ dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) | |||
284 | goto err; | 292 | goto err; |
285 | 293 | ||
286 | /* Compute part of 's = inv(k) (m + xr) mod q' */ | 294 | /* Compute part of 's = inv(k) (m + xr) mod q' */ |
287 | if ((kinv = BN_mod_inverse_ct(NULL, &k, dsa->q, ctx)) == NULL) | 295 | if ((kinv = BN_mod_inverse_ct(NULL, k, dsa->q, ctx)) == NULL) |
288 | goto err; | 296 | goto err; |
289 | 297 | ||
290 | BN_clear_free(*kinvp); | 298 | BN_free(*kinvp); |
291 | *kinvp = kinv; | 299 | *kinvp = kinv; |
292 | kinv = NULL; | 300 | kinv = NULL; |
293 | BN_clear_free(*rp); | 301 | |
302 | BN_free(*rp); | ||
294 | *rp = r; | 303 | *rp = r; |
295 | 304 | ||
296 | ret = 1; | 305 | ret = 1; |
@@ -298,13 +307,11 @@ dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) | |||
298 | err: | 307 | err: |
299 | if (!ret) { | 308 | if (!ret) { |
300 | DSAerror(ERR_R_BN_LIB); | 309 | DSAerror(ERR_R_BN_LIB); |
301 | BN_clear_free(r); | 310 | BN_free(r); |
302 | } | 311 | } |
303 | if (ctx_in == NULL) | 312 | BN_CTX_end(ctx); |
313 | if (ctx != ctx_in) | ||
304 | BN_CTX_free(ctx); | 314 | BN_CTX_free(ctx); |
305 | BN_clear_free(&k); | ||
306 | BN_clear_free(&l); | ||
307 | BN_clear_free(&m); | ||
308 | 315 | ||
309 | return ret; | 316 | return ret; |
310 | } | 317 | } |
@@ -312,13 +319,13 @@ dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) | |||
312 | static int | 319 | static int |
313 | dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa) | 320 | dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa) |
314 | { | 321 | { |
315 | BN_CTX *ctx; | 322 | BIGNUM *u1 = NULL, *u2 = NULL, *t1 = NULL; |
316 | BIGNUM u1, u2, t1; | 323 | BN_CTX *ctx = NULL; |
317 | BN_MONT_CTX *mont = NULL; | 324 | BN_MONT_CTX *mont = NULL; |
318 | int qbits; | 325 | int qbits; |
319 | int ret = -1; | 326 | int ret = -1; |
320 | 327 | ||
321 | if (!dsa->p || !dsa->q || !dsa->g) { | 328 | if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) { |
322 | DSAerror(DSA_R_MISSING_PARAMETERS); | 329 | DSAerror(DSA_R_MISSING_PARAMETERS); |
323 | return -1; | 330 | return -1; |
324 | } | 331 | } |
@@ -334,13 +341,18 @@ dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa) | |||
334 | return -1; | 341 | return -1; |
335 | } | 342 | } |
336 | 343 | ||
337 | BN_init(&u1); | ||
338 | BN_init(&u2); | ||
339 | BN_init(&t1); | ||
340 | |||
341 | if ((ctx = BN_CTX_new()) == NULL) | 344 | if ((ctx = BN_CTX_new()) == NULL) |
342 | goto err; | 345 | goto err; |
343 | 346 | ||
347 | BN_CTX_start(ctx); | ||
348 | |||
349 | if ((u1 = BN_CTX_get(ctx)) == NULL) | ||
350 | goto err; | ||
351 | if ((u2 = BN_CTX_get(ctx)) == NULL) | ||
352 | goto err; | ||
353 | if ((t1 = BN_CTX_get(ctx)) == NULL) | ||
354 | goto err; | ||
355 | |||
344 | if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || | 356 | if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || |
345 | BN_ucmp(sig->r, dsa->q) >= 0) { | 357 | BN_ucmp(sig->r, dsa->q) >= 0) { |
346 | ret = 0; | 358 | ret = 0; |
@@ -353,7 +365,7 @@ dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa) | |||
353 | } | 365 | } |
354 | 366 | ||
355 | /* Calculate w = inv(s) mod q, saving w in u2. */ | 367 | /* Calculate w = inv(s) mod q, saving w in u2. */ |
356 | if ((BN_mod_inverse_ct(&u2, sig->s, dsa->q, ctx)) == NULL) | 368 | if ((BN_mod_inverse_ct(u2, sig->s, dsa->q, ctx)) == NULL) |
357 | goto err; | 369 | goto err; |
358 | 370 | ||
359 | /* | 371 | /* |
@@ -364,15 +376,15 @@ dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa) | |||
364 | dgst_len = (qbits >> 3); | 376 | dgst_len = (qbits >> 3); |
365 | 377 | ||
366 | /* Save m in u1. */ | 378 | /* Save m in u1. */ |
367 | if (BN_bin2bn(dgst, dgst_len, &u1) == NULL) | 379 | if (BN_bin2bn(dgst, dgst_len, u1) == NULL) |
368 | goto err; | 380 | goto err; |
369 | 381 | ||
370 | /* u1 = m * w mod q */ | 382 | /* u1 = m * w mod q */ |
371 | if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) | 383 | if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx)) |
372 | goto err; | 384 | goto err; |
373 | 385 | ||
374 | /* u2 = r * w mod q */ | 386 | /* u2 = r * w mod q */ |
375 | if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) | 387 | if (!BN_mod_mul(u2, sig->r, u2, dsa->q, ctx)) |
376 | goto err; | 388 | goto err; |
377 | 389 | ||
378 | if (dsa->flags & DSA_FLAG_CACHE_MONT_P) { | 390 | if (dsa->flags & DSA_FLAG_CACHE_MONT_P) { |
@@ -383,30 +395,27 @@ dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa) | |||
383 | } | 395 | } |
384 | 396 | ||
385 | if (dsa->meth->dsa_mod_exp != NULL) { | 397 | if (dsa->meth->dsa_mod_exp != NULL) { |
386 | if (!dsa->meth->dsa_mod_exp(dsa, &t1, dsa->g, &u1, dsa->pub_key, | 398 | if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->g, u1, dsa->pub_key, |
387 | &u2, dsa->p, ctx, mont)) | 399 | u2, dsa->p, ctx, mont)) |
388 | goto err; | 400 | goto err; |
389 | } else { | 401 | } else { |
390 | if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, | 402 | if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, |
391 | dsa->p, ctx, mont)) | 403 | dsa->p, ctx, mont)) |
392 | goto err; | 404 | goto err; |
393 | } | 405 | } |
394 | 406 | ||
395 | /* BN_copy(&u1,&t1); */ | ||
396 | /* let u1 = u1 mod q */ | 407 | /* let u1 = u1 mod q */ |
397 | if (!BN_mod_ct(&u1, &t1, dsa->q, ctx)) | 408 | if (!BN_mod_ct(u1, t1, dsa->q, ctx)) |
398 | goto err; | 409 | goto err; |
399 | 410 | ||
400 | /* v is in u1 - if the signature is correct, it will be equal to r. */ | 411 | /* v is in u1 - if the signature is correct, it will be equal to r. */ |
401 | ret = BN_ucmp(&u1, sig->r) == 0; | 412 | ret = BN_ucmp(u1, sig->r) == 0; |
402 | 413 | ||
403 | err: | 414 | err: |
404 | if (ret < 0) | 415 | if (ret < 0) |
405 | DSAerror(ERR_R_BN_LIB); | 416 | DSAerror(ERR_R_BN_LIB); |
417 | BN_CTX_end(ctx); | ||
406 | BN_CTX_free(ctx); | 418 | BN_CTX_free(ctx); |
407 | BN_free(&u1); | ||
408 | BN_free(&u2); | ||
409 | BN_free(&t1); | ||
410 | 419 | ||
411 | return ret; | 420 | return ret; |
412 | } | 421 | } |