Debian Codegraph @

lib/alg-sha256.c

raw symbols SCIP
1 /*-
2 * Copyright 2005-2016 Colin Percival
3 * Copyright 2016-2018,2021 Alexander Peslyak
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include "crypt-port.h"cxx . . $ `<file>/lib/crypt-port.h`/
29
30 #if INCLUDE_gost_yescryptcxx . . $ `crypt-hashes.h:13:9`! || INCLUDE_yescryptcxx . . $ `crypt-hashes.h:23:9`! || INCLUDE_scryptcxx . . $ `crypt-hashes.h:16:9`! || \
31 INCLUDE_sha256cryptcxx . . $ `crypt-hashes.h:18:9`! || INCLUDE_sm3_yescryptcxx . . $ `crypt-hashes.h:20:9`!
32
33 #include "alg-sha256.h"cxx . . $ `<file>/lib/alg-sha256.h`/
34 #include "byteorder.h"cxx . . $ `<file>/lib/byteorder.h`/
35
36 #ifdef __ICC
37 /* Miscompile with icc 14.0.0 (at least), so don't use restrict there */
38 #define restrict
39 #elif __STDC_VERSION__cxx . . $ `<built-in>:405:9`! >= 199901L
40 /* Have restrict */
41 #elif defined(__GNUC__)
42 #define restrict __restrict
43 #else
44 #define restrict
45 #endif
46
47 /* SHA256 round constants. */
48 static const uint32_tcxx . . $ uint32_t# Krndcxx . . $ Krnd.[64] = {
49 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
50 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
51 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
52 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
53 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
54 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
55 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
56 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
57 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
58 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
59 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
60 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
61 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
62 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
63 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
64 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
65 };
66
67 /* Elementary functions used by SHA256 */
68 #define Chcxx . . $ `lib/alg-sha256.c:68:9`!(x, y, z) ((x & (y ^ z)) ^ z)
69 #if 1 /* Explicit caching/reuse of common subexpression between rounds */
70 #define Majcxx . . $ `lib/alg-sha256.c:70:9`!(x, y, z) (y ^ ((x_xor_y = x ^ y) & y_xor_z))
71 #else /* Let the compiler cache/reuse or not */
72 #define Maj(x, y, z) (y ^ ((x ^ y) & (y ^ z)))
73 #endif
74 #define SHRcxx . . $ `lib/alg-sha256.c:74:9`!(x, n) (x >> n)
75 #define ROTRcxx . . $ `lib/alg-sha256.c:75:9`!(x, n) ((x >> n) | (x << (32 - n)))
76 #define S0cxx . . $ `lib/alg-sha256.c:76:9`!(x) (ROTRcxx . . $ `lib/alg-sha256.c:75:9`!(x, 2) ^ ROTRcxx . . $ `lib/alg-sha256.c:75:9`!(x, 13) ^ ROTRcxx . . $ `lib/alg-sha256.c:75:9`!(x, 22))
77 #define S1cxx . . $ `lib/alg-sha256.c:77:9`!(x) (ROTRcxx . . $ `lib/alg-sha256.c:75:9`!(x, 6) ^ ROTRcxx . . $ `lib/alg-sha256.c:75:9`!(x, 11) ^ ROTRcxx . . $ `lib/alg-sha256.c:75:9`!(x, 25))
78 #define s0cxx . . $ `lib/alg-sha256.c:78:9`!(x) (ROTRcxx . . $ `lib/alg-sha256.c:75:9`!(x, 7) ^ ROTRcxx . . $ `lib/alg-sha256.c:75:9`!(x, 18) ^ SHRcxx . . $ `lib/alg-sha256.c:74:9`!(x, 3))
79 #define s1cxx . . $ `lib/alg-sha256.c:79:9`!(x) (ROTRcxx . . $ `lib/alg-sha256.c:75:9`!(x, 17) ^ ROTRcxx . . $ `lib/alg-sha256.c:75:9`!(x, 19) ^ SHRcxx . . $ `lib/alg-sha256.c:74:9`!(x, 10))
80
81 /* SHA256 round function */
82 #define RNDcxx . . $ `lib/alg-sha256.c:82:9`!(a, b, c, d, e, f, g, h, k) \
83 h += S1cxx . . $ `lib/alg-sha256.c:77:9`!(e) + Chcxx . . $ `lib/alg-sha256.c:68:9`!(e, f, g) + k; \
84 d += h; \
85 h += S0cxx . . $ `lib/alg-sha256.c:76:9`!(a) + Majcxx . . $ `lib/alg-sha256.c:70:9`!(a, b, c); \
86 y_xor_z = x_xor_y;
87
88 /* Adjusted round function for rotating state */
89 #define RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, i, ii) \
90 RNDcxx . . $ `lib/alg-sha256.c:82:9`!(S[(64 - i) % 8], S[(65 - i) % 8], \
91 S[(66 - i) % 8], S[(67 - i) % 8], \
92 S[(68 - i) % 8], S[(69 - i) % 8], \
93 S[(70 - i) % 8], S[(71 - i) % 8], \
94 W[i + ii] + Krnd[i + ii])
95
96 /* Message schedule computation */
97 #define MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, ii, i) \
98 W[i + ii + 16] = s1cxx . . $ `lib/alg-sha256.c:79:9`!(W[i + ii + 14]) + W[i + ii + 9] + s0cxx . . $ `lib/alg-sha256.c:78:9`!(W[i + ii + 1]) + W[i + ii]
99
100 /*
101 * SHA256 block compression function. The 256-bit state is transformed via
102 * the 512-bit input block to produce a new state.
103 */
104 static void
105 SHA256_Transformcxx . . $ SHA256_Transform(6b082d6b8eb13260).(uint32_tcxx . . $ uint32_t# state[static restrict 8],
106 const uint8_tcxx . . $ uint8_t# block[static restrict 64],
107 uint32_tcxx . . $ uint32_t# W[static restrict 64], uint32_tcxx . . $ uint32_t# S[static restrict 8])
108 {
109 int i;
110
111 /* 1. Prepare the first part of the message schedule W. */
112 be32dec_vectcxx . . $ `lib/byteorder.h:160:9`!(W, block, 16);
113
114 /* 2. Initialize working variables. */
115 memcpycxx . . $ memcpy(cc817cb175f282d).(S, state, 32);
116
117 /* 3. Mix. */
118 for (i = 0; i <= 48; i += 16) {
119 uint32_tcxx . . $ uint32_t# x_xor_y, y_xor_z = S[(65 - i) % 8] ^ S[(66 - i) % 8];
120 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 0, i);
121 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 1, i);
122 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 2, i);
123 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 3, i);
124 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 4, i);
125 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 5, i);
126 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 6, i);
127 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 7, i);
128 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 8, i);
129 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 9, i);
130 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 10, i);
131 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 11, i);
132 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 12, i);
133 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 13, i);
134 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 14, i);
135 RNDrcxx . . $ `lib/alg-sha256.c:89:9`!(S, W, 15, i);
136
137 if (i == 48)
138 break;
139
140 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 0, i);
141 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 1, i);
142 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 2, i);
143 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 3, i);
144 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 4, i);
145 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 5, i);
146 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 6, i);
147 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 7, i);
148 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 8, i);
149 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 9, i);
150 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 10, i);
151 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 11, i);
152 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 12, i);
153 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 13, i);
154 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 14, i);
155 MSCHcxx . . $ `lib/alg-sha256.c:97:9`!(W, 15, i);
156 }
157
158 /* 4. Mix local working variables into global state. */
159 state[0] += S[0];
160 state[1] += S[1];
161 state[2] += S[2];
162 state[3] += S[3];
163 state[4] += S[4];
164 state[5] += S[5];
165 state[6] += S[6];
166 state[7] += S[7];
167 }
168
169 static const uint8_tcxx . . $ uint8_t# PADcxx . . $ PAD.[64] = {
170 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
174 };
175
176 /* Add padding and terminating bit-count. */
177 static void
178 SHA256_Padcxx . . $ SHA256_Pad(fabd60dddd27782b).(SHA256_CTXcxx . . $ `lib/alg-sha256.h:43:9`! * ctx, uint32_tcxx . . $ uint32_t# tmp32[static restrict 72])
179 {
180 size_t r;
181
182 /* Figure out how many bytes we have buffered. */
183 r = (ctx->countcxx . . $ $anonymous_type_ab29d8db97f036de_0#count. >> 3) & 0x3f;
184
185 /* Pad to 56 mod 64, transforming if we finish a block en route. */
186 if (r < 56) {
187 /* Pad to 56 mod 64. */
188 memcpycxx . . $ memcpy(cc817cb175f282d).(&ctx->bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf.[r], PADcxx . . $ PAD., 56 - r);
189 } else {
190 /* Finish the current block and mix. */
191 memcpycxx . . $ memcpy(cc817cb175f282d).(&ctx->bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf.[r], PADcxx . . $ PAD., 64 - r);
192 SHA256_Transformcxx . . $ SHA256_Transform(6b082d6b8eb13260).(ctx->statecxx . . $ $anonymous_type_ab29d8db97f036de_0#state., ctx->bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf., &tmp32[0], &tmp32[64]);
193
194 /* The start of the final block is all zeroes. */
195 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&ctx->bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf.[0], 0, 56);
196 }
197
198 /* Add the terminating bit-count. */
199 be64enccxx . . $ `lib/byteorder.h:156:9`!(&ctx->bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf.[56], ctx->countcxx . . $ $anonymous_type_ab29d8db97f036de_0#count.);
200
201 /* Mix in the final block. */
202 SHA256_Transformcxx . . $ SHA256_Transform(6b082d6b8eb13260).(ctx->statecxx . . $ $anonymous_type_ab29d8db97f036de_0#state., ctx->bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf., &tmp32[0], &tmp32[64]);
203 }
204
205 /* Magic initialization constants. */
206 static const uint32_tcxx . . $ uint32_t# initial_statecxx . . $ initial_state.[8] = {
207 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
208 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19
209 };
210
211 /**
212 * SHA256_Init(ctx):
213 * Initialize the SHA256 context ${ctx}.
214 */
215 void
216 SHA256_Initcxx . . $ `lib/alg-sha256.h:39:9`!(SHA256_CTXcxx . . $ `lib/alg-sha256.h:43:9`! * ctx)
217 {
218
219 /* Zero bits processed so far. */
220 ctx->countcxx . . $ $anonymous_type_ab29d8db97f036de_0#count. = 0;
221
222 /* Initialize state. */
223 memcpycxx . . $ memcpy(cc817cb175f282d).(ctx->statecxx . . $ $anonymous_type_ab29d8db97f036de_0#state., initial_statecxx . . $ initial_state., sizeof(initial_statecxx . . $ initial_state.));
224 }
225
226 /**
227 * SHA256_Update(ctx, in, len):
228 * Input ${len} bytes from ${in} into the SHA256 context ${ctx}.
229 */
230 static void
231 _SHA256_Updatecxx . . $ _SHA256_Update(c9c5b6931ca65d46).(SHA256_CTXcxx . . $ `lib/alg-sha256.h:43:9`! * ctx, const void * in, size_t len,
232 uint32_tcxx . . $ uint32_t# tmp32[static restrict 72])
233 {
234 uint32_tcxx . . $ uint32_t# r;
235 const uint8_tcxx . . $ uint8_t# * src = in;
236
237 /* Return immediately if we have nothing to do. */
238 if (len == 0)
239 return;
240
241 /* Number of bytes left in the buffer from previous updates. */
242 r = (ctx->countcxx . . $ $anonymous_type_ab29d8db97f036de_0#count. >> 3) & 0x3f;
243
244 /* Update number of bits. */
245 ctx->countcxx . . $ $anonymous_type_ab29d8db97f036de_0#count. += (uint64_tcxx . . $ uint64_t#)(len) << 3;
246
247 /* Handle the case where we don't need to perform any transforms. */
248 if (len < 64 - r) {
249 memcpycxx . . $ memcpy(cc817cb175f282d).(&ctx->bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf.[r], src, len);
250 return;
251 }
252
253 /* Finish the current block. */
254 memcpycxx . . $ memcpy(cc817cb175f282d).(&ctx->bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf.[r], src, 64 - r);
255 SHA256_Transformcxx . . $ SHA256_Transform(6b082d6b8eb13260).(ctx->statecxx . . $ $anonymous_type_ab29d8db97f036de_0#state., ctx->bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf., &tmp32[0], &tmp32[64]);
256 src += 64 - r;
257 len -= 64 - r;
258
259 /* Perform complete blocks. */
260 while (len >= 64) {
261 SHA256_Transformcxx . . $ SHA256_Transform(6b082d6b8eb13260).(ctx->statecxx . . $ $anonymous_type_ab29d8db97f036de_0#state., src, &tmp32[0], &tmp32[64]);
262 src += 64;
263 len -= 64;
264 }
265
266 /* Copy left over data into buffer. */
267 memcpycxx . . $ memcpy(cc817cb175f282d).(ctx->bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf., src, len);
268 }
269
270 /* Wrapper function for intermediate-values sanitization. */
271 void
272 SHA256_Updatecxx . . $ `lib/alg-sha256.h:40:9`!(SHA256_CTXcxx . . $ `lib/alg-sha256.h:43:9`! * ctx, const void * in, size_t len)
273 {
274 uint32_tcxx . . $ uint32_t# tmp32[72];
275
276 /* Call the real function. */
277 _SHA256_Updatecxx . . $ _SHA256_Update(c9c5b6931ca65d46).(ctx, in, len, tmp32);
278
279 /* Clean the stack. */
280 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(tmp32, 288);
281 }
282
283 /**
284 * SHA256_Final(digest, ctx):
285 * Output the SHA256 hash of the data input to the context ${ctx} into the
286 * buffer ${digest}.
287 */
288 static void
289 _SHA256_Finalcxx . . $ _SHA256_Final(2e9e3f61dfa63799).(uint8_tcxx . . $ uint8_t# digest[32], SHA256_CTXcxx . . $ `lib/alg-sha256.h:43:9`! * ctx,
290 uint32_tcxx . . $ uint32_t# tmp32[static restrict 72])
291 {
292
293 /* Add padding. */
294 SHA256_Padcxx . . $ SHA256_Pad(fabd60dddd27782b).(ctx, tmp32);
295
296 /* Write the hash. */
297 be32enc_vectcxx . . $ `lib/byteorder.h:159:9`!(digest, ctx->statecxx . . $ $anonymous_type_ab29d8db97f036de_0#state., 8);
298 }
299
300 /* Wrapper function for intermediate-values sanitization. */
301 void
302 SHA256_Finalcxx . . $ `lib/alg-sha256.h:41:9`!(uint8_tcxx . . $ uint8_t# digest[32], SHA256_CTXcxx . . $ `lib/alg-sha256.h:43:9`! * ctx)
303 {
304 uint32_tcxx . . $ uint32_t# tmp32[72];
305
306 /* Call the real function. */
307 _SHA256_Finalcxx . . $ _SHA256_Final(2e9e3f61dfa63799).(digest, ctx, tmp32);
308
309 /* Clear the context state. */
310 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(ctx, sizeof(SHA256_CTXcxx . . $ `lib/alg-sha256.h:43:9`!));
311
312 /* Clean the stack. */
313 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(tmp32, 288);
314 }
315
316 /**
317 * SHA256_Buf(in, len, digest):
318 * Compute the SHA256 hash of ${len} bytes from ${in} and write it to ${digest}.
319 */
320 void
321 SHA256_Bufcxx . . $ `lib/alg-sha256.h:42:9`!(const void * in, size_t len, uint8_tcxx . . $ uint8_t# digest[32])
322 {
323 SHA256_CTXcxx . . $ `lib/alg-sha256.h:43:9`! ctx;
324 uint32_tcxx . . $ uint32_t# tmp32[72];
325
326 SHA256_Initcxx . . $ `lib/alg-sha256.h:39:9`!(&ctx);
327 _SHA256_Updatecxx . . $ _SHA256_Update(c9c5b6931ca65d46).(&ctx, in, len, tmp32);
328 _SHA256_Finalcxx . . $ _SHA256_Final(2e9e3f61dfa63799).(digest, &ctx, tmp32);
329
330 /* Clean the stack. */
331 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(&ctx, sizeof(SHA256_CTXcxx . . $ `lib/alg-sha256.h:43:9`!));
332 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(tmp32, 288);
333 }
334
335 #endif /* INCLUDE_gost_yescrypt || INCLUDE_yescrypt || INCLUDE_scrypt ||
336 INCLUDE_sha256crypt || INCLUDE_sm3_yescrypt */
337
338 #if INCLUDE_gost_yescryptcxx . . $ `crypt-hashes.h:13:9`! || INCLUDE_yescryptcxx . . $ `crypt-hashes.h:23:9`! || INCLUDE_scryptcxx . . $ `crypt-hashes.h:16:9`! || \
339 INCLUDE_sm3_yescryptcxx . . $ `crypt-hashes.h:20:9`!
340
341 /**
342 * HMAC_SHA256_Init(ctx, K, Klen):
343 * Initialize the HMAC-SHA256 context ${ctx} with ${Klen} bytes of key from
344 * ${K}.
345 */
346 static void
347 _HMAC_SHA256_Initcxx . . $ _HMAC_SHA256_Init(c1517d5a911e72f3).(HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`! * ctx, const void * _K, size_t Klen,
348 uint32_tcxx . . $ uint32_t# tmp32[static restrict 72], uint8_tcxx . . $ uint8_t# pad[static restrict 64],
349 uint8_tcxx . . $ uint8_t# khash[static restrict 32])
350 {
351 const uint8_tcxx . . $ uint8_t# * K = _K;
352 size_t i;
353
354 /* If Klen > 64, the key is really SHA256(K). */
355 if (Klen > 64) {
356 SHA256_Initcxx . . $ `lib/alg-sha256.h:39:9`!(&ctx->ictxcxx . . $ $anonymous_type_ab29d8db97f036de_1#ictx.);
357 _SHA256_Updatecxx . . $ _SHA256_Update(c9c5b6931ca65d46).(&ctx->ictxcxx . . $ $anonymous_type_ab29d8db97f036de_1#ictx., K, Klen, tmp32);
358 _SHA256_Finalcxx . . $ _SHA256_Final(2e9e3f61dfa63799).(khash, &ctx->ictxcxx . . $ $anonymous_type_ab29d8db97f036de_1#ictx., tmp32);
359 K = khash;
360 Klen = 32;
361 }
362
363 /* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */
364 SHA256_Initcxx . . $ `lib/alg-sha256.h:39:9`!(&ctx->ictxcxx . . $ $anonymous_type_ab29d8db97f036de_1#ictx.);
365 memsetcxx . . $ memset(d1de1e4fb215f1c6).(pad, 0x36, 64);
366 for (i = 0; i < Klen; i++)
367 pad[i] ^= K[i];
368 _SHA256_Updatecxx . . $ _SHA256_Update(c9c5b6931ca65d46).(&ctx->ictxcxx . . $ $anonymous_type_ab29d8db97f036de_1#ictx., pad, 64, tmp32);
369
370 /* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */
371 SHA256_Initcxx . . $ `lib/alg-sha256.h:39:9`!(&ctx->octxcxx . . $ $anonymous_type_ab29d8db97f036de_1#octx.);
372 memsetcxx . . $ memset(d1de1e4fb215f1c6).(pad, 0x5c, 64);
373 for (i = 0; i < Klen; i++)
374 pad[i] ^= K[i];
375 _SHA256_Updatecxx . . $ _SHA256_Update(c9c5b6931ca65d46).(&ctx->octxcxx . . $ $anonymous_type_ab29d8db97f036de_1#octx., pad, 64, tmp32);
376 }
377
378 /* Wrapper function for intermediate-values sanitization. */
379 void
380 HMAC_SHA256_Initcxx . . $ `lib/alg-sha256.h:44:9`!(HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`! * ctx, const void * _K, size_t Klen)
381 {
382 uint32_tcxx . . $ uint32_t# tmp32[72];
383 uint8_tcxx . . $ uint8_t# pad[64];
384 uint8_tcxx . . $ uint8_t# khash[32];
385
386 /* Call the real function. */
387 _HMAC_SHA256_Initcxx . . $ _HMAC_SHA256_Init(c1517d5a911e72f3).(ctx, _K, Klen, tmp32, pad, khash);
388
389 /* Clean the stack. */
390 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(tmp32, 288);
391 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(khash, 32);
392 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(pad, 64);
393 }
394
395 /**
396 * HMAC_SHA256_Update(ctx, in, len):
397 * Input ${len} bytes from ${in} into the HMAC-SHA256 context ${ctx}.
398 */
399 static void
400 _HMAC_SHA256_Updatecxx . . $ _HMAC_SHA256_Update(66e5d2a0ae09ddbf).(HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`! * ctx, const void * in, size_t len,
401 uint32_tcxx . . $ uint32_t# tmp32[static restrict 72])
402 {
403
404 /* Feed data to the inner SHA256 operation. */
405 _SHA256_Updatecxx . . $ _SHA256_Update(c9c5b6931ca65d46).(&ctx->ictxcxx . . $ $anonymous_type_ab29d8db97f036de_1#ictx., in, len, tmp32);
406 }
407
408 /* Wrapper function for intermediate-values sanitization. */
409 void
410 HMAC_SHA256_Updatecxx . . $ `lib/alg-sha256.h:45:9`!(HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`! * ctx, const void * in, size_t len)
411 {
412 uint32_tcxx . . $ uint32_t# tmp32[72];
413
414 /* Call the real function. */
415 _HMAC_SHA256_Updatecxx . . $ _HMAC_SHA256_Update(66e5d2a0ae09ddbf).(ctx, in, len, tmp32);
416
417 /* Clean the stack. */
418 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(tmp32, 288);
419 }
420
421 /**
422 * HMAC_SHA256_Final(digest, ctx):
423 * Output the HMAC-SHA256 of the data input to the context ${ctx} into the
424 * buffer ${digest}.
425 */
426 static void
427 _HMAC_SHA256_Finalcxx . . $ _HMAC_SHA256_Final(65c6606ed0897865).(uint8_tcxx . . $ uint8_t# digest[32], HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`! * ctx,
428 uint32_tcxx . . $ uint32_t# tmp32[static restrict 72], uint8_tcxx . . $ uint8_t# ihash[static restrict 32])
429 {
430
431 /* Finish the inner SHA256 operation. */
432 _SHA256_Finalcxx . . $ _SHA256_Final(2e9e3f61dfa63799).(ihash, &ctx->ictxcxx . . $ $anonymous_type_ab29d8db97f036de_1#ictx., tmp32);
433
434 /* Feed the inner hash to the outer SHA256 operation. */
435 _SHA256_Updatecxx . . $ _SHA256_Update(c9c5b6931ca65d46).(&ctx->octxcxx . . $ $anonymous_type_ab29d8db97f036de_1#octx., ihash, 32, tmp32);
436
437 /* Finish the outer SHA256 operation. */
438 _SHA256_Finalcxx . . $ _SHA256_Final(2e9e3f61dfa63799).(digest, &ctx->octxcxx . . $ $anonymous_type_ab29d8db97f036de_1#octx., tmp32);
439 }
440
441 /* Wrapper function for intermediate-values sanitization. */
442 void
443 HMAC_SHA256_Finalcxx . . $ `lib/alg-sha256.h:46:9`!(uint8_tcxx . . $ uint8_t# digest[32], HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`! * ctx)
444 {
445 uint32_tcxx . . $ uint32_t# tmp32[72];
446 uint8_tcxx . . $ uint8_t# ihash[32];
447
448 /* Call the real function. */
449 _HMAC_SHA256_Finalcxx . . $ _HMAC_SHA256_Final(65c6606ed0897865).(digest, ctx, tmp32, ihash);
450
451 /* Clear the context state. */
452 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(ctx, sizeof(HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`!));
453
454 /* Clean the stack. */
455 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(tmp32, 288);
456 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(ihash, 32);
457 }
458
459 /**
460 * HMAC_SHA256_Buf(K, Klen, in, len, digest):
461 * Compute the HMAC-SHA256 of ${len} bytes from ${in} using the key ${K} of
462 * length ${Klen}, and write the result to ${digest}.
463 */
464 void
465 HMAC_SHA256_Bufcxx . . $ `lib/alg-sha256.h:47:9`!(const void * K, size_t Klen, const void * in, size_t len,
466 uint8_tcxx . . $ uint8_t# digest[32])
467 {
468 HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`! ctx;
469 uint32_tcxx . . $ uint32_t# tmp32[72];
470 uint8_tcxx . . $ uint8_t# tmp8[96];
471
472 _HMAC_SHA256_Initcxx . . $ _HMAC_SHA256_Init(c1517d5a911e72f3).(&ctx, K, Klen, tmp32, &tmp8[0], &tmp8[64]);
473 _HMAC_SHA256_Updatecxx . . $ _HMAC_SHA256_Update(66e5d2a0ae09ddbf).(&ctx, in, len, tmp32);
474 _HMAC_SHA256_Finalcxx . . $ _HMAC_SHA256_Final(65c6606ed0897865).(digest, &ctx, tmp32, &tmp8[0]);
475
476 /* Clean the stack. */
477 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(&ctx, sizeof(HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`!));
478 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(tmp32, 288);
479 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(tmp8, 96);
480 }
481
482 /* Add padding and terminating bit-count, but don't invoke Transform yet. */
483 static int
484 SHA256_Pad_Almostcxx . . $ SHA256_Pad_Almost(2cffd19da587f9c6).(SHA256_CTXcxx . . $ `lib/alg-sha256.h:43:9`! * ctx, uint8_tcxx . . $ uint8_t# len[static restrict 8],
485 uint32_tcxx . . $ uint32_t# tmp32[static restrict 72])
486 {
487 uint32_tcxx . . $ uint32_t# r;
488
489 r = (ctx->countcxx . . $ $anonymous_type_ab29d8db97f036de_0#count. >> 3) & 0x3f;
490 if (r >= 56)
491 return -1;
492
493 /*
494 * Convert length to a vector of bytes -- we do this now rather
495 * than later because the length will change after we pad.
496 */
497 be64enccxx . . $ `lib/byteorder.h:156:9`!(len, ctx->countcxx . . $ $anonymous_type_ab29d8db97f036de_0#count.);
498
499 /* Add 1--56 bytes so that the resulting length is 56 mod 64. */
500 _SHA256_Updatecxx . . $ _SHA256_Update(c9c5b6931ca65d46).(ctx, PADcxx . . $ PAD., 56 - r, tmp32);
501
502 /* Add the terminating bit-count. */
503 ctx->bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf.[63] = len[7];
504 _SHA256_Updatecxx . . $ _SHA256_Update(c9c5b6931ca65d46).(ctx, len, 7, tmp32);
505
506 return 0;
507 }
508
509 /**
510 * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
511 * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
512 * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
513 */
514 void
515 PBKDF2_SHA256cxx . . $ `lib/crypt-port.h:376:9`!(const uint8_tcxx . . $ uint8_t# * passwd, size_t passwdlen, const uint8_tcxx . . $ uint8_t# * salt,
516 size_t saltlen, uint64_tcxx . . $ uint64_t# c, uint8_tcxx . . $ uint8_t# * buf, size_t dkLen)
517 {
518 HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`! Phctx, PShctx, hctx;
519 uint32_tcxx . . $ uint32_t# tmp32[72];
520 unioncxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0# {
521 uint8_tcxx . . $ uint8_t# tmp8cxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#tmp8.[96];
522 uint32_tcxx . . $ uint32_t# statecxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#state.[8];
523 } u;
524 size_t i;
525 uint8_tcxx . . $ uint8_t# ivec[4];
526 uint8_tcxx . . $ uint8_t# U[32];
527 uint8_tcxx . . $ uint8_t# T[32];
528 uint64_tcxx . . $ uint64_t# j;
529 int k;
530 size_t clen;
531
532 /* Sanity-check. */
533 assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(dkLen <= 32 * (size_t)(UINT32_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:124:10`!));
534
535 if (c == 1 && (dkLen & 31) == 0 && (saltlen & 63) <= 51) {
536 uint32_tcxx . . $ uint32_t# oldcount;
537 uint8_tcxx . . $ uint8_t# * ivecp;
538
539 /* Compute HMAC state after processing P and S. */
540 _HMAC_SHA256_Initcxx . . $ _HMAC_SHA256_Init(c1517d5a911e72f3).(&hctx, passwd, passwdlen,
541 tmp32, &u.tmp8cxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#tmp8.[0], &u.tmp8cxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#tmp8.[64]);
542 _HMAC_SHA256_Updatecxx . . $ _HMAC_SHA256_Update(66e5d2a0ae09ddbf).(&hctx, salt, saltlen, tmp32);
543
544 /* Prepare ictx padding. */
545 oldcount = hctx.ictxcxx . . $ $anonymous_type_ab29d8db97f036de_1#ictx..countcxx . . $ $anonymous_type_ab29d8db97f036de_0#count. & (0x3f << 3);
546 _HMAC_SHA256_Updatecxx . . $ _HMAC_SHA256_Update(66e5d2a0ae09ddbf).(&hctx, "\0\0\0", 4, tmp32);
547 if ((hctx.ictxcxx . . $ $anonymous_type_ab29d8db97f036de_1#ictx..countcxx . . $ $anonymous_type_ab29d8db97f036de_0#count. & (0x3f << 3)) < oldcount ||
548 SHA256_Pad_Almostcxx . . $ SHA256_Pad_Almost(2cffd19da587f9c6).(&hctx.ictxcxx . . $ $anonymous_type_ab29d8db97f036de_1#ictx., u.tmp8cxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#tmp8., tmp32))
549 goto generic; /* Can't happen due to saltlen check */
550 ivecp = hctx.ictxcxx . . $ $anonymous_type_ab29d8db97f036de_1#ictx..bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf. + (oldcount >> 3);
551
552 /* Prepare octx padding. */
553 hctx.octxcxx . . $ $anonymous_type_ab29d8db97f036de_1#octx..countcxx . . $ $anonymous_type_ab29d8db97f036de_0#count. += 32 << 3;
554 SHA256_Pad_Almostcxx . . $ SHA256_Pad_Almost(2cffd19da587f9c6).(&hctx.octxcxx . . $ $anonymous_type_ab29d8db97f036de_1#octx., u.tmp8cxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#tmp8., tmp32);
555
556 /* Iterate through the blocks. */
557 for (i = 0; i * 32 < dkLen; i++) {
558 /* Generate INT(i + 1). */
559 be32enccxx . . $ `lib/byteorder.h:154:9`!(ivecp, (uint32_tcxx . . $ uint32_t#)(i + 1));
560
561 /* Compute U_1 = PRF(P, S || INT(i)). */
562 memcpycxx . . $ memcpy(cc817cb175f282d).(u.statecxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#state., hctx.ictxcxx . . $ $anonymous_type_ab29d8db97f036de_1#ictx..statecxx . . $ $anonymous_type_ab29d8db97f036de_0#state., sizeof(u.statecxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#state.));
563 SHA256_Transformcxx . . $ SHA256_Transform(6b082d6b8eb13260).(u.statecxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#state., hctx.ictxcxx . . $ $anonymous_type_ab29d8db97f036de_1#ictx..bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf.,
564 &tmp32[0], &tmp32[64]);
565 be32enc_vectcxx . . $ `lib/byteorder.h:159:9`!(hctx.octxcxx . . $ $anonymous_type_ab29d8db97f036de_1#octx..bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf., u.statecxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#state., 8);
566 memcpycxx . . $ memcpy(cc817cb175f282d).(u.statecxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#state., hctx.octxcxx . . $ $anonymous_type_ab29d8db97f036de_1#octx..statecxx . . $ $anonymous_type_ab29d8db97f036de_0#state., sizeof(u.statecxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#state.));
567 SHA256_Transformcxx . . $ SHA256_Transform(6b082d6b8eb13260).(u.statecxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#state., hctx.octxcxx . . $ $anonymous_type_ab29d8db97f036de_1#octx..bufcxx . . $ $anonymous_type_ab29d8db97f036de_0#buf.,
568 &tmp32[0], &tmp32[64]);
569 be32enc_vectcxx . . $ `lib/byteorder.h:159:9`!(&buf[i * 32], u.statecxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#state., 8);
570 }
571
572 goto cleanup;
573 }
574
575 generic:
576 /* Compute HMAC state after processing P. */
577 _HMAC_SHA256_Initcxx . . $ _HMAC_SHA256_Init(c1517d5a911e72f3).(&Phctx, passwd, passwdlen,
578 tmp32, &u.tmp8cxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#tmp8.[0], &u.tmp8cxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#tmp8.[64]);
579
580 /* Compute HMAC state after processing P and S. */
581 memcpycxx . . $ memcpy(cc817cb175f282d).(&PShctx, &Phctx, sizeof(HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`!));
582 _HMAC_SHA256_Updatecxx . . $ _HMAC_SHA256_Update(66e5d2a0ae09ddbf).(&PShctx, salt, saltlen, tmp32);
583
584 /* Iterate through the blocks. */
585 for (i = 0; i * 32 < dkLen; i++) {
586 /* Generate INT(i + 1). */
587 be32enccxx . . $ `lib/byteorder.h:154:9`!(ivec, (uint32_tcxx . . $ uint32_t#)(i + 1));
588
589 /* Compute U_1 = PRF(P, S || INT(i)). */
590 memcpycxx . . $ memcpy(cc817cb175f282d).(&hctx, &PShctx, sizeof(HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`!));
591 _HMAC_SHA256_Updatecxx . . $ _HMAC_SHA256_Update(66e5d2a0ae09ddbf).(&hctx, ivec, 4, tmp32);
592 _HMAC_SHA256_Finalcxx . . $ _HMAC_SHA256_Final(65c6606ed0897865).(T, &hctx, tmp32, u.tmp8cxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#tmp8.);
593
594 if (c > 1) {
595 /* T_i = U_1 ... */
596 memcpycxx . . $ memcpy(cc817cb175f282d).(U, T, 32);
597
598 for (j = 2; j <= c; j++) {
599 /* Compute U_j. */
600 memcpycxx . . $ memcpy(cc817cb175f282d).(&hctx, &Phctx, sizeof(HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`!));
601 _HMAC_SHA256_Updatecxx . . $ _HMAC_SHA256_Update(66e5d2a0ae09ddbf).(&hctx, U, 32, tmp32);
602 _HMAC_SHA256_Finalcxx . . $ _HMAC_SHA256_Final(65c6606ed0897865).(U, &hctx, tmp32, u.tmp8cxx . . $ _crypt_PBKDF2_SHA256(4ba76405251fce3c).$anonymous_type_0#tmp8.);
603
604 /* ... xor U_j ... */
605 for (k = 0; k < 32; k++)
606 T[k] ^= U[k];
607 }
608 }
609
610 /* Copy as many bytes as necessary into buf. */
611 clen = dkLen - i * 32;
612 if (clen > 32)
613 clen = 32;
614 memcpycxx . . $ memcpy(cc817cb175f282d).(&buf[i * 32], T, clen);
615 }
616
617 /* Clean the stack. */
618 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(&Phctx, sizeof(HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`!));
619 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(&PShctx, sizeof(HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`!));
620 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(U, 32);
621 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(T, 32);
622
623 cleanup:
624 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(&hctx, sizeof(HMAC_SHA256_CTXcxx . . $ `lib/alg-sha256.h:48:9`!));
625 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(tmp32, 288);
626 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(&u, sizeof(u));
627 }
628
629 #endif /* INCLUDE_gost_yescrypt || INCLUDE_yescrypt || INCLUDE_scrypt ||
630 INCLUDE_sm3_yescrypt */