Path: blob/main/crypto/openssl/providers/implementations/macs/cmac_prov.c
48383 views
/*1* Copyright 2018-2024 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/*10* CMAC low level APIs are deprecated for public use, but still ok for internal11* use.12*/13#include "internal/deprecated.h"1415#include <openssl/core_dispatch.h>16#include <openssl/core_names.h>17#include <openssl/params.h>18#include <openssl/evp.h>19#include <openssl/cmac.h>20#include <openssl/err.h>21#include <openssl/proverr.h>2223#include "prov/securitycheck.h"24#include "prov/implementations.h"25#include "prov/provider_ctx.h"26#include "prov/provider_util.h"27#include "prov/providercommon.h"28#include "crypto/cmac.h"2930/*31* Forward declaration of everything implemented here. This is not strictly32* necessary for the compiler, but provides an assurance that the signatures33* of the functions in the dispatch table are correct.34*/35static OSSL_FUNC_mac_newctx_fn cmac_new;36static OSSL_FUNC_mac_dupctx_fn cmac_dup;37static OSSL_FUNC_mac_freectx_fn cmac_free;38static OSSL_FUNC_mac_gettable_ctx_params_fn cmac_gettable_ctx_params;39static OSSL_FUNC_mac_get_ctx_params_fn cmac_get_ctx_params;40static OSSL_FUNC_mac_settable_ctx_params_fn cmac_settable_ctx_params;41static OSSL_FUNC_mac_set_ctx_params_fn cmac_set_ctx_params;42static OSSL_FUNC_mac_init_fn cmac_init;43static OSSL_FUNC_mac_update_fn cmac_update;44static OSSL_FUNC_mac_final_fn cmac_final;4546/* local CMAC data */4748struct cmac_data_st {49void *provctx;50CMAC_CTX *ctx;51PROV_CIPHER cipher;52OSSL_FIPS_IND_DECLARE53};5455static void *cmac_new(void *provctx)56{57struct cmac_data_st *macctx;5859if (!ossl_prov_is_running())60return NULL;6162if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL63|| (macctx->ctx = CMAC_CTX_new()) == NULL) {64OPENSSL_free(macctx);65macctx = NULL;66} else {67macctx->provctx = provctx;68OSSL_FIPS_IND_INIT(macctx)69}7071return macctx;72}7374static void cmac_free(void *vmacctx)75{76struct cmac_data_st *macctx = vmacctx;7778if (macctx != NULL) {79CMAC_CTX_free(macctx->ctx);80ossl_prov_cipher_reset(&macctx->cipher);81OPENSSL_free(macctx);82}83}8485static void *cmac_dup(void *vsrc)86{87struct cmac_data_st *src = vsrc;88struct cmac_data_st *dst;8990if (!ossl_prov_is_running())91return NULL;9293dst = cmac_new(src->provctx);94if (dst == NULL)95return NULL;96if (!CMAC_CTX_copy(dst->ctx, src->ctx)97|| !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {98cmac_free(dst);99return NULL;100}101OSSL_FIPS_IND_COPY(dst, src)102return dst;103}104105static size_t cmac_size(void *vmacctx)106{107struct cmac_data_st *macctx = vmacctx;108const EVP_CIPHER_CTX *cipherctx = CMAC_CTX_get0_cipher_ctx(macctx->ctx);109110if (EVP_CIPHER_CTX_get0_cipher(cipherctx) == NULL)111return 0;112113return EVP_CIPHER_CTX_get_block_size(cipherctx);114}115116#ifdef FIPS_MODULE117/*118* TDES Encryption is not approved in FIPS 140-3.119*120* In strict approved mode we just fail here (by returning 0).121* If we are going to bypass it using a FIPS indicator then we need to pass that122* information down to the cipher also.123* This function returns the param to pass down in 'p'.124* state will return OSSL_FIPS_IND_STATE_UNKNOWN if the param has not been set.125*126* The name 'OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK' used below matches the127* key name used by the Triple-DES.128*/129static int tdes_check_param(struct cmac_data_st *macctx, OSSL_PARAM *p,130int *state)131{132OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(macctx->provctx);133const EVP_CIPHER *cipher = ossl_prov_cipher_cipher(&macctx->cipher);134135*state = OSSL_FIPS_IND_STATE_UNKNOWN;136if (EVP_CIPHER_is_a(cipher, "DES-EDE3-CBC")) {137if (!OSSL_FIPS_IND_ON_UNAPPROVED(macctx, OSSL_FIPS_IND_SETTABLE0,138libctx, "CMAC", "Triple-DES",139ossl_fips_config_tdes_encrypt_disallowed))140return 0;141OSSL_FIPS_IND_GET_PARAM(macctx, p, state, OSSL_FIPS_IND_SETTABLE0,142OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK)143}144return 1;145}146#endif147148static int cmac_setkey(struct cmac_data_st *macctx,149const unsigned char *key, size_t keylen)150{151int rv;152OSSL_PARAM *p = NULL;153#ifdef FIPS_MODULE154int state = OSSL_FIPS_IND_STATE_UNKNOWN;155OSSL_PARAM prms[2] = { OSSL_PARAM_END, OSSL_PARAM_END };156157if (!tdes_check_param(macctx, &prms[0], &state))158return 0;159if (state != OSSL_FIPS_IND_STATE_UNKNOWN)160p = prms;161#endif162rv = ossl_cmac_init(macctx->ctx, key, keylen,163ossl_prov_cipher_cipher(&macctx->cipher),164ossl_prov_cipher_engine(&macctx->cipher), p);165ossl_prov_cipher_reset(&macctx->cipher);166return rv;167}168169static int cmac_init(void *vmacctx, const unsigned char *key,170size_t keylen, const OSSL_PARAM params[])171{172struct cmac_data_st *macctx = vmacctx;173174if (!ossl_prov_is_running() || !cmac_set_ctx_params(macctx, params))175return 0;176if (key != NULL)177return cmac_setkey(macctx, key, keylen);178/* Reinitialize the CMAC context */179return CMAC_Init(macctx->ctx, NULL, 0, NULL, NULL);180}181182static int cmac_update(void *vmacctx, const unsigned char *data,183size_t datalen)184{185struct cmac_data_st *macctx = vmacctx;186187return CMAC_Update(macctx->ctx, data, datalen);188}189190static int cmac_final(void *vmacctx, unsigned char *out, size_t *outl,191size_t outsize)192{193struct cmac_data_st *macctx = vmacctx;194195if (!ossl_prov_is_running())196return 0;197198return CMAC_Final(macctx->ctx, out, outl);199}200201static const OSSL_PARAM known_gettable_ctx_params[] = {202OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),203OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),204OSSL_FIPS_IND_GETTABLE_CTX_PARAM()205OSSL_PARAM_END206};207static const OSSL_PARAM *cmac_gettable_ctx_params(ossl_unused void *ctx,208ossl_unused void *provctx)209{210return known_gettable_ctx_params;211}212213static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])214{215OSSL_PARAM *p;216217if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL218&& !OSSL_PARAM_set_size_t(p, cmac_size(vmacctx)))219return 0;220221if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL222&& !OSSL_PARAM_set_size_t(p, cmac_size(vmacctx)))223return 0;224225if (!OSSL_FIPS_IND_GET_CTX_PARAM((struct cmac_data_st *)vmacctx, params))226return 0;227return 1;228}229230static const OSSL_PARAM known_settable_ctx_params[] = {231OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),232OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),233OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),234OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK)235OSSL_PARAM_END236};237static const OSSL_PARAM *cmac_settable_ctx_params(ossl_unused void *ctx,238ossl_unused void *provctx)239{240return known_settable_ctx_params;241}242243/*244* ALL parameters should be set before init().245*/246static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])247{248struct cmac_data_st *macctx = vmacctx;249OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx);250const OSSL_PARAM *p;251252if (ossl_param_is_empty(params))253return 1;254255if (!OSSL_FIPS_IND_SET_CTX_PARAM(macctx,256OSSL_FIPS_IND_SETTABLE0, params,257OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK))258return 0;259260if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CIPHER)) != NULL) {261if (!ossl_prov_cipher_load_from_params(&macctx->cipher, params, ctx))262return 0;263264if (EVP_CIPHER_get_mode(ossl_prov_cipher_cipher(&macctx->cipher))265!= EVP_CIPH_CBC_MODE) {266ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);267return 0;268}269#ifdef FIPS_MODULE270{271const EVP_CIPHER *cipher = ossl_prov_cipher_cipher(&macctx->cipher);272273if (!EVP_CIPHER_is_a(cipher, "AES-256-CBC")274&& !EVP_CIPHER_is_a(cipher, "AES-192-CBC")275&& !EVP_CIPHER_is_a(cipher, "AES-128-CBC")276&& !EVP_CIPHER_is_a(cipher, "DES-EDE3-CBC")) {277ERR_raise(ERR_LIB_PROV, EVP_R_UNSUPPORTED_CIPHER);278return 0;279}280}281#endif282}283284if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {285if (p->data_type != OSSL_PARAM_OCTET_STRING)286return 0;287return cmac_setkey(macctx, p->data, p->data_size);288}289return 1;290}291292const OSSL_DISPATCH ossl_cmac_functions[] = {293{ OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },294{ OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },295{ OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },296{ OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },297{ OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },298{ OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },299{ OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,300(void (*)(void))cmac_gettable_ctx_params },301{ OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params },302{ OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,303(void (*)(void))cmac_settable_ctx_params },304{ OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params },305OSSL_DISPATCH_END306};307308309