libmicrohttpd2

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

sha512_256_mbedtls.h (5898B)


      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_mbedtls.h
     40  * @brief  Wrapper declarations for SHA-512/256 calculation performed by
     41  *         mbedTLS backend
     42  * @author Karlson2k (Evgeny Grin)
     43  */
     44 #ifndef MHD_SHA512_256_MBEDTLS_H
     45 #define MHD_SHA512_256_MBEDTLS_H 1
     46 
     47 #include "mhd_sys_options.h"
     48 
     49 #include "sys_bool_type.h"
     50 #include "sys_base_types.h"
     51 /* MbedTLS header is small in size and scope, it is directly included here */
     52 #include <mbedtls/sha512.h>
     53 
     54 #ifndef mhd_SHA512_256_DIGEST_SIZE
     55 /**
     56  * Size of SHA-512/256 resulting digest in bytes
     57  * This is the final digest size, not the intermediate hash.
     58  */
     59 #  define mhd_SHA512_256_DIGEST_SIZE (32)
     60 #endif
     61 
     62 /**
     63  * SHA-512/256 calculation context
     64  */
     65 struct mhd_Sha512_256CtxMtls
     66 {
     67   struct mbedtls_sha512_context mbed_ctx; /**< Hash calculation handle */
     68   bool ext_error; /**< 'true' if the hashing backend has reported an error */
     69 };
     70 
     71 /**
     72  * Indicates whether struct mhd_Sha512_256CtxMtls has an 'ext_error' member.
     73  *
     74  * Set to '1' when the structure has an 'ext_error' member.
     75  */
     76 #define mhd_SHA512_256_MTLS_EXT_ERROR_FLAG 1
     77 
     78 /**
     79  * Initialise the context for SHA-512/256 calculation.
     80  *
     81  * This function must not be called more than once for @a ctx without an
     82  * intervening #mhd_SHA512_256_mtls_deinit().
     83  *
     84  * Whether or not an error is reported, #mhd_SHA512_256_mtls_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_SHA512_256_mtls_init (struct mhd_Sha512_256CtxMtls *ctx)
     94 MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_OUT_ (1);
     95 
     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_mtls_update (struct mhd_Sha512_256CtxMtls *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_mtls_finish (
    124   struct mhd_Sha512_256CtxMtls *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_mtls_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_mtls_reset (struct mhd_Sha512_256CtxMtls *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_mtls_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_mtls_deinit (struct mhd_Sha512_256CtxMtls *ctx)
    157 MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1);
    158 
    159 /**
    160  * Indicates whether #mhd_SHA512_256_mtls_deinit() is a real function or a
    161  * no-op.
    162  *
    163  * Set to '0' as #mhd_SHA512_256_mtls_deinit() is a real function.
    164  */
    165 #define mhd_SHA512_256_MTLS_DEINIT_NOOP_FLAG 0
    166 
    167 
    168 #endif /* MHD_SHA512_256_MBEDTLS_H */