diff options
Diffstat (limited to 'src/microhttpd/sha512_256.h')
-rw-r--r-- | src/microhttpd/sha512_256.h | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/src/microhttpd/sha512_256.h b/src/microhttpd/sha512_256.h new file mode 100644 index 00000000..43359dc7 --- /dev/null +++ b/src/microhttpd/sha512_256.h | |||
@@ -0,0 +1,138 @@ | |||
1 | /* | ||
2 | This file is part of GNU libmicrohttpd | ||
3 | Copyright (C) 2022 Karlson2k (Evgeny Grin) | ||
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 this library. | ||
17 | If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | ||
19 | |||
20 | /** | ||
21 | * @file microhttpd/sha512_256.h | ||
22 | * @brief Calculation of SHA-512/256 digest | ||
23 | * @author Karlson2k (Evgeny Grin) | ||
24 | */ | ||
25 | |||
26 | #ifndef MHD_SHA512_256_H | ||
27 | #define MHD_SHA512_256_H 1 | ||
28 | |||
29 | #include "mhd_options.h" | ||
30 | #include <stdint.h> | ||
31 | #ifdef HAVE_STDDEF_H | ||
32 | #include <stddef.h> /* for size_t */ | ||
33 | #endif /* HAVE_STDDEF_H */ | ||
34 | |||
35 | |||
36 | /** | ||
37 | * Number of bits in single SHA-512/256 word. | ||
38 | */ | ||
39 | #define SHA512_256_WORD_SIZE_BITS 64 | ||
40 | |||
41 | /** | ||
42 | * Number of bytes in single SHA-512/256 word. | ||
43 | */ | ||
44 | #define SHA512_256_BYTES_IN_WORD (SHA512_256_WORD_SIZE_BITS / 8) | ||
45 | |||
46 | /** | ||
47 | * Hash is kept internally as 8 64-bit words. | ||
48 | * This is intermediate hash size, used during computing the final digest. | ||
49 | */ | ||
50 | #define SHA512_256_HASH_SIZE_WORDS 8 | ||
51 | |||
52 | /** | ||
53 | * Size of SHA-512/256 resulting digest in bytes. | ||
54 | * This is the final digest size, not intermediate hash. | ||
55 | */ | ||
56 | #define SHA512_256_DIGEST_SIZE_WORDS (SHA512_256_HASH_SIZE_WORDS / 2) | ||
57 | |||
58 | /** | ||
59 | * Size of SHA-512/256 resulting digest in bytes | ||
60 | * This is the final digest size, not intermediate hash. | ||
61 | */ | ||
62 | #define SHA512_256_DIGEST_SIZE \ | ||
63 | (SHA512_256_DIGEST_SIZE_WORDS * SHA512_256_BYTES_IN_WORD) | ||
64 | |||
65 | /** | ||
66 | * Size of SHA-512/256 digest string in chars including termination NUL. | ||
67 | */ | ||
68 | #define SHA512_256_DIGEST_STRING_SIZE ((SHA512_256_DIGEST_SIZE) * 2 + 1) | ||
69 | |||
70 | /** | ||
71 | * Size of single processing block in bits. | ||
72 | * This is the final digest size, not intermediate hash. | ||
73 | */ | ||
74 | #define SHA512_256_BLOCK_SIZE_BITS 1024 | ||
75 | |||
76 | /** | ||
77 | * Size of single processing block in bytes. | ||
78 | */ | ||
79 | #define SHA512_256_BLOCK_SIZE (SHA512_256_BLOCK_SIZE_BITS / 8) | ||
80 | |||
81 | /** | ||
82 | * Size of single processing block in words. | ||
83 | */ | ||
84 | #define SHA512_256_BLOCK_SIZE_WORDS \ | ||
85 | (SHA512_256_BLOCK_SIZE_BITS / SHA512_256_WORD_SIZE_BITS) | ||
86 | |||
87 | |||
88 | /** | ||
89 | * SHA-512/256 calculation context | ||
90 | */ | ||
91 | struct Sha512_256Ctx | ||
92 | { | ||
93 | uint64_t H[SHA512_256_HASH_SIZE_WORDS]; /**< Intermediate hash value */ | ||
94 | uint64_t buffer[SHA512_256_BLOCK_SIZE_WORDS]; /**< SHA512_256 input data buffer */ | ||
95 | /** | ||
96 | * The number of bytes, lower part | ||
97 | */ | ||
98 | uint64_t count; | ||
99 | /** | ||
100 | * The number of bits, high part. | ||
101 | * Unlike lower part, this counts the number of bits, not bytes. | ||
102 | */ | ||
103 | uint64_t count_bits_hi; | ||
104 | }; | ||
105 | |||
106 | /** | ||
107 | * Initialise structure for SHA-512/256 calculation. | ||
108 | * | ||
109 | * @param ctx the calculation context | ||
110 | */ | ||
111 | void | ||
112 | MHD_SHA512_256_init (struct Sha512_256Ctx *ctx); | ||
113 | |||
114 | |||
115 | /** | ||
116 | * Process portion of bytes. | ||
117 | * | ||
118 | * @param ctx the calculation context | ||
119 | * @param data bytes to add to hash | ||
120 | * @param length number of bytes in @a data | ||
121 | */ | ||
122 | void | ||
123 | MHD_SHA512_256_update (struct Sha512_256Ctx *ctx, | ||
124 | const uint8_t *data, | ||
125 | size_t length); | ||
126 | |||
127 | |||
128 | /** | ||
129 | * Finalise SHA-512/256 calculation, return digest. | ||
130 | * | ||
131 | * @param ctx the calculation context | ||
132 | * @param[out] digest set to the hash, must be #SHA512_256_DIGEST_SIZE bytes | ||
133 | */ | ||
134 | void | ||
135 | MHD_SHA512_256_finish (struct Sha512_256Ctx *ctx, | ||
136 | uint8_t digest[SHA512_256_DIGEST_SIZE]); | ||
137 | |||
138 | #endif /* MHD_SHA512_256_H */ | ||