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