1 /* $NetBSD: md.h,v 1.7 2025/01/26 16:25:41 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * SPDX-License-Identifier: MPL-2.0 7 * 8 * This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 * 12 * See the COPYRIGHT file distributed with this work for additional 13 * information regarding copyright ownership. 14 */ 15 16 /*! 17 * \file isc/md.h 18 * \brief This is the header file for message digest algorithms. 19 */ 20 21 #pragma once 22 23 #include <isc/lang.h> 24 #include <isc/result.h> 25 #include <isc/types.h> 26 27 typedef void isc_md_t; 28 29 /** 30 * isc_md_type_t: 31 * @ISC_MD_MD5: MD5 32 * @ISC_MD_SHA1: SHA-1 33 * @ISC_MD_SHA224: SHA-224 34 * @ISC_MD_SHA256: SHA-256 35 * @ISC_MD_SHA384: SHA-384 36 * @ISC_MD_SHA512: SHA-512 37 * 38 * Enumeration of supported message digest algorithms. 39 */ 40 typedef void isc_md_type_t; 41 42 extern const isc_md_type_t *isc__md_md5; 43 extern const isc_md_type_t *isc__md_sha1; 44 extern const isc_md_type_t *isc__md_sha224; 45 extern const isc_md_type_t *isc__md_sha256; 46 extern const isc_md_type_t *isc__md_sha384; 47 extern const isc_md_type_t *isc__md_sha512; 48 49 #define ISC_MD_MD5 isc__md_md5 50 #define ISC_MD_SHA1 isc__md_sha1 51 #define ISC_MD_SHA224 isc__md_sha224 52 #define ISC_MD_SHA256 isc__md_sha256 53 #define ISC_MD_SHA384 isc__md_sha384 54 #define ISC_MD_SHA512 isc__md_sha512 55 56 #define ISC_MD5_DIGESTLENGTH isc_md_type_get_size(ISC_MD_MD5) 57 #define ISC_MD5_BLOCK_LENGTH isc_md_type_get_block_size(ISC_MD_MD5) 58 #define ISC_SHA1_DIGESTLENGTH isc_md_type_get_size(ISC_MD_SHA1) 59 #define ISC_SHA1_BLOCK_LENGTH isc_md_type_get_block_size(ISC_MD_SHA1) 60 #define ISC_SHA224_DIGESTLENGTH isc_md_type_get_size(ISC_MD_SHA224) 61 #define ISC_SHA224_BLOCK_LENGTH isc_md_type_get_block_size(ISC_MD_SHA224) 62 #define ISC_SHA256_DIGESTLENGTH isc_md_type_get_size(ISC_MD_SHA256) 63 #define ISC_SHA256_BLOCK_LENGTH isc_md_type_get_block_size(ISC_MD_SHA256) 64 #define ISC_SHA384_DIGESTLENGTH isc_md_type_get_size(ISC_MD_SHA384) 65 #define ISC_SHA384_BLOCK_LENGTH isc_md_type_get_block_size(ISC_MD_SHA384) 66 #define ISC_SHA512_DIGESTLENGTH isc_md_type_get_size(ISC_MD_SHA512) 67 #define ISC_SHA512_BLOCK_LENGTH isc_md_type_get_block_size(ISC_MD_SHA512) 68 69 #define ISC_MAX_MD_SIZE 64U /* EVP_MAX_MD_SIZE */ 70 #define ISC_MAX_BLOCK_SIZE 128U /* ISC_SHA512_BLOCK_LENGTH */ 71 72 /** 73 * isc_md: 74 * @type: the digest type 75 * @buf: the data to hash 76 * @len: the length of the data to hash 77 * @digest: the output buffer 78 * @digestlen: the length of the data written to @digest 79 * 80 * This function hashes @len bytes of data at @buf and places the result in 81 * @digest. If the @digestlen parameter is not NULL then the number of bytes of 82 * data written (i.e. the length of the digest) will be written to the integer 83 * at @digestlen, at most ISC_MAX_MD_SIZE bytes will be written. 84 */ 85 isc_result_t 86 isc_md(const isc_md_type_t *type, const unsigned char *buf, const size_t len, 87 unsigned char *digest, unsigned int *digestlen); 88 89 /** 90 * isc_md_new: 91 * 92 * This function allocates, initializes and returns a digest context. 93 */ 94 isc_md_t * 95 isc_md_new(void); 96 97 /** 98 * isc_md_free: 99 * @md: message digest context 100 * 101 * This function cleans up digest context ctx and frees up the space allocated 102 * to it. 103 */ 104 void 105 isc_md_free(isc_md_t *); 106 107 /** 108 * isc_md_init: 109 * @md: message digest context 110 * @type: digest type 111 * 112 * This function sets up digest context @md to use a digest @type. @md must be 113 * initialized before calling this function. 114 */ 115 isc_result_t 116 isc_md_init(isc_md_t *, const isc_md_type_t *md_type); 117 118 /** 119 * isc_md_reset: 120 * @md: message digest context 121 * 122 * This function resets the digest context ctx. This can be used to reuse an 123 * already existing context. 124 */ 125 isc_result_t 126 isc_md_reset(isc_md_t *md); 127 128 /** 129 * isc_md_update: 130 * @md: message digest context 131 * @buf: data to hash 132 * @len: length of the data to hash 133 * 134 * This function hashes @len bytes of data at @buf into the digest context @md. 135 * This function can be called several times on the same @md to hash additional 136 * data. 137 */ 138 isc_result_t 139 isc_md_update(isc_md_t *md, const unsigned char *buf, const size_t len); 140 141 /** 142 * isc_md_final: 143 * @md: message digest context 144 * @digest: the output buffer 145 * @digestlen: the length of the data written to @digest 146 * 147 * This function retrieves the digest value from @md and places it in @digest. 148 * If the @digestlen parameter is not NULL then the number of bytes of data 149 * written (i.e. the length of the digest) will be written to the integer at 150 * @digestlen, at most ISC_MAX_MD_SIZE bytes will be written. After calling 151 * this function no additional calls to isc_md_update() can be made. 152 */ 153 isc_result_t 154 isc_md_final(isc_md_t *md, unsigned char *digest, unsigned int *digestlen); 155 156 /** 157 * isc_md_get_type: 158 * @md: message digest contezt 159 * 160 * This function return the isc_md_type_t previously set for the supplied 161 * message digest context or NULL if no isc_md_type_t has been set. 162 */ 163 const isc_md_type_t * 164 isc_md_get_md_type(isc_md_t *md); 165 166 /** 167 * isc_md_size: 168 * 169 * This function return the size of the message digest when passed an isc_md_t 170 * structure, i.e. the size of the hash. 171 */ 172 size_t 173 isc_md_get_size(isc_md_t *md); 174 175 /** 176 * isc_md_block_size: 177 * 178 * This function return the block size of the message digest when passed an 179 * isc_md_t structure. 180 */ 181 size_t 182 isc_md_get_block_size(isc_md_t *md); 183 184 /** 185 * isc_md_size: 186 * 187 * This function return the size of the message digest when passed an 188 * isc_md_type_t , i.e. the size of the hash. 189 */ 190 size_t 191 isc_md_type_get_size(const isc_md_type_t *md_type); 192 193 /** 194 * isc_md_block_size: 195 * 196 * This function return the block size of the message digest when passed an 197 * isc_md_type_t. 198 */ 199 size_t 200 isc_md_type_get_block_size(const isc_md_type_t *md_type); 201 202 /** 203 * Private 204 */ 205 206 void 207 isc__md_initialize(void); 208 209 void 210 isc__md_shutdown(void); 211