Coverage Report

Created: 2025-10-13 06:05

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