libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

mhd_md5.h (8635B)


      1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */
      2 /*
      3   This file is part of GNU libmicrohttpd.
      4   Copyright (C) 2022-2026 Evgeny Grin (Karlson2k)
      5 
      6   GNU libmicrohttpd is free software; you can redistribute it and/or
      7   modify it under the terms of the GNU Lesser General Public
      8   License as published by the Free Software Foundation; either
      9   version 2.1 of the License, or (at your option) any later version.
     10 
     11   GNU libmicrohttpd is distributed in the hope that it will be useful,
     12   but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14   Lesser General Public License for more details.
     15 
     16   Alternatively, you can redistribute GNU libmicrohttpd and/or
     17   modify it under the terms of the GNU General Public License as
     18   published by the Free Software Foundation; either version 2 of
     19   the License, or (at your option) any later version, together
     20   with the eCos exception, as follows:
     21 
     22     As a special exception, if other files instantiate templates or
     23     use macros or inline functions from this file, or you compile this
     24     file and link it with other works to produce a work based on this
     25     file, this file does not by itself cause the resulting work to be
     26     covered by the GNU General Public License. However the source code
     27     for this file must still be made available in accordance with
     28     section (3) of the GNU General Public License v2.
     29 
     30     This exception does not invalidate any other reasons why a work
     31     based on this file might be covered by the GNU General Public
     32     License.
     33 
     34   You should have received copies of the GNU Lesser General Public
     35   License and the GNU General Public License along with this library;
     36   if not, see <https://www.gnu.org/licenses/>.
     37 */
     38 
     39 /**
     40  * @file src/mhd2/mhd_md5.h
     41  * @brief  Simple wrapper for selecting the MD5 implementation backend
     42  *
     43  * Usage: declare a context as 'struct mhd_Md5Ctx', then call mhd_MD5_init(),
     44  * mhd_MD5_update() (any number of times) and mhd_MD5_finish() to get the
     45  * digest. Reuse the context with mhd_MD5_reset() or release it with
     46  * mhd_MD5_deinit(). With some backends an operation can fail; check with
     47  * mhd_MD5_has_err().
     48  *
     49  * @author Karlson2k (Evgeny Grin)
     50  */
     51 
     52 #ifndef MHD_MD5_H
     53 #define MHD_MD5_H 1
     54 
     55 #include "mhd_sys_options.h"
     56 
     57 #ifndef MHD_SUPPORT_MD5
     58 #  error This file must be used only when MD5 is enabled
     59 #endif
     60 
     61 #include "mhd_macro_concat.h"
     62 
     63 #if defined(MHD_MD5_GNUTLS)
     64 #  include "md5_gnutls.h"
     65 
     66 /**
     67  * The hashing backend identifier for function names
     68  */
     69 #  define mhd_MD5_FUNC_NAME_ID gtls
     70 /**
     71  * The hashing backend identifier for data names
     72  */
     73 #  define mhd_MD5_DATA_NAME_ID Gtls
     74 /**
     75  * The hashing backend identifier for macro names
     76  */
     77 #  define mhd_MD5_MACRO_NAME_ID GTLS
     78 
     79 #elif defined(MHD_MD5_OPENSSL)
     80 #  include "md5_openssl.h"
     81 
     82 /**
     83  * The hashing backend identifier for function names
     84  */
     85 #  define mhd_MD5_FUNC_NAME_ID ossl
     86 /**
     87  * The hashing backend identifier for data names
     88  */
     89 #  define mhd_MD5_DATA_NAME_ID Ossl
     90 /**
     91  * The hashing backend identifier for macro names
     92  */
     93 #  define mhd_MD5_MACRO_NAME_ID OSSL
     94 
     95 #elif defined(MHD_MD5_MBEDTLS)
     96 #  include "md5_mbedtls.h"
     97 
     98 /**
     99  * The hashing backend identifier for function names
    100  */
    101 #  define mhd_MD5_FUNC_NAME_ID mtls
    102 /**
    103  * The hashing backend identifier for data names
    104  */
    105 #  define mhd_MD5_DATA_NAME_ID Mtls
    106 /**
    107  * The hashing backend identifier for macro names
    108  */
    109 #  define mhd_MD5_MACRO_NAME_ID MTLS
    110 #elif defined(MHD_MD5_BUILTIN)
    111 #  include "md5_builtin.h"
    112 
    113 /**
    114  * The hashing backend identifier for function names
    115  */
    116 #  define mhd_MD5_FUNC_NAME_ID blti
    117 /**
    118  * The hashing backend identifier for data names
    119  */
    120 #  define mhd_MD5_DATA_NAME_ID Blti
    121 /**
    122  * The hashing backend identifier for macro names
    123  */
    124 #  define mhd_MD5_MACRO_NAME_ID BLTI
    125 #else
    126 #  error No MD5 implementation enabled
    127 #endif
    128 
    129 #if mhd_MD5_DIGEST_SIZE != 16
    130 #  error Wrong value of mhd_MD5_DIGEST_SIZE
    131 #endif
    132 
    133 /**
    134  * Form the name of the function specific to the selected hashing backend
    135  */
    136 #define mhd_MD5_FUNC(name_suffix)    \
    137         mhd_MACRO_CONCAT3 (mhd_MD5_,mhd_MD5_FUNC_NAME_ID,name_suffix)
    138 
    139 /**
    140  * Form the name of the macro specific to the selected hashing backend
    141  */
    142 #define mhd_MD5_MACRO(name_suffix)    \
    143         mhd_MACRO_CONCAT3 (mhd_MD5_,mhd_MD5_MACRO_NAME_ID,name_suffix)
    144 
    145 /**
    146  * Struct tag of the calculation context for the selected MD5 backend
    147  */
    148 #define mhd_Md5Ctx mhd_MACRO_CONCAT(mhd_Md5Ctx,mhd_MD5_DATA_NAME_ID)
    149 
    150 
    151 #if 0 != mhd_MD5_MACRO (_EXT_ERROR_FLAG)
    152 /**
    153  * Indicates that MD5 hashing or initialisation may result in an error
    154  */
    155 #  define mhd_MD5_HAS_EXT_ERROR 1
    156 /**
    157  * 'true' if the hashing backend has reported an error
    158  */
    159 #  define mhd_MD5_has_err(ctx) (!!((ctx)->ext_error))
    160 /**
    161  * Get the error number reported by hashing backend (zero if no error)
    162  */
    163 #  define mhd_MD5_get_err(ctx) ((int)((ctx)->ext_error))
    164 #else
    165 #  define mhd_MD5_has_err(ctx) (((void) (ctx)), !!0)
    166 /**
    167  * Get the error number (zero if no error)
    168  */
    169 #  define mhd_MD5_get_err(ctx) ((int)(0))
    170 #endif
    171 
    172 /**
    173  * Initialise the context for MD5 calculation.
    174  *
    175  * This function must not be called more than once for @a ctx without an
    176  * intervening #mhd_MD5_deinit().
    177  *
    178  * Whether or not an error is reported, #mhd_MD5_deinit() must be
    179  * called for @a ctx once this function has returned.
    180  *
    181  * For backends that track errors (see #mhd_MD5_has_err()), the error state is
    182  * set according to the result: cleared on success, set on failure.
    183  *
    184  * @param ctx the calculation context
    185  */
    186 #define mhd_MD5_init(ctx) mhd_MD5_FUNC(_init) ((ctx))
    187 
    188 /**
    189  * Add a portion of data to the MD5 calculation.
    190  *
    191  * For backends that track errors (see #mhd_MD5_has_err()), this does nothing
    192  * if @a ctx already has a recorded error, and records an error on failure.
    193  *
    194  * @param ctx the calculation context
    195  * @param size number of bytes in @a data, must not be 0
    196  * @param data bytes to add to hash
    197  */
    198 #define mhd_MD5_update(ctx, size, data) \
    199         mhd_MD5_FUNC(_update) ((ctx),(size),(data))
    200 
    201 /**
    202  * Finalise the MD5 calculation and return the digest.
    203  *
    204  * For backends that track errors (see #mhd_MD5_has_err()), this does nothing
    205  * if @a ctx already has a recorded error, and may record an error on failure.
    206  *
    207  * @param ctx the calculation context
    208  * @param[out] digest set to the hash, must be #mhd_MD5_DIGEST_SIZE bytes
    209  */
    210 #define mhd_MD5_finish(ctx, digest) \
    211         mhd_MD5_FUNC(_finish) ((ctx),(digest))
    212 
    213 /**
    214  * Reset the context for a new MD5 calculation.
    215  *
    216  * This function may be called only after #mhd_MD5_finish(). If the context is
    217  * subsequently reused for a new calculation, this function must be called
    218  * before starting that calculation.
    219  *
    220  * For backends that track errors (see #mhd_MD5_has_err()), this does nothing
    221  * if @a ctx already has a recorded error, and may record an error on failure.
    222  *
    223  * @param ctx the calculation context
    224  */
    225 #define mhd_MD5_reset(ctx) mhd_MD5_FUNC(_reset) ((ctx))
    226 
    227 /**
    228  * Finalise the MD5 calculation, return the digest and reset the context.
    229  *
    230  * For backends that track errors (see #mhd_MD5_has_err()): if @a ctx already
    231  * has a recorded error, this does nothing and the error is preserved.
    232  * Otherwise, an error is recorded in @a ctx if either the finalisation or the
    233  * reset fails. A recorded error is never cleared by this macro.
    234  *
    235  * @param ctx the calculation context
    236  * @param[out] digest set to the hash, must be #mhd_MD5_DIGEST_SIZE bytes
    237  */
    238 #define mhd_MD5_finish_reset(ctx, digest) \
    239         (mhd_MD5_finish((ctx),(digest)), mhd_MD5_reset((ctx)))
    240 
    241 /**
    242  * Deinitialise the MD5 calculation context.
    243  *
    244  * After calling this function, @a ctx can be reinitialised with
    245  * #mhd_MD5_init().
    246  *
    247  * For backends that track errors (see #mhd_MD5_has_err()), the recorded error
    248  * state is preserved.
    249  *
    250  * @param ctx the calculation context
    251  */
    252 #define mhd_MD5_deinit(ctx) mhd_MD5_FUNC(_deinit) ((ctx))
    253 
    254 /**
    255  * Finalise the MD5 calculation, return the digest and deinitialise the context.
    256  *
    257  * For backends that track errors (see #mhd_MD5_has_err()): if @a ctx already
    258  * has a recorded error, the error is preserved. Otherwise, an error is
    259  * recorded in @a ctx if the finalisation fails.
    260  *
    261  * @param ctx the calculation context
    262  * @param[out] digest set to the hash, must be #mhd_MD5_DIGEST_SIZE bytes
    263  */
    264 #define mhd_MD5_finish_deinit(ctx, digest) \
    265         (mhd_MD5_finish((ctx),(digest)), mhd_MD5_deinit((ctx)))
    266 
    267 #if 0 == mhd_MD5_MACRO (_DEINIT_NOOP_FLAG)
    268 /**
    269  * Indicates that #mhd_MD5_deinit() is a real function that must be called.
    270  */
    271 #  define mhd_MD5_HAS_DEINIT
    272 #endif
    273 
    274 
    275 #endif /* MHD_MD5_H */