2018-03-20 17:20:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
*
|
|
|
|
* 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
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-03-20 17:20:50 +00:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
2020-03-09 16:17:26 +01:00
|
|
|
#include <openssl/hmac.h>
|
|
|
|
#include <openssl/opensslv.h>
|
|
|
|
|
2018-03-20 17:20:50 +00:00
|
|
|
#include <isc/assertions.h>
|
|
|
|
#include <isc/hmac.h>
|
|
|
|
#include <isc/md.h>
|
|
|
|
#include <isc/safe.h>
|
|
|
|
#include <isc/string.h>
|
|
|
|
#include <isc/types.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include "openssl_shim.h"
|
|
|
|
|
2018-03-20 17:20:50 +00:00
|
|
|
isc_hmac_t *
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_hmac_new(void) {
|
2020-03-12 09:45:58 +01:00
|
|
|
HMAC_CTX *hmac = HMAC_CTX_new();
|
2018-03-20 17:20:50 +00:00
|
|
|
RUNTIME_CHECK(hmac != NULL);
|
2020-03-12 09:45:58 +01:00
|
|
|
return ((struct hmac *)hmac);
|
2018-03-20 17:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_hmac_free(isc_hmac_t *hmac) {
|
2018-03-20 17:20:50 +00:00
|
|
|
if (ISC_UNLIKELY(hmac == NULL)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HMAC_CTX_free(hmac);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_hmac_init(isc_hmac_t *hmac, const void *key, size_t keylen,
|
2020-03-12 10:20:37 +01:00
|
|
|
const isc_md_type_t *md_type) {
|
2018-03-20 17:20:50 +00:00
|
|
|
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
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_hmac_reset(isc_hmac_t *hmac) {
|
2018-03-20 17:20:50 +00:00
|
|
|
REQUIRE(hmac != NULL);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
if (HMAC_CTX_reset(hmac) != 1) {
|
2018-03-20 17:20:50 +00:00
|
|
|
return (ISC_R_CRYPTOFAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_hmac_update(isc_hmac_t *hmac, const unsigned char *buf, const size_t len) {
|
2018-03-20 17:20:50 +00:00
|
|
|
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
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_hmac_final(isc_hmac_t *hmac, unsigned char *digest,
|
|
|
|
unsigned int *digestlen) {
|
2018-03-20 17:20:50 +00:00
|
|
|
REQUIRE(hmac != NULL);
|
|
|
|
REQUIRE(digest != NULL);
|
|
|
|
|
|
|
|
if (HMAC_Final(hmac, digest, digestlen) != 1) {
|
|
|
|
return (ISC_R_CRYPTOFAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2020-03-12 10:20:37 +01:00
|
|
|
const isc_md_type_t *
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_hmac_get_md_type(isc_hmac_t *hmac) {
|
2018-03-20 17:20:50 +00:00
|
|
|
REQUIRE(hmac != NULL);
|
|
|
|
|
|
|
|
return (HMAC_CTX_get_md(hmac));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_hmac_get_size(isc_hmac_t *hmac) {
|
2018-03-20 17:20:50 +00:00
|
|
|
REQUIRE(hmac != NULL);
|
|
|
|
|
|
|
|
return ((size_t)EVP_MD_size(HMAC_CTX_get_md(hmac)));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_hmac_get_block_size(isc_hmac_t *hmac) {
|
2018-03-20 17:20:50 +00:00
|
|
|
REQUIRE(hmac != NULL);
|
|
|
|
|
|
|
|
return (EVP_MD_block_size(HMAC_CTX_get_md(hmac)));
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-03-12 10:20:37 +01:00
|
|
|
isc_hmac(const isc_md_type_t *type, const void *key, const int keylen,
|
2020-02-12 13:59:18 +01:00
|
|
|
const unsigned char *buf, const size_t len, unsigned char *digest,
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int *digestlen) {
|
2018-03-20 17:20:50 +00:00
|
|
|
isc_result_t res;
|
2020-03-12 09:45:58 +01:00
|
|
|
isc_hmac_t *hmac = isc_hmac_new();
|
2018-03-20 17:20:50 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
end:
|
2018-03-20 17:20:50 +00:00
|
|
|
isc_hmac_free(hmac);
|
|
|
|
|
|
|
|
return (res);
|
|
|
|
}
|