bcrypt.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* $OpenBSD: bcrypt.c,v 1.24 2008/04/02 19:54:05 millert Exp $ */
  2. /*
  3. * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by Niels Provos.
  17. * 4. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  21. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /* This password hashing algorithm was designed by David Mazieres
  32. * <dm@lcs.mit.edu> and works as follows:
  33. *
  34. * 1. state := InitState ()
  35. * 2. state := ExpandKey (state, salt, password) 3.
  36. * REPEAT rounds:
  37. * state := ExpandKey (state, 0, salt)
  38. * state := ExpandKey(state, 0, password)
  39. * 4. ctext := "OrpheanBeholderScryDoubt"
  40. * 5. REPEAT 64:
  41. * ctext := Encrypt_ECB (state, ctext);
  42. * 6. RETURN Concatenate (salt, ctext);
  43. *
  44. */
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <sys/types.h>
  48. #include <string.h>
  49. #include "node_blf.h"
  50. #ifdef _WIN32
  51. #define snprintf _snprintf
  52. #endif
  53. //#if !defined(__APPLE__) && !defined(__MACH__)
  54. //#include "bsd/stdlib.h"
  55. //#endif
  56. /* This implementation is adaptable to current computing power.
  57. * You can have up to 2^31 rounds which should be enough for some
  58. * time to come.
  59. */
  60. /*char *bcrypt(const char *, const char *);
  61. void encode_salt(char *, u_int8_t *, u_int16_t, u_int8_t);
  62. char * bcrypt_gensalt(u_int8_t log_rounds);*/
  63. static void encode_base64(u_int8_t *, u_int8_t *, u_int16_t);
  64. static void decode_base64(u_int8_t *, u_int16_t, u_int8_t *);
  65. const static char* error = ":";
  66. const static u_int8_t Base64Code[] =
  67. "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  68. const static u_int8_t index_64[128] = {
  69. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  70. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  71. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  72. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  73. 255, 255, 255, 255, 255, 255, 0, 1, 54, 55,
  74. 56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
  75. 255, 255, 255, 255, 255, 2, 3, 4, 5, 6,
  76. 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  77. 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
  78. 255, 255, 255, 255, 255, 255, 28, 29, 30,
  79. 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  80. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
  81. 51, 52, 53, 255, 255, 255, 255, 255
  82. };
  83. #define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)])
  84. static void
  85. decode_base64(u_int8_t *buffer, u_int16_t len, u_int8_t *data)
  86. {
  87. u_int8_t *bp = buffer;
  88. u_int8_t *p = data;
  89. u_int8_t c1, c2, c3, c4;
  90. while (bp < buffer + len) {
  91. c1 = CHAR64(*p);
  92. c2 = CHAR64(*(p + 1));
  93. /* Invalid data */
  94. if (c1 == 255 || c2 == 255)
  95. break;
  96. *bp++ = (c1 << 2) | ((c2 & 0x30) >> 4);
  97. if (bp >= buffer + len)
  98. break;
  99. c3 = CHAR64(*(p + 2));
  100. if (c3 == 255)
  101. break;
  102. *bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
  103. if (bp >= buffer + len)
  104. break;
  105. c4 = CHAR64(*(p + 3));
  106. if (c4 == 255)
  107. break;
  108. *bp++ = ((c3 & 0x03) << 6) | c4;
  109. p += 4;
  110. }
  111. }
  112. void
  113. encode_salt(char *salt, u_int8_t *csalt, u_int16_t clen, u_int8_t logr)
  114. {
  115. salt[0] = '$';
  116. salt[1] = BCRYPT_VERSION;
  117. salt[2] = 'a';
  118. salt[3] = '$';
  119. snprintf(salt + 4, 4, "%2.2u$", logr);
  120. encode_base64((u_int8_t *) salt + 7, csalt, clen);
  121. }
  122. /* Generates a salt for this version of crypt.
  123. Since versions may change. Keeping this here
  124. seems sensible.
  125. from: http://mail-index.netbsd.org/tech-crypto/2002/05/24/msg000204.html
  126. */
  127. void
  128. bcrypt_gensalt(u_int8_t log_rounds, u_int8_t *seed, char *gsalt)
  129. {
  130. if (log_rounds < 4)
  131. log_rounds = 4;
  132. else if (log_rounds > 31)
  133. log_rounds = 31;
  134. encode_salt(gsalt, seed, BCRYPT_MAXSALT, log_rounds);
  135. }
  136. /* We handle $Vers$log2(NumRounds)$salt+passwd$
  137. i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */
  138. void
  139. bcrypt(const char *key, const char *salt, char *encrypted)
  140. {
  141. blf_ctx state;
  142. u_int32_t rounds, i, k;
  143. u_int16_t j;
  144. u_int8_t key_len, salt_len, logr, minor;
  145. u_int8_t ciphertext[4 * BCRYPT_BLOCKS+1] = "OrpheanBeholderScryDoubt";
  146. u_int8_t csalt[BCRYPT_MAXSALT];
  147. u_int32_t cdata[BCRYPT_BLOCKS];
  148. int n;
  149. /* Discard "$" identifier */
  150. salt++;
  151. if (*salt > BCRYPT_VERSION) {
  152. /* How do I handle errors ? Return ':' */
  153. strcpy(encrypted, error);
  154. return;
  155. }
  156. /* Check for minor versions */
  157. if (salt[1] != '$') {
  158. switch (salt[1]) {
  159. case 'a':
  160. /* 'ab' should not yield the same as 'abab' */
  161. minor = salt[1];
  162. salt++;
  163. break;
  164. default:
  165. strcpy(encrypted, error);
  166. return;
  167. }
  168. } else
  169. minor = 0;
  170. /* Discard version + "$" identifier */
  171. salt += 2;
  172. if (salt[2] != '$') {
  173. /* Out of sync with passwd entry */
  174. strcpy(encrypted, error);
  175. return;
  176. }
  177. /* Computer power doesn't increase linear, 2^x should be fine */
  178. n = atoi(salt);
  179. if (n > 31 || n < 0) {
  180. strcpy(encrypted, error);
  181. return;
  182. }
  183. logr = (u_int8_t)n;
  184. if ((rounds = (u_int32_t) 1 << logr) < BCRYPT_MINROUNDS) {
  185. strcpy(encrypted, error);
  186. return;
  187. }
  188. /* Discard num rounds + "$" identifier */
  189. salt += 3;
  190. if (strlen(salt) * 3 / 4 < BCRYPT_MAXSALT) {
  191. strcpy(encrypted, error);
  192. return;
  193. }
  194. /* We dont want the base64 salt but the raw data */
  195. decode_base64(csalt, BCRYPT_MAXSALT, (u_int8_t *) salt);
  196. salt_len = BCRYPT_MAXSALT;
  197. key_len = strlen(key) + (minor >= 'a' ? 1 : 0);
  198. /* Setting up S-Boxes and Subkeys */
  199. Blowfish_initstate(&state);
  200. Blowfish_expandstate(&state, csalt, salt_len,
  201. (u_int8_t *) key, key_len);
  202. for (k = 0; k < rounds; k++) {
  203. Blowfish_expand0state(&state, (u_int8_t *) key, key_len);
  204. Blowfish_expand0state(&state, csalt, salt_len);
  205. }
  206. /* This can be precomputed later */
  207. j = 0;
  208. for (i = 0; i < BCRYPT_BLOCKS; i++)
  209. cdata[i] = Blowfish_stream2word(ciphertext, 4 * BCRYPT_BLOCKS, &j);
  210. /* Now do the encryption */
  211. for (k = 0; k < 64; k++)
  212. blf_enc(&state, cdata, BCRYPT_BLOCKS / 2);
  213. for (i = 0; i < BCRYPT_BLOCKS; i++) {
  214. ciphertext[4 * i + 3] = cdata[i] & 0xff;
  215. cdata[i] = cdata[i] >> 8;
  216. ciphertext[4 * i + 2] = cdata[i] & 0xff;
  217. cdata[i] = cdata[i] >> 8;
  218. ciphertext[4 * i + 1] = cdata[i] & 0xff;
  219. cdata[i] = cdata[i] >> 8;
  220. ciphertext[4 * i + 0] = cdata[i] & 0xff;
  221. }
  222. i = 0;
  223. encrypted[i++] = '$';
  224. encrypted[i++] = BCRYPT_VERSION;
  225. if (minor)
  226. encrypted[i++] = minor;
  227. encrypted[i++] = '$';
  228. snprintf(encrypted + i, 4, "%2.2u$", logr);
  229. encode_base64((u_int8_t *) encrypted + i + 3, csalt, BCRYPT_MAXSALT);
  230. encode_base64((u_int8_t *) encrypted + strlen(encrypted), ciphertext,
  231. 4 * BCRYPT_BLOCKS - 1);
  232. memset(&state, 0, sizeof(state));
  233. memset(ciphertext, 0, sizeof(ciphertext));
  234. memset(csalt, 0, sizeof(csalt));
  235. memset(cdata, 0, sizeof(cdata));
  236. }
  237. u_int32_t bcrypt_get_rounds(const char * hash)
  238. {
  239. /* skip past the leading "$" */
  240. if (!hash || *(hash++) != '$') return 0;
  241. /* skip past version */
  242. if (0 == (*hash++)) return 0;
  243. if (*hash && *hash != '$') hash++;
  244. if (*hash++ != '$') return 0;
  245. return atoi(hash);
  246. }
  247. static void
  248. encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len)
  249. {
  250. u_int8_t *bp = buffer;
  251. u_int8_t *p = data;
  252. u_int8_t c1, c2;
  253. while (p < data + len) {
  254. c1 = *p++;
  255. *bp++ = Base64Code[(c1 >> 2)];
  256. c1 = (c1 & 0x03) << 4;
  257. if (p >= data + len) {
  258. *bp++ = Base64Code[c1];
  259. break;
  260. }
  261. c2 = *p++;
  262. c1 |= (c2 >> 4) & 0x0f;
  263. *bp++ = Base64Code[c1];
  264. c1 = (c2 & 0x0f) << 2;
  265. if (p >= data + len) {
  266. *bp++ = Base64Code[c1];
  267. break;
  268. }
  269. c2 = *p++;
  270. c1 |= (c2 >> 6) & 0x03;
  271. *bp++ = Base64Code[c1];
  272. *bp++ = Base64Code[c2 & 0x3f];
  273. }
  274. *bp = '\0';
  275. }