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