2018-06-01 09:31:59 +02: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-06-01 09:31:59 +02:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2020-03-09 16:17:26 +01:00
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/opensslv.h>
|
|
|
|
|
2018-06-01 09:31:59 +02:00
|
|
|
#include <isc/md.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include "openssl_shim.h"
|
|
|
|
|
2018-06-01 09:31:59 +02:00
|
|
|
isc_md_t *
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_md_new(void) {
|
2018-06-01 09:31:59 +02:00
|
|
|
isc_md_t *md = EVP_MD_CTX_new();
|
|
|
|
RUNTIME_CHECK(md != NULL);
|
|
|
|
return (md);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_md_free(isc_md_t *md) {
|
2021-10-14 10:33:24 +02:00
|
|
|
if (md == NULL) {
|
2018-06-01 09:31:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EVP_MD_CTX_free(md);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-03-12 10:20:37 +01:00
|
|
|
isc_md_init(isc_md_t *md, const isc_md_type_t *md_type) {
|
2018-06-01 09:31:59 +02:00
|
|
|
REQUIRE(md != NULL);
|
|
|
|
|
|
|
|
if (md_type == NULL) {
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EVP_DigestInit_ex(md, md_type, NULL) != 1) {
|
|
|
|
return (ISC_R_CRYPTOFAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_md_reset(isc_md_t *md) {
|
2018-06-01 09:31:59 +02:00
|
|
|
REQUIRE(md != NULL);
|
|
|
|
|
|
|
|
if (EVP_MD_CTX_reset(md) != 1) {
|
|
|
|
return (ISC_R_CRYPTOFAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_md_update(isc_md_t *md, const unsigned char *buf, const size_t len) {
|
2018-06-01 09:31:59 +02:00
|
|
|
REQUIRE(md != NULL);
|
|
|
|
|
2021-10-14 10:33:24 +02:00
|
|
|
if (buf == NULL || len == 0) {
|
2018-06-01 09:31:59 +02:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EVP_DigestUpdate(md, buf, len) != 1) {
|
|
|
|
return (ISC_R_CRYPTOFAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_md_final(isc_md_t *md, unsigned char *digest, unsigned int *digestlen) {
|
2018-06-01 09:31:59 +02:00
|
|
|
REQUIRE(md != NULL);
|
|
|
|
REQUIRE(digest != NULL);
|
|
|
|
|
|
|
|
if (EVP_DigestFinal_ex(md, 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_md_get_md_type(isc_md_t *md) {
|
2018-06-01 09:31:59 +02:00
|
|
|
REQUIRE(md != NULL);
|
|
|
|
|
|
|
|
return (EVP_MD_CTX_md(md));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_md_get_size(isc_md_t *md) {
|
2018-06-01 09:31:59 +02:00
|
|
|
REQUIRE(md != NULL);
|
|
|
|
|
|
|
|
return (EVP_MD_CTX_size(md));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_md_get_block_size(isc_md_t *md) {
|
2018-06-01 09:31:59 +02:00
|
|
|
REQUIRE(md != NULL);
|
|
|
|
|
|
|
|
return (EVP_MD_CTX_block_size(md));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2020-03-12 10:20:37 +01:00
|
|
|
isc_md_type_get_size(const isc_md_type_t *md_type) {
|
|
|
|
STATIC_ASSERT(ISC_MAX_MD_SIZE >= EVP_MAX_MD_SIZE,
|
|
|
|
"Change ISC_MAX_MD_SIZE to be greater than or equal to "
|
|
|
|
"EVP_MAX_MD_SIZE");
|
2018-06-01 09:31:59 +02:00
|
|
|
if (md_type != NULL) {
|
|
|
|
return ((size_t)EVP_MD_size(md_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_MAX_MD_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2020-03-12 10:20:37 +01:00
|
|
|
isc_md_type_get_block_size(const isc_md_type_t *md_type) {
|
|
|
|
STATIC_ASSERT(ISC_MAX_MD_SIZE >= EVP_MAX_MD_SIZE,
|
|
|
|
"Change ISC_MAX_MD_SIZE to be greater than or equal to "
|
|
|
|
"EVP_MAX_MD_SIZE");
|
2018-06-01 09:31:59 +02:00
|
|
|
if (md_type != NULL) {
|
|
|
|
return ((size_t)EVP_MD_block_size(md_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_MAX_MD_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-03-12 10:20:37 +01:00
|
|
|
isc_md(const isc_md_type_t *md_type, const unsigned char *buf, const size_t len,
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned char *digest, unsigned int *digestlen) {
|
|
|
|
isc_md_t *md;
|
2018-06-01 09:31:59 +02:00
|
|
|
isc_result_t res;
|
|
|
|
|
|
|
|
md = isc_md_new();
|
|
|
|
|
|
|
|
res = isc_md_init(md, md_type);
|
|
|
|
if (res != ISC_R_SUCCESS) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = isc_md_update(md, buf, len);
|
|
|
|
if (res != ISC_R_SUCCESS) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = isc_md_final(md, digest, digestlen);
|
|
|
|
if (res != ISC_R_SUCCESS) {
|
|
|
|
goto end;
|
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
end:
|
2018-06-01 09:31:59 +02:00
|
|
|
isc_md_free(md);
|
|
|
|
|
|
|
|
return (res);
|
|
|
|
}
|
2020-03-12 10:20:37 +01:00
|
|
|
|
|
|
|
#define md_register_algorithm(alg) \
|
|
|
|
const isc_md_type_t *isc__md_##alg(void) { return (EVP_##alg()); }
|
|
|
|
|
|
|
|
md_register_algorithm(md5);
|
|
|
|
md_register_algorithm(sha1);
|
|
|
|
md_register_algorithm(sha224);
|
|
|
|
md_register_algorithm(sha256);
|
|
|
|
md_register_algorithm(sha384);
|
|
|
|
md_register_algorithm(sha512);
|