blob: 44221482f6edaabbb5ded08b7260e0d1485a66a7 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* One way encryption based on SHA256 sum.
2 Copyright (C) 2007, 2009 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2007.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C 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 GNU
14 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 the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
20
21#include <assert.h>
22#include <errno.h>
23#include <stdbool.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/param.h>
27
28#include "sha256.h"
29#include "libcrypt.h"
30
31/* Define our magic string to mark salt for SHA256 "encryption"
32 replacement. */
33static const char sha256_salt_prefix[] = "$5$";
34
35/* Prefix for optional rounds specification. */
36static const char sha256_rounds_prefix[] = "rounds=";
37
38/* Maximum salt string length. */
39#define SALT_LEN_MAX 16
40/* Default number of rounds if not explicitly specified. */
41#define ROUNDS_DEFAULT 5000
42/* Minimum number of rounds. */
43#define ROUNDS_MIN 1000
44/* Maximum number of rounds. */
45#define ROUNDS_MAX 999999999
46
47/* Table with characters for base64 transformation. */
48static const char b64t[64] =
49"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
50
51#define B64_FROM_24BIT(b2, b1, b0, steps) \
52 { \
53 int n = (steps); \
54 unsigned int w = ((b2) << 16) | ((b1) << 8) | (b0); \
55 while (n-- > 0 && buflen > 0) \
56 { \
57 *cp++ = b64t[w & 0x3f]; \
58 --buflen; \
59 w >>= 6; \
60 } \
61 }
62
63char *
64__sha256_crypt_r (const char *key,
65 const char *salt,
66 char *buffer,
67 int buflen)
68{
69 unsigned char alt_result[32]
70 __attribute__ ((__aligned__ (__alignof__ (uint32_t))));
71 unsigned char temp_result[32]
72 __attribute__ ((__aligned__ (__alignof__ (uint32_t))));
73 size_t salt_len;
74 size_t key_len;
75 size_t cnt;
76 char *cp;
77 char *copied_key = NULL;
78 char *copied_salt = NULL;
79 char *p_bytes;
80 char *s_bytes;
81 /* Default number of rounds. */
82 size_t rounds = ROUNDS_DEFAULT;
83 bool rounds_custom = false;
84
85 /* Find beginning of salt string. The prefix should normally always
86 be present. Just in case it is not. */
87 if (strncmp (sha256_salt_prefix, salt, sizeof (sha256_salt_prefix) - 1) == 0)
88 /* Skip salt prefix. */
89 salt += sizeof (sha256_salt_prefix) - 1;
90
91 if (strncmp (salt, sha256_rounds_prefix, sizeof (sha256_rounds_prefix) - 1)
92 == 0)
93 {
94 const char *num = salt + sizeof (sha256_rounds_prefix) - 1;
95 char *endp;
96 unsigned long int srounds = strtoul (num, &endp, 10);
97 if (*endp == '$')
98 {
99 salt = endp + 1;
100 rounds = MAX (ROUNDS_MIN, MIN (srounds, ROUNDS_MAX));
101 rounds_custom = true;
102 }
103 }
104
105 salt_len = MIN (strcspn (salt, "$"), SALT_LEN_MAX);
106 key_len = strlen (key);
107
108 if ((key - (char *) 0) % __alignof__ (uint32_t) != 0)
109 {
110 char *tmp = (char *) alloca (key_len + __alignof__ (uint32_t));
111 key = copied_key =
112 memcpy (tmp + __alignof__ (uint32_t)
113 - (tmp - (char *) 0) % __alignof__ (uint32_t),
114 key, key_len);
115 assert ((key - (char *) 0) % __alignof__ (uint32_t) == 0);
116 }
117
118 if ((salt - (char *) 0) % __alignof__ (uint32_t) != 0)
119 {
120 char *tmp = (char *) alloca (salt_len + __alignof__ (uint32_t));
121 salt = copied_salt =
122 memcpy (tmp + __alignof__ (uint32_t)
123 - (tmp - (char *) 0) % __alignof__ (uint32_t),
124 salt, salt_len);
125 assert ((salt - (char *) 0) % __alignof__ (uint32_t) == 0);
126 }
127
128 struct sha256_ctx ctx;
129 struct sha256_ctx alt_ctx;
130
131 /* Prepare for the real work. */
132 __sha256_init_ctx (&ctx);
133
134 /* Add the key string. */
135 __sha256_process_bytes (key, key_len, &ctx);
136
137 /* The last part is the salt string. This must be at most 16
138 characters and it ends at the first `$' character. */
139 __sha256_process_bytes (salt, salt_len, &ctx);
140
141
142 /* Compute alternate SHA256 sum with input KEY, SALT, and KEY. The
143 final result will be added to the first context. */
144 __sha256_init_ctx (&alt_ctx);
145
146 /* Add key. */
147 __sha256_process_bytes (key, key_len, &alt_ctx);
148
149 /* Add salt. */
150 __sha256_process_bytes (salt, salt_len, &alt_ctx);
151
152 /* Add key again. */
153 __sha256_process_bytes (key, key_len, &alt_ctx);
154
155 /* Now get result of this (32 bytes) and add it to the other
156 context. */
157 __sha256_finish_ctx (&alt_ctx, alt_result);
158
159 /* Add for any character in the key one byte of the alternate sum. */
160 for (cnt = key_len; cnt > 32; cnt -= 32)
161 __sha256_process_bytes (alt_result, 32, &ctx);
162 __sha256_process_bytes (alt_result, cnt, &ctx);
163
164 /* Take the binary representation of the length of the key and for every
165 1 add the alternate sum, for every 0 the key. */
166 for (cnt = key_len; cnt > 0; cnt >>= 1)
167 if ((cnt & 1) != 0)
168 __sha256_process_bytes (alt_result, 32, &ctx);
169 else
170 __sha256_process_bytes (key, key_len, &ctx);
171
172 /* Create intermediate result. */
173 __sha256_finish_ctx (&ctx, alt_result);
174
175 /* Start computation of P byte sequence. */
176 __sha256_init_ctx (&alt_ctx);
177
178 /* For every character in the password add the entire password. */
179 for (cnt = 0; cnt < key_len; ++cnt)
180 __sha256_process_bytes (key, key_len, &alt_ctx);
181
182 /* Finish the digest. */
183 __sha256_finish_ctx (&alt_ctx, temp_result);
184
185 /* Create byte sequence P. */
186 cp = p_bytes = alloca (key_len);
187 for (cnt = key_len; cnt >= 32; cnt -= 32)
188 cp = mempcpy (cp, temp_result, 32);
189 memcpy (cp, temp_result, cnt);
190
191 /* Start computation of S byte sequence. */
192 __sha256_init_ctx (&alt_ctx);
193
194 /* For every character in the password add the entire password. */
195 for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt)
196 __sha256_process_bytes (salt, salt_len, &alt_ctx);
197
198 /* Finish the digest. */
199 __sha256_finish_ctx (&alt_ctx, temp_result);
200
201 /* Create byte sequence S. */
202 cp = s_bytes = alloca (salt_len);
203 for (cnt = salt_len; cnt >= 32; cnt -= 32)
204 cp = mempcpy (cp, temp_result, 32);
205 memcpy (cp, temp_result, cnt);
206
207 /* Repeatedly run the collected hash value through SHA256 to burn
208 CPU cycles. */
209 for (cnt = 0; cnt < rounds; ++cnt)
210 {
211 /* New context. */
212 __sha256_init_ctx (&ctx);
213
214 /* Add key or last result. */
215 if ((cnt & 1) != 0)
216 __sha256_process_bytes (p_bytes, key_len, &ctx);
217 else
218 __sha256_process_bytes (alt_result, 32, &ctx);
219
220 /* Add salt for numbers not divisible by 3. */
221 if (cnt % 3 != 0)
222 __sha256_process_bytes (s_bytes, salt_len, &ctx);
223
224 /* Add key for numbers not divisible by 7. */
225 if (cnt % 7 != 0)
226 __sha256_process_bytes (p_bytes, key_len, &ctx);
227
228 /* Add key or last result. */
229 if ((cnt & 1) != 0)
230 __sha256_process_bytes (alt_result, 32, &ctx);
231 else
232 __sha256_process_bytes (p_bytes, key_len, &ctx);
233
234 /* Create intermediate result. */
235 __sha256_finish_ctx (&ctx, alt_result);
236 }
237
238 /* Now we can construct the result string. It consists of three
239 parts. */
240 cp = stpncpy (buffer, sha256_salt_prefix, MAX (0, buflen));
241 buflen -= sizeof (sha256_salt_prefix) - 1;
242
243 if (rounds_custom)
244 {
245 int n = snprintf (cp, MAX (0, buflen), "%s%zu$",
246 sha256_rounds_prefix, rounds);
247 cp += n;
248 buflen -= n;
249 }
250
251 cp = stpncpy (cp, salt, MIN ((size_t) MAX (0, buflen), salt_len));
252 buflen -= MIN ((size_t) MAX (0, buflen), salt_len);
253
254 if (buflen > 0)
255 {
256 *cp++ = '$';
257 --buflen;
258 }
259
260 B64_FROM_24BIT (alt_result[0], alt_result[10], alt_result[20], 4);
261 B64_FROM_24BIT (alt_result[21], alt_result[1], alt_result[11], 4);
262 B64_FROM_24BIT (alt_result[12], alt_result[22], alt_result[2], 4);
263 B64_FROM_24BIT (alt_result[3], alt_result[13], alt_result[23], 4);
264 B64_FROM_24BIT (alt_result[24], alt_result[4], alt_result[14], 4);
265 B64_FROM_24BIT (alt_result[15], alt_result[25], alt_result[5], 4);
266 B64_FROM_24BIT (alt_result[6], alt_result[16], alt_result[26], 4);
267 B64_FROM_24BIT (alt_result[27], alt_result[7], alt_result[17], 4);
268 B64_FROM_24BIT (alt_result[18], alt_result[28], alt_result[8], 4);
269 B64_FROM_24BIT (alt_result[9], alt_result[19], alt_result[29], 4);
270 B64_FROM_24BIT (0, alt_result[31], alt_result[30], 3);
271 if (buflen <= 0)
272 {
273 __set_errno (ERANGE);
274 buffer = NULL;
275 }
276 else
277 *cp = '\0'; /* Terminate the string. */
278
279 /* Clear the buffer for the intermediate result so that people
280 attaching to processes or reading core dumps cannot get any
281 information. We do it in this way to clear correct_words[]
282 inside the SHA256 implementation as well. */
283 __sha256_init_ctx (&ctx);
284 __sha256_finish_ctx (&ctx, alt_result);
285 memset (&ctx, '\0', sizeof (ctx));
286 memset (&alt_ctx, '\0', sizeof (alt_ctx));
287
288 memset (temp_result, '\0', sizeof (temp_result));
289 memset (p_bytes, '\0', key_len);
290 memset (s_bytes, '\0', salt_len);
291 if (copied_key != NULL)
292 memset (copied_key, '\0', key_len);
293 if (copied_salt != NULL)
294 memset (copied_salt, '\0', salt_len);
295
296 return buffer;
297}
298
299static char *buffer;
300
301/* This entry point is equivalent to the `crypt' function in Unix
302 libcs. */
303char *
304__sha256_crypt (const unsigned char *key, const unsigned char *salt)
305{
306 /* We don't want to have an arbitrary limit in the size of the
307 password. We can compute an upper bound for the size of the
308 result in advance and so we can prepare the buffer we pass to
309 `sha256_crypt_r'. */
310 static int buflen;
311 int needed = (sizeof (sha256_salt_prefix) - 1
312 + sizeof (sha256_rounds_prefix) + 9 + 1
313 + strlen (salt) + 1 + 43 + 1);
314
315 if (buflen < needed)
316 {
317 char *new_buffer = (char *) realloc (buffer, needed);
318 if (new_buffer == NULL)
319 return NULL;
320
321 buffer = new_buffer;
322 buflen = needed;
323 }
324
325 return __sha256_crypt_r ((const char *) key, (const char *) salt, buffer, buflen);
326}