diff --git a/crypto/hmac/build.info b/crypto/hmac/build.info index b1c146182c..56ad67ef8f 100644 --- a/crypto/hmac/build.info +++ b/crypto/hmac/build.info @@ -1,6 +1,6 @@ LIBS=../../libcrypto -$COMMON=hmac.c hm_meth.c +$COMMON=hmac.c SOURCE[../../libcrypto]=$COMMON hm_ameth.c SOURCE[../../providers/fips]=$COMMON diff --git a/crypto/hmac/hm_meth.c b/crypto/hmac/hm_meth.c deleted file mode 100644 index 19278ef67d..0000000000 --- a/crypto/hmac/hm_meth.c +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright 2018 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 -#include -#include -#include -#include -#include "internal/evp_int.h" - -/* local HMAC context structure */ - -/* typedef EVP_MAC_IMPL */ -struct evp_mac_impl_st { - /* tmpmd and tmpengine are set to NULL after a CMAC_Init call */ - const EVP_MD *tmpmd; /* HMAC digest */ - const ENGINE *tmpengine; /* HMAC digest engine */ - HMAC_CTX *ctx; /* HMAC context */ -}; - -static EVP_MAC_IMPL *hmac_new(void) -{ - EVP_MAC_IMPL *hctx; - - if ((hctx = OPENSSL_zalloc(sizeof(*hctx))) == NULL - || (hctx->ctx = HMAC_CTX_new()) == NULL) { - OPENSSL_free(hctx); - return NULL; - } - - return hctx; -} - -static void hmac_free(EVP_MAC_IMPL *hctx) -{ - if (hctx != NULL) { - HMAC_CTX_free(hctx->ctx); - OPENSSL_free(hctx); - } -} - -static EVP_MAC_IMPL *hmac_dup(const EVP_MAC_IMPL *hsrc) -{ - EVP_MAC_IMPL *hdst; - - hdst = hmac_new(); - if (hdst == NULL) - return NULL; - - if (!HMAC_CTX_copy(hdst->ctx, hsrc->ctx)) { - hmac_free(hdst); - return NULL; - } - - hdst->tmpengine = hsrc->tmpengine; - hdst->tmpmd = hsrc->tmpmd; - - return hdst; -} - -static size_t hmac_size(EVP_MAC_IMPL *hctx) -{ - return HMAC_size(hctx->ctx); -} - -static int hmac_init(EVP_MAC_IMPL *hctx) -{ - int rv = 1; - - /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */ - if (hctx->tmpmd != NULL) - rv = HMAC_Init_ex(hctx->ctx, NULL, 0, hctx->tmpmd, - (ENGINE * )hctx->tmpengine); - hctx->tmpengine = NULL; - hctx->tmpmd = NULL; - return rv; -} - -static int hmac_update(EVP_MAC_IMPL *hctx, const unsigned char *data, - size_t datalen) -{ - return HMAC_Update(hctx->ctx, data, datalen); -} - -static int hmac_final(EVP_MAC_IMPL *hctx, unsigned char *out) -{ - unsigned int hlen; - - return HMAC_Final(hctx->ctx, out, &hlen); -} - -static int hmac_ctrl(EVP_MAC_IMPL *hctx, int cmd, va_list args) -{ - switch (cmd) { - case EVP_MAC_CTRL_SET_FLAGS: - { - unsigned long flags = va_arg(args, unsigned long); - - HMAC_CTX_set_flags(hctx->ctx, flags); - } - break; - case EVP_MAC_CTRL_SET_KEY: - { - const unsigned char *key = va_arg(args, const unsigned char *); - size_t keylen = va_arg(args, size_t); - int rv = HMAC_Init_ex(hctx->ctx, key, keylen, hctx->tmpmd, - (ENGINE *)hctx->tmpengine); - - hctx->tmpengine = NULL; - hctx->tmpmd = NULL; - return rv; - } - break; - case EVP_MAC_CTRL_SET_MD: - hctx->tmpmd = va_arg(args, const EVP_MD *); - break; - case EVP_MAC_CTRL_SET_ENGINE: - hctx->tmpengine = va_arg(args, const ENGINE *); - break; - default: - return -2; - - } - return 1; -} - -static int hmac_ctrl_int(EVP_MAC_IMPL *hctx, int cmd, ...) -{ - int rv; - va_list args; - - va_start(args, cmd); - rv = hmac_ctrl(hctx, cmd, args); - va_end(args); - - return rv; -} - -static int hmac_ctrl_str_cb(void *hctx, int cmd, void *buf, size_t buflen) -{ - return hmac_ctrl_int(hctx, cmd, buf, buflen); -} - -static int hmac_ctrl_str(EVP_MAC_IMPL *hctx, const char *type, - const char *value) -{ - if (!value) - return 0; -#ifndef FIPS_MODE - /* - * We don't have EVP_get_digestbyname() in FIPS_MODE. That function returns - * an EVP_MD without an associated provider implementation (i.e. it is - * using "implicit fetch"). We could replace it with an "explicit" fetch - * using EVP_MD_fetch(), but we'd then be required to free the returned - * EVP_MD somewhere. Probably the complexity isn't worth it as we are - * unlikely to need this ctrl in FIPS_MODE anyway. - */ - if (strcmp(type, "digest") == 0) { - const EVP_MD *d = EVP_get_digestbyname(value); - - if (d == NULL) - return 0; - return hmac_ctrl_int(hctx, EVP_MAC_CTRL_SET_MD, d); - } -#endif - if (strcmp(type, "key") == 0) - return EVP_str2ctrl(hmac_ctrl_str_cb, hctx, EVP_MAC_CTRL_SET_KEY, - value); - if (strcmp(type, "hexkey") == 0) - return EVP_hex2ctrl(hmac_ctrl_str_cb, hctx, EVP_MAC_CTRL_SET_KEY, - value); - return -2; -} - -const EVP_MAC hmac_meth = { - EVP_MAC_HMAC, - hmac_new, - hmac_dup, - hmac_free, - hmac_size, - hmac_init, - hmac_update, - hmac_final, - hmac_ctrl, - hmac_ctrl_str -}; diff --git a/providers/common/include/internal/provider_algs.h b/providers/common/include/internal/provider_algs.h index 74420bf1b3..579ec4117a 100644 --- a/providers/common/include/internal/provider_algs.h +++ b/providers/common/include/internal/provider_algs.h @@ -71,6 +71,7 @@ extern const OSSL_DISPATCH blake2bmac_functions[]; extern const OSSL_DISPATCH blake2smac_functions[]; extern const OSSL_DISPATCH cmac_functions[]; extern const OSSL_DISPATCH gmac_functions[]; +extern const OSSL_DISPATCH hmac_functions[]; /* Key management */ extern const OSSL_DISPATCH dh_keymgmt_functions[]; diff --git a/providers/common/macs/build.info b/providers/common/macs/build.info index 0477342d5a..e7b3e263a0 100644 --- a/providers/common/macs/build.info +++ b/providers/common/macs/build.info @@ -1,4 +1,4 @@ -$COMMON=cmac_prov.c gmac_prov.c +$COMMON=cmac_prov.c gmac_prov.c hmac_prov.c LIBS=../../../libcrypto SOURCE[../../../libcrypto]=$COMMON diff --git a/providers/common/macs/hmac_prov.c b/providers/common/macs/hmac_prov.c new file mode 100644 index 0000000000..09d29bec1d --- /dev/null +++ b/providers/common/macs/hmac_prov.c @@ -0,0 +1,284 @@ +/* + * Copyright 2018 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 +#include +#include +#include +#include +#include + +#include "internal/provider_algs.h" +#include "internal/provider_ctx.h" + +/* + * Forward declaration of everything implemented here. This is not strictly + * necessary for the compiler, but provides an assurance that the signatures + * of the functions in the dispatch table are correct. + */ +static OSSL_OP_mac_newctx_fn hmac_new; +static OSSL_OP_mac_dupctx_fn hmac_dup; +static OSSL_OP_mac_freectx_fn hmac_free; +static OSSL_OP_mac_gettable_ctx_params_fn hmac_gettable_ctx_params; +static OSSL_OP_mac_ctx_get_params_fn hmac_ctx_get_params; +static OSSL_OP_mac_settable_ctx_params_fn hmac_settable_ctx_params; +static OSSL_OP_mac_ctx_set_params_fn hmac_ctx_set_params; +static OSSL_OP_mac_init_fn hmac_init; +static OSSL_OP_mac_update_fn hmac_update; +static OSSL_OP_mac_final_fn hmac_final; + +/* local HMAC context structure */ + +/* typedef EVP_MAC_IMPL */ +struct hmac_data_st { + void *provctx; + HMAC_CTX *ctx; /* HMAC context */ + + /* + * References to the underlying digest implementation. tmpmd caches + * the md, always. alloc_md only holds a reference to an explicitly + * fetched digest. + * tmpmd is cleared after a CMAC_Init call. + */ + const EVP_MD *tmpmd; /* HMAC digest */ + EVP_MD *alloc_md; /* fetched digest */ + + /* + * Conditions for legacy EVP_MD uses. + * tmpengine is cleared after a CMAC_Init call. + */ + ENGINE *tmpengine; /* HMAC digest engine */ +}; + +static size_t hmac_size(void *vmacctx); + +static void *hmac_new(void *provctx) +{ + struct hmac_data_st *macctx; + + if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL + || (macctx->ctx = HMAC_CTX_new()) == NULL) { + OPENSSL_free(macctx); + return NULL; + } + /* TODO(3.0) Should we do something more with that context? */ + macctx->provctx = provctx; + + return macctx; +} + +static void hmac_free(void *vmacctx) +{ + struct hmac_data_st *macctx = vmacctx; + + if (macctx != NULL) { + HMAC_CTX_free(macctx->ctx); + EVP_MD_meth_free(macctx->alloc_md); + OPENSSL_free(macctx); + } +} + +static void *hmac_dup(void *vsrc) +{ + struct hmac_data_st *src = vsrc; + struct hmac_data_st *dst = hmac_new(src->provctx); + + if (dst == NULL) + return NULL; + + if (!HMAC_CTX_copy(dst->ctx, src->ctx)) { + hmac_free(dst); + return NULL; + } + + if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md)) { + hmac_free(dst); + return NULL; + } + + dst->tmpengine = src->tmpengine; + dst->tmpmd = src->tmpmd; + dst->alloc_md = src->alloc_md; + return dst; +} + +static size_t hmac_size(void *vmacctx) +{ + struct hmac_data_st *macctx = vmacctx; + + return HMAC_size(macctx->ctx); +} + +static int hmac_init(void *vmacctx) +{ + struct hmac_data_st *macctx = vmacctx; + int rv = 1; + + /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */ + if (macctx->tmpmd != NULL) + rv = HMAC_Init_ex(macctx->ctx, NULL, 0, macctx->tmpmd, + (ENGINE * )macctx->tmpengine); + macctx->tmpengine = NULL; + macctx->tmpmd = NULL; + return rv; +} + +static int hmac_update(void *vmacctx, const unsigned char *data, + size_t datalen) +{ + struct hmac_data_st *macctx = vmacctx; + + return HMAC_Update(macctx->ctx, data, datalen); +} + +static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl, + size_t outsize) +{ + unsigned int hlen; + struct hmac_data_st *macctx = vmacctx; + + if (!HMAC_Final(macctx->ctx, out, &hlen)) + return 0; + if (outl != NULL) + *outl = hlen; + return 1; +} + +static const OSSL_PARAM known_gettable_ctx_params[] = { + OSSL_PARAM_size_t(OSSL_MAC_PARAM_OUTLEN, NULL), + OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), /* Same as "outlen" */ + OSSL_PARAM_END +}; +static const OSSL_PARAM *hmac_gettable_ctx_params(void) +{ + return known_gettable_ctx_params; +} + +static int hmac_ctx_get_params(void *vmacctx, OSSL_PARAM params[]) +{ + OSSL_PARAM *p; + + if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_OUTLEN)) != NULL + || (p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL) + return OSSL_PARAM_set_size_t(p, hmac_size(vmacctx)); + + return 1; +} + +static const OSSL_PARAM known_settable_ctx_params[] = { + /* "algorithm" and "digest" are the same parameter */ + OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ALGORITHM, NULL, 0), + OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0), + OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ENGINE, NULL, 0), + OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0), + OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), + OSSL_PARAM_int(OSSL_MAC_PARAM_FLAGS, NULL), + OSSL_PARAM_END +}; +static const OSSL_PARAM *hmac_settable_ctx_params(void) +{ + return known_settable_ctx_params; +} + +/* + * ALL parameters should be set before init(). + */ +static int hmac_ctx_set_params(void *vmacctx, const OSSL_PARAM params[]) +{ + struct hmac_data_st *macctx = vmacctx; + const OSSL_PARAM *p; + + if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_DIGEST)) != NULL + || (p = OSSL_PARAM_locate_const(params, + OSSL_MAC_PARAM_ALGORITHM)) != NULL) { + if (p->data_type != OSSL_PARAM_UTF8_STRING) + return 0; + + { + const char *algoname = p->data; + const char *propquery = NULL; + +#ifndef FIPS_MODE /* Inside the FIPS module, we don't support engines */ + ENGINE_finish(macctx->tmpengine); + macctx->tmpengine = NULL; + + if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_ENGINE)) + != NULL) { + if (p->data_type != OSSL_PARAM_UTF8_STRING) + return 0; + + macctx->tmpengine = ENGINE_by_id(p->data); + if (macctx->tmpengine == NULL) + return 0; + } +#endif + if ((p = OSSL_PARAM_locate_const(params, + OSSL_MAC_PARAM_PROPERTIES)) + != NULL) { + if (p->data_type != OSSL_PARAM_UTF8_STRING) + return 0; + + propquery = p->data; + } + + EVP_MD_meth_free(macctx->alloc_md); + + macctx->tmpmd = macctx->alloc_md = + EVP_MD_fetch(PROV_LIBRARY_CONTEXT_OF(macctx->provctx), + algoname, propquery); + +#ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */ + /* TODO(3.0) BEGIN legacy stuff, to be removed */ + if (macctx->tmpmd == NULL) + macctx->tmpmd = EVP_get_digestbyname(algoname); + /* TODO(3.0) END of legacy stuff */ +#endif + + if (macctx->tmpmd == NULL) + return 0; + } + } + /* TODO(3.0) formalize the meaning of "flags", perhaps as other params */ + if ((p = OSSL_PARAM_locate_const(params, + OSSL_MAC_PARAM_FLAGS)) != NULL) { + int flags = 0; + + if (!OSSL_PARAM_get_int(p, &flags)) + return 0; + HMAC_CTX_set_flags(macctx->ctx, flags); + } + if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) { + if (p->data_type != OSSL_PARAM_OCTET_STRING) + return 0; + + if (!HMAC_Init_ex(macctx->ctx, p->data, p->data_size, + macctx->tmpmd, NULL /* ENGINE */)) + return 0; + + macctx->tmpmd = NULL; + macctx->tmpengine = NULL; + } + return 1; +} + +const OSSL_DISPATCH hmac_functions[] = { + { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new }, + { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup }, + { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free }, + { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init }, + { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update }, + { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final }, + { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, + (void (*)(void))hmac_gettable_ctx_params }, + { OSSL_FUNC_MAC_CTX_GET_PARAMS, (void (*)(void))hmac_ctx_get_params }, + { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, + (void (*)(void))hmac_settable_ctx_params }, + { OSSL_FUNC_MAC_CTX_SET_PARAMS, (void (*)(void))hmac_ctx_set_params }, + { 0, NULL } +}; diff --git a/providers/default/defltprov.c b/providers/default/defltprov.c index d4e57bacdf..6f7ae3df1b 100644 --- a/providers/default/defltprov.c +++ b/providers/default/defltprov.c @@ -132,6 +132,7 @@ static const OSSL_ALGORITHM deflt_macs[] = { { "CMAC", "default=yes", cmac_functions }, #endif { "GMAC", "default=yes", gmac_functions }, + { "HMAC", "default=yes", hmac_functions }, { NULL, NULL, NULL } }; diff --git a/providers/fips/fipsprov.c b/providers/fips/fipsprov.c index be601c5dac..9b444ff89e 100644 --- a/providers/fips/fipsprov.c +++ b/providers/fips/fipsprov.c @@ -277,6 +277,7 @@ static const OSSL_ALGORITHM fips_ciphers[] = { static const OSSL_ALGORITHM fips_macs[] = { { "CMAC", "fips=yes", cmac_functions }, { "GMAC", "fips=yes", gmac_functions }, + { "HMAC", "fips=yes", hmac_functions }, { NULL, NULL, NULL } };