pw_encrypt_des.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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] ALIGN1 = {
  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] ALIGN1 = {
  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] ALIGN1 = {
  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] ALIGN1 = {
  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] ALIGN1 = {
  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] ALIGN4 = {
  180. 0x80000000, 0x40000000, 0x20000000, 0x10000000,
  181. 0x08000000, 0x04000000, 0x02000000, 0x01000000,
  182. 0x00800000, 0x00400000, 0x00200000, 0x00100000,
  183. 0x00080000, 0x00040000, 0x00020000, 0x00010000,
  184. 0x00008000, 0x00004000, 0x00002000, 0x00001000,
  185. 0x00000800, 0x00000400, 0x00000200, 0x00000100,
  186. 0x00000080, 0x00000040, 0x00000020, 0x00000010,
  187. 0x00000008, 0x00000004, 0x00000002, 0x00000001
  188. };
  189. static const uint8_t bits8[8] ALIGN1 = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
  190. static int
  191. ascii_to_bin(char ch)
  192. {
  193. if (ch > 'z')
  194. return 0;
  195. if (ch >= 'a')
  196. return (ch - 'a' + 38);
  197. if (ch > 'Z')
  198. return 0;
  199. if (ch >= 'A')
  200. return (ch - 'A' + 12);
  201. if (ch > '9')
  202. return 0;
  203. if (ch >= '.')
  204. return (ch - '.');
  205. return 0;
  206. }
  207. /* Static stuff that stays resident and doesn't change after
  208. * being initialized, and therefore doesn't need to be made
  209. * reentrant. */
  210. struct const_des_ctx {
  211. #if USE_ip_mask
  212. uint8_t init_perm[64]; /* referenced 2 times */
  213. #endif
  214. uint8_t final_perm[64]; /* 2 times */
  215. uint8_t m_sbox[4][4096]; /* 5 times */
  216. };
  217. #define C (*cctx)
  218. #define init_perm (C.init_perm )
  219. #define final_perm (C.final_perm)
  220. #define m_sbox (C.m_sbox )
  221. static struct const_des_ctx*
  222. const_des_init(void)
  223. {
  224. unsigned i, j, b;
  225. struct const_des_ctx *cctx;
  226. #if !USE_PRECOMPUTED_u_sbox
  227. uint8_t u_sbox[8][64];
  228. cctx = xmalloc(sizeof(*cctx));
  229. /* Invert the S-boxes, reordering the input bits. */
  230. for (i = 0; i < 8; i++) {
  231. for (j = 0; j < 64; j++) {
  232. b = (j & 0x20) | ((j & 1) << 4) | ((j >> 1) & 0xf);
  233. u_sbox[i][j] = sbox[i][b];
  234. }
  235. }
  236. for (i = 0; i < 8; i++) {
  237. fprintf(stderr, "\t{\t");
  238. for (j = 0; j < 64; j+=2)
  239. fprintf(stderr, " 0x%02x,", u_sbox[i][j] + u_sbox[i][j+1]*16);
  240. fprintf(stderr, "\n\t},\n");
  241. }
  242. /*
  243. * Convert the inverted S-boxes into 4 arrays of 8 bits.
  244. * Each will handle 12 bits of the S-box input.
  245. */
  246. for (b = 0; b < 4; b++)
  247. for (i = 0; i < 64; i++)
  248. for (j = 0; j < 64; j++)
  249. m_sbox[b][(i << 6) | j] =
  250. (uint8_t)((u_sbox[(b << 1)][i] << 4) |
  251. u_sbox[(b << 1) + 1][j]);
  252. #else
  253. cctx = xmalloc(sizeof(*cctx));
  254. /*
  255. * Convert the inverted S-boxes into 4 arrays of 8 bits.
  256. * Each will handle 12 bits of the S-box input.
  257. */
  258. for (b = 0; b < 4; b++)
  259. for (i = 0; i < 64; i++)
  260. for (j = 0; j < 64; j++) {
  261. uint8_t lo, hi;
  262. hi = u_sbox[(b << 1)][i / 2];
  263. if (!(i & 1))
  264. hi <<= 4;
  265. lo = u_sbox[(b << 1) + 1][j / 2];
  266. if (j & 1)
  267. lo >>= 4;
  268. m_sbox[b][(i << 6) | j] = (hi & 0xf0) | (lo & 0x0f);
  269. }
  270. #endif
  271. /*
  272. * Set up the initial & final permutations into a useful form.
  273. */
  274. for (i = 0; i < 64; i++) {
  275. final_perm[i] = IP[i] - 1;
  276. #if USE_ip_mask
  277. init_perm[final_perm[i]] = (uint8_t)i;
  278. #endif
  279. }
  280. return cctx;
  281. }
  282. struct des_ctx {
  283. const struct const_des_ctx *const_ctx;
  284. uint32_t saltbits; /* referenced 5 times */
  285. #if USE_REPETITIVE_SPEEDUP
  286. uint32_t old_salt; /* 3 times */
  287. uint32_t old_rawkey0, old_rawkey1; /* 3 times each */
  288. #endif
  289. uint8_t un_pbox[32]; /* 2 times */
  290. uint8_t inv_comp_perm[56]; /* 3 times */
  291. uint8_t inv_key_perm[64]; /* 3 times */
  292. uint32_t en_keysl[16], en_keysr[16]; /* 2 times each */
  293. #if USE_de_keys
  294. uint32_t de_keysl[16], de_keysr[16]; /* 2 times each */
  295. #endif
  296. #if USE_ip_mask
  297. uint32_t ip_maskl[8][256], ip_maskr[8][256]; /* 9 times each */
  298. #endif
  299. uint32_t fp_maskl[8][256], fp_maskr[8][256]; /* 9 times each */
  300. uint32_t key_perm_maskl[8][128], key_perm_maskr[8][128]; /* 9 times */
  301. uint32_t comp_maskl[8][128], comp_maskr[8][128]; /* 9 times each */
  302. uint32_t psbox[4][256]; /* 5 times */
  303. };
  304. #define D (*ctx)
  305. #define const_ctx (D.const_ctx )
  306. #define saltbits (D.saltbits )
  307. #define old_salt (D.old_salt )
  308. #define old_rawkey0 (D.old_rawkey0 )
  309. #define old_rawkey1 (D.old_rawkey1 )
  310. #define un_pbox (D.un_pbox )
  311. #define inv_comp_perm (D.inv_comp_perm )
  312. #define inv_key_perm (D.inv_key_perm )
  313. #define en_keysl (D.en_keysl )
  314. #define en_keysr (D.en_keysr )
  315. #define de_keysl (D.de_keysl )
  316. #define de_keysr (D.de_keysr )
  317. #define ip_maskl (D.ip_maskl )
  318. #define ip_maskr (D.ip_maskr )
  319. #define fp_maskl (D.fp_maskl )
  320. #define fp_maskr (D.fp_maskr )
  321. #define key_perm_maskl (D.key_perm_maskl )
  322. #define key_perm_maskr (D.key_perm_maskr )
  323. #define comp_maskl (D.comp_maskl )
  324. #define comp_maskr (D.comp_maskr )
  325. #define psbox (D.psbox )
  326. static struct des_ctx*
  327. des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx)
  328. {
  329. int i, j, b, k, inbit, obit;
  330. uint32_t p;
  331. const uint32_t *bits28, *bits24;
  332. if (!ctx)
  333. ctx = xmalloc(sizeof(*ctx));
  334. const_ctx = cctx;
  335. #if USE_REPETITIVE_SPEEDUP
  336. old_rawkey0 = old_rawkey1 = 0;
  337. old_salt = 0;
  338. #endif
  339. saltbits = 0;
  340. bits28 = bits32 + 4;
  341. bits24 = bits28 + 4;
  342. /* Initialise the inverted key permutation. */
  343. for (i = 0; i < 64; i++) {
  344. inv_key_perm[i] = 255;
  345. }
  346. /*
  347. * Invert the key permutation and initialise the inverted key
  348. * compression permutation.
  349. */
  350. for (i = 0; i < 56; i++) {
  351. inv_key_perm[key_perm[i] - 1] = (uint8_t)i;
  352. inv_comp_perm[i] = 255;
  353. }
  354. /* Invert the key compression permutation. */
  355. for (i = 0; i < 48; i++) {
  356. inv_comp_perm[comp_perm[i] - 1] = (uint8_t)i;
  357. }
  358. /*
  359. * Set up the OR-mask arrays for the initial and final permutations,
  360. * and for the key initial and compression permutations.
  361. */
  362. for (k = 0; k < 8; k++) {
  363. uint32_t il, ir;
  364. uint32_t fl, fr;
  365. for (i = 0; i < 256; i++) {
  366. #if USE_ip_mask
  367. il = 0;
  368. ir = 0;
  369. #endif
  370. fl = 0;
  371. fr = 0;
  372. for (j = 0; j < 8; j++) {
  373. inbit = 8 * k + j;
  374. if (i & bits8[j]) {
  375. #if USE_ip_mask
  376. obit = init_perm[inbit];
  377. if (obit < 32)
  378. il |= bits32[obit];
  379. else
  380. ir |= bits32[obit - 32];
  381. #endif
  382. obit = final_perm[inbit];
  383. if (obit < 32)
  384. fl |= bits32[obit];
  385. else
  386. fr |= bits32[obit - 32];
  387. }
  388. }
  389. #if USE_ip_mask
  390. ip_maskl[k][i] = il;
  391. ip_maskr[k][i] = ir;
  392. #endif
  393. fp_maskl[k][i] = fl;
  394. fp_maskr[k][i] = fr;
  395. }
  396. for (i = 0; i < 128; i++) {
  397. il = 0;
  398. ir = 0;
  399. for (j = 0; j < 7; j++) {
  400. inbit = 8 * k + j;
  401. if (i & bits8[j + 1]) {
  402. obit = inv_key_perm[inbit];
  403. if (obit == 255)
  404. continue;
  405. if (obit < 28)
  406. il |= bits28[obit];
  407. else
  408. ir |= bits28[obit - 28];
  409. }
  410. }
  411. key_perm_maskl[k][i] = il;
  412. key_perm_maskr[k][i] = ir;
  413. il = 0;
  414. ir = 0;
  415. for (j = 0; j < 7; j++) {
  416. inbit = 7 * k + j;
  417. if (i & bits8[j + 1]) {
  418. obit = inv_comp_perm[inbit];
  419. if (obit == 255)
  420. continue;
  421. if (obit < 24)
  422. il |= bits24[obit];
  423. else
  424. ir |= bits24[obit - 24];
  425. }
  426. }
  427. comp_maskl[k][i] = il;
  428. comp_maskr[k][i] = ir;
  429. }
  430. }
  431. /*
  432. * Invert the P-box permutation, and convert into OR-masks for
  433. * handling the output of the S-box arrays setup above.
  434. */
  435. for (i = 0; i < 32; i++)
  436. un_pbox[pbox[i] - 1] = (uint8_t)i;
  437. for (b = 0; b < 4; b++) {
  438. for (i = 0; i < 256; i++) {
  439. p = 0;
  440. for (j = 0; j < 8; j++) {
  441. if (i & bits8[j])
  442. p |= bits32[un_pbox[8 * b + j]];
  443. }
  444. psbox[b][i] = p;
  445. }
  446. }
  447. return ctx;
  448. }
  449. static void
  450. setup_salt(struct des_ctx *ctx, uint32_t salt)
  451. {
  452. uint32_t obit, saltbit;
  453. int i;
  454. #if USE_REPETITIVE_SPEEDUP
  455. if (salt == old_salt)
  456. return;
  457. old_salt = salt;
  458. #endif
  459. saltbits = 0;
  460. saltbit = 1;
  461. obit = 0x800000;
  462. for (i = 0; i < 24; i++) {
  463. if (salt & saltbit)
  464. saltbits |= obit;
  465. saltbit <<= 1;
  466. obit >>= 1;
  467. }
  468. }
  469. static void
  470. des_setkey(struct des_ctx *ctx, const char *key)
  471. {
  472. uint32_t k0, k1, rawkey0, rawkey1;
  473. int shifts, round;
  474. rawkey0 = ntohl(*(const uint32_t *) key);
  475. rawkey1 = ntohl(*(const uint32_t *) (key + 4));
  476. #if USE_REPETITIVE_SPEEDUP
  477. if ((rawkey0 | rawkey1)
  478. && rawkey0 == old_rawkey0
  479. && rawkey1 == old_rawkey1
  480. ) {
  481. /*
  482. * Already setup for this key.
  483. * This optimisation fails on a zero key (which is weak and
  484. * has bad parity anyway) in order to simplify the starting
  485. * conditions.
  486. */
  487. return;
  488. }
  489. old_rawkey0 = rawkey0;
  490. old_rawkey1 = rawkey1;
  491. #endif
  492. /*
  493. * Do key permutation and split into two 28-bit subkeys.
  494. */
  495. k0 = key_perm_maskl[0][rawkey0 >> 25]
  496. | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f]
  497. | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f]
  498. | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f]
  499. | key_perm_maskl[4][rawkey1 >> 25]
  500. | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f]
  501. | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f]
  502. | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f];
  503. k1 = key_perm_maskr[0][rawkey0 >> 25]
  504. | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f]
  505. | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f]
  506. | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f]
  507. | key_perm_maskr[4][rawkey1 >> 25]
  508. | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f]
  509. | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f]
  510. | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f];
  511. /*
  512. * Rotate subkeys and do compression permutation.
  513. */
  514. shifts = 0;
  515. for (round = 0; round < 16; round++) {
  516. uint32_t t0, t1;
  517. shifts += key_shifts[round];
  518. t0 = (k0 << shifts) | (k0 >> (28 - shifts));
  519. t1 = (k1 << shifts) | (k1 >> (28 - shifts));
  520. #if USE_de_keys
  521. de_keysl[15 - round] =
  522. #endif
  523. en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f]
  524. | comp_maskl[1][(t0 >> 14) & 0x7f]
  525. | comp_maskl[2][(t0 >> 7) & 0x7f]
  526. | comp_maskl[3][t0 & 0x7f]
  527. | comp_maskl[4][(t1 >> 21) & 0x7f]
  528. | comp_maskl[5][(t1 >> 14) & 0x7f]
  529. | comp_maskl[6][(t1 >> 7) & 0x7f]
  530. | comp_maskl[7][t1 & 0x7f];
  531. #if USE_de_keys
  532. de_keysr[15 - round] =
  533. #endif
  534. en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f]
  535. | comp_maskr[1][(t0 >> 14) & 0x7f]
  536. | comp_maskr[2][(t0 >> 7) & 0x7f]
  537. | comp_maskr[3][t0 & 0x7f]
  538. | comp_maskr[4][(t1 >> 21) & 0x7f]
  539. | comp_maskr[5][(t1 >> 14) & 0x7f]
  540. | comp_maskr[6][(t1 >> 7) & 0x7f]
  541. | comp_maskr[7][t1 & 0x7f];
  542. }
  543. }
  544. static void
  545. do_des(struct des_ctx *ctx, /*uint32_t l_in, uint32_t r_in,*/ uint32_t *l_out, uint32_t *r_out, int count)
  546. {
  547. const struct const_des_ctx *cctx = const_ctx;
  548. /*
  549. * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format.
  550. */
  551. uint32_t l, r, *kl, *kr;
  552. uint32_t f = f; /* silence gcc */
  553. uint32_t r48l, r48r;
  554. int round;
  555. /* Do initial permutation (IP). */
  556. #if USE_ip_mask
  557. uint32_t l_in = 0;
  558. uint32_t r_in = 0;
  559. l = ip_maskl[0][l_in >> 24]
  560. | ip_maskl[1][(l_in >> 16) & 0xff]
  561. | ip_maskl[2][(l_in >> 8) & 0xff]
  562. | ip_maskl[3][l_in & 0xff]
  563. | ip_maskl[4][r_in >> 24]
  564. | ip_maskl[5][(r_in >> 16) & 0xff]
  565. | ip_maskl[6][(r_in >> 8) & 0xff]
  566. | ip_maskl[7][r_in & 0xff];
  567. r = ip_maskr[0][l_in >> 24]
  568. | ip_maskr[1][(l_in >> 16) & 0xff]
  569. | ip_maskr[2][(l_in >> 8) & 0xff]
  570. | ip_maskr[3][l_in & 0xff]
  571. | ip_maskr[4][r_in >> 24]
  572. | ip_maskr[5][(r_in >> 16) & 0xff]
  573. | ip_maskr[6][(r_in >> 8) & 0xff]
  574. | ip_maskr[7][r_in & 0xff];
  575. #elif 0 /* -65 bytes (using the fact that l_in == r_in == 0) */
  576. l = r = 0;
  577. for (round = 0; round < 8; round++) {
  578. l |= ip_maskl[round][0];
  579. r |= ip_maskr[round][0];
  580. }
  581. bb_error_msg("l:%x r:%x", l, r); /* reports 0, 0 always! */
  582. #else /* using the fact that ip_maskX[] is constant (written to by des_init) */
  583. l = r = 0;
  584. #endif
  585. do {
  586. /* Do each round. */
  587. kl = en_keysl;
  588. kr = en_keysr;
  589. round = 16;
  590. do {
  591. /* Expand R to 48 bits (simulate the E-box). */
  592. r48l = ((r & 0x00000001) << 23)
  593. | ((r & 0xf8000000) >> 9)
  594. | ((r & 0x1f800000) >> 11)
  595. | ((r & 0x01f80000) >> 13)
  596. | ((r & 0x001f8000) >> 15);
  597. r48r = ((r & 0x0001f800) << 7)
  598. | ((r & 0x00001f80) << 5)
  599. | ((r & 0x000001f8) << 3)
  600. | ((r & 0x0000001f) << 1)
  601. | ((r & 0x80000000) >> 31);
  602. /*
  603. * Do salting for crypt() and friends, and
  604. * XOR with the permuted key.
  605. */
  606. f = (r48l ^ r48r) & saltbits;
  607. r48l ^= f ^ *kl++;
  608. r48r ^= f ^ *kr++;
  609. /*
  610. * Do sbox lookups (which shrink it back to 32 bits)
  611. * and do the pbox permutation at the same time.
  612. */
  613. f = psbox[0][m_sbox[0][r48l >> 12]]
  614. | psbox[1][m_sbox[1][r48l & 0xfff]]
  615. | psbox[2][m_sbox[2][r48r >> 12]]
  616. | psbox[3][m_sbox[3][r48r & 0xfff]];
  617. /* Now that we've permuted things, complete f(). */
  618. f ^= l;
  619. l = r;
  620. r = f;
  621. } while (--round);
  622. r = l;
  623. l = f;
  624. } while (--count);
  625. /* Do final permutation (inverse of IP). */
  626. *l_out = fp_maskl[0][l >> 24]
  627. | fp_maskl[1][(l >> 16) & 0xff]
  628. | fp_maskl[2][(l >> 8) & 0xff]
  629. | fp_maskl[3][l & 0xff]
  630. | fp_maskl[4][r >> 24]
  631. | fp_maskl[5][(r >> 16) & 0xff]
  632. | fp_maskl[6][(r >> 8) & 0xff]
  633. | fp_maskl[7][r & 0xff];
  634. *r_out = fp_maskr[0][l >> 24]
  635. | fp_maskr[1][(l >> 16) & 0xff]
  636. | fp_maskr[2][(l >> 8) & 0xff]
  637. | fp_maskr[3][l & 0xff]
  638. | fp_maskr[4][r >> 24]
  639. | fp_maskr[5][(r >> 16) & 0xff]
  640. | fp_maskr[6][(r >> 8) & 0xff]
  641. | fp_maskr[7][r & 0xff];
  642. }
  643. #define DES_OUT_BUFSIZE 21
  644. static void
  645. to64_msb_first(char *s, unsigned v)
  646. {
  647. #if 0
  648. *s++ = ascii64[(v >> 18) & 0x3f]; /* bits 23..18 */
  649. *s++ = ascii64[(v >> 12) & 0x3f]; /* bits 17..12 */
  650. *s++ = ascii64[(v >> 6) & 0x3f]; /* bits 11..6 */
  651. *s = ascii64[v & 0x3f]; /* bits 5..0 */
  652. #endif
  653. *s++ = i64c(v >> 18); /* bits 23..18 */
  654. *s++ = i64c(v >> 12); /* bits 17..12 */
  655. *s++ = i64c(v >> 6); /* bits 11..6 */
  656. *s = i64c(v); /* bits 5..0 */
  657. }
  658. static char *
  659. NOINLINE
  660. des_crypt(struct des_ctx *ctx, char output[DES_OUT_BUFSIZE],
  661. const unsigned char *key, const unsigned char *salt_str)
  662. {
  663. uint32_t salt, r0, r1, keybuf[2];
  664. uint8_t *q;
  665. /* Bad salt? Mimic crypt() API - return NULL */
  666. if (!salt_str[0] || !salt_str[1])
  667. return NULL;
  668. /*
  669. * Copy the key, shifting each character up by one bit
  670. * and padding with zeros.
  671. */
  672. q = (uint8_t *)keybuf;
  673. while (q - (uint8_t *)keybuf != 8) {
  674. *q = *key << 1;
  675. if (*q)
  676. key++;
  677. q++;
  678. }
  679. des_setkey(ctx, (char *)keybuf);
  680. /*
  681. * salt_str - 2 bytes of salt
  682. * key - up to 8 characters
  683. */
  684. output[0] = salt_str[0];
  685. output[1] = salt_str[1];
  686. salt = (ascii_to_bin(salt_str[1]) << 6)
  687. | ascii_to_bin(salt_str[0]);
  688. setup_salt(ctx, salt);
  689. /* Do it. */
  690. do_des(ctx, /*0, 0,*/ &r0, &r1, 25 /* count */);
  691. /* Now encode the result. */
  692. #if 0
  693. {
  694. uint32_t l = (r0 >> 8);
  695. q = (uint8_t *)output + 2;
  696. *q++ = ascii64[(l >> 18) & 0x3f]; /* bits 31..26 of r0 */
  697. *q++ = ascii64[(l >> 12) & 0x3f]; /* bits 25..20 of r0 */
  698. *q++ = ascii64[(l >> 6) & 0x3f]; /* bits 19..14 of r0 */
  699. *q++ = ascii64[l & 0x3f]; /* bits 13..8 of r0 */
  700. l = ((r0 << 16) | (r1 >> 16));
  701. *q++ = ascii64[(l >> 18) & 0x3f]; /* bits 7..2 of r0 */
  702. *q++ = ascii64[(l >> 12) & 0x3f]; /* bits 1..2 of r0 and 31..28 of r1 */
  703. *q++ = ascii64[(l >> 6) & 0x3f]; /* bits 27..22 of r1 */
  704. *q++ = ascii64[l & 0x3f]; /* bits 21..16 of r1 */
  705. l = r1 << 2;
  706. *q++ = ascii64[(l >> 12) & 0x3f]; /* bits 15..10 of r1 */
  707. *q++ = ascii64[(l >> 6) & 0x3f]; /* bits 9..4 of r1 */
  708. *q++ = ascii64[l & 0x3f]; /* bits 3..0 of r1 + 00 */
  709. *q = 0;
  710. }
  711. #else
  712. /* Each call takes low-order 24 bits and stores 4 chars */
  713. /* bits 31..8 of r0 */
  714. to64_msb_first(output + 2, (r0 >> 8));
  715. /* bits 7..0 of r0 and 31..16 of r1 */
  716. to64_msb_first(output + 6, (r0 << 16) | (r1 >> 16));
  717. /* bits 15..0 of r1 and two zero bits (plus extra zero byte) */
  718. to64_msb_first(output + 10, (r1 << 8));
  719. /* extra zero byte is encoded as '.', fixing it */
  720. output[13] = '\0';
  721. #endif
  722. return output;
  723. }
  724. #undef USE_PRECOMPUTED_u_sbox
  725. #undef USE_REPETITIVE_SPEEDUP
  726. #undef USE_ip_mask
  727. #undef USE_de_keys
  728. #undef C
  729. #undef init_perm
  730. #undef final_perm
  731. #undef m_sbox
  732. #undef D
  733. #undef const_ctx
  734. #undef saltbits
  735. #undef old_salt
  736. #undef old_rawkey0
  737. #undef old_rawkey1
  738. #undef un_pbox
  739. #undef inv_comp_perm
  740. #undef inv_key_perm
  741. #undef en_keysl
  742. #undef en_keysr
  743. #undef de_keysl
  744. #undef de_keysr
  745. #undef ip_maskl
  746. #undef ip_maskr
  747. #undef fp_maskl
  748. #undef fp_maskr
  749. #undef key_perm_maskl
  750. #undef key_perm_maskr
  751. #undef comp_maskl
  752. #undef comp_maskr
  753. #undef psbox