diff options
| author | jsing <> | 2023-01-11 04:35:26 +0000 |
|---|---|---|
| committer | jsing <> | 2023-01-11 04:35:26 +0000 |
| commit | eb6711ff03a552477038555c20ec570d7d13bc2b (patch) | |
| tree | 693f7557b15a88c16bf6664a8cb90d688f4a17e9 /src/lib/libc | |
| parent | 8dd331f8906d93b3666014c96fd7a1b2d8b05067 (diff) | |
| download | openbsd-eb6711ff03a552477038555c20ec570d7d13bc2b.tar.gz openbsd-eb6711ff03a552477038555c20ec570d7d13bc2b.tar.bz2 openbsd-eb6711ff03a552477038555c20ec570d7d13bc2b.zip | |
Simplify BIGNUM handling in dsa_builtin_keygen().
Rather than having complicated "attempt to reuse" dances, always allocate
priv_key/pub_key, then free and assign on success.
ok tb@
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_key.c | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/src/lib/libcrypto/dsa/dsa_key.c b/src/lib/libcrypto/dsa/dsa_key.c index 051c812781..890f991df9 100644 --- a/src/lib/libcrypto/dsa/dsa_key.c +++ b/src/lib/libcrypto/dsa/dsa_key.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: dsa_key.c,v 1.32 2022/11/26 16:08:52 tb Exp $ */ | 1 | /* $OpenBSD: dsa_key.c,v 1.33 2023/01/11 04:35:26 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 | * |
| @@ -82,39 +82,38 @@ DSA_generate_key(DSA *dsa) | |||
| 82 | static int | 82 | static int |
| 83 | dsa_builtin_keygen(DSA *dsa) | 83 | dsa_builtin_keygen(DSA *dsa) |
| 84 | { | 84 | { |
| 85 | int ok = 0; | ||
| 86 | BN_CTX *ctx = NULL; | ||
| 87 | BIGNUM *pub_key = NULL, *priv_key = NULL; | 85 | BIGNUM *pub_key = NULL, *priv_key = NULL; |
| 86 | BN_CTX *ctx = NULL; | ||
| 87 | int ok = 0; | ||
| 88 | 88 | ||
| 89 | if ((ctx = BN_CTX_new()) == NULL) | 89 | if ((priv_key = BN_new()) == NULL) |
| 90 | goto err; | ||
| 91 | if ((pub_key = BN_new()) == NULL) | ||
| 90 | goto err; | 92 | goto err; |
| 91 | 93 | ||
| 92 | if ((priv_key = dsa->priv_key) == NULL) { | 94 | if ((ctx = BN_CTX_new()) == NULL) |
| 93 | if ((priv_key = BN_new()) == NULL) | 95 | goto err; |
| 94 | goto err; | ||
| 95 | } | ||
| 96 | 96 | ||
| 97 | if (!bn_rand_interval(priv_key, BN_value_one(), dsa->q)) | 97 | if (!bn_rand_interval(priv_key, BN_value_one(), dsa->q)) |
| 98 | goto err; | 98 | goto err; |
| 99 | |||
| 100 | if ((pub_key = dsa->pub_key) == NULL) { | ||
| 101 | if ((pub_key = BN_new()) == NULL) | ||
| 102 | goto err; | ||
| 103 | } | ||
| 104 | |||
| 105 | if (!BN_mod_exp_ct(pub_key, dsa->g, priv_key, dsa->p, ctx)) | 99 | if (!BN_mod_exp_ct(pub_key, dsa->g, priv_key, dsa->p, ctx)) |
| 106 | goto err; | 100 | goto err; |
| 107 | 101 | ||
| 102 | BN_free(dsa->priv_key); | ||
| 108 | dsa->priv_key = priv_key; | 103 | dsa->priv_key = priv_key; |
| 104 | priv_key = NULL; | ||
| 105 | |||
| 106 | BN_free(dsa->pub_key); | ||
| 109 | dsa->pub_key = pub_key; | 107 | dsa->pub_key = pub_key; |
| 108 | pub_key = NULL; | ||
| 109 | |||
| 110 | ok = 1; | 110 | ok = 1; |
| 111 | 111 | ||
| 112 | err: | 112 | err: |
| 113 | if (dsa->pub_key == NULL) | 113 | BN_free(pub_key); |
| 114 | BN_free(pub_key); | 114 | BN_free(priv_key); |
| 115 | if (dsa->priv_key == NULL) | ||
| 116 | BN_free(priv_key); | ||
| 117 | BN_CTX_free(ctx); | 115 | BN_CTX_free(ctx); |
| 116 | |||
| 118 | return ok; | 117 | return ok; |
| 119 | } | 118 | } |
| 120 | #endif | 119 | #endif |
