sha256-crypt.c 10.0 KB

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