des.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * FreeSec: libcrypt for NetBSD
  3. *
  4. * Copyright (c) 1994 David Burren
  5. * All rights reserved.
  6. *
  7. * Adapted for FreeBSD-2.0 by Geoffrey M. Rehmet
  8. * this file should now *only* export crypt(), in order to make
  9. * binaries of libcrypt exportable from the USA
  10. *
  11. * Adapted for FreeBSD-4.0 by Mark R V Murray
  12. * this file should now *only* export crypt_des(), in order to make
  13. * a module that can be optionally included in libcrypt.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * 3. Neither the name of the author nor the names of other contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  28. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37. * SUCH DAMAGE.
  38. *
  39. * This is an original implementation of the DES and the crypt(3) interfaces
  40. * by David Burren <davidb@werj.com.au>.
  41. *
  42. * An excellent reference on the underlying algorithm (and related
  43. * algorithms) is:
  44. *
  45. * B. Schneier, Applied Cryptography: protocols, algorithms,
  46. * and source code in C, John Wiley & Sons, 1994.
  47. *
  48. * Note that in that book's description of DES the lookups for the initial,
  49. * pbox, and final permutations are inverted (this has been brought to the
  50. * attention of the author). A list of errata for this book has been
  51. * posted to the sci.crypt newsgroup by the author and is available for FTP.
  52. *
  53. * ARCHITECTURE ASSUMPTIONS:
  54. * It is assumed that the 8-byte arrays passed by reference can be
  55. * addressed as arrays of u_int32_t's (ie. the CPU is not picky about
  56. * alignment).
  57. */
  58. #include <sys/cdefs.h>
  59. #include <sys/types.h>
  60. #include <sys/param.h>
  61. #include <netinet/in.h>
  62. #include <pwd.h>
  63. #include <string.h>
  64. #include <crypt.h>
  65. #include "libcrypt.h"
  66. #include "des_tables.c"
  67. /* Re-entrantify me -- all this junk needs to be in
  68. * struct crypt_data to make this really reentrant... */
  69. static u_int32_t en_keysl[16], en_keysr[16];
  70. static u_int32_t de_keysl[16], de_keysr[16];
  71. static u_int32_t saltbits;
  72. static u_int32_t old_salt;
  73. static u_int32_t old_rawkey0, old_rawkey1;
  74. /* A pile of data */
  75. static const u_char ascii64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  76. static const u_char key_shifts[16] = {
  77. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  78. };
  79. static const u_int32_t bits32[32] =
  80. {
  81. 0x80000000, 0x40000000, 0x20000000, 0x10000000,
  82. 0x08000000, 0x04000000, 0x02000000, 0x01000000,
  83. 0x00800000, 0x00400000, 0x00200000, 0x00100000,
  84. 0x00080000, 0x00040000, 0x00020000, 0x00010000,
  85. 0x00008000, 0x00004000, 0x00002000, 0x00001000,
  86. 0x00000800, 0x00000400, 0x00000200, 0x00000100,
  87. 0x00000080, 0x00000040, 0x00000020, 0x00000010,
  88. 0x00000008, 0x00000004, 0x00000002, 0x00000001
  89. };
  90. static const u_char bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
  91. static int
  92. ascii_to_bin(char ch)
  93. {
  94. if (ch > 'z')
  95. return(0);
  96. if (ch >= 'a')
  97. return(ch - 'a' + 38);
  98. if (ch > 'Z')
  99. return(0);
  100. if (ch >= 'A')
  101. return(ch - 'A' + 12);
  102. if (ch > '9')
  103. return(0);
  104. if (ch >= '.')
  105. return(ch - '.');
  106. return(0);
  107. }
  108. static void
  109. des_init(void)
  110. {
  111. static int des_initialised = 0;
  112. if (des_initialised==1)
  113. return;
  114. old_rawkey0 = old_rawkey1 = 0L;
  115. saltbits = 0L;
  116. old_salt = 0L;
  117. des_initialised = 1;
  118. }
  119. static void
  120. setup_salt(u_int32_t salt)
  121. {
  122. u_int32_t obit, saltbit;
  123. int i;
  124. if (salt == old_salt)
  125. return;
  126. old_salt = salt;
  127. saltbits = 0L;
  128. saltbit = 1;
  129. obit = 0x800000;
  130. for (i = 0; i < 24; i++) {
  131. if (salt & saltbit)
  132. saltbits |= obit;
  133. saltbit <<= 1;
  134. obit >>= 1;
  135. }
  136. }
  137. static void
  138. des_setkey(const char *key)
  139. {
  140. u_int32_t k0, k1, rawkey0, rawkey1;
  141. int shifts, round;
  142. des_init();
  143. rawkey0 = ntohl(*(const u_int32_t *) key);
  144. rawkey1 = ntohl(*(const u_int32_t *) (key + 4));
  145. if ((rawkey0 | rawkey1)
  146. && rawkey0 == old_rawkey0
  147. && rawkey1 == old_rawkey1) {
  148. /*
  149. * Already setup for this key.
  150. * This optimisation fails on a zero key (which is weak and
  151. * has bad parity anyway) in order to simplify the starting
  152. * conditions.
  153. */
  154. return;
  155. }
  156. old_rawkey0 = rawkey0;
  157. old_rawkey1 = rawkey1;
  158. /*
  159. * Do key permutation and split into two 28-bit subkeys.
  160. */
  161. k0 = key_perm_maskl[0][rawkey0 >> 25]
  162. | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f]
  163. | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f]
  164. | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f]
  165. | key_perm_maskl[4][rawkey1 >> 25]
  166. | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f]
  167. | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f]
  168. | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f];
  169. k1 = key_perm_maskr[0][rawkey0 >> 25]
  170. | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f]
  171. | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f]
  172. | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f]
  173. | key_perm_maskr[4][rawkey1 >> 25]
  174. | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f]
  175. | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f]
  176. | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f];
  177. /*
  178. * Rotate subkeys and do compression permutation.
  179. */
  180. shifts = 0;
  181. for (round = 0; round < 16; round++) {
  182. u_int32_t t0, t1;
  183. shifts += key_shifts[round];
  184. t0 = (k0 << shifts) | (k0 >> (28 - shifts));
  185. t1 = (k1 << shifts) | (k1 >> (28 - shifts));
  186. de_keysl[15 - round] =
  187. en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f]
  188. | comp_maskl[1][(t0 >> 14) & 0x7f]
  189. | comp_maskl[2][(t0 >> 7) & 0x7f]
  190. | comp_maskl[3][t0 & 0x7f]
  191. | comp_maskl[4][(t1 >> 21) & 0x7f]
  192. | comp_maskl[5][(t1 >> 14) & 0x7f]
  193. | comp_maskl[6][(t1 >> 7) & 0x7f]
  194. | comp_maskl[7][t1 & 0x7f];
  195. de_keysr[15 - round] =
  196. en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f]
  197. | comp_maskr[1][(t0 >> 14) & 0x7f]
  198. | comp_maskr[2][(t0 >> 7) & 0x7f]
  199. | comp_maskr[3][t0 & 0x7f]
  200. | comp_maskr[4][(t1 >> 21) & 0x7f]
  201. | comp_maskr[5][(t1 >> 14) & 0x7f]
  202. | comp_maskr[6][(t1 >> 7) & 0x7f]
  203. | comp_maskr[7][t1 & 0x7f];
  204. }
  205. }
  206. static int
  207. do_des( u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out, int count)
  208. {
  209. /* l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. */
  210. u_int32_t l, r, *kl, *kr, *kl1, *kr1;
  211. u_int32_t f, r48l, r48r;
  212. int round;
  213. if (count == 0) {
  214. return 1;
  215. }
  216. if (count > 0) {
  217. /* Encrypting */
  218. kl1 = en_keysl;
  219. kr1 = en_keysr;
  220. } else {
  221. /* Decrypting */
  222. count = -count;
  223. kl1 = de_keysl;
  224. kr1 = de_keysr;
  225. }
  226. /* Do initial permutation (IP). */
  227. l = ip_maskl[0][l_in >> 24]
  228. | ip_maskl[1][(l_in >> 16) & 0xff]
  229. | ip_maskl[2][(l_in >> 8) & 0xff]
  230. | ip_maskl[3][l_in & 0xff]
  231. | ip_maskl[4][r_in >> 24]
  232. | ip_maskl[5][(r_in >> 16) & 0xff]
  233. | ip_maskl[6][(r_in >> 8) & 0xff]
  234. | ip_maskl[7][r_in & 0xff];
  235. r = ip_maskr[0][l_in >> 24]
  236. | ip_maskr[1][(l_in >> 16) & 0xff]
  237. | ip_maskr[2][(l_in >> 8) & 0xff]
  238. | ip_maskr[3][l_in & 0xff]
  239. | ip_maskr[4][r_in >> 24]
  240. | ip_maskr[5][(r_in >> 16) & 0xff]
  241. | ip_maskr[6][(r_in >> 8) & 0xff]
  242. | ip_maskr[7][r_in & 0xff];
  243. while (count--) {
  244. /* Do each round. */
  245. kl = kl1;
  246. kr = kr1;
  247. round = 16;
  248. do {
  249. /* Expand R to 48 bits (simulate the E-box). */
  250. r48l = ((r & 0x00000001) << 23)
  251. | ((r & 0xf8000000) >> 9)
  252. | ((r & 0x1f800000) >> 11)
  253. | ((r & 0x01f80000) >> 13)
  254. | ((r & 0x001f8000) >> 15);
  255. r48r = ((r & 0x0001f800) << 7)
  256. | ((r & 0x00001f80) << 5)
  257. | ((r & 0x000001f8) << 3)
  258. | ((r & 0x0000001f) << 1)
  259. | ((r & 0x80000000) >> 31);
  260. /*
  261. * Do salting for crypt() and friends, and
  262. * XOR with the permuted key.
  263. */
  264. f = (r48l ^ r48r) & saltbits;
  265. r48l ^= f ^ *kl++;
  266. r48r ^= f ^ *kr++;
  267. /*
  268. * Do sbox lookups (which shrink it back to 32 bits)
  269. * and do the pbox permutation at the same time.
  270. */
  271. f = psbox[0][m_sbox[0][r48l >> 12]]
  272. | psbox[1][m_sbox[1][r48l & 0xfff]]
  273. | psbox[2][m_sbox[2][r48r >> 12]]
  274. | psbox[3][m_sbox[3][r48r & 0xfff]];
  275. /* Now that we've permuted things, complete f(). */
  276. f ^= l;
  277. l = r;
  278. r = f;
  279. } while (--round);
  280. r = l;
  281. l = f;
  282. }
  283. /* Do final permutation (inverse of IP). */
  284. *l_out = fp_maskl[0][l >> 24]
  285. | fp_maskl[1][(l >> 16) & 0xff]
  286. | fp_maskl[2][(l >> 8) & 0xff]
  287. | fp_maskl[3][l & 0xff]
  288. | fp_maskl[4][r >> 24]
  289. | fp_maskl[5][(r >> 16) & 0xff]
  290. | fp_maskl[6][(r >> 8) & 0xff]
  291. | fp_maskl[7][r & 0xff];
  292. *r_out = fp_maskr[0][l >> 24]
  293. | fp_maskr[1][(l >> 16) & 0xff]
  294. | fp_maskr[2][(l >> 8) & 0xff]
  295. | fp_maskr[3][l & 0xff]
  296. | fp_maskr[4][r >> 24]
  297. | fp_maskr[5][(r >> 16) & 0xff]
  298. | fp_maskr[6][(r >> 8) & 0xff]
  299. | fp_maskr[7][r & 0xff];
  300. return(0);
  301. }
  302. #if 0
  303. static int
  304. des_cipher(const char *in, char *out, u_int32_t salt, int count)
  305. {
  306. u_int32_t l_out, r_out, rawl, rawr;
  307. int retval;
  308. union {
  309. u_int32_t *ui32;
  310. const char *c;
  311. } trans;
  312. des_init();
  313. setup_salt(salt);
  314. trans.c = in;
  315. rawl = ntohl(*trans.ui32++);
  316. rawr = ntohl(*trans.ui32);
  317. retval = do_des(rawl, rawr, &l_out, &r_out, count);
  318. trans.c = out;
  319. *trans.ui32++ = htonl(l_out);
  320. *trans.ui32 = htonl(r_out);
  321. return(retval);
  322. }
  323. #endif
  324. void
  325. setkey(const char *key)
  326. {
  327. int i, j;
  328. u_int32_t packed_keys[2];
  329. u_char *p;
  330. p = (u_char *) packed_keys;
  331. for (i = 0; i < 8; i++) {
  332. p[i] = 0;
  333. for (j = 0; j < 8; j++)
  334. if (*key++ & 1)
  335. p[i] |= bits8[j];
  336. }
  337. des_setkey((char *)p);
  338. }
  339. void
  340. encrypt(char *block, int flag)
  341. {
  342. u_int32_t io[2];
  343. u_char *p;
  344. int i, j;
  345. des_init();
  346. setup_salt(0L);
  347. p = (u_char*)block;
  348. for (i = 0; i < 2; i++) {
  349. io[i] = 0L;
  350. for (j = 0; j < 32; j++)
  351. if (*p++ & 1)
  352. io[i] |= bits32[j];
  353. }
  354. do_des(io[0], io[1], io, io + 1, flag ? -1 : 1);
  355. for (i = 0; i < 2; i++)
  356. for (j = 0; j < 32; j++)
  357. block[(i << 5) | j] = (io[i] & bits32[j]) ? 1 : 0;
  358. }
  359. char *__des_crypt(const unsigned char *key, const unsigned char *setting)
  360. {
  361. u_int32_t count, salt, l, r0, r1, keybuf[2];
  362. u_char *p, *q;
  363. static char output[21];
  364. des_init();
  365. /*
  366. * Copy the key, shifting each character up by one bit
  367. * and padding with zeros.
  368. */
  369. q = (u_char *)keybuf;
  370. while (q - (u_char *)keybuf - 8) {
  371. *q++ = *key << 1;
  372. if (*(q - 1))
  373. key++;
  374. }
  375. des_setkey((char *)keybuf);
  376. #if 0
  377. if (*setting == _PASSWORD_EFMT1) {
  378. int i;
  379. /*
  380. * "new"-style:
  381. * setting - underscore, 4 bytes of count, 4 bytes of salt
  382. * key - unlimited characters
  383. */
  384. for (i = 1, count = 0L; i < 5; i++)
  385. count |= ascii_to_bin(setting[i]) << ((i - 1) * 6);
  386. for (i = 5, salt = 0L; i < 9; i++)
  387. salt |= ascii_to_bin(setting[i]) << ((i - 5) * 6);
  388. while (*key) {
  389. /*
  390. * Encrypt the key with itself.
  391. */
  392. if (des_cipher((char *)keybuf, (char *)keybuf, 0L, 1))
  393. return(NULL);
  394. /*
  395. * And XOR with the next 8 characters of the key.
  396. */
  397. q = (u_char *)keybuf;
  398. while (q - (u_char *)keybuf - 8 && *key)
  399. *q++ ^= *key++ << 1;
  400. des_setkey((char *)keybuf);
  401. }
  402. strncpy(output, setting, 9);
  403. /*
  404. * Double check that we weren't given a short setting.
  405. * If we were, the above code will probably have created
  406. * wierd values for count and salt, but we don't really care.
  407. * Just make sure the output string doesn't have an extra
  408. * NUL in it.
  409. */
  410. output[9] = '\0';
  411. p = (u_char *)output + strlen(output);
  412. } else
  413. #endif
  414. {
  415. /*
  416. * "old"-style:
  417. * setting - 2 bytes of salt
  418. * key - up to 8 characters
  419. */
  420. count = 25;
  421. salt = (ascii_to_bin(setting[1]) << 6)
  422. | ascii_to_bin(setting[0]);
  423. output[0] = setting[0];
  424. /*
  425. * If the encrypted password that the salt was extracted from
  426. * is only 1 character long, the salt will be corrupted. We
  427. * need to ensure that the output string doesn't have an extra
  428. * NUL in it!
  429. */
  430. output[1] = setting[1] ? setting[1] : output[0];
  431. p = (u_char *)output + 2;
  432. }
  433. setup_salt(salt);
  434. /*
  435. * Do it.
  436. */
  437. if (do_des(0L, 0L, &r0, &r1, (int)count))
  438. return(NULL);
  439. /*
  440. * Now encode the result...
  441. */
  442. l = (r0 >> 8);
  443. *p++ = ascii64[(l >> 18) & 0x3f];
  444. *p++ = ascii64[(l >> 12) & 0x3f];
  445. *p++ = ascii64[(l >> 6) & 0x3f];
  446. *p++ = ascii64[l & 0x3f];
  447. l = (r0 << 16) | ((r1 >> 16) & 0xffff);
  448. *p++ = ascii64[(l >> 18) & 0x3f];
  449. *p++ = ascii64[(l >> 12) & 0x3f];
  450. *p++ = ascii64[(l >> 6) & 0x3f];
  451. *p++ = ascii64[l & 0x3f];
  452. l = r1 << 2;
  453. *p++ = ascii64[(l >> 12) & 0x3f];
  454. *p++ = ascii64[(l >> 6) & 0x3f];
  455. *p++ = ascii64[l & 0x3f];
  456. *p = 0;
  457. return(output);
  458. }