Debian Codegraph @

lib/alg-yescrypt-common.c

raw symbols SCIP
1 /*-
2 * Copyright 2013-2018 Alexander Peslyak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted.
7 *
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
9 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
11 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
12 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
13 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
14 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
16 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
17 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
18 * SUCH DAMAGE.
19 */
20
21 #include "crypt-port.h"cxx . . $ `<file>/lib/crypt-port.h`/
22
23 #if INCLUDE_yescryptcxx . . $ `crypt-hashes.h:23:9`! || INCLUDE_scryptcxx . . $ `crypt-hashes.h:16:9`! || INCLUDE_gost_yescryptcxx . . $ `crypt-hashes.h:13:9`! || \
24 INCLUDE_sm3_yescryptcxx . . $ `crypt-hashes.h:20:9`!
25
26 #include <stdint.h>
27 #include <string.h>
28
29 #include "alg-sha256.h"cxx . . $ `<file>/lib/alg-sha256.h`/
30
31 #define YESCRYPT_INTERNALcxx . . $ `lib/alg-yescrypt-common.c:31:9`!
32 #include "alg-yescrypt.h"cxx . . $ `<file>/lib/alg-yescrypt.h`/
33
34 #pragma GCC diagnostic ignored "-Wconversion"
35 #pragma GCC diagnostic ignored "-Wsign-conversion"
36 #pragma GCC diagnostic ignored "-Wsign-compare"
37 #pragma GCC diagnostic ignored "-Wcast-qual"
38
39 #define BYTES2CHARScxx . . $ `lib/alg-yescrypt-common.c:39:9`!(bytes) ((((bytes) * 8) + 5) / 6)
40
41 #define HASH_SIZEcxx . . $ `lib/alg-yescrypt-common.c:41:9`! sizeof(yescrypt_binary_t) /* bytes */
42 #define HASH_LENcxx . . $ `lib/alg-yescrypt-common.c:42:9`! BYTES2CHARScxx . . $ `lib/alg-yescrypt-common.c:39:9`!(HASH_SIZEcxx . . $ `lib/alg-yescrypt-common.c:41:9`!) /* base-64 chars */
43
44 /*
45 * "$y$", up to 8 params of up to 6 chars each, '$', salt
46 * Alternatively, but that's smaller:
47 * "$7$", 3 params encoded as 1+5+5 chars, salt
48 */
49 #define PREFIX_LENcxx . . $ `lib/alg-yescrypt-common.c:49:9`! (3 + 8 * 6 + 1 + BYTES2CHARScxx . . $ `lib/alg-yescrypt-common.c:39:9`!(32))
50
51 static const uint8_tcxx . . $ uint8_t# atoi64_partialcxx . . $ atoi64_partial.[77] = {
52 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
53 64, 64, 64, 64, 64, 64, 64,
54 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
55 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
56 64, 64, 64, 64, 64, 64,
57 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
58 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63
59 };
60
61 static uint8_tcxx . . $ uint8_t# *encode64_uint32cxx . . $ encode64_uint32(a6a77548108b5da8).(uint8_tcxx . . $ uint8_t# *dst, size_t dstlen,
62 uint32_tcxx . . $ uint32_t# src, uint32_tcxx . . $ uint32_t# min)
63 {
64 uint32_tcxx . . $ uint32_t# start = 0, end = 47, chars = 1, bits = 0;
65
66 if (src < min)
67 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
68 src -= min;
69
70 do {
71 uint32_tcxx . . $ uint32_t# count = (end + 1 - start) << bits;
72 if (src < count)
73 break;
74 if (start >= 63)
75 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
76 start = end + 1;
77 end = start + (62 - end) / 2;
78 src -= count;
79 chars++;
80 bits += 6;
81 } while (1);
82
83 if (dstlen <= chars) /* require room for a NUL terminator */
84 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
85
86 *dst++ = itoa64cxx . . $ `lib/crypt-port.h:502:9`![start + (src >> bits)];
87
88 while (--chars) {
89 bits -= 6;
90 *dst++ = itoa64cxx . . $ `lib/crypt-port.h:502:9`![(src >> bits) & 0x3f];
91 }
92
93 *dst = 0; /* NUL terminate just in case */
94
95 return dst;
96 }
97
98 static inline uint32_tcxx . . $ uint32_t# atoi64cxx . . $ atoi64(ded9d4c05c7b1550).(uint8_tcxx . . $ uint8_t# src)
99 {
100 if (src >= '.' && src <= 'z')
101 return atoi64_partialcxx . . $ atoi64_partial.[src - '.'];
102
103 return 64;
104 }
105
106 static const uint8_tcxx . . $ uint8_t# *decode64_uint32cxx . . $ decode64_uint32(ab2979bbc0030ca4).(uint32_tcxx . . $ uint32_t# *dst,
107 const uint8_tcxx . . $ uint8_t# *src, uint32_tcxx . . $ uint32_t# min)
108 {
109 uint32_tcxx . . $ uint32_t# start = 0, end = 47, chars = 1, bits = 0;
110 uint32_tcxx . . $ uint32_t# c;
111
112 c = atoi64cxx . . $ atoi64(ded9d4c05c7b1550).(*src++);
113 if (c > 63)
114 goto fail;
115
116 *dst = min;
117 while (c > end) {
118 *dst += (end + 1 - start) << bits;
119 start = end + 1;
120 end = start + (62 - end) / 2;
121 chars++;
122 bits += 6;
123 }
124
125 *dst += (c - start) << bits;
126
127 while (--chars) {
128 c = atoi64cxx . . $ atoi64(ded9d4c05c7b1550).(*src++);
129 if (c > 63)
130 goto fail;
131 bits -= 6;
132 *dst += c << bits;
133 }
134
135 return src;
136
137 fail:
138 *dst = 0;
139 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
140 }
141
142 static uint8_tcxx . . $ uint8_t# *encode64_uint32_fixedcxx . . $ encode64_uint32_fixed(a6a77548108b5da8).(uint8_tcxx . . $ uint8_t# *dst, size_t dstlen,
143 uint32_tcxx . . $ uint32_t# src, uint32_tcxx . . $ uint32_t# srcbits)
144 {
145 uint32_tcxx . . $ uint32_t# bits;
146
147 for (bits = 0; bits < srcbits; bits += 6) {
148 if (dstlen < 2)
149 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
150 *dst++ = itoa64cxx . . $ `lib/crypt-port.h:502:9`![src & 0x3f];
151 dstlen--;
152 src >>= 6;
153 }
154
155 if (src || dstlen < 1)
156 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
157
158 *dst = 0; /* NUL terminate just in case */
159
160 return dst;
161 }
162
163 uint8_tcxx . . $ uint8_t# *encode64cxx . . $ `lib/alg-yescrypt.h:347:9`!(uint8_tcxx . . $ uint8_t# *dst, size_t dstlen,
164 const uint8_tcxx . . $ uint8_t# *src, size_t srclen)
165 {
166 size_t i;
167
168 for (i = 0; i < srclen; ) {
169 uint8_tcxx . . $ uint8_t# *dnext;
170 uint32_tcxx . . $ uint32_t# value = 0, bits = 0;
171 do {
172 value |= (uint32_tcxx . . $ uint32_t#)src[i++] << bits;
173 bits += 8;
174 } while (bits < 24 && i < srclen);
175 dnext = encode64_uint32_fixedcxx . . $ encode64_uint32_fixed(a6a77548108b5da8).(dst, dstlen, value, bits);
176 if (!dnext)
177 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
178 dstlen -= dnext - dst;
179 dst = dnext;
180 }
181
182 if (dstlen < 1)
183 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
184
185 *dst = 0; /* NUL terminate just in case */
186
187 return dst;
188 }
189
190 static const uint8_tcxx . . $ uint8_t# *decode64_uint32_fixedcxx . . $ decode64_uint32_fixed(543f406d21c57f2c).(uint32_tcxx . . $ uint32_t# *dst, uint32_tcxx . . $ uint32_t# dstbits,
191 const uint8_tcxx . . $ uint8_t# *src)
192 {
193 uint32_tcxx . . $ uint32_t# bits;
194
195 *dst = 0;
196 for (bits = 0; bits < dstbits; bits += 6) {
197 uint32_tcxx . . $ uint32_t# c = atoi64cxx . . $ atoi64(ded9d4c05c7b1550).(*src++);
198 if (c > 63) {
199 *dst = 0;
200 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
201 }
202 *dst |= c << bits;
203 }
204
205 return src;
206 }
207
208 const uint8_tcxx . . $ uint8_t# *decode64cxx . . $ `lib/alg-yescrypt.h:346:9`!(uint8_tcxx . . $ uint8_t# *dst, size_t *dstlen,
209 const uint8_tcxx . . $ uint8_t# *src, size_t srclen)
210 {
211 size_t dstpos = 0;
212
213 while (dstpos <= *dstlen && srclen) {
214 uint32_tcxx . . $ uint32_t# value = 0, bits = 0;
215 while (srclen--) {
216 uint32_tcxx . . $ uint32_t# c = atoi64cxx . . $ atoi64(ded9d4c05c7b1550).(*src);
217 if (c > 63) {
218 srclen = 0;
219 break;
220 }
221 src++;
222 value |= c << bits;
223 bits += 6;
224 if (bits >= 24)
225 break;
226 }
227 if (!bits)
228 break;
229 if (bits < 12) /* must have at least one full byte */
230 goto fail;
231 while (dstpos++ < *dstlen) {
232 *dst++ = value;
233 value >>= 8;
234 bits -= 8;
235 if (bits < 8) { /* 2 or 4 */
236 if (value) /* must be 0 */
237 goto fail;
238 bits = 0;
239 break;
240 }
241 }
242 if (bits)
243 goto fail;
244 }
245
246 if (!srclen && dstpos <= *dstlen) {
247 *dstlen = dstpos;
248 return src;
249 }
250
251 fail:
252 *dstlen = 0;
253 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
254 }
255
256 typedef enumcxx . . $ $anonymous_type_ee3c738069ad8adb_0# { ENCcxx . . $ ENC. = 1, DECcxx . . $ DEC. = -1 } encrypt_dir_tcxx . . $ encrypt_dir_t#;
257
258 static void memxorcxx . . $ memxor(7587534ddf9ffdd4).(unsigned char *dst, unsigned char *src, size_t size)
259 {
260 while (size--)
261 *dst++ ^= *src++;
262 }
263
264 static void yescrypt_sha256_ciphercxx . . $ yescrypt_sha256_cipher(9307a67e0f9bf321).(unsigned char *data, size_t datalen,
265 const yescrypt_binary_tcxx . . $ yescrypt_binary_t# *key, encrypt_dir_tcxx . . $ encrypt_dir_t# dir)
266 {
267 SHA256_CTXcxx . . $ `lib/alg-sha256.h:43:9`! ctx;
268 unsigned char f[32 + 4];
269 size_t halflen, which;
270 unsigned char mask, round, target;
271
272 if (!datalen)
273 return;
274 if (datalen > 64)
275 datalen = 64;
276
277 halflen = datalen >> 1;
278
279 which = 0; /* offset to half we are working on (0 or halflen) */
280 mask = 0x0f; /* current half's extra nibble mask if datalen is odd */
281
282 round = 0;
283 target = 5; /* 6 rounds due to Jacques Patarin's CRYPTO 2004 paper */
284
285 if (dir == DECcxx . . $ DEC.) {
286 which = halflen; /* even round count, so swap the halves */
287 mask ^= 0xff;
288
289 round = target;
290 target = 0;
291 }
292
293 f[32] = 0;
294 f[33] = sizeof(*key);
295 f[34] = datalen;
296
297 do {
298 SHA256_Initcxx . . $ `lib/alg-sha256.h:39:9`!(&ctx);
299 f[35] = round;
300 SHA256_Updatecxx . . $ `lib/alg-sha256.h:40:9`!(&ctx, &f[32], 4);
301 SHA256_Updatecxx . . $ `lib/alg-sha256.h:40:9`!(&ctx, key, sizeof(*key));
302 SHA256_Updatecxx . . $ `lib/alg-sha256.h:40:9`!(&ctx, &data[which], halflen);
303 if (datalen & 1) {
304 f[0] = data[datalen - 1] & mask;
305 SHA256_Updatecxx . . $ `lib/alg-sha256.h:40:9`!(&ctx, f, 1);
306 }
307 SHA256_Finalcxx . . $ `lib/alg-sha256.h:41:9`!(f, &ctx);
308 which ^= halflen;
309 memxorcxx . . $ memxor(7587534ddf9ffdd4).(&data[which], f, halflen);
310 if (datalen & 1) {
311 mask ^= 0xff;
312 data[datalen - 1] ^= f[halflen] & mask;
313 }
314 if (round == target)
315 break;
316 // coverity[overflow_const]
317 round += dir;
318 } while (1);
319
320 /* ctx is presumably zeroized by SHA256_Final() */
321 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(f, sizeof(f));
322 }
323
324 uint8_tcxx . . $ uint8_t# *yescrypt_rcxx . . $ `lib/crypt-port.h:389:9`!(const yescrypt_shared_tcxx . . $ yescrypt_shared_t# *shared, yescrypt_local_tcxx . . $ yescrypt_local_t# *local,
325 const uint8_tcxx . . $ uint8_t# *passwd, size_t passwdlen,
326 const uint8_tcxx . . $ uint8_t# *setting,
327 const yescrypt_binary_tcxx . . $ yescrypt_binary_t# *key,
328 uint8_tcxx . . $ uint8_t# *buf, size_t buflen)
329 {
330 unsigned char saltbin[64], hashbin[32];
331 const uint8_tcxx . . $ uint8_t# *src, *saltstr, *salt;
332 uint8_tcxx . . $ uint8_t# *dst;
333 size_t need, prefixlen, saltstrlen, saltlen;
334 yescrypt_params_tcxx . . $ yescrypt_params_t# params = { .p = 1 };
335
336 if (setting[0] != '$' ||
337 (setting[1] != '7' && setting[1] != 'y') ||
338 setting[2] != '$')
339 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
340 src = setting + 3;
341
342 if (setting[1] == '7') {
343 uint32_tcxx . . $ uint32_t# N_log2 = atoi64cxx . . $ atoi64(ded9d4c05c7b1550).(*src++);
344 if (N_log2 < 1 || N_log2 > 63)
345 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
346 params.Ncxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#N. = (uint64_tcxx . . $ uint64_t#)1 << N_log2;
347
348 src = decode64_uint32_fixedcxx . . $ decode64_uint32_fixed(543f406d21c57f2c).(&params.rcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#r., 30, src);
349 if (!src)
350 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
351
352 src = decode64_uint32_fixedcxx . . $ decode64_uint32_fixed(543f406d21c57f2c).(&params.pcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#p., 30, src);
353 if (!src)
354 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
355
356 if (key)
357 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
358 } else {
359 uint32_tcxx . . $ uint32_t# flavor, N_log2;
360
361 src = decode64_uint32cxx . . $ decode64_uint32(ab2979bbc0030ca4).(&flavor, src, 0);
362 if (!src)
363 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
364
365 if (flavor < YESCRYPT_RWcxx . . $ `lib/alg-yescrypt.h:90:9`!) {
366 params.flagscxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#flags. = flavor;
367 } else if (flavor <= YESCRYPT_RWcxx . . $ `lib/alg-yescrypt.h:90:9`! + (YESCRYPT_RW_FLAVOR_MASKcxx . . $ `lib/alg-yescrypt.h:114:9`! >> 2)) {
368 params.flagscxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#flags. = YESCRYPT_RWcxx . . $ `lib/alg-yescrypt.h:90:9`! + ((flavor - YESCRYPT_RWcxx . . $ `lib/alg-yescrypt.h:90:9`!) << 2);
369 } else {
370 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
371 }
372
373 src = decode64_uint32cxx . . $ decode64_uint32(ab2979bbc0030ca4).(&N_log2, src, 1);
374 if (!src || N_log2 > 63)
375 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
376 params.Ncxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#N. = (uint64_tcxx . . $ uint64_t#)1 << N_log2;
377
378 src = decode64_uint32cxx . . $ decode64_uint32(ab2979bbc0030ca4).(&params.rcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#r., src, 1);
379 if (!src)
380 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
381
382 if (*src != '$') {
383 uint32_tcxx . . $ uint32_t# have;
384
385 src = decode64_uint32cxx . . $ decode64_uint32(ab2979bbc0030ca4).(&have, src, 1);
386 if (!src)
387 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
388
389 if (have & 1) {
390 src = decode64_uint32cxx . . $ decode64_uint32(ab2979bbc0030ca4).(&params.pcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#p., src, 2);
391 if (!src)
392 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
393 }
394
395 if (have & 2) {
396 src = decode64_uint32cxx . . $ decode64_uint32(ab2979bbc0030ca4).(&params.tcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#t., src, 1);
397 if (!src)
398 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
399 }
400
401 if (have & 4) {
402 src = decode64_uint32cxx . . $ decode64_uint32(ab2979bbc0030ca4).(&params.gcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#g., src, 1);
403 if (!src)
404 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
405 }
406
407 if (have & 8) {
408 uint32_tcxx . . $ uint32_t# NROM_log2;
409 src = decode64_uint32cxx . . $ decode64_uint32(ab2979bbc0030ca4).(&NROM_log2, src, 1);
410 if (!src || NROM_log2 > 63)
411 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
412 params.NROMcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#NROM. = (uint64_tcxx . . $ uint64_t#)1 << NROM_log2;
413 }
414 }
415
416 if (*src++ != '$')
417 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
418 }
419
420 prefixlen = src - setting;
421
422 saltstr = src;
423 src = (uint8_tcxx . . $ uint8_t# *)strrchrcxx . . $ `<external>/13506128340496143423/string.h:295:11`!((char *)saltstr, '$');
424 if (src)
425 saltstrlen = src - saltstr;
426 else
427 saltstrlen = strlencxx . . $ strlen(751346f8406fd082).((char *)saltstr);
428
429 if (setting[1] == '7') {
430 salt = saltstr;
431 saltlen = saltstrlen;
432 } else {
433 const uint8_tcxx . . $ uint8_t# *saltend;
434
435 saltlen = sizeof(saltbin);
436 saltend = decode64cxx . . $ `lib/alg-yescrypt.h:346:9`!(saltbin, &saltlen, saltstr, saltstrlen);
437
438 if (!saltend || (size_t)(saltend - saltstr) != saltstrlen)
439 goto fail;
440
441 salt = saltbin;
442
443 if (key)
444 yescrypt_sha256_ciphercxx . . $ yescrypt_sha256_cipher(9307a67e0f9bf321).(saltbin, saltlen, key, ENCcxx . . $ ENC.);
445 }
446
447 need = prefixlen + saltstrlen + 1 + HASH_LENcxx . . $ `lib/alg-yescrypt-common.c:42:9`! + 1;
448 if (need > buflen || need < saltstrlen)
449 goto fail;
450
451 if (yescrypt_kdfcxx . . $ `lib/crypt-port.h:388:9`!(shared, local, passwd, passwdlen, salt, saltlen,
452 &params, hashbin, sizeof(hashbin)))
453 goto fail;
454
455 if (key) {
456 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(saltbin, sizeof(saltbin));
457 yescrypt_sha256_ciphercxx . . $ yescrypt_sha256_cipher(9307a67e0f9bf321).(hashbin, sizeof(hashbin), key, ENCcxx . . $ ENC.);
458 }
459
460 dst = buf;
461 memcpycxx . . $ memcpy(cc817cb175f282d).(dst, setting, prefixlen + saltstrlen);
462 dst += prefixlen + saltstrlen;
463 *dst++ = '$';
464
465 dst = encode64cxx . . $ `lib/alg-yescrypt.h:347:9`!(dst, buflen - (dst - buf), hashbin, sizeof(hashbin));
466 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(hashbin, sizeof(hashbin));
467 if (!dst || dst >= buf + buflen)
468 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
469
470 *dst = 0; /* NUL termination */
471
472 return buf;
473
474 fail:
475 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(saltbin, sizeof(saltbin));
476 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(hashbin, sizeof(hashbin));
477 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
478 }
479
480 uint8_tcxx . . $ uint8_t# *yescryptcxx . . $ `lib/crypt-port.h:378:9`!(const uint8_tcxx . . $ uint8_t# *passwd, const uint8_tcxx . . $ uint8_t# *setting)
481 {
482 /* prefix, '$', hash, NUL */
483 static uint8_tcxx . . $ uint8_t# buf[PREFIX_LENcxx . . $ `lib/alg-yescrypt-common.c:49:9`! + 1 + HASH_LENcxx . . $ `lib/alg-yescrypt-common.c:42:9`! + 1];
484 yescrypt_local_tcxx . . $ yescrypt_local_t# local;
485 uint8_tcxx . . $ uint8_t# *retval;
486
487 if (yescrypt_init_localcxx . . $ `lib/crypt-port.h:386:9`!(&local))
488 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
489 retval = yescrypt_rcxx . . $ `lib/crypt-port.h:389:9`!(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, &local,
490 passwd, strlencxx . . $ strlen(751346f8406fd082).((char *)passwd), setting, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, buf, sizeof(buf));
491 if (yescrypt_free_localcxx . . $ `lib/crypt-port.h:384:9`!(&local))
492 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
493 return retval;
494 }
495
496 uint8_tcxx . . $ uint8_t# *yescrypt_reencryptcxx . . $ `lib/crypt-port.h:390:9`!(uint8_tcxx . . $ uint8_t# *hash,
497 const yescrypt_binary_tcxx . . $ yescrypt_binary_t# *from_key,
498 const yescrypt_binary_tcxx . . $ yescrypt_binary_t# *to_key)
499 {
500 uint8_tcxx . . $ uint8_t# *retval = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, *saltstart, *hashstart;
501 const uint8_tcxx . . $ uint8_t# *hashend;
502 unsigned char saltbin[64], hashbin[32];
503 size_t saltstrlen, saltlen = 0, hashlen;
504
505 if (strncmpcxx . . $ strncmp(fd278af2f295fa2e).((char *)hash, "$y$", 3))
506 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
507
508 saltstart = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
509 hashstart = (uint8_tcxx . . $ uint8_t# *)strrchrcxx . . $ `<external>/13506128340496143423/string.h:295:11`!((char *)hash, '$');
510 if (hashstart) {
511 if (hashstart > (uint8_tcxx . . $ uint8_t# *)hash) {
512 saltstart = hashstart - 1;
513 while (*saltstart != '$' && saltstart > hash)
514 saltstart--;
515 if (*saltstart == '$')
516 saltstart++;
517 }
518 hashstart++;
519 } else {
520 hashstart = hash;
521 }
522 saltstrlen = saltstart ? (hashstart - 1 - saltstart) : 0;
523 if (saltstrlen > BYTES2CHARScxx . . $ `lib/alg-yescrypt-common.c:39:9`!(64) ||
524 strlencxx . . $ strlen(751346f8406fd082).((char *)hashstart) != HASH_LENcxx . . $ `lib/alg-yescrypt-common.c:42:9`!)
525 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
526
527 if (saltstrlen) {
528 const uint8_tcxx . . $ uint8_t# *saltend;
529 saltlen = sizeof(saltbin);
530 saltend = decode64cxx . . $ `lib/alg-yescrypt.h:346:9`!(saltbin, &saltlen, saltstart, saltstrlen);
531 if (!saltend || *saltend != '$' || saltlen < 1 || saltlen > 64)
532 goto out;
533
534 if (from_key)
535 yescrypt_sha256_ciphercxx . . $ yescrypt_sha256_cipher(9307a67e0f9bf321).(saltbin, saltlen, from_key, ENCcxx . . $ ENC.);
536 if (to_key)
537 yescrypt_sha256_ciphercxx . . $ yescrypt_sha256_cipher(9307a67e0f9bf321).(saltbin, saltlen, to_key, DECcxx . . $ DEC.);
538 }
539
540 hashlen = sizeof(hashbin);
541 hashend = decode64cxx . . $ `lib/alg-yescrypt.h:346:9`!(hashbin, &hashlen, hashstart, HASH_LENcxx . . $ `lib/alg-yescrypt-common.c:42:9`!);
542 if (!hashend || *hashend || hashlen != sizeof(hashbin))
543 goto out;
544
545 if (from_key)
546 yescrypt_sha256_ciphercxx . . $ yescrypt_sha256_cipher(9307a67e0f9bf321).(hashbin, hashlen, from_key, DECcxx . . $ DEC.);
547 if (to_key)
548 yescrypt_sha256_ciphercxx . . $ yescrypt_sha256_cipher(9307a67e0f9bf321).(hashbin, hashlen, to_key, ENCcxx . . $ ENC.);
549
550 if (saltstrlen) {
551 if (!encode64cxx . . $ `lib/alg-yescrypt.h:347:9`!(saltstart, saltstrlen + 1, saltbin, saltlen))
552 goto out; /* can't happen */
553 *(saltstart + saltstrlen) = '$';
554 }
555
556 if (!encode64cxx . . $ `lib/alg-yescrypt.h:347:9`!(hashstart, HASH_LENcxx . . $ `lib/alg-yescrypt-common.c:42:9`! + 1, hashbin, hashlen))
557 goto out; /* can't happen */
558
559 retval = hash;
560
561 out:
562 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(saltbin, sizeof(saltbin));
563 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(hashbin, sizeof(hashbin));
564
565 return retval;
566 }
567
568 static uint32_tcxx . . $ uint32_t# N2log2cxx . . $ N2log2(ea88b852781e8a38).(uint64_tcxx . . $ uint64_t# N)
569 {
570 uint32_tcxx . . $ uint32_t# N_log2;
571
572 if (N < 2)
573 return 0;
574
575 N_log2 = 2;
576 while (N >> N_log2 != 0)
577 N_log2++;
578 N_log2--;
579
580 if (N >> N_log2 != 1)
581 return 0;
582
583 return N_log2;
584 }
585
586 uint8_tcxx . . $ uint8_t# *yescrypt_encode_params_rcxx . . $ `lib/crypt-port.h:383:9`!(const yescrypt_params_tcxx . . $ yescrypt_params_t# *params,
587 const uint8_tcxx . . $ uint8_t# *src, size_t srclen,
588 uint8_tcxx . . $ uint8_t# *buf, size_t buflen)
589 {
590 uint32_tcxx . . $ uint32_t# flavor, N_log2, NROM_log2, have;
591 uint8_tcxx . . $ uint8_t# *dst;
592
593 if (srclen > SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:222:11`! / 16)
594 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
595
596 if (params->flagscxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#flags. < YESCRYPT_RWcxx . . $ `lib/alg-yescrypt.h:90:9`!) {
597 flavor = params->flagscxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#flags.;
598 } else if ((params->flagscxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#flags. & YESCRYPT_MODE_MASKcxx . . $ `lib/alg-yescrypt.h:113:9`!) == YESCRYPT_RWcxx . . $ `lib/alg-yescrypt.h:90:9`! &&
599 params->flagscxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#flags. <= (YESCRYPT_RWcxx . . $ `lib/alg-yescrypt.h:90:9`! | YESCRYPT_RW_FLAVOR_MASKcxx . . $ `lib/alg-yescrypt.h:114:9`!)) {
600 flavor = YESCRYPT_RWcxx . . $ `lib/alg-yescrypt.h:90:9`! + (params->flagscxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#flags. >> 2);
601 } else {
602 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
603 }
604
605 N_log2 = N2log2cxx . . $ N2log2(ea88b852781e8a38).(params->Ncxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#N.);
606 if (!N_log2)
607 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
608
609 NROM_log2 = N2log2cxx . . $ N2log2(ea88b852781e8a38).(params->NROMcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#NROM.);
610 if (params->NROMcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#NROM. && !NROM_log2)
611 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
612
613 if ((uint64_tcxx . . $ uint64_t#)params->rcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#r. * (uint64_tcxx . . $ uint64_t#)params->pcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#p. >= (1U << 30))
614 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
615
616 dst = buf;
617 *dst++ = '$';
618 *dst++ = 'y';
619 *dst++ = '$';
620
621 dst = encode64_uint32cxx . . $ encode64_uint32(a6a77548108b5da8).(dst, buflen - (dst - buf), flavor, 0);
622 if (!dst)
623 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
624
625 dst = encode64_uint32cxx . . $ encode64_uint32(a6a77548108b5da8).(dst, buflen - (dst - buf), N_log2, 1);
626 if (!dst)
627 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
628
629 dst = encode64_uint32cxx . . $ encode64_uint32(a6a77548108b5da8).(dst, buflen - (dst - buf), params->rcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#r., 1);
630 if (!dst)
631 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
632
633 have = 0;
634 if (params->pcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#p. != 1)
635 have |= 1;
636 if (params->tcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#t.)
637 have |= 2;
638 if (params->gcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#g.)
639 have |= 4;
640 if (NROM_log2)
641 have |= 8;
642
643 if (have) {
644 dst = encode64_uint32cxx . . $ encode64_uint32(a6a77548108b5da8).(dst, buflen - (dst - buf), have, 1);
645 if (!dst)
646 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
647 }
648
649 if (params->pcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#p. != 1) {
650 dst = encode64_uint32cxx . . $ encode64_uint32(a6a77548108b5da8).(dst, buflen - (dst - buf), params->pcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#p., 2);
651 if (!dst)
652 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
653 }
654
655 if (params->tcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#t.) {
656 dst = encode64_uint32cxx . . $ encode64_uint32(a6a77548108b5da8).(dst, buflen - (dst - buf), params->tcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#t., 1);
657 if (!dst)
658 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
659 }
660
661 if (params->gcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#g.) {
662 dst = encode64_uint32cxx . . $ encode64_uint32(a6a77548108b5da8).(dst, buflen - (dst - buf), params->gcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#g., 1);
663 if (!dst)
664 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
665 }
666
667 if (NROM_log2) {
668 dst = encode64_uint32cxx . . $ encode64_uint32(a6a77548108b5da8).(dst, buflen - (dst - buf), NROM_log2, 1);
669 if (!dst)
670 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
671 }
672
673 if (dst >= buf + buflen)
674 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
675
676 *dst++ = '$';
677
678 dst = encode64cxx . . $ `lib/alg-yescrypt.h:347:9`!(dst, buflen - (dst - buf), src, srclen);
679 if (!dst || dst >= buf + buflen)
680 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
681
682 *dst = 0; /* NUL termination */
683
684 return buf;
685 }
686
687 uint8_tcxx . . $ uint8_t# *yescrypt_encode_paramscxx . . $ `lib/crypt-port.h:382:9`!(const yescrypt_params_tcxx . . $ yescrypt_params_t# *params,
688 const uint8_tcxx . . $ uint8_t# *src, size_t srclen)
689 {
690 /* prefix, NUL */
691 static uint8_tcxx . . $ uint8_t# buf[PREFIX_LENcxx . . $ `lib/alg-yescrypt-common.c:49:9`! + 1];
692 return yescrypt_encode_params_rcxx . . $ `lib/crypt-port.h:383:9`!(params, src, srclen, buf, sizeof(buf));
693 }
694
695 int crypto_scryptcxx . . $ `lib/crypt-port.h:377:9`!(const uint8_tcxx . . $ uint8_t# *passwd, size_t passwdlen,
696 const uint8_tcxx . . $ uint8_t# *salt, size_t saltlen, uint64_tcxx . . $ uint64_t# N, uint32_tcxx . . $ uint32_t# r, uint32_tcxx . . $ uint32_t# p,
697 uint8_tcxx . . $ uint8_t# *buf, size_t buflen)
698 {
699 yescrypt_local_tcxx . . $ yescrypt_local_t# local;
700 yescrypt_params_tcxx . . $ yescrypt_params_t# params = { .flags = 0, .N = N, .r = r, .p = p };
701 int retval;
702
703 if (yescrypt_init_localcxx . . $ `lib/crypt-port.h:386:9`!(&local))
704 return -1;
705 retval = yescrypt_kdfcxx . . $ `lib/crypt-port.h:388:9`!(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, &local,
706 passwd, passwdlen, salt, saltlen, &params, buf, buflen);
707 if (yescrypt_free_localcxx . . $ `lib/crypt-port.h:384:9`!(&local))
708 return -1;
709 return retval;
710 }
711
712 #endif /* INCLUDE_yescrypt || INCLUDE_scrypt || INCLUDE_gost_yescrypt ||
713 INCLUDE_sm3_yescrypt */