sha256_mbedtls.h (5744B)
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/sha256_mbedtls.h 40 * @brief Wrapper declarations for SHA-256 calculation performed by 41 * mbedTLS backend 42 * @author Karlson2k (Evgeny Grin) 43 */ 44 #ifndef MHD_SHA256_MBEDTLS_H 45 #define MHD_SHA256_MBEDTLS_H 1 46 47 #include "mhd_sys_options.h" 48 49 #include "sys_bool_type.h" 50 #include "sys_base_types.h" 51 52 /* MbedTLS header is small in size and scope, it is directly included here */ 53 #include <mbedtls/sha256.h> 54 55 #ifndef mhd_SHA256_DIGEST_SIZE 56 /** 57 * Size of SHA-256 resulting digest in bytes 58 * This is the final digest size, not the intermediate hash. 59 */ 60 # define mhd_SHA256_DIGEST_SIZE (32) 61 #endif 62 63 /** 64 * SHA-256 calculation context 65 */ 66 struct mhd_Sha256CtxMtls 67 { 68 struct mbedtls_sha256_context mbed_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_Sha256CtxMtls has an 'ext_error' member. 74 * 75 * Set to '1' when the structure has an 'ext_error' member. 76 */ 77 #define mhd_SHA256_MTLS_EXT_ERROR_FLAG 1 78 79 /** 80 * Initialise the context for SHA-256 calculation. 81 * 82 * This function must not be called more than once for @a ctx without an 83 * intervening #mhd_SHA256_mtls_deinit(). 84 * 85 * Whether or not an error is reported, #mhd_SHA256_mtls_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_SHA256_mtls_init (struct mhd_Sha256CtxMtls *ctx) 95 MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_OUT_ (1); 96 97 98 /** 99 * Add a portion of data to the SHA-256 calculation. 100 * 101 * If @a ctx already has a recorded error, this function does nothing. 102 * Otherwise, if the operation fails, the error is recorded in @a ctx. 103 * 104 * @param ctx the calculation context 105 * @param size number of bytes in @a data, must not be 0 106 * @param data bytes to add to hash 107 */ 108 MHD_INTERNAL void 109 mhd_SHA256_mtls_update (struct mhd_Sha256CtxMtls *restrict ctx, 110 size_t size, 111 const void *restrict data) 112 MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) MHD_FN_PAR_IN_SIZE_ (3, 2); 113 114 /** 115 * Finalise the SHA-256 calculation and return the digest. 116 * 117 * If @a ctx already has a recorded error, this function does nothing. 118 * Otherwise, if the operation fails, the error is recorded in @a ctx. 119 * 120 * @param ctx the calculation context 121 * @param[out] digest set to the hash, must be #mhd_SHA256_DIGEST_SIZE bytes 122 */ 123 MHD_INTERNAL void 124 mhd_SHA256_mtls_finish ( 125 struct mhd_Sha256CtxMtls *restrict ctx, 126 uint8_t digest[MHD_FN_PAR_FIX_ARR_SIZE_ (mhd_SHA256_DIGEST_SIZE)]) 127 MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) MHD_FN_PAR_OUT_ (2); 128 129 /** 130 * Reset the context for a new SHA-256 calculation. 131 * 132 * This function may be called only after #mhd_SHA256_mtls_finish(). If the 133 * context is subsequently reused for a new calculation, this function must be 134 * called before starting that calculation. 135 * 136 * If @a ctx already has a recorded error, this function does nothing and the 137 * error is preserved. Otherwise, if the operation fails, the error is recorded 138 * in @a ctx. 139 * 140 * @param ctx the calculation context 141 */ 142 MHD_INTERNAL void 143 mhd_SHA256_mtls_reset (struct mhd_Sha256CtxMtls *ctx) 144 MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1); 145 146 /** 147 * Deinitialise the SHA-256 calculation context. 148 * 149 * After calling this function, @a ctx can be reinitialised with 150 * #mhd_SHA256_mtls_init(). 151 * 152 * The recorded error state in @a ctx is preserved. 153 * 154 * @param ctx the calculation context 155 */ 156 MHD_INTERNAL void 157 mhd_SHA256_mtls_deinit (struct mhd_Sha256CtxMtls *ctx) 158 MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1); 159 160 /** 161 * Indicates whether #mhd_SHA256_mtls_deinit() is a real function or a no-op. 162 * 163 * Set to '0' as #mhd_SHA256_mtls_deinit() is a real function. 164 */ 165 #define mhd_SHA256_MTLS_DEINIT_NOOP_FLAG 0 166 167 168 #endif /* MHD_SHA256_MBEDTLS_H */