home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / mint / init_5 / fcrypt.c < prev    next >
C/C++ Source or Header  |  1993-08-03  |  23KB  |  761 lines

  1. #define BIG_ENDIAN    /* mc680x0 family */
  2.  
  3. /*
  4.  * This program is copyright (c) Alec Muffett 1991 except for certain
  5.  * portions of code ("crack-fcrypt.c") copyright (c) Robert Baldwin, Icarus
  6.  * Sparry and Alec Muffett.  The author(s) disclaims all responsibility or
  7.  * liability with respect to it's usage or its effect upon hardware or
  8.  * computer systems.  This software is in freely redistributable PROVIDED
  9.  * that this notice remains intact.
  10.  */
  11.  
  12. /*
  13.  * Misc defs for the fast password transform optimisations.
  14.  */
  15.  
  16. #ifndef LOGIN
  17. #include "crack.h"        /* contains switches - AEM */
  18. #endif
  19.  
  20. /*
  21.  * Rename the types for greater convenience ? - This is from original code.
  22.  */
  23. #define    reg    register
  24. #define    uns    unsigned
  25. #define unsb    uns char
  26. #define    unsl    uns long
  27.  
  28. /*
  29.  * Types for the different ways to represent DES bit patterns.  Bits are
  30.  * always right justified within fields.  Bits which have lower indices in
  31.  * the NBS spec are stored in the vax bits with less significance (e.g., Bit
  32.  * 1 of NBS spec is stored in the bit with weight 2 ** 0 to the Vax.
  33.  */
  34.  
  35. #define    obpb1    unsb        /* One bit per byte. */
  36. #define sbpb6    unsb        /* Six bits per byte, 6 held. */
  37. #define sbpb6R    unsb        /* Six bits per byte Reversed order, 6 held. */
  38. #define    sbpb24    unsl        /* Six bits per byte, 24 held. */
  39. #define    ebpb24    unsl        /* Eight bits per bit, 24 held. */
  40. #define    fbpb4    unsb        /* Four bits per byte, 4 held. */
  41. #define    fbpb4R    unsb        /* Four bits per byte Reversed order, 4 held. */
  42.  
  43. /*
  44.  * The operation (6 * x) is often better optimised as this (for really
  45.  * braindead compilers) - AEM
  46.  */
  47.  
  48. #ifdef BRAINDEAD6
  49. #define SIX_TIMES(exprn)        (((exprn) << 2) + ((exprn) << 1))
  50. #else
  51. #define SIX_TIMES(exprn)        (6 * (exprn))
  52. #endif                /* BRAINDEAD6 */
  53.  
  54. /*
  55.  * Data segment gathered into one place - AEM
  56.  */
  57.  
  58. /* Try to keep this stuff long aligned - AEM */
  59. static char iobuf[16];
  60.  
  61. static obpb1 crypt_block[72];    /* 72 is next multiple of 8 bytes after 66 */
  62. static sbpb24 KS[32];
  63. static sbpb24 S0H[64], S1H[64], S2H[64], S3H[64];
  64. static sbpb24 S4H[64], S5H[64], S6H[64], S7H[64];
  65. static sbpb24 S0L[64], S1L[64], S2L[64], S3L[64];
  66. static sbpb24 S4L[64], S5L[64], S6L[64], S7L[64];
  67. static sbpb24 out96[4];
  68.  
  69. /*
  70.  * These used to be rather slow and frequently used functions - AEM
  71.  */
  72.  
  73. #define TF_TO_SIXBIT(tf) \
  74.     (sbpb24)((tf & 077L) | \
  75.         ((tf & 07700L) << 2) | \
  76.         ((tf & 0770000L) << 4) | \
  77.         ((tf & 077000000L) << 6))
  78.  
  79. #define SIXBIT_TO_TF(sb) \
  80.     (ebpb24)((sb & 0x3fL) | \
  81.         ((sb & 0x3f00L) >> 2) | \
  82.         ((sb & 0x3f0000L) >> 4) | \
  83.         ((sb & 0x3f000000L) >> 6))
  84. /*
  85.  * Start of the real thing
  86.  */
  87.  
  88. void
  89. fsetkey ()
  90. {
  91.     /*
  92.      * This used to be utterly horrendous. It still is, but it's much, much,
  93.      * smaller... AEM.
  94.      */
  95.     static unsb KeyToKS[] =
  96.     {
  97.     9, 50, 33, 59, 48, 16, 32, 56, 1, 8, 18, 41, 2, 34, 25, 24,
  98.     43, 57, 58, 0, 35, 26, 17, 40, 21, 27, 38, 53, 36, 3, 46, 29,
  99.     4, 52, 22, 28, 60, 20, 37, 62, 14, 19, 44, 13, 12, 61, 54, 30,
  100.     1, 42, 25, 51, 40, 8, 24, 48, 58, 0, 10, 33, 59, 26, 17, 16,
  101.     35, 49, 50, 57, 56, 18, 9, 32, 13, 19, 30, 45, 28, 62, 38, 21,
  102.     27, 44, 14, 20, 52, 12, 29, 54, 6, 11, 36, 5, 4, 53, 46, 22,
  103.     50, 26, 9, 35, 24, 57, 8, 32, 42, 49, 59, 17, 43, 10, 1, 0,
  104.     48, 33, 34, 41, 40, 2, 58, 16, 60, 3, 14, 29, 12, 46, 22, 5,
  105.     11, 28, 61, 4, 36, 27, 13, 38, 53, 62, 20, 52, 19, 37, 30, 6,
  106.     34, 10, 58, 48, 8, 41, 57, 16, 26, 33, 43, 1, 56, 59, 50, 49,
  107.     32, 17, 18, 25, 24, 51, 42, 0, 44, 54, 61, 13, 27, 30, 6, 52,
  108.     62, 12, 45, 19, 20, 11, 60, 22, 37, 46, 4, 36, 3, 21, 14, 53,
  109.     18, 59, 42, 32, 57, 25, 41, 0, 10, 17, 56, 50, 40, 43, 34, 33,
  110.     16, 1, 2, 9, 8, 35, 26, 49, 28, 38, 45, 60, 11, 14, 53, 36,
  111.     46, 27, 29, 3, 4, 62, 44, 6, 21, 30, 19, 20, 54, 5, 61, 37,
  112.     2, 43, 26, 16, 41, 9, 25, 49, 59, 1, 40, 34, 24, 56, 18, 17,
  113.     0, 50, 51, 58, 57, 48, 10, 33, 12, 22, 29, 44, 62, 61, 37, 20,
  114.     30, 11, 13, 54, 19, 46, 28, 53, 5, 14, 3, 4, 38, 52, 45, 21,
  115.     51, 56, 10, 0, 25, 58, 9, 33, 43, 50, 24, 18, 8, 40, 2, 1,
  116.     49, 34, 35, 42, 41, 32, 59, 17, 27, 6, 13, 28, 46, 45, 21, 4,
  117.     14, 62, 60, 38, 3, 30, 12, 37, 52, 61, 54, 19, 22, 36, 29, 5,
  118.     35, 40, 59, 49, 9, 42, 58, 17, 56, 34, 8, 2, 57, 24, 51, 50,
  119.     33, 18, 48, 26, 25, 16, 43, 1, 11, 53, 60, 12, 30, 29, 5, 19,
  120.     61, 46, 44, 22, 54, 14, 27, 21, 36, 45, 38, 3, 6, 20, 13, 52,
  121.     56, 32, 51, 41, 1, 34, 50, 9, 48, 26, 0, 59, 49, 16, 43, 42,
  122.     25, 10, 40, 18, 17, 8, 35, 58, 3, 45, 52, 4, 22, 21, 60, 11,
  123.     53, 38, 36, 14, 46, 6, 19, 13, 28, 37, 30, 62, 61, 12, 5, 44,
  124.     40, 16, 35, 25, 50, 18, 34, 58, 32, 10, 49, 43, 33, 0, 56, 26,
  125.     9, 59, 24, 2, 1, 57, 48, 42, 54, 29, 36, 19, 6, 5, 44, 62,
  126.     37, 22, 20, 61, 30, 53, 3, 60, 12, 21, 14, 46, 45, 27, 52, 28,
  127.     24, 0, 48, 9, 34, 2, 18, 42, 16, 59, 33, 56, 17, 49, 40, 10,
  128.     58, 43, 8, 51, 50, 41, 32, 26, 38, 13, 20, 3, 53, 52, 28, 46,
  129.     21, 6, 4, 45, 14, 37, 54, 44, 27, 5, 61, 30, 29, 11, 36, 12,
  130.     8, 49, 32, 58, 18, 51, 2, 26, 0, 43, 17, 40, 1, 33, 24, 59,
  131.     42, 56, 57, 35, 34, 25, 16, 10, 22, 60, 4, 54, 37, 36, 12, 30,
  132.     5, 53, 19, 29, 61, 21, 38, 28, 11, 52, 45, 14, 13, 62, 20, 27,
  133.     57, 33, 16, 42, 2, 35, 51, 10, 49, 56, 1, 24, 50, 17, 8, 43,
  134.     26, 40, 41, 48, 18, 9, 0, 59, 6, 44, 19, 38, 21, 20, 27, 14,
  135.     52, 37, 3, 13, 45, 5, 22, 12, 62, 36, 29, 61, 60, 46, 4, 11,
  136.     41, 17, 0, 26, 51, 48, 35, 59, 33, 40, 50, 8, 34, 1, 57, 56,
  137.     10, 24, 25, 32, 2, 58, 49, 43, 53, 28, 3, 22, 5, 4, 11, 61,
  138.     36, 21, 54, 60, 29, 52, 6, 27, 46, 20, 13, 45, 44, 30, 19, 62,
  139.     25, 1, 49, 10, 35, 32, 48, 43, 17, 24, 34, 57, 18, 50, 41, 40,
  140.     59, 8, 9, 16, 51, 42, 33, 56, 37, 12, 54, 6, 52, 19, 62, 45,
  141.     20, 5, 38, 44, 13, 36, 53, 11, 30, 4, 60, 29, 28, 14, 3, 46,
  142.     17, 58, 41, 2, 56, 24, 40, 35, 9, 16, 26, 49, 10, 42, 33, 32,
  143.     51, 0, 1, 8, 43, 34, 25, 48, 29, 4, 46, 61, 44, 11, 54, 37,
  144.     12, 60, 30, 36, 5, 28, 45, 3, 22, 27, 52, 21, 20, 6, 62, 38
  145.     };
  146.  
  147.     reg int i;
  148.     reg int j;
  149.     reg int r;
  150.     reg unsb *k;
  151.  
  152.     k = KeyToKS;
  153.  
  154.     for (i = 0; i < 32; i++)    /* loops cache better ? - AEM */
  155.     {
  156.     r = 0;
  157.     for (j = 0; j < 24; j++)
  158.     {
  159.         r |= crypt_block[*(k++)] << j;
  160.     }
  161.     KS[i] = TF_TO_SIXBIT (r);
  162.     }
  163. }
  164.  
  165. void
  166. XForm (saltvalue)
  167.     sbpb24 saltvalue;
  168. {
  169.     union
  170.     {
  171.     sbpb24 b[2];
  172.     sbpb6 c[8];
  173.     } sdata;
  174.  
  175.     /*
  176.      * Icarus Sparry, Bath - mod AEM
  177.      */
  178.  
  179. #ifdef BIG_ENDIAN
  180. #define STEP --
  181. #define START &sdata.c[7]
  182. #define Dl sdata.b[1]
  183. #define Dh sdata.b[0]
  184. #else                /* LITTLE_ENDIAN */
  185. #define STEP ++
  186. #define START &sdata.c[0]
  187. #define Dl sdata.b[0]
  188. #define Dh sdata.b[1]
  189. #endif
  190.  
  191.     /*
  192.      * Thanks to Matt Bishop for this idea... AEM.
  193.      */
  194.  
  195. #ifndef FDES_4BYTE
  196. #define SIZEFIX        0
  197. #define INDIRECT(a,b)     (a)[b]
  198. #else
  199. #define SIZEFIX        2    /* "n" where 2^n == sizeof(sbpb24) */
  200. #define INDIRECT(a,b)     (*((sbpb24 *)(((unsigned char *) a) + (b))))
  201. #endif
  202.  
  203.     reg sbpb24 Rl;
  204.     reg sbpb24 Rh;
  205.     reg sbpb24 Ll;
  206.     reg sbpb24 Lh;
  207.     reg sbpb6 *dp;
  208.  
  209.     int loop;
  210.     reg sbpb24 k;
  211.     sbpb24 *kp;
  212.     sbpb24 *kend;
  213. #ifdef FDES_8BYTE
  214.     reg sbpb24 tmpi;
  215. #endif    /* FDES_8BYTE */
  216.  
  217.     Ll = Lh = Rl = Rh = 0;
  218.  
  219.     kend = &KS[32];
  220.  
  221.     for (loop = 25; loop-- > 0; /* nothing */ )
  222.     {
  223.     for (kp = KS; kp < kend; /* nothing */ )
  224.     {
  225.         /*
  226.          * Oddly enough, direct addressing of dp slows things down, as
  227.          * well as knackering portability - AEM
  228.          */
  229.  
  230.         k = (Rl ^ Rh) & saltvalue;
  231. #ifndef FDES_8BYTE
  232.         Dl = (k ^ Rl ^ *kp++) << SIZEFIX;
  233.         Dh = (k ^ Rh ^ *kp++) << SIZEFIX;
  234. #else
  235.         /* hack to make things work better - matthew kaufman */
  236.         /* I haven't tried any of this - I don't have a cray... AEM */
  237.         tmpi = (k ^ Rl ^ *kp++);
  238.         sdata.c[3] = (tmpi >> 24) & 0x00ff;
  239.         sdata.c[2] = (tmpi >> 16) & 0x00ff;
  240.         sdata.c[1] = (tmpi >> 8) & 0x00ff;
  241.         sdata.c[0] = (tmpi) & 0x00ff;
  242.         tmpi = (k ^ Rh ^ *kp++);
  243.         sdata.c[7] = (tmpi >> 24) & 0x00ff;
  244.         sdata.c[6] = (tmpi >> 16) & 0x00ff;
  245.         sdata.c[5] = (tmpi >> 8) & 0x00ff;
  246.         sdata.c[4] = (tmpi) & 0x00ff;
  247. #endif    /* FDES_8BYTE */
  248.  
  249.         dp = START;
  250.         Lh ^= INDIRECT (S0H, *dp);
  251.         Ll ^= INDIRECT (S0L, *dp STEP);
  252.         Lh ^= INDIRECT (S1H, *dp);
  253.         Ll ^= INDIRECT (S1L, *dp STEP);
  254.         Lh ^= INDIRECT (S2H, *dp);
  255.         Ll ^= INDIRECT (S2L, *dp STEP);
  256.         Lh ^= INDIRECT (S3H, *dp);
  257.         Ll ^= INDIRECT (S3L, *dp STEP);
  258.         Lh ^= INDIRECT (S4H, *dp);
  259.         Ll ^= INDIRECT (S4L, *dp STEP);
  260.         Lh ^= INDIRECT (S5H, *dp);
  261.         Ll ^= INDIRECT (S5L, *dp STEP);
  262.         Lh ^= INDIRECT (S6H, *dp);
  263.         Ll ^= INDIRECT (S6L, *dp STEP);
  264.         Lh ^= INDIRECT (S7H, *dp);
  265.         Ll ^= INDIRECT (S7L, *dp STEP);
  266.  
  267.         k = (Ll ^ Lh) & saltvalue;
  268. #ifndef FDES_8BYTE
  269.         Dl = (k ^ Ll ^ *kp++) << SIZEFIX;
  270.         Dh = (k ^ Lh ^ *kp++) << SIZEFIX;
  271. #else
  272.         tmpi = (k ^ Ll ^ *kp++);
  273.         sdata.c[3] = (tmpi >> 24) & 0x00ff;
  274.         sdata.c[2] = (tmpi >> 16) & 0x00ff;
  275.         sdata.c[1] = (tmpi >> 8) & 0x00ff;
  276.         sdata.c[0] = (tmpi) & 0x00ff;
  277.         tmpi = (k ^ Lh ^ *kp++);
  278.         sdata.c[7] = (tmpi >> 24) & 0x00ff;
  279.         sdata.c[6] = (tmpi >> 16) & 0x00ff;
  280.         sdata.c[5] = (tmpi >> 8) & 0x00ff;
  281.         sdata.c[4] = (tmpi) & 0x00ff;
  282. #endif    /* FDES_8BYTE */
  283.  
  284.         dp = START;
  285.         Rh ^= INDIRECT (S0H, *dp);
  286.         Rl ^= INDIRECT (S0L, *dp STEP);
  287.         Rh ^= INDIRECT (S1H, *dp);
  288.         Rl ^= INDIRECT (S1L, *dp STEP);
  289.         Rh ^= INDIRECT (S2H, *dp);
  290.         Rl ^= INDIRECT (S2L, *dp STEP);
  291.         Rh ^= INDIRECT (S3H, *dp);
  292.         Rl ^= INDIRECT (S3L, *dp STEP);
  293.         Rh ^= INDIRECT (S4H, *dp);
  294.         Rl ^= INDIRECT (S4L, *dp STEP);
  295.         Rh ^= INDIRECT (S5H, *dp);
  296.         Rl ^= INDIRECT (S5L, *dp STEP);
  297.         Rh ^= INDIRECT (S6H, *dp);
  298.         Rl ^= INDIRECT (S6L, *dp STEP);
  299.         Rh ^= INDIRECT (S7H, *dp);
  300.         Rl ^= INDIRECT (S7L, *dp STEP);
  301.     }
  302.  
  303.     Ll ^= Rl;
  304.     Lh ^= Rh;
  305.     Rl ^= Ll;
  306.     Rh ^= Lh;
  307.     Ll ^= Rl;
  308.     Lh ^= Rh;
  309.     }
  310.  
  311.     /*
  312.      * for reasons that I cannot explain, if I insert the contents of the
  313.      * UnXForm function right HERE, making the tweaks as necessary to avoid
  314.      * using out96[] to pass data, I LOSE 30% of my speed.  I don't know why.
  315.      * Hence, I continue to use out96[]...
  316.      */
  317.     {
  318.     reg sbpb24 *qp;
  319.     qp = out96;
  320.     *qp++ = Ll;
  321.     *qp++ = Lh;
  322.     *qp++ = Rl;
  323.     *qp++ = Rh;
  324.     }
  325. }
  326.  
  327. void
  328. UnXForm ()
  329. {
  330.     reg obpb1 *ptr;
  331.     reg sbpb24 Rl;
  332.     reg sbpb24 Rh;
  333.     reg sbpb24 Ll;
  334.     reg sbpb24 Lh;
  335.  
  336.     Ll = SIXBIT_TO_TF (out96[0]);
  337.     Lh = SIXBIT_TO_TF (out96[1]);
  338.     Rl = SIXBIT_TO_TF (out96[2]);
  339.     Rh = SIXBIT_TO_TF (out96[3]);
  340.     ptr = crypt_block;
  341.  
  342.     *(ptr++) = Rl & 0x000400L ? 0x01 : 0;
  343.     *(ptr++) = Ll & 0x000400L ? 0x01 : 0;
  344.     *(ptr++) = Rl & 0x400000L ? 0x01 : 0;
  345.     *(ptr++) = Ll & 0x400000L ? 0x01 : 0;
  346.     *(ptr++) = Rh & 0x000400L ? 0x01 : 0;
  347.     *(ptr++) = Lh & 0x000400L ? 0x01 : 0;
  348.     *(ptr++) = Rh & 0x400000L ? 0x01 : 0;
  349.     *(ptr++) = Lh & 0x400000L ? 0x01 : 0;
  350.     *(ptr++) = Rl & 0x000200L ? 0x01 : 0;
  351.     *(ptr++) = Ll & 0x000200L ? 0x01 : 0;
  352.     *(ptr++) = Rl & 0x200000L ? 0x01 : 0;
  353.     *(ptr++) = Ll & 0x200000L ? 0x01 : 0;
  354.     *(ptr++) = Rh & 0x000200L ? 0x01 : 0;
  355.     *(ptr++) = Lh & 0x000200L ? 0x01 : 0;
  356.     *(ptr++) = Rh & 0x200000L ? 0x01 : 0;
  357.     *(ptr++) = Lh & 0x200000L ? 0x01 : 0;
  358.     *(ptr++) = Rl & 0x000100L ? 0x01 : 0;
  359.     *(ptr++) = Ll & 0x000100L ? 0x01 : 0;
  360.     *(ptr++) = Rl & 0x100000L ? 0x01 : 0;
  361.     *(ptr++) = Ll & 0x100000L ? 0x01 : 0;
  362.     *(ptr++) = Rh & 0x000100L ? 0x01 : 0;
  363.     *(ptr++) = Lh & 0x000100L ? 0x01 : 0;
  364.     *(ptr++) = Rh & 0x100000L ? 0x01 : 0;
  365.     *(ptr++) = Lh & 0x100000L ? 0x01 : 0;
  366.     *(ptr++) = Rl & 0x000080L ? 0x01 : 0;
  367.     *(ptr++) = Ll & 0x000080L ? 0x01 : 0;
  368.     *(ptr++) = Rl & 0x080000L ? 0x01 : 0;
  369.     *(ptr++) = Ll & 0x080000L ? 0x01 : 0;
  370.     *(ptr++) = Rh & 0x000080L ? 0x01 : 0;
  371.     *(ptr++) = Lh & 0x000080L ? 0x01 : 0;
  372.     *(ptr++) = Rh & 0x080000L ? 0x01 : 0;
  373.     *(ptr++) = Lh & 0x080000L ? 0x01 : 0;
  374.     *(ptr++) = Rl & 0x000010L ? 0x01 : 0;
  375.     *(ptr++) = Ll & 0x000010L ? 0x01 : 0;
  376.     *(ptr++) = Rl & 0x010000L ? 0x01 : 0;
  377.     *(ptr++) = Ll & 0x010000L ? 0x01 : 0;
  378.     *(ptr++) = Rh & 0x000010L ? 0x01 : 0;
  379.     *(ptr++) = Lh & 0x000010L ? 0x01 : 0;
  380.     *(ptr++) = Rh & 0x010000L ? 0x01 : 0;
  381.     *(ptr++) = Lh & 0x010000L ? 0x01 : 0;
  382.     *(ptr++) = Rl & 0x000008L ? 0x01 : 0;
  383.     *(ptr++) = Ll & 0x000008L ? 0x01 : 0;
  384.     *(ptr++) = Rl & 0x008000L ? 0x01 : 0;
  385.     *(ptr++) = Ll & 0x008000L ? 0x01 : 0;
  386.     *(ptr++) = Rh & 0x000008L ? 0x01 : 0;
  387.     *(ptr++) = Lh & 0x000008L ? 0x01 : 0;
  388.     *(ptr++) = Rh & 0x008000L ? 0x01 : 0;
  389.     *(ptr++) = Lh & 0x008000L ? 0x01 : 0;
  390.     *(ptr++) = Rl & 0x000004L ? 0x01 : 0;
  391.     *(ptr++) = Ll & 0x000004L ? 0x01 : 0;
  392.     *(ptr++) = Rl & 0x004000L ? 0x01 : 0;
  393.     *(ptr++) = Ll & 0x004000L ? 0x01 : 0;
  394.     *(ptr++) = Rh & 0x000004L ? 0x01 : 0;
  395.     *(ptr++) = Lh & 0x000004L ? 0x01 : 0;
  396.     *(ptr++) = Rh & 0x004000L ? 0x01 : 0;
  397.     *(ptr++) = Lh & 0x004000L ? 0x01 : 0;
  398.     *(ptr++) = Rl & 0x000002L ? 0x01 : 0;
  399.     *(ptr++) = Ll & 0x000002L ? 0x01 : 0;
  400.     *(ptr++) = Rl & 0x002000L ? 0x01 : 0;
  401.     *(ptr++) = Ll & 0x002000L ? 0x01 : 0;
  402.     *(ptr++) = Rh & 0x000002L ? 0x01 : 0;
  403.     *(ptr++) = Lh & 0x000002L ? 0x01 : 0;
  404.     *(ptr++) = Rh & 0x002000L ? 0x01 : 0;
  405.     *ptr = Lh & 0x002000L ? 0x01 : 0;
  406. }
  407.  
  408. #ifdef LOGIN
  409. static int _fcrypt_initialised = 0;
  410. #endif
  411.  
  412. char *
  413. fcrypt (pw, salt)
  414.     char *pw;
  415.     char *salt;
  416. {
  417.     /* Table lookups for salts reduce fcrypt() overhead dramatically */
  418.     static sbpb24 salt0[] =
  419.     {
  420.     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
  421.     34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  422.     50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 1,
  423.     2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 5, 6, 7, 8, 9, 10,
  424.     11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  425.     27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 32, 33, 34, 35, 36,
  426.     37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  427.     53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 1, 2, 3, 4,
  428.     5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  429.     21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
  430.     37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  431.     53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 1, 2, 3, 4,
  432.     5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  433.     21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
  434.     37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  435.     53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 1, 2, 3, 4
  436.     };
  437.     static sbpb24 salt1[] =
  438.     {
  439.     1152, 1216, 1280, 1344, 1408, 1472, 1536, 1600,
  440.     1664, 1728, 1792, 1856, 1920, 1984, 2048, 2112,
  441.     2176, 2240, 2304, 2368, 2432, 2496, 2560, 2624,
  442.     2688, 2752, 2816, 2880, 2944, 3008, 3072, 3136,
  443.     3200, 3264, 3328, 3392, 3456, 3520, 3584, 3648,
  444.     3712, 3776, 3840, 3904, 3968, 4032, 0, 64,
  445.     128, 192, 256, 320, 384, 448, 512, 576,
  446.     640, 704, 320, 384, 448, 512, 576, 640,
  447.     704, 768, 832, 896, 960, 1024, 1088, 1152,
  448.     1216, 1280, 1344, 1408, 1472, 1536, 1600, 1664,
  449.     1728, 1792, 1856, 1920, 1984, 2048, 2112, 2176,
  450.     2240, 2304, 2368, 2048, 2112, 2176, 2240, 2304,
  451.     2368, 2432, 2496, 2560, 2624, 2688, 2752, 2816,
  452.     2880, 2944, 3008, 3072, 3136, 3200, 3264, 3328,
  453.     3392, 3456, 3520, 3584, 3648, 3712, 3776, 3840,
  454.     3904, 3968, 4032, 0, 64, 128, 192, 256,
  455.     320, 384, 448, 512, 576, 640, 704, 768,
  456.     832, 896, 960, 1024, 1088, 1152, 1216, 1280,
  457.     1344, 1408, 1472, 1536, 1600, 1664, 1728, 1792,
  458.     1856, 1920, 1984, 2048, 2112, 2176, 2240, 2304,
  459.     2368, 2432, 2496, 2560, 2624, 2688, 2752, 2816,
  460.     2880, 2944, 3008, 3072, 3136, 3200, 3264, 3328,
  461.     3392, 3456, 3520, 3584, 3648, 3712, 3776, 3840,
  462.     3904, 3968, 4032, 0, 64, 128, 192, 256,
  463.     320, 384, 448, 512, 576, 640, 704, 768,
  464.     832, 896, 960, 1024, 1088, 1152, 1216, 1280,
  465.     1344, 1408, 1472, 1536, 1600, 1664, 1728, 1792,
  466.     1856, 1920, 1984, 2048, 2112, 2176, 2240, 2304,
  467.     2368, 2432, 2496, 2560, 2624, 2688, 2752, 2816,
  468.     2880, 2944, 3008, 3072, 3136, 3200, 3264, 3328,
  469.     3392, 3456, 3520, 3584, 3648, 3712, 3776, 3840,
  470.     3904, 3968, 4032, 0, 64, 128, 192, 256
  471.     };
  472.  
  473.     /* final perutation desalting */
  474.     static obpb1 final[] =
  475.     {
  476.     46, 47, 48, 49, 50, 51, 52, 53,
  477.     54, 55, 56, 57, 65, 66, 67, 68,
  478.     69, 70, 71, 72, 73, 74, 75, 76,
  479.     77, 78, 79, 80, 81, 82, 83, 84,
  480.     85, 86, 87, 88, 89, 90, 97, 98,
  481.     99, 100, 101, 102, 103, 104, 105, 106,
  482.     107, 108, 109, 110, 111, 112, 113, 114,
  483.     115, 116, 117, 118, 119, 120, 121, 122,
  484.     123, 124, 125, 126, 127, 128, 129, 130,
  485.     131, 132, 133, 134, 135, 136, 137, 138,
  486.     139, 140, 141, 142, 143, 144, 145, 146,
  487.     147, 148, 149, 150, 151, 152, 153, 154,
  488.     155, 156, 157, 158, 159, 160, 161, 162,
  489.     163, 164, 165, 166, 167, 168, 169, 170,
  490.     171, 172, 173, 174, 175, 176, 177, 178,
  491.     179, 180, 181, 182, 183, 184, 185, 186,
  492.     187, 188, 189, 190, 191, 192, 193, 194,
  493.     195, 196, 197, 198, 199, 200, 201, 202,
  494.     203, 204, 205, 206, 207, 208, 209, 210,
  495.     211, 212, 213, 214, 215, 216, 217, 218,
  496.     219, 220, 221, 222, 223, 224, 225, 226,
  497.     227, 228, 229, 230, 231, 232, 233, 234,
  498.     235, 236, 237, 238, 239, 240, 241, 242,
  499.     243, 244, 245, 246, 247, 248, 249, 250,
  500.     251, 252, 253, 254, 255,
  501.     /* Truncate overflow bits at 256 */
  502.     0, 1, 2, 3, 4, 5, 6, 7,
  503.     8, 9, 10, 11, 12, 13, 14, 15,
  504.     16, 17, 18, 19, 20, 21, 22, 23,
  505.     24, 25, 26, 27, 28, 29, 30, 31,
  506.     32, 33, 34, 35, 36, 37, 38, 39,
  507.     40, 41, 42, 43, 44, 45, 46, 47,
  508.     48, 49, 50, 51, 52, 53, 54, 55,
  509.     56, 57, 58
  510.     };
  511.  
  512.     reg int i, j, k;
  513.     reg long int *lip;
  514.     sbpb24 saltvalue;
  515.  
  516. #ifdef LOGIN
  517.     if (_fcrypt_initialised == 0)
  518.     {
  519.         init_des();
  520.     _fcrypt_initialised = 1;
  521.     }
  522. #endif
  523.  
  524. #ifdef BUILTIN_CLEAR
  525.     lip = (long int *) crypt_block;
  526.     for (i = (sizeof (crypt_block) / sizeof (long int)); i > 0; i--)
  527.     {
  528.     *(lip++) = 0L;
  529.     }
  530. #else                /* BUILTIN_CLEAR */
  531. #ifdef BZERO
  532.     bzero (crypt_block, 66);
  533. #else                /* BZERO */
  534.     for (i = 0; i < 66; i++)
  535.     {
  536.     crypt_block[i] = '\0';
  537.     }
  538. #endif                /* BZERO */
  539. #endif                /* BUILTIN_CLEAR */
  540.  
  541.     for (i = 0; (k = *pw) && i < 64; pw++)
  542.     {
  543.     crypt_block[i++] = (k >> 6) & 01;
  544.     crypt_block[i++] = (k >> 5) & 01;
  545.     crypt_block[i++] = (k >> 4) & 01;
  546.     crypt_block[i++] = (k >> 3) & 01;
  547.     crypt_block[i++] = (k >> 2) & 01;
  548.     crypt_block[i++] = (k >> 1) & 01;
  549.     crypt_block[i++] = (k >> 0) & 01;
  550.     i++;            /* have to skip one here (parity bit) */
  551.     }
  552.  
  553.     fsetkey ( /* crypt_block */ );
  554.  
  555. #ifdef BUILTIN_CLEAR
  556.     lip = (long int *) crypt_block;
  557.     for (i = (sizeof (crypt_block) / sizeof (long int)); i > 0; i--)
  558.     {
  559.     *(lip++) = 0L;
  560.     }
  561. #else                /* BUILTIN_CLEAR */
  562. #ifdef BZERO
  563.     bzero (crypt_block, 66);
  564. #else                /* BZERO */
  565.     for (i = 0; i < 66; i++)
  566.     {
  567.     crypt_block[i] = '\0';
  568.     }
  569. #endif                /* BZERO */
  570. #endif                /* BUILTIN_CLEAR */
  571.  
  572.     iobuf[0] = salt[0];
  573.     iobuf[1] = salt[1];
  574.  
  575.     saltvalue = salt0[iobuf[0]] | salt1[iobuf[1]];
  576.     saltvalue = TF_TO_SIXBIT (saltvalue);
  577.  
  578.     XForm (saltvalue);
  579.     UnXForm ();
  580.  
  581.     for (i = 0; i < 11; i++)
  582.     {
  583.     k = 0;
  584.  
  585.     for (j = 0; j < 6; j++)
  586.     {
  587.         k = (k << 1) | crypt_block[SIX_TIMES (i) + j];
  588.     }
  589.     iobuf[i + 2] = final[k];
  590.     }
  591.  
  592.     iobuf[i + 2] = 0;
  593.  
  594.     if (iobuf[1] == 0)
  595.     {
  596.     iobuf[1] = iobuf[0];
  597.     }
  598.     return (iobuf);
  599. }
  600. /********* INITIALISATION ROUTINES *********/
  601.  
  602. fbpb4
  603. lookupS (tableno, t6bits)
  604.     unsl tableno;
  605.     sbpb6R t6bits;
  606. {
  607.     static fbpb4R S[8][64] =
  608.     {
  609.     14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
  610.     0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
  611.     4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
  612.     15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
  613.  
  614.     15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
  615.     3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
  616.     0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
  617.     13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
  618.  
  619.     10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
  620.     13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
  621.     13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
  622.     1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
  623.  
  624.     7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
  625.     13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
  626.     10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
  627.     3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
  628.  
  629.     2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
  630.     14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
  631.     4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
  632.     11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
  633.  
  634.     12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
  635.     10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
  636.     9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
  637.     4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
  638.  
  639.     4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
  640.     13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
  641.     1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
  642.     6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
  643.  
  644.     13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
  645.     1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
  646.     7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
  647.     2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11,
  648.     };
  649.     sbpb6 fixed6bits;
  650.     fbpb4R r;
  651.     fbpb4 fixedr;
  652.  
  653.     fixed6bits = (((t6bits >> 0) & 01) << 5) +
  654.     (((t6bits >> 1) & 01) << 3) +
  655.     (((t6bits >> 2) & 01) << 2) +
  656.     (((t6bits >> 3) & 01) << 1) +
  657.     (((t6bits >> 4) & 01) << 0) +
  658.     (((t6bits >> 5) & 01) << 4);
  659.  
  660.     r = S[tableno][fixed6bits];
  661.  
  662.     fixedr = (((r >> 3) & 01) << 0) +
  663.     (((r >> 2) & 01) << 1) +
  664.     (((r >> 1) & 01) << 2) +
  665.     (((r >> 0) & 01) << 3);
  666.  
  667.     return (fixedr);
  668. }
  669.  
  670. void
  671. init (tableno, lowptr, highptr)
  672.     unsl tableno;
  673.     sbpb24 *lowptr, *highptr;
  674. {
  675.  
  676.     static unsb P[] =
  677.     {
  678.     15, 6, 19, 20,
  679.     28, 11, 27, 16,
  680.     0, 14, 22, 25,
  681.     4, 17, 30, 9,
  682.     1, 7, 23, 13,
  683.     31, 26, 2, 8,
  684.     18, 12, 29, 5,
  685.     21, 10, 3, 24,
  686.     };
  687.  
  688.     static unsb E[] =
  689.     {
  690.     31, 0, 1, 2, 3, 4,
  691.     3, 4, 5, 6, 7, 8,
  692.     7, 8, 9, 10, 11, 12,
  693.     11, 12, 13, 14, 15, 16,
  694.     15, 16, 17, 18, 19, 20,
  695.     19, 20, 21, 22, 23, 24,
  696.     23, 24, 25, 26, 27, 28,
  697.     27, 28, 29, 30, 31, 0,
  698.     };
  699.  
  700.     static obpb1 tmp32[32];
  701.     static obpb1 tmpP32[32];
  702.     static obpb1 tmpE[48];
  703.  
  704.     int j, k, i;
  705.     int tablenoX4;
  706.     reg sbpb24 spare24;
  707.  
  708.     tablenoX4 = tableno * 4;
  709.  
  710.     for (j = 0; j < 64; j++)
  711.     {
  712.     k = lookupS (tableno, j);
  713.  
  714.     for (i = 0; i < 32; i++)
  715.     {
  716.         tmp32[i] = 0;
  717.     }
  718.     for (i = 0; i < 4; i++)
  719.     {
  720.         tmp32[tablenoX4 + i] = (k >> i) & 01;
  721.     }
  722.     for (i = 0; i < 32; i++)
  723.     {
  724.         tmpP32[i] = tmp32[P[i]];
  725.     }
  726.     for (i = 0; i < 48; i++)
  727.     {
  728.         tmpE[i] = tmpP32[E[i]];
  729.     }
  730.  
  731.     lowptr[j] = 0;
  732.     highptr[j] = 0;
  733.  
  734.     for (i = 0; i < 24; i++)
  735.     {
  736.         lowptr[j] |= tmpE[i] << i;
  737.     }
  738.     for (k = 0, i = 24; i < 48; i++, k++)
  739.     {
  740.         highptr[j] |= tmpE[i] << k;
  741.     }
  742.  
  743.     spare24 = lowptr[j];    /* to allow for macro expansion */
  744.     lowptr[j] = TF_TO_SIXBIT (spare24);
  745.     spare24 = highptr[j];    /* to allow for macro expansion */
  746.     highptr[j] = TF_TO_SIXBIT (spare24);
  747.     }
  748. }
  749. init_des ()
  750. {
  751.     init (0, S0L, S0H);
  752.     init (1, S1L, S1H);
  753.     init (2, S2L, S2H);
  754.     init (3, S3L, S3H);
  755.     init (4, S4L, S4H);
  756.     init (5, S5L, S5H);
  757.     init (6, S6L, S6H);
  758.     init (7, S7L, S7H);
  759. }
  760.  
  761.