sha512-crypt.c 14 KB

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