libmicrohttpd2

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

sha512_256_ext_mbedtls.c (6143B)


      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) 2025 Christian Grothoff
      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 microhttpd/sha512_256_ext_mbedtls.c
     41  * @brief  Wrapper for SHA-512/256 calculation performed by mbedTLS library
     42  * @author Christian Grothoff
     43  */
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #define MBEDTLS_ALLOW_PRIVATE_ACCESS 1
     47 #include <mbedtls/build_info.h>
     48 #include <mbedtls/sha512.h>
     49 #define MHD_SHA512_256_Context mbedtls_sha512_context
     50 #include "sha512_256_ext.h"
     51 #include "mhd_assert.h"
     52 
     53 
     54 /**
     55  * Initialize mbedtls context for SHA-512/256.
     56  * Since SHA-512/256 is not natively supported by
     57  * mbedTLS, we initialize for SHA-512 and then
     58  * override the state with the SHA-512/256 IV.
     59  *
     60  * @param[in,out] ctx hash context to initialize
     61  */
     62 static void
     63 init512_256 (struct mhd_Sha512_256CtxExt *ctx)
     64 {
     65   static const uint64_t iv_sha512_256[8] = {
     66     0x22312194FC2BF72CULL, 0x9F555FA3C84C64C2ULL,
     67     0x2393B86B6F53B151ULL, 0x963877195940EABDULL,
     68     0x96283EE2A88EFFE3ULL, 0xBE5E1E2553863992ULL,
     69     0x2B0199FC2C85B8AAULL, 0x0EB72DDC81C52CA2ULL
     70   };
     71 
     72   mbedtls_sha512_init (ctx->handle);
     73   /* is384=0 for SHA-512 */
     74 #if MBEDTLS_VERSION_MAJOR >= 4
     75   ctx->ext_error = mbedtls_sha512_starts_ret (ctx->handle,
     76                                               0);
     77   if (0 != ctx->ext_error)
     78   {
     79     mbedtls_sha512_free (ctx->handle);
     80     free (ctx->handle);
     81     ctx->handle = NULL;
     82     return;
     83   }
     84   mhd_assert (sizeof (ctx->handle.state) ==
     85               sizeof (iv_sha512_256));
     86   memcpy (ctx->handle.state,
     87           iv_sha512_256,
     88           sizeof (iv_sha512_256));
     89 #else
     90   mbedtls_sha512_starts (ctx->handle,
     91                          0);
     92   mhd_assert (sizeof (ctx->handle->state) ==
     93               sizeof (iv_sha512_256));
     94   memcpy (ctx->handle->state,
     95           iv_sha512_256,
     96           sizeof (iv_sha512_256));
     97 #endif
     98 }
     99 
    100 
    101 /**
    102  * Initialise structure for SHA-512/256 calculation, allocate resources.
    103  *
    104  * This function must not be called more than one time for @a ctx.
    105  *
    106  * @param ctx the calculation context
    107  */
    108 void
    109 mhd_SHA512_256_init_one_time (struct mhd_Sha512_256CtxExt *ctx)
    110 {
    111   ctx->ext_error = 0;
    112   ctx->handle = (mbedtls_sha512_context *) malloc (
    113     sizeof (mbedtls_sha512_context));
    114   if (NULL == ctx->handle)
    115   {
    116     ctx->ext_error = 1; /* Allocation failure */
    117     return;
    118   }
    119 
    120   init512_256 (ctx);
    121 
    122   /* If handle is NULL, the error must be set */
    123   mhd_assert ((NULL != ctx->handle) || (0 != ctx->ext_error));
    124   /* If error is set, the handle must be NULL */
    125   mhd_assert ((0 == ctx->ext_error) || (NULL == ctx->handle));
    126 }
    127 
    128 
    129 /**
    130  * Process portion of bytes.
    131  *
    132  * @param ctx the calculation context
    133  * @param data bytes to add to hash
    134  * @param length number of bytes in @a data
    135  */
    136 void
    137 mhd_SHA512_256_update (struct mhd_Sha512_256CtxExt *ctx,
    138                        size_t size,
    139                        const uint8_t *data)
    140 {
    141   mhd_assert (0 != size);
    142 #if MBEDTLS_VERSION_MAJOR >= 4
    143   if (0 == ctx->ext_error)
    144     ctx->ext_error = mbedtls_sha512_update_ret (ctx->handle,
    145                                                 data,
    146                                                 size);
    147 #else
    148   mbedtls_sha512_update (ctx->handle,
    149                          data,
    150                          size);
    151 #endif
    152 }
    153 
    154 
    155 /**
    156  * Finalise SHA-512/256 calculation, return digest, reset hash calculation.
    157  *
    158  * @param ctx the calculation context
    159  * @param[out] digest set to the hash, must be #mhd_SHA512_256_DIGEST_SIZE bytes
    160  */
    161 void
    162 mhd_SHA512_256_finish_reset (struct mhd_Sha512_256CtxExt *ctx,
    163                              uint8_t digest[mhd_SHA512_256_DIGEST_SIZE])
    164 {
    165   uint8_t full_digest[64]; /* SHA-512 produces 64 bytes */
    166 
    167   if (0 == ctx->ext_error)
    168   {
    169 #if MBEDTLS_VERSION_MAJOR >= 4
    170     ctx->ext_error = mbedtls_sha512_finish_ret (ctx->handle,
    171                                                 full_digest);
    172 #else
    173     mbedtls_sha512_finish (ctx->handle,
    174                            full_digest);
    175 #endif
    176     if (0 == ctx->ext_error)
    177     {
    178       /* SHA-512/256 uses first 32 bytes of SHA-512 with different IV */
    179       memcpy (digest,
    180               full_digest,
    181               mhd_SHA512_256_DIGEST_SIZE);
    182 
    183       /* Reset for potential reuse */
    184       init512_256 (ctx);
    185     }
    186   }
    187 }
    188 
    189 
    190 /**
    191  * Free allocated resources.
    192  *
    193  * @param ctx the calculation context
    194  */
    195 void
    196 mhd_SHA512_256_deinit (struct mhd_Sha512_256CtxExt *ctx)
    197 {
    198   if (NULL != ctx->handle)
    199   {
    200     mbedtls_sha512_free (ctx->handle);
    201     free (ctx->handle);
    202   }
    203 }