crypt_util.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /*
  2. * UFC-crypt: ultra fast crypt(3) implementation
  3. *
  4. * Copyright (C) 1991-2019 Free Software Foundation, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; see the file COPYING.LIB. If not,
  18. * see <http://www.gnu.org/licenses/>.
  19. *
  20. * @(#)crypt_util.c 2.56 12/20/96
  21. *
  22. * Support routines
  23. *
  24. */
  25. #ifdef DEBUG
  26. #include <stdio.h>
  27. #endif
  28. #include <atomic.h>
  29. #include <string.h>
  30. #ifndef STATIC
  31. #define STATIC static
  32. #endif
  33. #include "crypt-private.h"
  34. #include <shlib-compat.h>
  35. /* Prototypes for local functions. */
  36. #ifndef __GNU_LIBRARY__
  37. void _ufc_clearmem (char *start, int cnt);
  38. void _ufc_copymem (char *from, char *to, int cnt);
  39. #endif
  40. #ifdef _UFC_32_
  41. STATIC void shuffle_sb (long32 *k, ufc_long saltbits);
  42. #else
  43. STATIC void shuffle_sb (long64 *k, ufc_long saltbits);
  44. #endif
  45. /*
  46. * Permutation done once on the 56 bit
  47. * key derived from the original 8 byte ASCII key.
  48. */
  49. static const int pc1[56] = {
  50. 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
  51. 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
  52. 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
  53. 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
  54. };
  55. /*
  56. * How much to rotate each 28 bit half of the pc1 permutated
  57. * 56 bit key before using pc2 to give the i' key
  58. */
  59. static const int rots[16] = {
  60. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  61. };
  62. /*
  63. * Permutation giving the key
  64. * of the i' DES round
  65. */
  66. static const int pc2[48] = {
  67. 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
  68. 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
  69. 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
  70. 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
  71. };
  72. /*
  73. * The E expansion table which selects
  74. * bits from the 32 bit intermediate result.
  75. */
  76. static const int esel[48] = {
  77. 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
  78. 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
  79. 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
  80. 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1
  81. };
  82. /*
  83. * Permutation done on the
  84. * result of sbox lookups
  85. */
  86. static const int perm32[32] = {
  87. 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
  88. 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
  89. };
  90. /*
  91. * The sboxes
  92. */
  93. static const int sbox[8][4][16]= {
  94. { { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 },
  95. { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 },
  96. { 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 },
  97. { 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 }
  98. },
  99. { { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 },
  100. { 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 },
  101. { 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 },
  102. { 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 }
  103. },
  104. { { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 },
  105. { 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 },
  106. { 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 },
  107. { 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 }
  108. },
  109. { { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 },
  110. { 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 },
  111. { 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 },
  112. { 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 }
  113. },
  114. { { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 },
  115. { 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 },
  116. { 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 },
  117. { 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 }
  118. },
  119. { { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 },
  120. { 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 },
  121. { 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 },
  122. { 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 }
  123. },
  124. { { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 },
  125. { 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 },
  126. { 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 },
  127. { 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 }
  128. },
  129. { { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 },
  130. { 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 },
  131. { 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 },
  132. { 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 }
  133. }
  134. };
  135. #if SHLIB_COMPAT (libcrypt, GLIBC_2_0, GLIBC_2_28)
  136. /*
  137. * This is the initial
  138. * permutation matrix
  139. */
  140. static const int initial_perm[64] = {
  141. 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
  142. 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
  143. 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
  144. 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
  145. };
  146. #endif
  147. /*
  148. * This is the final
  149. * permutation matrix
  150. */
  151. static const int final_perm[64] = {
  152. 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31,
  153. 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29,
  154. 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27,
  155. 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25
  156. };
  157. #define ascii_to_bin(c) ((c)>='a'?(c-59):(c)>='A'?((c)-53):(c)-'.')
  158. #define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')
  159. static const ufc_long BITMASK[24] = {
  160. 0x40000000, 0x20000000, 0x10000000, 0x08000000, 0x04000000, 0x02000000,
  161. 0x01000000, 0x00800000, 0x00400000, 0x00200000, 0x00100000, 0x00080000,
  162. 0x00004000, 0x00002000, 0x00001000, 0x00000800, 0x00000400, 0x00000200,
  163. 0x00000100, 0x00000080, 0x00000040, 0x00000020, 0x00000010, 0x00000008
  164. };
  165. static const unsigned char bytemask[8] = {
  166. 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
  167. };
  168. static const ufc_long longmask[32] = {
  169. 0x80000000, 0x40000000, 0x20000000, 0x10000000,
  170. 0x08000000, 0x04000000, 0x02000000, 0x01000000,
  171. 0x00800000, 0x00400000, 0x00200000, 0x00100000,
  172. 0x00080000, 0x00040000, 0x00020000, 0x00010000,
  173. 0x00008000, 0x00004000, 0x00002000, 0x00001000,
  174. 0x00000800, 0x00000400, 0x00000200, 0x00000100,
  175. 0x00000080, 0x00000040, 0x00000020, 0x00000010,
  176. 0x00000008, 0x00000004, 0x00000002, 0x00000001
  177. };
  178. /*
  179. * do_pc1: permform pc1 permutation in the key schedule generation.
  180. *
  181. * The first index is the byte number in the 8 byte ASCII key
  182. * - second - - the two 28 bits halfs of the result
  183. * - third - selects the 7 bits actually used of each byte
  184. *
  185. * The result is kept with 28 bit per 32 bit with the 4 most significant
  186. * bits zero.
  187. */
  188. static ufc_long do_pc1[8][2][128];
  189. /*
  190. * do_pc2: permform pc2 permutation in the key schedule generation.
  191. *
  192. * The first index is the septet number in the two 28 bit intermediate values
  193. * - second - - - septet values
  194. *
  195. * Knowledge of the structure of the pc2 permutation is used.
  196. *
  197. * The result is kept with 28 bit per 32 bit with the 4 most significant
  198. * bits zero.
  199. */
  200. static ufc_long do_pc2[8][128];
  201. /*
  202. * eperm32tab: do 32 bit permutation and E selection
  203. *
  204. * The first index is the byte number in the 32 bit value to be permuted
  205. * - second - is the value of this byte
  206. * - third - selects the two 32 bit values
  207. *
  208. * The table is used and generated internally in init_des to speed it up
  209. */
  210. static ufc_long eperm32tab[4][256][2];
  211. /*
  212. * efp: undo an extra e selection and do final
  213. * permutation giving the DES result.
  214. *
  215. * Invoked 6 bit a time on two 48 bit values
  216. * giving two 32 bit longs.
  217. */
  218. static ufc_long efp[16][64][2];
  219. /* Table with characters for base64 transformation. */
  220. static const char b64t[64] =
  221. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  222. /*
  223. * For use by the old, non-reentrant routines
  224. * (crypt/encrypt/setkey)
  225. */
  226. struct crypt_data _ufc_foobar;
  227. #ifdef __GNU_LIBRARY__
  228. #include <libc-lock.h>
  229. __libc_lock_define_initialized (static, _ufc_tables_lock)
  230. #endif
  231. #ifdef DEBUG
  232. void
  233. _ufc_prbits (ufc_long *a, int n)
  234. {
  235. ufc_long i, j, t, tmp;
  236. n /= 8;
  237. for(i = 0; i < n; i++) {
  238. tmp=0;
  239. for(j = 0; j < 8; j++) {
  240. t=8*i+j;
  241. tmp|=(a[t/24] & BITMASK[t % 24])?bytemask[j]:0;
  242. }
  243. (void)printf("%02lx ", tmp);
  244. }
  245. printf(" ");
  246. }
  247. static void __attribute__ ((unused))
  248. _ufc_set_bits (ufc_long v, ufc_long *b)
  249. {
  250. ufc_long i;
  251. *b = 0;
  252. for(i = 0; i < 24; i++) {
  253. if(v & longmask[8 + i])
  254. *b |= BITMASK[i];
  255. }
  256. }
  257. #endif
  258. #ifndef __GNU_LIBRARY__
  259. /*
  260. * Silly rewrites of 'bzero'/'memset'. I do so
  261. * because some machines don't have
  262. * bzero and some don't have memset.
  263. */
  264. void
  265. _ufc_clearmem (char *start, int cnt)
  266. {
  267. while(cnt--)
  268. *start++ = '\0';
  269. }
  270. void
  271. _ufc_copymem (char *from, char *to, int cnt)
  272. {
  273. while(cnt--)
  274. *to++ = *from++;
  275. }
  276. #else
  277. #define _ufc_clearmem(start, cnt) memset(start, 0, cnt)
  278. #define _ufc_copymem(from, to, cnt) memcpy(to, from, cnt)
  279. #endif
  280. /* lookup a 6 bit value in sbox */
  281. #define s_lookup(i,s) sbox[(i)][(((s)>>4) & 0x2)|((s) & 0x1)][((s)>>1) & 0xf];
  282. /*
  283. * Initialize unit - may be invoked directly
  284. * by fcrypt users.
  285. */
  286. void
  287. __init_des_r (struct crypt_data * __restrict __data)
  288. {
  289. int comes_from_bit;
  290. int bit, sg;
  291. ufc_long j;
  292. ufc_long mask1, mask2;
  293. int e_inverse[64];
  294. static volatile int small_tables_initialized = 0;
  295. #ifdef _UFC_32_
  296. long32 *sb[4];
  297. sb[0] = (long32*)__data->sb0; sb[1] = (long32*)__data->sb1;
  298. sb[2] = (long32*)__data->sb2; sb[3] = (long32*)__data->sb3;
  299. #endif
  300. #ifdef _UFC_64_
  301. long64 *sb[4];
  302. sb[0] = (long64*)__data->sb0; sb[1] = (long64*)__data->sb1;
  303. sb[2] = (long64*)__data->sb2; sb[3] = (long64*)__data->sb3;
  304. #endif
  305. if(small_tables_initialized == 0) {
  306. #ifdef __GNU_LIBRARY__
  307. __libc_lock_lock (_ufc_tables_lock);
  308. if(small_tables_initialized)
  309. goto small_tables_done;
  310. #endif
  311. /*
  312. * Create the do_pc1 table used
  313. * to affect pc1 permutation
  314. * when generating keys
  315. */
  316. _ufc_clearmem((char*)do_pc1, (int)sizeof(do_pc1));
  317. for(bit = 0; bit < 56; bit++) {
  318. comes_from_bit = pc1[bit] - 1;
  319. mask1 = bytemask[comes_from_bit % 8 + 1];
  320. mask2 = longmask[bit % 28 + 4];
  321. for(j = 0; j < 128; j++) {
  322. if(j & mask1)
  323. do_pc1[comes_from_bit / 8][bit / 28][j] |= mask2;
  324. }
  325. }
  326. /*
  327. * Create the do_pc2 table used
  328. * to affect pc2 permutation when
  329. * generating keys
  330. */
  331. _ufc_clearmem((char*)do_pc2, (int)sizeof(do_pc2));
  332. for(bit = 0; bit < 48; bit++) {
  333. comes_from_bit = pc2[bit] - 1;
  334. mask1 = bytemask[comes_from_bit % 7 + 1];
  335. mask2 = BITMASK[bit % 24];
  336. for(j = 0; j < 128; j++) {
  337. if(j & mask1)
  338. do_pc2[comes_from_bit / 7][j] |= mask2;
  339. }
  340. }
  341. /*
  342. * Now generate the table used to do combined
  343. * 32 bit permutation and e expansion
  344. *
  345. * We use it because we have to permute 16384 32 bit
  346. * longs into 48 bit in order to initialize sb.
  347. *
  348. * Looping 48 rounds per permutation becomes
  349. * just too slow...
  350. *
  351. */
  352. _ufc_clearmem((char*)eperm32tab, (int)sizeof(eperm32tab));
  353. for(bit = 0; bit < 48; bit++) {
  354. ufc_long mask1,comes_from;
  355. comes_from = perm32[esel[bit]-1]-1;
  356. mask1 = bytemask[comes_from % 8];
  357. for(j = 256; j--;) {
  358. if(j & mask1)
  359. eperm32tab[comes_from / 8][j][bit / 24] |= BITMASK[bit % 24];
  360. }
  361. }
  362. /*
  363. * Create an inverse matrix for esel telling
  364. * where to plug out bits if undoing it
  365. */
  366. for(bit=48; bit--;) {
  367. e_inverse[esel[bit] - 1 ] = bit;
  368. e_inverse[esel[bit] - 1 + 32] = bit + 48;
  369. }
  370. /*
  371. * create efp: the matrix used to
  372. * undo the E expansion and effect final permutation
  373. */
  374. _ufc_clearmem((char*)efp, (int)sizeof efp);
  375. for(bit = 0; bit < 64; bit++) {
  376. int o_bit, o_long;
  377. ufc_long word_value, mask1, mask2;
  378. int comes_from_f_bit, comes_from_e_bit;
  379. int comes_from_word, bit_within_word;
  380. /* See where bit i belongs in the two 32 bit long's */
  381. o_long = bit / 32; /* 0..1 */
  382. o_bit = bit % 32; /* 0..31 */
  383. /*
  384. * And find a bit in the e permutated value setting this bit.
  385. *
  386. * Note: the e selection may have selected the same bit several
  387. * times. By the initialization of e_inverse, we only look
  388. * for one specific instance.
  389. */
  390. comes_from_f_bit = final_perm[bit] - 1; /* 0..63 */
  391. comes_from_e_bit = e_inverse[comes_from_f_bit]; /* 0..95 */
  392. comes_from_word = comes_from_e_bit / 6; /* 0..15 */
  393. bit_within_word = comes_from_e_bit % 6; /* 0..5 */
  394. mask1 = longmask[bit_within_word + 26];
  395. mask2 = longmask[o_bit];
  396. for(word_value = 64; word_value--;) {
  397. if(word_value & mask1)
  398. efp[comes_from_word][word_value][o_long] |= mask2;
  399. }
  400. }
  401. atomic_write_barrier ();
  402. small_tables_initialized = 1;
  403. #ifdef __GNU_LIBRARY__
  404. small_tables_done:
  405. __libc_lock_unlock(_ufc_tables_lock);
  406. #endif
  407. } else
  408. atomic_read_barrier ();
  409. /*
  410. * Create the sb tables:
  411. *
  412. * For each 12 bit segment of an 48 bit intermediate
  413. * result, the sb table precomputes the two 4 bit
  414. * values of the sbox lookups done with the two 6
  415. * bit halves, shifts them to their proper place,
  416. * sends them through perm32 and finally E expands
  417. * them so that they are ready for the next
  418. * DES round.
  419. *
  420. */
  421. if (__data->sb0 + sizeof (__data->sb0) == __data->sb1
  422. && __data->sb1 + sizeof (__data->sb1) == __data->sb2
  423. && __data->sb2 + sizeof (__data->sb2) == __data->sb3)
  424. _ufc_clearmem(__data->sb0,
  425. (int)sizeof(__data->sb0)
  426. + (int)sizeof(__data->sb1)
  427. + (int)sizeof(__data->sb2)
  428. + (int)sizeof(__data->sb3));
  429. else {
  430. _ufc_clearmem(__data->sb0, (int)sizeof(__data->sb0));
  431. _ufc_clearmem(__data->sb1, (int)sizeof(__data->sb1));
  432. _ufc_clearmem(__data->sb2, (int)sizeof(__data->sb2));
  433. _ufc_clearmem(__data->sb3, (int)sizeof(__data->sb3));
  434. }
  435. for(sg = 0; sg < 4; sg++) {
  436. int j1, j2;
  437. int s1, s2;
  438. for(j1 = 0; j1 < 64; j1++) {
  439. s1 = s_lookup(2 * sg, j1);
  440. for(j2 = 0; j2 < 64; j2++) {
  441. ufc_long to_permute, inx;
  442. s2 = s_lookup(2 * sg + 1, j2);
  443. to_permute = (((ufc_long)s1 << 4) |
  444. (ufc_long)s2) << (24 - 8 * (ufc_long)sg);
  445. #ifdef _UFC_32_
  446. inx = ((j1 << 6) | j2) << 1;
  447. sb[sg][inx ] = eperm32tab[0][(to_permute >> 24) & 0xff][0];
  448. sb[sg][inx+1] = eperm32tab[0][(to_permute >> 24) & 0xff][1];
  449. sb[sg][inx ] |= eperm32tab[1][(to_permute >> 16) & 0xff][0];
  450. sb[sg][inx+1] |= eperm32tab[1][(to_permute >> 16) & 0xff][1];
  451. sb[sg][inx ] |= eperm32tab[2][(to_permute >> 8) & 0xff][0];
  452. sb[sg][inx+1] |= eperm32tab[2][(to_permute >> 8) & 0xff][1];
  453. sb[sg][inx ] |= eperm32tab[3][(to_permute) & 0xff][0];
  454. sb[sg][inx+1] |= eperm32tab[3][(to_permute) & 0xff][1];
  455. #endif
  456. #ifdef _UFC_64_
  457. inx = ((j1 << 6) | j2);
  458. sb[sg][inx] =
  459. ((long64)eperm32tab[0][(to_permute >> 24) & 0xff][0] << 32) |
  460. (long64)eperm32tab[0][(to_permute >> 24) & 0xff][1];
  461. sb[sg][inx] |=
  462. ((long64)eperm32tab[1][(to_permute >> 16) & 0xff][0] << 32) |
  463. (long64)eperm32tab[1][(to_permute >> 16) & 0xff][1];
  464. sb[sg][inx] |=
  465. ((long64)eperm32tab[2][(to_permute >> 8) & 0xff][0] << 32) |
  466. (long64)eperm32tab[2][(to_permute >> 8) & 0xff][1];
  467. sb[sg][inx] |=
  468. ((long64)eperm32tab[3][(to_permute) & 0xff][0] << 32) |
  469. (long64)eperm32tab[3][(to_permute) & 0xff][1];
  470. #endif
  471. }
  472. }
  473. }
  474. __data->current_saltbits = 0;
  475. __data->current_salt[0] = 0;
  476. __data->current_salt[1] = 0;
  477. __data->initialized++;
  478. }
  479. void
  480. __init_des (void)
  481. {
  482. __init_des_r(&_ufc_foobar);
  483. }
  484. /*
  485. * Process the elements of the sb table permuting the
  486. * bits swapped in the expansion by the current salt.
  487. */
  488. #ifdef _UFC_32_
  489. STATIC void
  490. shuffle_sb (long32 *k, ufc_long saltbits)
  491. {
  492. ufc_long j;
  493. long32 x;
  494. for(j=4096; j--;) {
  495. x = (k[0] ^ k[1]) & (long32)saltbits;
  496. *k++ ^= x;
  497. *k++ ^= x;
  498. }
  499. }
  500. #endif
  501. #ifdef _UFC_64_
  502. STATIC void
  503. shuffle_sb (long64 *k, ufc_long saltbits)
  504. {
  505. ufc_long j;
  506. long64 x;
  507. for(j=4096; j--;) {
  508. x = ((*k >> 32) ^ *k) & (long64)saltbits;
  509. *k++ ^= (x << 32) | x;
  510. }
  511. }
  512. #endif
  513. /*
  514. * Return false iff C is in the specified alphabet for crypt salt.
  515. */
  516. static bool
  517. bad_for_salt (char c)
  518. {
  519. switch (c)
  520. {
  521. case '0' ... '9':
  522. case 'A' ... 'Z':
  523. case 'a' ... 'z':
  524. case '.': case '/':
  525. return false;
  526. default:
  527. return true;
  528. }
  529. }
  530. /*
  531. * Setup the unit for a new salt
  532. * Hopefully we'll not see a new salt in each crypt call.
  533. * Return false if an unexpected character was found in s[0] or s[1].
  534. */
  535. bool
  536. _ufc_setup_salt_r (const char *s, struct crypt_data * __restrict __data)
  537. {
  538. ufc_long i, j, saltbits;
  539. char s0, s1;
  540. if(__data->initialized == 0)
  541. __init_des_r(__data);
  542. s0 = s[0];
  543. if(bad_for_salt (s0))
  544. return false;
  545. s1 = s[1];
  546. if(bad_for_salt (s1))
  547. return false;
  548. if(s0 == __data->current_salt[0] && s1 == __data->current_salt[1])
  549. return true;
  550. __data->current_salt[0] = s0;
  551. __data->current_salt[1] = s1;
  552. /*
  553. * This is the only crypt change to DES:
  554. * entries are swapped in the expansion table
  555. * according to the bits set in the salt.
  556. */
  557. saltbits = 0;
  558. for(i = 0; i < 2; i++) {
  559. long c=ascii_to_bin(s[i]);
  560. for(j = 0; j < 6; j++) {
  561. if((c >> j) & 0x1)
  562. saltbits |= BITMASK[6 * i + j];
  563. }
  564. }
  565. /*
  566. * Permute the sb table values
  567. * to reflect the changed e
  568. * selection table
  569. */
  570. #ifdef _UFC_32_
  571. #define LONGG long32*
  572. #endif
  573. #ifdef _UFC_64_
  574. #define LONGG long64*
  575. #endif
  576. shuffle_sb((LONGG)__data->sb0, __data->current_saltbits ^ saltbits);
  577. shuffle_sb((LONGG)__data->sb1, __data->current_saltbits ^ saltbits);
  578. shuffle_sb((LONGG)__data->sb2, __data->current_saltbits ^ saltbits);
  579. shuffle_sb((LONGG)__data->sb3, __data->current_saltbits ^ saltbits);
  580. __data->current_saltbits = saltbits;
  581. return true;
  582. }
  583. void
  584. _ufc_mk_keytab_r (const char *key, struct crypt_data * __restrict __data)
  585. {
  586. ufc_long v1, v2, *k1;
  587. int i;
  588. #ifdef _UFC_32_
  589. long32 v, *k2;
  590. k2 = (long32*)__data->keysched;
  591. #endif
  592. #ifdef _UFC_64_
  593. long64 v, *k2;
  594. k2 = (long64*)__data->keysched;
  595. #endif
  596. v1 = v2 = 0; k1 = &do_pc1[0][0][0];
  597. for(i = 8; i--;) {
  598. v1 |= k1[*key & 0x7f]; k1 += 128;
  599. v2 |= k1[*key++ & 0x7f]; k1 += 128;
  600. }
  601. for(i = 0; i < 16; i++) {
  602. k1 = &do_pc2[0][0];
  603. v1 = (v1 << rots[i]) | (v1 >> (28 - rots[i]));
  604. v = k1[(v1 >> 21) & 0x7f]; k1 += 128;
  605. v |= k1[(v1 >> 14) & 0x7f]; k1 += 128;
  606. v |= k1[(v1 >> 7) & 0x7f]; k1 += 128;
  607. v |= k1[(v1 ) & 0x7f]; k1 += 128;
  608. #ifdef _UFC_32_
  609. *k2++ = (v | 0x00008000);
  610. v = 0;
  611. #endif
  612. #ifdef _UFC_64_
  613. v = (v << 32);
  614. #endif
  615. v2 = (v2 << rots[i]) | (v2 >> (28 - rots[i]));
  616. v |= k1[(v2 >> 21) & 0x7f]; k1 += 128;
  617. v |= k1[(v2 >> 14) & 0x7f]; k1 += 128;
  618. v |= k1[(v2 >> 7) & 0x7f]; k1 += 128;
  619. v |= k1[(v2 ) & 0x7f];
  620. #ifdef _UFC_32_
  621. *k2++ = (v | 0x00008000);
  622. #endif
  623. #ifdef _UFC_64_
  624. *k2++ = v | 0x0000800000008000l;
  625. #endif
  626. }
  627. __data->direction = 0;
  628. }
  629. /*
  630. * Undo an extra E selection and do final permutations
  631. */
  632. void
  633. _ufc_dofinalperm_r (ufc_long *res, struct crypt_data * __restrict __data)
  634. {
  635. ufc_long v1, v2, x;
  636. ufc_long l1,l2,r1,r2;
  637. l1 = res[0]; l2 = res[1];
  638. r1 = res[2]; r2 = res[3];
  639. x = (l1 ^ l2) & __data->current_saltbits; l1 ^= x; l2 ^= x;
  640. x = (r1 ^ r2) & __data->current_saltbits; r1 ^= x; r2 ^= x;
  641. v1=v2=0; l1 >>= 3; l2 >>= 3; r1 >>= 3; r2 >>= 3;
  642. v1 |= efp[15][ r2 & 0x3f][0]; v2 |= efp[15][ r2 & 0x3f][1];
  643. v1 |= efp[14][(r2 >>= 6) & 0x3f][0]; v2 |= efp[14][ r2 & 0x3f][1];
  644. v1 |= efp[13][(r2 >>= 10) & 0x3f][0]; v2 |= efp[13][ r2 & 0x3f][1];
  645. v1 |= efp[12][(r2 >>= 6) & 0x3f][0]; v2 |= efp[12][ r2 & 0x3f][1];
  646. v1 |= efp[11][ r1 & 0x3f][0]; v2 |= efp[11][ r1 & 0x3f][1];
  647. v1 |= efp[10][(r1 >>= 6) & 0x3f][0]; v2 |= efp[10][ r1 & 0x3f][1];
  648. v1 |= efp[ 9][(r1 >>= 10) & 0x3f][0]; v2 |= efp[ 9][ r1 & 0x3f][1];
  649. v1 |= efp[ 8][(r1 >>= 6) & 0x3f][0]; v2 |= efp[ 8][ r1 & 0x3f][1];
  650. v1 |= efp[ 7][ l2 & 0x3f][0]; v2 |= efp[ 7][ l2 & 0x3f][1];
  651. v1 |= efp[ 6][(l2 >>= 6) & 0x3f][0]; v2 |= efp[ 6][ l2 & 0x3f][1];
  652. v1 |= efp[ 5][(l2 >>= 10) & 0x3f][0]; v2 |= efp[ 5][ l2 & 0x3f][1];
  653. v1 |= efp[ 4][(l2 >>= 6) & 0x3f][0]; v2 |= efp[ 4][ l2 & 0x3f][1];
  654. v1 |= efp[ 3][ l1 & 0x3f][0]; v2 |= efp[ 3][ l1 & 0x3f][1];
  655. v1 |= efp[ 2][(l1 >>= 6) & 0x3f][0]; v2 |= efp[ 2][ l1 & 0x3f][1];
  656. v1 |= efp[ 1][(l1 >>= 10) & 0x3f][0]; v2 |= efp[ 1][ l1 & 0x3f][1];
  657. v1 |= efp[ 0][(l1 >>= 6) & 0x3f][0]; v2 |= efp[ 0][ l1 & 0x3f][1];
  658. res[0] = v1; res[1] = v2;
  659. }
  660. /*
  661. * crypt only: convert from 64 bit to 11 bit ASCII
  662. * prefixing with the salt
  663. */
  664. void
  665. _ufc_output_conversion_r (ufc_long v1, ufc_long v2, const char *salt,
  666. struct crypt_data * __restrict __data)
  667. {
  668. int i, s, shf;
  669. __data->crypt_3_buf[0] = salt[0];
  670. __data->crypt_3_buf[1] = salt[1] ? salt[1] : salt[0];
  671. for(i = 0; i < 5; i++) {
  672. shf = (26 - 6 * i); /* to cope with MSC compiler bug */
  673. __data->crypt_3_buf[i + 2] = bin_to_ascii((v1 >> shf) & 0x3f);
  674. }
  675. s = (v2 & 0xf) << 2;
  676. v2 = (v2 >> 2) | ((v1 & 0x3) << 30);
  677. for(i = 5; i < 10; i++) {
  678. shf = (56 - 6 * i);
  679. __data->crypt_3_buf[i + 2] = bin_to_ascii((v2 >> shf) & 0x3f);
  680. }
  681. __data->crypt_3_buf[12] = bin_to_ascii(s);
  682. __data->crypt_3_buf[13] = 0;
  683. }
  684. #if SHLIB_COMPAT (libcrypt, GLIBC_2_0, GLIBC_2_28)
  685. /*
  686. * UNIX encrypt function. Takes a bitvector
  687. * represented by one byte per bit and
  688. * encrypt/decrypt according to edflag
  689. */
  690. void
  691. __encrypt_r (char *__block, int __edflag,
  692. struct crypt_data * __restrict __data)
  693. {
  694. ufc_long l1, l2, r1, r2, res[4];
  695. int i;
  696. #ifdef _UFC_32_
  697. long32 *kt;
  698. kt = (long32*)__data->keysched;
  699. #endif
  700. #ifdef _UFC_64_
  701. long64 *kt;
  702. kt = (long64*)__data->keysched;
  703. #endif
  704. /*
  705. * Undo any salt changes to E expansion
  706. */
  707. _ufc_setup_salt_r("..", __data);
  708. /*
  709. * Reverse key table if
  710. * changing operation (encrypt/decrypt)
  711. */
  712. if((__edflag == 0) != (__data->direction == 0)) {
  713. for(i = 0; i < 8; i++) {
  714. #ifdef _UFC_32_
  715. long32 x;
  716. x = kt[2 * (15-i)];
  717. kt[2 * (15-i)] = kt[2 * i];
  718. kt[2 * i] = x;
  719. x = kt[2 * (15-i) + 1];
  720. kt[2 * (15-i) + 1] = kt[2 * i + 1];
  721. kt[2 * i + 1] = x;
  722. #endif
  723. #ifdef _UFC_64_
  724. long64 x;
  725. x = kt[15-i];
  726. kt[15-i] = kt[i];
  727. kt[i] = x;
  728. #endif
  729. }
  730. __data->direction = __edflag;
  731. }
  732. /*
  733. * Do initial permutation + E expansion
  734. */
  735. i = 0;
  736. for(l1 = 0; i < 24; i++) {
  737. if(__block[initial_perm[esel[i]-1]-1])
  738. l1 |= BITMASK[i];
  739. }
  740. for(l2 = 0; i < 48; i++) {
  741. if(__block[initial_perm[esel[i]-1]-1])
  742. l2 |= BITMASK[i-24];
  743. }
  744. i = 0;
  745. for(r1 = 0; i < 24; i++) {
  746. if(__block[initial_perm[esel[i]-1+32]-1])
  747. r1 |= BITMASK[i];
  748. }
  749. for(r2 = 0; i < 48; i++) {
  750. if(__block[initial_perm[esel[i]-1+32]-1])
  751. r2 |= BITMASK[i-24];
  752. }
  753. /*
  754. * Do DES inner loops + final conversion
  755. */
  756. res[0] = l1; res[1] = l2;
  757. res[2] = r1; res[3] = r2;
  758. _ufc_doit_r((ufc_long)1, __data, &res[0]);
  759. /*
  760. * Do final permutations
  761. */
  762. _ufc_dofinalperm_r(res, __data);
  763. /*
  764. * And convert to bit array
  765. */
  766. l1 = res[0]; r1 = res[1];
  767. for(i = 0; i < 32; i++) {
  768. *__block++ = (l1 & longmask[i]) != 0;
  769. }
  770. for(i = 0; i < 32; i++) {
  771. *__block++ = (r1 & longmask[i]) != 0;
  772. }
  773. }
  774. weak_alias (__encrypt_r, encrypt_r)
  775. compat_symbol (libcrypt, encrypt_r, encrypt_r, GLIBC_2_0);
  776. void
  777. encrypt (char *__block, int __edflag)
  778. {
  779. __encrypt_r(__block, __edflag, &_ufc_foobar);
  780. }
  781. compat_symbol (libcrypt, encrypt, encrypt, GLIBC_2_0);
  782. /*
  783. * UNIX setkey function. Take a 64 bit DES
  784. * key and setup the machinery.
  785. */
  786. void
  787. __setkey_r (const char *__key, struct crypt_data * __restrict __data)
  788. {
  789. int i,j;
  790. unsigned char c;
  791. unsigned char ktab[8];
  792. _ufc_setup_salt_r("..", __data); /* be sure we're initialized */
  793. for(i = 0; i < 8; i++) {
  794. for(j = 0, c = 0; j < 8; j++)
  795. c = c << 1 | *__key++;
  796. ktab[i] = c >> 1;
  797. }
  798. _ufc_mk_keytab_r((char *) ktab, __data);
  799. }
  800. weak_alias (__setkey_r, setkey_r)
  801. compat_symbol (libcrypt, setkey_r, setkey_r, GLIBC_2_0);
  802. void
  803. setkey (const char *__key)
  804. {
  805. __setkey_r(__key, &_ufc_foobar);
  806. }
  807. compat_symbol (libcrypt, setkey, setkey, GLIBC_2_0);
  808. #endif /* SHLIB_COMPAT (libcrypt, GLIBC_2_0, GLIBC_2_28) */
  809. void
  810. __b64_from_24bit (char **cp, int *buflen,
  811. unsigned int b2, unsigned int b1, unsigned int b0,
  812. int n)
  813. {
  814. unsigned int w = (b2 << 16) | (b1 << 8) | b0;
  815. while (n-- > 0 && (*buflen) > 0)
  816. {
  817. *(*cp)++ = b64t[w & 0x3f];
  818. --(*buflen);
  819. w >>= 6;
  820. }
  821. }