diff options
author | Tomas Mraz <tomas@openssl.org> | 2021-04-26 12:08:27 +0200 |
---|---|---|
committer | Tomas Mraz <tomas@openssl.org> | 2021-04-28 09:38:31 +0200 |
commit | eaf8a40d97d642ccd2c55fbf8bb8ee3242aec04a (patch) | |
tree | 23ef2d3756c42a91841270eb74330a8840dbf5d0 /apps/speed.c | |
parent | c0a79e9836a9aa30912978f69fab3b3bb3a8ddc5 (diff) |
Prefer fetch over legacy get_digestby/get_cipherby
Fixes #14198
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15028)
Diffstat (limited to 'apps/speed.c')
-rw-r--r-- | apps/speed.c | 86 |
1 files changed, 21 insertions, 65 deletions
diff --git a/apps/speed.c b/apps/speed.c index 939baf934d..5363b0d7f8 100644 --- a/apps/speed.c +++ b/apps/speed.c @@ -502,73 +502,35 @@ static const char *evp_md_name = NULL; static char *evp_mac_ciphername = "aes-128-cbc"; static char *evp_cmac_name = NULL; -static EVP_MD *obtain_md(const char *name, int *fetched) -{ - EVP_MD *md = NULL; - - *fetched = 0; - /* Look through providers' digests */ - ERR_set_mark(); - md = EVP_MD_fetch(NULL, name, NULL); - ERR_pop_to_mark(); - if (md != NULL) { - *fetched = 1; - return md; - } - - return (EVP_MD *)EVP_get_digestbyname(name); -} - static int have_md(const char *name) { - int fetched = 0; int ret = 0; - EVP_MD *md = obtain_md(name, &fetched); + EVP_MD *md = NULL; - if (md != NULL) { + if (opt_md_silent(name, &md)) { EVP_MD_CTX *ctx = EVP_MD_CTX_new(); if (ctx != NULL && EVP_DigestInit(ctx, md) > 0) ret = 1; EVP_MD_CTX_free(ctx); - if (fetched) - EVP_MD_free(md); + EVP_MD_free(md); } return ret; } -static EVP_CIPHER *obtain_cipher(const char *name, int *fetched) -{ - EVP_CIPHER *cipher = NULL; - - *fetched = 0; - /* Look through providers' digests */ - ERR_set_mark(); - cipher = EVP_CIPHER_fetch(NULL, name, NULL); - ERR_pop_to_mark(); - if (cipher != NULL) { - *fetched = 1; - return cipher; - } - - return (EVP_CIPHER *)EVP_get_cipherbyname(name); -} - static int have_cipher(const char *name) { - int fetched = 0; int ret = 0; - EVP_CIPHER *cipher = obtain_cipher(name, &fetched); + EVP_CIPHER *cipher = NULL; - if (cipher != NULL) { + if (opt_cipher_silent(name, &cipher)) { EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); if (ctx != NULL && EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1) > 0) ret = 1; EVP_CIPHER_CTX_free(ctx); - if (fetched) - EVP_CIPHER_free(cipher); + EVP_CIPHER_free(cipher); } return ret; } @@ -578,10 +540,10 @@ static int EVP_Digest_loop(const char *mdname, int algindex, void *args) loopargs_t *tempargs = *(loopargs_t **) args; unsigned char *buf = tempargs->buf; unsigned char digest[EVP_MAX_MD_SIZE]; - int count, fetched = 0; - EVP_MD *md = obtain_md(mdname, &fetched); + int count; + EVP_MD *md = NULL; - if (md == NULL) + if (!opt_md_silent(mdname, &md)) return -1; for (count = 0; COND(c[algindex][testnum]); count++) { if (!EVP_Digest(buf, (size_t)lengths[testnum], digest, NULL, md, @@ -590,8 +552,7 @@ static int EVP_Digest_loop(const char *mdname, int algindex, void *args) break; } } - if (fetched) - EVP_MD_free(md); + EVP_MD_free(md); return count; } @@ -714,10 +675,9 @@ static EVP_CIPHER_CTX *init_evp_cipher_ctx(const char *ciphername, int keylen) { EVP_CIPHER_CTX *ctx = NULL; - int fetched = 0; - EVP_CIPHER *cipher = obtain_cipher(ciphername, &fetched); + EVP_CIPHER *cipher = NULL; - if (cipher == NULL) + if (!opt_cipher_silent(ciphername, &cipher)) return NULL; if ((ctx = EVP_CIPHER_CTX_new()) == NULL) @@ -742,8 +702,7 @@ static EVP_CIPHER_CTX *init_evp_cipher_ctx(const char *ciphername, } end: - if (fetched) - EVP_CIPHER_free(cipher); + EVP_CIPHER_free(cipher); return ctx; } @@ -1388,7 +1347,6 @@ int speed_main(int argc, char **argv) unsigned int i, k, loopargs_len = 0, async_jobs = 0; int keylen; int buflen; - int fetched_cipher = 0; BIGNUM *bn = NULL; EVP_PKEY_CTX *genctx = NULL; #ifndef NO_FORK @@ -1533,17 +1491,19 @@ int speed_main(int argc, char **argv) BIO_printf(bio_err, "%s: -evp option cannot be used more than once\n", prog); goto opterr; } - evp_cipher = obtain_cipher(opt_arg(), &fetched_cipher); - if (evp_cipher == NULL) { + ERR_set_mark(); + if (!opt_cipher_silent(opt_arg(), &evp_cipher)) { if (have_md(opt_arg())) evp_md_name = opt_arg(); } if (evp_cipher == NULL && evp_md_name == NULL) { + ERR_clear_last_mark(); BIO_printf(bio_err, "%s: %s is an unknown cipher or digest\n", prog, opt_arg()); goto end; } + ERR_pop_to_mark(); doit[D_EVP] = 1; break; case OPT_HMAC: @@ -2294,17 +2254,15 @@ int speed_main(int argc, char **argv) if (doit[D_EVP_CMAC]) { EVP_MAC *mac = EVP_MAC_fetch(NULL, "CMAC", NULL); OSSL_PARAM params[3]; - EVP_CIPHER *cipher; - int fetched = 0; + EVP_CIPHER *cipher = NULL; if (mac == NULL || evp_mac_ciphername == NULL) goto end; - if ((cipher = obtain_cipher(evp_mac_ciphername, &fetched)) == NULL) + if (!opt_cipher(evp_mac_ciphername, &cipher)) goto end; keylen = EVP_CIPHER_key_length(cipher); - if (fetched) - EVP_CIPHER_free(cipher); + EVP_CIPHER_free(cipher); if (keylen <= 0 || keylen > (int)sizeof(key32)) { BIO_printf(bio_err, "\nRequested CMAC cipher with unsupported key length.\n"); goto end; @@ -3360,9 +3318,7 @@ int speed_main(int argc, char **argv) } OPENSSL_free(loopargs); release_engine(e); - if (fetched_cipher) { - EVP_CIPHER_free(evp_cipher); - } + EVP_CIPHER_free(evp_cipher); return ret; } |