sha512-crypt.c 11 KB

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