[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[libmicrohttpd] 06/06: Implemented SHA-1 calculation
From: |
gnunet |
Subject: |
[libmicrohttpd] 06/06: Implemented SHA-1 calculation |
Date: |
Sun, 16 May 2021 19:04:37 +0200 |
This is an automated email from the git hooks/post-receive script.
karlson2k pushed a commit to branch master
in repository libmicrohttpd.
commit 714c5735e4a1ffce8f3124835dc4bb59a8735c0b
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
AuthorDate: Sun May 16 18:20:10 2021 +0300
Implemented SHA-1 calculation
---
src/microhttpd/Makefile.am | 5 +
src/microhttpd/mhd_bithelpers.h | 27 +++
src/microhttpd/sha1.c | 353 ++++++++++++++++++++++++++++++++++
src/microhttpd/sha1.h | 98 ++++++++++
src/microhttpd/test_sha1.c | 417 ++++++++++++++++++++++++++++++++++++++++
5 files changed, 900 insertions(+)
diff --git a/src/microhttpd/Makefile.am b/src/microhttpd/Makefile.am
index 1e252d5a..84fe4f81 100644
--- a/src/microhttpd/Makefile.am
+++ b/src/microhttpd/Makefile.am
@@ -160,6 +160,7 @@ check_PROGRAMS = \
test_str_token \
test_http_reasons \
test_md5 \
+ test_sha1 \
test_sha256 \
test_start_stop \
test_daemon \
@@ -364,6 +365,10 @@ test_sha256_SOURCES = \
test_sha256.c test_helpers.h \
sha256.c sha256.h mhd_bithelpers.h
+test_sha1_SOURCES = \
+ test_sha1.c test_helpers.h \
+ sha1.c sha1.h mhd_bithelpers.h
+
test_options_SOURCES = \
test_options.c
test_options_LDADD = \
diff --git a/src/microhttpd/mhd_bithelpers.h b/src/microhttpd/mhd_bithelpers.h
index ea7682aa..c8c814ac 100644
--- a/src/microhttpd/mhd_bithelpers.h
+++ b/src/microhttpd/mhd_bithelpers.h
@@ -238,6 +238,33 @@ _MHD_ROTR32 (uint32_t value32, int bits)
/* Defined in form which modern compiler could optimize. */
return (value32 >> bits) | (value32 << (32 - bits));
}
+
+
+#endif /* ! _MSC_FULL_VER */
+
+
+/**
+ * Rotate left 32-bit value by number of bits.
+ * bits parameter must be more than zero and must be less than 32.
+ */
+#if defined(_MSC_FULL_VER) && (! defined(__clang__) || (defined(__c2__) && \
+ defined(__OPTIMIZE__)))
+/* Clang/C2 do not inline this function if optimizations are turned off. */
+#ifndef __clang__
+#pragma intrinsic(_rotl)
+#endif /* ! __clang__ */
+#define _MHD_ROTL32(value32, bits) \
+ ((uint32_t) _rotl ((uint32_t) (value32),(bits)))
+#else /* ! _MSC_FULL_VER */
+_MHD_static_inline uint32_t
+_MHD_ROTL32 (uint32_t value32, int bits)
+{
+ mhd_assert (bits < 32);
+ /* Defined in form which modern compiler could optimize. */
+ return (value32 << bits) | (value32 >> (32 - bits));
+}
+
+
#endif /* ! _MSC_FULL_VER */
diff --git a/src/microhttpd/sha1.c b/src/microhttpd/sha1.c
new file mode 100644
index 00000000..51deabaa
--- /dev/null
+++ b/src/microhttpd/sha1.c
@@ -0,0 +1,353 @@
+/*
+ This file is part of libmicrohttpd
+ Copyright (C) 2019-2021 Karlson2k (Evgeny Grin)
+
+ libmicrohttpd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library.
+ If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file microhttpd/sha1.c
+ * @brief Calculation of SHA-1 digest as defined in FIPS PUB 180-4 (2015)
+ * @author Karlson2k (Evgeny Grin)
+ */
+
+#include "sha1.h"
+
+#include <string.h>
+#ifdef HAVE_MEMORY_H
+#include <memory.h>
+#endif /* HAVE_MEMORY_H */
+#include "mhd_bithelpers.h"
+#include "mhd_assert.h"
+
+/**
+ * Initialise structure for SHA-1 calculation.
+ *
+ * @param ctx_ must be a `struct sha1_ctx *`
+ */
+void
+MHD_SHA1_init (void *ctx_)
+{
+ struct sha1_ctx *const ctx = ctx_;
+ /* Initial hash values, see FIPS PUB 180-4 paragraph 5.3.1 */
+ /* Just some "magic" numbers defined by standard */
+ ctx->H[0] = 0x67452301UL;
+ ctx->H[1] = 0xefcdab89UL;
+ ctx->H[2] = 0x98badcfeUL;
+ ctx->H[3] = 0x10325476UL;
+ ctx->H[4] = 0xc3d2e1f0UL;
+
+ /* Initialise number of bytes. */
+ ctx->count = 0;
+}
+
+
+/**
+ * Number of bytes in single SHA-1 word
+ */
+#define SHA1_BYTES_IN_WORD (32 / 8)
+
+/**
+ * Base of SHA-1 transformation.
+ * Gets full 512 bits / 64 bytes block of data and updates hash values;
+ * @param H hash values
+ * @param data data, must be exactly 64 bytes long
+ */
+static void
+sha1_transform (uint32_t H[_SHA1_DIGEST_LENGTH],
+ const uint8_t data[SHA1_BLOCK_SIZE])
+{
+ /* Working variables,
+ see FIPS PUB 180-4 paragraph 6.1.3 */
+ uint32_t a = H[0];
+ uint32_t b = H[1];
+ uint32_t c = H[2];
+ uint32_t d = H[3];
+ uint32_t e = H[4];
+
+ /* Data buffer, used as cyclic buffer.
+ See FIPS PUB 180-4 paragraphs 5.2.1, 6.1.3 */
+ uint32_t W[16];
+
+ /* 'Ch' and 'Maj' macro functions are defined with
+ widely-used optimization.
+ See FIPS PUB 180-4 formulae 4.1. */
+#define Ch(x,y,z) ( (z) ^ ((x) & ((y) ^ (z))) )
+#define Maj(x,y,z) ( ((x) & (y)) ^ ((z) & ((x) ^ (y))) )
+ /* Unoptimized (original) versions: */
+/* #define Ch(x,y,z) ( ( (x) & (y) ) ^ ( ~(x) & (z) ) ) */
+/* #define Maj(x,y,z) ( ((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)) ) */
+#define Par(x,y,z) ( (x) ^ (y) ^ (z) )
+
+ /* Single step of SHA-1 computation,
+ see FIPS PUB 180-4 paragraph 6.1.3 step 3.
+ * Note: instead of reassigning all working variables on each step,
+ variables are rotated for each step:
+ SHA1STEP32 (a, b, c, d, e, func, K00, W[0]);
+ SHA1STEP32 (e, a, b, c, d, func, K00, W[1]);
+ so current 'vC' will be used as 'vD' on the next step,
+ current 'vE' will be used as 'vA' on the next step.
+ * Note: 'wt' must be used exactly one time in this macro as it change other
data as well
+ every time when used. */
+
+#define SHA1STEP32(vA,vB,vC,vD,vE,ft,kt,wt) do { \
+ (vE) += _MHD_ROTL32 ((vA), 5) + ft ((vB), (vC), (vD)) + (kt) + (wt); \
+ (vB) = _MHD_ROTL32 ((vB), 30); } while (0)
+
+ /* Get value of W(t) from input data buffer,
+ See FIPS PUB 180-4 paragraph 6.1.3.
+ Input data must be read in big-endian bytes order,
+ see FIPS PUB 180-4 paragraph 3.1.2. */
+#define GET_W_FROM_DATA(buf,t) \
+ _MHD_GET_32BIT_BE (((const uint8_t*) (buf)) + (t) * SHA1_BYTES_IN_WORD)
+
+/* SHA-1 values of Kt for t=0..19, see FIPS PUB 180-4 paragraph 4.2.1. */
+#define K00 0x5a827999UL
+/* SHA-1 values of Kt for t=20..39, see FIPS PUB 180-4 paragraph 4.2.1.*/
+#define K20 0x6ed9eba1UL
+/* SHA-1 values of Kt for t=40..59, see FIPS PUB 180-4 paragraph 4.2.1.*/
+#define K40 0x8f1bbcdcUL
+/* SHA-1 values of Kt for t=60..79, see FIPS PUB 180-4 paragraph 4.2.1.*/
+#define K60 0xca62c1d6UL
+
+ /* During first 16 steps, before making any calculations on each step,
+ the W element is read from input data buffer as big-endian value and
+ stored in array of W elements. */
+ /* Note: instead of using K constants as array, all K values are specified
+ individually for each step. */
+ SHA1STEP32 (a, b, c, d, e, Ch, K00, W[0] = GET_W_FROM_DATA (data, 0));
+ SHA1STEP32 (e, a, b, c, d, Ch, K00, W[1] = GET_W_FROM_DATA (data, 1));
+ SHA1STEP32 (d, e, a, b, c, Ch, K00, W[2] = GET_W_FROM_DATA (data, 2));
+ SHA1STEP32 (c, d, e, a, b, Ch, K00, W[3] = GET_W_FROM_DATA (data, 3));
+ SHA1STEP32 (b, c, d, e, a, Ch, K00, W[4] = GET_W_FROM_DATA (data, 4));
+ SHA1STEP32 (a, b, c, d, e, Ch, K00, W[5] = GET_W_FROM_DATA (data, 5));
+ SHA1STEP32 (e, a, b, c, d, Ch, K00, W[6] = GET_W_FROM_DATA (data, 6));
+ SHA1STEP32 (d, e, a, b, c, Ch, K00, W[7] = GET_W_FROM_DATA (data, 7));
+ SHA1STEP32 (c, d, e, a, b, Ch, K00, W[8] = GET_W_FROM_DATA (data, 8));
+ SHA1STEP32 (b, c, d, e, a, Ch, K00, W[9] = GET_W_FROM_DATA (data, 9));
+ SHA1STEP32 (a, b, c, d, e, Ch, K00, W[10] = GET_W_FROM_DATA (data, 10));
+ SHA1STEP32 (e, a, b, c, d, Ch, K00, W[11] = GET_W_FROM_DATA (data, 11));
+ SHA1STEP32 (d, e, a, b, c, Ch, K00, W[12] = GET_W_FROM_DATA (data, 12));
+ SHA1STEP32 (c, d, e, a, b, Ch, K00, W[13] = GET_W_FROM_DATA (data, 13));
+ SHA1STEP32 (b, c, d, e, a, Ch, K00, W[14] = GET_W_FROM_DATA (data, 14));
+ SHA1STEP32 (a, b, c, d, e, Ch, K00, W[15] = GET_W_FROM_DATA (data, 15));
+
+ /* 'W' generation and assignment for 16 <= t <= 79.
+ See FIPS PUB 180-4 paragraph 6.1.3.
+ As only last 16 'W' are used in calculations, it is possible to
+ use 16 elements array of W as cyclic buffer. */
+#define Wgen(w,t) _MHD_ROTL32((w)[(t + 13) & 0xf] ^ (w)[(t + 8) & 0xf] \
+ ^ (w)[(t + 2) & 0xf] ^ (w)[t & 0xf], 1)
+
+ /* During last 60 steps, before making any calculations on each step,
+ W element is generated from W elements of cyclic buffer and generated
value
+ stored back in cyclic buffer. */
+ /* Note: instead of using K constants as array, all K values are specified
+ individually for each step, see FIPS PUB 180-4 paragraph 4.2.1. */
+ SHA1STEP32 (e, a, b, c, d, Ch, K00, W[16 & 0xf] = Wgen (W, 16));
+ SHA1STEP32 (d, e, a, b, c, Ch, K00, W[17 & 0xf] = Wgen (W, 17));
+ SHA1STEP32 (c, d, e, a, b, Ch, K00, W[18 & 0xf] = Wgen (W, 18));
+ SHA1STEP32 (b, c, d, e, a, Ch, K00, W[19 & 0xf] = Wgen (W, 19));
+ SHA1STEP32 (a, b, c, d, e, Par, K20, W[20 & 0xf] = Wgen (W, 20));
+ SHA1STEP32 (e, a, b, c, d, Par, K20, W[21 & 0xf] = Wgen (W, 21));
+ SHA1STEP32 (d, e, a, b, c, Par, K20, W[22 & 0xf] = Wgen (W, 22));
+ SHA1STEP32 (c, d, e, a, b, Par, K20, W[23 & 0xf] = Wgen (W, 23));
+ SHA1STEP32 (b, c, d, e, a, Par, K20, W[24 & 0xf] = Wgen (W, 24));
+ SHA1STEP32 (a, b, c, d, e, Par, K20, W[25 & 0xf] = Wgen (W, 25));
+ SHA1STEP32 (e, a, b, c, d, Par, K20, W[26 & 0xf] = Wgen (W, 26));
+ SHA1STEP32 (d, e, a, b, c, Par, K20, W[27 & 0xf] = Wgen (W, 27));
+ SHA1STEP32 (c, d, e, a, b, Par, K20, W[28 & 0xf] = Wgen (W, 28));
+ SHA1STEP32 (b, c, d, e, a, Par, K20, W[29 & 0xf] = Wgen (W, 29));
+ SHA1STEP32 (a, b, c, d, e, Par, K20, W[30 & 0xf] = Wgen (W, 30));
+ SHA1STEP32 (e, a, b, c, d, Par, K20, W[31 & 0xf] = Wgen (W, 31));
+ SHA1STEP32 (d, e, a, b, c, Par, K20, W[32 & 0xf] = Wgen (W, 32));
+ SHA1STEP32 (c, d, e, a, b, Par, K20, W[33 & 0xf] = Wgen (W, 33));
+ SHA1STEP32 (b, c, d, e, a, Par, K20, W[34 & 0xf] = Wgen (W, 34));
+ SHA1STEP32 (a, b, c, d, e, Par, K20, W[35 & 0xf] = Wgen (W, 35));
+ SHA1STEP32 (e, a, b, c, d, Par, K20, W[36 & 0xf] = Wgen (W, 36));
+ SHA1STEP32 (d, e, a, b, c, Par, K20, W[37 & 0xf] = Wgen (W, 37));
+ SHA1STEP32 (c, d, e, a, b, Par, K20, W[38 & 0xf] = Wgen (W, 38));
+ SHA1STEP32 (b, c, d, e, a, Par, K20, W[39 & 0xf] = Wgen (W, 39));
+ SHA1STEP32 (a, b, c, d, e, Maj, K40, W[40 & 0xf] = Wgen (W, 40));
+ SHA1STEP32 (e, a, b, c, d, Maj, K40, W[41 & 0xf] = Wgen (W, 41));
+ SHA1STEP32 (d, e, a, b, c, Maj, K40, W[42 & 0xf] = Wgen (W, 42));
+ SHA1STEP32 (c, d, e, a, b, Maj, K40, W[43 & 0xf] = Wgen (W, 43));
+ SHA1STEP32 (b, c, d, e, a, Maj, K40, W[44 & 0xf] = Wgen (W, 44));
+ SHA1STEP32 (a, b, c, d, e, Maj, K40, W[45 & 0xf] = Wgen (W, 45));
+ SHA1STEP32 (e, a, b, c, d, Maj, K40, W[46 & 0xf] = Wgen (W, 46));
+ SHA1STEP32 (d, e, a, b, c, Maj, K40, W[47 & 0xf] = Wgen (W, 47));
+ SHA1STEP32 (c, d, e, a, b, Maj, K40, W[48 & 0xf] = Wgen (W, 48));
+ SHA1STEP32 (b, c, d, e, a, Maj, K40, W[49 & 0xf] = Wgen (W, 49));
+ SHA1STEP32 (a, b, c, d, e, Maj, K40, W[50 & 0xf] = Wgen (W, 50));
+ SHA1STEP32 (e, a, b, c, d, Maj, K40, W[51 & 0xf] = Wgen (W, 51));
+ SHA1STEP32 (d, e, a, b, c, Maj, K40, W[52 & 0xf] = Wgen (W, 52));
+ SHA1STEP32 (c, d, e, a, b, Maj, K40, W[53 & 0xf] = Wgen (W, 53));
+ SHA1STEP32 (b, c, d, e, a, Maj, K40, W[54 & 0xf] = Wgen (W, 54));
+ SHA1STEP32 (a, b, c, d, e, Maj, K40, W[55 & 0xf] = Wgen (W, 55));
+ SHA1STEP32 (e, a, b, c, d, Maj, K40, W[56 & 0xf] = Wgen (W, 56));
+ SHA1STEP32 (d, e, a, b, c, Maj, K40, W[57 & 0xf] = Wgen (W, 57));
+ SHA1STEP32 (c, d, e, a, b, Maj, K40, W[58 & 0xf] = Wgen (W, 58));
+ SHA1STEP32 (b, c, d, e, a, Maj, K40, W[59 & 0xf] = Wgen (W, 59));
+ SHA1STEP32 (a, b, c, d, e, Par, K60, W[60 & 0xf] = Wgen (W, 60));
+ SHA1STEP32 (e, a, b, c, d, Par, K60, W[61 & 0xf] = Wgen (W, 61));
+ SHA1STEP32 (d, e, a, b, c, Par, K60, W[62 & 0xf] = Wgen (W, 62));
+ SHA1STEP32 (c, d, e, a, b, Par, K60, W[63 & 0xf] = Wgen (W, 63));
+ SHA1STEP32 (b, c, d, e, a, Par, K60, W[64 & 0xf] = Wgen (W, 64));
+ SHA1STEP32 (a, b, c, d, e, Par, K60, W[65 & 0xf] = Wgen (W, 65));
+ SHA1STEP32 (e, a, b, c, d, Par, K60, W[66 & 0xf] = Wgen (W, 66));
+ SHA1STEP32 (d, e, a, b, c, Par, K60, W[67 & 0xf] = Wgen (W, 67));
+ SHA1STEP32 (c, d, e, a, b, Par, K60, W[68 & 0xf] = Wgen (W, 68));
+ SHA1STEP32 (b, c, d, e, a, Par, K60, W[69 & 0xf] = Wgen (W, 69));
+ SHA1STEP32 (a, b, c, d, e, Par, K60, W[70 & 0xf] = Wgen (W, 70));
+ SHA1STEP32 (e, a, b, c, d, Par, K60, W[71 & 0xf] = Wgen (W, 71));
+ SHA1STEP32 (d, e, a, b, c, Par, K60, W[72 & 0xf] = Wgen (W, 72));
+ SHA1STEP32 (c, d, e, a, b, Par, K60, W[73 & 0xf] = Wgen (W, 73));
+ SHA1STEP32 (b, c, d, e, a, Par, K60, W[74 & 0xf] = Wgen (W, 74));
+ SHA1STEP32 (a, b, c, d, e, Par, K60, W[75 & 0xf] = Wgen (W, 75));
+ SHA1STEP32 (e, a, b, c, d, Par, K60, W[76 & 0xf] = Wgen (W, 76));
+ SHA1STEP32 (d, e, a, b, c, Par, K60, W[77 & 0xf] = Wgen (W, 77));
+ SHA1STEP32 (c, d, e, a, b, Par, K60, W[78 & 0xf] = Wgen (W, 78));
+ SHA1STEP32 (b, c, d, e, a, Par, K60, W[79 & 0xf] = Wgen (W, 79));
+
+ /* Compute intermediate hash.
+ See FIPS PUB 180-4 paragraph 6.1.3 step 4. */
+ H[0] += a;
+ H[1] += b;
+ H[2] += c;
+ H[3] += d;
+ H[4] += e;
+}
+
+
+/**
+ * Process portion of bytes.
+ *
+ * @param ctx_ must be a `struct sha1_ctx *`
+ * @param data bytes to add to hash
+ * @param length number of bytes in @a data
+ */
+void
+MHD_SHA1_update (void *ctx_,
+ const uint8_t *data,
+ size_t length)
+{
+ struct sha1_ctx *const ctx = ctx_;
+ unsigned bytes_have; /**< Number of bytes in buffer */
+
+ mhd_assert ((data != NULL) || (length == 0));
+
+ if (0 == length)
+ return; /* Do nothing */
+
+ /* Note: (count & (SHA1_BLOCK_SIZE-1))
+ equal (count % SHA1_BLOCK_SIZE) for this block size. */
+ bytes_have = (unsigned) (ctx->count & (SHA1_BLOCK_SIZE - 1));
+ ctx->count += length;
+
+ if (0 != bytes_have)
+ {
+ unsigned bytes_left = SHA1_BLOCK_SIZE - bytes_have;
+ if (length >= bytes_left)
+ { /* Combine new data with the data in the buffer and
+ process the full block. */
+ memcpy (ctx->buffer + bytes_have,
+ data,
+ bytes_left);
+ data += bytes_left;
+ length -= bytes_left;
+ sha1_transform (ctx->H, ctx->buffer);
+ bytes_have = 0;
+ }
+ }
+
+ while (SHA1_BLOCK_SIZE <= length)
+ { /* Process any full blocks of new data directly,
+ without copying to the buffer. */
+ sha1_transform (ctx->H, data);
+ data += SHA1_BLOCK_SIZE;
+ length -= SHA1_BLOCK_SIZE;
+ }
+
+ if (0 != length)
+ { /* Copy incomplete block of new data (if any)
+ to the buffer. */
+ memcpy (ctx->buffer + bytes_have, data, length);
+ }
+}
+
+
+/**
+ * Size of "length" padding addition in bytes.
+ * See FIPS PUB 180-4 paragraph 5.1.1.
+ */
+#define SHA1_SIZE_OF_LEN_ADD (64 / 8)
+
+/**
+ * Finalise SHA-1 calculation, return digest.
+ *
+ * @param ctx_ must be a `struct sha1_ctx *`
+ * @param[out] digest set to the hash, must be #SHA1_DIGEST_SIZE bytes
+ */
+void
+MHD_SHA1_finish (void *ctx_,
+ uint8_t digest[SHA1_DIGEST_SIZE])
+{
+ struct sha1_ctx *const ctx = ctx_;
+ uint64_t num_bits; /**< Number of processed bits */
+ unsigned bytes_have; /**< Number of bytes in buffer */
+
+ num_bits = ctx->count << 3;
+ /* Note: (count & (SHA1_BLOCK_SIZE-1))
+ equal (count % SHA1_BLOCK_SIZE) for this block size. */
+ bytes_have = (unsigned) (ctx->count & (SHA1_BLOCK_SIZE - 1));
+
+ /* Input data must be padded with bit "1" and with length of data in bits.
+ See FIPS PUB 180-4 paragraph 5.1.1. */
+ /* Data is always processed in form of bytes (not by individual bits),
+ therefore position of first padding bit in byte is always predefined
(0x80). */
+ /* Buffer always have space at least for one byte (as full buffers are
+ processed immediately). */
+ ctx->buffer[bytes_have++] = 0x80;
+
+ if (SHA1_BLOCK_SIZE - bytes_have < SHA1_SIZE_OF_LEN_ADD)
+ { /* No space in current block to put total length of message.
+ Pad current block with zeros and process it. */
+ if (SHA1_BLOCK_SIZE > bytes_have)
+ memset (ctx->buffer + bytes_have, 0, SHA1_BLOCK_SIZE - bytes_have);
+ /* Process full block. */
+ sha1_transform (ctx->H, ctx->buffer);
+ /* Start new block. */
+ bytes_have = 0;
+ }
+
+ /* Pad the rest of the buffer with zeros. */
+ memset (ctx->buffer + bytes_have, 0,
+ SHA1_BLOCK_SIZE - SHA1_SIZE_OF_LEN_ADD - bytes_have);
+ /* Put the number of bits in the processed message as a big-endian value. */
+ _MHD_PUT_64BIT_BE (ctx->buffer + SHA1_BLOCK_SIZE - SHA1_SIZE_OF_LEN_ADD,
+ num_bits);
+ /* Process the full final block. */
+ sha1_transform (ctx->H, ctx->buffer);
+
+ /* Put final hash/digest in BE mode */
+ _MHD_PUT_32BIT_BE (digest + 0 * SHA1_BYTES_IN_WORD, ctx->H[0]);
+ _MHD_PUT_32BIT_BE (digest + 1 * SHA1_BYTES_IN_WORD, ctx->H[1]);
+ _MHD_PUT_32BIT_BE (digest + 2 * SHA1_BYTES_IN_WORD, ctx->H[2]);
+ _MHD_PUT_32BIT_BE (digest + 3 * SHA1_BYTES_IN_WORD, ctx->H[3]);
+ _MHD_PUT_32BIT_BE (digest + 4 * SHA1_BYTES_IN_WORD, ctx->H[4]);
+
+ /* Erase potentially sensitive data. */
+ memset (ctx, 0, sizeof(struct sha1_ctx));
+}
diff --git a/src/microhttpd/sha1.h b/src/microhttpd/sha1.h
new file mode 100644
index 00000000..800a4909
--- /dev/null
+++ b/src/microhttpd/sha1.h
@@ -0,0 +1,98 @@
+/*
+ This file is part of libmicrohttpd
+ Copyright (C) 2019-2021 Karlson2k (Evgeny Grin)
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library.
+ If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file microhttpd/sha1.h
+ * @brief Calculation of SHA-1 digest
+ * @author Karlson2k (Evgeny Grin)
+ */
+
+#ifndef MHD_SHA1_H
+#define MHD_SHA1_H 1
+
+#include "mhd_options.h"
+#include <stdint.h>
+#include <stddef.h>
+
+/**
+ * SHA-1 digest is kept internally as 5 32-bit words.
+ */
+#define _SHA1_DIGEST_LENGTH 5
+
+/**
+ * Size of SHA-1 digest in bytes
+ */
+#define SHA1_DIGEST_SIZE (_SHA1_DIGEST_LENGTH * 4)
+
+/**
+ * Size of SHA-1 digest string in chars including termination NUL
+ */
+#define SHA1_DIGEST_STRING_SIZE ((SHA1_DIGEST_SIZE) * 2 + 1)
+
+/**
+ * Size of single processing block in bits
+ */
+#define SHA1_BLOCK_SIZE_BITS 512
+
+/**
+ * Size of single processing block in bytes
+ */
+#define SHA1_BLOCK_SIZE (SHA1_BLOCK_SIZE_BITS / 8)
+
+
+struct sha1_ctx
+{
+ uint32_t H[_SHA1_DIGEST_LENGTH]; /**< Intermediate hash value / digest at
end of calculation */
+ uint8_t buffer[SHA1_BLOCK_SIZE]; /**< SHA256 input data buffer */
+ uint64_t count; /**< number of bytes, mod 2^64 */
+};
+
+/**
+ * Initialise structure for SHA-1 calculation.
+ *
+ * @param ctx must be a `struct sha1_ctx *`
+ */
+void
+MHD_SHA1_init (void *ctx_);
+
+
+/**
+ * Process portion of bytes.
+ *
+ * @param ctx_ must be a `struct sha1_ctx *`
+ * @param data bytes to add to hash
+ * @param length number of bytes in @a data
+ */
+void
+MHD_SHA1_update (void *ctx_,
+ const uint8_t *data,
+ size_t length);
+
+
+/**
+ * Finalise SHA-1 calculation, return digest.
+ *
+ * @param ctx_ must be a `struct sha1_ctx *`
+ * @param[out] digest set to the hash, must be #SHA1_DIGEST_SIZE bytes
+ */
+void
+MHD_SHA1_finish (void *ctx_,
+ uint8_t digest[SHA1_DIGEST_SIZE]);
+
+#endif /* MHD_SHA1_H */
diff --git a/src/microhttpd/test_sha1.c b/src/microhttpd/test_sha1.c
new file mode 100644
index 00000000..276c7838
--- /dev/null
+++ b/src/microhttpd/test_sha1.c
@@ -0,0 +1,417 @@
+/*
+ This file is part of libmicrohttpd
+ Copyright (C) 2019-2021 Karlson2k (Evgeny Grin)
+
+ This test tool is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or
+ (at your option) any later version.
+
+ This test tool is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
+*/
+
+/**
+ * @file microhttpd/test_sha1.h
+ * @brief Unit tests for SHA-1 functions
+ * @author Karlson2k (Evgeny Grin)
+ */
+
+#include "mhd_options.h"
+#include "sha1.h"
+#include "test_helpers.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static int verbose = 0; /* verbose level (0-1)*/
+
+
+struct str_with_len
+{
+ const char *const str;
+ const size_t len;
+};
+
+#define D_STR_W_LEN(s) {(s), (sizeof((s)) / sizeof(char)) - 1}
+
+struct data_unit1
+{
+ const struct str_with_len str_l;
+ const uint8_t digest[SHA1_DIGEST_SIZE];
+};
+
+static const struct data_unit1 data_units1[] = {
+ {D_STR_W_LEN ("abc"),
+ {0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E, 0x25, 0x71,
+ 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D}},
+ {D_STR_W_LEN ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"),
+ {0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, 0x4A, 0xA1,
+ 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1}},
+ {D_STR_W_LEN (""),
+ {0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef,
+ 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09}},
+ {D_STR_W_LEN ("The quick brown fox jumps over the lazy dog"),
+ {0x2f, 0xd4, 0xe1, 0xc6, 0x7a, 0x2d, 0x28, 0xfc, 0xed, 0x84, 0x9e, 0xe1,
+ 0xbb, 0x76, 0xe7, 0x39, 0x1b, 0x93, 0xeb, 0x12}},
+ {D_STR_W_LEN ("1234567890!@~%&$@#{}[]\\/!?`."),
+ {0xa7, 0x08, 0x9e, 0x3d, 0xe1, 0x6b, 0x63, 0xa3, 0x2a, 0x43, 0xa5, 0xe7,
+ 0xf3, 0xb5, 0x4f, 0x80, 0x6a, 0xc8, 0x4f, 0x53}},
+ {D_STR_W_LEN ("Simple string."),
+ {0x6c, 0x6c, 0x9c, 0xef, 0x53, 0xff, 0x22, 0x39, 0x0c, 0x54, 0xc7, 0xba,
+ 0x6d, 0x98, 0xe0, 0xd5, 0x6c, 0x24, 0x0d, 0x55}},
+ {D_STR_W_LEN ("abcdefghijklmnopqrstuvwxyz"),
+ {0x32, 0xd1, 0x0c, 0x7b, 0x8c, 0xf9, 0x65, 0x70, 0xca, 0x04, 0xce, 0x37,
+ 0xf2, 0xa1, 0x9d, 0x84, 0x24, 0x0d, 0x3a, 0x89}},
+ {D_STR_W_LEN ("zyxwvutsrqponMLKJIHGFEDCBA"),
+ {0x59, 0x0f, 0xc8, 0xea, 0xde, 0xa2, 0x78, 0x65, 0x5a, 0xf2, 0xa1, 0xe5,
+ 0xb4, 0xc9, 0x61, 0xa4, 0x3a, 0xc3, 0x6a, 0x83}},
+ {D_STR_W_LEN ("abcdefghijklmnopqrstuvwxyzzyxwvutsrqponMLKJIHGFEDCBA"
+ "abcdefghijklmnopqrstuvwxyzzyxwvutsrqponMLKJIHGFEDCBA"),
+ {0xa7, 0x87, 0x4c, 0x16, 0xc0, 0x4e, 0xd6, 0x24, 0x5f, 0x25, 0xbe, 0x06,
+ 0x7b, 0x1b, 0x6b, 0xaf, 0xf4, 0x0e, 0x74, 0x0b}},
+};
+
+static const size_t units1_num = sizeof(data_units1) / sizeof(data_units1[0]);
+
+struct bin_with_len
+{
+ const uint8_t bin[512];
+ const size_t len;
+};
+
+struct data_unit2
+{
+ const struct bin_with_len bin_l;
+ const uint8_t digest[SHA1_DIGEST_SIZE];
+};
+
+/* Size must be less than 512 bytes! */
+static const struct data_unit2 data_units2[] = {
+ { { {97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
+ 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122}, 26}, /* a..z
ASCII sequence */
+ {0x32, 0xd1, 0x0c, 0x7b, 0x8c, 0xf9, 0x65, 0x70, 0xca, 0x04, 0xce, 0x37,
+ 0xf2, 0xa1, 0x9d, 0x84, 0x24, 0x0d, 0x3a, 0x89}},
+ { { {65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
+ 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
+ 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
+ 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65},
+ 72 }, /* 'A' x 72 times */
+ {0x41, 0xf9, 0x07, 0x05, 0x04, 0xf9, 0xc8, 0x1a, 0xbf, 0xbb, 0x61, 0x4d,
+ 0xaa, 0xec, 0x3b, 0x26, 0xa2, 0xf9, 0x23, 0x7e}},
+ { { {19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
+ 37, 38, 39, 40, 41, 42,43, 44, 45, 46, 47, 48, 49, 50, 51, 52,53, 54,
+ 55, 56, 57, 58, 59, 60,61, 62, 63, 64, 65, 66, 67,68, 69, 70, 71, 72,
+ 73}, 55}, /* 19..73 sequence */
+ {0xf2, 0x50, 0xb2, 0x79, 0x62, 0xcc, 0xff, 0x4b, 0x1b, 0x61, 0x4a, 0x6f,
+ 0x80, 0xec, 0x9b, 0x4d, 0xd0, 0xc0, 0xfb, 0x7b}},
+ { { {7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
+ 26, 27, 28, 29, 30, 31,32, 33, 34, 35, 36, 37, 38, 39, 40, 41,42, 43,
+ 44, 45, 46, 47, 48, 49,50, 51, 52, 53, 54, 55, 56,57, 58, 59, 60, 61,
+ 62, 63, 64, 65, 66, 67, 68, 69}, 63}, /* 7..69 sequence */
+ {0xf0, 0xa3, 0xfc, 0x4b, 0x90, 0x6f, 0x1b, 0x41, 0x68, 0xc8, 0x3e, 0x05,
+ 0xb0, 0xc5, 0xac, 0xb7, 0x3d, 0xcd, 0x6b, 0x0f}},
+ { { {38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
+ 56, 57, 58, 59, 60, 61,62, 63, 64, 65, 66, 67, 68, 69, 70, 71,72, 73,
+ 74, 75, 76, 77, 78, 79,80, 81, 82, 83, 84, 85, 86,87, 88, 89, 90, 91,
+ 92}, 55}, /* 38..92 sequence */
+ {0x93, 0xa4, 0x9c, 0x5f, 0xda, 0x22, 0x63, 0x73, 0x85, 0xeb, 0x70, 0xd2,
+ 0x00, 0x52, 0x0c, 0xb0, 0x2e, 0x86, 0x9b, 0xa0 }},
+ { { {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20,21,
+ 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,37, 38, 39,
+ 40, 41, 42, 43, 44, 45,46, 47, 48, 49, 50, 51, 52,53, 54, 55, 56, 57,
+ 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,71, 72},72}, /*
1..72 sequence */
+ {0x8b, 0x30, 0xd3, 0x41, 0x89, 0xb6, 0x1b, 0x66, 0x5a, 0x1a, 0x9a, 0x51,
+ 0x64, 0x93, 0xab, 0x5e, 0x78, 0x81, 0x52, 0xb5}},
+ { { {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20,
+ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,37, 38,
+ 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,52, 53, 54, 55, 56,
+ 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,70, 71, 72, 73, 74,
+ 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,86, 87, 88, 89, 90, 91, 92,
+ 93, 94, 95, 96, 97, 98, 99, 100,101, 102, 103, 104, 105, 106, 107, 108,
+ 109, 110, 111, 112, 113, 114,115, 116, 117, 118, 119, 120, 121, 122,
+ 123, 124, 125, 126, 127,128, 129, 130, 131, 132, 133, 134, 135, 136,
+ 137, 138, 139, 140,141, 142, 143, 144, 145, 146, 147, 148, 149, 150,
+ 151, 152, 153, 154,155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
+ 165, 166, 167,168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178,
+ 179, 180,181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192,
+ 193, 194,195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206,
+ 207,208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,221,
+ 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234,235,
+ 236, 237, 238, 239, 240,241, 242, 243, 244, 245, 246, 247,248, 249, 250,
+ 251, 252, 253, 254, 255}, 256}, /* 0..255 sequence */
+ {0x49, 0x16, 0xd6, 0xbd, 0xb7, 0xf7, 0x8e, 0x68, 0x03, 0x69, 0x8c, 0xab,
+ 0x32, 0xd1, 0x58, 0x6e, 0xa4, 0x57, 0xdf, 0xc8}},
+ { { {199, 198, 197, 196, 195, 194, 193, 192, 191, 190, 189, 188, 187,
186,185,
+ 184, 183, 182, 181, 180,179, 178, 177, 176, 175, 174, 173,172, 171, 170,
+ 169, 168, 167, 166,165, 164, 163, 162, 161, 160,159, 158, 157, 156, 155,
+ 154, 153, 152, 151, 150, 149, 148, 147, 146,145, 144, 143, 142, 141,
+ 140,139}, 61}, /* 199..139 sequence */
+ {0xb3, 0xec, 0x61, 0x3c, 0xf7, 0x36, 0x57, 0x94, 0x61, 0xdb, 0xb2, 0x16,
+ 0x75, 0xfe, 0x34, 0x60, 0x99, 0x94, 0xb5, 0xfc}},
+ { { {255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242,
+ 241, 240, 239, 238, 237, 236, 235, 234, 233, 232, 231, 230, 229, 228,
+ 227, 226, 225, 224, 223, 222, 221, 220, 219, 218, 217, 216, 215, 214,
+ 213, 212, 211, 210, 209, 208, 207, 206, 205, 204, 203, 202, 201, 200,
+ 199, 198, 197, 196, 195, 194, 193, 192, 191, 190, 189, 188, 187, 186,
+ 185, 184, 183, 182, 181, 180, 179, 178, 177, 176, 175, 174, 173, 172,
+ 171, 170, 169, 168, 167, 166, 165, 164, 163, 162, 161, 160, 159, 158,
+ 157, 156, 155, 154, 153, 152, 151, 150, 149, 148, 147, 146, 145, 144,
+ 143, 142, 141, 140, 139, 138, 137, 136, 135, 134, 133, 132, 131, 130,
+ 129, 128, 127, 126, 125, 124, 123, 122, 121, 120, 119, 118, 117, 116,
+ 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102,
+ 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85,
+ 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67,
+ 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49,
+ 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
+ 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13,
+ 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, 255}, /* 255..1 sequence */
+ {0x99, 0x1d, 0xc6, 0x95, 0xe1, 0xaa, 0xcc, 0xc9, 0x35, 0x60, 0xbe, 0xe3,
+ 0xe2, 0x41, 0x2b, 0xa7, 0xab, 0x49, 0x32, 0xf7}},
+ { { {41, 35, 190, 132, 225, 108, 214, 174, 82, 144, 73, 241, 241, 187, 233,
+ 235, 179, 166, 219, 60, 135, 12, 62, 153, 36, 94, 13, 28, 6, 183, 71,
+ 222, 179, 18, 77, 200, 67, 187, 139, 166, 31, 3, 90, 125, 9, 56, 37, 31,
+ 93, 212, 203, 252, 150, 245, 69, 59, 19, 13, 137, 10, 28, 219, 174, 50,
+ 32, 154, 80, 238, 64, 120, 54, 253, 18, 73, 50, 246, 158, 125, 73, 220,
+ 173, 79, 20, 242, 68, 64, 102, 208, 107, 196, 48, 183, 50, 59, 161, 34,
+ 246, 34, 145, 157, 225, 139, 31, 218, 176, 202, 153, 2, 185, 114, 157,
+ 73, 44, 128, 126, 197, 153, 213, 233, 128, 178, 234, 201, 204, 83, 191,
+ 103, 214, 191, 20, 214, 126, 45, 220, 142, 102, 131, 239, 87, 73, 97,
+ 255, 105, 143, 97, 205, 209, 30, 157, 156, 22, 114, 114, 230, 29, 240,
+ 132, 79, 74, 119, 2, 215, 232, 57, 44, 83, 203, 201, 18, 30, 51, 116,
+ 158, 12, 244, 213, 212, 159, 212, 164, 89, 126, 53, 207, 50, 34, 244,
+ 204, 207, 211, 144, 45, 72, 211, 143, 117, 230, 217, 29, 42, 229, 192,
+ 247, 43, 120, 129, 135, 68, 14, 95, 80, 0, 212, 97, 141, 190, 123, 5,
+ 21, 7, 59, 51, 130, 31, 24, 112, 146, 218, 100, 84, 206, 177, 133, 62,
+ 105, 21, 248, 70, 106, 4, 150, 115, 14, 217, 22, 47, 103, 104, 212, 247,
+ 74, 74, 208, 87, 104}, 255}, /* pseudo-random data */
+ {0x9e, 0xb6, 0xce, 0x48, 0xf4, 0x6e, 0x9c, 0xf4, 0x1b, 0x4f, 0x9f, 0x66,
+ 0xdd, 0xe8, 0x41, 0x01, 0x71, 0xf6, 0xf5, 0xd6}}
+};
+
+static const size_t units2_num = sizeof(data_units2) / sizeof(data_units2[0]);
+
+
+/*
+ * Helper functions
+ */
+
+/**
+ * Print bin as hex
+ *
+ * @param bin binary data
+ * @param len number of bytes in bin
+ * @param hex pointer to len*2+1 bytes buffer
+ */
+static void
+bin2hex (const uint8_t *bin,
+ size_t len,
+ char *hex)
+{
+ while (len-- > 0)
+ {
+ unsigned int b1, b2;
+ b1 = (*bin >> 4) & 0xf;
+ *hex++ = (char) ((b1 > 9) ? (b1 + 'A' - 10) : (b1 + '0'));
+ b2 = *bin++ & 0xf;
+ *hex++ = (char) ((b2 > 9) ? (b2 + 'A' - 10) : (b2 + '0'));
+ }
+ *hex = 0;
+}
+
+
+static int
+check_result (const char *test_name,
+ unsigned int check_num,
+ const uint8_t calculated[SHA1_DIGEST_SIZE],
+ const uint8_t expected[SHA1_DIGEST_SIZE])
+{
+ int failed = memcmp (calculated, expected, SHA1_DIGEST_SIZE);
+ check_num++; /* Print 1-based numbers */
+ if (failed)
+ {
+ char calc_str[SHA1_DIGEST_STRING_SIZE];
+ char expc_str[SHA1_DIGEST_STRING_SIZE];
+ bin2hex (calculated, SHA1_DIGEST_SIZE, calc_str);
+ bin2hex (expected, SHA1_DIGEST_SIZE, expc_str);
+ fprintf (stderr,
+ "FAILED: %s check %u: calculated digest %s, expected digest
%s.\n",
+ test_name, check_num, calc_str, expc_str);
+ fflush (stderr);
+ }
+ else if (verbose)
+ {
+ char calc_str[SHA1_DIGEST_STRING_SIZE];
+ bin2hex (calculated, SHA1_DIGEST_SIZE, calc_str);
+ printf (
+ "PASSED: %s check %u: calculated digest %s matches expected digest.\n",
+ test_name, check_num, calc_str);
+ fflush (stdout);
+ }
+ return failed ? 1 : 0;
+}
+
+
+/*
+ * Tests
+ */
+
+/* Calculated SHA-256 as one pass for whole data */
+static int
+test1_str (void)
+{
+ int num_failed = 0;
+ unsigned int i;
+
+ for (i = 0; i < units1_num; i++)
+ {
+ struct sha1_ctx ctx;
+ uint8_t digest[SHA1_DIGEST_SIZE];
+
+ MHD_SHA1_init (&ctx);
+ MHD_SHA1_update (&ctx, (const uint8_t*) data_units1[i].str_l.str,
+ data_units1[i].str_l.len);
+ MHD_SHA1_finish (&ctx, digest);
+ num_failed += check_result (__FUNCTION__, i, digest,
+ data_units1[i].digest);
+ }
+ return num_failed;
+}
+
+
+static int
+test1_bin (void)
+{
+ int num_failed = 0;
+ unsigned int i;
+
+ for (i = 0; i < units2_num; i++)
+ {
+ struct sha1_ctx ctx;
+ uint8_t digest[SHA1_DIGEST_SIZE];
+
+ MHD_SHA1_init (&ctx);
+ MHD_SHA1_update (&ctx, data_units2[i].bin_l.bin,
+ data_units2[i].bin_l.len);
+ MHD_SHA1_finish (&ctx, digest);
+ num_failed += check_result (__FUNCTION__, i, digest,
+ data_units2[i].digest);
+ }
+ return num_failed;
+}
+
+
+/* Calculated SHA-256 as two iterations for whole data */
+static int
+test2_str (void)
+{
+ int num_failed = 0;
+ unsigned int i;
+
+ for (i = 0; i < units1_num; i++)
+ {
+ struct sha1_ctx ctx;
+ uint8_t digest[SHA1_DIGEST_SIZE];
+ size_t part_s = data_units1[i].str_l.len / 4;
+
+ MHD_SHA1_init (&ctx);
+ MHD_SHA1_update (&ctx, (const uint8_t*) data_units1[i].str_l.str, part_s);
+ MHD_SHA1_update (&ctx, (const uint8_t*) data_units1[i].str_l.str + part_s,
+ data_units1[i].str_l.len - part_s);
+ MHD_SHA1_finish (&ctx, digest);
+ num_failed += check_result (__FUNCTION__, i, digest,
+ data_units1[i].digest);
+ }
+ return num_failed;
+}
+
+
+static int
+test2_bin (void)
+{
+ int num_failed = 0;
+ unsigned int i;
+
+ for (i = 0; i < units2_num; i++)
+ {
+ struct sha1_ctx ctx;
+ uint8_t digest[SHA1_DIGEST_SIZE];
+ size_t part_s = data_units2[i].bin_l.len * 2 / 3;
+
+ MHD_SHA1_init (&ctx);
+ MHD_SHA1_update (&ctx, data_units2[i].bin_l.bin, part_s);
+ MHD_SHA1_update (&ctx, data_units2[i].bin_l.bin + part_s,
+ data_units2[i].bin_l.len - part_s);
+ MHD_SHA1_finish (&ctx, digest);
+ num_failed += check_result (__FUNCTION__, i, digest,
+ data_units2[i].digest);
+ }
+ return num_failed;
+}
+
+
+/* Use data set number 7 as it is the longest sequence */
+#define DATA_POS 6
+#define MAX_OFFSET 31
+
+static int
+test_unaligned (void)
+{
+ int num_failed = 0;
+ unsigned int offset;
+ uint8_t *buf;
+ uint8_t *digest_buf;
+
+ const struct data_unit2 *const tdata = data_units2 + DATA_POS;
+
+ buf = malloc (tdata->bin_l.len + MAX_OFFSET);
+ digest_buf = malloc (SHA1_DIGEST_SIZE + MAX_OFFSET);
+ if ((NULL == buf) || (NULL == digest_buf))
+ _exit (99);
+
+ for (offset = MAX_OFFSET; offset >= 1; --offset)
+ {
+ struct sha1_ctx ctx;
+ uint8_t *unaligned_digest;
+ uint8_t *unaligned_buf;
+
+ unaligned_buf = buf + offset;
+ memcpy (unaligned_buf, tdata->bin_l.bin, tdata->bin_l.len);
+ unaligned_digest = digest_buf + MAX_OFFSET - offset;
+ memset (unaligned_digest, 0, SHA1_DIGEST_SIZE);
+
+ MHD_SHA1_init (&ctx);
+ MHD_SHA1_update (&ctx, unaligned_buf, tdata->bin_l.len);
+ MHD_SHA1_finish (&ctx, unaligned_digest);
+ num_failed += check_result (__FUNCTION__, MAX_OFFSET - offset,
+ unaligned_digest, tdata->digest);
+ }
+ free (buf);
+ return num_failed;
+}
+
+
+int
+main (int argc, char *argv[])
+{
+ int num_failed = 0;
+ (void) has_in_name; /* Mute compiler warning. */
+ if (has_param (argc, argv, "-v") || has_param (argc, argv, "--verbose"))
+ verbose = 1;
+
+ num_failed += test1_str ();
+ num_failed += test1_bin ();
+
+ num_failed += test2_str ();
+ num_failed += test2_bin ();
+
+ num_failed += test_unaligned ();
+
+ return num_failed ? 1 : 0;
+}
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
- [libmicrohttpd] branch master updated (8a9a64a7 -> 714c5735), gnunet, 2021/05/16
- [libmicrohttpd] 01/06: mhd_bithelpers: replaced macro with static function, gnunet, 2021/05/16
- [libmicrohttpd] 03/06: sha256: re-arranged struct members to have better alignment, gnunet, 2021/05/16
- [libmicrohttpd] 05/06: sha256: formatting, cosmetics, typos in comments, gnunet, 2021/05/16
- [libmicrohttpd] 02/06: sha256: renamed one function for clarity and uniformity, gnunet, 2021/05/16
- [libmicrohttpd] 04/06: sha256: minor optimization, gnunet, 2021/05/16
- [libmicrohttpd] 06/06: Implemented SHA-1 calculation,
gnunet <=