crypt-sha512.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* One way encryption based on the SHA512-based Unix crypt implementation.
  2. *
  3. * Written by Ulrich Drepper <drepper at redhat.com> in 2007 [1].
  4. * Modified by Zack Weinberg <zackw at panix.com> in 2017, 2018.
  5. * Composed by Björn Esser <besser82 at fedoraproject.org> in 2018.
  6. * Modified by Björn Esser <besser82 at fedoraproject.org> in 2020.
  7. * Modified by Steffen Jaeckel <jaeckel-floss at eyet-services.de> in 2020.
  8. * To the extent possible under law, the named authors have waived all
  9. * copyright and related or neighboring rights to this work.
  10. *
  11. * See https://creativecommons.org/publicdomain/zero/1.0/ for further
  12. * details.
  13. *
  14. * This file is a modified except from [2], lines 1403 up to 1676.
  15. *
  16. * [1] https://www.akkadia.org/drepper/sha-crypt.html
  17. * [2] https://www.akkadia.org/drepper/SHA-crypt.txt
  18. */
  19. #include "crypt-port.h"
  20. #include "alg-sha512.h"
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #if INCLUDE_sha512crypt
  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. /* The maximum possible length of a SHA512-hashed password string,
  39. including the terminating NUL character. Prefix (including its NUL)
  40. + rounds tag ("rounds=$" = "rounds=\0") + strlen(ROUNDS_MAX)
  41. + salt (up to SALT_LEN_MAX chars) + '$' + hash (86 chars). */
  42. #define LENGTH_OF_NUMBER(n) (sizeof #n - 1)
  43. #define SHA512_HASH_LENGTH \
  44. (sizeof (sha512_salt_prefix) + sizeof (sha512_rounds_prefix) + \
  45. LENGTH_OF_NUMBER (ROUNDS_MAX) + SALT_LEN_MAX + 1 + 86)
  46. static_assert (SHA512_HASH_LENGTH <= CRYPT_OUTPUT_SIZE,
  47. "CRYPT_OUTPUT_SIZE is too small for SHA512");
  48. /* A sha512_buffer holds all of the sensitive intermediate data. */
  49. struct sha512_buffer
  50. {
  51. SHA512_CTX ctx;
  52. uint8_t result[64];
  53. uint8_t p_bytes[64];
  54. uint8_t s_bytes[64];
  55. };
  56. static_assert (sizeof (struct sha512_buffer) <= ALG_SPECIFIC_SIZE,
  57. "ALG_SPECIFIC_SIZE is too small for SHA512");
  58. /* Subroutine of _xcrypt_crypt_sha512crypt_rn: Feed CTX with LEN bytes of a
  59. virtual byte sequence consisting of BLOCK repeated over and over
  60. indefinitely. */
  61. static void
  62. sha512_process_recycled_bytes (unsigned char block[64], size_t len,
  63. SHA512_CTX *ctx)
  64. {
  65. size_t cnt;
  66. for (cnt = len; cnt >= 64; cnt -= 64)
  67. SHA512_Update (ctx, block, 64);
  68. SHA512_Update (ctx, block, cnt);
  69. }
  70. void
  71. crypt_sha512crypt_rn (const char *phrase, size_t phr_size,
  72. const char *setting, size_t ARG_UNUSED (set_size),
  73. uint8_t *output, size_t out_size,
  74. void *scratch, size_t scr_size)
  75. {
  76. /* This shouldn't ever happen, but... */
  77. if (out_size < SHA512_HASH_LENGTH
  78. || scr_size < sizeof (struct sha512_buffer))
  79. {
  80. errno = ERANGE;
  81. return;
  82. }
  83. struct sha512_buffer *buf = scratch;
  84. SHA512_CTX *ctx = &buf->ctx;
  85. uint8_t *result = buf->result;
  86. uint8_t *p_bytes = buf->p_bytes;
  87. uint8_t *s_bytes = buf->s_bytes;
  88. char *cp = (char *)output;
  89. const char *salt = setting;
  90. size_t salt_size;
  91. size_t cnt;
  92. /* Default number of rounds. */
  93. size_t rounds = ROUNDS_DEFAULT;
  94. bool rounds_custom = false;
  95. /* Find beginning of salt string. The prefix should normally always
  96. be present. Just in case it is not. */
  97. if (strncmp (sha512_salt_prefix, salt, sizeof (sha512_salt_prefix) - 1) == 0)
  98. /* Skip salt prefix. */
  99. salt += sizeof (sha512_salt_prefix) - 1;
  100. if (strncmp (salt, sha512_rounds_prefix, sizeof (sha512_rounds_prefix) - 1)
  101. == 0)
  102. {
  103. const char *num = salt + sizeof (sha512_rounds_prefix) - 1;
  104. /* Do not allow an explicit setting of zero rounds, nor of the
  105. default number of rounds, nor leading zeroes on the rounds. */
  106. if (!(*num >= '1' && *num <= '9'))
  107. {
  108. errno = EINVAL;
  109. return;
  110. }
  111. errno = 0;
  112. char *endp;
  113. rounds = strtoul (num, &endp, 10);
  114. if (endp == num || *endp != '$'
  115. || rounds < ROUNDS_MIN
  116. || rounds > ROUNDS_MAX
  117. || errno)
  118. {
  119. errno = EINVAL;
  120. return;
  121. }
  122. salt = endp + 1;
  123. rounds_custom = true;
  124. }
  125. /* The salt ends at the next '$' or the end of the string.
  126. Ensure ':' does not appear in the salt (it is used as a separator in /etc/passwd).
  127. Also check for '\n', as in /etc/passwd the whole parameters of the user data must
  128. be on a single line. */
  129. salt_size = strcspn (salt, "$:\n");
  130. if (!(salt[salt_size] == '$' || !salt[salt_size]))
  131. {
  132. errno = EINVAL;
  133. return;
  134. }
  135. /* Ensure we do not use more salt than SALT_LEN_MAX. */
  136. if (salt_size > SALT_LEN_MAX)
  137. salt_size = SALT_LEN_MAX;
  138. /* Compute alternate SHA512 sum with input PHRASE, SALT, and PHRASE. The
  139. final result will be added to the first context. */
  140. SHA512_Init (ctx);
  141. /* Add phrase. */
  142. SHA512_Update (ctx, phrase, phr_size);
  143. /* Add salt. */
  144. SHA512_Update (ctx, salt, salt_size);
  145. /* Add phrase again. */
  146. SHA512_Update (ctx, phrase, phr_size);
  147. /* Now get result of this (64 bytes) and add it to the other
  148. context. */
  149. SHA512_Final (result, ctx);
  150. /* Prepare for the real work. */
  151. SHA512_Init (ctx);
  152. /* Add the phrase string. */
  153. SHA512_Update (ctx, phrase, phr_size);
  154. /* The last part is the salt string. This must be at most 8
  155. characters and it ends at the first `$' character (for
  156. compatibility with existing implementations). */
  157. SHA512_Update (ctx, salt, salt_size);
  158. /* Add for any character in the phrase one byte of the alternate sum. */
  159. for (cnt = phr_size; cnt > 64; cnt -= 64)
  160. SHA512_Update (ctx, result, 64);
  161. SHA512_Update (ctx, result, cnt);
  162. /* Take the binary representation of the length of the phrase and for every
  163. 1 add the alternate sum, for every 0 the phrase. */
  164. for (cnt = phr_size; cnt > 0; cnt >>= 1)
  165. if ((cnt & 1) != 0)
  166. SHA512_Update (ctx, result, 64);
  167. else
  168. SHA512_Update (ctx, phrase, phr_size);
  169. /* Create intermediate result. */
  170. SHA512_Final (result, ctx);
  171. /* Start computation of P byte sequence. */
  172. SHA512_Init (ctx);
  173. /* For every character in the password add the entire password. */
  174. for (cnt = 0; cnt < phr_size; ++cnt)
  175. SHA512_Update (ctx, phrase, phr_size);
  176. /* Finish the digest. */
  177. SHA512_Final (p_bytes, ctx);
  178. /* Start computation of S byte sequence. */
  179. SHA512_Init (ctx);
  180. /* For every character in the password add the entire password. */
  181. for (cnt = 0; cnt < (size_t) 16 + (size_t) result[0]; ++cnt)
  182. SHA512_Update (ctx, salt, salt_size);
  183. /* Finish the digest. */
  184. SHA512_Final (s_bytes, ctx);
  185. /* Repeatedly run the collected hash value through SHA512 to burn
  186. CPU cycles. */
  187. for (cnt = 0; cnt < rounds; ++cnt)
  188. {
  189. /* New context. */
  190. SHA512_Init (ctx);
  191. /* Add phrase or last result. */
  192. if ((cnt & 1) != 0)
  193. sha512_process_recycled_bytes (p_bytes, phr_size, ctx);
  194. else
  195. SHA512_Update (ctx, result, 64);
  196. /* Add salt for numbers not divisible by 3. */
  197. if (cnt % 3 != 0)
  198. sha512_process_recycled_bytes (s_bytes, salt_size, ctx);
  199. /* Add phrase for numbers not divisible by 7. */
  200. if (cnt % 7 != 0)
  201. sha512_process_recycled_bytes (p_bytes, phr_size, ctx);
  202. /* Add phrase or last result. */
  203. if ((cnt & 1) != 0)
  204. SHA512_Update (ctx, result, 64);
  205. else
  206. sha512_process_recycled_bytes (p_bytes, phr_size, ctx);
  207. /* Create intermediate result. */
  208. SHA512_Final (result, ctx);
  209. }
  210. /* Now we can construct the result string. It consists of four
  211. parts, one of which is optional. We already know that buflen is
  212. at least sha512_hash_length, therefore none of the string bashing
  213. below can overflow the buffer. */
  214. memcpy (cp, sha512_salt_prefix, sizeof (sha512_salt_prefix) - 1);
  215. cp += sizeof (sha512_salt_prefix) - 1;
  216. if (rounds_custom)
  217. {
  218. int n = snprintf (cp,
  219. SHA512_HASH_LENGTH - (sizeof (sha512_salt_prefix) - 1),
  220. "%s%zu$", sha512_rounds_prefix, rounds);
  221. cp += n;
  222. }
  223. memcpy (cp, salt, salt_size);
  224. cp += salt_size;
  225. *cp++ = '$';
  226. #define b64_from_24bit(B2, B1, B0, N) \
  227. do { \
  228. unsigned int w = ((((unsigned int)(B2)) << 16) | \
  229. (((unsigned int)(B1)) << 8) | \
  230. ((unsigned int)(B0))); \
  231. int n = (N); \
  232. while (n-- > 0) \
  233. { \
  234. *cp++ = b64t[w & 0x3f]; \
  235. w >>= 6; \
  236. } \
  237. } while (0)
  238. b64_from_24bit (result[0], result[21], result[42], 4);
  239. b64_from_24bit (result[22], result[43], result[1], 4);
  240. b64_from_24bit (result[44], result[2], result[23], 4);
  241. b64_from_24bit (result[3], result[24], result[45], 4);
  242. b64_from_24bit (result[25], result[46], result[4], 4);
  243. b64_from_24bit (result[47], result[5], result[26], 4);
  244. b64_from_24bit (result[6], result[27], result[48], 4);
  245. b64_from_24bit (result[28], result[49], result[7], 4);
  246. b64_from_24bit (result[50], result[8], result[29], 4);
  247. b64_from_24bit (result[9], result[30], result[51], 4);
  248. b64_from_24bit (result[31], result[52], result[10], 4);
  249. b64_from_24bit (result[53], result[11], result[32], 4);
  250. b64_from_24bit (result[12], result[33], result[54], 4);
  251. b64_from_24bit (result[34], result[55], result[13], 4);
  252. b64_from_24bit (result[56], result[14], result[35], 4);
  253. b64_from_24bit (result[15], result[36], result[57], 4);
  254. b64_from_24bit (result[37], result[58], result[16], 4);
  255. b64_from_24bit (result[59], result[17], result[38], 4);
  256. b64_from_24bit (result[18], result[39], result[60], 4);
  257. b64_from_24bit (result[40], result[61], result[19], 4);
  258. b64_from_24bit (result[62], result[20], result[41], 4);
  259. b64_from_24bit (0, 0, result[63], 2);
  260. *cp = '\0';
  261. }
  262. #ifndef NO_GENSALT
  263. void
  264. gensalt_sha512crypt_rn (unsigned long count,
  265. const uint8_t *rbytes, size_t nrbytes,
  266. uint8_t *output, size_t output_size)
  267. {
  268. gensalt_sha_rn ('6', SALT_LEN_MAX, ROUNDS_DEFAULT, ROUNDS_MIN, ROUNDS_MAX,
  269. count, rbytes, nrbytes, output, output_size);
  270. }
  271. #endif
  272. #endif