libmicrohttpd2

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

mhd_sha256.h (8932B)


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