/src/openssl30/crypto/evp/mac_meth.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
| 5 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 6 |  |  * in the file LICENSE in the source distribution or at | 
| 7 |  |  * https://www.openssl.org/source/license.html | 
| 8 |  |  */ | 
| 9 |  |  | 
| 10 |  | #include <openssl/evp.h> | 
| 11 |  | #include <openssl/err.h> | 
| 12 |  | #include <openssl/core.h> | 
| 13 |  | #include <openssl/core_dispatch.h> | 
| 14 |  | #include "internal/provider.h" | 
| 15 |  | #include "internal/core.h" | 
| 16 |  | #include "crypto/evp.h" | 
| 17 |  | #include "evp_local.h" | 
| 18 |  |  | 
| 19 |  | static int evp_mac_up_ref(void *vmac) | 
| 20 | 180k | { | 
| 21 | 180k |     EVP_MAC *mac = vmac; | 
| 22 | 180k |     int ref = 0; | 
| 23 |  |  | 
| 24 | 180k |     CRYPTO_UP_REF(&mac->refcnt, &ref, mac->lock); | 
| 25 | 180k |     return 1; | 
| 26 | 180k | } | 
| 27 |  |  | 
| 28 |  | static void evp_mac_free(void *vmac) | 
| 29 | 180k | { | 
| 30 | 180k |     EVP_MAC *mac = vmac; | 
| 31 | 180k |     int ref = 0; | 
| 32 |  |  | 
| 33 | 180k |     if (mac == NULL) | 
| 34 | 0 |         return; | 
| 35 |  |  | 
| 36 | 180k |     CRYPTO_DOWN_REF(&mac->refcnt, &ref, mac->lock); | 
| 37 | 180k |     if (ref > 0) | 
| 38 | 180k |         return; | 
| 39 | 54 |     OPENSSL_free(mac->type_name); | 
| 40 | 54 |     ossl_provider_free(mac->prov); | 
| 41 | 54 |     CRYPTO_THREAD_lock_free(mac->lock); | 
| 42 | 54 |     OPENSSL_free(mac); | 
| 43 | 54 | } | 
| 44 |  |  | 
| 45 |  | static void *evp_mac_new(void) | 
| 46 | 27 | { | 
| 47 | 27 |     EVP_MAC *mac = NULL; | 
| 48 |  |  | 
| 49 | 27 |     if ((mac = OPENSSL_zalloc(sizeof(*mac))) == NULL | 
| 50 | 27 |         || (mac->lock = CRYPTO_THREAD_lock_new()) == NULL) { | 
| 51 | 0 |         evp_mac_free(mac); | 
| 52 | 0 |         return NULL; | 
| 53 | 0 |     } | 
| 54 |  |  | 
| 55 | 27 |     mac->refcnt = 1; | 
| 56 |  |  | 
| 57 | 27 |     return mac; | 
| 58 | 27 | } | 
| 59 |  |  | 
| 60 |  | static void *evp_mac_from_algorithm(int name_id, | 
| 61 |  |                                     const OSSL_ALGORITHM *algodef, | 
| 62 |  |                                     OSSL_PROVIDER *prov) | 
| 63 | 54 | { | 
| 64 | 54 |     const OSSL_DISPATCH *fns = algodef->implementation; | 
| 65 | 54 |     EVP_MAC *mac = NULL; | 
| 66 | 54 |     int fnmaccnt = 0, fnctxcnt = 0; | 
| 67 |  |  | 
| 68 | 54 |     if ((mac = evp_mac_new()) == NULL) { | 
| 69 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | 
| 70 | 0 |         return NULL; | 
| 71 | 0 |     } | 
| 72 | 54 |     mac->name_id = name_id; | 
| 73 | 54 |     if ((mac->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) { | 
| 74 | 0 |         evp_mac_free(mac); | 
| 75 | 0 |         return NULL; | 
| 76 | 0 |     } | 
| 77 | 54 |     mac->description = algodef->algorithm_description; | 
| 78 |  |  | 
| 79 | 594 |     for (; fns->function_id != 0; fns++) { | 
| 80 | 540 |         switch (fns->function_id) { | 
| 81 | 54 |         case OSSL_FUNC_MAC_NEWCTX: | 
| 82 | 54 |             if (mac->newctx != NULL) | 
| 83 | 0 |                 break; | 
| 84 | 54 |             mac->newctx = OSSL_FUNC_mac_newctx(fns); | 
| 85 | 54 |             fnctxcnt++; | 
| 86 | 54 |             break; | 
| 87 | 54 |         case OSSL_FUNC_MAC_DUPCTX: | 
| 88 | 54 |             if (mac->dupctx != NULL) | 
| 89 | 0 |                 break; | 
| 90 | 54 |             mac->dupctx = OSSL_FUNC_mac_dupctx(fns); | 
| 91 | 54 |             break; | 
| 92 | 54 |         case OSSL_FUNC_MAC_FREECTX: | 
| 93 | 54 |             if (mac->freectx != NULL) | 
| 94 | 0 |                 break; | 
| 95 | 54 |             mac->freectx = OSSL_FUNC_mac_freectx(fns); | 
| 96 | 54 |             fnctxcnt++; | 
| 97 | 54 |             break; | 
| 98 | 54 |         case OSSL_FUNC_MAC_INIT: | 
| 99 | 54 |             if (mac->init != NULL) | 
| 100 | 0 |                 break; | 
| 101 | 54 |             mac->init = OSSL_FUNC_mac_init(fns); | 
| 102 | 54 |             fnmaccnt++; | 
| 103 | 54 |             break; | 
| 104 | 54 |         case OSSL_FUNC_MAC_UPDATE: | 
| 105 | 54 |             if (mac->update != NULL) | 
| 106 | 0 |                 break; | 
| 107 | 54 |             mac->update = OSSL_FUNC_mac_update(fns); | 
| 108 | 54 |             fnmaccnt++; | 
| 109 | 54 |             break; | 
| 110 | 54 |         case OSSL_FUNC_MAC_FINAL: | 
| 111 | 54 |             if (mac->final != NULL) | 
| 112 | 0 |                 break; | 
| 113 | 54 |             mac->final = OSSL_FUNC_mac_final(fns); | 
| 114 | 54 |             fnmaccnt++; | 
| 115 | 54 |             break; | 
| 116 | 12 |         case OSSL_FUNC_MAC_GETTABLE_PARAMS: | 
| 117 | 12 |             if (mac->gettable_params != NULL) | 
| 118 | 0 |                 break; | 
| 119 | 12 |             mac->gettable_params = | 
| 120 | 12 |                 OSSL_FUNC_mac_gettable_params(fns); | 
| 121 | 12 |             break; | 
| 122 | 42 |         case OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS: | 
| 123 | 42 |             if (mac->gettable_ctx_params != NULL) | 
| 124 | 0 |                 break; | 
| 125 | 42 |             mac->gettable_ctx_params = | 
| 126 | 42 |                 OSSL_FUNC_mac_gettable_ctx_params(fns); | 
| 127 | 42 |             break; | 
| 128 | 54 |         case OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS: | 
| 129 | 54 |             if (mac->settable_ctx_params != NULL) | 
| 130 | 0 |                 break; | 
| 131 | 54 |             mac->settable_ctx_params = | 
| 132 | 54 |                 OSSL_FUNC_mac_settable_ctx_params(fns); | 
| 133 | 54 |             break; | 
| 134 | 12 |         case OSSL_FUNC_MAC_GET_PARAMS: | 
| 135 | 12 |             if (mac->get_params != NULL) | 
| 136 | 0 |                 break; | 
| 137 | 12 |             mac->get_params = OSSL_FUNC_mac_get_params(fns); | 
| 138 | 12 |             break; | 
| 139 | 42 |         case OSSL_FUNC_MAC_GET_CTX_PARAMS: | 
| 140 | 42 |             if (mac->get_ctx_params != NULL) | 
| 141 | 0 |                 break; | 
| 142 | 42 |             mac->get_ctx_params = OSSL_FUNC_mac_get_ctx_params(fns); | 
| 143 | 42 |             break; | 
| 144 | 54 |         case OSSL_FUNC_MAC_SET_CTX_PARAMS: | 
| 145 | 54 |             if (mac->set_ctx_params != NULL) | 
| 146 | 0 |                 break; | 
| 147 | 54 |             mac->set_ctx_params = OSSL_FUNC_mac_set_ctx_params(fns); | 
| 148 | 54 |             break; | 
| 149 | 540 |         } | 
| 150 | 540 |     } | 
| 151 | 54 |     if (fnmaccnt != 3 | 
| 152 | 54 |         || fnctxcnt != 2) { | 
| 153 |  |         /* | 
| 154 |  |          * In order to be a consistent set of functions we must have at least | 
| 155 |  |          * a complete set of "mac" functions, and a complete set of context | 
| 156 |  |          * management functions, as well as the size function. | 
| 157 |  |          */ | 
| 158 | 0 |         evp_mac_free(mac); | 
| 159 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); | 
| 160 | 0 |         return NULL; | 
| 161 | 0 |     } | 
| 162 | 54 |     mac->prov = prov; | 
| 163 | 54 |     if (prov != NULL) | 
| 164 | 54 |         ossl_provider_up_ref(prov); | 
| 165 |  |  | 
| 166 | 54 |     return mac; | 
| 167 | 54 | } | 
| 168 |  |  | 
| 169 |  | EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm, | 
| 170 |  |                        const char *properties) | 
| 171 | 22.5k | { | 
| 172 | 22.5k |     return evp_generic_fetch(libctx, OSSL_OP_MAC, algorithm, properties, | 
| 173 | 22.5k |                              evp_mac_from_algorithm, evp_mac_up_ref, | 
| 174 | 22.5k |                              evp_mac_free); | 
| 175 | 22.5k | } | 
| 176 |  |  | 
| 177 |  | int EVP_MAC_up_ref(EVP_MAC *mac) | 
| 178 | 158k | { | 
| 179 | 158k |     return evp_mac_up_ref(mac); | 
| 180 | 158k | } | 
| 181 |  |  | 
| 182 |  | void EVP_MAC_free(EVP_MAC *mac) | 
| 183 | 180k | { | 
| 184 | 180k |     evp_mac_free(mac); | 
| 185 | 180k | } | 
| 186 |  |  | 
| 187 |  | const OSSL_PROVIDER *EVP_MAC_get0_provider(const EVP_MAC *mac) | 
| 188 | 4.38k | { | 
| 189 | 4.38k |     return mac->prov; | 
| 190 | 4.38k | } | 
| 191 |  |  | 
| 192 |  | const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac) | 
| 193 | 0 | { | 
| 194 | 0 |     if (mac->gettable_params == NULL) | 
| 195 | 0 |         return NULL; | 
| 196 | 0 |     return mac->gettable_params(ossl_provider_ctx(EVP_MAC_get0_provider(mac))); | 
| 197 | 0 | } | 
| 198 |  |  | 
| 199 |  | const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac) | 
| 200 | 0 | { | 
| 201 | 0 |     void *alg; | 
| 202 |  | 
 | 
| 203 | 0 |     if (mac->gettable_ctx_params == NULL) | 
| 204 | 0 |         return NULL; | 
| 205 | 0 |     alg = ossl_provider_ctx(EVP_MAC_get0_provider(mac)); | 
| 206 | 0 |     return mac->gettable_ctx_params(NULL, alg); | 
| 207 | 0 | } | 
| 208 |  |  | 
| 209 |  | const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac) | 
| 210 | 4.38k | { | 
| 211 | 4.38k |     void *alg; | 
| 212 |  |  | 
| 213 | 4.38k |     if (mac->settable_ctx_params == NULL) | 
| 214 | 0 |         return NULL; | 
| 215 | 4.38k |     alg = ossl_provider_ctx(EVP_MAC_get0_provider(mac)); | 
| 216 | 4.38k |     return mac->settable_ctx_params(NULL, alg); | 
| 217 | 4.38k | } | 
| 218 |  |  | 
| 219 |  | const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx) | 
| 220 | 0 | { | 
| 221 | 0 |     void *alg; | 
| 222 |  | 
 | 
| 223 | 0 |     if (ctx->meth->gettable_ctx_params == NULL) | 
| 224 | 0 |         return NULL; | 
| 225 | 0 |     alg = ossl_provider_ctx(EVP_MAC_get0_provider(ctx->meth)); | 
| 226 | 0 |     return ctx->meth->gettable_ctx_params(ctx->algctx, alg); | 
| 227 | 0 | } | 
| 228 |  |  | 
| 229 |  | const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx) | 
| 230 | 0 | { | 
| 231 | 0 |     void *alg; | 
| 232 |  | 
 | 
| 233 | 0 |     if (ctx->meth->settable_ctx_params == NULL) | 
| 234 | 0 |         return NULL; | 
| 235 | 0 |     alg = ossl_provider_ctx(EVP_MAC_get0_provider(ctx->meth)); | 
| 236 | 0 |     return ctx->meth->settable_ctx_params(ctx->algctx, alg); | 
| 237 | 0 | } | 
| 238 |  |  | 
| 239 |  | void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx, | 
| 240 |  |                              void (*fn)(EVP_MAC *mac, void *arg), | 
| 241 |  |                              void *arg) | 
| 242 | 0 | { | 
| 243 | 0 |     evp_generic_do_all(libctx, OSSL_OP_MAC, | 
| 244 | 0 |                        (void (*)(void *, void *))fn, arg, | 
| 245 | 0 |                        evp_mac_from_algorithm, evp_mac_up_ref, evp_mac_free); | 
| 246 | 0 | } |