3
0

pw_encrypt_des.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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 uint32_t's (ie. the CPU is not picky about
  56. * alignment).
  57. */
  58. /* Parts busybox doesn't need or had optimized */
  59. #define USE_PRECOMPUTED_u_sbox 1
  60. #define USE_REPETITIVE_SPEEDUP 0
  61. #define USE_ip_mask 0
  62. #define USE_de_keys 0
  63. /* A pile of data */
  64. static const uint8_t IP[64] = {
  65. 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
  66. 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
  67. 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
  68. 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
  69. };
  70. static const uint8_t key_perm[56] = {
  71. 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
  72. 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
  73. 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
  74. 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
  75. };
  76. static const uint8_t key_shifts[16] = {
  77. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  78. };
  79. static const uint8_t comp_perm[48] = {
  80. 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
  81. 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
  82. 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
  83. 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
  84. };
  85. /*
  86. * No E box is used, as it's replaced by some ANDs, shifts, and ORs.
  87. */
  88. #if !USE_PRECOMPUTED_u_sbox
  89. static const uint8_t sbox[8][64] = {
  90. { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
  91. 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
  92. 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
  93. 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
  94. },
  95. { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
  96. 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
  97. 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
  98. 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
  99. },
  100. { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
  101. 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
  102. 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
  103. 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
  104. },
  105. { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
  106. 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
  107. 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
  108. 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
  109. },
  110. { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
  111. 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
  112. 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
  113. 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
  114. },
  115. { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
  116. 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
  117. 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
  118. 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
  119. },
  120. { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
  121. 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
  122. 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
  123. 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
  124. },
  125. { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
  126. 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
  127. 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
  128. 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
  129. }
  130. };
  131. #else /* precomputed, with half-bytes packed into one byte */
  132. static const uint8_t u_sbox[8][32] = {
  133. { 0x0e, 0xf4, 0x7d, 0x41, 0xe2, 0x2f, 0xdb, 0x18,
  134. 0xa3, 0x6a, 0xc6, 0xbc, 0x95, 0x59, 0x30, 0x87,
  135. 0xf4, 0xc1, 0x8e, 0x28, 0x4d, 0x96, 0x12, 0x7b,
  136. 0x5f, 0xbc, 0x39, 0xe7, 0xa3, 0x0a, 0x65, 0xd0,
  137. },
  138. { 0x3f, 0xd1, 0x48, 0x7e, 0xf6, 0x2b, 0x83, 0xe4,
  139. 0xc9, 0x07, 0x12, 0xad, 0x6c, 0x90, 0xb5, 0x5a,
  140. 0xd0, 0x8e, 0xa7, 0x1b, 0x3a, 0xf4, 0x4d, 0x21,
  141. 0xb5, 0x68, 0x7c, 0xc6, 0x09, 0x53, 0xe2, 0x9f,
  142. },
  143. { 0xda, 0x70, 0x09, 0x9e, 0x36, 0x43, 0x6f, 0xa5,
  144. 0x21, 0x8d, 0x5c, 0xe7, 0xcb, 0xb4, 0xf2, 0x18,
  145. 0x1d, 0xa6, 0xd4, 0x09, 0x68, 0x9f, 0x83, 0x70,
  146. 0x4b, 0xf1, 0xe2, 0x3c, 0xb5, 0x5a, 0x2e, 0xc7,
  147. },
  148. { 0xd7, 0x8d, 0xbe, 0x53, 0x60, 0xf6, 0x09, 0x3a,
  149. 0x41, 0x72, 0x28, 0xc5, 0x1b, 0xac, 0xe4, 0x9f,
  150. 0x3a, 0xf6, 0x09, 0x60, 0xac, 0x1b, 0xd7, 0x8d,
  151. 0x9f, 0x41, 0x53, 0xbe, 0xc5, 0x72, 0x28, 0xe4,
  152. },
  153. { 0xe2, 0xbc, 0x24, 0xc1, 0x47, 0x7a, 0xdb, 0x16,
  154. 0x58, 0x05, 0xf3, 0xaf, 0x3d, 0x90, 0x8e, 0x69,
  155. 0xb4, 0x82, 0xc1, 0x7b, 0x1a, 0xed, 0x27, 0xd8,
  156. 0x6f, 0xf9, 0x0c, 0x95, 0xa6, 0x43, 0x50, 0x3e,
  157. },
  158. { 0xac, 0xf1, 0x4a, 0x2f, 0x79, 0xc2, 0x96, 0x58,
  159. 0x60, 0x1d, 0xd3, 0xe4, 0x0e, 0xb7, 0x35, 0x8b,
  160. 0x49, 0x3e, 0x2f, 0xc5, 0x92, 0x58, 0xfc, 0xa3,
  161. 0xb7, 0xe0, 0x14, 0x7a, 0x61, 0x0d, 0x8b, 0xd6,
  162. },
  163. { 0xd4, 0x0b, 0xb2, 0x7e, 0x4f, 0x90, 0x18, 0xad,
  164. 0xe3, 0x3c, 0x59, 0xc7, 0x25, 0xfa, 0x86, 0x61,
  165. 0x61, 0xb4, 0xdb, 0x8d, 0x1c, 0x43, 0xa7, 0x7e,
  166. 0x9a, 0x5f, 0x06, 0xf8, 0xe0, 0x25, 0x39, 0xc2,
  167. },
  168. { 0x1d, 0xf2, 0xd8, 0x84, 0xa6, 0x3f, 0x7b, 0x41,
  169. 0xca, 0x59, 0x63, 0xbe, 0x05, 0xe0, 0x9c, 0x27,
  170. 0x27, 0x1b, 0xe4, 0x71, 0x49, 0xac, 0x8e, 0xd2,
  171. 0xf0, 0xc6, 0x9a, 0x0d, 0x3f, 0x53, 0x65, 0xb8,
  172. },
  173. };
  174. #endif
  175. static const uint8_t pbox[32] = {
  176. 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
  177. 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
  178. };
  179. static const uint32_t bits32[32] =
  180. {
  181. 0x80000000, 0x40000000, 0x20000000, 0x10000000,
  182. 0x08000000, 0x04000000, 0x02000000, 0x01000000,
  183. 0x00800000, 0x00400000, 0x00200000, 0x00100000,
  184. 0x00080000, 0x00040000, 0x00020000, 0x00010000,
  185. 0x00008000, 0x00004000, 0x00002000, 0x00001000,
  186. 0x00000800, 0x00000400, 0x00000200, 0x00000100,
  187. 0x00000080, 0x00000040, 0x00000020, 0x00000010,
  188. 0x00000008, 0x00000004, 0x00000002, 0x00000001
  189. };
  190. static const uint8_t bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
  191. static int
  192. ascii_to_bin(char ch)
  193. {
  194. if (ch > 'z')
  195. return 0;
  196. if (ch >= 'a')
  197. return (ch - 'a' + 38);
  198. if (ch > 'Z')
  199. return 0;
  200. if (ch >= 'A')
  201. return (ch - 'A' + 12);
  202. if (ch > '9')
  203. return 0;
  204. if (ch >= '.')
  205. return (ch - '.');
  206. return 0;
  207. }
  208. /* Static stuff that stays resident and doesn't change after
  209. * being initialized, and therefore doesn't need to be made
  210. * reentrant. */
  211. struct const_des_ctx {
  212. #if USE_ip_mask
  213. uint8_t init_perm[64]; /* referenced 2 times */
  214. #endif
  215. uint8_t final_perm[64]; /* 2 times */
  216. uint8_t m_sbox[4][4096]; /* 5 times */
  217. };
  218. #define C (*cctx)
  219. #define init_perm (C.init_perm )
  220. #define final_perm (C.final_perm)
  221. #define m_sbox (C.m_sbox )
  222. static struct const_des_ctx*
  223. const_des_init(void)
  224. {
  225. unsigned i, j, b;
  226. struct const_des_ctx *cctx;
  227. #if !USE_PRECOMPUTED_u_sbox
  228. uint8_t u_sbox[8][64];
  229. cctx = xmalloc(sizeof(*cctx));
  230. /* Invert the S-boxes, reordering the input bits. */
  231. for (i = 0; i < 8; i++) {
  232. for (j = 0; j < 64; j++) {
  233. b = (j & 0x20) | ((j & 1) << 4) | ((j >> 1) & 0xf);
  234. u_sbox[i][j] = sbox[i][b];
  235. }
  236. }
  237. for (i = 0; i < 8; i++) {
  238. fprintf(stderr, "\t{\t");
  239. for (j = 0; j < 64; j+=2)
  240. fprintf(stderr, " 0x%02x,", u_sbox[i][j] + u_sbox[i][j+1]*16);
  241. fprintf(stderr, "\n\t},\n");
  242. }
  243. /*
  244. * Convert the inverted S-boxes into 4 arrays of 8 bits.
  245. * Each will handle 12 bits of the S-box input.
  246. */
  247. for (b = 0; b < 4; b++)
  248. for (i = 0; i < 64; i++)
  249. for (j = 0; j < 64; j++)
  250. m_sbox[b][(i << 6) | j] =
  251. (uint8_t)((u_sbox[(b << 1)][i] << 4) |
  252. u_sbox[(b << 1) + 1][j]);
  253. #else
  254. cctx = xmalloc(sizeof(*cctx));
  255. /*
  256. * Convert the inverted S-boxes into 4 arrays of 8 bits.
  257. * Each will handle 12 bits of the S-box input.
  258. */
  259. for (b = 0; b < 4; b++)
  260. for (i = 0; i < 64; i++)
  261. for (j = 0; j < 64; j++) {
  262. uint8_t lo, hi;
  263. hi = u_sbox[(b << 1)][i / 2];
  264. if (!(i & 1))
  265. hi <<= 4;
  266. lo = u_sbox[(b << 1) + 1][j / 2];
  267. if (j & 1)
  268. lo >>= 4;
  269. m_sbox[b][(i << 6) | j] = (hi & 0xf0) | (lo & 0x0f);
  270. }
  271. #endif
  272. /*
  273. * Set up the initial & final permutations into a useful form.
  274. */
  275. for (i = 0; i < 64; i++) {
  276. final_perm[i] = IP[i] - 1;
  277. #if USE_ip_mask
  278. init_perm[final_perm[i]] = (uint8_t)i;
  279. #endif
  280. }
  281. return cctx;
  282. }
  283. struct des_ctx {
  284. const struct const_des_ctx *const_ctx;
  285. uint32_t saltbits; /* referenced 5 times */
  286. #if USE_REPETITIVE_SPEEDUP
  287. uint32_t old_salt; /* 3 times */
  288. uint32_t old_rawkey0, old_rawkey1; /* 3 times each */
  289. #endif
  290. uint8_t un_pbox[32]; /* 2 times */
  291. uint8_t inv_comp_perm[56]; /* 3 times */
  292. uint8_t inv_key_perm[64]; /* 3 times */
  293. uint32_t en_keysl[16], en_keysr[16]; /* 2 times each */
  294. #if USE_de_keys
  295. uint32_t de_keysl[16], de_keysr[16]; /* 2 times each */
  296. #endif
  297. #if USE_ip_mask
  298. uint32_t ip_maskl[8][256], ip_maskr[8][256]; /* 9 times each */
  299. #endif
  300. uint32_t fp_maskl[8][256], fp_maskr[8][256]; /* 9 times each */
  301. uint32_t key_perm_maskl[8][128], key_perm_maskr[8][128]; /* 9 times */
  302. uint32_t comp_maskl[8][128], comp_maskr[8][128]; /* 9 times each */
  303. uint32_t psbox[4][256]; /* 5 times */
  304. };
  305. #define D (*ctx)
  306. #define const_ctx (D.const_ctx )
  307. #define saltbits (D.saltbits )
  308. #define old_salt (D.old_salt )
  309. #define old_rawkey0 (D.old_rawkey0 )
  310. #define old_rawkey1 (D.old_rawkey1 )
  311. #define un_pbox (D.un_pbox )
  312. #define inv_comp_perm (D.inv_comp_perm )
  313. #define inv_key_perm (D.inv_key_perm )
  314. #define en_keysl (D.en_keysl )
  315. #define en_keysr (D.en_keysr )
  316. #define de_keysl (D.de_keysl )
  317. #define de_keysr (D.de_keysr )
  318. #define ip_maskl (D.ip_maskl )
  319. #define ip_maskr (D.ip_maskr )
  320. #define fp_maskl (D.fp_maskl )
  321. #define fp_maskr (D.fp_maskr )
  322. #define key_perm_maskl (D.key_perm_maskl )
  323. #define key_perm_maskr (D.key_perm_maskr )
  324. #define comp_maskl (D.comp_maskl )
  325. #define comp_maskr (D.comp_maskr )
  326. #define psbox (D.psbox )
  327. static struct des_ctx*
  328. des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx)
  329. {
  330. int i, j, b, k, inbit, obit;
  331. uint32_t p;
  332. const uint32_t *bits28, *bits24;
  333. if (!ctx)
  334. ctx = xmalloc(sizeof(*ctx));
  335. const_ctx = cctx;
  336. #if USE_REPETITIVE_SPEEDUP
  337. old_rawkey0 = old_rawkey1 = 0;
  338. old_salt = 0;
  339. #endif
  340. saltbits = 0;
  341. bits28 = bits32 + 4;
  342. bits24 = bits28 + 4;
  343. /* Initialise the inverted key permutation. */
  344. for (i = 0; i < 64; i++) {
  345. inv_key_perm[i] = 255;
  346. }
  347. /*
  348. * Invert the key permutation and initialise the inverted key
  349. * compression permutation.
  350. */
  351. for (i = 0; i < 56; i++) {
  352. inv_key_perm[key_perm[i] - 1] = (uint8_t)i;
  353. inv_comp_perm[i] = 255;
  354. }
  355. /* Invert the key compression permutation. */
  356. for (i = 0; i < 48; i++) {
  357. inv_comp_perm[comp_perm[i] - 1] = (uint8_t)i;
  358. }
  359. /*
  360. * Set up the OR-mask arrays for the initial and final permutations,
  361. * and for the key initial and compression permutations.
  362. */
  363. for (k = 0; k < 8; k++) {
  364. uint32_t il, ir;
  365. uint32_t fl, fr;
  366. for (i = 0; i < 256; i++) {
  367. #if USE_ip_mask
  368. il = 0;
  369. ir = 0;
  370. #endif
  371. fl = 0;
  372. fr = 0;
  373. for (j = 0; j < 8; j++) {
  374. inbit = 8 * k + j;
  375. if (i & bits8[j]) {
  376. #if USE_ip_mask
  377. obit = init_perm[inbit];
  378. if (obit < 32)
  379. il |= bits32[obit];
  380. else
  381. ir |= bits32[obit - 32];
  382. #endif
  383. obit = final_perm[inbit];
  384. if (obit < 32)
  385. fl |= bits32[obit];
  386. else
  387. fr |= bits32[obit - 32];
  388. }
  389. }
  390. #if USE_ip_mask
  391. ip_maskl[k][i] = il;
  392. ip_maskr[k][i] = ir;
  393. #endif
  394. fp_maskl[k][i] = fl;
  395. fp_maskr[k][i] = fr;
  396. }
  397. for (i = 0; i < 128; i++) {
  398. il = 0;
  399. ir = 0;
  400. for (j = 0; j < 7; j++) {
  401. inbit = 8 * k + j;
  402. if (i & bits8[j + 1]) {
  403. obit = inv_key_perm[inbit];
  404. if (obit == 255)
  405. continue;
  406. if (obit < 28)
  407. il |= bits28[obit];
  408. else
  409. ir |= bits28[obit - 28];
  410. }
  411. }
  412. key_perm_maskl[k][i] = il;
  413. key_perm_maskr[k][i] = ir;
  414. il = 0;
  415. ir = 0;
  416. for (j = 0; j < 7; j++) {
  417. inbit = 7 * k + j;
  418. if (i & bits8[j + 1]) {
  419. obit = inv_comp_perm[inbit];
  420. if (obit == 255)
  421. continue;
  422. if (obit < 24)
  423. il |= bits24[obit];
  424. else
  425. ir |= bits24[obit - 24];
  426. }
  427. }
  428. comp_maskl[k][i] = il;
  429. comp_maskr[k][i] = ir;
  430. }
  431. }
  432. /*
  433. * Invert the P-box permutation, and convert into OR-masks for
  434. * handling the output of the S-box arrays setup above.
  435. */
  436. for (i = 0; i < 32; i++)
  437. un_pbox[pbox[i] - 1] = (uint8_t)i;
  438. for (b = 0; b < 4; b++) {
  439. for (i = 0; i < 256; i++) {
  440. p = 0;
  441. for (j = 0; j < 8; j++) {
  442. if (i & bits8[j])
  443. p |= bits32[un_pbox[8 * b + j]];
  444. }
  445. psbox[b][i] = p;
  446. }
  447. }
  448. return ctx;
  449. }
  450. static void
  451. setup_salt(struct des_ctx *ctx, uint32_t salt)
  452. {
  453. uint32_t obit, saltbit;
  454. int i;
  455. #if USE_REPETITIVE_SPEEDUP
  456. if (salt == old_salt)
  457. return;
  458. old_salt = salt;
  459. #endif
  460. saltbits = 0;
  461. saltbit = 1;
  462. obit = 0x800000;
  463. for (i = 0; i < 24; i++) {
  464. if (salt & saltbit)
  465. saltbits |= obit;
  466. saltbit <<= 1;
  467. obit >>= 1;
  468. }
  469. }
  470. static void
  471. des_setkey(struct des_ctx *ctx, const char *key)
  472. {
  473. uint32_t k0, k1, rawkey0, rawkey1;
  474. int shifts, round;
  475. rawkey0 = ntohl(*(const uint32_t *) key);
  476. rawkey1 = ntohl(*(const uint32_t *) (key + 4));
  477. #if USE_REPETITIVE_SPEEDUP
  478. if ((rawkey0 | rawkey1)
  479. && rawkey0 == old_rawkey0
  480. && rawkey1 == old_rawkey1
  481. ) {
  482. /*
  483. * Already setup for this key.
  484. * This optimisation fails on a zero key (which is weak and
  485. * has bad parity anyway) in order to simplify the starting
  486. * conditions.
  487. */
  488. return;
  489. }
  490. old_rawkey0 = rawkey0;
  491. old_rawkey1 = rawkey1;
  492. #endif
  493. /*
  494. * Do key permutation and split into two 28-bit subkeys.
  495. */
  496. k0 = key_perm_maskl[0][rawkey0 >> 25]
  497. | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f]
  498. | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f]
  499. | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f]
  500. | key_perm_maskl[4][rawkey1 >> 25]
  501. | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f]
  502. | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f]
  503. | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f];
  504. k1 = key_perm_maskr[0][rawkey0 >> 25]
  505. | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f]
  506. | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f]
  507. | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f]
  508. | key_perm_maskr[4][rawkey1 >> 25]
  509. | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f]
  510. | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f]
  511. | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f];
  512. /*
  513. * Rotate subkeys and do compression permutation.
  514. */
  515. shifts = 0;
  516. for (round = 0; round < 16; round++) {
  517. uint32_t t0, t1;
  518. shifts += key_shifts[round];
  519. t0 = (k0 << shifts) | (k0 >> (28 - shifts));
  520. t1 = (k1 << shifts) | (k1 >> (28 - shifts));
  521. #if USE_de_keys
  522. de_keysl[15 - round] =
  523. #endif
  524. en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f]
  525. | comp_maskl[1][(t0 >> 14) & 0x7f]
  526. | comp_maskl[2][(t0 >> 7) & 0x7f]
  527. | comp_maskl[3][t0 & 0x7f]
  528. | comp_maskl[4][(t1 >> 21) & 0x7f]
  529. | comp_maskl[5][(t1 >> 14) & 0x7f]
  530. | comp_maskl[6][(t1 >> 7) & 0x7f]
  531. | comp_maskl[7][t1 & 0x7f];
  532. #if USE_de_keys
  533. de_keysr[15 - round] =
  534. #endif
  535. en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f]
  536. | comp_maskr[1][(t0 >> 14) & 0x7f]
  537. | comp_maskr[2][(t0 >> 7) & 0x7f]
  538. | comp_maskr[3][t0 & 0x7f]
  539. | comp_maskr[4][(t1 >> 21) & 0x7f]
  540. | comp_maskr[5][(t1 >> 14) & 0x7f]
  541. | comp_maskr[6][(t1 >> 7) & 0x7f]
  542. | comp_maskr[7][t1 & 0x7f];
  543. }
  544. }
  545. static void
  546. do_des(struct des_ctx *ctx, /*uint32_t l_in, uint32_t r_in,*/ uint32_t *l_out, uint32_t *r_out, int count)
  547. {
  548. const struct const_des_ctx *cctx = const_ctx;
  549. /*
  550. * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format.
  551. */
  552. uint32_t l, r, *kl, *kr;
  553. uint32_t f = f; /* silence gcc */
  554. uint32_t r48l, r48r;
  555. int round;
  556. /* Do initial permutation (IP). */
  557. #if USE_ip_mask
  558. uint32_t l_in = 0;
  559. uint32_t r_in = 0;
  560. l = ip_maskl[0][l_in >> 24]
  561. | ip_maskl[1][(l_in >> 16) & 0xff]
  562. | ip_maskl[2][(l_in >> 8) & 0xff]
  563. | ip_maskl[3][l_in & 0xff]
  564. | ip_maskl[4][r_in >> 24]
  565. | ip_maskl[5][(r_in >> 16) & 0xff]
  566. | ip_maskl[6][(r_in >> 8) & 0xff]
  567. | ip_maskl[7][r_in & 0xff];
  568. r = ip_maskr[0][l_in >> 24]
  569. | ip_maskr[1][(l_in >> 16) & 0xff]
  570. | ip_maskr[2][(l_in >> 8) & 0xff]
  571. | ip_maskr[3][l_in & 0xff]
  572. | ip_maskr[4][r_in >> 24]
  573. | ip_maskr[5][(r_in >> 16) & 0xff]
  574. | ip_maskr[6][(r_in >> 8) & 0xff]
  575. | ip_maskr[7][r_in & 0xff];
  576. #elif 0 /* -65 bytes (using the fact that l_in == r_in == 0) */
  577. l = r = 0;
  578. for (round = 0; round < 8; round++) {
  579. l |= ip_maskl[round][0];
  580. r |= ip_maskr[round][0];
  581. }
  582. bb_error_msg("l:%x r:%x", l, r); /* reports 0, 0 always! */
  583. #else /* using the fact that ip_maskX[] is constant (written to by des_init) */
  584. l = r = 0;
  585. #endif
  586. do {
  587. /* Do each round. */
  588. kl = en_keysl;
  589. kr = en_keysr;
  590. round = 16;
  591. do {
  592. /* Expand R to 48 bits (simulate the E-box). */
  593. r48l = ((r & 0x00000001) << 23)
  594. | ((r & 0xf8000000) >> 9)
  595. | ((r & 0x1f800000) >> 11)
  596. | ((r & 0x01f80000) >> 13)
  597. | ((r & 0x001f8000) >> 15);
  598. r48r = ((r & 0x0001f800) << 7)
  599. | ((r & 0x00001f80) << 5)
  600. | ((r & 0x000001f8) << 3)
  601. | ((r & 0x0000001f) << 1)
  602. | ((r & 0x80000000) >> 31);
  603. /*
  604. * Do salting for crypt() and friends, and
  605. * XOR with the permuted key.
  606. */
  607. f = (r48l ^ r48r) & saltbits;
  608. r48l ^= f ^ *kl++;
  609. r48r ^= f ^ *kr++;
  610. /*
  611. * Do sbox lookups (which shrink it back to 32 bits)
  612. * and do the pbox permutation at the same time.
  613. */
  614. f = psbox[0][m_sbox[0][r48l >> 12]]
  615. | psbox[1][m_sbox[1][r48l & 0xfff]]
  616. | psbox[2][m_sbox[2][r48r >> 12]]
  617. | psbox[3][m_sbox[3][r48r & 0xfff]];
  618. /* Now that we've permuted things, complete f(). */
  619. f ^= l;
  620. l = r;
  621. r = f;
  622. } while (--round);
  623. r = l;
  624. l = f;
  625. } while (--count);
  626. /* Do final permutation (inverse of IP). */
  627. *l_out = fp_maskl[0][l >> 24]
  628. | fp_maskl[1][(l >> 16) & 0xff]
  629. | fp_maskl[2][(l >> 8) & 0xff]
  630. | fp_maskl[3][l & 0xff]
  631. | fp_maskl[4][r >> 24]
  632. | fp_maskl[5][(r >> 16) & 0xff]
  633. | fp_maskl[6][(r >> 8) & 0xff]
  634. | fp_maskl[7][r & 0xff];
  635. *r_out = fp_maskr[0][l >> 24]
  636. | fp_maskr[1][(l >> 16) & 0xff]
  637. | fp_maskr[2][(l >> 8) & 0xff]
  638. | fp_maskr[3][l & 0xff]
  639. | fp_maskr[4][r >> 24]
  640. | fp_maskr[5][(r >> 16) & 0xff]
  641. | fp_maskr[6][(r >> 8) & 0xff]
  642. | fp_maskr[7][r & 0xff];
  643. }
  644. #define DES_OUT_BUFSIZE 21
  645. static void
  646. to64_msb_first(char *s, unsigned v)
  647. {
  648. #if 0
  649. *s++ = ascii64[(v >> 18) & 0x3f]; /* bits 23..18 */
  650. *s++ = ascii64[(v >> 12) & 0x3f]; /* bits 17..12 */
  651. *s++ = ascii64[(v >> 6) & 0x3f]; /* bits 11..6 */
  652. *s = ascii64[v & 0x3f]; /* bits 5..0 */
  653. #endif
  654. *s++ = i64c(v >> 18); /* bits 23..18 */
  655. *s++ = i64c(v >> 12); /* bits 17..12 */
  656. *s++ = i64c(v >> 6); /* bits 11..6 */
  657. *s = i64c(v); /* bits 5..0 */
  658. }
  659. static char *
  660. NOINLINE
  661. des_crypt(struct des_ctx *ctx, char output[DES_OUT_BUFSIZE],
  662. const unsigned char *key, const unsigned char *setting)
  663. {
  664. uint32_t salt, r0, r1, keybuf[2];
  665. uint8_t *q;
  666. /*
  667. * Copy the key, shifting each character up by one bit
  668. * and padding with zeros.
  669. */
  670. q = (uint8_t *)keybuf;
  671. while (q - (uint8_t *)keybuf != 8) {
  672. *q = *key << 1;
  673. if (*q)
  674. key++;
  675. q++;
  676. }
  677. des_setkey(ctx, (char *)keybuf);
  678. /*
  679. * setting - 2 bytes of salt
  680. * key - up to 8 characters
  681. */
  682. salt = (ascii_to_bin(setting[1]) << 6)
  683. | ascii_to_bin(setting[0]);
  684. output[0] = setting[0];
  685. /*
  686. * If the encrypted password that the salt was extracted from
  687. * is only 1 character long, the salt will be corrupted. We
  688. * need to ensure that the output string doesn't have an extra
  689. * NUL in it!
  690. */
  691. output[1] = setting[1] ? setting[1] : output[0];
  692. setup_salt(ctx, salt);
  693. /* Do it. */
  694. do_des(ctx, /*0, 0,*/ &r0, &r1, 25 /* count */);
  695. /* Now encode the result. */
  696. #if 0
  697. {
  698. uint32_t l = (r0 >> 8);
  699. q = (uint8_t *)output + 2;
  700. *q++ = ascii64[(l >> 18) & 0x3f]; /* bits 31..26 of r0 */
  701. *q++ = ascii64[(l >> 12) & 0x3f]; /* bits 25..20 of r0 */
  702. *q++ = ascii64[(l >> 6) & 0x3f]; /* bits 19..14 of r0 */
  703. *q++ = ascii64[l & 0x3f]; /* bits 13..8 of r0 */
  704. l = ((r0 << 16) | (r1 >> 16));
  705. *q++ = ascii64[(l >> 18) & 0x3f]; /* bits 7..2 of r0 */
  706. *q++ = ascii64[(l >> 12) & 0x3f]; /* bits 1..2 of r0 and 31..28 of r1 */
  707. *q++ = ascii64[(l >> 6) & 0x3f]; /* bits 27..22 of r1 */
  708. *q++ = ascii64[l & 0x3f]; /* bits 21..16 of r1 */
  709. l = r1 << 2;
  710. *q++ = ascii64[(l >> 12) & 0x3f]; /* bits 15..10 of r1 */
  711. *q++ = ascii64[(l >> 6) & 0x3f]; /* bits 9..4 of r1 */
  712. *q++ = ascii64[l & 0x3f]; /* bits 3..0 of r1 + 00 */
  713. *q = 0;
  714. }
  715. #else
  716. /* Each call takes low-order 24 bits and stores 4 chars */
  717. /* bits 31..8 of r0 */
  718. to64_msb_first(output + 2, (r0 >> 8));
  719. /* bits 7..0 of r0 and 31..16 of r1 */
  720. to64_msb_first(output + 6, (r0 << 16) | (r1 >> 16));
  721. /* bits 15..0 of r1 and two zero bits (plus extra zero byte) */
  722. to64_msb_first(output + 10, (r1 << 8));
  723. /* extra zero byte is encoded as '.', fixing it */
  724. output[13] = '\0';
  725. #endif
  726. return output;
  727. }
  728. #undef USE_PRECOMPUTED_u_sbox
  729. #undef USE_REPETITIVE_SPEEDUP
  730. #undef USE_ip_mask
  731. #undef USE_de_keys
  732. #undef C
  733. #undef init_perm
  734. #undef final_perm
  735. #undef m_sbox
  736. #undef D
  737. #undef const_ctx
  738. #undef saltbits
  739. #undef old_salt
  740. #undef old_rawkey0
  741. #undef old_rawkey1
  742. #undef un_pbox
  743. #undef inv_comp_perm
  744. #undef inv_key_perm
  745. #undef en_keysl
  746. #undef en_keysr
  747. #undef de_keysl
  748. #undef de_keysr
  749. #undef ip_maskl
  750. #undef ip_maskr
  751. #undef fp_maskl
  752. #undef fp_maskr
  753. #undef key_perm_maskl
  754. #undef key_perm_maskr
  755. #undef comp_maskl
  756. #undef comp_maskr
  757. #undef psbox