Debian Codegraph @

lib/crypt.c

raw symbols SCIP
1 /* High-level libcrypt interfaces.
2
3 Copyright 2007-2017 Thorsten Kukuk and Zack Weinberg
4 Copyright 2018-2025 Björn Esser
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License
8 as published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
10
11 This library 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
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20 #include "crypt-port.h"cxx . . $ `<file>/lib/crypt-port.h`/
21
22 #include <errno.h>
23 #include <stdlib.h>
24
25 /* The internal storage area within struct crypt_data is used as
26 follows. We don't know what alignment the algorithm modules will
27 need for their scratch data, so give it the maximum natural
28 alignment. Note that the C11 alignas() specifier can't be applied
29 directly to a struct type, but it can be applied to the first field
30 of a struct, which effectively forces alignment of the entire
31 struct, since the first field must always have offset 0. */
32 struct crypt_internalcxx . . $ crypt_internal#
33 {
34 /* Alignment critical data members. */
35 char alignascxx . . $ `config.h:302:9`! (alignofcxx . . $ `config.h:306:9`! (max_align_t)) alg_specificcxx . . $ crypt_internal#alg_specific.[ALG_SPECIFIC_SIZEcxx . . $ `lib/crypt-port.h:510:9`!];
36 /* Not alignment critical data members. */
37 char outputcxx . . $ crypt_internal#output.[CRYPT_OUTPUT_SIZEcxx . . $ `crypt.h:32:9`!];
38 };
39
40 static_assertcxx . . $ `<external>/4398592474888995393/assert.h:210:10`!(sizeof (struct crypt_internalcxx . . $ crypt_internal#) + alignofcxx . . $ `config.h:306:9`! (struct crypt_internal)
41 <= CRYPT_DATA_INTERNAL_SIZEcxx . . $ `crypt.h:69:9`!,
42 "crypt_data.internal is too small for crypt_internal");
43
44 /* struct crypt_data is allocated by application code and contains
45 only char-typed fields, so its 'internal' field may not be
46 sufficiently aligned. */
47 static inline struct crypt_internalcxx . . $ crypt_internal# *
48 get_internalcxx . . $ get_internal(fb2c37d89851a42e). (struct crypt_datacxx . . $ crypt_data# *data)
49 {
50 uintptr_tcxx . . $ uintptr_t# internalp = (uintptr_tcxx . . $ uintptr_t#) data->internalcxx . . $ crypt_data#internal.;
51 const uintptr_tcxx . . $ uintptr_t# align = alignofcxx . . $ `config.h:306:9`! (struct crypt_internal);
52 internalp = (internalp + align - 1) & ~(align - 1);
53 return (struct crypt_internalcxx . . $ crypt_internal# *)internalp;
54 }
55
56 typedef void (*crypt_fncxx . . $ crypt_fn#) (const char *phrase, size_t phr_size,
57 const char *setting, size_t set_size,
58 uint8_tcxx . . $ uint8_t# *output, size_t out_size,
59 void *scratch, size_t scr_size);
60
61 typedef void (*gensalt_fncxx . . $ gensalt_fn#) (unsigned long count,
62 const uint8_tcxx . . $ uint8_t# *rbytes, size_t nrbytes,
63 uint8_tcxx . . $ uint8_t# *output, size_t output_size);
64
65 struct hashfncxx . . $ hashfn#
66 {
67 const char *prefixcxx . . $ hashfn#prefix.;
68 size_t plencxx . . $ hashfn#plen.;
69 crypt_fncxx . . $ crypt_fn# cryptcxx . . $ `crypt-symbol-vers.h:60:9`!;
70 gensalt_fncxx . . $ gensalt_fn# gensaltcxx . . $ hashfn#gensalt.;
71 /* The type of this field is unsigned char to ensure that it cannot
72 be set larger than the size of an internal buffer in crypt_gensalt_rn. */
73 unsigned char nrbytescxx . . $ hashfn#nrbytes.;
74 unsigned char is_strongcxx . . $ hashfn#is_strong.;
75 };
76
77 static const struct hashfncxx . . $ hashfn# hash_algorithmscxx . . $ hash_algorithms.[] =
78 {
79 HASH_ALGORITHM_TABLE_ENTRIEScxx . . $ `crypt-hashes.h:154:9`!
80 };
81
82 #if INCLUDE_descryptcxx . . $ `crypt-hashes.h:12:9`! || INCLUDE_bigcryptcxx . . $ `crypt-hashes.h:10:9`!
83 static int
84 is_des_salt_charcxx . . $ is_des_salt_char(784f3405e7e545be). (char c)
85 {
86 return ((c >= 'a' && c <= 'z') ||
87 (c >= 'A' && c <= 'Z') ||
88 (c >= '0' && c <= '9') ||
89 c == '.' || c == '/');
90 }
91 #endif
92
93 static const struct hashfncxx . . $ hashfn# *
94 get_hashfncxx . . $ get_hashfn(6a662cb7fac9e190). (const char *setting)
95 {
96 const struct hashfncxx . . $ hashfn# *h;
97 for (h = hash_algorithmscxx . . $ hash_algorithms.; h->prefixcxx . . $ hashfn#prefix.; h++)
98 {
99 if (h->plencxx . . $ hashfn#plen. > 0)
100 {
101 if (!strncmpcxx . . $ strncmp(fd278af2f295fa2e). (setting, h->prefixcxx . . $ hashfn#prefix., h->plencxx . . $ hashfn#plen.))
102 return h;
103 }
104 #if INCLUDE_descryptcxx . . $ `crypt-hashes.h:12:9`! || INCLUDE_bigcryptcxx . . $ `crypt-hashes.h:10:9`!
105 else
106 {
107 if (setting[0] == '\0' ||
108 (is_des_salt_charcxx . . $ is_des_salt_char(784f3405e7e545be). (setting[0]) && is_des_salt_charcxx . . $ is_des_salt_char(784f3405e7e545be). (setting[1])))
109 return h;
110 }
111 #endif
112 }
113 return 0;
114 }
115
116 /* Check a setting string for generic validity, according to the rule
117 stated in crypt(5):
118
119 "Hashed passphrases are always entirely printable ASCII, and do
120 not contain any whitespace or the characters ':', ';', '*', '!',
121 or '\\'. (These characters are used as delimiters and special
122 markers in the passwd(5) and shadow(5) files.)"
123
124 There is a precautionary case for rejecting additional ASCII
125 punctuation, particularly other characters often given syntactic
126 significance in configuration files, such as " ' and #. However,
127 this check didn't used to exist at all, and some of the hash
128 function implementations don't restrict the set of byte values they
129 they will accept in their setting strings (particularly in the salt
130 component) either. Thus, to maintain compatibility with the widest
131 variety of existing hashed passphrases, we are only enforcing the
132 documented rule for now.
133
134 See <https://github.com/besser82/libxcrypt/issues/135> for
135 additional discussion. */
136 static int
137 check_badsalt_charscxx . . $ check_badsalt_chars(5c4306d8553b3696). (const char *setting)
138 {
139 size_t i;
140
141 for (i = 0; setting[i] != '\0'; i++)
142 if ((unsigned char) setting[i] <= 0x20 ||
143 (unsigned char) setting[i] >= 0x7f)
144 return 1;
145
146 return strcspncxx . . $ strcspn(335c1dc86f5c867f). (setting, "!*:;\\") != i;
147 }
148
149 static void
150 do_cryptcxx . . $ do_crypt(8e811c27bb635ed4). (const char *phrase, const char *setting, struct crypt_datacxx . . $ crypt_data# *data)
151 {
152 struct crypt_internalcxx . . $ crypt_internal# *cint = get_internalcxx . . $ get_internal(fb2c37d89851a42e). (data);
153 memsetcxx . . $ memset(d1de1e4fb215f1c6). (cint->outputcxx . . $ crypt_internal#output., 0, sizeof cint->outputcxx . . $ crypt_internal#output.);
154 make_failure_tokencxx . . $ `lib/crypt-port.h:325:9`! (setting, cint->outputcxx . . $ crypt_internal#output., sizeof cint->outputcxx . . $ crypt_internal#output.);
155
156 if (!phrase || !setting)
157 {
158 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
159 goto out;
160 }
161 /* Do these strlen() calls before reading prefixes of either
162 'phrase' or 'setting', so we get a predictable crash if they are
163 not valid strings. */
164 size_t phr_size = strlencxx . . $ strlen(751346f8406fd082). (phrase);
165 size_t set_size = strlencxx . . $ strlen(751346f8406fd082). (setting);
166 if (phr_size >= CRYPT_MAX_PASSPHRASE_SIZEcxx . . $ `crypt.h:36:9`!)
167 {
168 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!;
169 goto out;
170 }
171 if (check_badsalt_charscxx . . $ check_badsalt_chars(5c4306d8553b3696). (setting))
172 {
173 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
174 goto out;
175 }
176
177 const struct hashfncxx . . $ hashfn# *h = get_hashfncxx . . $ get_hashfn(6a662cb7fac9e190). (setting);
178 if (!h)
179 {
180 /* Unrecognized hash algorithm */
181 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
182 goto out;
183 }
184
185 h->cryptcxx . . $ `crypt-symbol-vers.h:60:9`! (phrase, phr_size, setting, set_size,
186 (unsigned char *) cint->outputcxx . . $ crypt_internal#output., sizeof cint->outputcxx . . $ crypt_internal#output.,
187 cint->alg_specificcxx . . $ crypt_internal#alg_specific., sizeof cint->alg_specificcxx . . $ crypt_internal#alg_specific.);
188
189 out:
190 strcpy_or_abortcxx . . $ `lib/crypt-port.h:188:9`! (data->outputcxx . . $ crypt_data#output., sizeof data->outputcxx . . $ crypt_data#output., cint->outputcxx . . $ crypt_internal#output.);
191 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`! (data->internal, sizeof data->internal);
192 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`! (data->reserved, sizeof data->reserved);
193 data->initializedcxx . . $ crypt_data#initialized. = 0;
194 }
195
196 #if INCLUDE_crypt_rncxx . . $ `crypt-symbol-vers.h:22:9`!
197 char *
198 crypt_rncxx . . $ `crypt-symbol-vers.h:69:9`! (const char *phrase, const char *setting, void *data, int size)
199 {
200 if (size < (int) sizeof (struct crypt_datacxx . . $ crypt_data#))
201 {
202 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!;
203 make_failure_tokencxx . . $ `lib/crypt-port.h:325:9`! (setting, data, size);
204 return 0;
205 }
206
207 struct crypt_datacxx . . $ crypt_data# *p = data;
208 do_cryptcxx . . $ do_crypt(8e811c27bb635ed4). (phrase, setting, p);
209 return p->outputcxx . . $ crypt_data#output.[0] == '*' ? 0 : p->outputcxx . . $ crypt_data#output.;
210 }
211 SYMVER_crypt_rncxx . . $ `crypt-symbol-vers.h:111:9`!;
212 #endif
213
214 #if INCLUDE_crypt_racxx . . $ `crypt-symbol-vers.h:21:9`!
215 char *
216 crypt_racxx . . $ `crypt-symbol-vers.h:68:9`! (const char *phrase, const char *setting, void **data, int *size)
217 {
218 struct crypt_datacxx . . $ crypt_data# *p = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
219
220 /* Short-circuit, if possible. */
221 if (*data && *size >= (int) sizeof (struct crypt_datacxx . . $ crypt_data#))
222 {
223 p = *data;
224 do_cryptcxx . . $ do_crypt(8e811c27bb635ed4). (phrase, setting, p);
225 return p->outputcxx . . $ crypt_data#output.[0] == '*' ? 0 : p->outputcxx . . $ crypt_data#output.;
226 }
227
228 /* Allocate new memory for struct crypt_data. */
229 p = malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (sizeof (struct crypt_datacxx . . $ crypt_data#));
230 if (!p)
231 {
232 /* ERRNO is set by malloc. */
233 if (*data)
234 make_failure_tokencxx . . $ `lib/crypt-port.h:325:9`! (setting, *data, *size);
235 return 0;
236 }
237 memsetcxx . . $ memset(d1de1e4fb215f1c6). (p, 0, sizeof (struct crypt_datacxx . . $ crypt_data#));
238
239 do_cryptcxx . . $ do_crypt(8e811c27bb635ed4). (phrase, setting, p);
240
241 /* Zeroize memory, if needed, before assigning
242 the new memory location to the data pointer. */
243 if (*data && *size > 0)
244 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`! (*data, (size_t) *size);
245 freecxx . . $ free(5d22bdacc48458e8). (*data);
246 *data = p;
247 *size = sizeof (struct crypt_datacxx . . $ crypt_data#);
248
249 return p->outputcxx . . $ crypt_data#output.[0] == '*' ? 0 : p->outputcxx . . $ crypt_data#output.;
250 }
251 SYMVER_crypt_racxx . . $ `crypt-symbol-vers.h:108:9`!;
252 #endif
253
254 #if INCLUDE_crypt_rcxx . . $ `crypt-symbol-vers.h:20:9`!
255 char *
256 crypt_rcxx . . $ `crypt-symbol-vers.h:67:9`! (const char *phrase, const char *setting, struct crypt_datacxx . . $ crypt_data# *data)
257 {
258 do_cryptcxx . . $ do_crypt(8e811c27bb635ed4). (phrase, setting, data);
259 #if ENABLE_FAILURE_TOKENScxx . . $ `config.h:9:9`!
260 return data->outputcxx . . $ crypt_data#output.;
261 #else
262 return data->output[0] == '*' ? 0 : data->output;
263 #endif
264 }
265 SYMVER_crypt_rcxx . . $ `crypt-symbol-vers.h:105:9`!;
266 #endif
267
268 /* For code compatibility with older versions (v3.1.1 and earlier). */
269 #if INCLUDE_crypt_rcxx . . $ `crypt-symbol-vers.h:20:9`! && INCLUDE_xcrypt_rcxx . . $ `crypt-symbol-vers.h:31:9`!
270 strong_aliascxx . . $ `lib/crypt-port.h:193:9`! (crypt_rcxx . . $ `crypt-symbol-vers.h:67:9`!, xcrypt_rcxx . . $ `crypt-symbol-vers.h:78:9`!);
271 SYMVER_xcrypt_rcxx . . $ `crypt-symbol-vers.h:130:9`!;
272 #endif
273
274 #if INCLUDE_crypt_gensalt_rncxx . . $ `crypt-symbol-vers.h:18:9`!
275 char *
276 crypt_gensalt_rncxx . . $ `crypt-symbol-vers.h:65:9`! (const char *prefix, unsigned long count,
277 const char *rbytes, int nrbytes, char *output,
278 int output_size)
279 {
280 /* Individual gensalt functions will check for adequate space for
281 their own breed of setting, but the shortest possible one is
282 three bytes (DES two-character salt + NUL terminator) and we
283 also want to rule out negative numbers early. */
284 if (output_size < 3)
285 {
286 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!;
287 make_failure_tokencxx . . $ `lib/crypt-port.h:325:9`! (prefix, output, output_size);
288 return 0;
289 }
290
291 char outbuf[CRYPT_GENSALT_OUTPUT_SIZEcxx . . $ `crypt.h:44:9`!];
292 unsigned char internal_nrbytes = 0;
293 memsetcxx . . $ memset(d1de1e4fb215f1c6). (outbuf, 0, sizeof outbuf);
294 make_failure_tokencxx . . $ `lib/crypt-port.h:325:9`! (prefix, outbuf, sizeof outbuf);
295
296 /* If the prefix is 0, that means to use the current best default.
297 Note that this is different from the behavior when the prefix is
298 "", which selects DES. HASH_ALGORITHM_DEFAULT is not defined when
299 the current default algorithm was disabled at configure time. */
300 if (!prefix)
301 {
302 #if defined HASH_ALGORITHM_DEFAULTcxx . . $ `crypt-hashes.h:175:9`!
303 prefix = HASH_ALGORITHM_DEFAULTcxx . . $ `crypt-hashes.h:175:9`!;
304 #else
305 errno = EINVAL;
306 goto out;
307 #endif
308 }
309
310 const struct hashfncxx . . $ hashfn# *h = get_hashfncxx . . $ get_hashfn(6a662cb7fac9e190). (prefix);
311 if (!h)
312 {
313 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
314 goto out;
315 }
316
317 char internal_rbytes[UCHAR_MAXcxx . . $ `<external>/2419331716647297546/limits.h:82:10`!] = "\0";
318 /* typeof (internal_nrbytes) == typeof (h->nrbytes). */
319
320 /* If rbytes is 0, read random bytes from the operating system if
321 possible. */
322 if (!rbytes)
323 {
324 if (!get_random_bytescxx . . $ `lib/crypt-port.h:324:9`! (internal_rbytes, h->nrbytescxx . . $ hashfn#nrbytes.))
325 goto out;
326
327 rbytes = internal_rbytes;
328 nrbytes = internal_nrbytes = h->nrbytescxx . . $ hashfn#nrbytes.;
329 }
330
331 h->gensaltcxx . . $ hashfn#gensalt. (count,
332 (const unsigned char *) rbytes, (size_t) nrbytes,
333 (unsigned char *) outbuf,
334 MINcxx . . $ `lib/crypt-port.h:145:9`! ((size_t) output_size, sizeof outbuf));
335
336 out:
337 strcpy_or_abortcxx . . $ `lib/crypt-port.h:188:9`! (output, (size_t) output_size, outbuf);
338 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`! (outbuf, sizeof outbuf);
339
340 if (internal_nrbytes)
341 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`! (internal_rbytes, internal_nrbytes);
342
343 return output[0] == '*' ? 0 : output;
344 }
345 SYMVER_crypt_gensalt_rncxx . . $ `crypt-symbol-vers.h:99:9`!;
346 #endif
347
348 /* For code compatibility with older versions (v3.1.1 and earlier). */
349 #if INCLUDE_crypt_gensalt_rncxx . . $ `crypt-symbol-vers.h:18:9`! && INCLUDE_crypt_gensalt_rcxx . . $ `crypt-symbol-vers.h:16:9`!
350 strong_aliascxx . . $ `lib/crypt-port.h:193:9`! (crypt_gensalt_rncxx . . $ `crypt-symbol-vers.h:65:9`!, crypt_gensalt_rcxx . . $ `crypt-symbol-vers.h:63:9`!);
351 SYMVER_crypt_gensalt_rcxx . . $ `crypt-symbol-vers.h:93:9`!;
352 #endif
353
354 /* For code compatibility with older versions (v3.1.1 and earlier). */
355 #if INCLUDE_crypt_gensalt_rncxx . . $ `crypt-symbol-vers.h:18:9`! && INCLUDE_xcrypt_gensalt_rcxx . . $ `crypt-symbol-vers.h:30:9`!
356 strong_aliascxx . . $ `lib/crypt-port.h:193:9`! (crypt_gensalt_rncxx . . $ `crypt-symbol-vers.h:65:9`!, xcrypt_gensalt_rcxx . . $ `crypt-symbol-vers.h:77:9`!);
357 SYMVER_xcrypt_gensalt_rcxx . . $ `crypt-symbol-vers.h:128:9`!;
358 #endif
359
360 #if INCLUDE_crypt_gensalt_racxx . . $ `crypt-symbol-vers.h:17:9`!
361 char *
362 crypt_gensalt_racxx . . $ `crypt-symbol-vers.h:64:9`! (const char *prefix, unsigned long count,
363 const char *rbytes, int nrbytes)
364 {
365 char *output = malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (CRYPT_GENSALT_OUTPUT_SIZEcxx . . $ `crypt.h:44:9`!);
366 if (!output)
367 return 0;
368
369 char *result = crypt_gensalt_rncxx . . $ `crypt-symbol-vers.h:65:9`! (prefix, count, rbytes, nrbytes, output,
370 CRYPT_GENSALT_OUTPUT_SIZEcxx . . $ `crypt.h:44:9`!);
371 if (result == 0)
372 freecxx . . $ free(5d22bdacc48458e8). (output);
373 return result;
374 }
375 SYMVER_crypt_gensalt_racxx . . $ `crypt-symbol-vers.h:95:9`!;
376 #endif
377
378 #if INCLUDE_crypt_checksaltcxx . . $ `crypt-symbol-vers.h:14:9`!
379 static_assertcxx . . $ `<external>/4398592474888995393/assert.h:210:10`!(CRYPT_SALT_OKcxx . . $ `crypt.h:213:9`! == 0, "CRYPT_SALT_OK does not equal zero");
380
381 int
382 crypt_checksaltcxx . . $ `crypt-symbol-vers.h:61:9`! (const char *setting)
383 {
384 int retval = CRYPT_SALT_INVALIDcxx . . $ `crypt.h:214:9`!;
385
386 if (!setting || /* NULL string */
387 setting[0] == '\0' || /* empty passphrase */
388 check_badsalt_charscxx . . $ check_badsalt_chars(5c4306d8553b3696). (setting)) /* bad salt chars */
389 goto end;
390
391 const struct hashfncxx . . $ hashfn# *h = get_hashfncxx . . $ get_hashfn(6a662cb7fac9e190). (setting);
392
393 if (h)
394 {
395 retval = CRYPT_SALT_OKcxx . . $ `crypt.h:213:9`!;
396
397 if (h->is_strongcxx . . $ hashfn#is_strong. == 0)
398 {
399 retval = CRYPT_SALT_METHOD_LEGACYcxx . . $ `crypt.h:216:9`!;
400 goto end;
401 }
402 }
403
404 end:
405 return retval;
406 }
407 SYMVER_crypt_checksaltcxx . . $ `crypt-symbol-vers.h:87:9`!;
408 #endif
409
410 #if INCLUDE_crypt_preferred_methodcxx . . $ `crypt-symbol-vers.h:19:9`!
411 const char *
412 crypt_preferred_methodcxx . . $ `crypt-symbol-vers.h:66:9`! (void)
413 {
414 #if defined HASH_ALGORITHM_DEFAULTcxx . . $ `crypt-hashes.h:175:9`!
415 return HASH_ALGORITHM_DEFAULTcxx . . $ `crypt-hashes.h:175:9`!;
416 #else
417 return NULL;
418 #endif
419 }
420 SYMVER_crypt_preferred_methodcxx . . $ `crypt-symbol-vers.h:103:9`!;
421 #endif