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