1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
/*
* Copyright 2022-2025 The OpenSSL Project Authors. All Rights Reserved.
*
* 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
*/
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/core.h>
#include <openssl/core_dispatch.h>
#include "internal/provider.h"
#include "internal/core.h"
#include "crypto/evp.h"
#include "evp_local.h"
static int evp_mac_up_ref(void *vmac)
{
EVP_MAC *mac = vmac;
int ref = 0;
CRYPTO_UP_REF(&mac->refcnt, &ref);
return 1;
}
static void evp_mac_free(void *vmac)
{
EVP_MAC *mac = vmac;
int ref = 0;
if (mac == NULL)
return;
CRYPTO_DOWN_REF(&mac->refcnt, &ref);
if (ref > 0)
return;
OPENSSL_free(mac->type_name);
ossl_provider_free(mac->prov);
CRYPTO_FREE_REF(&mac->refcnt);
OPENSSL_free(mac);
}
static void *evp_mac_new(void)
{
EVP_MAC *mac = NULL;
if ((mac = OPENSSL_zalloc(sizeof(*mac))) == NULL
|| !CRYPTO_NEW_REF(&mac->refcnt, 1)) {
evp_mac_free(mac);
return NULL;
}
return mac;
}
static void *evp_mac_from_algorithm(int name_id,
const OSSL_ALGORITHM *algodef,
OSSL_PROVIDER *prov)
{
const OSSL_DISPATCH *fns = algodef->implementation;
EVP_MAC *mac = NULL;
int fnmaccnt = 0, fnctxcnt = 0, mac_init_found = 0;
if ((mac = evp_mac_new()) == NULL) {
ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
goto err;
}
mac->name_id = name_id;
if ((mac->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
goto err;
mac->description = algodef->algorithm_description;
for (; fns->function_id != 0; fns++) {
switch (fns->function_id) {
case OSSL_FUNC_MAC_NEWCTX:
if (mac->newctx != NULL)
break;
mac->newctx = OSSL_FUNC_mac_newctx(fns);
fnctxcnt++;
break;
case OSSL_FUNC_MAC_DUPCTX:
if (mac->dupctx != NULL)
break;
mac->dupctx = OSSL_FUNC_mac_dupctx(fns);
break;
case OSSL_FUNC_MAC_FREECTX:
if (mac->freectx != NULL)
break;
mac->freectx = OSSL_FUNC_mac_freectx(fns);
fnctxcnt++;
break;
case OSSL_FUNC_MAC_INIT:
if (mac->init != NULL)
break;
mac->init = OSSL_FUNC_mac_init(fns);
mac_init_found = 1;
break;
case OSSL_FUNC_MAC_UPDATE:
if (mac->update != NULL)
break;
mac->update = OSSL_FUNC_mac_update(fns);
fnmaccnt++;
break;
case OSSL_FUNC_MAC_FINAL:
if (mac->final != NULL)
break;
mac->final = OSSL_FUNC_mac_final(fns);
fnmaccnt++;
break;
case OSSL_FUNC_MAC_GETTABLE_PARAMS:
if (mac->gettable_params != NULL)
break;
mac->gettable_params =
OSSL_FUNC_mac_gettable_params(fns);
break;
case OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS:
if (mac->gettable_ctx_params != NULL)
break;
mac->gettable_ctx_params =
OSSL_FUNC_mac_gettable_ctx_params(fns);
break;
case OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS:
if (mac->settable_ctx_params != NULL)
break;
mac->settable_ctx_params =
OSSL_FUNC_mac_settable_ctx_params(fns);
break;
case OSSL_FUNC_MAC_GET_PARAMS:
if (mac->get_params != NULL)
break;
mac->get_params = OSSL_FUNC_mac_get_params(fns);
break;
case OSSL_FUNC_MAC_GET_CTX_PARAMS:
if (mac->get_ctx_params != NULL)
break;
mac->get_ctx_params = OSSL_FUNC_mac_get_ctx_params(fns);
break;
case OSSL_FUNC_MAC_SET_CTX_PARAMS:
if (mac->set_ctx_params != NULL)
break;
mac->set_ctx_params = OSSL_FUNC_mac_set_ctx_params(fns);
break;
case OSSL_FUNC_MAC_INIT_SKEY:
if (mac->init_skey != NULL)
break;
mac->init_skey = OSSL_FUNC_mac_init_skey(fns);
mac_init_found = 1;
break;
}
}
fnmaccnt += mac_init_found;
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.
*/
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
goto err;
}
if (prov != NULL && !ossl_provider_up_ref(prov))
goto err;
mac->prov = prov;
return mac;
err:
evp_mac_free(mac);
return NULL;
}
EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
const char *properties)
{
return evp_generic_fetch(libctx, OSSL_OP_MAC, algorithm, properties,
evp_mac_from_algorithm, evp_mac_up_ref,
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);
}
const OSSL_PROVIDER *EVP_MAC_get0_provider(const EVP_MAC *mac)
{
return mac->prov;
}
const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac)
{
if (mac->gettable_params == NULL)
return NULL;
return mac->gettable_params(ossl_provider_ctx(EVP_MAC_get0_provider(mac)));
}
const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac)
{
void *alg;
if (mac->gettable_ctx_params == NULL)
return NULL;
alg = ossl_provider_ctx(EVP_MAC_get0_provider(mac));
return mac->gettable_ctx_params(NULL, alg);
}
const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac)
{
void *alg;
if (mac->settable_ctx_params == NULL)
return NULL;
alg = ossl_provider_ctx(EVP_MAC_get0_provider(mac));
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;
alg = ossl_provider_ctx(EVP_MAC_get0_provider(ctx->meth));
return ctx->meth->gettable_ctx_params(ctx->algctx, alg);
}
const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx)
{
void *alg;
if (ctx->meth->settable_ctx_params == NULL)
return NULL;
alg = ossl_provider_ctx(EVP_MAC_get0_provider(ctx->meth));
return ctx->meth->settable_ctx_params(ctx->algctx, alg);
}
void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx,
void (*fn)(EVP_MAC *mac, void *arg),
void *arg)
{
evp_generic_do_all(libctx, OSSL_OP_MAC,
(void (*)(void *, void *))fn, arg,
evp_mac_from_algorithm, evp_mac_up_ref, evp_mac_free);
}
EVP_MAC *evp_mac_fetch_from_prov(OSSL_PROVIDER *prov,
const char *algorithm,
const char *properties)
{
return evp_generic_fetch_from_prov(prov, OSSL_OP_MAC,
algorithm, properties,
evp_mac_from_algorithm,
evp_mac_up_ref,
evp_mac_free);
}
|