mhd_sha512_256.h (9014B)
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_sha512_256.h 41 * @brief Simple wrapper for selecting the SHA-512/256 implementation backend 42 * 43 * Usage: declare a context as 'struct mhd_Sha512_256Ctx', then call 44 * mhd_SHA512_256_init(), mhd_SHA512_256_update() (any number of times) and 45 * mhd_SHA512_256_finish() to get the digest. Reuse the context with 46 * mhd_SHA512_256_reset() or release it with mhd_SHA512_256_deinit(). With some 47 * backends an operation can fail; check with mhd_SHA512_256_has_err(). 48 * 49 * @author Karlson2k (Evgeny Grin) 50 */ 51 52 #ifndef MHD_SHA512_256_H 53 #define MHD_SHA512_256_H 1 54 55 #include "mhd_sys_options.h" 56 57 #ifndef MHD_SUPPORT_SHA512_256 58 # error This file must be used only when SHA-512/256 is enabled 59 #endif 60 61 #include "mhd_macro_concat.h" 62 63 #if defined(MHD_SHA512_256_OPENSSL) 64 # include "sha512_256_openssl.h" 65 66 /** 67 * The hashing backend identifier for function names 68 */ 69 # define mhd_SHA512_256_FUNC_NAME_ID ossl 70 /** 71 * The hashing backend identifier for data names 72 */ 73 # define mhd_SHA512_256_DATA_NAME_ID Ossl 74 /** 75 * The hashing backend identifier for macro names 76 */ 77 # define mhd_SHA512_256_MACRO_NAME_ID OSSL 78 79 #elif defined(MHD_SHA512_X_MBEDTLS) 80 # include "sha512_256_mbedtls.h" 81 82 /** 83 * The hashing backend identifier for function names 84 */ 85 # define mhd_SHA512_256_FUNC_NAME_ID mtls 86 /** 87 * The hashing backend identifier for data names 88 */ 89 # define mhd_SHA512_256_DATA_NAME_ID Mtls 90 /** 91 * The hashing backend identifier for macro names 92 */ 93 # define mhd_SHA512_256_MACRO_NAME_ID MTLS 94 #elif defined(MHD_SHA512_256_BUILTIN) 95 # include "sha512_256_builtin.h" 96 97 /** 98 * The hashing backend identifier for function names 99 */ 100 # define mhd_SHA512_256_FUNC_NAME_ID blti 101 /** 102 * The hashing backend identifier for data names 103 */ 104 # define mhd_SHA512_256_DATA_NAME_ID Blti 105 /** 106 * The hashing backend identifier for macro names 107 */ 108 # define mhd_SHA512_256_MACRO_NAME_ID BLTI 109 #else 110 # error No SHA-512/256 implementation enabled 111 #endif 112 113 #if mhd_SHA512_256_DIGEST_SIZE != 32 114 # error Wrong value of mhd_SHA512_256_DIGEST_SIZE 115 #endif 116 117 /** 118 * Form the name of the function specific to the selected hashing backend 119 */ 120 #define mhd_SHA512_256_FUNC(name_suffix) \ 121 mhd_MACRO_CONCAT3 (mhd_SHA512_256_,mhd_SHA512_256_FUNC_NAME_ID, \ 122 name_suffix) 123 124 /** 125 * Form the name of the macro specific to the selected hashing backend 126 */ 127 #define mhd_SHA512_256_MACRO(name_suffix) \ 128 mhd_MACRO_CONCAT3 (mhd_SHA512_256_,mhd_SHA512_256_MACRO_NAME_ID, \ 129 name_suffix) 130 131 /** 132 * Struct tag of the calculation context for the selected SHA-512/256 backend 133 */ 134 #define mhd_Sha512_256Ctx \ 135 mhd_MACRO_CONCAT (mhd_Sha512_256Ctx,mhd_SHA512_256_DATA_NAME_ID) 136 137 138 #if 0 != mhd_SHA512_256_MACRO (_EXT_ERROR_FLAG) 139 /** 140 * Indicates that SHA-512/256 hashing or initialisation may result in an error 141 */ 142 # define mhd_SHA512_256_HAS_EXT_ERROR 1 143 /** 144 * 'true' if the hashing backend has reported an error 145 */ 146 # define mhd_SHA512_256_has_err(ctx) (!!((ctx)->ext_error)) 147 /** 148 * Get the error number reported by hashing backend (zero if no error) 149 */ 150 # define mhd_SHA512_256_get_err(ctx) ((int)((ctx)->ext_error)) 151 #else 152 # define mhd_SHA512_256_has_err(ctx) (((void) (ctx)), !!0) 153 /** 154 * Get the error number (zero if no error) 155 */ 156 # define mhd_SHA512_256_get_err(ctx) ((int)(0)) 157 #endif 158 159 /** 160 * Initialise the context for SHA-512/256 calculation. 161 * 162 * This function must not be called more than once for @a ctx without an 163 * intervening #mhd_SHA512_256_deinit(). 164 * 165 * Whether or not an error is reported, #mhd_SHA512_256_deinit() must be 166 * called for @a ctx once this function has returned. 167 * 168 * For backends that track errors (see #mhd_SHA512_256_has_err()), the error 169 * state is set according to the result: cleared on success, set on failure. 170 * 171 * @param ctx the calculation context 172 */ 173 #define mhd_SHA512_256_init(ctx) mhd_SHA512_256_FUNC(_init) ((ctx)) 174 175 /** 176 * Add a portion of data to the SHA-512/256 calculation. 177 * 178 * For backends that track errors (see #mhd_SHA512_256_has_err()), this does 179 * nothing if @a ctx already has a recorded error, and records an error on 180 * failure. 181 * 182 * @param ctx the calculation context 183 * @param size number of bytes in @a data, must not be 0 184 * @param data bytes to add to hash 185 */ 186 #define mhd_SHA512_256_update(ctx, size, data) \ 187 mhd_SHA512_256_FUNC(_update) ((ctx),(size),(data)) 188 189 /** 190 * Finalise the SHA-512/256 calculation and return the digest. 191 * 192 * For backends that track errors (see #mhd_SHA512_256_has_err()), this does 193 * nothing if @a ctx already has a recorded error, and may record an error on 194 * failure. 195 * 196 * @param ctx the calculation context 197 * @param[out] digest set to the hash, must be #mhd_SHA512_256_DIGEST_SIZE bytes 198 */ 199 #define mhd_SHA512_256_finish(ctx, digest) \ 200 mhd_SHA512_256_FUNC(_finish) ((ctx),(digest)) 201 202 /** 203 * Reset the context for a new SHA-512/256 calculation. 204 * 205 * This function may be called only after #mhd_SHA512_256_finish(). If the 206 * context is subsequently reused for a new calculation, this function must be 207 * called before starting that calculation. 208 * 209 * For backends that track errors (see #mhd_SHA512_256_has_err()), this does 210 * nothing if @a ctx already has a recorded error, and may record an error on 211 * failure. 212 * 213 * @param ctx the calculation context 214 */ 215 #define mhd_SHA512_256_reset(ctx) mhd_SHA512_256_FUNC(_reset) ((ctx)) 216 217 /** 218 * Finalise the SHA-512/256 calculation, return the digest and reset the 219 * context. 220 * 221 * For backends that track errors (see #mhd_SHA512_256_has_err()): if @a ctx 222 * already has a recorded error, this does nothing and the error is preserved. 223 * Otherwise, an error is recorded in @a ctx if either the finalisation or the 224 * reset fails. A recorded error is never cleared by this macro. 225 * 226 * @param ctx the calculation context 227 * @param[out] digest set to the hash, must be #mhd_SHA512_256_DIGEST_SIZE bytes 228 */ 229 #define mhd_SHA512_256_finish_reset(ctx, digest) \ 230 (mhd_SHA512_256_finish((ctx),(digest)), mhd_SHA512_256_reset((ctx))) 231 232 /** 233 * Deinitialise the SHA-512/256 calculation context. 234 * 235 * After calling this function, @a ctx can be reinitialised with 236 * #mhd_SHA512_256_init(). 237 * 238 * For backends that track errors (see #mhd_SHA512_256_has_err()), the recorded 239 * error state is preserved. 240 * 241 * @param ctx the calculation context 242 */ 243 #define mhd_SHA512_256_deinit(ctx) mhd_SHA512_256_FUNC(_deinit) ((ctx)) 244 245 /** 246 * Finalise the SHA-512/256 calculation, return the digest and deinitialise 247 * the context. 248 * 249 * For backends that track errors (see #mhd_SHA512_256_has_err()): if @a ctx 250 * already has a recorded error, the error is preserved. Otherwise, an error is 251 * recorded in @a ctx if the finalisation fails. 252 * 253 * @param ctx the calculation context 254 * @param[out] digest set to the hash, must be #mhd_SHA512_256_DIGEST_SIZE bytes 255 */ 256 #define mhd_SHA512_256_finish_deinit(ctx, digest) \ 257 (mhd_SHA512_256_finish((ctx),(digest)), mhd_SHA512_256_deinit((ctx))) 258 259 #if 0 == mhd_SHA512_256_MACRO (_DEINIT_NOOP_FLAG) 260 /** 261 * Indicates that #mhd_SHA512_256_deinit() is a real function that must be 262 * called. 263 */ 264 # define mhd_SHA512_256_HAS_DEINIT 265 #endif 266 267 268 #endif /* MHD_SHA512_256_H */