GNU libmicrohttpd  1.0.1
sha256_ext.c
Go to the documentation of this file.
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 
26 #include <gnutls/crypto.h>
27 #include "sha256_ext.h"
28 #include "mhd_assert.h"
29 
30 
38 void
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 
66 void
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 
82 void
84  uint8_t digest[SHA256_DIGEST_SIZE])
85 {
86  if (0 == ctx->ext_error)
87  gnutls_hash_output (ctx->handle, digest);
88 }
89 
90 
96 void
98 {
99  if (NULL != ctx->handle)
100  gnutls_hash_deinit (ctx->handle, NULL);
101 }
#define mhd_assert(CHK)
Definition: mhd_assert.h:39
#define NULL
Definition: reason_phrase.c:30
macros for mhd_assert()
void * data
Definition: microhttpd.h:3968
#define SHA256_DIGEST_SIZE
Definition: sha256.h:55
void MHD_SHA256_deinit(struct Sha256CtxExt *ctx)
Definition: sha256_ext.c:97
void MHD_SHA256_finish_reset(struct Sha256CtxExt *ctx, uint8_t digest[SHA256_DIGEST_SIZE])
Definition: sha256_ext.c:83
void MHD_SHA256_update(struct Sha256CtxExt *ctx, const uint8_t *data, size_t length)
Definition: sha256_ext.c:67
void MHD_SHA256_init_one_time(struct Sha256CtxExt *ctx)
Definition: sha256_ext.c:39
Wrapper for SHA-256 calculation performed by TLS library.
struct hash_hd_st * handle
Definition: sha256_ext.h:54