libmicrohttpd2

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

sha256_gnutls.h (5728B)


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