Debian Codegraph @

lib/crypt-yescrypt.c

raw symbols SCIP
1 /* Copyright (C) 2018 [email protected]
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted.
5 *
6 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
7 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
10 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
11 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
12 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
14 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
15 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
16 * SUCH DAMAGE.
17 */
18
19 #include "crypt-port.h"cxx . . $ `<file>/lib/crypt-port.h`/
20 #include "alg-yescrypt.h"cxx . . $ `<file>/lib/alg-yescrypt.h`/
21
22 #include <errno.h>
23
24 #if INCLUDE_yescryptcxx . . $ `crypt-hashes.h:23:9`! || INCLUDE_scryptcxx . . $ `crypt-hashes.h:16:9`!
25
26 /* For use in scratch space by crypt_yescrypt_rn(). */
27 typedef structcxx . . $ $anonymous_type_1a65bb9cf99e554d_0#
28 {
29 yescrypt_local_tcxx . . $ yescrypt_local_t# localcxx . . $ $anonymous_type_1a65bb9cf99e554d_0#local.;
30 uint8_tcxx . . $ uint8_t# outbufcxx . . $ $anonymous_type_1a65bb9cf99e554d_0#outbuf.[CRYPT_OUTPUT_SIZEcxx . . $ `crypt.h:32:9`!];
31 uint8_tcxx . . $ uint8_t# *retvalcxx . . $ $anonymous_type_1a65bb9cf99e554d_0#retval.;
32 } crypt_yescrypt_internal_tcxx . . $ crypt_yescrypt_internal_t#;
33
34 static_assertcxx . . $ `<external>/4398592474888995393/assert.h:210:10`! (sizeof (crypt_yescrypt_internal_tcxx . . $ crypt_yescrypt_internal_t#) <= ALG_SPECIFIC_SIZEcxx . . $ `lib/crypt-port.h:510:9`!,
35 "ALG_SPECIFIC_SIZE is too small for YESCRYPT.");
36
37 void
38 crypt_yescrypt_rncxx . . $ `crypt-hashes.h:60:9`! (const char *phrase, size_t phr_size,
39 const char *setting, size_t set_size,
40 uint8_tcxx . . $ uint8_t# *output, size_t o_size,
41 void *scratch, size_t s_size)
42 {
43 #if !INCLUDE_scryptcxx . . $ `crypt-hashes.h:16:9`!
44
45 /* If scrypt is disabled fail when called with its prefix. */
46 if (!strncmp (setting, "$7$", 3))
47 {
48 errno = EINVAL;
49 return;
50 }
51
52 #endif /* !INCLUDE_scrypt */
53
54 #if !INCLUDE_yescryptcxx . . $ `crypt-hashes.h:23:9`!
55
56 /* If yescrypt is disabled fail when called with its prefix. */
57 if (!strncmp (setting, "$y$", 3))
58 {
59 errno = EINVAL;
60 return;
61 }
62
63 #endif /* !INCLUDE_yescrypt */
64
65 if (o_size < set_size + 1 + 43 + 1 ||
66 CRYPT_OUTPUT_SIZEcxx . . $ `crypt.h:32:9`! < set_size + 1 + 43 + 1 ||
67 s_size < sizeof (crypt_yescrypt_internal_tcxx . . $ crypt_yescrypt_internal_t#))
68 {
69 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!;
70 return;
71 }
72
73 crypt_yescrypt_internal_tcxx . . $ crypt_yescrypt_internal_t# *intbuf = scratch;
74
75 if (yescrypt_init_localcxx . . $ `lib/crypt-port.h:386:9`! (&intbuf->localcxx . . $ $anonymous_type_1a65bb9cf99e554d_0#local.))
76 return;
77
78 intbuf->retvalcxx . . $ $anonymous_type_1a65bb9cf99e554d_0#retval. = yescrypt_rcxx . . $ `lib/crypt-port.h:389:9`! (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, &intbuf->localcxx . . $ $anonymous_type_1a65bb9cf99e554d_0#local.,
79 (const uint8_tcxx . . $ uint8_t# *)phrase, phr_size,
80 (const uint8_tcxx . . $ uint8_t# *)setting, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
81 intbuf->outbufcxx . . $ $anonymous_type_1a65bb9cf99e554d_0#outbuf., o_size);
82
83 if (!intbuf->retvalcxx . . $ $anonymous_type_1a65bb9cf99e554d_0#retval.)
84 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
85
86 if (yescrypt_free_localcxx . . $ `lib/crypt-port.h:384:9`! (&intbuf->localcxx . . $ $anonymous_type_1a65bb9cf99e554d_0#local.) || !intbuf->retvalcxx . . $ $anonymous_type_1a65bb9cf99e554d_0#retval.)
87 return;
88
89 strcpy_or_abortcxx . . $ `lib/crypt-port.h:188:9`! (output, o_size, intbuf->outbufcxx . . $ $anonymous_type_1a65bb9cf99e554d_0#outbuf.);
90 return;
91 }
92
93 #endif /* INCLUDE_yescrypt || INCLUDE_scrypt */
94
95 #if INCLUDE_gost_yescryptcxx . . $ `crypt-hashes.h:13:9`! || INCLUDE_yescryptcxx . . $ `crypt-hashes.h:23:9`! || INCLUDE_sm3_yescryptcxx . . $ `crypt-hashes.h:20:9`!
96
97 /*
98 * As OUTPUT is initialized with a failure token before gensalt_yescrypt_rn
99 * is called, in case of an error we could just set an appropriate errno
100 * and return.
101 * Since O_SIZE is guaranteed to be greater than 2, we may fill OUTPUT
102 * with a short failure token when need.
103 */
104 void
105 gensalt_yescrypt_rncxx . . $ `crypt-hashes.h:61:9`! (unsigned long count,
106 const uint8_tcxx . . $ uint8_t# *rbytes, size_t nrbytes,
107 uint8_tcxx . . $ uint8_t# *output, size_t o_size)
108 {
109 /* Up to 512 bits (64 bytes) of entropy for computing the salt portion
110 of the MCF-setting are supported. */
111 nrbytes = (nrbytes > 64 ? 64 : nrbytes);
112
113 if (o_size < 3 + 8 * 6 + 1 + BASE64_LENcxx . . $ `lib/crypt-port.h:506:9`! (nrbytes) + 1 ||
114 CRYPT_GENSALT_OUTPUT_SIZEcxx . . $ `crypt.h:44:9`! < 3 + 8 * 6 + 1 + BASE64_LENcxx . . $ `lib/crypt-port.h:506:9`! (nrbytes) + 1)
115 {
116 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!;
117 return;
118 }
119
120 if (count > 11 || nrbytes < 16)
121 {
122 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
123 return;
124 }
125
126 /* Temporary buffer for operation. The buffer is guaranteed to be
127 large enough to hold the maximum size of the generated salt. */
128 uint8_tcxx . . $ uint8_t# outbuf[CRYPT_GENSALT_OUTPUT_SIZEcxx . . $ `crypt.h:44:9`!];
129
130 yescrypt_params_tcxx . . $ yescrypt_params_t# params =
131 {
132 .flags = YESCRYPT_DEFAULTScxx . . $ `lib/alg-yescrypt.h:125:9`!,
133 .p = 1,
134 };
135
136 /* Valid cost parameters are from 1 to 11. The default is 5.
137 These are used to set yescrypt's 'N' and 'r' parameters as
138 follows:
139 N (block count) is specified in units of r (block size,
140 adjustable in steps of 128 bytes).
141
142 128 bytes * r = size of each memory block
143
144 128 bytes * r * N = total amount of memory used for hashing
145 in N blocks of r * 128 bytes.
146
147 The author of yescrypt recommends in the documentation to use
148 r=8 (a block size of 1 KiB) for total sizes of 2 MiB and less,
149 and r=32 (a block size of 4KiB) above that.
150 This has to do with the typical per-core last-level cache sizes
151 of current CPUs. */
152
153 if (count == 0)
154 count = 5;
155
156 if (count < 3)
157 {
158 params.rcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#r. = 8; // N in 1KiB
159 params.Ncxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#N. = 1ULL << (count + 9); // 1 -> 1024, 2 -> 2048
160 }
161 else
162 {
163 params.rcxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#r. = 32; // N in 4KiB
164 params.Ncxx . . $ $anonymous_type_b025ac6af4ec7e0f_1#N. = 1ULL << (count + 7); // 3 -> 1024, 4 -> 2048, ... 11 -> 262144
165 }
166
167 if (!yescrypt_encode_params_rcxx . . $ `lib/crypt-port.h:383:9`! (&params, rbytes, nrbytes, outbuf, o_size))
168 {
169 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!;
170 return;
171 }
172
173 strcpy_or_abortcxx . . $ `lib/crypt-port.h:188:9`! (output, o_size, outbuf);
174 return;
175 }
176
177 #endif /* INCLUDE_gost_yescrypt || INCLUDE_yescrypt || INCLUDE_sm3_yescrypt */