1 /* 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.
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.
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.
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
26 #include <sys/param.h>
31 /* Define our magic string to mark salt for SHA256 "encryption"
33 static const char sha256_salt_prefix[] = "$5$";
35 /* Prefix for optional rounds specification. */
36 static const char sha256_rounds_prefix[] = "rounds=";
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
47 /* Table with characters for base64 transformation. */
48 static const char b64t[64] =
49 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
51 #define B64_FROM_24BIT(b2, b1, b0, steps) \
54 unsigned int w = ((b2) << 16) | ((b1) << 8) | (b0); \
55 while (n-- > 0 && buflen > 0) \
57 *cp++ = b64t[w & 0x3f]; \
64 __sha256_crypt_r (const char *key,
69 unsigned char alt_result[32]
70 __attribute__ ((__aligned__ (__alignof__ (uint32_t))));
71 unsigned char temp_result[32]
72 __attribute__ ((__aligned__ (__alignof__ (uint32_t))));
77 char *copied_key = NULL;
78 char *copied_salt = NULL;
81 /* Default number of rounds. */
82 size_t rounds = ROUNDS_DEFAULT;
83 bool rounds_custom = false;
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;
91 if (strncmp (salt, sha256_rounds_prefix, sizeof (sha256_rounds_prefix) - 1)
94 const char *num = salt + sizeof (sha256_rounds_prefix) - 1;
96 unsigned long int srounds = strtoul (num, &endp, 10);
100 rounds = MAX (ROUNDS_MIN, MIN (srounds, ROUNDS_MAX));
101 rounds_custom = true;
105 salt_len = MIN (strcspn (salt, "$"), SALT_LEN_MAX);
106 key_len = strlen (key);
108 if ((key - (char *) 0) % __alignof__ (uint32_t) != 0)
110 char *tmp = (char *) alloca (key_len + __alignof__ (uint32_t));
112 memcpy (tmp + __alignof__ (uint32_t)
113 - (tmp - (char *) 0) % __alignof__ (uint32_t),
115 assert ((key - (char *) 0) % __alignof__ (uint32_t) == 0);
118 if ((salt - (char *) 0) % __alignof__ (uint32_t) != 0)
120 char *tmp = (char *) alloca (salt_len + __alignof__ (uint32_t));
122 memcpy (tmp + __alignof__ (uint32_t)
123 - (tmp - (char *) 0) % __alignof__ (uint32_t),
125 assert ((salt - (char *) 0) % __alignof__ (uint32_t) == 0);
128 struct sha256_ctx ctx;
129 struct sha256_ctx alt_ctx;
131 /* Prepare for the real work. */
132 __sha256_init_ctx (&ctx);
134 /* Add the key string. */
135 __sha256_process_bytes (key, key_len, &ctx);
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);
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);
147 __sha256_process_bytes (key, key_len, &alt_ctx);
150 __sha256_process_bytes (salt, salt_len, &alt_ctx);
153 __sha256_process_bytes (key, key_len, &alt_ctx);
155 /* Now get result of this (32 bytes) and add it to the other
157 __sha256_finish_ctx (&alt_ctx, alt_result);
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);
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)
168 __sha256_process_bytes (alt_result, 32, &ctx);
170 __sha256_process_bytes (key, key_len, &ctx);
172 /* Create intermediate result. */
173 __sha256_finish_ctx (&ctx, alt_result);
175 /* Start computation of P byte sequence. */
176 __sha256_init_ctx (&alt_ctx);
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);
182 /* Finish the digest. */
183 __sha256_finish_ctx (&alt_ctx, temp_result);
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);
191 /* Start computation of S byte sequence. */
192 __sha256_init_ctx (&alt_ctx);
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);
198 /* Finish the digest. */
199 __sha256_finish_ctx (&alt_ctx, temp_result);
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);
207 /* Repeatedly run the collected hash value through SHA256 to burn
209 for (cnt = 0; cnt < rounds; ++cnt)
212 __sha256_init_ctx (&ctx);
214 /* Add key or last result. */
216 __sha256_process_bytes (p_bytes, key_len, &ctx);
218 __sha256_process_bytes (alt_result, 32, &ctx);
220 /* Add salt for numbers not divisible by 3. */
222 __sha256_process_bytes (s_bytes, salt_len, &ctx);
224 /* Add key for numbers not divisible by 7. */
226 __sha256_process_bytes (p_bytes, key_len, &ctx);
228 /* Add key or last result. */
230 __sha256_process_bytes (alt_result, 32, &ctx);
232 __sha256_process_bytes (p_bytes, key_len, &ctx);
234 /* Create intermediate result. */
235 __sha256_finish_ctx (&ctx, alt_result);
238 /* Now we can construct the result string. It consists of three
240 cp = stpncpy (buffer, sha256_salt_prefix, MAX (0, buflen));
241 buflen -= sizeof (sha256_salt_prefix) - 1;
245 int n = snprintf (cp, MAX (0, buflen), "%s%zu$",
246 sha256_rounds_prefix, rounds);
251 cp = stpncpy (cp, salt, MIN ((size_t) MAX (0, buflen), salt_len));
252 buflen -= MIN ((size_t) MAX (0, buflen), salt_len);
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);
273 __set_errno (ERANGE);
277 *cp = '\0'; /* Terminate the string. */
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));
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);
301 /* This entry point is equivalent to the `crypt' function in Unix
304 __sha256_crypt (const unsigned char *key, const unsigned char *salt)
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
311 int needed = (sizeof (sha256_salt_prefix) - 1
312 + sizeof (sha256_rounds_prefix) + 9 + 1
313 + strlen (salt) + 1 + 43 + 1);
317 char *new_buffer = (char *) realloc (buffer, needed);
318 if (new_buffer == NULL)
325 return __sha256_crypt_r ((const char *) key, (const char *) salt, buffer, buflen);