summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/dsa/dsa_ameth.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/dsa/dsa_ameth.c33
1 files changed, 22 insertions, 11 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:
242dsaerr: 244dsaerr:
243 DSA_free(dsa); 245 DSA_free(dsa);
244done: 246done:
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;