home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xntp3.zip / authstuff / makeIPFP.c < prev    next >
C/C++ Source or Header  |  1992-08-10  |  7KB  |  338 lines

  1. /*
  2.  * makeIPFP - make fast DES IP and FP tables
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7.  
  8. #define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
  9.  
  10. u_long IPL[256];
  11. u_long FPL[256];
  12.  
  13. char *progname;
  14. int debug;
  15.  
  16. /*
  17.  * main - parse arguments and handle options
  18.  */
  19. main(argc, argv)
  20. int argc;
  21. char *argv[];
  22. {
  23.     int c;
  24.     int errflg = 0;
  25.     extern int optind;
  26.     extern char *optarg;
  27.  
  28.     progname = argv[0];
  29.     while ((c = getopt(argc, argv, "d")) != EOF)
  30.         switch (c) {
  31.         case 'd':
  32.             ++debug;
  33.             break;
  34.         default:
  35.             errflg++;
  36.             break;
  37.         }
  38.     if (errflg) {
  39.         (void) fprintf(stderr, "usage: %s [-d]\n", progname);
  40.         exit(2);
  41.     }
  42.     doit();
  43.     exit(0);
  44. }
  45.  
  46.  
  47. /*
  48.  * Initial permutation table
  49.  */
  50. u_char IP[64] = {
  51.     58, 50, 42, 34, 26, 18, 10,  2,
  52.     60, 52, 44, 36, 28, 20, 12,  4,
  53.     62, 54, 46, 38, 30, 22, 14,  6,
  54.     64, 56, 48, 40, 32, 24, 16,  8,
  55.     57, 49, 41, 33, 25, 17,  9,  1,
  56.     59, 51, 43, 35, 27, 19, 11,  3,
  57.     61, 53, 45, 37, 29, 21, 13,  5,
  58.     63, 55, 47, 39, 31, 23, 15,  7
  59. };
  60.  
  61. /*
  62.  * Inverse initial permutation table
  63.  */
  64. u_char FP[64] = {
  65.     40,  8, 48, 16, 56, 24, 64, 32,
  66.     39,  7, 47, 15, 55, 23, 63, 31,
  67.     38,  6, 46, 14, 54, 22, 62, 30,
  68.     37,  5, 45, 13, 53, 21, 61, 29,
  69.     36,  4, 44, 12, 52, 20, 60, 28,
  70.     35,  3, 43, 11, 51, 19, 59, 27,
  71.     34,  2, 42, 10, 50, 18, 58, 26,
  72.     33,  1, 41,  9, 49, 17, 57, 25
  73. };
  74.  
  75.  
  76. /*
  77.  * Bit order after the operation
  78.  *
  79.  * ((left & 0x55555555) << 1) | (right & 0x55555555)
  80.  */
  81. u_char IPLbits[32] = {
  82.      2, 34,  4, 36,  6, 38,  8, 40,
  83.     10, 42, 12, 44, 14, 46, 16, 48,
  84.     18, 50, 20, 52, 22, 54, 24, 56,
  85.     26, 58, 28, 60, 30, 62, 32, 64
  86. };
  87.  
  88.  
  89. /*
  90.  * Bit order after the operation
  91.  *
  92.  * (left & 0xaaaaaaaa) | ((right & 0xaaaaaaaa) >> 1)
  93.  */
  94. u_char IPRbits[32] = {
  95.      1, 33,  3, 35,  5, 37,  7, 39,
  96.      9, 41, 11, 43, 13, 45, 15, 47,
  97.     17, 49, 19, 51, 21, 53, 23, 55,
  98.     25, 57, 27, 59, 29, 61, 31, 63
  99. };
  100.  
  101.  
  102. /*
  103.  * Bit order after the operation
  104.  *
  105.  * ((left & 0x0f0f0f0f) << 4) | (right & 0x0f0f0f0f)
  106.  */
  107. u_char FPLbits[32] = {
  108.      5,  6,  7,  8, 37, 38, 39, 40,
  109.     13, 14, 15, 16, 45, 46, 47, 48,
  110.     21, 22, 23, 24, 53, 54, 55, 56,
  111.     29, 30, 31, 32, 61, 62, 63, 64
  112. };
  113.  
  114.  
  115. /*
  116.  * Bit order after the operation
  117.  *
  118.  * (left & 0xf0f0f0f0) | ((right & 0xf0f0f0f0) >> 4)
  119.  */
  120. u_char FPRbits[32] = {
  121.      1,  2,  3,  4, 33, 34, 35, 36,
  122.      9, 10, 11, 12, 41, 42, 43, 44,
  123.     17, 18, 19, 20, 49, 50, 51, 52,
  124.     25, 26, 27, 28, 57, 58, 59, 60
  125. };
  126.  
  127.  
  128. /*
  129.  * perm - do a permutation with the given table
  130.  */
  131. perm(databits, permtab, leftp, rightp)
  132.     u_char *databits;
  133.     u_char *permtab;
  134.     u_long *leftp;
  135.     u_long *rightp;
  136. {
  137.     register u_long left;
  138.     register u_long right;
  139.     register u_char *P;
  140.     register u_char *bits;
  141.     register int i;
  142.  
  143.     left = right = 0;
  144.     P = permtab;
  145.     bits = databits;
  146.  
  147.     for (i = 0; i < 32; i++) {
  148.         left <<= 1;
  149.         if (bits[P[i]-1])
  150.             left |= 1;
  151.     }
  152.  
  153.     for (i = 32; i < 64; i++) {
  154.         right <<= 1;
  155.         if (bits[P[i]-1])
  156.             right |= 1;
  157.     }
  158.  
  159.     *leftp = left;
  160.     *rightp = right;
  161. }
  162.  
  163.  
  164. /*
  165.  * doit - make up the tables
  166.  */
  167. doit()
  168. {
  169.     u_char bits[64];
  170.     u_long left;
  171.     u_long right;
  172.     int tabno;
  173.     int i;
  174.     int ind0, ind1, ind2, ind3;
  175.     int ind4, ind5, ind6, ind7;
  176.     int octbits;
  177.  
  178.     bzero((char *)bits, sizeof bits);
  179.  
  180.     /*
  181.      * Do the rounds for the IP table.  We save the results of
  182.      * this as well as printing them.  Note that this is the
  183.      * left-half table, the right half table will be identical.
  184.      */
  185.     printf("static u_long IP[256] = {");
  186.     for (tabno = 0; tabno < 4; tabno++) {
  187.         i = tabno * 8;
  188.         ind7 = IPLbits[i] - 1;
  189.         ind6 = IPLbits[i+1] - 1;
  190.         ind5 = IPLbits[i+2] - 1;
  191.         ind4 = IPLbits[i+3] - 1;
  192.         ind3 = IPLbits[i+4] - 1;
  193.         ind2 = IPLbits[i+5] - 1;
  194.         ind1 = IPLbits[i+6] - 1;
  195.         ind0 = IPLbits[i+7] - 1;
  196.         for (octbits = 0; octbits < 256; octbits++) {
  197.             if (octbits & (1 << 7))
  198.                 bits[ind7] = 1;
  199.             if (octbits & (1 << 6))
  200.                 bits[ind6] = 1;
  201.             if (octbits & (1 << 5))
  202.                 bits[ind5] = 1;
  203.             if (octbits & (1 << 4))
  204.                 bits[ind4] = 1;
  205.             if (octbits & (1 << 3))
  206.                 bits[ind3] = 1;
  207.             if (octbits & (1 << 2))
  208.                 bits[ind2] = 1;
  209.             if (octbits & (1 << 1))
  210.                 bits[ind1] = 1;
  211.             if (octbits & 1)
  212.                 bits[ind0] = 1;
  213.             perm(bits, IP, &left, &right);
  214.             bits[ind7] = 0;
  215.             bits[ind6] = 0;
  216.             bits[ind5] = 0;
  217.             bits[ind4] = 0;
  218.             bits[ind3] = 0;
  219.             bits[ind2] = 0;
  220.             bits[ind1] = 0;
  221.             bits[ind0] = 0;
  222.             if (right != 0) {
  223.                 fprintf(stderr,
  224.                     "IP tabno %d oct %d right not zero\n",
  225.                     tabno, octbits);
  226.                 exit(1);
  227.             }
  228.             if (tabno > 0) {
  229.                 if ((IPL[octbits] << tabno) != left) {
  230.                     fprintf(stderr,
  231.             "IP tabno %d oct %d IP %d left %d, IP != left\n",
  232.                         tabno, octbits, IPL[octbits], left);
  233.                     exit (1);
  234.                 }
  235.             } else {
  236.                 IPL[octbits] = left;
  237.                 if (octbits == 255) {
  238.                     printf(" 0x%08x", left);
  239.                 } else if (octbits & 0x3) {
  240.                     printf(" 0x%08x,", left);
  241.                 } else {
  242.                     printf("\n\t0x%08x,", left);
  243.                 }
  244.             }
  245.         }
  246.         if (tabno == 0)
  247.             printf("\n};\n\n");
  248.     }
  249.  
  250.     /*
  251.      * Next is the FP table, in big endian order
  252.      */
  253.     printf("#if BYTE_ORDER == LITTLE_ENDIAN\nstatic u_long FP[256] = {");
  254.     for (tabno = 3; tabno >= 0; tabno--) {
  255.         i = tabno * 8;
  256.         ind7 = FPLbits[i] - 1;
  257.         ind6 = FPLbits[i+1] - 1;
  258.         ind5 = FPLbits[i+2] - 1;
  259.         ind4 = FPLbits[i+3] - 1;
  260.         ind3 = FPLbits[i+4] - 1;
  261.         ind2 = FPLbits[i+5] - 1;
  262.         ind1 = FPLbits[i+6] - 1;
  263.         ind0 = FPLbits[i+7] - 1;
  264.         for (octbits = 0; octbits < 256; octbits++) {
  265.             if (octbits & (1 << 7))
  266.                 bits[ind7] = 1;
  267.             if (octbits & (1 << 6))
  268.                 bits[ind6] = 1;
  269.             if (octbits & (1 << 5))
  270.                 bits[ind5] = 1;
  271.             if (octbits & (1 << 4))
  272.                 bits[ind4] = 1;
  273.             if (octbits & (1 << 3))
  274.                 bits[ind3] = 1;
  275.             if (octbits & (1 << 2))
  276.                 bits[ind2] = 1;
  277.             if (octbits & (1 << 1))
  278.                 bits[ind1] = 1;
  279.             if (octbits & 1)
  280.                 bits[ind0] = 1;
  281.             perm(bits, FP, &left, &right);
  282.             bits[ind7] = 0;
  283.             bits[ind6] = 0;
  284.             bits[ind5] = 0;
  285.             bits[ind4] = 0;
  286.             bits[ind3] = 0;
  287.             bits[ind2] = 0;
  288.             bits[ind1] = 0;
  289.             bits[ind0] = 0;
  290.             if (right != 0) {
  291.                 fprintf(stderr,
  292.                     "FP tabno %d oct %d right not zero\n",
  293.                     tabno, octbits);
  294.                 exit(1);
  295.             }
  296.             if (tabno != 3) {
  297.                 if ((FPL[octbits] << ((3-tabno)<<1)) != left) {
  298.                     fprintf(stderr,
  299.             "FP tabno %d oct %d FP %x left %x, FP != left\n",
  300.                         tabno, octbits, FPL[octbits], left);
  301.                     exit (1);
  302.                 }
  303.             } else {
  304.                 FPL[octbits] = left;
  305.                 if (octbits == 255) {
  306.                     printf(" 0x%08x", left);
  307.                 } else if (octbits & 0x3) {
  308.                     printf(" 0x%08x,", left);
  309.                 } else {
  310.                     printf("\n\t0x%08x,", left);
  311.                 }
  312.             }
  313.         }
  314.         if (tabno == 3)
  315.             printf("\n};\n");
  316.     }
  317.  
  318.     /*
  319.      * Now reouput the FP table in order appropriate for little
  320.      * endian machines
  321.      */
  322.     printf("#else\nstatic u_long FP[256] = {");
  323.     for (octbits = 0; octbits < 256; octbits++) {
  324.         left = ((FPL[octbits] >> 24) & 0x000000ff)
  325.              | ((FPL[octbits] >>  8) & 0x0000ff00)
  326.              | ((FPL[octbits] <<  8) & 0x00ff0000)
  327.              | ((FPL[octbits] << 24) & 0xff000000);
  328.         if (octbits == 255) {
  329.             printf(" 0x%08x", left);
  330.         } else if (octbits & 0x3) {
  331.             printf(" 0x%08x,", left);
  332.         } else {
  333.             printf("\n\t0x%08x,", left);
  334.         }
  335.     }
  336.     printf("\n};\n#endif\n");
  337. }
  338.