sha256-crypt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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.
  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 "sha256.h"
  24. #include "libcrypt.h"
  25. /* Define our magic string to mark salt for SHA256 "encryption"
  26. replacement. */
  27. static const char sha256_salt_prefix[] = "$5$";
  28. /* Prefix for optional rounds specification. */
  29. static const char sha256_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. __sha256_crypt_r (const char *key,
  54. const char *salt,
  55. char *buffer,
  56. int buflen)
  57. {
  58. unsigned char alt_result[32]
  59. __attribute__ ((__aligned__ (__alignof__ (uint32_t))));
  60. unsigned char temp_result[32]
  61. __attribute__ ((__aligned__ (__alignof__ (uint32_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 (sha256_salt_prefix, salt, sizeof (sha256_salt_prefix) - 1) == 0)
  76. /* Skip salt prefix. */
  77. salt += sizeof (sha256_salt_prefix) - 1;
  78. if (strncmp (salt, sha256_rounds_prefix, sizeof (sha256_rounds_prefix) - 1)
  79. == 0)
  80. {
  81. const char *num = salt + sizeof (sha256_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__ (uint32_t) != 0)
  94. {
  95. char *tmp = (char *) alloca (key_len + __alignof__ (uint32_t));
  96. key = copied_key =
  97. memcpy (tmp + __alignof__ (uint32_t)
  98. - (tmp - (char *) 0) % __alignof__ (uint32_t),
  99. key, key_len);
  100. assert ((key - (char *) 0) % __alignof__ (uint32_t) == 0);
  101. }
  102. if ((salt - (char *) 0) % __alignof__ (uint32_t) != 0)
  103. {
  104. char *tmp = (char *) alloca (salt_len + __alignof__ (uint32_t));
  105. salt = copied_salt =
  106. memcpy (tmp + __alignof__ (uint32_t)
  107. - (tmp - (char *) 0) % __alignof__ (uint32_t),
  108. salt, salt_len);
  109. assert ((salt - (char *) 0) % __alignof__ (uint32_t) == 0);
  110. }
  111. struct sha256_ctx ctx;
  112. struct sha256_ctx alt_ctx;
  113. /* Prepare for the real work. */
  114. __sha256_init_ctx (&ctx);
  115. /* Add the key string. */
  116. __sha256_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. __sha256_process_bytes (salt, salt_len, &ctx);
  120. /* Compute alternate SHA256 sum with input KEY, SALT, and KEY. The
  121. final result will be added to the first context. */
  122. __sha256_init_ctx (&alt_ctx);
  123. /* Add key. */
  124. __sha256_process_bytes (key, key_len, &alt_ctx);
  125. /* Add salt. */
  126. __sha256_process_bytes (salt, salt_len, &alt_ctx);
  127. /* Add key again. */
  128. __sha256_process_bytes (key, key_len, &alt_ctx);
  129. /* Now get result of this (32 bytes) and add it to the other
  130. context. */
  131. __sha256_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 > 32; cnt -= 32)
  134. __sha256_process_bytes (alt_result, 32, &ctx);
  135. __sha256_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. __sha256_process_bytes (alt_result, 32, &ctx);
  141. else
  142. __sha256_process_bytes (key, key_len, &ctx);
  143. /* Create intermediate result. */
  144. __sha256_finish_ctx (&ctx, alt_result);
  145. /* Start computation of P byte sequence. */
  146. __sha256_init_ctx (&alt_ctx);
  147. /* For every character in the password add the entire password. */
  148. for (cnt = 0; cnt < key_len; ++cnt)
  149. __sha256_process_bytes (key, key_len, &alt_ctx);
  150. /* Finish the digest. */
  151. __sha256_finish_ctx (&alt_ctx, temp_result);
  152. /* Create byte sequence P. */
  153. cp = p_bytes = alloca (key_len);
  154. for (cnt = key_len; cnt >= 32; cnt -= 32)
  155. cp = mempcpy (cp, temp_result, 32);
  156. memcpy (cp, temp_result, cnt);
  157. /* Start computation of S byte sequence. */
  158. __sha256_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. __sha256_process_bytes (salt, salt_len, &alt_ctx);
  162. /* Finish the digest. */
  163. __sha256_finish_ctx (&alt_ctx, temp_result);
  164. /* Create byte sequence S. */
  165. cp = s_bytes = alloca (salt_len);
  166. for (cnt = salt_len; cnt >= 32; cnt -= 32)
  167. cp = mempcpy (cp, temp_result, 32);
  168. memcpy (cp, temp_result, cnt);
  169. /* Repeatedly run the collected hash value through SHA256 to burn
  170. CPU cycles. */
  171. for (cnt = 0; cnt < rounds; ++cnt)
  172. {
  173. /* New context. */
  174. __sha256_init_ctx (&ctx);
  175. /* Add key or last result. */
  176. if ((cnt & 1) != 0)
  177. __sha256_process_bytes (p_bytes, key_len, &ctx);
  178. else
  179. __sha256_process_bytes (alt_result, 32, &ctx);
  180. /* Add salt for numbers not divisible by 3. */
  181. if (cnt % 3 != 0)
  182. __sha256_process_bytes (s_bytes, salt_len, &ctx);
  183. /* Add key for numbers not divisible by 7. */
  184. if (cnt % 7 != 0)
  185. __sha256_process_bytes (p_bytes, key_len, &ctx);
  186. /* Add key or last result. */
  187. if ((cnt & 1) != 0)
  188. __sha256_process_bytes (alt_result, 32, &ctx);
  189. else
  190. __sha256_process_bytes (p_bytes, key_len, &ctx);
  191. /* Create intermediate result. */
  192. __sha256_finish_ctx (&ctx, alt_result);
  193. }
  194. /* Now we can construct the result string. It consists of three
  195. parts. */
  196. cp = stpncpy (buffer, sha256_salt_prefix, MAX (0, buflen));
  197. buflen -= sizeof (sha256_salt_prefix) - 1;
  198. if (rounds_custom)
  199. {
  200. int n = snprintf (cp, MAX (0, buflen), "%s%zu$",
  201. sha256_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[10], alt_result[20], 4);
  213. B64_FROM_24BIT (alt_result[21], alt_result[1], alt_result[11], 4);
  214. B64_FROM_24BIT (alt_result[12], alt_result[22], alt_result[2], 4);
  215. B64_FROM_24BIT (alt_result[3], alt_result[13], alt_result[23], 4);
  216. B64_FROM_24BIT (alt_result[24], alt_result[4], alt_result[14], 4);
  217. B64_FROM_24BIT (alt_result[15], alt_result[25], alt_result[5], 4);
  218. B64_FROM_24BIT (alt_result[6], alt_result[16], alt_result[26], 4);
  219. B64_FROM_24BIT (alt_result[27], alt_result[7], alt_result[17], 4);
  220. B64_FROM_24BIT (alt_result[18], alt_result[28], alt_result[8], 4);
  221. B64_FROM_24BIT (alt_result[9], alt_result[19], alt_result[29], 4);
  222. B64_FROM_24BIT (0, alt_result[31], alt_result[30], 3);
  223. if (buflen <= 0)
  224. {
  225. __set_errno (ERANGE);
  226. buffer = NULL;
  227. }
  228. else
  229. *cp = '\0'; /* Terminate the string. */
  230. /* Clear the buffer for the intermediate result so that people
  231. attaching to processes or reading core dumps cannot get any
  232. information. We do it in this way to clear correct_words[]
  233. inside the SHA256 implementation as well. */
  234. __sha256_init_ctx (&ctx);
  235. __sha256_finish_ctx (&ctx, alt_result);
  236. memset (&ctx, '\0', sizeof (ctx));
  237. memset (&alt_ctx, '\0', sizeof (alt_ctx));
  238. memset (temp_result, '\0', sizeof (temp_result));
  239. memset (p_bytes, '\0', key_len);
  240. memset (s_bytes, '\0', salt_len);
  241. if (copied_key != NULL)
  242. memset (copied_key, '\0', key_len);
  243. if (copied_salt != NULL)
  244. memset (copied_salt, '\0', salt_len);
  245. return buffer;
  246. }
  247. static char *buffer;
  248. /* This entry point is equivalent to the `crypt' function in Unix
  249. libcs. */
  250. char *
  251. __sha256_crypt (const unsigned char *key, const unsigned char *salt)
  252. {
  253. /* We don't want to have an arbitrary limit in the size of the
  254. password. We can compute an upper bound for the size of the
  255. result in advance and so we can prepare the buffer we pass to
  256. `sha256_crypt_r'. */
  257. static int buflen;
  258. int needed = (sizeof (sha256_salt_prefix) - 1
  259. + sizeof (sha256_rounds_prefix) + 9 + 1
  260. + strlen (salt) + 1 + 43 + 1);
  261. if (buflen < needed)
  262. {
  263. char *new_buffer = (char *) realloc (buffer, needed);
  264. if (new_buffer == NULL)
  265. return NULL;
  266. buffer = new_buffer;
  267. buflen = needed;
  268. }
  269. return __sha256_crypt_r ((const char *) key, (const char *) salt, buffer, buflen);
  270. }