libmicrohttpd2

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

md5_openssl.h (5596B)


      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  * @file src/mhd2/md5_openssl.h
     40  * @brief  Wrapper declarations for MD5 calculation performed by OpenSSL backend
     41  * @author Karlson2k (Evgeny Grin)
     42  */
     43 #ifndef MHD_MD5_OPENSSL_H
     44 #define MHD_MD5_OPENSSL_H 1
     45 
     46 #include "mhd_sys_options.h"
     47 
     48 #include "sys_bool_type.h"
     49 #include "sys_base_types.h"
     50 
     51 #ifndef mhd_MD5_DIGEST_SIZE
     52 /**
     53  * Size of MD5 resulting digest in bytes
     54  * This is the final digest size, not the intermediate hash.
     55  */
     56 #  define mhd_MD5_DIGEST_SIZE (16)
     57 #endif
     58 
     59 /* Forward declaration for OpenSSL struct, to avoid including large header */
     60 struct evp_md_ctx_st;
     61 
     62 /**
     63  * MD5 calculation context
     64  */
     65 struct mhd_Md5CtxOssl
     66 {
     67   struct evp_md_ctx_st *ossl_ctx; /**< Hash calculation handle */
     68   bool ext_error; /**< 'true' if the hashing backend has reported an error */
     69 };
     70 
     71 /**
     72  * Indicates whether struct mhd_Md5CtxOssl has an 'ext_error' member.
     73  *
     74  * Set to '1' when the structure has an 'ext_error' member.
     75  */
     76 #define mhd_MD5_OSSL_EXT_ERROR_FLAG 1
     77 
     78 /**
     79  * Initialise the context for MD5 calculation.
     80  *
     81  * This function must not be called more than once for @a ctx without an
     82  * intervening #mhd_MD5_ossl_deinit().
     83  *
     84  * Whether or not an error is reported, #mhd_MD5_ossl_deinit() must be
     85  * called for @a ctx once this function has returned.
     86  *
     87  * The error state in @a ctx is set according to the result: cleared on
     88  * success, set on failure.
     89  *
     90  * @param ctx the calculation context
     91  */
     92 MHD_INTERNAL void
     93 mhd_MD5_ossl_init (struct mhd_Md5CtxOssl *ctx)
     94 MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_OUT_ (1);
     95 
     96 /**
     97  * Add a portion of data to the MD5 calculation.
     98  *
     99  * If @a ctx already has a recorded error, this function does nothing.
    100  * Otherwise, if the operation fails, the error is recorded in @a ctx.
    101  *
    102  * @param ctx the calculation context
    103  * @param size number of bytes in @a data, must not be 0
    104  * @param data bytes to add to hash
    105  */
    106 MHD_INTERNAL void
    107 mhd_MD5_ossl_update (struct mhd_Md5CtxOssl *restrict ctx,
    108                      size_t size,
    109                      const void *restrict data)
    110 MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) MHD_FN_PAR_IN_SIZE_ (3, 2);
    111 
    112 /**
    113  * Finalise the MD5 calculation and return the digest.
    114  *
    115  * If @a ctx already has a recorded error, this function does nothing.
    116  * Otherwise, if the operation fails, the error is recorded in @a ctx.
    117  *
    118  * @param ctx the calculation context
    119  * @param[out] digest set to the hash, must be #mhd_MD5_DIGEST_SIZE bytes
    120  */
    121 MHD_INTERNAL void
    122 mhd_MD5_ossl_finish (
    123   struct mhd_Md5CtxOssl *restrict ctx,
    124   uint8_t digest[MHD_FN_PAR_FIX_ARR_SIZE_ (mhd_MD5_DIGEST_SIZE)])
    125 MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) MHD_FN_PAR_OUT_ (2);
    126 
    127 /**
    128  * Reset the context for a new MD5 calculation.
    129  *
    130  * This function may be called only after #mhd_MD5_ossl_finish(). If the context
    131  * is subsequently reused for a new calculation, this function must be called
    132  * before starting that calculation.
    133  *
    134  * If @a ctx already has a recorded error, this function does nothing and the
    135  * error is preserved. Otherwise, if the operation fails, the error is recorded
    136  * in @a ctx.
    137  *
    138  * @param ctx the calculation context
    139  */
    140 MHD_INTERNAL void
    141 mhd_MD5_ossl_reset (struct mhd_Md5CtxOssl *ctx)
    142 MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1);
    143 
    144 /**
    145  * Deinitialise the MD5 calculation context.
    146  *
    147  * After calling this function, @a ctx can be reinitialised with
    148  * #mhd_MD5_ossl_init().
    149  *
    150  * The recorded error state in @a ctx is preserved.
    151  *
    152  * @param ctx the calculation context
    153  */
    154 MHD_INTERNAL void
    155 mhd_MD5_ossl_deinit (struct mhd_Md5CtxOssl *ctx)
    156 MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1);
    157 
    158 /**
    159  * Indicates whether #mhd_MD5_ossl_deinit() is a real function or a no-op.
    160  *
    161  * Set to '0' as #mhd_MD5_ossl_deinit() is a real function.
    162  */
    163 #define mhd_MD5_OSSL_DEINIT_NOOP_FLAG 0
    164 
    165 
    166 #endif /* MHD_MD5_OPENSSL_H */