Debian Codegraph @

lib/crypt-sm3.c

raw symbols SCIP
1 /* One way encryption based on the SHA256-based Unix crypt implementation.
2 *
3 * Written by Ulrich Drepper <drepper at redhat.com> in 2007 [1].
4 * Modified by Zack Weinberg <zackw at panix.com> in 2017, 2018.
5 * Composed by Björn Esser <besser82 at fedoraproject.org> in 2018.
6 * Modified by Björn Esser <besser82 at fedoraproject.org> in 2020.
7 * Modified by Tianjia Zhang <tianjia.zhang at linux.alibaba.com> in 2024.
8 * To the extent possible under law, the named authors have waived all
9 * copyright and related or neighboring rights to this work.
10 *
11 * See https://creativecommons.org/publicdomain/zero/1.0/ for further
12 * details.
13 *
14 * This file is a modified except from [2], lines 648 up to 909.
15 *
16 * [1] https://www.akkadia.org/drepper/sha-crypt.html
17 * [2] https://www.akkadia.org/drepper/SHA-crypt.txt
18 */
19
20 #include "crypt-port.h"cxx . . $ `<file>/lib/crypt-port.h`/
21 #include "alg-sm3.h"cxx . . $ `<file>/lib/alg-sm3.h`/
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #if INCLUDE_sm3cryptcxx . . $ `crypt-hashes.h:21:9`!
28
29 /* Define our magic string to mark salt for SM3 "encryption"
30 replacement. */
31 static const char sm3_salt_prefixcxx . . $ sm3_salt_prefix.[] = "$sm3$";
32
33 /* Prefix for optional rounds specification. */
34 static const char sm3_rounds_prefixcxx . . $ sm3_rounds_prefix.[] = "rounds=";
35
36 /* Maximum salt string length. */
37 #define SALT_LEN_MAXcxx . . $ `lib/crypt-sm3.c:37:9`! 16
38 /* Default number of rounds if not explicitly specified. */
39 #define ROUNDS_DEFAULTcxx . . $ `lib/crypt-sm3.c:39:9`! 5000
40 /* Minimum number of rounds. */
41 #define ROUNDS_MINcxx . . $ `lib/crypt-sm3.c:41:9`! 1000
42 /* Maximum number of rounds. */
43 #define ROUNDS_MAXcxx . . $ `lib/crypt-sm3.c:43:9`! 999999999
44
45 /* The maximum possible length of a SM3-hashed password string,
46 including the terminating NUL character. Prefix (including its NUL)
47 + rounds tag ("rounds=$" = "rounds=\0") + strlen(ROUNDS_MAX)
48 + salt (up to SALT_LEN_MAX chars) + '$' + hash (43 chars). */
49
50 #define LENGTH_OF_NUMBERcxx . . $ `lib/crypt-sm3.c:50:9`!(n) (sizeof #n - 1)
51
52 #define SM3_HASH_LENGTHcxx . . $ `lib/crypt-sm3.c:52:9`! \
53 (sizeof (sm3_salt_prefix) + sizeof (sm3_rounds_prefix) + \
54 LENGTH_OF_NUMBERcxx . . $ `lib/crypt-sm3.c:50:9`! (ROUNDS_MAX) + SALT_LEN_MAXcxx . . $ `lib/crypt-sm3.c:37:9`! + 1 + 43)
55
56 static_assertcxx . . $ `<external>/4398592474888995393/assert.h:210:10`! (SM3_HASH_LENGTHcxx . . $ `lib/crypt-sm3.c:52:9`! <= CRYPT_OUTPUT_SIZEcxx . . $ `crypt.h:32:9`!,
57 "CRYPT_OUTPUT_SIZE is too small for SM3");
58
59 /* A sm3_buffer holds all of the sensitive intermediate data. */
60 struct sm3_buffercxx . . $ sm3_buffer#
61 {
62 sm3_ctxcxx . . $ sm3_ctx# ctxcxx . . $ sm3_buffer#ctx.;
63 uint8_tcxx . . $ uint8_t# resultcxx . . $ sm3_buffer#result.[32];
64 uint8_tcxx . . $ uint8_t# p_bytescxx . . $ sm3_buffer#p_bytes.[32];
65 uint8_tcxx . . $ uint8_t# s_bytescxx . . $ sm3_buffer#s_bytes.[32];
66 };
67
68 static_assertcxx . . $ `<external>/4398592474888995393/assert.h:210:10`! (sizeof (struct sm3_buffercxx . . $ sm3_buffer#) <= ALG_SPECIFIC_SIZEcxx . . $ `lib/crypt-port.h:510:9`!,
69 "ALG_SPECIFIC_SIZE is too small for SM3crypt");
70
71
72 /* Feed CTX with LEN bytes of a virtual byte sequence consisting of
73 BLOCK repeated over and over indefinitely. */
74 static void
75 sm3_update_recycledcxx . . $ sm3_update_recycled(e407298020807eb2). (sm3_ctxcxx . . $ sm3_ctx# *ctx,
76 unsigned char block[32], size_t len)
77 {
78 size_t cnt;
79 for (cnt = len; cnt >= 32; cnt -= 32)
80 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, block, 32);
81 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, block, cnt);
82 }
83
84 void
85 crypt_sm3crypt_rncxx . . $ `crypt-hashes.h:56:9`! (const char *phrase, size_t phr_size,
86 const char *setting, size_t ARG_UNUSEDcxx . . $ `lib/crypt-port.h:78:10`! (set_size),
87 uint8_tcxx . . $ uint8_t# *output, size_t out_size,
88 void *scratch, size_t scr_size)
89 {
90 /* This shouldn't ever happen, but... */
91 if (out_size < SM3_HASH_LENGTHcxx . . $ `lib/crypt-sm3.c:52:9`!
92 || scr_size < sizeof (struct sm3_buffercxx . . $ sm3_buffer#))
93 {
94 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!;
95 return;
96 }
97
98 struct sm3_buffercxx . . $ sm3_buffer# *buf = scratch;
99 sm3_ctxcxx . . $ sm3_ctx# *ctx = &buf->ctxcxx . . $ sm3_buffer#ctx.;
100 uint8_tcxx . . $ uint8_t# *result = buf->resultcxx . . $ sm3_buffer#result.;
101 uint8_tcxx . . $ uint8_t# *p_bytes = buf->p_bytescxx . . $ sm3_buffer#p_bytes.;
102 uint8_tcxx . . $ uint8_t# *s_bytes = buf->s_bytescxx . . $ sm3_buffer#s_bytes.;
103 char *cp = (char *)output;
104 const char *salt = setting;
105
106 size_t salt_size;
107 size_t cnt;
108 /* Default number of rounds. */
109 size_t rounds = ROUNDS_DEFAULTcxx . . $ `lib/crypt-sm3.c:39:9`!;
110 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! rounds_custom = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
111
112 /* Find beginning of salt string. The prefix should normally always
113 be present. Just in case it is not. */
114 if (strncmpcxx . . $ strncmp(fd278af2f295fa2e). (sm3_salt_prefixcxx . . $ sm3_salt_prefix., salt, sizeof (sm3_salt_prefixcxx . . $ sm3_salt_prefix.) - 1) == 0)
115 /* Skip salt prefix. */
116 salt += sizeof (sm3_salt_prefixcxx . . $ sm3_salt_prefix.) - 1;
117
118 if (strncmpcxx . . $ strncmp(fd278af2f295fa2e). (salt, sm3_rounds_prefixcxx . . $ sm3_rounds_prefix., sizeof (sm3_rounds_prefixcxx . . $ sm3_rounds_prefix.) - 1)
119 == 0)
120 {
121 const char *num = salt + sizeof (sm3_rounds_prefixcxx . . $ sm3_rounds_prefix.) - 1;
122 /* Do not allow an explicit setting of zero rounds, nor of the
123 default number of rounds, nor leading zeroes on the rounds. */
124 if (!(*num >= '1' && *num <= '9'))
125 {
126 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
127 return;
128 }
129
130 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0;
131 char *endp;
132 rounds = strtoulcxx . . $ strtoul(31746e8d6ec49860). (num, &endp, 10);
133 if (endp == num || *endp != '$'
134 || rounds < ROUNDS_MINcxx . . $ `lib/crypt-sm3.c:41:9`!
135 || rounds > ROUNDS_MAXcxx . . $ `lib/crypt-sm3.c:43:9`!
136 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)
137 {
138 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
139 return;
140 }
141 salt = endp + 1;
142 rounds_custom = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
143 }
144
145 /* The salt ends at the next '$' or the end of the string.
146 Ensure ':' does not appear in the salt (it is used as a separator in /etc/passwd).
147 Also check for '\n', as in /etc/passwd the whole parameters of the user data must
148 be on a single line. */
149 salt_size = strcspncxx . . $ strcspn(335c1dc86f5c867f). (salt, "$:\n");
150 if (!(salt[salt_size] == '$' || !salt[salt_size]))
151 {
152 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
153 return;
154 }
155
156 /* Ensure we do not use more salt than SALT_LEN_MAX. */
157 if (salt_size > SALT_LEN_MAXcxx . . $ `lib/crypt-sm3.c:37:9`!)
158 salt_size = SALT_LEN_MAXcxx . . $ `lib/crypt-sm3.c:37:9`!;
159
160 /* Compute alternate SM3 sum with input PHRASE, SALT, and PHRASE. The
161 final result will be added to the first context. */
162 sm3_initcxx . . $ `lib/crypt-port.h:407:9`! (ctx);
163
164 /* Add phrase. */
165 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, phrase, phr_size);
166
167 /* Add salt. */
168 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, salt, salt_size);
169
170 /* Add phrase again. */
171 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, phrase, phr_size);
172
173 /* Now get result of this (32 bytes). */
174 sm3_finalcxx . . $ `lib/crypt-port.h:409:9`! (result, ctx);
175
176 /* Prepare for the real work. */
177 sm3_initcxx . . $ `lib/crypt-port.h:407:9`! (ctx);
178
179 /* Add the phrase string. */
180 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, phrase, phr_size);
181
182 /* The last part is the salt string. This must be at most 8
183 characters and it ends at the first `$' character (for
184 compatibility with existing implementations). */
185 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, salt, salt_size);
186
187 /* Add for any character in the phrase one byte of the alternate sum. */
188 for (cnt = phr_size; cnt > 32; cnt -= 32)
189 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, result, 32);
190 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, result, cnt);
191
192 /* Take the binary representation of the length of the phrase and for every
193 1 add the alternate sum, for every 0 the phrase. */
194 for (cnt = phr_size; cnt > 0; cnt >>= 1)
195 if ((cnt & 1) != 0)
196 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, result, 32);
197 else
198 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, phrase, phr_size);
199
200 /* Create intermediate result. */
201 sm3_finalcxx . . $ `lib/crypt-port.h:409:9`! (result, ctx);
202
203 /* Start computation of P byte sequence. */
204 sm3_initcxx . . $ `lib/crypt-port.h:407:9`! (ctx);
205
206 /* For every character in the password add the entire password. */
207 for (cnt = 0; cnt < phr_size; ++cnt)
208 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, phrase, phr_size);
209
210 /* Finish the digest. */
211 sm3_finalcxx . . $ `lib/crypt-port.h:409:9`! (p_bytes, ctx);
212
213 /* Start computation of S byte sequence. */
214 sm3_initcxx . . $ `lib/crypt-port.h:407:9`! (ctx);
215
216 /* For every character in the password add the entire password. */
217 for (cnt = 0; cnt < (size_t) 16 + (size_t) result[0]; ++cnt)
218 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, salt, salt_size);
219
220 /* Finish the digest. */
221 sm3_finalcxx . . $ `lib/crypt-port.h:409:9`! (s_bytes, ctx);
222
223 /* Repeatedly run the collected hash value through SM3 to burn
224 CPU cycles. */
225 for (cnt = 0; cnt < rounds; ++cnt)
226 {
227 /* New context. */
228 sm3_initcxx . . $ `lib/crypt-port.h:407:9`! (ctx);
229
230 /* Add phrase or last result. */
231 if ((cnt & 1) != 0)
232 sm3_update_recycledcxx . . $ sm3_update_recycled(e407298020807eb2). (ctx, p_bytes, phr_size);
233 else
234 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, result, 32);
235
236 /* Add salt for numbers not divisible by 3. */
237 if (cnt % 3 != 0)
238 sm3_update_recycledcxx . . $ sm3_update_recycled(e407298020807eb2). (ctx, s_bytes, salt_size);
239
240 /* Add phrase for numbers not divisible by 7. */
241 if (cnt % 7 != 0)
242 sm3_update_recycledcxx . . $ sm3_update_recycled(e407298020807eb2). (ctx, p_bytes, phr_size);
243
244 /* Add phrase or last result. */
245 if ((cnt & 1) != 0)
246 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`! (ctx, result, 32);
247 else
248 sm3_update_recycledcxx . . $ sm3_update_recycled(e407298020807eb2). (ctx, p_bytes, phr_size);
249
250 /* Create intermediate result. */
251 sm3_finalcxx . . $ `lib/crypt-port.h:409:9`! (result, ctx);
252 }
253
254 /* Now we can construct the result string. It consists of four
255 parts, one of which is optional. We already know that there
256 is sufficient space at CP for the longest possible result string. */
257 memcpycxx . . $ memcpy(cc817cb175f282d). (cp, sm3_salt_prefixcxx . . $ sm3_salt_prefix., sizeof (sm3_salt_prefixcxx . . $ sm3_salt_prefix.) - 1);
258 cp += sizeof (sm3_salt_prefixcxx . . $ sm3_salt_prefix.) - 1;
259
260 if (rounds_custom)
261 {
262 int n = snprintfcxx . . $ snprintf(ea502d73e74c754c). (cp, SM3_HASH_LENGTHcxx . . $ `lib/crypt-sm3.c:52:9`! - (sizeof (sm3_salt_prefixcxx . . $ sm3_salt_prefix.) - 1),
263 "%s%zu$", sm3_rounds_prefixcxx . . $ sm3_rounds_prefix., rounds);
264 cp += n;
265 }
266
267 memcpycxx . . $ memcpy(cc817cb175f282d). (cp, salt, salt_size);
268 cp += salt_size;
269 *cp++ = '$';
270
271 #define b64_from_24bitcxx . . $ `lib/crypt-sm3.c:271:9`!(B2, B1, B0, N) \
272 do { \
273 unsigned int w = ((((unsigned int)(B2)) << 16) | \
274 (((unsigned int)(B1)) << 8) | \
275 ((unsigned int)(B0))); \
276 int n = (N); \
277 while (n-- > 0) \
278 { \
279 *cp++ = b64tcxx . . $ `lib/crypt-port.h:501:9`![w & 0x3f]; \
280 w >>= 6; \
281 } \
282 } while (0)
283
284 b64_from_24bitcxx . . $ `lib/crypt-sm3.c:271:9`! (result[0], result[10], result[20], 4);
285 b64_from_24bitcxx . . $ `lib/crypt-sm3.c:271:9`! (result[21], result[1], result[11], 4);
286 b64_from_24bitcxx . . $ `lib/crypt-sm3.c:271:9`! (result[12], result[22], result[2], 4);
287 b64_from_24bitcxx . . $ `lib/crypt-sm3.c:271:9`! (result[3], result[13], result[23], 4);
288 b64_from_24bitcxx . . $ `lib/crypt-sm3.c:271:9`! (result[24], result[4], result[14], 4);
289 b64_from_24bitcxx . . $ `lib/crypt-sm3.c:271:9`! (result[15], result[25], result[5], 4);
290 b64_from_24bitcxx . . $ `lib/crypt-sm3.c:271:9`! (result[6], result[16], result[26], 4);
291 b64_from_24bitcxx . . $ `lib/crypt-sm3.c:271:9`! (result[27], result[7], result[17], 4);
292 b64_from_24bitcxx . . $ `lib/crypt-sm3.c:271:9`! (result[18], result[28], result[8], 4);
293 b64_from_24bitcxx . . $ `lib/crypt-sm3.c:271:9`! (result[9], result[19], result[29], 4);
294 b64_from_24bitcxx . . $ `lib/crypt-sm3.c:271:9`! (0, result[31], result[30], 3);
295
296 *cp = '\0';
297 }
298
299 void
300 gensalt_sm3crypt_rncxx . . $ `crypt-hashes.h:57:9`! (unsigned long count,
301 const uint8_tcxx . . $ uint8_t# *rbytes, size_t nrbytes,
302 uint8_tcxx . . $ uint8_t# *output, size_t output_size)
303 {
304 gensalt_sha_rncxx . . $ `lib/crypt-port.h:371:9`! ("sm3", SALT_LEN_MAXcxx . . $ `lib/crypt-sm3.c:37:9`!, ROUNDS_DEFAULTcxx . . $ `lib/crypt-sm3.c:39:9`!, ROUNDS_MINcxx . . $ `lib/crypt-sm3.c:41:9`!, ROUNDS_MAXcxx . . $ `lib/crypt-sm3.c:43:9`!,
305 count, rbytes, nrbytes, output, output_size);
306 }
307
308 #endif