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, see
18 <http://www.gnu.org/licenses/>. */
25 #include <sys/param.h>
30 /* Define our magic string to mark salt for SHA512 "encryption"
32 static const char sha512_salt_prefix
[] = "$6$";
34 /* Prefix for optional rounds specification. */
35 static const char sha512_rounds_prefix
[] = "rounds=";
37 /* Maximum salt string length. */
38 #define SALT_LEN_MAX 16
39 /* Default number of rounds if not explicitly specified. */
40 #define ROUNDS_DEFAULT 5000
41 /* Minimum number of rounds. */
42 #define ROUNDS_MIN 1000
43 /* Maximum number of rounds. */
44 #define ROUNDS_MAX 999999999
46 /* Table with characters for base64 transformation. */
47 static const char b64t
[64] =
48 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
50 #define B64_FROM_24BIT(b2, b1, b0, steps) \
53 unsigned int w = ((b2) << 16) | ((b1) << 8) | (b0); \
54 while (n-- > 0 && buflen > 0) \
56 *cp++ = b64t[w & 0x3f]; \
63 __sha512_crypt_r (const char *key
,
68 unsigned char alt_result
[64]
69 __attribute__ ((__aligned__ (__alignof__ (uint64_t))));
70 unsigned char temp_result
[64]
71 __attribute__ ((__aligned__ (__alignof__ (uint64_t))));
76 char *copied_key
= NULL
;
77 char *copied_salt
= NULL
;
80 /* Default number of rounds. */
81 size_t rounds
= ROUNDS_DEFAULT
;
82 bool rounds_custom
= false;
84 /* Find beginning of salt string. The prefix should normally always
85 be present. Just in case it is not. */
86 if (strncmp (sha512_salt_prefix
, salt
, sizeof (sha512_salt_prefix
) - 1) == 0)
87 /* Skip salt prefix. */
88 salt
+= sizeof (sha512_salt_prefix
) - 1;
90 if (strncmp (salt
, sha512_rounds_prefix
, sizeof (sha512_rounds_prefix
) - 1)
93 const char *num
= salt
+ sizeof (sha512_rounds_prefix
) - 1;
95 unsigned long int srounds
= strtoul (num
, &endp
, 10);
99 rounds
= MAX (ROUNDS_MIN
, MIN (srounds
, ROUNDS_MAX
));
100 rounds_custom
= true;
104 salt_len
= MIN (strcspn (salt
, "$"), SALT_LEN_MAX
);
105 key_len
= strlen (key
);
107 if ((uintptr_t)key
% __alignof__ (uint64_t) != 0)
109 char *tmp
= (char *) alloca (key_len
+ __alignof__ (uint64_t));
111 memcpy (tmp
+ __alignof__ (uint64_t)
112 - (uintptr_t)tmp
% __alignof__ (uint64_t),
114 assert ((key
- (char *) 0) % __alignof__ (uint64_t) == 0);
117 if ((uintptr_t)salt
% __alignof__ (uint64_t) != 0)
119 char *tmp
= (char *) alloca (salt_len
+ __alignof__ (uint64_t));
121 memcpy (tmp
+ __alignof__ (uint64_t)
122 - (uintptr_t)tmp
% __alignof__ (uint64_t),
124 assert ((uintptr_t)salt
% __alignof__ (uint64_t) == 0);
127 struct sha512_ctx ctx
;
128 struct sha512_ctx alt_ctx
;
130 /* Prepare for the real work. */
131 __sha512_init_ctx (&ctx
);
133 /* Add the key string. */
134 __sha512_process_bytes (key
, key_len
, &ctx
);
136 /* The last part is the salt string. This must be at most 16
137 characters and it ends at the first `$' character. */
138 __sha512_process_bytes (salt
, salt_len
, &ctx
);
141 /* Compute alternate SHA512 sum with input KEY, SALT, and KEY. The
142 final result will be added to the first context. */
143 __sha512_init_ctx (&alt_ctx
);
146 __sha512_process_bytes (key
, key_len
, &alt_ctx
);
149 __sha512_process_bytes (salt
, salt_len
, &alt_ctx
);
152 __sha512_process_bytes (key
, key_len
, &alt_ctx
);
154 /* Now get result of this (64 bytes) and add it to the other
156 __sha512_finish_ctx (&alt_ctx
, alt_result
);
158 /* Add for any character in the key one byte of the alternate sum. */
159 for (cnt
= key_len
; cnt
> 64; cnt
-= 64)
160 __sha512_process_bytes (alt_result
, 64, &ctx
);
162 __sha512_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 __sha512_process_bytes (alt_result
, 64, &ctx
);
170 __sha512_process_bytes (key
, key_len
, &ctx
);
172 /* Create intermediate result. */
173 __sha512_finish_ctx (&ctx
, alt_result
);
175 /* Start computation of P byte sequence. */
176 __sha512_init_ctx (&alt_ctx
);
178 /* For every character in the password add the entire password. */
179 for (cnt
= 0; cnt
< key_len
; ++cnt
)
180 __sha512_process_bytes (key
, key_len
, &alt_ctx
);
182 /* Finish the digest. */
183 __sha512_finish_ctx (&alt_ctx
, temp_result
);
185 /* Create byte sequence P. */
186 cp
= p_bytes
= alloca (key_len
);
187 for (cnt
= key_len
; cnt
>= 64; cnt
-= 64)
188 cp
= mempcpy (cp
, temp_result
, 64);
189 memcpy (cp
, temp_result
, cnt
);
191 /* Start computation of S byte sequence. */
192 __sha512_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 __sha512_process_bytes (salt
, salt_len
, &alt_ctx
);
198 /* Finish the digest. */
199 __sha512_finish_ctx (&alt_ctx
, temp_result
);
201 /* Create byte sequence S. */
202 cp
= s_bytes
= alloca (salt_len
);
203 for (cnt
= salt_len
; cnt
>= 64; cnt
-= 64)
204 cp
= mempcpy (cp
, temp_result
, 64);
205 memcpy (cp
, temp_result
, cnt
);
207 /* Repeatedly run the collected hash value through SHA512 to burn
209 for (cnt
= 0; cnt
< rounds
; ++cnt
)
212 __sha512_init_ctx (&ctx
);
214 /* Add key or last result. */
216 __sha512_process_bytes (p_bytes
, key_len
, &ctx
);
218 __sha512_process_bytes (alt_result
, 64, &ctx
);
220 /* Add salt for numbers not divisible by 3. */
222 __sha512_process_bytes (s_bytes
, salt_len
, &ctx
);
224 /* Add key for numbers not divisible by 7. */
226 __sha512_process_bytes (p_bytes
, key_len
, &ctx
);
228 /* Add key or last result. */
230 __sha512_process_bytes (alt_result
, 64, &ctx
);
232 __sha512_process_bytes (p_bytes
, key_len
, &ctx
);
234 /* Create intermediate result. */
235 __sha512_finish_ctx (&ctx
, alt_result
);
238 /* Now we can construct the result string. It consists of three
240 cp
= stpncpy (buffer
, sha512_salt_prefix
, MAX (0, buflen
));
241 buflen
-= sizeof (sha512_salt_prefix
) - 1;
245 int n
= snprintf (cp
, MAX (0, buflen
), "%s%zu$",
246 sha512_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
[21], alt_result
[42], 4);
261 B64_FROM_24BIT (alt_result
[22], alt_result
[43], alt_result
[1], 4);
262 B64_FROM_24BIT (alt_result
[44], alt_result
[2], alt_result
[23], 4);
263 B64_FROM_24BIT (alt_result
[3], alt_result
[24], alt_result
[45], 4);
264 B64_FROM_24BIT (alt_result
[25], alt_result
[46], alt_result
[4], 4);
265 B64_FROM_24BIT (alt_result
[47], alt_result
[5], alt_result
[26], 4);
266 B64_FROM_24BIT (alt_result
[6], alt_result
[27], alt_result
[48], 4);
267 B64_FROM_24BIT (alt_result
[28], alt_result
[49], alt_result
[7], 4);
268 B64_FROM_24BIT (alt_result
[50], alt_result
[8], alt_result
[29], 4);
269 B64_FROM_24BIT (alt_result
[9], alt_result
[30], alt_result
[51], 4);
270 B64_FROM_24BIT (alt_result
[31], alt_result
[52], alt_result
[10], 4);
271 B64_FROM_24BIT (alt_result
[53], alt_result
[11], alt_result
[32], 4);
272 B64_FROM_24BIT (alt_result
[12], alt_result
[33], alt_result
[54], 4);
273 B64_FROM_24BIT (alt_result
[34], alt_result
[55], alt_result
[13], 4);
274 B64_FROM_24BIT (alt_result
[56], alt_result
[14], alt_result
[35], 4);
275 B64_FROM_24BIT (alt_result
[15], alt_result
[36], alt_result
[57], 4);
276 B64_FROM_24BIT (alt_result
[37], alt_result
[58], alt_result
[16], 4);
277 B64_FROM_24BIT (alt_result
[59], alt_result
[17], alt_result
[38], 4);
278 B64_FROM_24BIT (alt_result
[18], alt_result
[39], alt_result
[60], 4);
279 B64_FROM_24BIT (alt_result
[40], alt_result
[61], alt_result
[19], 4);
280 B64_FROM_24BIT (alt_result
[62], alt_result
[20], alt_result
[41], 4);
281 B64_FROM_24BIT (0, 0, alt_result
[63], 2);
285 __set_errno (ERANGE
);
289 *cp
= '\0'; /* Terminate the string. */
291 /* Clear the buffer for the intermediate result so that people
292 attaching to processes or reading core dumps cannot get any
293 information. We do it in this way to clear correct_words[]
294 inside the SHA512 implementation as well. */
295 __sha512_init_ctx (&ctx
);
296 __sha512_finish_ctx (&ctx
, alt_result
);
297 memset (&ctx
, '\0', sizeof (ctx
));
298 memset (&alt_ctx
, '\0', sizeof (alt_ctx
));
300 memset (temp_result
, '\0', sizeof (temp_result
));
301 memset (p_bytes
, '\0', key_len
);
302 memset (s_bytes
, '\0', salt_len
);
303 if (copied_key
!= NULL
)
304 memset (copied_key
, '\0', key_len
);
305 if (copied_salt
!= NULL
)
306 memset (copied_salt
, '\0', salt_len
);
313 /* This entry point is equivalent to the `crypt' function in Unix
316 __sha512_crypt (const unsigned char *key
, const unsigned char *salt
)
318 /* We don't want to have an arbitrary limit in the size of the
319 password. We can compute an upper bound for the size of the
320 result in advance and so we can prepare the buffer we pass to
323 int needed
= (sizeof (sha512_salt_prefix
) - 1
324 + sizeof (sha512_rounds_prefix
) + 9 + 1
325 + strlen (salt
) + 1 + 86 + 1);
329 char *new_buffer
= (char *) realloc (buffer
, needed
);
330 if (new_buffer
== NULL
)
337 return __sha512_crypt_r ((const char *) key
, (const char *) salt
, buffer
, buflen
);