crypt-sha256.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* One way encryption based on the SHA256-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 648 up to 909.
  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-sha256.h"
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #if INCLUDE_sha256crypt
  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. /* The maximum possible length of a SHA256-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 (43 chars). */
  42. #define LENGTH_OF_NUMBER(n) (sizeof #n - 1)
  43. #define SHA256_HASH_LENGTH \
  44. (sizeof (sha256_salt_prefix) + sizeof (sha256_rounds_prefix) + \
  45. LENGTH_OF_NUMBER (ROUNDS_MAX) + SALT_LEN_MAX + 1 + 43)
  46. static_assert (SHA256_HASH_LENGTH <= CRYPT_OUTPUT_SIZE,
  47. "CRYPT_OUTPUT_SIZE is too small for SHA256");
  48. /* A sha256_buffer holds all of the sensitive intermediate data. */
  49. struct sha256_buffer
  50. {
  51. SHA256_CTX ctx;
  52. uint8_t result[32];
  53. uint8_t p_bytes[32];
  54. uint8_t s_bytes[32];
  55. };
  56. static_assert (sizeof (struct sha256_buffer) <= ALG_SPECIFIC_SIZE,
  57. "ALG_SPECIFIC_SIZE is too small for SHA256");
  58. /* Feed CTX with LEN bytes of a virtual byte sequence consisting of
  59. BLOCK repeated over and over indefinitely. */
  60. static void
  61. SHA256_Update_recycled (SHA256_CTX *ctx,
  62. unsigned char block[32], size_t len)
  63. {
  64. size_t cnt;
  65. for (cnt = len; cnt >= 32; cnt -= 32)
  66. SHA256_Update (ctx, block, 32);
  67. SHA256_Update (ctx, block, cnt);
  68. }
  69. void
  70. crypt_sha256crypt_rn (const char *phrase, size_t phr_size,
  71. const char *setting, size_t ARG_UNUSED (set_size),
  72. uint8_t *output, size_t out_size,
  73. void *scratch, size_t scr_size)
  74. {
  75. /* This shouldn't ever happen, but... */
  76. if (out_size < SHA256_HASH_LENGTH
  77. || scr_size < sizeof (struct sha256_buffer))
  78. {
  79. errno = ERANGE;
  80. return;
  81. }
  82. struct sha256_buffer *buf = scratch;
  83. SHA256_CTX *ctx = &buf->ctx;
  84. uint8_t *result = buf->result;
  85. uint8_t *p_bytes = buf->p_bytes;
  86. uint8_t *s_bytes = buf->s_bytes;
  87. char *cp = (char *)output;
  88. const char *salt = setting;
  89. size_t salt_size;
  90. size_t cnt;
  91. /* Default number of rounds. */
  92. size_t rounds = ROUNDS_DEFAULT;
  93. bool rounds_custom = false;
  94. /* Find beginning of salt string. The prefix should normally always
  95. be present. Just in case it is not. */
  96. if (strncmp (sha256_salt_prefix, salt, sizeof (sha256_salt_prefix) - 1) == 0)
  97. /* Skip salt prefix. */
  98. salt += sizeof (sha256_salt_prefix) - 1;
  99. if (strncmp (salt, sha256_rounds_prefix, sizeof (sha256_rounds_prefix) - 1)
  100. == 0)
  101. {
  102. const char *num = salt + sizeof (sha256_rounds_prefix) - 1;
  103. /* Do not allow an explicit setting of zero rounds, nor of the
  104. default number of rounds, nor leading zeroes on the rounds. */
  105. if (!(*num >= '1' && *num <= '9'))
  106. {
  107. errno = EINVAL;
  108. return;
  109. }
  110. errno = 0;
  111. char *endp;
  112. rounds = strtoul (num, &endp, 10);
  113. if (endp == num || *endp != '$'
  114. || rounds < ROUNDS_MIN
  115. || rounds > ROUNDS_MAX
  116. || errno)
  117. {
  118. errno = EINVAL;
  119. return;
  120. }
  121. salt = endp + 1;
  122. rounds_custom = true;
  123. }
  124. /* The salt ends at the next '$' or the end of the string.
  125. Ensure ':' does not appear in the salt (it is used as a separator in /etc/passwd).
  126. Also check for '\n', as in /etc/passwd the whole parameters of the user data must
  127. be on a single line. */
  128. salt_size = strcspn (salt, "$:\n");
  129. if (!(salt[salt_size] == '$' || !salt[salt_size]))
  130. {
  131. errno = EINVAL;
  132. return;
  133. }
  134. /* Ensure we do not use more salt than SALT_LEN_MAX. */
  135. if (salt_size > SALT_LEN_MAX)
  136. salt_size = SALT_LEN_MAX;
  137. /* Compute alternate SHA256 sum with input PHRASE, SALT, and PHRASE. The
  138. final result will be added to the first context. */
  139. SHA256_Init (ctx);
  140. /* Add phrase. */
  141. SHA256_Update (ctx, phrase, phr_size);
  142. /* Add salt. */
  143. SHA256_Update (ctx, salt, salt_size);
  144. /* Add phrase again. */
  145. SHA256_Update (ctx, phrase, phr_size);
  146. /* Now get result of this (32 bytes). */
  147. SHA256_Final (result, ctx);
  148. /* Prepare for the real work. */
  149. SHA256_Init (ctx);
  150. /* Add the phrase string. */
  151. SHA256_Update (ctx, phrase, phr_size);
  152. /* The last part is the salt string. This must be at most 8
  153. characters and it ends at the first `$' character (for
  154. compatibility with existing implementations). */
  155. SHA256_Update (ctx, salt, salt_size);
  156. /* Add for any character in the phrase one byte of the alternate sum. */
  157. for (cnt = phr_size; cnt > 32; cnt -= 32)
  158. SHA256_Update (ctx, result, 32);
  159. SHA256_Update (ctx, result, cnt);
  160. /* Take the binary representation of the length of the phrase and for every
  161. 1 add the alternate sum, for every 0 the phrase. */
  162. for (cnt = phr_size; cnt > 0; cnt >>= 1)
  163. if ((cnt & 1) != 0)
  164. SHA256_Update (ctx, result, 32);
  165. else
  166. SHA256_Update (ctx, phrase, phr_size);
  167. /* Create intermediate result. */
  168. SHA256_Final (result, ctx);
  169. /* Start computation of P byte sequence. */
  170. SHA256_Init (ctx);
  171. /* For every character in the password add the entire password. */
  172. for (cnt = 0; cnt < phr_size; ++cnt)
  173. SHA256_Update (ctx, phrase, phr_size);
  174. /* Finish the digest. */
  175. SHA256_Final (p_bytes, ctx);
  176. /* Start computation of S byte sequence. */
  177. SHA256_Init (ctx);
  178. /* For every character in the password add the entire password. */
  179. for (cnt = 0; cnt < (size_t) 16 + (size_t) result[0]; ++cnt)
  180. SHA256_Update (ctx, salt, salt_size);
  181. /* Finish the digest. */
  182. SHA256_Final (s_bytes, ctx);
  183. /* Repeatedly run the collected hash value through SHA256 to burn
  184. CPU cycles. */
  185. for (cnt = 0; cnt < rounds; ++cnt)
  186. {
  187. /* New context. */
  188. SHA256_Init (ctx);
  189. /* Add phrase or last result. */
  190. if ((cnt & 1) != 0)
  191. SHA256_Update_recycled (ctx, p_bytes, phr_size);
  192. else
  193. SHA256_Update (ctx, result, 32);
  194. /* Add salt for numbers not divisible by 3. */
  195. if (cnt % 3 != 0)
  196. SHA256_Update_recycled (ctx, s_bytes, salt_size);
  197. /* Add phrase for numbers not divisible by 7. */
  198. if (cnt % 7 != 0)
  199. SHA256_Update_recycled (ctx, p_bytes, phr_size);
  200. /* Add phrase or last result. */
  201. if ((cnt & 1) != 0)
  202. SHA256_Update (ctx, result, 32);
  203. else
  204. SHA256_Update_recycled (ctx, p_bytes, phr_size);
  205. /* Create intermediate result. */
  206. SHA256_Final (result, ctx);
  207. }
  208. /* Now we can construct the result string. It consists of four
  209. parts, one of which is optional. We already know that there
  210. is sufficient space at CP for the longest possible result string. */
  211. memcpy (cp, sha256_salt_prefix, sizeof (sha256_salt_prefix) - 1);
  212. cp += sizeof (sha256_salt_prefix) - 1;
  213. if (rounds_custom)
  214. {
  215. int n = snprintf (cp,
  216. SHA256_HASH_LENGTH - (sizeof (sha256_salt_prefix) - 1),
  217. "%s%zu$", sha256_rounds_prefix, rounds);
  218. cp += n;
  219. }
  220. memcpy (cp, salt, salt_size);
  221. cp += salt_size;
  222. *cp++ = '$';
  223. #define b64_from_24bit(B2, B1, B0, N) \
  224. do { \
  225. unsigned int w = ((((unsigned int)(B2)) << 16) | \
  226. (((unsigned int)(B1)) << 8) | \
  227. ((unsigned int)(B0))); \
  228. int n = (N); \
  229. while (n-- > 0) \
  230. { \
  231. *cp++ = b64t[w & 0x3f]; \
  232. w >>= 6; \
  233. } \
  234. } while (0)
  235. b64_from_24bit (result[0], result[10], result[20], 4);
  236. b64_from_24bit (result[21], result[1], result[11], 4);
  237. b64_from_24bit (result[12], result[22], result[2], 4);
  238. b64_from_24bit (result[3], result[13], result[23], 4);
  239. b64_from_24bit (result[24], result[4], result[14], 4);
  240. b64_from_24bit (result[15], result[25], result[5], 4);
  241. b64_from_24bit (result[6], result[16], result[26], 4);
  242. b64_from_24bit (result[27], result[7], result[17], 4);
  243. b64_from_24bit (result[18], result[28], result[8], 4);
  244. b64_from_24bit (result[9], result[19], result[29], 4);
  245. b64_from_24bit (0, result[31], result[30], 3);
  246. *cp = '\0';
  247. }
  248. #ifndef NO_GENSALT
  249. void
  250. gensalt_sha256crypt_rn (unsigned long count,
  251. const uint8_t *rbytes, size_t nrbytes,
  252. uint8_t *output, size_t output_size)
  253. {
  254. gensalt_sha_rn ('5', SALT_LEN_MAX, ROUNDS_DEFAULT, ROUNDS_MIN, ROUNDS_MAX,
  255. count, rbytes, nrbytes, output, output_size);
  256. }
  257. #endif
  258. #endif