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