/src/mhd2/src/mhd2/sha256_int.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | This file is part of GNU libmicrohttpd |
3 | | Copyright (C) 2019-2024 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 | | GNU libmicrohttpd 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; if not, write to the Free Software |
17 | | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
18 | | |
19 | | */ |
20 | | |
21 | | /** |
22 | | * @file src/mhd2/sha256.c |
23 | | * @brief Calculation of SHA-256 digest as defined in FIPS PUB 180-4 (2015) |
24 | | * @author Karlson2k (Evgeny Grin) |
25 | | */ |
26 | | |
27 | | #include "mhd_sys_options.h" |
28 | | |
29 | | #include "sys_bool_type.h" |
30 | | |
31 | | #include <string.h> |
32 | | #include "mhd_bithelpers.h" |
33 | | #include "mhd_assert.h" |
34 | | |
35 | | #include "sha256_int.h" |
36 | | |
37 | | MHD_INTERNAL void MHD_FN_PAR_NONNULL_ALL_ |
38 | | mhd_SHA256_init (struct mhd_Sha256CtxInt *ctx) |
39 | 0 | { |
40 | | /* Initial hash values, see FIPS PUB 180-4 paragraph 5.3.3 */ |
41 | | /* First thirty-two bits of the fractional parts of the square |
42 | | * roots of the first eight prime numbers: 2, 3, 5, 7, 11, 13, |
43 | | * 17, 19." */ |
44 | 0 | ctx->H[0] = UINT32_C (0x6a09e667); |
45 | 0 | ctx->H[1] = UINT32_C (0xbb67ae85); |
46 | 0 | ctx->H[2] = UINT32_C (0x3c6ef372); |
47 | 0 | ctx->H[3] = UINT32_C (0xa54ff53a); |
48 | 0 | ctx->H[4] = UINT32_C (0x510e527f); |
49 | 0 | ctx->H[5] = UINT32_C (0x9b05688c); |
50 | 0 | ctx->H[6] = UINT32_C (0x1f83d9ab); |
51 | 0 | ctx->H[7] = UINT32_C (0x5be0cd19); |
52 | | |
53 | | /* Initialise number of bytes. */ |
54 | 0 | ctx->count = 0; |
55 | 0 | } |
56 | | |
57 | | |
58 | | mhd_DATA_TRUNCATION_RUNTIME_CHECK_DISABLE |
59 | | |
60 | | static MHD_FN_PAR_NONNULL_ALL_ void |
61 | | sha256_transform (uint32_t H[mhd_SHA256_DIGEST_SIZE_WORDS], |
62 | | const void *restrict data) |
63 | 0 | { |
64 | | /* Working variables, |
65 | | see FIPS PUB 180-4 paragraph 6.2. */ |
66 | 0 | uint32_t a = H[0]; |
67 | 0 | uint32_t b = H[1]; |
68 | 0 | uint32_t c = H[2]; |
69 | 0 | uint32_t d = H[3]; |
70 | 0 | uint32_t e = H[4]; |
71 | 0 | uint32_t f = H[5]; |
72 | 0 | uint32_t g = H[6]; |
73 | 0 | uint32_t h = H[7]; |
74 | | |
75 | | /* Data buffer, used as cyclic buffer. |
76 | | See FIPS PUB 180-4 paragraphs 5.2.1, 6.2. */ |
77 | 0 | uint32_t W[16]; |
78 | |
|
79 | 0 | #ifndef mhd_GET_32BIT_BE_UNALIGNED |
80 | 0 | if (0 != (((uintptr_t) data) % mhd_UINT32_ALIGN)) |
81 | 0 | { |
82 | | /* Copy the unaligned input data to the aligned buffer */ |
83 | 0 | memcpy (W, data, mhd_SHA256_BLOCK_SIZE); |
84 | | /* The W[] buffer itself will be used as the source of the data, |
85 | | * but data will be reloaded in correct bytes order during |
86 | | * the next steps */ |
87 | 0 | data = (const void *) W; |
88 | 0 | } |
89 | 0 | #endif /* mhd_GET_32BIT_BE_UNALIGNED */ |
90 | | |
91 | | /* 'Ch' and 'Maj' macro functions are defined with |
92 | | widely-used optimization. |
93 | | See FIPS PUB 180-4 formulae 4.2, 4.3. */ |
94 | 0 | #define Ch(x,y,z) ( (z) ^ ((x) & ((y) ^ (z))) ) |
95 | 0 | #define Maj(x,y,z) ( ((x) & (y)) ^ ((z) & ((x) ^ (y))) ) |
96 | | /* Unoptimized (original) versions: */ |
97 | | /* #define Ch(x,y,z) ( ( (x) & (y) ) ^ ( ~(x) & (z) ) ) */ |
98 | | /* #define Maj(x,y,z) ( ((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)) ) */ |
99 | | |
100 | | /* Four 'Sigma' macro functions. |
101 | | See FIPS PUB 180-4 formulae 4.4, 4.5, 4.6, 4.7. */ |
102 | 0 | #define SIG0(x) (mhd_ROTR32 ((x), 2) ^ mhd_ROTR32 ((x), 13) ^ \ |
103 | 0 | mhd_ROTR32 ((x), 22) ) |
104 | 0 | #define SIG1(x) (mhd_ROTR32 ((x), 6) ^ mhd_ROTR32 ((x), 11) ^ \ |
105 | 0 | mhd_ROTR32 ((x), 25) ) |
106 | 0 | #define sig0(x) (mhd_ROTR32 ((x), 7) ^ mhd_ROTR32 ((x), 18) ^ \ |
107 | 0 | ((x) >> 3) ) |
108 | 0 | #define sig1(x) (mhd_ROTR32 ((x), 17) ^ mhd_ROTR32 ((x),19) ^ \ |
109 | 0 | ((x) >> 10) ) |
110 | | |
111 | | /* One step of SHA-256 computation, |
112 | | see FIPS PUB 180-4 paragraph 6.2.2 step 3. |
113 | | * Note: this macro updates working variables in-place, without rotation. |
114 | | * Note: first (vH += SIG1(vE) + Ch(vE,vF,vG) + kt + wt) equals T1 in FIPS PUB 180-4 paragraph 6.2.2 step 3. |
115 | | second (vH += SIG0(vA) + Maj(vE,vF,vC) equals T1 + T2 in FIPS PUB 180-4 paragraph 6.2.2 step 3. |
116 | | * Note: 'wt' must be used exactly one time in this macro as it change other data as well |
117 | | every time when used. */ |
118 | 0 | #define SHA2STEP32(vA,vB,vC,vD,vE,vF,vG,vH,kt,wt) do { \ |
119 | 0 | (vD) += ((vH) += SIG1 ((vE)) + Ch ((vE),(vF),(vG)) + (kt) + (wt)); \ |
120 | 0 | (vH) += SIG0 ((vA)) + Maj ((vA),(vB),(vC)); } while (0) |
121 | | |
122 | | /* Get value of W(t) from input data buffer, |
123 | | See FIPS PUB 180-4 paragraph 6.2. |
124 | | Input data must be read in big-endian bytes order, |
125 | | see FIPS PUB 180-4 paragraph 3.1.2. */ |
126 | | /* Use cast to (const void*) to mute compiler alignment warning, |
127 | | * data was already aligned in previous step */ |
128 | 0 | #define GET_W_FROM_DATA(buf,t) \ |
129 | 0 | mhd_GET_32BIT_BE ((const void*) (((const uint8_t*) (buf)) + \ |
130 | 0 | (t) * mhd_SHA256_BYTES_IN_WORD)) |
131 | | |
132 | | /* 'W' generation and assignment for 16 <= t <= 63. |
133 | | See FIPS PUB 180-4 paragraph 6.2.2. |
134 | | As only last 16 'W' are used in calculations, it is possible to |
135 | | use 16 elements array of W as cyclic buffer. |
136 | | * Note: ((t-16)&0xf) have same value as (t&0xf) */ |
137 | 0 | #define Wgen(w,t) ( (w)[(t - 16) & 0xf] + sig1 ((w)[((t) - 2) & 0xf]) \ |
138 | 0 | + (w)[((t) - 7) & 0xf] + sig0 ((w)[((t) - 15) & 0xf]) ) |
139 | |
|
140 | 0 | #ifndef MHD_FAVOR_SMALL_CODE |
141 | | |
142 | | /* Note: instead of using K constants as array, all K values are specified |
143 | | individually for each step, see FIPS PUB 180-4 paragraph 4.2.2 for |
144 | | K values. */ |
145 | | /* Note: instead of reassigning all working variables on each step, |
146 | | variables are rotated for each step: |
147 | | SHA2STEP32(a, b, c, d, e, f, g, h, K[0], data[0]); |
148 | | SHA2STEP32(h, a, b, c, d, e, f, g, K[1], data[1]); |
149 | | so current 'vD' will be used as 'vE' on next step, |
150 | | current 'vH' will be used as 'vA' on next step. */ |
151 | | #if mhd_BYTE_ORDER == mhd_BIG_ENDIAN |
152 | | if ((const void *) W == data) |
153 | | { |
154 | | /* The input data is already in the cyclic data buffer W[] in correct bytes |
155 | | order. */ |
156 | | SHA2STEP32 (a, b, c, d, e, f, g, h, UINT32_C (0x428a2f98), W[0]); |
157 | | SHA2STEP32 (h, a, b, c, d, e, f, g, UINT32_C (0x71374491), W[1]); |
158 | | SHA2STEP32 (g, h, a, b, c, d, e, f, UINT32_C (0xb5c0fbcf), W[2]); |
159 | | SHA2STEP32 (f, g, h, a, b, c, d, e, UINT32_C (0xe9b5dba5), W[3]); |
160 | | SHA2STEP32 (e, f, g, h, a, b, c, d, UINT32_C (0x3956c25b), W[4]); |
161 | | SHA2STEP32 (d, e, f, g, h, a, b, c, UINT32_C (0x59f111f1), W[5]); |
162 | | SHA2STEP32 (c, d, e, f, g, h, a, b, UINT32_C (0x923f82a4), W[6]); |
163 | | SHA2STEP32 (b, c, d, e, f, g, h, a, UINT32_C (0xab1c5ed5), W[7]); |
164 | | SHA2STEP32 (a, b, c, d, e, f, g, h, UINT32_C (0xd807aa98), W[8]); |
165 | | SHA2STEP32 (h, a, b, c, d, e, f, g, UINT32_C (0x12835b01), W[9]); |
166 | | SHA2STEP32 (g, h, a, b, c, d, e, f, UINT32_C (0x243185be), W[10]); |
167 | | SHA2STEP32 (f, g, h, a, b, c, d, e, UINT32_C (0x550c7dc3), W[11]); |
168 | | SHA2STEP32 (e, f, g, h, a, b, c, d, UINT32_C (0x72be5d74), W[12]); |
169 | | SHA2STEP32 (d, e, f, g, h, a, b, c, UINT32_C (0x80deb1fe), W[13]); |
170 | | SHA2STEP32 (c, d, e, f, g, h, a, b, UINT32_C (0x9bdc06a7), W[14]); |
171 | | SHA2STEP32 (b, c, d, e, f, g, h, a, UINT32_C (0xc19bf174), W[15]); |
172 | | } |
173 | | else /* Combined with the next 'if' */ |
174 | | #endif /* mhd_BYTE_ORDER == mhd_BIG_ENDIAN */ |
175 | 0 | if (1) |
176 | 0 | { |
177 | | /* During first 16 steps, before making any calculations on each step, |
178 | | the W element is read from input data buffer as big-endian value and |
179 | | stored in array of W elements. */ |
180 | 0 | SHA2STEP32 (a, b, c, d, e, f, g, h, UINT32_C (0x428a2f98), W[0] = \ |
181 | 0 | GET_W_FROM_DATA (data, 0)); |
182 | 0 | SHA2STEP32 (h, a, b, c, d, e, f, g, UINT32_C (0x71374491), W[1] = \ |
183 | 0 | GET_W_FROM_DATA (data, 1)); |
184 | 0 | SHA2STEP32 (g, h, a, b, c, d, e, f, UINT32_C (0xb5c0fbcf), W[2] = \ |
185 | 0 | GET_W_FROM_DATA (data, 2)); |
186 | 0 | SHA2STEP32 (f, g, h, a, b, c, d, e, UINT32_C (0xe9b5dba5), W[3] = \ |
187 | 0 | GET_W_FROM_DATA (data, 3)); |
188 | 0 | SHA2STEP32 (e, f, g, h, a, b, c, d, UINT32_C (0x3956c25b), W[4] = \ |
189 | 0 | GET_W_FROM_DATA (data, 4)); |
190 | 0 | SHA2STEP32 (d, e, f, g, h, a, b, c, UINT32_C (0x59f111f1), W[5] = \ |
191 | 0 | GET_W_FROM_DATA (data, 5)); |
192 | 0 | SHA2STEP32 (c, d, e, f, g, h, a, b, UINT32_C (0x923f82a4), W[6] = \ |
193 | 0 | GET_W_FROM_DATA (data, 6)); |
194 | 0 | SHA2STEP32 (b, c, d, e, f, g, h, a, UINT32_C (0xab1c5ed5), W[7] = \ |
195 | 0 | GET_W_FROM_DATA (data, 7)); |
196 | 0 | SHA2STEP32 (a, b, c, d, e, f, g, h, UINT32_C (0xd807aa98), W[8] = \ |
197 | 0 | GET_W_FROM_DATA (data, 8)); |
198 | 0 | SHA2STEP32 (h, a, b, c, d, e, f, g, UINT32_C (0x12835b01), W[9] = \ |
199 | 0 | GET_W_FROM_DATA (data, 9)); |
200 | 0 | SHA2STEP32 (g, h, a, b, c, d, e, f, UINT32_C (0x243185be), W[10] = \ |
201 | 0 | GET_W_FROM_DATA (data, 10)); |
202 | 0 | SHA2STEP32 (f, g, h, a, b, c, d, e, UINT32_C (0x550c7dc3), W[11] = \ |
203 | 0 | GET_W_FROM_DATA (data, 11)); |
204 | 0 | SHA2STEP32 (e, f, g, h, a, b, c, d, UINT32_C (0x72be5d74), W[12] = \ |
205 | 0 | GET_W_FROM_DATA (data, 12)); |
206 | 0 | SHA2STEP32 (d, e, f, g, h, a, b, c, UINT32_C (0x80deb1fe), W[13] = \ |
207 | 0 | GET_W_FROM_DATA (data, 13)); |
208 | 0 | SHA2STEP32 (c, d, e, f, g, h, a, b, UINT32_C (0x9bdc06a7), W[14] = \ |
209 | 0 | GET_W_FROM_DATA (data, 14)); |
210 | 0 | SHA2STEP32 (b, c, d, e, f, g, h, a, UINT32_C (0xc19bf174), W[15] = \ |
211 | 0 | GET_W_FROM_DATA (data, 15)); |
212 | 0 | } |
213 | | |
214 | | /* During last 48 steps, before making any calculations on each step, |
215 | | current W element is generated from other W elements of the cyclic buffer |
216 | | and the generated value is stored back in the cyclic buffer. */ |
217 | | /* Note: instead of using K constants as array, all K values are specified |
218 | | individually for each step, see FIPS PUB 180-4 paragraph 4.2.2 for K values. */ |
219 | 0 | SHA2STEP32 (a, b, c, d, e, f, g, h, UINT32_C (0xe49b69c1), W[16 & 0xf] = \ |
220 | 0 | Wgen (W,16)); |
221 | 0 | SHA2STEP32 (h, a, b, c, d, e, f, g, UINT32_C (0xefbe4786), W[17 & 0xf] = \ |
222 | 0 | Wgen (W,17)); |
223 | 0 | SHA2STEP32 (g, h, a, b, c, d, e, f, UINT32_C (0x0fc19dc6), W[18 & 0xf] = \ |
224 | 0 | Wgen (W,18)); |
225 | 0 | SHA2STEP32 (f, g, h, a, b, c, d, e, UINT32_C (0x240ca1cc), W[19 & 0xf] = \ |
226 | 0 | Wgen (W,19)); |
227 | 0 | SHA2STEP32 (e, f, g, h, a, b, c, d, UINT32_C (0x2de92c6f), W[20 & 0xf] = \ |
228 | 0 | Wgen (W,20)); |
229 | 0 | SHA2STEP32 (d, e, f, g, h, a, b, c, UINT32_C (0x4a7484aa), W[21 & 0xf] = \ |
230 | 0 | Wgen (W,21)); |
231 | 0 | SHA2STEP32 (c, d, e, f, g, h, a, b, UINT32_C (0x5cb0a9dc), W[22 & 0xf] = \ |
232 | 0 | Wgen (W,22)); |
233 | 0 | SHA2STEP32 (b, c, d, e, f, g, h, a, UINT32_C (0x76f988da), W[23 & 0xf] = \ |
234 | 0 | Wgen (W,23)); |
235 | 0 | SHA2STEP32 (a, b, c, d, e, f, g, h, UINT32_C (0x983e5152), W[24 & 0xf] = \ |
236 | 0 | Wgen (W,24)); |
237 | 0 | SHA2STEP32 (h, a, b, c, d, e, f, g, UINT32_C (0xa831c66d), W[25 & 0xf] = \ |
238 | 0 | Wgen (W,25)); |
239 | 0 | SHA2STEP32 (g, h, a, b, c, d, e, f, UINT32_C (0xb00327c8), W[26 & 0xf] = \ |
240 | 0 | Wgen (W,26)); |
241 | 0 | SHA2STEP32 (f, g, h, a, b, c, d, e, UINT32_C (0xbf597fc7), W[27 & 0xf] = \ |
242 | 0 | Wgen (W,27)); |
243 | 0 | SHA2STEP32 (e, f, g, h, a, b, c, d, UINT32_C (0xc6e00bf3), W[28 & 0xf] = \ |
244 | 0 | Wgen (W,28)); |
245 | 0 | SHA2STEP32 (d, e, f, g, h, a, b, c, UINT32_C (0xd5a79147), W[29 & 0xf] = \ |
246 | 0 | Wgen (W,29)); |
247 | 0 | SHA2STEP32 (c, d, e, f, g, h, a, b, UINT32_C (0x06ca6351), W[30 & 0xf] = \ |
248 | 0 | Wgen (W,30)); |
249 | 0 | SHA2STEP32 (b, c, d, e, f, g, h, a, UINT32_C (0x14292967), W[31 & 0xf] = \ |
250 | 0 | Wgen (W,31)); |
251 | 0 | SHA2STEP32 (a, b, c, d, e, f, g, h, UINT32_C (0x27b70a85), W[32 & 0xf] = \ |
252 | 0 | Wgen (W,32)); |
253 | 0 | SHA2STEP32 (h, a, b, c, d, e, f, g, UINT32_C (0x2e1b2138), W[33 & 0xf] = \ |
254 | 0 | Wgen (W,33)); |
255 | 0 | SHA2STEP32 (g, h, a, b, c, d, e, f, UINT32_C (0x4d2c6dfc), W[34 & 0xf] = \ |
256 | 0 | Wgen (W,34)); |
257 | 0 | SHA2STEP32 (f, g, h, a, b, c, d, e, UINT32_C (0x53380d13), W[35 & 0xf] = \ |
258 | 0 | Wgen (W,35)); |
259 | 0 | SHA2STEP32 (e, f, g, h, a, b, c, d, UINT32_C (0x650a7354), W[36 & 0xf] = \ |
260 | 0 | Wgen (W,36)); |
261 | 0 | SHA2STEP32 (d, e, f, g, h, a, b, c, UINT32_C (0x766a0abb), W[37 & 0xf] = \ |
262 | 0 | Wgen (W,37)); |
263 | 0 | SHA2STEP32 (c, d, e, f, g, h, a, b, UINT32_C (0x81c2c92e), W[38 & 0xf] = \ |
264 | 0 | Wgen (W,38)); |
265 | 0 | SHA2STEP32 (b, c, d, e, f, g, h, a, UINT32_C (0x92722c85), W[39 & 0xf] = \ |
266 | 0 | Wgen (W,39)); |
267 | 0 | SHA2STEP32 (a, b, c, d, e, f, g, h, UINT32_C (0xa2bfe8a1), W[40 & 0xf] = \ |
268 | 0 | Wgen (W,40)); |
269 | 0 | SHA2STEP32 (h, a, b, c, d, e, f, g, UINT32_C (0xa81a664b), W[41 & 0xf] = \ |
270 | 0 | Wgen (W,41)); |
271 | 0 | SHA2STEP32 (g, h, a, b, c, d, e, f, UINT32_C (0xc24b8b70), W[42 & 0xf] = \ |
272 | 0 | Wgen (W,42)); |
273 | 0 | SHA2STEP32 (f, g, h, a, b, c, d, e, UINT32_C (0xc76c51a3), W[43 & 0xf] = \ |
274 | 0 | Wgen (W,43)); |
275 | 0 | SHA2STEP32 (e, f, g, h, a, b, c, d, UINT32_C (0xd192e819), W[44 & 0xf] = \ |
276 | 0 | Wgen (W,44)); |
277 | 0 | SHA2STEP32 (d, e, f, g, h, a, b, c, UINT32_C (0xd6990624), W[45 & 0xf] = \ |
278 | 0 | Wgen (W,45)); |
279 | 0 | SHA2STEP32 (c, d, e, f, g, h, a, b, UINT32_C (0xf40e3585), W[46 & 0xf] = \ |
280 | 0 | Wgen (W,46)); |
281 | 0 | SHA2STEP32 (b, c, d, e, f, g, h, a, UINT32_C (0x106aa070), W[47 & 0xf] = \ |
282 | 0 | Wgen (W,47)); |
283 | 0 | SHA2STEP32 (a, b, c, d, e, f, g, h, UINT32_C (0x19a4c116), W[48 & 0xf] = \ |
284 | 0 | Wgen (W,48)); |
285 | 0 | SHA2STEP32 (h, a, b, c, d, e, f, g, UINT32_C (0x1e376c08), W[49 & 0xf] = \ |
286 | 0 | Wgen (W,49)); |
287 | 0 | SHA2STEP32 (g, h, a, b, c, d, e, f, UINT32_C (0x2748774c), W[50 & 0xf] = \ |
288 | 0 | Wgen (W,50)); |
289 | 0 | SHA2STEP32 (f, g, h, a, b, c, d, e, UINT32_C (0x34b0bcb5), W[51 & 0xf] = \ |
290 | 0 | Wgen (W,51)); |
291 | 0 | SHA2STEP32 (e, f, g, h, a, b, c, d, UINT32_C (0x391c0cb3), W[52 & 0xf] = \ |
292 | 0 | Wgen (W,52)); |
293 | 0 | SHA2STEP32 (d, e, f, g, h, a, b, c, UINT32_C (0x4ed8aa4a), W[53 & 0xf] = \ |
294 | 0 | Wgen (W,53)); |
295 | 0 | SHA2STEP32 (c, d, e, f, g, h, a, b, UINT32_C (0x5b9cca4f), W[54 & 0xf] = \ |
296 | 0 | Wgen (W,54)); |
297 | 0 | SHA2STEP32 (b, c, d, e, f, g, h, a, UINT32_C (0x682e6ff3), W[55 & 0xf] = \ |
298 | 0 | Wgen (W,55)); |
299 | 0 | SHA2STEP32 (a, b, c, d, e, f, g, h, UINT32_C (0x748f82ee), W[56 & 0xf] = \ |
300 | 0 | Wgen (W,56)); |
301 | 0 | SHA2STEP32 (h, a, b, c, d, e, f, g, UINT32_C (0x78a5636f), W[57 & 0xf] = \ |
302 | 0 | Wgen (W,57)); |
303 | 0 | SHA2STEP32 (g, h, a, b, c, d, e, f, UINT32_C (0x84c87814), W[58 & 0xf] = \ |
304 | 0 | Wgen (W,58)); |
305 | 0 | SHA2STEP32 (f, g, h, a, b, c, d, e, UINT32_C (0x8cc70208), W[59 & 0xf] = \ |
306 | 0 | Wgen (W,59)); |
307 | 0 | SHA2STEP32 (e, f, g, h, a, b, c, d, UINT32_C (0x90befffa), W[60 & 0xf] = \ |
308 | 0 | Wgen (W,60)); |
309 | 0 | SHA2STEP32 (d, e, f, g, h, a, b, c, UINT32_C (0xa4506ceb), W[61 & 0xf] = \ |
310 | 0 | Wgen (W,61)); |
311 | 0 | SHA2STEP32 (c, d, e, f, g, h, a, b, UINT32_C (0xbef9a3f7), W[62 & 0xf] = \ |
312 | 0 | Wgen (W,62)); |
313 | 0 | SHA2STEP32 (b, c, d, e, f, g, h, a, UINT32_C (0xc67178f2), W[63 & 0xf] = \ |
314 | 0 | Wgen (W,63)); |
315 | | #else /* ! MHD_FAVOR_SMALL_CODE */ |
316 | | if (1) |
317 | | { |
318 | | unsigned int t; |
319 | | /* K constants array. |
320 | | See FIPS PUB 180-4 paragraph 4.2.2 for K values. */ |
321 | | static const uint32_t K[80] = |
322 | | { UINT32_C (0x428a2f98), UINT32_C (0x71374491), UINT32_C (0xb5c0fbcf), |
323 | | UINT32_C (0xe9b5dba5), UINT32_C (0x3956c25b), UINT32_C (0x59f111f1), |
324 | | UINT32_C (0x923f82a4), UINT32_C (0xab1c5ed5), UINT32_C (0xd807aa98), |
325 | | UINT32_C (0x12835b01), UINT32_C (0x243185be), UINT32_C (0x550c7dc3), |
326 | | UINT32_C (0x72be5d74), UINT32_C (0x80deb1fe), UINT32_C (0x9bdc06a7), |
327 | | UINT32_C (0xc19bf174), UINT32_C (0xe49b69c1), UINT32_C (0xefbe4786), |
328 | | UINT32_C (0x0fc19dc6), UINT32_C (0x240ca1cc), UINT32_C (0x2de92c6f), |
329 | | UINT32_C (0x4a7484aa), UINT32_C (0x5cb0a9dc), UINT32_C (0x76f988da), |
330 | | UINT32_C (0x983e5152), UINT32_C (0xa831c66d), UINT32_C (0xb00327c8), |
331 | | UINT32_C (0xbf597fc7), UINT32_C (0xc6e00bf3), UINT32_C (0xd5a79147), |
332 | | UINT32_C (0x06ca6351), UINT32_C (0x14292967), UINT32_C (0x27b70a85), |
333 | | UINT32_C (0x2e1b2138), UINT32_C (0x4d2c6dfc), UINT32_C (0x53380d13), |
334 | | UINT32_C (0x650a7354), UINT32_C (0x766a0abb), UINT32_C (0x81c2c92e), |
335 | | UINT32_C (0x92722c85), UINT32_C (0xa2bfe8a1), UINT32_C (0xa81a664b), |
336 | | UINT32_C (0xc24b8b70), UINT32_C (0xc76c51a3), UINT32_C (0xd192e819), |
337 | | UINT32_C (0xd6990624), UINT32_C (0xf40e3585), UINT32_C (0x106aa070), |
338 | | UINT32_C (0x19a4c116), UINT32_C (0x1e376c08), UINT32_C (0x2748774c), |
339 | | UINT32_C (0x34b0bcb5), UINT32_C (0x391c0cb3), UINT32_C (0x4ed8aa4a), |
340 | | UINT32_C (0x5b9cca4f), UINT32_C (0x682e6ff3), UINT32_C (0x748f82ee), |
341 | | UINT32_C (0x78a5636f), UINT32_C (0x84c87814), UINT32_C (0x8cc70208), |
342 | | UINT32_C (0x90befffa), UINT32_C (0xa4506ceb), UINT32_C (0xbef9a3f7), |
343 | | UINT32_C (0xc67178f2) }; |
344 | | /* One step of SHA-256 computation with working variables rotation, |
345 | | see FIPS PUB 180-4 paragraph 6.2.2 step 3. |
346 | | * Note: this version of macro reassign all working variable on |
347 | | each step. */ |
348 | | #define SHA2STEP32RV(vA,vB,vC,vD,vE,vF,vG,vH,kt,wt) do { \ |
349 | | uint32_t tmp_h_ = (vH); \ |
350 | | SHA2STEP32 ((vA),(vB),(vC),(vD),(vE),(vF),(vG),tmp_h_,(kt),(wt)); \ |
351 | | (vH) = (vG); \ |
352 | | (vG) = (vF); \ |
353 | | (vF) = (vE); \ |
354 | | (vE) = (vD); \ |
355 | | (vD) = (vC); \ |
356 | | (vC) = (vB); \ |
357 | | (vB) = (vA); \ |
358 | | (vA) = tmp_h_; \ |
359 | | } \ |
360 | | while (0) |
361 | | |
362 | | /* During first 16 steps, before making any calculations on each step, |
363 | | the W element is read from input data buffer as big-endian value and |
364 | | stored in array of W elements. */ |
365 | | for (t = 0; t < 16; ++t) |
366 | | { |
367 | | SHA2STEP32RV (a, b, c, d, e, f, g, h, K[t], \ |
368 | | W[t] = GET_W_FROM_DATA (data, t)); |
369 | | } |
370 | | |
371 | | /* During last 48 steps, before making any calculations on each step, |
372 | | current W element is generated from other W elements of the cyclic buffer |
373 | | and the generated value is stored back in the cyclic buffer. */ |
374 | | for (t = 16; t < 64; ++t) |
375 | | { |
376 | | SHA2STEP32RV (a, b, c, d, e, f, g, h, K[t], W[t & 15] = Wgen (W,t)); |
377 | | } |
378 | | } |
379 | | #endif /* ! MHD_FAVOR_SMALL_CODE */ |
380 | | |
381 | | /* Compute intermediate hash. |
382 | | See FIPS PUB 180-4 paragraph 6.2.2 step 4. */ |
383 | 0 | H[0] += a; |
384 | 0 | H[1] += b; |
385 | 0 | H[2] += c; |
386 | 0 | H[3] += d; |
387 | 0 | H[4] += e; |
388 | 0 | H[5] += f; |
389 | 0 | H[6] += g; |
390 | 0 | H[7] += h; |
391 | 0 | } |
392 | | |
393 | | |
394 | | MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ |
395 | | MHD_FN_PAR_IN_SIZE_ (3, 2) void |
396 | | mhd_SHA256_update (struct mhd_Sha256CtxInt *restrict ctx, |
397 | | size_t size, |
398 | | const uint8_t *restrict data) |
399 | 0 | { |
400 | 0 | unsigned bytes_have; /**< Number of bytes in buffer */ |
401 | |
|
402 | 0 | mhd_assert (0 != size); |
403 | | |
404 | | /* Note: (count & (mhd_SHA256_BLOCK_SIZE-1)) |
405 | | equals (count % mhd_SHA256_BLOCK_SIZE) for this block size. */ |
406 | 0 | bytes_have = (unsigned) (ctx->count & (mhd_SHA256_BLOCK_SIZE - 1)); |
407 | 0 | ctx->count += size; |
408 | |
|
409 | 0 | if (0 != bytes_have) |
410 | 0 | { |
411 | 0 | unsigned bytes_left = mhd_SHA256_BLOCK_SIZE - bytes_have; |
412 | 0 | if (size >= bytes_left) |
413 | 0 | { /* Combine new data with data in the buffer and |
414 | | process full block. */ |
415 | 0 | memcpy (((uint8_t *) ctx->buffer) + bytes_have, |
416 | 0 | data, |
417 | 0 | bytes_left); |
418 | 0 | data += bytes_left; |
419 | 0 | size -= bytes_left; |
420 | 0 | sha256_transform (ctx->H, ctx->buffer); |
421 | 0 | bytes_have = 0; |
422 | 0 | } |
423 | 0 | } |
424 | |
|
425 | 0 | while (mhd_SHA256_BLOCK_SIZE <= size) |
426 | 0 | { /* Process any full blocks of new data directly, |
427 | | without copying to the buffer. */ |
428 | 0 | sha256_transform (ctx->H, data); |
429 | 0 | data += mhd_SHA256_BLOCK_SIZE; |
430 | 0 | size -= mhd_SHA256_BLOCK_SIZE; |
431 | 0 | } |
432 | |
|
433 | 0 | if (0 != size) |
434 | 0 | { /* Copy incomplete block of new data (if any) |
435 | | to the buffer. */ |
436 | 0 | memcpy (((uint8_t *) ctx->buffer) + bytes_have, data, size); |
437 | 0 | } |
438 | 0 | } |
439 | | |
440 | | |
441 | | /** |
442 | | * Size of "length" padding addition in bytes. |
443 | | * See FIPS PUB 180-4 paragraph 5.1.1. |
444 | | */ |
445 | 0 | #define SHA256_SIZE_OF_LEN_ADD (64 / 8) |
446 | | |
447 | | MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ void |
448 | | mhd_SHA256_finish (struct mhd_Sha256CtxInt *restrict ctx, |
449 | | uint8_t digest[mhd_SHA256_DIGEST_SIZE]) |
450 | 0 | { |
451 | 0 | uint64_t num_bits; /**< Number of processed bits */ |
452 | 0 | unsigned bytes_have; /**< Number of bytes in buffer */ |
453 | |
|
454 | 0 | num_bits = ctx->count << 3; |
455 | | /* Note: (count & (mhd_SHA256_BLOCK_SIZE-1)) |
456 | | equal (count % mhd_SHA256_BLOCK_SIZE) for this block size. */ |
457 | 0 | bytes_have = (unsigned) (ctx->count & (mhd_SHA256_BLOCK_SIZE - 1)); |
458 | | |
459 | | /* Input data must be padded with a single bit "1", then with zeros and |
460 | | the finally the length of data in bits must be added as the final bytes |
461 | | of the last block. |
462 | | See FIPS PUB 180-4 paragraph 5.1.1. */ |
463 | | |
464 | | /* Data is always processed in form of bytes (not by individual bits), |
465 | | therefore position of first padding bit in byte is always |
466 | | predefined (0x80). */ |
467 | | /* Buffer always have space at least for one byte (as full buffers are |
468 | | processed immediately). */ |
469 | 0 | ((uint8_t *) ctx->buffer)[bytes_have++] = 0x80; |
470 | |
|
471 | 0 | if (mhd_SHA256_BLOCK_SIZE - bytes_have < SHA256_SIZE_OF_LEN_ADD) |
472 | 0 | { /* No space in current block to put total length of message. |
473 | | Pad current block with zeros and process it. */ |
474 | 0 | if (bytes_have < mhd_SHA256_BLOCK_SIZE) |
475 | 0 | memset (((uint8_t *) ctx->buffer) + bytes_have, 0, |
476 | 0 | mhd_SHA256_BLOCK_SIZE - bytes_have); |
477 | | /* Process full block. */ |
478 | 0 | sha256_transform (ctx->H, ctx->buffer); |
479 | | /* Start new block. */ |
480 | 0 | bytes_have = 0; |
481 | 0 | } |
482 | | |
483 | | /* Pad the rest of the buffer with zeros. */ |
484 | 0 | memset (((uint8_t *) ctx->buffer) + bytes_have, 0, |
485 | 0 | mhd_SHA256_BLOCK_SIZE - SHA256_SIZE_OF_LEN_ADD - bytes_have); |
486 | | /* Put the number of bits in processed message as big-endian value. */ |
487 | 0 | mhd_PUT_64BIT_BE_UNALIGN (ctx->buffer + mhd_SHA256_BLOCK_SIZE_WORDS - 2, |
488 | 0 | num_bits); |
489 | | /* Process full final block. */ |
490 | 0 | sha256_transform (ctx->H, ctx->buffer); |
491 | | |
492 | | /* Put final hash/digest in BE mode */ |
493 | 0 | if (1) |
494 | 0 | { |
495 | 0 | bool use_tmp_buf_to_align_result; |
496 | |
|
497 | | #if defined(mhd_PUT_32BIT_BE_UNALIGNED) |
498 | | use_tmp_buf_to_align_result = false; |
499 | | #elif defined (MHD_FAVOR_SMALL_CODE) |
500 | | use_tmp_buf_to_align_result = true; /* smaller code: eliminated branch below */ |
501 | | #else |
502 | 0 | use_tmp_buf_to_align_result = |
503 | 0 | (0 != ((uintptr_t) digest) % mhd_UINT32_ALIGN); |
504 | 0 | #endif |
505 | 0 | if (use_tmp_buf_to_align_result) |
506 | 0 | { |
507 | | /* If storing of the final result requires aligned address and |
508 | | the destination address is not aligned or compact code is used, |
509 | | store the final digest in aligned temporary buffer first, then |
510 | | copy it to the destination. */ |
511 | 0 | uint32_t alig_dgst[mhd_SHA256_DIGEST_SIZE_WORDS]; |
512 | 0 | mhd_PUT_32BIT_BE (alig_dgst + 0, ctx->H[0]); |
513 | 0 | mhd_PUT_32BIT_BE (alig_dgst + 1, ctx->H[1]); |
514 | 0 | mhd_PUT_32BIT_BE (alig_dgst + 2, ctx->H[2]); |
515 | 0 | mhd_PUT_32BIT_BE (alig_dgst + 3, ctx->H[3]); |
516 | 0 | mhd_PUT_32BIT_BE (alig_dgst + 4, ctx->H[4]); |
517 | 0 | mhd_PUT_32BIT_BE (alig_dgst + 5, ctx->H[5]); |
518 | 0 | mhd_PUT_32BIT_BE (alig_dgst + 6, ctx->H[6]); |
519 | 0 | mhd_PUT_32BIT_BE (alig_dgst + 7, ctx->H[7]); |
520 | | /* Copy result to unaligned destination address */ |
521 | 0 | memcpy (digest, alig_dgst, mhd_SHA256_DIGEST_SIZE); |
522 | 0 | } |
523 | 0 | else |
524 | 0 | { |
525 | | /* Use cast to (void*) here to mute compiler alignment warnings. |
526 | | * Compilers are not smart enough to see that alignment has been checked. */ |
527 | 0 | mhd_PUT_32BIT_BE ((void *) (digest + 0 * mhd_SHA256_BYTES_IN_WORD), \ |
528 | 0 | ctx->H[0]); |
529 | 0 | mhd_PUT_32BIT_BE ((void *) (digest + 1 * mhd_SHA256_BYTES_IN_WORD), \ |
530 | 0 | ctx->H[1]); |
531 | 0 | mhd_PUT_32BIT_BE ((void *) (digest + 2 * mhd_SHA256_BYTES_IN_WORD), \ |
532 | 0 | ctx->H[2]); |
533 | 0 | mhd_PUT_32BIT_BE ((void *) (digest + 3 * mhd_SHA256_BYTES_IN_WORD), \ |
534 | 0 | ctx->H[3]); |
535 | 0 | mhd_PUT_32BIT_BE ((void *) (digest + 4 * mhd_SHA256_BYTES_IN_WORD), \ |
536 | 0 | ctx->H[4]); |
537 | 0 | mhd_PUT_32BIT_BE ((void *) (digest + 5 * mhd_SHA256_BYTES_IN_WORD), \ |
538 | 0 | ctx->H[5]); |
539 | 0 | mhd_PUT_32BIT_BE ((void *) (digest + 6 * mhd_SHA256_BYTES_IN_WORD), \ |
540 | 0 | ctx->H[6]); |
541 | 0 | mhd_PUT_32BIT_BE ((void *) (digest + 7 * mhd_SHA256_BYTES_IN_WORD), \ |
542 | 0 | ctx->H[7]); |
543 | 0 | } |
544 | 0 | } |
545 | | |
546 | | /* Erase potentially sensitive data. */ |
547 | 0 | memset (ctx, 0, sizeof(struct mhd_Sha256CtxInt)); |
548 | 0 | } |
549 | | |
550 | | |
551 | | mhd_DATA_TRUNCATION_RUNTIME_CHECK_RESTORE |