libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

sha256_ext.c (2889B)


      1 /*
      2      This file is part of GNU libmicrohttpd
      3      Copyright (C) 2022-2023 Evgeny Grin (Karlson2k)
      4 
      5      GNU libmicrohttpd is free software; you can redistribute it and/or
      6      modify it under the terms of the GNU Lesser General Public
      7      License as published by the Free Software Foundation; either
      8      version 2.1 of the License, or (at your option) any later version.
      9 
     10      This library is distributed in the hope that it will be useful,
     11      but WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      Lesser General Public License for more details.
     14 
     15      You should have received a copy of the GNU Lesser General Public
     16      License along with GNU libmicrohttpd.
     17      If not, see <http://www.gnu.org/licenses/>.
     18 */
     19 
     20 /**
     21  * @file microhttpd/sha256_ext.h
     22  * @brief  Wrapper for SHA-256 calculation performed by TLS library
     23  * @author Karlson2k (Evgeny Grin)
     24  */
     25 
     26 #include <gnutls/crypto.h>
     27 #include "sha256_ext.h"
     28 #include "mhd_assert.h"
     29 
     30 
     31 /**
     32  * Initialise structure for SHA-256 calculation, allocate resources.
     33  *
     34  * This function must not be called more than one time for @a ctx.
     35  *
     36  * @param ctx the calculation context
     37  */
     38 void
     39 MHD_SHA256_init_one_time (struct Sha256CtxExt *ctx)
     40 {
     41   ctx->handle = NULL;
     42   ctx->ext_error = gnutls_hash_init (&ctx->handle, GNUTLS_DIG_SHA256);
     43   if ((0 != ctx->ext_error) && (NULL != ctx->handle))
     44   {
     45     /* GnuTLS may return initialisation error and set the handle at the
     46        same time. Such handle cannot be used for calculations.
     47        Note: GnuTLS may also return an error and NOT set the handle. */
     48     gnutls_free (ctx->handle);
     49     ctx->handle = NULL;
     50   }
     51 
     52   /* If handle is NULL, the error must be set */
     53   mhd_assert ((NULL != ctx->handle) || (0 != ctx->ext_error));
     54   /* If error is set, the handle must be NULL */
     55   mhd_assert ((0 == ctx->ext_error) || (NULL == ctx->handle));
     56 }
     57 
     58 
     59 /**
     60  * Process portion of bytes.
     61  *
     62  * @param ctx the calculation context
     63  * @param data bytes to add to hash
     64  * @param length number of bytes in @a data
     65  */
     66 void
     67 MHD_SHA256_update (struct Sha256CtxExt *ctx,
     68                    const uint8_t *data,
     69                    size_t length)
     70 {
     71   if (0 == ctx->ext_error)
     72     ctx->ext_error = gnutls_hash (ctx->handle, data, length);
     73 }
     74 
     75 
     76 /**
     77  * Finalise SHA-256 calculation, return digest, reset hash calculation.
     78  *
     79  * @param ctx the calculation context
     80  * @param[out] digest set to the hash, must be #SHA256_DIGEST_SIZE bytes
     81  */
     82 void
     83 MHD_SHA256_finish_reset (struct Sha256CtxExt *ctx,
     84                          uint8_t digest[SHA256_DIGEST_SIZE])
     85 {
     86   if (0 == ctx->ext_error)
     87     gnutls_hash_output (ctx->handle, digest);
     88 }
     89 
     90 
     91 /**
     92  * Free allocated resources.
     93  *
     94  * @param ctx the calculation context
     95  */
     96 void
     97 MHD_SHA256_deinit (struct Sha256CtxExt *ctx)
     98 {
     99   if (NULL != ctx->handle)
    100     gnutls_hash_deinit (ctx->handle, NULL);
    101 }