1 /* One way encryption based on SHA512 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 SHA512 "encryption"
33 static const char sha512_salt_prefix[] = "$6$";
35 /* Prefix for optional rounds specification. */
36 static const char sha512_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 __sha512_crypt_r (const char *key,
69 unsigned char alt_result[64]
70 __attribute__ ((__aligned__ (__alignof__ (uint64_t))));
71 unsigned char temp_result[64]
72 __attribute__ ((__aligned__ (__alignof__ (uint64_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 (sha512_salt_prefix, salt, sizeof (sha512_salt_prefix) - 1) == 0)
88 /* Skip salt prefix. */
89 salt += sizeof (sha512_salt_prefix) - 1;
91 if (strncmp (salt, sha512_rounds_prefix, sizeof (sha512_rounds_prefix) - 1)
94 const char *num = salt + sizeof (sha512_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__ (uint64_t) != 0)
110 char *tmp = (char *) alloca (key_len + __alignof__ (uint64_t));
112 memcpy (tmp + __alignof__ (uint64_t)
113 - (tmp - (char *) 0) % __alignof__ (uint64_t),
115 assert ((key - (char *) 0) % __alignof__ (uint64_t) == 0);
118 if ((salt - (char *) 0) % __alignof__ (uint64_t) != 0)
120 char *tmp = (char *) alloca (salt_len + __alignof__ (uint64_t));
122 memcpy (tmp + __alignof__ (uint64_t)
123 - (tmp - (char *) 0) % __alignof__ (uint64_t),
125 assert ((salt - (char *) 0) % __alignof__ (uint64_t) == 0);
128 struct sha512_ctx ctx;
129 struct sha512_ctx alt_ctx;
131 /* Prepare for the real work. */
132 __sha512_init_ctx (&ctx);
134 /* Add the key string. */
135 __sha512_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 __sha512_process_bytes (salt, salt_len, &ctx);
142 /* Compute alternate SHA512 sum with input KEY, SALT, and KEY. The
143 final result will be added to the first context. */
144 __sha512_init_ctx (&alt_ctx);
147 __sha512_process_bytes (key, key_len, &alt_ctx);
150 __sha512_process_bytes (salt, salt_len, &alt_ctx);
153 __sha512_process_bytes (key, key_len, &alt_ctx);
155 /* Now get result of this (64 bytes) and add it to the other
157 __sha512_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 > 64; cnt -= 64)
161 __sha512_process_bytes (alt_result, 64, &ctx);
163 __sha512_process_bytes (alt_result, cnt, &ctx);
165 /* Take the binary representation of the length of the key and for every
166 1 add the alternate sum, for every 0 the key. */
167 for (cnt = key_len; cnt > 0; cnt >>= 1)
169 __sha512_process_bytes (alt_result, 64, &ctx);
171 __sha512_process_bytes (key, key_len, &ctx);
173 /* Create intermediate result. */
174 __sha512_finish_ctx (&ctx, alt_result);
176 /* Start computation of P byte sequence. */
177 __sha512_init_ctx (&alt_ctx);
179 /* For every character in the password add the entire password. */
180 for (cnt = 0; cnt < key_len; ++cnt)
181 __sha512_process_bytes (key, key_len, &alt_ctx);
183 /* Finish the digest. */
184 __sha512_finish_ctx (&alt_ctx, temp_result);
186 /* Create byte sequence P. */
187 cp = p_bytes = alloca (key_len);
188 for (cnt = key_len; cnt >= 64; cnt -= 64)
189 cp = mempcpy (cp, temp_result, 64);
190 memcpy (cp, temp_result, cnt);
192 /* Start computation of S byte sequence. */
193 __sha512_init_ctx (&alt_ctx);
195 /* For every character in the password add the entire password. */
196 for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt)
197 __sha512_process_bytes (salt, salt_len, &alt_ctx);
199 /* Finish the digest. */
200 __sha512_finish_ctx (&alt_ctx, temp_result);
202 /* Create byte sequence S. */
203 cp = s_bytes = alloca (salt_len);
204 for (cnt = salt_len; cnt >= 64; cnt -= 64)
205 cp = mempcpy (cp, temp_result, 64);
206 memcpy (cp, temp_result, cnt);
208 /* Repeatedly run the collected hash value through SHA512 to burn
210 for (cnt = 0; cnt < rounds; ++cnt)
213 __sha512_init_ctx (&ctx);
215 /* Add key or last result. */
217 __sha512_process_bytes (p_bytes, key_len, &ctx);
219 __sha512_process_bytes (alt_result, 64, &ctx);
221 /* Add salt for numbers not divisible by 3. */
223 __sha512_process_bytes (s_bytes, salt_len, &ctx);
225 /* Add key for numbers not divisible by 7. */
227 __sha512_process_bytes (p_bytes, key_len, &ctx);
229 /* Add key or last result. */
231 __sha512_process_bytes (alt_result, 64, &ctx);
233 __sha512_process_bytes (p_bytes, key_len, &ctx);
235 /* Create intermediate result. */
236 __sha512_finish_ctx (&ctx, alt_result);
239 /* Now we can construct the result string. It consists of three
241 cp = stpncpy (buffer, sha512_salt_prefix, MAX (0, buflen));
242 buflen -= sizeof (sha512_salt_prefix) - 1;
246 int n = snprintf (cp, MAX (0, buflen), "%s%zu$",
247 sha512_rounds_prefix, rounds);
252 cp = stpncpy (cp, salt, MIN ((size_t) MAX (0, buflen), salt_len));
253 buflen -= MIN ((size_t) MAX (0, buflen), salt_len);
261 B64_FROM_24BIT (alt_result[0], alt_result[21], alt_result[42], 4);
262 B64_FROM_24BIT (alt_result[22], alt_result[43], alt_result[1], 4);
263 B64_FROM_24BIT (alt_result[44], alt_result[2], alt_result[23], 4);
264 B64_FROM_24BIT (alt_result[3], alt_result[24], alt_result[45], 4);
265 B64_FROM_24BIT (alt_result[25], alt_result[46], alt_result[4], 4);
266 B64_FROM_24BIT (alt_result[47], alt_result[5], alt_result[26], 4);
267 B64_FROM_24BIT (alt_result[6], alt_result[27], alt_result[48], 4);
268 B64_FROM_24BIT (alt_result[28], alt_result[49], alt_result[7], 4);
269 B64_FROM_24BIT (alt_result[50], alt_result[8], alt_result[29], 4);
270 B64_FROM_24BIT (alt_result[9], alt_result[30], alt_result[51], 4);
271 B64_FROM_24BIT (alt_result[31], alt_result[52], alt_result[10], 4);
272 B64_FROM_24BIT (alt_result[53], alt_result[11], alt_result[32], 4);
273 B64_FROM_24BIT (alt_result[12], alt_result[33], alt_result[54], 4);
274 B64_FROM_24BIT (alt_result[34], alt_result[55], alt_result[13], 4);
275 B64_FROM_24BIT (alt_result[56], alt_result[14], alt_result[35], 4);
276 B64_FROM_24BIT (alt_result[15], alt_result[36], alt_result[57], 4);
277 B64_FROM_24BIT (alt_result[37], alt_result[58], alt_result[16], 4);
278 B64_FROM_24BIT (alt_result[59], alt_result[17], alt_result[38], 4);
279 B64_FROM_24BIT (alt_result[18], alt_result[39], alt_result[60], 4);
280 B64_FROM_24BIT (alt_result[40], alt_result[61], alt_result[19], 4);
281 B64_FROM_24BIT (alt_result[62], alt_result[20], alt_result[41], 4);
282 B64_FROM_24BIT (0, 0, alt_result[63], 2);
286 __set_errno (ERANGE);
290 *cp = '\0'; /* Terminate the string. */
292 /* Clear the buffer for the intermediate result so that people
293 attaching to processes or reading core dumps cannot get any
294 information. We do it in this way to clear correct_words[]
295 inside the SHA512 implementation as well. */
296 __sha512_init_ctx (&ctx);
297 __sha512_finish_ctx (&ctx, alt_result);
298 memset (&ctx, '\0', sizeof (ctx));
299 memset (&alt_ctx, '\0', sizeof (alt_ctx));
301 memset (temp_result, '\0', sizeof (temp_result));
302 memset (p_bytes, '\0', key_len);
303 memset (s_bytes, '\0', salt_len);
304 if (copied_key != NULL)
305 memset (copied_key, '\0', key_len);
306 if (copied_salt != NULL)
307 memset (copied_salt, '\0', salt_len);
314 /* This entry point is equivalent to the `crypt' function in Unix
317 __sha512_crypt (const unsigned char *key, const unsigned char *salt)
319 /* We don't want to have an arbitrary limit in the size of the
320 password. We can compute an upper bound for the size of the
321 result in advance and so we can prepare the buffer we pass to
324 int needed = (sizeof (sha512_salt_prefix) - 1
325 + sizeof (sha512_rounds_prefix) + 9 + 1
326 + strlen (salt) + 1 + 86 + 1);
330 char *new_buffer = (char *) realloc (buffer, needed);
331 if (new_buffer == NULL)
338 return __sha512_crypt_r ((const char *) key, (const char *) salt, buffer, buflen);