2022-01-28 03:18:28 +08:00
|
|
|
/*
|
2023-09-07 16:59:15 +08:00
|
|
|
* Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
|
2022-01-28 03:18:28 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
2019-05-07 18:39:58 +08:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/core.h>
|
2020-06-21 07:21:19 +08:00
|
|
|
#include <openssl/core_dispatch.h>
|
2019-05-07 18:39:58 +08:00
|
|
|
#include "internal/provider.h"
|
2021-04-16 22:22:03 +08:00
|
|
|
#include "internal/core.h"
|
|
|
|
#include "crypto/evp.h"
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "evp_local.h"
|
2019-05-07 18:39:58 +08:00
|
|
|
|
|
|
|
static int evp_mac_up_ref(void *vmac)
|
|
|
|
{
|
|
|
|
EVP_MAC *mac = vmac;
|
|
|
|
int ref = 0;
|
|
|
|
|
2023-06-22 07:48:49 +08:00
|
|
|
CRYPTO_UP_REF(&mac->refcnt, &ref);
|
2019-05-07 18:39:58 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void evp_mac_free(void *vmac)
|
|
|
|
{
|
|
|
|
EVP_MAC *mac = vmac;
|
|
|
|
int ref = 0;
|
|
|
|
|
|
|
|
if (mac == NULL)
|
|
|
|
return;
|
|
|
|
|
2023-06-22 07:48:49 +08:00
|
|
|
CRYPTO_DOWN_REF(&mac->refcnt, &ref);
|
2019-05-07 18:39:58 +08:00
|
|
|
if (ref > 0)
|
|
|
|
return;
|
2021-04-16 22:22:03 +08:00
|
|
|
OPENSSL_free(mac->type_name);
|
2019-05-07 18:39:58 +08:00
|
|
|
ossl_provider_free(mac->prov);
|
2023-06-22 07:48:49 +08:00
|
|
|
CRYPTO_FREE_REF(&mac->refcnt);
|
2019-05-07 18:39:58 +08:00
|
|
|
OPENSSL_free(mac);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *evp_mac_new(void)
|
|
|
|
{
|
|
|
|
EVP_MAC *mac = NULL;
|
|
|
|
|
|
|
|
if ((mac = OPENSSL_zalloc(sizeof(*mac))) == NULL
|
2023-06-22 07:48:49 +08:00
|
|
|
|| !CRYPTO_NEW_REF(&mac->refcnt, 1)) {
|
2019-05-07 18:39:58 +08:00
|
|
|
evp_mac_free(mac);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return mac;
|
|
|
|
}
|
|
|
|
|
2021-03-16 21:14:43 +08:00
|
|
|
static void *evp_mac_from_algorithm(int name_id,
|
|
|
|
const OSSL_ALGORITHM *algodef,
|
|
|
|
OSSL_PROVIDER *prov)
|
2019-05-07 18:39:58 +08:00
|
|
|
{
|
2021-03-16 21:14:43 +08:00
|
|
|
const OSSL_DISPATCH *fns = algodef->implementation;
|
2019-05-07 18:39:58 +08:00
|
|
|
EVP_MAC *mac = NULL;
|
|
|
|
int fnmaccnt = 0, fnctxcnt = 0;
|
|
|
|
|
In provider implemented methods, save the name number, not the name string
Multiple names per implementation is already supported in the namemap,
but hasn't been used yet. However, as soon as we have multiple names,
we will get an issue with what name should be saved in the method.
The solution is to not save the name itself, but rather the number
it's associated with. This number is supposed to be unique for each
set of names, and we assume that algorithm names are globally unique,
i.e. there can be no name overlap between different algorithm types.
Incidently, it was also found that the 'get' function used by
ossl_construct_method() doesn't need all the parameters it was given;
most of what it needs, it can now get through the data structure given
by the caller of ossl_construct_method(). As a consequence,
ossl_construct_method() itself doesn't need all the parameters it was
given either.
There are some added internal functions that are expected to disappear
as soon as legacy code is removed, such as evp_first_name() and
ossl_namemap_num2name().
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9897)
2019-09-14 22:22:19 +08:00
|
|
|
if ((mac = evp_mac_new()) == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
|
2019-05-07 18:39:58 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
In provider implemented methods, save the name number, not the name string
Multiple names per implementation is already supported in the namemap,
but hasn't been used yet. However, as soon as we have multiple names,
we will get an issue with what name should be saved in the method.
The solution is to not save the name itself, but rather the number
it's associated with. This number is supposed to be unique for each
set of names, and we assume that algorithm names are globally unique,
i.e. there can be no name overlap between different algorithm types.
Incidently, it was also found that the 'get' function used by
ossl_construct_method() doesn't need all the parameters it was given;
most of what it needs, it can now get through the data structure given
by the caller of ossl_construct_method(). As a consequence,
ossl_construct_method() itself doesn't need all the parameters it was
given either.
There are some added internal functions that are expected to disappear
as soon as legacy code is removed, such as evp_first_name() and
ossl_namemap_num2name().
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9897)
2019-09-14 22:22:19 +08:00
|
|
|
mac->name_id = name_id;
|
2021-04-16 22:22:03 +08:00
|
|
|
if ((mac->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
|
|
|
|
evp_mac_free(mac);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-03-16 21:14:43 +08:00
|
|
|
mac->description = algodef->algorithm_description;
|
2019-05-07 18:39:58 +08:00
|
|
|
|
|
|
|
for (; fns->function_id != 0; fns++) {
|
|
|
|
switch (fns->function_id) {
|
|
|
|
case OSSL_FUNC_MAC_NEWCTX:
|
|
|
|
if (mac->newctx != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
mac->newctx = OSSL_FUNC_mac_newctx(fns);
|
2019-05-07 18:39:58 +08:00
|
|
|
fnctxcnt++;
|
|
|
|
break;
|
|
|
|
case OSSL_FUNC_MAC_DUPCTX:
|
|
|
|
if (mac->dupctx != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
mac->dupctx = OSSL_FUNC_mac_dupctx(fns);
|
2019-05-07 18:39:58 +08:00
|
|
|
break;
|
|
|
|
case OSSL_FUNC_MAC_FREECTX:
|
|
|
|
if (mac->freectx != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
mac->freectx = OSSL_FUNC_mac_freectx(fns);
|
2019-05-07 18:39:58 +08:00
|
|
|
fnctxcnt++;
|
|
|
|
break;
|
|
|
|
case OSSL_FUNC_MAC_INIT:
|
|
|
|
if (mac->init != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
mac->init = OSSL_FUNC_mac_init(fns);
|
2019-05-07 18:39:58 +08:00
|
|
|
fnmaccnt++;
|
|
|
|
break;
|
|
|
|
case OSSL_FUNC_MAC_UPDATE:
|
|
|
|
if (mac->update != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
mac->update = OSSL_FUNC_mac_update(fns);
|
2019-05-07 18:39:58 +08:00
|
|
|
fnmaccnt++;
|
|
|
|
break;
|
|
|
|
case OSSL_FUNC_MAC_FINAL:
|
|
|
|
if (mac->final != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
mac->final = OSSL_FUNC_mac_final(fns);
|
2019-05-07 18:39:58 +08:00
|
|
|
fnmaccnt++;
|
|
|
|
break;
|
|
|
|
case OSSL_FUNC_MAC_GETTABLE_PARAMS:
|
|
|
|
if (mac->gettable_params != NULL)
|
|
|
|
break;
|
|
|
|
mac->gettable_params =
|
2020-06-21 07:19:16 +08:00
|
|
|
OSSL_FUNC_mac_gettable_params(fns);
|
2019-05-07 18:39:58 +08:00
|
|
|
break;
|
|
|
|
case OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS:
|
|
|
|
if (mac->gettable_ctx_params != NULL)
|
|
|
|
break;
|
|
|
|
mac->gettable_ctx_params =
|
2020-06-21 07:19:16 +08:00
|
|
|
OSSL_FUNC_mac_gettable_ctx_params(fns);
|
2019-05-07 18:39:58 +08:00
|
|
|
break;
|
|
|
|
case OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS:
|
|
|
|
if (mac->settable_ctx_params != NULL)
|
|
|
|
break;
|
|
|
|
mac->settable_ctx_params =
|
2020-06-21 07:19:16 +08:00
|
|
|
OSSL_FUNC_mac_settable_ctx_params(fns);
|
2019-05-07 18:39:58 +08:00
|
|
|
break;
|
|
|
|
case OSSL_FUNC_MAC_GET_PARAMS:
|
|
|
|
if (mac->get_params != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
mac->get_params = OSSL_FUNC_mac_get_params(fns);
|
2019-05-07 18:39:58 +08:00
|
|
|
break;
|
2019-08-16 15:04:29 +08:00
|
|
|
case OSSL_FUNC_MAC_GET_CTX_PARAMS:
|
|
|
|
if (mac->get_ctx_params != NULL)
|
2019-05-07 18:39:58 +08:00
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
mac->get_ctx_params = OSSL_FUNC_mac_get_ctx_params(fns);
|
2019-05-07 18:39:58 +08:00
|
|
|
break;
|
2019-08-16 15:04:29 +08:00
|
|
|
case OSSL_FUNC_MAC_SET_CTX_PARAMS:
|
|
|
|
if (mac->set_ctx_params != NULL)
|
2019-05-07 18:39:58 +08:00
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
mac->set_ctx_params = OSSL_FUNC_mac_set_ctx_params(fns);
|
2019-05-07 18:39:58 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fnmaccnt != 3
|
|
|
|
|| fnctxcnt != 2) {
|
|
|
|
/*
|
|
|
|
* In order to be a consistent set of functions we must have at least
|
|
|
|
* a complete set of "mac" functions, and a complete set of context
|
|
|
|
* management functions, as well as the size function.
|
|
|
|
*/
|
|
|
|
evp_mac_free(mac);
|
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
mac->prov = prov;
|
|
|
|
if (prov != NULL)
|
|
|
|
ossl_provider_up_ref(prov);
|
|
|
|
|
|
|
|
return mac;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
|
2019-05-07 18:39:58 +08:00
|
|
|
const char *properties)
|
|
|
|
{
|
|
|
|
return evp_generic_fetch(libctx, OSSL_OP_MAC, algorithm, properties,
|
2021-03-16 21:14:43 +08:00
|
|
|
evp_mac_from_algorithm, evp_mac_up_ref,
|
2019-05-07 18:39:58 +08:00
|
|
|
evp_mac_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
int EVP_MAC_up_ref(EVP_MAC *mac)
|
|
|
|
{
|
|
|
|
return evp_mac_up_ref(mac);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EVP_MAC_free(EVP_MAC *mac)
|
|
|
|
{
|
|
|
|
evp_mac_free(mac);
|
|
|
|
}
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
const OSSL_PROVIDER *EVP_MAC_get0_provider(const EVP_MAC *mac)
|
2019-06-05 00:14:38 +08:00
|
|
|
{
|
|
|
|
return mac->prov;
|
|
|
|
}
|
|
|
|
|
2019-05-07 18:39:58 +08:00
|
|
|
const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac)
|
|
|
|
{
|
|
|
|
if (mac->gettable_params == NULL)
|
|
|
|
return NULL;
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
return mac->gettable_params(ossl_provider_ctx(EVP_MAC_get0_provider(mac)));
|
2019-05-07 18:39:58 +08:00
|
|
|
}
|
|
|
|
|
2019-09-27 14:35:45 +08:00
|
|
|
const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac)
|
2019-05-07 18:39:58 +08:00
|
|
|
{
|
2021-02-23 09:03:49 +08:00
|
|
|
void *alg;
|
|
|
|
|
2019-05-07 18:39:58 +08:00
|
|
|
if (mac->gettable_ctx_params == NULL)
|
|
|
|
return NULL;
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
alg = ossl_provider_ctx(EVP_MAC_get0_provider(mac));
|
2021-02-23 09:03:49 +08:00
|
|
|
return mac->gettable_ctx_params(NULL, alg);
|
2019-05-07 18:39:58 +08:00
|
|
|
}
|
|
|
|
|
2019-09-27 14:35:45 +08:00
|
|
|
const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac)
|
2019-05-07 18:39:58 +08:00
|
|
|
{
|
2021-02-23 09:03:49 +08:00
|
|
|
void *alg;
|
|
|
|
|
2019-05-07 18:39:58 +08:00
|
|
|
if (mac->settable_ctx_params == NULL)
|
|
|
|
return NULL;
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
alg = ossl_provider_ctx(EVP_MAC_get0_provider(mac));
|
2021-02-23 09:03:49 +08:00
|
|
|
return mac->settable_ctx_params(NULL, alg);
|
|
|
|
}
|
|
|
|
|
|
|
|
const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx)
|
|
|
|
{
|
|
|
|
void *alg;
|
|
|
|
|
|
|
|
if (ctx->meth->gettable_ctx_params == NULL)
|
|
|
|
return NULL;
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
alg = ossl_provider_ctx(EVP_MAC_get0_provider(ctx->meth));
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->gettable_ctx_params(ctx->algctx, alg);
|
2021-02-23 09:03:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx)
|
|
|
|
{
|
|
|
|
void *alg;
|
|
|
|
|
|
|
|
if (ctx->meth->settable_ctx_params == NULL)
|
|
|
|
return NULL;
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
alg = ossl_provider_ctx(EVP_MAC_get0_provider(ctx->meth));
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->settable_ctx_params(ctx->algctx, alg);
|
2019-05-07 18:39:58 +08:00
|
|
|
}
|
2019-06-05 00:17:49 +08:00
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx,
|
EVP: add missing common functionality
This adds the missing functions that should be common for all
fetchable EVP sub-APIs:
EVP_KEYMGMT_is_a(), EVP_KEYMGMT_do_all_provided(), EVP_KEYEXCH_is_a(),
EVP_KEYEXCH_do_all_provided(), EVP_KDF_is_a(), EVP_MD_is_a(),
EVP_SIGNATURE_do_all_provided(), EVP_SIGNATURE_is_a().
This also renames EVP_MD_do_all_ex(), EVP_CIPHER_do_all_ex(),
EVP_KDF_do_all_ex(), EVP_MAC_do_all_ex() to change '_ex'
to '_provided'.
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/9979)
2019-09-23 16:33:26 +08:00
|
|
|
void (*fn)(EVP_MAC *mac, void *arg),
|
|
|
|
void *arg)
|
2019-06-05 00:17:49 +08:00
|
|
|
{
|
|
|
|
evp_generic_do_all(libctx, OSSL_OP_MAC,
|
|
|
|
(void (*)(void *, void *))fn, arg,
|
2021-06-09 13:52:09 +08:00
|
|
|
evp_mac_from_algorithm, evp_mac_up_ref, evp_mac_free);
|
2019-06-05 00:17:49 +08:00
|
|
|
}
|