Debian Codegraph @

lib/crypt-sm3.c

raw
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"
21 #include "alg-sm3.h"
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #if INCLUDE_sm3crypt
28
29 /* Define our magic string to mark salt for SM3 "encryption"
30 replacement. */
31 static const char sm3_salt_prefix[] = "$sm3$";
32
33 /* Prefix for optional rounds specification. */
34 static const char sm3_rounds_prefix[] = "rounds=";
35
36 /* Maximum salt string length. */
37 #define SALT_LEN_MAX 16
38 /* Default number of rounds if not explicitly specified. */
39 #define ROUNDS_DEFAULT 5000
40 /* Minimum number of rounds. */
41 #define ROUNDS_MIN 1000
42 /* Maximum number of rounds. */
43 #define ROUNDS_MAX 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_NUMBER(n) (sizeof #n - 1)
51
52 #define SM3_HASH_LENGTH \
53 (sizeof (sm3_salt_prefix) + sizeof (sm3_rounds_prefix) + \
54 LENGTH_OF_NUMBER (ROUNDS_MAX) + SALT_LEN_MAX + 1 + 43)
55
56 static_assert (SM3_HASH_LENGTH <= CRYPT_OUTPUT_SIZE,
57 "CRYPT_OUTPUT_SIZE is too small for SM3");
58
59 /* A sm3_buffer holds all of the sensitive intermediate data. */
60 struct sm3_buffer
61 {
62 sm3_ctx ctx;
63 uint8_t result[32];
64 uint8_t p_bytes[32];
65 uint8_t s_bytes[32];
66 };
67
68 static_assert (sizeof (struct sm3_buffer) <= ALG_SPECIFIC_SIZE,
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_recycled (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_update (ctx, block, 32);
81 sm3_update (ctx, block, cnt);
82 }
83
84 void
85 crypt_sm3crypt_rn (const char *phrase, size_t phr_size,
86 const char *setting, size_t ARG_UNUSED (set_size),
87 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_LENGTH
92 || scr_size < sizeof (struct sm3_buffer))
93 {
94 errno = ERANGE;
95 return;
96 }
97
98 struct sm3_buffer *buf = scratch;
99 sm3_ctx *ctx = &buf->ctx;
100 uint8_t *result = buf->result;
101 uint8_t *p_bytes = buf->p_bytes;
102 uint8_t *s_bytes = buf->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_DEFAULT;
110 bool rounds_custom = false;
111
112 /* Find beginning of salt string. The prefix should normally always
113 be present. Just in case it is not. */
114 if (strncmp (sm3_salt_prefix, salt, sizeof (sm3_salt_prefix) - 1) == 0)
115 /* Skip salt prefix. */
116 salt += sizeof (sm3_salt_prefix) - 1;
117
118 if (strncmp (salt, sm3_rounds_prefix, sizeof (sm3_rounds_prefix) - 1)
119 == 0)
120 {
121 const char *num = salt + sizeof (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 errno = EINVAL;
127 return;
128 }
129
130 errno = 0;
131 char *endp;
132 rounds = strtoul (num, &endp, 10);
133 if (endp == num || *endp != '$'
134 || rounds < ROUNDS_MIN
135 || rounds > ROUNDS_MAX
136 || errno)
137 {
138 errno = EINVAL;
139 return;
140 }
141 salt = endp + 1;
142 rounds_custom = true;
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 = strcspn (salt, "$:\n");
150 if (!(salt[salt_size] == '$' || !salt[salt_size]))
151 {
152 errno = EINVAL;
153 return;
154 }
155
156 /* Ensure we do not use more salt than SALT_LEN_MAX. */
157 if (salt_size > SALT_LEN_MAX)
158 salt_size = SALT_LEN_MAX;
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_init (ctx);
163
164 /* Add phrase. */
165 sm3_update (ctx, phrase, phr_size);
166
167 /* Add salt. */
168 sm3_update (ctx, salt, salt_size);
169
170 /* Add phrase again. */
171 sm3_update (ctx, phrase, phr_size);
172
173 /* Now get result of this (32 bytes). */
174 sm3_final (result, ctx);
175
176 /* Prepare for the real work. */
177 sm3_init (ctx);
178
179 /* Add the phrase string. */
180 sm3_update (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_update (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_update (ctx, result, 32);
190 sm3_update (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_update (ctx, result, 32);
197 else
198 sm3_update (ctx, phrase, phr_size);
199
200 /* Create intermediate result. */
201 sm3_final (result, ctx);
202
203 /* Start computation of P byte sequence. */
204 sm3_init (ctx);
205
206 /* For every character in the password add the entire password. */
207 for (cnt = 0; cnt < phr_size; ++cnt)
208 sm3_update (ctx, phrase, phr_size);
209
210 /* Finish the digest. */
211 sm3_final (p_bytes, ctx);
212
213 /* Start computation of S byte sequence. */
214 sm3_init (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_update (ctx, salt, salt_size);
219
220 /* Finish the digest. */
221 sm3_final (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_init (ctx);
229
230 /* Add phrase or last result. */
231 if ((cnt & 1) != 0)
232 sm3_update_recycled (ctx, p_bytes, phr_size);
233 else
234 sm3_update (ctx, result, 32);
235
236 /* Add salt for numbers not divisible by 3. */
237 if (cnt % 3 != 0)
238 sm3_update_recycled (ctx, s_bytes, salt_size);
239
240 /* Add phrase for numbers not divisible by 7. */
241 if (cnt % 7 != 0)
242 sm3_update_recycled (ctx, p_bytes, phr_size);
243
244 /* Add phrase or last result. */
245 if ((cnt & 1) != 0)
246 sm3_update (ctx, result, 32);
247 else
248 sm3_update_recycled (ctx, p_bytes, phr_size);
249
250 /* Create intermediate result. */
251 sm3_final (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 memcpy (cp, sm3_salt_prefix, sizeof (sm3_salt_prefix) - 1);
258 cp += sizeof (sm3_salt_prefix) - 1;
259
260 if (rounds_custom)
261 {
262 int n = snprintf (cp, SM3_HASH_LENGTH - (sizeof (sm3_salt_prefix) - 1),
263 "%s%zu$", sm3_rounds_prefix, rounds);
264 cp += n;
265 }
266
267 memcpy (cp, salt, salt_size);
268 cp += salt_size;
269 *cp++ = '$';
270
271 #define b64_from_24bit(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++ = b64t[w & 0x3f]; \
280 w >>= 6; \
281 } \
282 } while (0)
283
284 b64_from_24bit (result[0], result[10], result[20], 4);
285 b64_from_24bit (result[21], result[1], result[11], 4);
286 b64_from_24bit (result[12], result[22], result[2], 4);
287 b64_from_24bit (result[3], result[13], result[23], 4);
288 b64_from_24bit (result[24], result[4], result[14], 4);
289 b64_from_24bit (result[15], result[25], result[5], 4);
290 b64_from_24bit (result[6], result[16], result[26], 4);
291 b64_from_24bit (result[27], result[7], result[17], 4);
292 b64_from_24bit (result[18], result[28], result[8], 4);
293 b64_from_24bit (result[9], result[19], result[29], 4);
294 b64_from_24bit (0, result[31], result[30], 3);
295
296 *cp = '\0';
297 }
298
299 void
300 gensalt_sm3crypt_rn (unsigned long count,
301 const uint8_t *rbytes, size_t nrbytes,
302 uint8_t *output, size_t output_size)
303 {
304 gensalt_sha_rn ("sm3", SALT_LEN_MAX, ROUNDS_DEFAULT, ROUNDS_MIN, ROUNDS_MAX,
305 count, rbytes, nrbytes, output, output_size);
306 }
307
308 #endif