diff options
Diffstat (limited to 'src/microhttpd/sha256_ext.c')
-rw-r--r-- | src/microhttpd/sha256_ext.c | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/microhttpd/sha256_ext.c b/src/microhttpd/sha256_ext.c new file mode 100644 index 00000000..d1c2a6ed --- /dev/null +++ b/src/microhttpd/sha256_ext.c | |||
@@ -0,0 +1,95 @@ | |||
1 | /* | ||
2 | This file is part of GNU libmicrohttpd | ||
3 | Copyright (C) 2022 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_free (ctx->handle); | ||
46 | ctx->handle = NULL; | ||
47 | } | ||
48 | else | ||
49 | mhd_assert (NULL != ctx->handle); | ||
50 | } | ||
51 | |||
52 | |||
53 | /** | ||
54 | * Process portion of bytes. | ||
55 | * | ||
56 | * @param ctx the calculation context | ||
57 | * @param data bytes to add to hash | ||
58 | * @param length number of bytes in @a data | ||
59 | */ | ||
60 | void | ||
61 | MHD_SHA256_update (struct Sha256CtxExt *ctx, | ||
62 | const uint8_t *data, | ||
63 | size_t length) | ||
64 | { | ||
65 | if (0 == ctx->ext_error) | ||
66 | ctx->ext_error = gnutls_hash (ctx->handle, data, length); | ||
67 | } | ||
68 | |||
69 | |||
70 | /** | ||
71 | * Finalise SHA-256 calculation, return digest, reset hash calculation. | ||
72 | * | ||
73 | * @param ctx the calculation context | ||
74 | * @param[out] digest set to the hash, must be #SHA256_DIGEST_SIZE bytes | ||
75 | */ | ||
76 | void | ||
77 | MHD_SHA256_finish_reset (struct Sha256CtxExt *ctx, | ||
78 | uint8_t digest[SHA256_DIGEST_SIZE]) | ||
79 | { | ||
80 | if (0 == ctx->ext_error) | ||
81 | gnutls_hash_output (ctx->handle, digest); | ||
82 | } | ||
83 | |||
84 | |||
85 | /** | ||
86 | * Free allocated resources. | ||
87 | * | ||
88 | * @param ctx the calculation context | ||
89 | */ | ||
90 | void | ||
91 | MHD_SHA256_deinit (struct Sha256CtxExt *ctx) | ||
92 | { | ||
93 | if (NULL != ctx->handle) | ||
94 | gnutls_hash_deinit (ctx->handle, NULL); | ||
95 | } | ||