Debian Codegraph @

src/microhttpd/sha512_256.h

raw
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 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 SHA-512/256 single processing block in bits.
72 */
73 #define SHA512_256_BLOCK_SIZE_BITS 1024
74
75 /**
76 * Size of SHA-512/256 single processing block in bytes.
77 */
78 #define SHA512_256_BLOCK_SIZE (SHA512_256_BLOCK_SIZE_BITS / 8)
79
80 /**
81 * Size of SHA-512/256 single processing block in words.
82 */
83 #define SHA512_256_BLOCK_SIZE_WORDS \
84 (SHA512_256_BLOCK_SIZE_BITS / SHA512_256_WORD_SIZE_BITS)
85
86
87 /**
88 * SHA-512/256 calculation context
89 */
90 struct Sha512_256Ctx
91 {
92 uint64_t H[SHA512_256_HASH_SIZE_WORDS]; /**< Intermediate hash value */
93 uint64_t buffer[SHA512_256_BLOCK_SIZE_WORDS]; /**< SHA512_256 input data buffer */
94 /**
95 * The number of bytes, lower part
96 */
97 uint64_t count;
98 /**
99 * The number of bits, high part.
100 * Unlike lower part, this counts the number of bits, not bytes.
101 */
102 uint64_t count_bits_hi;
103 };
104
105 /**
106 * Initialise structure for SHA-512/256 calculation.
107 *
108 * @param ctx the calculation context
109 */
110 void
111 MHD_SHA512_256_init (struct Sha512_256Ctx *ctx);
112
113
114 /**
115 * Process portion of bytes.
116 *
117 * @param ctx the calculation context
118 * @param data bytes to add to hash
119 * @param length number of bytes in @a data
120 */
121 void
122 MHD_SHA512_256_update (struct Sha512_256Ctx *ctx,
123 const uint8_t *data,
124 size_t length);
125
126
127 /**
128 * Finalise SHA-512/256 calculation, return digest.
129 *
130 * @param ctx the calculation context
131 * @param[out] digest set to the hash, must be #SHA512_256_DIGEST_SIZE bytes
132 */
133 void
134 MHD_SHA512_256_finish (struct Sha512_256Ctx *ctx,
135 uint8_t digest[SHA512_256_DIGEST_SIZE]);
136
137 #endif /* MHD_SHA512_256_H */