From 45d6379135504814ab723b57f0eb8be23393a51d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 09:24:22 +0200 Subject: Adding upstream version 1:9.16.44. Signed-off-by: Daniel Baumann --- lib/isc/hmac.c | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 lib/isc/hmac.c (limited to 'lib/isc/hmac.c') diff --git a/lib/isc/hmac.c b/lib/isc/hmac.c new file mode 100644 index 0000000..442ae7f --- /dev/null +++ b/lib/isc/hmac.c @@ -0,0 +1,146 @@ +/* + * Copyright (C) Internet Systems Consortium, Inc. ("ISC") + * + * SPDX-License-Identifier: MPL-2.0 + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, you can obtain one at https://mozilla.org/MPL/2.0/. + * + * See the COPYRIGHT file distributed with this work for additional + * information regarding copyright ownership. + */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "openssl_shim.h" + +isc_hmac_t * +isc_hmac_new(void) { + HMAC_CTX *hmac = HMAC_CTX_new(); + RUNTIME_CHECK(hmac != NULL); + return ((struct hmac *)hmac); +} + +void +isc_hmac_free(isc_hmac_t *hmac) { + if (ISC_UNLIKELY(hmac == NULL)) { + return; + } + + HMAC_CTX_free(hmac); +} + +isc_result_t +isc_hmac_init(isc_hmac_t *hmac, const void *key, size_t keylen, + const isc_md_type_t *md_type) { + REQUIRE(hmac != NULL); + REQUIRE(key != NULL); + + if (md_type == NULL) { + return (ISC_R_NOTIMPLEMENTED); + } + + if (HMAC_Init_ex(hmac, key, keylen, md_type, NULL) != 1) { + return (ISC_R_CRYPTOFAILURE); + } + + return (ISC_R_SUCCESS); +} + +isc_result_t +isc_hmac_reset(isc_hmac_t *hmac) { + REQUIRE(hmac != NULL); + + if (HMAC_CTX_reset(hmac) != 1) { + return (ISC_R_CRYPTOFAILURE); + } + + return (ISC_R_SUCCESS); +} + +isc_result_t +isc_hmac_update(isc_hmac_t *hmac, const unsigned char *buf, const size_t len) { + REQUIRE(hmac != NULL); + + if (ISC_UNLIKELY(buf == NULL || len == 0)) { + return (ISC_R_SUCCESS); + } + + if (HMAC_Update(hmac, buf, len) != 1) { + return (ISC_R_CRYPTOFAILURE); + } + + return (ISC_R_SUCCESS); +} + +isc_result_t +isc_hmac_final(isc_hmac_t *hmac, unsigned char *digest, + unsigned int *digestlen) { + REQUIRE(hmac != NULL); + REQUIRE(digest != NULL); + + if (HMAC_Final(hmac, digest, digestlen) != 1) { + return (ISC_R_CRYPTOFAILURE); + } + + return (ISC_R_SUCCESS); +} + +const isc_md_type_t * +isc_hmac_get_md_type(isc_hmac_t *hmac) { + REQUIRE(hmac != NULL); + + return (HMAC_CTX_get_md(hmac)); +} + +size_t +isc_hmac_get_size(isc_hmac_t *hmac) { + REQUIRE(hmac != NULL); + + return ((size_t)EVP_MD_size(HMAC_CTX_get_md(hmac))); +} + +int +isc_hmac_get_block_size(isc_hmac_t *hmac) { + REQUIRE(hmac != NULL); + + return (EVP_MD_block_size(HMAC_CTX_get_md(hmac))); +} + +isc_result_t +isc_hmac(const isc_md_type_t *type, const void *key, const int keylen, + const unsigned char *buf, const size_t len, unsigned char *digest, + unsigned int *digestlen) { + isc_result_t res; + isc_hmac_t *hmac = isc_hmac_new(); + + res = isc_hmac_init(hmac, key, keylen, type); + if (res != ISC_R_SUCCESS) { + goto end; + } + + res = isc_hmac_update(hmac, buf, len); + if (res != ISC_R_SUCCESS) { + goto end; + } + + res = isc_hmac_final(hmac, digest, digestlen); + if (res != ISC_R_SUCCESS) { + goto end; + } +end: + isc_hmac_free(hmac); + + return (res); +} -- cgit v1.2.3