sha256-crypt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /* One way encryption based on SHA256 sum.
  2. Copyright (C) 2007-2019 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 <stdint.h>
  22. #include <sys/param.h>
  23. #include "sha256.h"
  24. #include "crypt-private.h"
  25. #ifdef USE_NSS
  26. typedef int PRBool;
  27. # include <hasht.h>
  28. # include <nsslowhash.h>
  29. # define sha256_init_ctx(ctxp, nss_ctxp) \
  30. do \
  31. { \
  32. if (((nss_ctxp = NSSLOWHASH_NewContext (nss_ictx, HASH_AlgSHA256)) \
  33. == NULL)) \
  34. { \
  35. if (nss_ctx != NULL) \
  36. NSSLOWHASH_Destroy (nss_ctx); \
  37. if (nss_alt_ctx != NULL) \
  38. NSSLOWHASH_Destroy (nss_alt_ctx); \
  39. return NULL; \
  40. } \
  41. NSSLOWHASH_Begin (nss_ctxp); \
  42. } \
  43. while (0)
  44. # define sha256_process_bytes(buf, len, ctxp, nss_ctxp) \
  45. NSSLOWHASH_Update (nss_ctxp, (const unsigned char *) buf, len)
  46. # define sha256_finish_ctx(ctxp, nss_ctxp, result) \
  47. do \
  48. { \
  49. unsigned int ret; \
  50. NSSLOWHASH_End (nss_ctxp, result, &ret, sizeof (result)); \
  51. assert (ret == sizeof (result)); \
  52. NSSLOWHASH_Destroy (nss_ctxp); \
  53. nss_ctxp = NULL; \
  54. } \
  55. while (0)
  56. #else
  57. # define sha256_init_ctx(ctxp, nss_ctxp) \
  58. __sha256_init_ctx (ctxp)
  59. # define sha256_process_bytes(buf, len, ctxp, nss_ctxp) \
  60. __sha256_process_bytes(buf, len, ctxp)
  61. # define sha256_finish_ctx(ctxp, nss_ctxp, result) \
  62. __sha256_finish_ctx (ctxp, result)
  63. #endif
  64. /* Define our magic string to mark salt for SHA256 "encryption"
  65. replacement. */
  66. static const char sha256_salt_prefix[] = "$5$";
  67. /* Prefix for optional rounds specification. */
  68. static const char sha256_rounds_prefix[] = "rounds=";
  69. /* Maximum salt string length. */
  70. #define SALT_LEN_MAX 16
  71. /* Default number of rounds if not explicitly specified. */
  72. #define ROUNDS_DEFAULT 5000
  73. /* Minimum number of rounds. */
  74. #define ROUNDS_MIN 1000
  75. /* Maximum number of rounds. */
  76. #define ROUNDS_MAX 999999999
  77. /* Prototypes for local functions. */
  78. extern char *__sha256_crypt_r (const char *key, const char *salt,
  79. char *buffer, int buflen);
  80. extern char *__sha256_crypt (const char *key, const char *salt);
  81. char *
  82. __sha256_crypt_r (const char *key, const char *salt, char *buffer, int buflen)
  83. {
  84. unsigned char alt_result[32]
  85. __attribute__ ((__aligned__ (__alignof__ (uint32_t))));
  86. unsigned char temp_result[32]
  87. __attribute__ ((__aligned__ (__alignof__ (uint32_t))));
  88. size_t salt_len;
  89. size_t key_len;
  90. size_t cnt;
  91. char *cp;
  92. char *copied_key = NULL;
  93. char *copied_salt = NULL;
  94. char *p_bytes;
  95. char *s_bytes;
  96. /* Default number of rounds. */
  97. size_t rounds = ROUNDS_DEFAULT;
  98. bool rounds_custom = false;
  99. size_t alloca_used = 0;
  100. char *free_key = NULL;
  101. char *free_pbytes = NULL;
  102. /* Find beginning of salt string. The prefix should normally always
  103. be present. Just in case it is not. */
  104. if (strncmp (sha256_salt_prefix, salt, sizeof (sha256_salt_prefix) - 1) == 0)
  105. /* Skip salt prefix. */
  106. salt += sizeof (sha256_salt_prefix) - 1;
  107. if (strncmp (salt, sha256_rounds_prefix, sizeof (sha256_rounds_prefix) - 1)
  108. == 0)
  109. {
  110. const char *num = salt + sizeof (sha256_rounds_prefix) - 1;
  111. char *endp;
  112. unsigned long int srounds = strtoul (num, &endp, 10);
  113. if (*endp == '$')
  114. {
  115. salt = endp + 1;
  116. rounds = MAX (ROUNDS_MIN, MIN (srounds, ROUNDS_MAX));
  117. rounds_custom = true;
  118. }
  119. }
  120. salt_len = MIN (strcspn (salt, "$"), SALT_LEN_MAX);
  121. key_len = strlen (key);
  122. if ((key - (char *) 0) % __alignof__ (uint32_t) != 0)
  123. {
  124. char *tmp;
  125. if (__libc_use_alloca (alloca_used + key_len + __alignof__ (uint32_t)))
  126. tmp = alloca_account (key_len + __alignof__ (uint32_t), alloca_used);
  127. else
  128. {
  129. free_key = tmp = (char *) malloc (key_len + __alignof__ (uint32_t));
  130. if (tmp == NULL)
  131. return NULL;
  132. }
  133. key = copied_key =
  134. memcpy (tmp + __alignof__ (uint32_t)
  135. - (tmp - (char *) 0) % __alignof__ (uint32_t),
  136. key, key_len);
  137. assert ((key - (char *) 0) % __alignof__ (uint32_t) == 0);
  138. }
  139. if ((salt - (char *) 0) % __alignof__ (uint32_t) != 0)
  140. {
  141. char *tmp = (char *) alloca (salt_len + __alignof__ (uint32_t));
  142. alloca_used += salt_len + __alignof__ (uint32_t);
  143. salt = copied_salt =
  144. memcpy (tmp + __alignof__ (uint32_t)
  145. - (tmp - (char *) 0) % __alignof__ (uint32_t),
  146. salt, salt_len);
  147. assert ((salt - (char *) 0) % __alignof__ (uint32_t) == 0);
  148. }
  149. #ifdef USE_NSS
  150. /* Initialize libfreebl3. */
  151. NSSLOWInitContext *nss_ictx = NSSLOW_Init ();
  152. if (nss_ictx == NULL)
  153. {
  154. free (free_key);
  155. return NULL;
  156. }
  157. NSSLOWHASHContext *nss_ctx = NULL;
  158. NSSLOWHASHContext *nss_alt_ctx = NULL;
  159. #else
  160. struct sha256_ctx ctx;
  161. struct sha256_ctx alt_ctx;
  162. #endif
  163. /* Prepare for the real work. */
  164. sha256_init_ctx (&ctx, nss_ctx);
  165. /* Add the key string. */
  166. sha256_process_bytes (key, key_len, &ctx, nss_ctx);
  167. /* The last part is the salt string. This must be at most 16
  168. characters and it ends at the first `$' character. */
  169. sha256_process_bytes (salt, salt_len, &ctx, nss_ctx);
  170. /* Compute alternate SHA256 sum with input KEY, SALT, and KEY. The
  171. final result will be added to the first context. */
  172. sha256_init_ctx (&alt_ctx, nss_alt_ctx);
  173. /* Add key. */
  174. sha256_process_bytes (key, key_len, &alt_ctx, nss_alt_ctx);
  175. /* Add salt. */
  176. sha256_process_bytes (salt, salt_len, &alt_ctx, nss_alt_ctx);
  177. /* Add key again. */
  178. sha256_process_bytes (key, key_len, &alt_ctx, nss_alt_ctx);
  179. /* Now get result of this (32 bytes) and add it to the other
  180. context. */
  181. sha256_finish_ctx (&alt_ctx, nss_alt_ctx, alt_result);
  182. /* Add for any character in the key one byte of the alternate sum. */
  183. for (cnt = key_len; cnt > 32; cnt -= 32)
  184. sha256_process_bytes (alt_result, 32, &ctx, nss_ctx);
  185. sha256_process_bytes (alt_result, cnt, &ctx, nss_ctx);
  186. /* Take the binary representation of the length of the key and for every
  187. 1 add the alternate sum, for every 0 the key. */
  188. for (cnt = key_len; cnt > 0; cnt >>= 1)
  189. if ((cnt & 1) != 0)
  190. sha256_process_bytes (alt_result, 32, &ctx, nss_ctx);
  191. else
  192. sha256_process_bytes (key, key_len, &ctx, nss_ctx);
  193. /* Create intermediate result. */
  194. sha256_finish_ctx (&ctx, nss_ctx, alt_result);
  195. /* Start computation of P byte sequence. */
  196. sha256_init_ctx (&alt_ctx, nss_alt_ctx);
  197. /* For every character in the password add the entire password. */
  198. for (cnt = 0; cnt < key_len; ++cnt)
  199. sha256_process_bytes (key, key_len, &alt_ctx, nss_alt_ctx);
  200. /* Finish the digest. */
  201. sha256_finish_ctx (&alt_ctx, nss_alt_ctx, temp_result);
  202. /* Create byte sequence P. */
  203. if (__libc_use_alloca (alloca_used + key_len))
  204. cp = p_bytes = (char *) alloca (key_len);
  205. else
  206. {
  207. free_pbytes = cp = p_bytes = (char *)malloc (key_len);
  208. if (free_pbytes == NULL)
  209. {
  210. free (free_key);
  211. return NULL;
  212. }
  213. }
  214. for (cnt = key_len; cnt >= 32; cnt -= 32)
  215. cp = mempcpy (cp, temp_result, 32);
  216. memcpy (cp, temp_result, cnt);
  217. /* Start computation of S byte sequence. */
  218. sha256_init_ctx (&alt_ctx, nss_alt_ctx);
  219. /* For every character in the password add the entire password. */
  220. for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt)
  221. sha256_process_bytes (salt, salt_len, &alt_ctx, nss_alt_ctx);
  222. /* Finish the digest. */
  223. sha256_finish_ctx (&alt_ctx, nss_alt_ctx, temp_result);
  224. /* Create byte sequence S. */
  225. cp = s_bytes = alloca (salt_len);
  226. for (cnt = salt_len; cnt >= 32; cnt -= 32)
  227. cp = mempcpy (cp, temp_result, 32);
  228. memcpy (cp, temp_result, cnt);
  229. /* Repeatedly run the collected hash value through SHA256 to burn
  230. CPU cycles. */
  231. for (cnt = 0; cnt < rounds; ++cnt)
  232. {
  233. /* New context. */
  234. sha256_init_ctx (&ctx, nss_ctx);
  235. /* Add key or last result. */
  236. if ((cnt & 1) != 0)
  237. sha256_process_bytes (p_bytes, key_len, &ctx, nss_ctx);
  238. else
  239. sha256_process_bytes (alt_result, 32, &ctx, nss_ctx);
  240. /* Add salt for numbers not divisible by 3. */
  241. if (cnt % 3 != 0)
  242. sha256_process_bytes (s_bytes, salt_len, &ctx, nss_ctx);
  243. /* Add key for numbers not divisible by 7. */
  244. if (cnt % 7 != 0)
  245. sha256_process_bytes (p_bytes, key_len, &ctx, nss_ctx);
  246. /* Add key or last result. */
  247. if ((cnt & 1) != 0)
  248. sha256_process_bytes (alt_result, 32, &ctx, nss_ctx);
  249. else
  250. sha256_process_bytes (p_bytes, key_len, &ctx, nss_ctx);
  251. /* Create intermediate result. */
  252. sha256_finish_ctx (&ctx, nss_ctx, alt_result);
  253. }
  254. #ifdef USE_NSS
  255. /* Free libfreebl3 resources. */
  256. NSSLOW_Shutdown (nss_ictx);
  257. #endif
  258. /* Now we can construct the result string. It consists of three
  259. parts. */
  260. cp = __stpncpy (buffer, sha256_salt_prefix, MAX (0, buflen));
  261. buflen -= sizeof (sha256_salt_prefix) - 1;
  262. if (rounds_custom)
  263. {
  264. int n = __snprintf (cp, MAX (0, buflen), "%s%zu$",
  265. sha256_rounds_prefix, rounds);
  266. cp += n;
  267. buflen -= n;
  268. }
  269. cp = __stpncpy (cp, salt, MIN ((size_t) MAX (0, buflen), salt_len));
  270. buflen -= MIN ((size_t) MAX (0, buflen), salt_len);
  271. if (buflen > 0)
  272. {
  273. *cp++ = '$';
  274. --buflen;
  275. }
  276. __b64_from_24bit (&cp, &buflen,
  277. alt_result[0], alt_result[10], alt_result[20], 4);
  278. __b64_from_24bit (&cp, &buflen,
  279. alt_result[21], alt_result[1], alt_result[11], 4);
  280. __b64_from_24bit (&cp, &buflen,
  281. alt_result[12], alt_result[22], alt_result[2], 4);
  282. __b64_from_24bit (&cp, &buflen,
  283. alt_result[3], alt_result[13], alt_result[23], 4);
  284. __b64_from_24bit (&cp, &buflen,
  285. alt_result[24], alt_result[4], alt_result[14], 4);
  286. __b64_from_24bit (&cp, &buflen,
  287. alt_result[15], alt_result[25], alt_result[5], 4);
  288. __b64_from_24bit (&cp, &buflen,
  289. alt_result[6], alt_result[16], alt_result[26], 4);
  290. __b64_from_24bit (&cp, &buflen,
  291. alt_result[27], alt_result[7], alt_result[17], 4);
  292. __b64_from_24bit (&cp, &buflen,
  293. alt_result[18], alt_result[28], alt_result[8], 4);
  294. __b64_from_24bit (&cp, &buflen,
  295. alt_result[9], alt_result[19], alt_result[29], 4);
  296. __b64_from_24bit (&cp, &buflen,
  297. 0, alt_result[31], alt_result[30], 3);
  298. if (buflen <= 0)
  299. {
  300. __set_errno (ERANGE);
  301. buffer = NULL;
  302. }
  303. else
  304. *cp = '\0'; /* Terminate the string. */
  305. /* Clear the buffer for the intermediate result so that people
  306. attaching to processes or reading core dumps cannot get any
  307. information. We do it in this way to clear correct_words[]
  308. inside the SHA256 implementation as well. */
  309. #ifndef USE_NSS
  310. __sha256_init_ctx (&ctx);
  311. __sha256_finish_ctx (&ctx, alt_result);
  312. explicit_bzero (&ctx, sizeof (ctx));
  313. explicit_bzero (&alt_ctx, sizeof (alt_ctx));
  314. #endif
  315. explicit_bzero (temp_result, sizeof (temp_result));
  316. explicit_bzero (p_bytes, key_len);
  317. explicit_bzero (s_bytes, salt_len);
  318. if (copied_key != NULL)
  319. explicit_bzero (copied_key, key_len);
  320. if (copied_salt != NULL)
  321. explicit_bzero (copied_salt, salt_len);
  322. free (free_key);
  323. free (free_pbytes);
  324. return buffer;
  325. }
  326. #ifndef _LIBC
  327. # define libc_freeres_ptr(decl) decl
  328. #endif
  329. libc_freeres_ptr (static char *buffer);
  330. /* This entry point is equivalent to the `crypt' function in Unix
  331. libcs. */
  332. char *
  333. __sha256_crypt (const char *key, const char *salt)
  334. {
  335. /* We don't want to have an arbitrary limit in the size of the
  336. password. We can compute an upper bound for the size of the
  337. result in advance and so we can prepare the buffer we pass to
  338. `sha256_crypt_r'. */
  339. static int buflen;
  340. int needed = (sizeof (sha256_salt_prefix) - 1
  341. + sizeof (sha256_rounds_prefix) + 9 + 1
  342. + strlen (salt) + 1 + 43 + 1);
  343. if (buflen < needed)
  344. {
  345. char *new_buffer = (char *) realloc (buffer, needed);
  346. if (new_buffer == NULL)
  347. return NULL;
  348. buffer = new_buffer;
  349. buflen = needed;
  350. }
  351. return __sha256_crypt_r (key, salt, buffer, buflen);
  352. }
  353. #ifndef _LIBC
  354. static void
  355. __attribute__ ((__destructor__))
  356. free_mem (void)
  357. {
  358. free (buffer);
  359. }
  360. #endif