Path: blob/main/crypto/openssl/providers/implementations/signature/mac_legacy_sig.c
48383 views
/*1* Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/* We need to use some engine deprecated APIs */10#define OPENSSL_SUPPRESS_DEPRECATED1112#include <openssl/crypto.h>13#include <openssl/evp.h>14#include <openssl/core_dispatch.h>15#include <openssl/core_names.h>16#include <openssl/params.h>17#include <openssl/err.h>18#include <openssl/proverr.h>19#ifndef FIPS_MODULE20# include <openssl/engine.h>21#endif22#include "prov/implementations.h"23#include "prov/provider_ctx.h"24#include "prov/macsignature.h"25#include "prov/providercommon.h"2627static OSSL_FUNC_signature_newctx_fn mac_hmac_newctx;28static OSSL_FUNC_signature_newctx_fn mac_siphash_newctx;29static OSSL_FUNC_signature_newctx_fn mac_poly1305_newctx;30static OSSL_FUNC_signature_newctx_fn mac_cmac_newctx;31static OSSL_FUNC_signature_digest_sign_init_fn mac_digest_sign_init;32static OSSL_FUNC_signature_digest_sign_update_fn mac_digest_sign_update;33static OSSL_FUNC_signature_digest_sign_final_fn mac_digest_sign_final;34static OSSL_FUNC_signature_freectx_fn mac_freectx;35static OSSL_FUNC_signature_dupctx_fn mac_dupctx;36static OSSL_FUNC_signature_set_ctx_params_fn mac_set_ctx_params;37static OSSL_FUNC_signature_settable_ctx_params_fn mac_hmac_settable_ctx_params;38static OSSL_FUNC_signature_settable_ctx_params_fn mac_siphash_settable_ctx_params;39static OSSL_FUNC_signature_settable_ctx_params_fn mac_poly1305_settable_ctx_params;40static OSSL_FUNC_signature_settable_ctx_params_fn mac_cmac_settable_ctx_params;4142typedef struct {43OSSL_LIB_CTX *libctx;44char *propq;45MAC_KEY *key;46EVP_MAC_CTX *macctx;47} PROV_MAC_CTX;4849static void *mac_newctx(void *provctx, const char *propq, const char *macname)50{51PROV_MAC_CTX *pmacctx;52EVP_MAC *mac = NULL;5354if (!ossl_prov_is_running())55return NULL;5657pmacctx = OPENSSL_zalloc(sizeof(PROV_MAC_CTX));58if (pmacctx == NULL)59return NULL;6061pmacctx->libctx = PROV_LIBCTX_OF(provctx);62if (propq != NULL && (pmacctx->propq = OPENSSL_strdup(propq)) == NULL)63goto err;6465mac = EVP_MAC_fetch(pmacctx->libctx, macname, propq);66if (mac == NULL)67goto err;6869pmacctx->macctx = EVP_MAC_CTX_new(mac);70if (pmacctx->macctx == NULL)71goto err;7273EVP_MAC_free(mac);7475return pmacctx;7677err:78OPENSSL_free(pmacctx->propq);79OPENSSL_free(pmacctx);80EVP_MAC_free(mac);81return NULL;82}8384#define MAC_NEWCTX(funcname, macname) \85static void *mac_##funcname##_newctx(void *provctx, const char *propq) \86{ \87return mac_newctx(provctx, propq, macname); \88}8990MAC_NEWCTX(hmac, "HMAC")91MAC_NEWCTX(siphash, "SIPHASH")92MAC_NEWCTX(poly1305, "POLY1305")93MAC_NEWCTX(cmac, "CMAC")9495static int mac_digest_sign_init(void *vpmacctx, const char *mdname, void *vkey,96const OSSL_PARAM params[])97{98PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;99const char *ciphername = NULL, *engine = NULL;100101if (!ossl_prov_is_running()102|| pmacctx == NULL)103return 0;104105if (pmacctx->key == NULL && vkey == NULL) {106ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);107return 0;108}109110if (vkey != NULL) {111if (!ossl_mac_key_up_ref(vkey))112return 0;113ossl_mac_key_free(pmacctx->key);114pmacctx->key = vkey;115}116117if (pmacctx->key->cipher.cipher != NULL)118ciphername = (char *)EVP_CIPHER_get0_name(pmacctx->key->cipher.cipher);119#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)120if (pmacctx->key->cipher.engine != NULL)121engine = (char *)ENGINE_get_id(pmacctx->key->cipher.engine);122#endif123124if (!ossl_prov_set_macctx(pmacctx->macctx, NULL,125(char *)ciphername,126(char *)mdname,127(char *)engine,128pmacctx->key->properties,129NULL, 0))130return 0;131132if (!EVP_MAC_init(pmacctx->macctx, pmacctx->key->priv_key,133pmacctx->key->priv_key_len, params))134return 0;135136return 1;137}138139int mac_digest_sign_update(void *vpmacctx, const unsigned char *data,140size_t datalen)141{142PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;143144if (pmacctx == NULL || pmacctx->macctx == NULL)145return 0;146147return EVP_MAC_update(pmacctx->macctx, data, datalen);148}149150int mac_digest_sign_final(void *vpmacctx, unsigned char *mac, size_t *maclen,151size_t macsize)152{153PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;154155if (!ossl_prov_is_running() || pmacctx == NULL || pmacctx->macctx == NULL)156return 0;157158return EVP_MAC_final(pmacctx->macctx, mac, maclen, macsize);159}160161static void mac_freectx(void *vpmacctx)162{163PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;164165OPENSSL_free(ctx->propq);166EVP_MAC_CTX_free(ctx->macctx);167ossl_mac_key_free(ctx->key);168OPENSSL_free(ctx);169}170171static void *mac_dupctx(void *vpmacctx)172{173PROV_MAC_CTX *srcctx = (PROV_MAC_CTX *)vpmacctx;174PROV_MAC_CTX *dstctx;175176if (!ossl_prov_is_running())177return NULL;178179dstctx = OPENSSL_zalloc(sizeof(*srcctx));180if (dstctx == NULL)181return NULL;182183*dstctx = *srcctx;184dstctx->propq = NULL;185dstctx->key = NULL;186dstctx->macctx = NULL;187188if (srcctx->propq != NULL && (dstctx->propq = OPENSSL_strdup(srcctx->propq)) == NULL)189goto err;190191if (srcctx->key != NULL && !ossl_mac_key_up_ref(srcctx->key))192goto err;193dstctx->key = srcctx->key;194195if (srcctx->macctx != NULL) {196dstctx->macctx = EVP_MAC_CTX_dup(srcctx->macctx);197if (dstctx->macctx == NULL)198goto err;199}200201return dstctx;202err:203mac_freectx(dstctx);204return NULL;205}206207static int mac_set_ctx_params(void *vpmacctx, const OSSL_PARAM params[])208{209PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;210211return EVP_MAC_CTX_set_params(ctx->macctx, params);212}213214static const OSSL_PARAM *mac_settable_ctx_params(ossl_unused void *ctx,215void *provctx,216const char *macname)217{218EVP_MAC *mac = EVP_MAC_fetch(PROV_LIBCTX_OF(provctx), macname,219NULL);220const OSSL_PARAM *params;221222if (mac == NULL)223return NULL;224225params = EVP_MAC_settable_ctx_params(mac);226EVP_MAC_free(mac);227228return params;229}230231#define MAC_SETTABLE_CTX_PARAMS(funcname, macname) \232static const OSSL_PARAM *mac_##funcname##_settable_ctx_params(void *ctx, \233void *provctx) \234{ \235return mac_settable_ctx_params(ctx, provctx, macname); \236}237238MAC_SETTABLE_CTX_PARAMS(hmac, "HMAC")239MAC_SETTABLE_CTX_PARAMS(siphash, "SIPHASH")240MAC_SETTABLE_CTX_PARAMS(poly1305, "POLY1305")241MAC_SETTABLE_CTX_PARAMS(cmac, "CMAC")242243#define MAC_SIGNATURE_FUNCTIONS(funcname) \244const OSSL_DISPATCH ossl_mac_legacy_##funcname##_signature_functions[] = { \245{ OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))mac_##funcname##_newctx }, \246{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, \247(void (*)(void))mac_digest_sign_init }, \248{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, \249(void (*)(void))mac_digest_sign_update }, \250{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, \251(void (*)(void))mac_digest_sign_final }, \252{ OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))mac_freectx }, \253{ OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))mac_dupctx }, \254{ OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, \255(void (*)(void))mac_set_ctx_params }, \256{ OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, \257(void (*)(void))mac_##funcname##_settable_ctx_params }, \258OSSL_DISPATCH_END \259};260261MAC_SIGNATURE_FUNCTIONS(hmac)262MAC_SIGNATURE_FUNCTIONS(siphash)263MAC_SIGNATURE_FUNCTIONS(poly1305)264MAC_SIGNATURE_FUNCTIONS(cmac)265266267