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